def plot_observations_positions(ax=None, **kwargs): from gammapy.data import ObservationTable from gammapy.spectrum import SpectrumExtraction kwargs.setdefault('marker', 'x') kwargs.setdefault('s', 150) ax = plot_exclusion_mask() if ax is None else ax obs_table = ObservationTable.read(SpectrumExtraction.OBSTABLE_FILE) ra = obs_table['RA_PNT'] dec = obs_table['DEC_PNT'] ax.scatter(ra, dec, transform=ax.get_transform('icrs'), **kwargs)
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)
from astropy.tests.helper import assert_quantity_allclose from gammapy.data import ObservationTable from gammapy.datasets import gammapy_extra from gammapy.spectrum import SpectrumFit, SpectrumObservationGrouping, group_obs_table obs_table_file = gammapy_extra.filename( 'datasets/hess-crab4_pha/observation_table.fits') obs_table = ObservationTable.read(obs_table_file) fit = SpectrumFit.from_observation_table(obs_table) fit.model = 'PL' fit.energy_threshold_low = '1 TeV' fit.energy_threshold_high = '10 TeV' fit.run(method='sherpa') #Use each obs in one group obs_table1 = group_obs_table(obs_table, eff_range=[90, 95], n_eff_bin=5) obs_table1.write('grouped_table1_debug.fits', overwrite=True) grouping = SpectrumObservationGrouping(obs_table1) grouping.run() fit_band2 = SpectrumFit.from_observation_table(grouping.stacked_obs_table) fit_band2.model = 'PL' fit_band2.energy_threshold_low = '1 TeV' fit_band2.energy_threshold_high = '10 TeV' fit_band2.run(method='sherpa') assert_quantity_allclose(fit.result.parameters["index"],
# Setup the logger import logging logging.basicConfig() log = logging.getLogger('gammapy.spectrum') log.setLevel(logging.WARNING) datastore = DataStore.from_dir($HESS_DATA) datastore = DataStore.from_dir("$HESS_DATA") datastore datastore.obs print datastore.obs print datastore.obs_list print datastore.obs_table print datastore.DEFAULT_NAME print datastore.DEFAULT_OBS_TABLE from gammapy.data import DataStore, ObservationTable ObservationTable.select_observations(datastore) get_ipython().magic(u'pinfo ObservationTable.select_observations') get_ipython().magic(u'pinfo datastore.obs') datastore.obs(22222) EXCLUSION_FILE = '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits' allsky_mask = SkyImage.read(EXCLUSION_FILE) exclusion_mask = allsky_mask.cutout( position=on_region.center, size=Angle('6 deg'), ) obs_id = data_store.obs_table['OBS_ID'].data obs_id = datastore.obs_table['OBS_ID'].data obs_id print("Use observations {}".format(obs_id)) datastore.obs_table['OBS_ID'].data datastore.obs_table['OBS_ID']
from astropy.tests.helper import assert_quantity_allclose from gammapy.data import ObservationTable from gammapy.datasets import gammapy_extra from gammapy.spectrum import SpectrumFit, SpectrumGrouping, group_obs_table obs_table_file = gammapy_extra.filename( 'datasets/hess-crab4_pha/observation_table.fits') obs_table = ObservationTable.read(obs_table_file) fit = SpectrumFit.from_observation_table(obs_table) fit.model = 'PL' fit.energy_threshold_low = '1 TeV' fit.energy_threshold_high = '10 TeV' fit.run(method='sherpa') #Use each obs in one group obs_table1 = group_obs_table(obs_table, eff_range=[90, 95], n_eff_bin=5) obs_table1.write('grouped_table1_debug.fits', overwrite=True) grouping = SpectrumGrouping(obs_table1) grouping.run() fit_band2 = SpectrumFit.from_observation_table(grouping.stacked_obs_table) fit_band2.model = 'PL' fit_band2.energy_threshold_low = '1 TeV' fit_band2.energy_threshold_high = '10 TeV' fit_band2.run(method='sherpa') assert_quantity_allclose(fit.result.parameters["index"],
from gammapy.data import ObservationTable intable = ObservationTable.read('observation_table.fits') for row in intable: infile = row['PHAFILE'] phafile = infile.split('/')[-1] outfile = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/ogip_data/{}'.format(phafile) row['PHAFILE'] = outfile intable.write('observation_table.fits', overwrite=True)