Skip to content

Commit

Permalink
fix for issue astropy#2237: do not cache results that cannot be parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkelley authored and burnout87 committed May 10, 2022
1 parent 67a88c4 commit bdf5883
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
17 changes: 15 additions & 2 deletions astroquery/jplhorizons/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ..query import BaseQuery
# async_to_sync generates the relevant query tools from _async methods
from ..utils import async_to_sync
from ..exceptions import TableParseError
# import configurable items declared in __init__.py
from . import conf

Expand Down Expand Up @@ -1287,11 +1288,23 @@ def _parse_result(self, response, verbose=None):
data : `astropy.Table`
"""
self.last_response = response
if self.query_type not in ['ephemerides', 'elements', 'vectors']:
return None
else:
data = self._parse_horizons(response.text)

try:
data = self._parse_horizons(response.text)
except Exception as ex:
try:
self._last_query.remove_cache_file(self.cache_location)
except OSError:
# this is allowed: if `cache` was set to False, this
# won't be needed
pass
raise TableParseError("Failed to parse JPL Horizons result. "
"The raw response can be found in "
"`self.last_response`. Exception: "
+ str(ex)) from ex
return data


Expand Down
5 changes: 4 additions & 1 deletion astroquery/jplhorizons/tests/data/README
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ ceres_vectors.txt
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=VECTORS&OUT_UNITS=AU-D&COMMAND=%22Ceres%3B%22&CENTER=%27500%4010%27&CSV_FORMAT=%22YES%22&REF_PLANE=ECLIPTIC&REF_SYSTEM=ICRF&TP_TYPE=ABSOLUTE&VEC_LABELS=YES&VEC_CORR=%22NONE%22&VEC_DELTA_T=NO&OBJ_DATA=YES&TLIST=2451544.5

no_H.txt
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=OBSERVER&QUANTITIES=%271%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C15%2C16%2C17%2C18%2C19%2C20%2C21%2C22%2C23%2C24%2C25%2C26%2C27%2C28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38%2C39%2C40%2C41%2C42%2C43%27&COMMAND=%221935+UZ%3B%22&SOLAR_ELONG=%220%2C180%22&LHA_CUTOFF=0&CSV_FORMAT=YES&CAL_FORMAT=BOTH&ANG_FORMAT=DEG&APPARENT=AIRLESS&REF_SYSTEM=ICRF&EXTRA_PREC=NO&CENTER=%27500%40399%27&TLIST=2459480.5004416634&SKIP_DAYLT=NO
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=OBSERVER&QUANTITIES=%271%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C15%2C16%2C17%2C18%2C19%2C20%2C21%2C22%2C23%2C24%2C25%2C26%2C27%2C28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38%2C39%2C40%2C41%2C42%2C43%27&COMMAND=%221935+UZ%3B%22&SOLAR_ELONG=%220%2C180%22&LHA_CUTOFF=0&CSV_FORMAT=YES&CAL_FORMAT=BOTH&ANG_FORMAT=DEG&APPARENT=AIRLESS&REF_SYSTEM=ICRF&EXTRA_PREC=NO&CENTER=%27500%40399%27&TLIST=2459480.5004416634&SKIP_DAYLT=NO

tlist_error.txt
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&COMMAND=%27499%27&OBJ_DATA=%27YES%27&MAKE_EPHEM=%27YES%27&EPHEM_TYPE=%27OBSERVER%27&CENTER=%27500@399%27&QUANTITIES=%271,9,20,23,24,29%27&TLIST=[]
5 changes: 5 additions & 0 deletions astroquery/jplhorizons/tests/data/tlist_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
API VERSION: 1.1
API SOURCE: NASA/JPL Horizons API

BATVAR: no TLIST values found (or missing quotes)
WLDINI: error loading execution-control file.
12 changes: 11 additions & 1 deletion astroquery/jplhorizons/tests/test_jplhorizons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
from astropy.utils.exceptions import AstropyDeprecationWarning

from ...utils.testing_tools import MockResponse
from ...query import AstroQuery
from ...exceptions import TableParseError
from ... import jplhorizons

# files in data/ for different query types
DATA_FILES = {'ephemerides': 'ceres_ephemerides.txt',
'elements': 'ceres_elements.txt',
'vectors': 'ceres_vectors.txt',
'"1935 UZ"': 'no_H.txt'}
'"1935 UZ"': 'no_H.txt',
'"tlist_error"': 'tlist_error.txt'}


def data_path(filename):
Expand Down Expand Up @@ -55,6 +58,13 @@ def patch_request(request):

# --------------------------------- actual test functions

def test_parse_result(patch_request):
q = jplhorizons.Horizons(id='tlist_error')
# need _last_query to be defined
q._last_query = AstroQuery('GET', 'http://dummy')
with pytest.raises(TableParseError):
res = q.ephemerides()


def test_ephemerides_query(patch_request):
# check values of Ceres for a given epoch
Expand Down

0 comments on commit bdf5883

Please sign in to comment.