def load_r1(self, fits_file, fits_mode=1, test=False): if not fits_file: self.log.error("No FITS file provided") return False if not os.path.isfile(fits_file): self.log.error("Bad file provided") return False self.log.info("Loading FITS file %s in mode %d", fits_file, fits_mode) if fits_mode == 0: try: f = fits.open(fits_file) except: self.log.error("Invalid FITS file provided") return False sc = self.__load_schema_from_astropy(f, test) f.close() return sc if fits_mode == 1: try: f = File(fits_file) except OSError: self.log.error("Invalid FITS file provided") return False sc = self.__load_schema_from_protozfits(f, test) f.close() return sc
class ZfitsDatafile: def __init__(self, zfits_filename, n_channel=2, n_sample=4320, n_synchro_max=None): self._file = ZfitsFile(zfits_filename) self.generator = self._file.Events self.events = {} self.synchronized = [] self.not_synchronized = [] self.n_channel = n_channel self.n_sample = n_sample event = next(self.generator) while True: self.add_camera_event(event) if n_synchro_max is not None: if len(self.synchronized) >= n_synchro_max: break try: event = next(self.generator) except StopIteration: break def __del__(self): self._file.close() self.events = {} self.synchronized = [] self.not_synchronized = [] def add_camera_event(self, event, disable_different_timing=True): evt_nb = event.eventNumber samples = event.sii.samples assert self.n_sample == np.size(samples) if evt_nb in self.events.keys(): acq = self.events[evt_nb] if event.local_time_nanosec != acq.time_ns or event.local_time_sec != acq.time_s: if disable_different_timing: return print("WARNING: error of", event.local_time_nanosec - acq.time_ns, "ns synchronizing event", evt_nb) acq.set_channel(event.sii.channelId, samples) nrecorded_channel = len(acq.filled_channels) if nrecorded_channel == self.n_channel: self.not_synchronized.remove(evt_nb) self.synchronized.append(evt_nb) elif nrecorded_channel == 1: self.not_synchronized.append(evt_nb) else: self.events[evt_nb] = SiiAcquisition( time_sec=event.local_time_sec, time_nanosec=event.local_time_nanosec, channel=event.sii.channelId, wf=samples, n_channel=self.n_channel) self.not_synchronized.append(evt_nb) def remove(self, evt_nb): self.events.pop(evt_nb) if evt_nb in self.synchronized: self.synchronized.remove(evt_nb) else: self.not_synchronized.remove(evt_nb) def add_sii_acquisition(self, evt_nb, event, disable_different_timing=True): assert isinstance(event, SiiAcquisition) if evt_nb in self.events.keys(): existing = self.events[evt_nb] if event.time_ns != existing.time_ns or event.time_s != existing.time_s: if disable_different_timing: return print("WARNING: error of", event.time_ns - existing.time_ns, "ns synchronizing event", evt_nb) for channel in event.filled_channels: existing.set_channel(channel, event.wf[channel]) if existing.is_synchronized(): self.not_synchronized.remove(evt_nb) self.synchronized.append(evt_nb) else: first_filled_channel = event.filled_channels[0] self.events[evt_nb] = SiiAcquisition( time_sec=event.time_s, time_nanosec=event.time_ns, channel=first_filled_channel, wf=event.wf[first_filled_channel, :], n_channel=self.n_channel) for channel in event.filled_channels[1:]: self.events[evt_nb].set_channel(channel, event.wf[channel, :]) if self.events[evt_nb].is_synchronized(): self.synchronized.append(evt_nb) else: self.not_synchronized.append(evt_nb) def add_datafile(self, datafile): self.generator = datafile.generator for evt_nb in datafile.events.keys(): event = datafile.events[evt_nb] self.add_sii_acquisition(evt_nb, event) def get_batch_synchronized(self, batch_size, remove_events=False): synchronized = self.synchronized if len(synchronized) < batch_size: event = next(self.generator) while len(self.synchronized) < batch_size: self.add_camera_event(event) try: event = next(self.generator) except StopIteration: break if len(synchronized) < batch_size: print( "WARNING: not enough synchronized events to make a full batch") batch_waveform = np.zeros( [len(synchronized), self.n_sample, self.n_channel]) else: batch_waveform = np.zeros( [batch_size, self.n_sample, self.n_channel]) event_read = [] for batch_idx, evt_nb in enumerate(synchronized): if batch_idx == batch_size: break batch_waveform[batch_idx, :, :] = self.events[evt_nb].wf.T event_read.append(evt_nb) if remove_events: for evt_nb in event_read: self.remove(evt_nb) return batch_waveform