From 51e739143a559894b566d87135cb471c3e03992f Mon Sep 17 00:00:00 2001 From: Matteo Bachetti Date: Tue, 22 Oct 2024 14:26:16 +0200 Subject: [PATCH 1/5] Update --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index fedd87d62..0081784d6 100644 --- a/.gitignore +++ b/.gitignore @@ -32,11 +32,16 @@ MANIFEST cov.xml *coverage* release.* +build/ +dist/ +stingray-*/ +joss/jats # Sphinx docs/api docs/_build + # Environments .env .venv From 510c3a26eccfd7353cfdbee2cceedb487ef25e24 Mon Sep 17 00:00:00 2001 From: Matteo Bachetti Date: Tue, 22 Oct 2024 15:23:59 +0200 Subject: [PATCH 2/5] Header was supposed to be a string --- stingray/io.py | 39 +++++++++++++++++--------------- stingray/mission_support/rxte.py | 3 +++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/stingray/io.py b/stingray/io.py index a20cedd16..f3a9ebb37 100644 --- a/stingray/io.py +++ b/stingray/io.py @@ -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 @@ -944,31 +948,32 @@ 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._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) @@ -983,9 +988,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 diff --git a/stingray/mission_support/rxte.py b/stingray/mission_support/rxte.py index 50a24efc6..659a77b9d 100644 --- a/stingray/mission_support/rxte.py +++ b/stingray/mission_support/rxte.py @@ -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 From 0d16c1600ea861896021920749c6e0889a558ca9 Mon Sep 17 00:00:00 2001 From: Matteo Bachetti Date: Tue, 22 Oct 2024 15:30:19 +0200 Subject: [PATCH 3/5] Add changelog --- docs/changes/853.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/853.bugfix.rst diff --git a/docs/changes/853.bugfix.rst b/docs/changes/853.bugfix.rst new file mode 100644 index 000000000..c3e29978d --- /dev/null +++ b/docs/changes/853.bugfix.rst @@ -0,0 +1 @@ +Fix issue with FITS headers, especially for RXTE data From 49e36cd8be534831dcc9db597d85280255b95076 Mon Sep 17 00:00:00 2001 From: Matteo Bachetti Date: Tue, 22 Oct 2024 16:10:54 +0200 Subject: [PATCH 4/5] Test header as string --- stingray/mission_support/tests/test_mission_support.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stingray/mission_support/tests/test_mission_support.py b/stingray/mission_support/tests/test_mission_support.py index 3ee8dbec4..aa9e3fe9b 100644 --- a/stingray/mission_support/tests/test_mission_support.py +++ b/stingray/mission_support/tests/test_mission_support.py @@ -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, From 480225749957f0c29dd37f551c4a9595d10dea36 Mon Sep 17 00:00:00 2001 From: Matteo Bachetti Date: Wed, 23 Oct 2024 11:25:29 +0200 Subject: [PATCH 5/5] Comment addition --- stingray/io.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stingray/io.py b/stingray/io.py index f3a9ebb37..6d92f17cd 100644 --- a/stingray/io.py +++ b/stingray/io.py @@ -949,6 +949,11 @@ def _initialize_header_events(self, fname, force_hduname=None): self._add_meta_attr("hduname", hduname) 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"])