Skip to content

Commit

Permalink
Merge pull request #853 from StingraySoftware/fix_header
Browse files Browse the repository at this point in the history
Fix header
  • Loading branch information
matteobachetti authored Oct 23, 2024
2 parents 28f2479 + 4802257 commit ba8d06d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ MANIFEST
cov.xml
*coverage*
release.*
build/
dist/
stingray-*/
joss/jats

# Sphinx
docs/api
docs/_build


# Environments
.env
.venv
Expand Down
1 change: 1 addition & 0 deletions docs/changes/853.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue with FITS headers, especially for RXTE data
44 changes: 26 additions & 18 deletions stingray/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,12 @@ def _initialize_header_events(self, fname, force_hduname=None):
EVENTS or the first extension.
"""
hdulist = fits.open(fname)

if not force_hduname:
probe_header = hdulist[0].header
for hdu in hdulist:
if "TELESCOP" in hdu.header or "MISSION" in hdu.header:
probe_header = hdu.header
break
else:
probe_header = hdulist[force_hduname].header

Expand Down Expand Up @@ -944,31 +948,37 @@ def _initialize_header_events(self, fname, force_hduname=None):
hduname = 1
self._add_meta_attr("hduname", hduname)

self._add_meta_attr("header", dict(hdulist[self.hduname].header))
self._add_meta_attr("nphot", self.header["NAXIS2"])
header = hdulist[hduname].header

# self.header has to be a string, for backwards compatibility and... for convenience!
# No need to cope with dicts working badly with Netcdf, for example. The header
# can be saved back and forth to files and be interpreted through
# fits.Header.fromstring(self.header) when needed.
self._add_meta_attr("header", hdulist[self.hduname].header.tostring())
self._add_meta_attr("nphot", header["NAXIS2"])

# These are the important keywords for timing.
ephem = timeref = timesys = None
if "PLEPHEM" in self.header:
if "PLEPHEM" in header:
# For the rare cases where this is a number, e.g. 200, I add `str`
# It's supposed to be a string.
ephem = str(self.header["PLEPHEM"]).strip().lstrip("JPL-").lower()
if "TIMEREF" in self.header:
timeref = self.header["TIMEREF"].strip().lower()
if "TIMESYS" in self.header:
timesys = self.header["TIMESYS"].strip().lower()
ephem = str(header["PLEPHEM"]).strip().lstrip("JPL-").lower()
if "TIMEREF" in header:
timeref = header["TIMEREF"].strip().lower()
if "TIMESYS" in header:
timesys = header["TIMESYS"].strip().lower()
self._add_meta_attr("ephem", ephem)
self._add_meta_attr("timeref", timeref)
self._add_meta_attr("timesys", timesys)

timezero = np.longdouble(0.0)
if "TIMEZERO" in self.header:
timezero = np.longdouble(self.header["TIMEZERO"])
if "TIMEZERO" in header:
timezero = np.longdouble(header["TIMEZERO"])
t_start = t_stop = None
if "TSTART" in self.header:
t_start = np.longdouble(self.header["TSTART"])
if "TSTOP" in self.header:
t_stop = np.longdouble(self.header["TSTOP"])
if "TSTART" in header:
t_start = np.longdouble(header["TSTART"])
if "TSTOP" in header:
t_stop = np.longdouble(header["TSTOP"])
self._add_meta_attr("timezero", timezero)
self._add_meta_attr("t_start", t_start)
self._add_meta_attr("t_stop", t_stop)
Expand All @@ -983,9 +993,7 @@ def _initialize_header_events(self, fname, force_hduname=None):
get_key_from_mission_info(db, "ccol", "NONE", instr, mode),
)

self._add_meta_attr(
"mjdref", np.longdouble(high_precision_keyword_read(self.header, "MJDREF"))
)
self._add_meta_attr("mjdref", np.longdouble(high_precision_keyword_read(header, "MJDREF")))

# Try to get the information needed to calculate the event energy. We start from the
# PI column
Expand Down
3 changes: 3 additions & 0 deletions stingray/mission_support/rxte.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ def rxte_pca_event_file_interpretation(input_data, header=None, hduname=None):
ignored otherwise.
"""
if isinstance(header, str):
header = fits.Header.fromstring(header)

if isinstance(input_data, str):
return rxte_pca_event_file_interpretation(
fits.open(input_data), header=header, hduname=hduname
Expand Down
5 changes: 5 additions & 0 deletions stingray/mission_support/tests/test_mission_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ def test_rxte_pca_event_file_interpretation(self):
new_data = rxte_pca_event_file_interpretation(
fits.open(filename)["XTE_SE"].data, header=fits.open(filename)["XTE_SE"].header
)
new_data_hdr_str = rxte_pca_event_file_interpretation(
fits.open(filename)["XTE_SE"].data,
header=fits.open(filename)["XTE_SE"].header.tostring(),
)

for data in (
new_data,
new_data_hdr_str,
new_hdu.data,
first_new_hdulist["XTE_SE"].data,
second_new_hdulist["XTE_SE"].data,
Expand Down

0 comments on commit ba8d06d

Please sign in to comment.