# data - DL3 data access and observations¶

## Introduction¶

IACT data is typically structured in “observations”, which define a given time interval during with the instrument response is considered stable.

gammapy.data currently contains the EventList class, as well as classes for IACT data and observation handling.

The main classes in Gammapy to access the DL3 data library are the DataStore and Observation. They are used to store and retrieve dynamically the datasets relevant to any observation (event list in the form of an EventList, IRFs see irf - Instrument response functions and other relevant information).

Once some observation selection has been selected, the user can build a list of observations: a Observations object, which will be used for the data reduction process.

## Getting started¶

You can use the EventList class to load IACT gamma-ray event lists:

from gammapy.data import EventList
filename = '$GAMMAPY_DATA/hess-dl3-dr1/data/hess_dl3_dr1_obs_id_023523.fits.gz' events = EventList.read(filename)  To load Fermi-LAT event lists, use the EventListLAT class: from gammapy.data import EventList filename = "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-events.fits.gz"


The other main class in gammapy.data is the DataStore, which makes it easy to load IACT data. E.g. an alternative way to load the events for observation ID 23523 is this:

from gammapy.data import DataStore
data_store = DataStore.from_dir('$GAMMAPY_DATA/hess-dl3-dr1') events = data_store.obs(23523).events  ## The index tables¶ A typical way to organize the files relevant to the data we are interested in are the index tables. There are two tables: • Observation index table: this table collects the information on each observation or run, with meta data about each of them, such as the pointing direction, the duration, the run ID… • HDU index table: this table provides, for each observation listed in the index table, the location of the corresponding data and instrument response files. A DataStore can then be created by providing each of these two tables in the same file with from_file(), or instead by the directory where they can be found with from_dir() as shown above. More details on these tables and their content can be found in https://gamma-astro-data-formats.readthedocs.io/en/latest/data_storage/index.html. ## Working with event lists¶ To take a quick look at the events inside the list, one can use peek() from gammapy.data import EventList filename = '$GAMMAPY_DATA/hess-dl3-dr1/data/hess_dl3_dr1_obs_id_023523.fits.gz'
events.peek()


(png, hires.png, pdf)

Events can be selected based on any of their properties, with dedicated functions existing for energy, time, offset from pointing position and the selection of events in a particular region of the sky.

import astropy.units as u
from astropy.time import Time
from gammapy.data import EventList
filename_2 = '$GAMMAPY_DATA/hess-dl3-dr1/data/hess_dl3_dr1_obs_id_023526.fits.gz' events_1 = EventList.read(filename_1) events_2 = EventList.read(filename_2) gti_1 = GTI.read(filename_1) gti_2 = GTI.read(filename_2) # stack in place, now the _1 object contains the information of both gti_1.stack(gti_2) events_1.stack(events_2) # or instead create a new event list from the other two combined_events = EventList.from_stack([events_1, events_2])  ## Writing event lists and GTIs to file¶ To write the events or GTIs separately, one can just save the underlying astropy.table.Table. However, it is usually best to save the events and their associated GTIs together in the same FITS file. from gammapy.data import EventList, GTI filename = "$GAMMAPY_DATA/hess-dl3-dr1/data/hess_dl3_dr1_obs_id_023523.fits.gz"

# Save separately
events.table.meta = {"extname": "EVENTS"}
events.table.write("test_events.fits.gz")
gti.write("test_gti.fits.gz")

# Save together
from astropy.io import fits
primary_hdu = fits.PrimaryHDU()
events_hdu = fits.BinTableHDU(data=events.table, name="events")
gti_hdu = fits.BinTableHDU(gti.table, name="gti")
hdulist = fits.HDUList([primary_hdu, events_hdu, gti_hdu])
hdulist.writeto("test_together.fits.gz")


## Using gammapy.data¶

Gammapy tutorial notebooks that show examples using gammapy.data:

## Reference/API¶

### gammapy.data Package¶

Data and observation handling.

#### Classes¶

 DataStore([hdu_table, obs_table]) IACT data store. EventList(table) Event list. IACT array pointing info. GTI(table) Good time intervals (GTI) Table. HDUIndexTable([data, masked, names, dtype, …]) HDU index table. Observation([obs_id, obs_info, gti, aeff, …]) In-memory observation. ObservationFilter([time_filter, event_filters]) Holds and applies filters to observation data. ObservationTable([data, masked, names, …]) Observation table. Observations([observations]) Container class that holds a list of observations. PointingInfo(table) IACT array pointing info.