Skip to content

Commit 5919666

Browse files
committed
clean up coordinate dict checks
1 parent ecce794 commit 5919666

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

astroquery/jplhorizons/core.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ def __init__(self, id=None, *, location=None, epochs=None,
5151
Parameters
5252
----------
5353
54-
id : str, required
55-
Name, number, or designation of the object to be queried.
54+
id : str or dict, required
55+
Name, number, or designation of target object. Uses the same codes
56+
as JPL Horizons. Arbitrary topocentric coordinates can be added
57+
in a dict. The dict has to be of the form
58+
{``'lon'``: longitude in deg (East positive, West
59+
negative), ``'lat'``: latitude in deg (North positive, South
60+
negative), ``'elevation'``: elevation in km above the reference
61+
ellipsoid, [``'body'``: Horizons body ID of the central body;
62+
optional; if this value is not provided it is assumed that this
63+
location is on Earth]}.
5664
5765
location : str or dict, optional
5866
Observer's location for ephemerides queries or center body name for
@@ -108,11 +116,16 @@ def __init__(self, id=None, *, location=None, epochs=None,
108116
"""
109117

110118
super().__init__()
111-
self.id = id if not isinstance(id, Mapping) else dict(id)
112-
self.location = (
113-
location if not isinstance(location, Mapping) else dict(location)
114-
)
115-
119+
# check & format coordinate dictionaries for id and location; simply
120+
# treat other values as given
121+
if isinstance(id, Mapping):
122+
self.id = self._prep_loc_dict(dict(id), "id")
123+
else:
124+
self.id = id
125+
if isinstance(location, Mapping):
126+
self.location = self._prep_loc_dict(dict(location), "location")
127+
else:
128+
self.location = location
116129
# check for epochs to be dict or list-like; else: make it a list
117130
if epochs is not None:
118131
if isinstance(epochs, (list, tuple, ndarray)):
@@ -541,13 +554,6 @@ def ephemerides_async(self, *, airmass_lessthan=99,
541554
if self.id is None:
542555
raise ValueError("'id' parameter not set. Query aborted.")
543556
elif isinstance(self.id, dict):
544-
if {'lat', 'lon', 'elevation'} - set(self.id.keys()) != set():
545-
raise ValueError(
546-
"dict values for 'id' must contain 'lat', 'lon', "
547-
"'elevation' (and optionally 'body')"
548-
)
549-
if 'body' not in self.id:
550-
self.id['body'] = 399
551557
commandline = (
552558
f"g:{self.id['lon']},{self.id['lat']},"
553559
f"{self.id['elevation']}@{self.id['body']}"
@@ -595,16 +601,6 @@ def ephemerides_async(self, *, airmass_lessthan=99,
595601
('EXTRA_PREC', {True: 'YES', False: 'NO'}[extra_precision])])
596602

597603
if isinstance(self.location, dict):
598-
if (
599-
{'lat', 'lon', 'elevation'} - set(self.location.keys())
600-
) != set():
601-
raise ValueError(
602-
"dict values for 'location' must contain 'lat', 'lon', "
603-
"'elevation' (and optionally 'body')"
604-
)
605-
606-
if 'body' not in self.location:
607-
self.location['body'] = '399'
608604
request_payload['CENTER'] = 'coord@{:s}'.format(
609605
str(self.location['body']))
610606
request_payload['COORD_TYPE'] = 'GEODETIC'
@@ -1150,6 +1146,16 @@ def vectors_async(self, *, get_query_payload=False,
11501146
return response
11511147

11521148
# ---------------------------------- parser functions
1149+
@staticmethod
1150+
def _prep_loc_dict(loc_dict, attr_name):
1151+
if {'lat', 'lon', 'elevation'} - set(loc_dict.keys()) != set():
1152+
raise ValueError(
1153+
f"dict values for '{attr_name}' must contain 'lat', 'lon', "
1154+
"'elevation' (and optionally 'body')"
1155+
)
1156+
if 'body' not in loc_dict:
1157+
loc_dict['body'] = 399
1158+
return loc_dict
11531159

11541160
def _parse_result(self, response, verbose=None):
11551161
"""

0 commit comments

Comments
 (0)