Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading Fermi/gbm fits file in Eventlist.read #894

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions stingray/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .base import StingrayTimeseries
from .filters import get_deadtime_mask
from .gti import generate_indices_of_boundaries
from .io import pi_to_energy, get_file_extension
from .io import pi_to_energy, get_file_extension, read_header_key, read_gbm_data
from .io import FITSTimeseriesReader
from .lightcurve import Lightcurve
from .utils import simon, njit
Expand Down Expand Up @@ -613,9 +613,16 @@ def read(cls, filename, fmt=None, rmf_file=None, **kwargs):
None, which implies no channel->energy conversion at this stage (or a default
calibration applied to selected missions).

source : str, optional
Source type. Currently supported sources: 'GBM'.

emin, emax : float
The minimum and maximum energies to use.

kwargs : dict
Any further keyword arguments to be passed to `load_events_and_gtis`
for reading in event lists in OGIP/HEASOFT format
for reading in event lists in OGIP/HEASOFT format.
The parameters `emin` and `emax` are only used when `source` == 'GBM'.

Returns
-------
Expand All @@ -627,6 +634,14 @@ def read(cls, filename, fmt=None, rmf_file=None, **kwargs):
if fits_ext in get_file_extension(filename).lower():
fmt = "hea"
break

source = kwargs.pop("source", None)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to detect the fermi/gbm files i used a new keyword source

if source == "GBM":
emin = kwargs.pop("emin", None)
emax = kwargs.pop("emax", None)
evt = read_gbm_data(filename, emin=emin, emax=emax)
return evt

if fmt is not None and fmt.lower() in ("hea", "ogip"):
additional_columns = kwargs.pop("additional_columns", None)

Expand Down
51 changes: 51 additions & 0 deletions stingray/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,3 +1550,54 @@ def _can_serialize_meta(probe_file: str, fmt: str) -> bool:
)
yes_it_can = False
return yes_it_can


def read_gbm_data(filename, emin=None, emax=None):
"""
Read Fermi/GBM detector data.

Parameters
----------
filename: str
Path to the fits file, Only TTE files are supported.

emin, emax : float
The minimum and maximum energies to use.

Returns
-------
ev: :class:`EventList` object
The :class:`EventList` object reconstructed from file.
"""
hdulist = fits.open(filename)
header = hdulist[0].header

elower = hdulist[1].data.field("E_MIN")
ehigher = hdulist[1].data.field("E_MAX")
emid = elower + (ehigher - elower) / 2.0

time = hdulist[2].data.field("TIME")
channels = hdulist[2].data.field("PHA")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this only read fermi/gbm TTE data

if emin is None:
emin = np.min(elower)
if emax is None:
emax = np.max(ehigher)
for c in channels:
emid[c]

energy = np.array([emid[c] for c in channels])
mask = (energy > emin) & (energy < emax)

time = time[mask]
energy = energy[mask]
trigtime = hdulist[0].header["TRIGTIME"]

hdulist.close()

# import here to prevent circular import
from stingray.events import EventList

evt = EventList(time, energy, header)
evt.trigtime = trigtime
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now i just associated trigtime with EventList, let me know if anything else needs to be done.

return evt
Loading