def test_datastore_from_file(): filename = "$GAMMAPY_DATA/hess-dl3-dr1/hess-dl3-dr3-with-background.fits.gz" data_store = DataStore.from_file(filename) obs = data_store.obs(obs_id=23523) # Check that things can be loaded: obs.events obs.bkg
def get_observations(self): """Fetch observations from the data store according to criteria defined in the configuration.""" observations_settings = self.config.observations path = make_path(observations_settings.datastore) if path.is_file(): self.datastore = DataStore.from_file(path) elif path.is_dir(): self.datastore = DataStore.from_dir(path) else: raise FileNotFoundError(f"Datastore not found: {path}") log.info("Fetching observations.") if (len(observations_settings.obs_ids) and observations_settings.obs_file is not None): raise ValueError( "Values for both parameters obs_ids and obs_file are not accepted." ) elif (not len(observations_settings.obs_ids) and observations_settings.obs_file is None): obs_list = self.datastore.get_observations() ids = [obs.obs_id for obs in obs_list] elif len(observations_settings.obs_ids): obs_list = self.datastore.get_observations( observations_settings.obs_ids) ids = [obs.obs_id for obs in obs_list] else: path = make_path(observations_settings.obs_file) ids = list( Table.read(path, format="ascii", data_start=0).columns[0]) if observations_settings.obs_cone.lon is not None: cone = dict( type="sky_circle", frame=observations_settings.obs_cone.frame, lon=observations_settings.obs_cone.lon, lat=observations_settings.obs_cone.lat, radius=observations_settings.obs_cone.radius, border="0 deg", ) selected_cone = self.datastore.obs_table.select_observations(cone) ids = list(set(ids) & set(selected_cone["OBS_ID"].tolist())) self.observations = self.datastore.get_observations(ids, skip_missing=True) if observations_settings.obs_time.start is not None: start = observations_settings.obs_time.start stop = observations_settings.obs_time.stop if len(start.shape) == 0: time_intervals = [(start, stop)] else: time_intervals = [(tstart, tstop) for tstart, tstop in zip(start, stop)] self.observations = self.observations.select_time(time_intervals) log.info(f"Number of selected observations: {len(self.observations)}") for obs in self.observations: log.debug(obs)
def _set_data_store(self): """Set the datastore on the Analysis object.""" path = make_path(self.config.observations.datastore) if path.is_file(): log.debug(f"Setting datastore from file: {path}") self.datastore = DataStore.from_file(path) elif path.is_dir(): log.debug(f"Setting datastore from directory: {path}") self.datastore = DataStore.from_dir(path) else: raise FileNotFoundError(f"Datastore not found: {path}")
def test_datastore_from_file(tmpdir): filename = "$GAMMAPY_DATA/hess-dl3-dr1/hdu-index.fits.gz" index_hdu = fits.open(make_path(filename))["HDU_INDEX"] filename = "$GAMMAPY_DATA/hess-dl3-dr1/obs-index.fits.gz" obs_hdu = fits.open(make_path(filename))["OBS_INDEX"] hdulist = fits.HDUList() hdulist.append(index_hdu) hdulist.append(obs_hdu) filename = tmpdir / "test-index.fits" hdulist.writeto(str(filename)) data_store = DataStore.from_file(filename) assert data_store.obs_table["OBS_ID"][0] == 20136
for idx, model in enumerate(models): hdu = model.to_fits() hdu.name = "BKG{}".format(idx) hdu_list.append(hdu) print([_.name for _ in hdu_list]) import os path = (Path(os.environ["GAMMAPY_DATA"]) / "hess-dl3-dr1/hess-dl3-dr3-with-background.fits.gz") hdu_list.writeto(str(path), overwrite=True) # In[ ]: # Let's see if it's possible to access the data ds2 = DataStore.from_file(path) ds2.info() obs = ds2.obs(20136) obs.events obs.aeff background2d_peek(obs.bkg) # ## Exercises # # - Play with the parameters here (energy binning, offset binning, zenith binning) # - Try to figure out why there are outliers on the zenith vs energy threshold curve. # - Does azimuth angle or optical efficiency have an effect on background rate? # - Use the background models for a 3D analysis (see "hess" notebook).
from gammapy.data import DataStore from gammapy.maps import Map, MapAxis, WcsGeom, WcsNDMap from gammapy.cube import MapMaker, MapFit, PSFKernel from gammapy.cube.models import SkyModel from gammapy.spectrum.models import PowerLaw, ExponentialCutoffPowerLaw from gammapy.image.models import SkyGaussian, SkyPointSource from gammapy.detect import TSMapEstimator from gammapy.scripts import SpectrumAnalysisIACT # ## Data access # # To access the data, we use the `DataStore`, and we use the ``obs_table`` to select the Crab runs. # In[ ]: data_store = DataStore.from_file( "$GAMMAPY_DATA/hess-dl3-dr1/hess-dl3-dr3-with-background.fits.gz") mask = data_store.obs_table["TARGET_NAME"] == "Crab" obs_table = data_store.obs_table[mask] observations = data_store.obs_list(obs_table["OBS_ID"]) # In[ ]: # pos_crab = SkyCoord.from_name('Crab') pos_crab = SkyCoord(83.633, 22.014, unit="deg") # ## Maps # # Let's make some 3D cubes, as well as 2D images. # # For the energy, we make 5 bins from 1 TeV to 10 TeV.