def values(self): """ Return the values of each parameter in the structure """ tvalues = [] keys = self.keys() for key in keys: if lalpulsar.PulsarCheckParam(self._pulsarparameters, key): # get type of parameter ptype = lalpulsar.PulsarGetParamType(self._pulsarparameters, key) if ptype == lalpulsar.PULSARTYPE_REAL8_t: value = lalpulsar.PulsarGetREAL8Param( self._pulsarparameters, key) elif ptype == lalpulsar.PULSARTYPE_REAL8Vector_t: tmpvalue = lalpulsar.PulsarGetREAL8VectorParam( self._pulsarparameters, key) value = tmpvalue.data # 'data' in a REAL8Vector gets returned as a numpy array elif ptype == lalpulsar.PULSARTYPE_string_t: value = lalpulsar.PulsarGetStringParam( self._pulsarparameters, key) elif ptype == lalpulsar.PULSARTYPE_UINT4_t: value = lalpulsar.PulsarGetUINT4Param( self._pulsarparameters, key) else: raise ValueError("UINT4-type cannot have an error") else: raise ValueError("Could not find {} in strcuture".format(key)) tvalues.append(value) return tvalues
def __getitem__(self, key): """ Get value from pulsar parameters """ if self._pulsarparameters is None: return None # check if key finishes with "_ERR", in which case check for error value geterr = False tkey = key if key[-4:].upper() == "_ERR": geterr = True tkey = key[:-4] # get the actual parameter key name # check if the key is asking for an individual parameter from a vector parameter # (e.g. 'F0' gets the first value from the 'F' vector) sname = re.sub(r'_\d', '', tkey) if '_' in tkey else re.sub( r'\d', '', tkey) sidx = None indkey = None if sname != tkey: # check additional index is an integer try: sidx = int(tkey.split('_')[-1]) if '_' in tkey else int( tkey[len(sname):]) except ValueError: pass # change tkey for checking parameter exists if sidx is not None: indkey = tkey # key with index tkey = sname # check if parameter key is present otherwise switch back to original name # (needed for, e.g., 'H0', 'PHI0', ...) if not lalpulsar.PulsarCheckParam(self._pulsarparameters, tkey): tkey = indkey # check if parameter given by the key is present if not lalpulsar.PulsarCheckParam(self._pulsarparameters, tkey): return None # get type of parameter ptype = lalpulsar.PulsarGetParamType(self._pulsarparameters, tkey) if ptype == lalpulsar.PULSARTYPE_REAL8_t: if not geterr: value = lalpulsar.PulsarGetREAL8Param(self._pulsarparameters, tkey) else: value = lalpulsar.PulsarGetREAL8ParamErr( self._pulsarparameters, tkey) elif ptype == lalpulsar.PULSARTYPE_REAL8Vector_t: if not geterr: if sidx is None: tmpvalue = lalpulsar.PulsarGetREAL8VectorParam( self._pulsarparameters, tkey) value = tmpvalue.data # 'data' in a REAL8Vector gets returned as a numpy array else: value = lalpulsar.PulsarGetREAL8VectorParamIndividual( self._pulsarparameters, indkey) else: if sidx is None: tmpvalue = lalpulsar.PulsarGetREAL8VectorParamErr( self._pulsarparameters, tkey) value = tmpvalue.data else: value = lalpulsar.PulsarGetREAL8VectorParamErrIndividual( self._pulsarparameters, indkey) elif ptype == lalpulsar.PULSARTYPE_string_t: if not geterr: value = lalpulsar.PulsarGetStringParam(self._pulsarparameters, tkey) else: raise ValueError("String-type cannot have an error") elif ptype == lalpulsar.PULSARTYPE_UINT4_t: if not geterr: value = lalpulsar.PulsarGetUINT4Param(self._pulsarparameters, tkey) else: raise ValueError("UINT4-type cannot have an error") else: raise ValueError("Unrecognised type") return value