Skip to content

Commit 7de626b

Browse files
authored
Merge pull request #2624 from keflavich/issue2560
Add blank table handling for new returns from heasarc
2 parents a867890 + 0b89749 commit 7de626b

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ casda
8181
- Use the standard ``login`` method for authenticating, which supports the system
8282
keyring [#2386]
8383

84+
heasarc
85+
^^^^^^^
86+
87+
- Fix issue 2560 in which blank tables raised exceptions [#2624]
88+
8489
ipac.nexsci.nasa_exoplanet_archive
8590
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8691

astroquery/heasarc/core.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ def _fallback(self, text):
233233

234234
return Table_read(data, hdu=1)
235235

236+
def _blank_table_fallback(self, data):
237+
"""
238+
In late 2022, we started seeing examples where the null result came
239+
back as a FITS file with an ImageHDU and no BinTableHDU.
240+
"""
241+
with fits.open(data) as fh:
242+
comments = fh[1].header['COMMENT']
243+
emptytable = Table()
244+
emptytable.meta['COMMENT'] = comments
245+
warnings.warn(NoResultsWarning("No matching rows were found in the query."))
246+
return emptytable
247+
236248
def _parse_result(self, response, verbose=False):
237249
# if verbose is False then suppress any VOTable related warnings
238250
if not verbose:
@@ -250,6 +262,10 @@ def _parse_result(self, response, verbose=False):
250262
warnings.warn(NoResultsWarning("No matching rows were found in the query."))
251263
return Table()
252264

265+
if "XTENSION= 'IMAGE '" in response.text:
266+
data = BytesIO(response.content)
267+
return self._blank_table_fallback(data)
268+
253269
try:
254270
data = BytesIO(response.content)
255271
return Table_read(data, hdu=1)
@@ -259,6 +275,7 @@ def _parse_result(self, response, verbose=False):
259275
except Exception:
260276
return self._old_w3query_fallback(response.content)
261277

278+
262279
def _args_to_payload(self, **kwargs):
263280
"""
264281
Generates the payload based on user supplied arguments
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SIMPLE = T / Browse (FitsDisplay V0.2) BITPIX = 8 NAXIS = 0 / Dummy HDU EXTEND = T / Extensions allowed END XTENSION= 'IMAGE ' / Tables not returning data BITPIX = 8 NAXIS = 0 PCOUNT = 0 GCOUNT = 1 COMMENT heasarc_fermilpsc : No matching rows END

astroquery/heasarc/tests/test_heasarc_remote.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
from .conftest import MockResponse, parametrization_local_save_remote, skycoord_3C_273
99

10+
from astroquery.exceptions import NoResultsWarning
11+
12+
from astropy.coordinates import SkyCoord
13+
from astropy import units as u
14+
1015

1116
@parametrization_local_save_remote
1217
class TestHeasarc:
@@ -96,3 +101,23 @@ def test_query_region(self):
96101
skycoord_3C_273, mission=mission, radius="1 degree")
97102

98103
assert len(table) == 63
104+
105+
def test_query_region_nohits(self):
106+
"""
107+
Regression test for #2560: HEASARC returns a FITS file as a null result
108+
"""
109+
heasarc = Heasarc()
110+
111+
with pytest.warns(NoResultsWarning, match='No matching rows were found in the query.'):
112+
# This was an example coordinate that returned nothing
113+
# Since Fermi is still active, it is possible that sometime in the
114+
# future an event will occur here.
115+
table = heasarc.query_region(
116+
SkyCoord(0.28136*u.deg, -0.09789*u.deg, frame='fk5'),
117+
mission='fermilpsc', radius=0.1*u.deg)
118+
119+
assert len(table) == 0
120+
# this is to check that the header comments got parsed correctly
121+
# I'm not certain that they will always be returned in the same order,
122+
# so it may be necessary in the future to change this part of the test
123+
assert 'heasarc_fermilpsc' in table.meta['COMMENT'][0]

0 commit comments

Comments
 (0)