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

heasarc: Adding extra parsing for non-fits standard units #2349

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions astroquery/heasarc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Union
import warnings
from io import StringIO, BytesIO
from astropy.table import Table
from astropy.table import QTable, Table
from astropy.io import fits
from astropy import coordinates
from astropy import units as u
Expand All @@ -16,11 +16,17 @@
__all__ = ['Heasarc', 'HeasarcClass']


UNIT_MAPPER = {'DEGREE': u.deg,
'CT': u.count,
'ERG': u.erg,
'ARCSEC': u.arcsec}


def Table_read(*args, **kwargs):
if commons.ASTROPY_LT_5_1:
return Table.read(*args, **kwargs)
return QTable.read(*args, **kwargs)
else:
return Table.read(*args, **kwargs, unit_parse_strict='silent')
return QTable.read(*args, **kwargs, unit_parse_strict='silent')


@async_to_sync
Expand Down Expand Up @@ -250,11 +256,12 @@ def _parse_result(self, response, verbose=False):

try:
data = BytesIO(response.content)
return Table_read(data, hdu=1)
with u.add_enabled_aliases(UNIT_MAPPER):
return Table_read(data, hdu=1)
except ValueError:
try:
return self._fallback(response.text)
except Exception as e:
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a catch-all Exception. I think we should keep this as

Suggested change
except Exception:
except Exception as ex:
print(f"Caught exception {ex}: using fallback")

because I don't like catching generic Exceptions (that can block even user-triggered control-c, for example). It would be better to catch the right exceptions, but without knowing what those are, I suggest we just be noisy instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

(this is also extremely minor and it's fine to ignore me)

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I agree, I have the same annoyance (on top of catching them but don't use the stored item), but didn't know what the expected exception would be from _fallback(). OTOH, here I clearly made this change quite late in the night without reading properly that ex is in fact used 🤦‍♀️

Copy link
Member

Choose a reason for hiding this comment

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

I don't like catching generic Exceptions (that can block even user-triggered control-c, for example)

except: does catch KeyboardInterrupt, but except Exception: does not.

Copy link
Contributor

Choose a reason for hiding this comment

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

🤯 I didn't know that!

Copy link
Member

Choose a reason for hiding this comment

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

return self._old_w3query_fallback(response.content)

def _args_to_payload(self, **kwargs):
Expand Down