create GTI table on the fly when missing, no caching of obs times
@@ -7,7 +7,9 @@ from astropy.coordinates import SkyCoord
from astropy.units import Quantity
from astropy.utils import lazyproperty
from astropy.time import Time
+from astropy.table import Table
from .event_list import EventListChecker
+from .gti import GTI
from ..utils.testing import Checker
from ..utils.fits import earth_location_from_dict
from ..utils.table import table_row_to_dict
@@ -198,7 +200,10 @@ class DataStoreObservation(object):
@property
def gti(self):
"""Load `gammapy.data.GTI` object."""
+ try:
return self.load(hdu_type="gti")
+ except IndexError: # HDU index file does not contain the GTI table. We catch this for backward compatibility.
+ return self._create_missing_gti()
@property
def aeff(self):
@@ -242,18 +247,15 @@ class DataStoreObservation(object):
time = met_ref + met
return time
- @lazyproperty
+ @property
def observation_time_duration(self):
"""Observation time duration in seconds (`~astropy.units.Quantity`).
The wall time, including dead-time.
"""
- try:
return self.gti.time_sum
- except IndexError: # HDU index file does not contain the GTI table
- return Quantity(self.obs_info["ONTIME"], "second")
- @lazyproperty
+ @property
def observation_live_time_duration(self):
"""Live-time duration in seconds (`~astropy.units.Quantity`).
@@ -262,10 +264,7 @@ class DataStoreObservation(object):
Computed as ``t_live = t_observation * (1 - f_dead)``
where ``f_dead`` is the dead-time fraction.
"""
- try:
return self.gti.time_sum * (1 - self.observation_dead_time_fraction)
- except IndexError: # HDU index file does not contain the GTI table
- return Quantity(self.obs_info["LIVETIME"], "second")
@lazyproperty
def observation_dead_time_fraction(self):
@@ -362,6 +361,32 @@ class DataStoreObservation(object):
checker = ObservationChecker(self)
return checker.run(checks=checks)
+ def _create_missing_gti(self):
+ """Returns a GTI table containing TSTART and TSTOP from the obs table meta data.
+
+ This method can be removed once GTI tables are explicitly required in Gammapy.
+ """
+ header = OrderedDict(
+ EXTNAME="GTI",
+ HDUCLASS="GADF",
+ HDUDOC="https://github.com/open-gamma-ray-astro/gamma-astro-data-formats",
+ HDUVERS="0.2",
+ HDUCLAS1="GTI",
+ MJDREFI=self.data_store.obs_table.meta["MJDREFI"],
+ MJDREFF=self.data_store.obs_table.meta["MJDREFF"],
+ TIMEUNIT=self.data_store.obs_table.meta.get("TIMEUNIT", "s"),
+ TIMESYS=self.data_store.obs_table.meta["TIMESYS"],
+ TIMEREF=self.data_store.obs_table.meta.get("TIMEREF", "LOCAL"),
+ )
+
+ start = self.obs_info["TSTART"].astype("float64")
+ stop = self.obs_info["TSTOP"].astype("float64")
+ gti_table = Table(
+ [[start.value], [stop.value]], names=("START", "STOP"), meta=header
+ )
+
+ return GTI(gti_table)
+
class Observations(object):
"""Container class that holds a list of observations.
