def get_observations(self): """Fetch observations from the data store according to criteria defined in the configuration.""" self.config.validate() log.info("Fetching observations.") datastore_path = make_path(self.settings["observations"]["datastore"]) if datastore_path.is_file(): self.datastore = DataStore().from_file(datastore_path) elif datastore_path.is_dir(): self.datastore = DataStore().from_dir(datastore_path) else: raise FileNotFoundError(f"Datastore {datastore_path} not found.") ids = [] selection = dict() for criteria in self.settings["observations"]["filters"]: selected_obs = ObservationTable() # TODO: Reduce significantly the code. # This block would be handled by datastore.obs_table.select_observations selection["type"] = criteria["filter_type"] for key, val in criteria.items(): if key in ["lon", "lat", "radius", "border"]: val = Angle(val) selection[key] = val if selection["type"] == "angle_box": selection["type"] = "par_box" selection["value_range"] = Angle(criteria["value_range"]) if selection["type"] == "sky_circle" or selection["type"].endswith( "_box"): selected_obs = self.datastore.obs_table.select_observations( selection) if selection["type"] == "par_value": mask = (self.datastore.obs_table[criteria["variable"]] == criteria["value_param"]) selected_obs = self.datastore.obs_table[mask] if selection["type"] == "ids": obs_list = self.datastore.get_observations(criteria["obs_ids"]) selected_obs["OBS_ID"] = [obs.obs_id for obs in obs_list.list] if selection["type"] == "all": obs_list = self.datastore.get_observations() selected_obs["OBS_ID"] = [obs.obs_id for obs in obs_list.list] if len(selected_obs): if "exclude" in criteria and criteria["exclude"]: exclude = selected_obs["OBS_ID"].tolist() selection = np.isin(ids, exclude) ids = list(np.array(ids)[~selection]) else: ids.extend(selected_obs["OBS_ID"].tolist()) self.observations = self.datastore.get_observations(ids, skip_missing=True) for obs in self.observations.list: log.info(obs)
def make_total_event_list(): """Make total event list. TODO: move this function to the datastore class and the sky box selection to the `make_counts_image` function. """ data_store = DataStore(dir=HESSFITS_MPP) observation_selection = dict(type='sky_box', frame='galactic', lon=Quantity([-120, 70], 'deg'), lat=Quantity([-5, 5], 'deg'), border=Quantity(2, 'deg')) observation_table = data_store.make_observation_table( observation_selection) # For testing, only process a small subset of observations # observation_table = observation_table.select_linspace_subset(num=1000) event_list_files = data_store.make_table_of_files(observation_table, filetypes=['events']) ds = EventListDataset.vstack_from_files(event_list_files['filename']) print('Total number of events: {}'.format(len(ds.event_list))) print('Total number of GTIs: {}'.format(len(ds.good_time_intervals))) print('Converting EventListDataset to HDUList ...') hdu_list = ds.to_fits() print('Writing {}'.format(TOTAL_EVENTS_FILE)) hdu_list.writeto(TOTAL_EVENTS_FILE, clobber=True)