def CalibrateValue(data, caldict_entry): '''Apply gain / offset units from G3 cal file to register''' uvalue = UnitValue(caldict_entry) g3type = type(data) # make a copy if np.size(data) == 1: data = data.value data2 = np.array(data, dtype='float64') thisdtype = data2.dtype # calibrate units data2 += np.array(caldict_entry['Offset'], dtype=thisdtype) data2 *= np.array(uvalue / caldict_entry['ReciprocalFactor'], dtype=thisdtype) if not data2.shape: data2 = data2.tolist() # if a register has units, it can't be an int anymore. well, actually, # it can't be an int if we're adding floats to it or multiplying it by # floats either, so convert everything that has an entry in the cal file # to float/double. if g3type == core.G3VectorInt: return core.G3VectorDouble(data2) elif g3type == core.G3MapInt: return core.G3MapDouble(data2) elif g3type == core.G3Int: return core.G3Double(data2) else: return g3type(data2)
def to_g3_type(val): if val is None: return c3g.G3String("NONE") elif isinstance(val, (bool)): return c3g.G3Bool(val) elif isinstance(val, (int)): return c3g.G3Int(val) elif isinstance(val, (float)): return c3g.G3Double(val) else: return c3g.G3String(val)
def CalibrateFrame(f, calibration_file=None): '''Apply gain / offset / units from G3 cal file''' if f.type != core.G3FrameType.GcpSlow: return try: if f['Calibrated'] == True: print('Already calibrated!\n') return except KeyError: f['Calibrated'] = True cf = CalFile.CalFileReader() cd = cf.readCalFile(calibration_file) for board in f.keys(): if board == 'Calibrated': continue cboard = copy.deepcopy(f[board]) for rmap in cboard.keys(): for reg in cboard[rmap].keys(): try: rcd = cd[board][rmap][reg] except KeyError: continue rsize = numpy.size(cboard[rmap][reg]) if rsize > 1: rshape = numpy.shape(cboard[rmap][reg]) if len(rshape) > 1: for i in range(rshape[0]): try: rcdi = rcd[i] except KeyError: rcdi = rcd uvalue = UnitValue(rcdi) datatemp = numpy.asarray(cboard[rmap][reg][i]) datatemp2 = datatemp.copy() # if a register has units, it can't be an # int anymore. # well, actually, it can't be an int if # we're adding floats to it or multiplying # it by floats either, so convert # everything that has an entry in the cal # file to float/double. datatemp2 = numpy.asarray(datatemp2, dtype='float64') thisdtype = datatemp2.dtype datatemp2 += \ numpy.array(rcdi['Offset'],dtype=thisdtype) datatemp2 *= numpy.array(uvalue / rcdi['ReciprocalFactor'], dtype=thisdtype) if type(cboard[rmap][reg][i]) \ is core.G3VectorInt: regitemp = core.G3VectorDouble(datatemp2) elif type(cboard[rmap][reg][i]) \ is core.G3MapInt: regitemp = core.G3MapDouble(datatemp2) elif type(cboard[rmap][reg][i]) \ is core.G3Int: regitemp = core.G3Double(datatemp2) else: regitemp = \ (type(cboard[rmap][reg][i]))(datatemp2) cboard[rmap][reg][i] = regitemp else: try: rcdi = rcd[0] except KeyError: rcdi = rcd uvalue = UnitValue(rcdi) datatemp = numpy.asarray(cboard[rmap][reg]) datatemp2 = datatemp.copy() # if a register has units, it can't be an # int anymore. well, actually (see above)... datatemp2 = numpy.asarray(datatemp2, dtype='float64') thisdtype = datatemp2.dtype datatemp2 += \ numpy.array(rcdi['Offset'],dtype=thisdtype) datatemp2 *= numpy.array(uvalue / rcdi['ReciprocalFactor'], dtype=thisdtype) if type(cboard[rmap][reg]) \ is core.G3VectorInt: regtemp = core.G3VectorDouble(datatemp2) elif type(cboard[rmap][reg]) \ is core.G3MapInt: regtemp = core.G3MapDouble(datatemp2) elif type(cboard[rmap][reg]) \ is core.G3Int: regtemp = core.G3Double(datatemp2) else: regtemp = \ (type(cboard[rmap][reg]))(datatemp2) cboard[rmap][reg] = regtemp else: try: rcdi = rcd[0] except KeyError: rcdi = rcd uvalue = UnitValue(rcdi) datatemp = cboard[rmap][reg].value datatemp2 = datatemp # if a register has units, it can't be an # int anymore. well, actually (see above)... datatemp2 = numpy.float(datatemp2) datatemp2 = datatemp2 + rcdi['Offset'] datatemp2 *= uvalue / rcdi['ReciprocalFactor'] if type(cboard[rmap][reg]) \ is core.G3VectorInt: regtemp = core.G3VectorDouble(datatemp2) elif type(cboard[rmap][reg]) \ is core.G3MapInt: regtemp = core.G3MapDouble(datatemp2) elif type(cboard[rmap][reg]) \ is core.G3Int: regtemp = core.G3Double(datatemp2) else: regtemp = \ (type(cboard[rmap][reg]))(datatemp2) cboard[rmap][reg] = regtemp del f[board] f[board] = cboard
def _process_irig_packet(self, parse_index): # Unpack the IRIG data start_ind = parse_index end_ind = start_ind + self._irig_packet_size unpacked_data = np.array( struct.unpack(self._irig_unpack_str, self._data[start_ind:end_ind])) # Unpack the IRIG header ind1 = 0 ind2 = ind1 + self._irig_header_units header = unpacked_data[ind1:ind2][0] if header != self._irig_header: raise RuntimeError("%s: IRIG header error: 0x%x" % (self._error_msg, header)) # Unpack the IRIG clock ind1 = ind2 ind2 = ind1 + self._irig_clock_units clock = unpacked_data[ind1:ind2][0] # Unpack the IRIG clock overflows ind1 = ind2 ind2 = ind1 + self._irig_overflow_units overflow = unpacked_data[ind1:ind2][0] # Adjust the IRIG clock for overflows clock_adjusted = clock + (overflow << self._num_overflow_bits) # Unpack the IRIG info ind1 = ind2 ind2 = ind1 + self._irig_data_length info = unpacked_data[ind1:ind2] # Unpack the IRIG synch pulses ind1 = ind2 ind2 = ind1 + self._irig_data_length synch = unpacked_data[ind1:ind2] # Unpack the IRIG synch pulse overflows ind1 = ind2 ind2 = ind1 + self._irig_data_length synch_overflow = unpacked_data[ind1:ind2] synch_adjusted = (synch + (synch_overflow << self._num_overflow_bits)) # Convert raw IRIG bits to a meaningful time year, yday, hour, mins, secs = self._irig_time_conversion(info) # Obtain the time using the G3 object irig_time = core.G3Time(y=int(year), d=int(yday), h=int(hour), m=int(mins), s=int(secs), ss=0) # no subseconds # Store the time as a G3Int time_s = core.G3Double( float(irig_time.time) / float(core.G3Units.seconds)) # time_s = core.G3UInt(irig_time.time) # Store clock value as a G3UInt clock_adjusted = core.G3UInt(int(clock_adjusted)) # Store the synchronization pulse clock values as a # synch_adjusted = core.G3VectorUInt(np.array(synch_adjusted)) # Create and return a G3 timepoint frame with clock and irig data irig_frame = core.G3Frame(core.G3FrameType.Timepoint) irig_frame['chwp_irig_time'] = time_s irig_frame['chwp_irig_clock'] = clock_adjusted # irig_frame['chwp_irig_synch'] = synch_adjusted return irig_frame