Skip to content

Commit 1a91063

Browse files
committed
add coord specification in vectors queries
1 parent 5919666 commit 1a91063

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

astroquery/jplhorizons/core.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def ephemerides_async(self, *, airmass_lessthan=99,
550550

551551
URL = conf.horizons_server
552552

553-
# check for required information
553+
# check for required information and assemble commanddline stub
554554
if self.id is None:
555555
raise ValueError("'id' parameter not set. Query aborted.")
556556
elif isinstance(self.id, dict):
@@ -564,7 +564,7 @@ def ephemerides_async(self, *, airmass_lessthan=99,
564564
self.location = '500@399'
565565
if self.epochs is None:
566566
self.epochs = Time.now().jd
567-
# assemble commandline based on self.id_type
567+
# expand commandline based on self.id_type
568568

569569
if self.id_type in ['designation', 'name',
570570
'asteroid_name', 'comet_name']:
@@ -601,12 +601,9 @@ def ephemerides_async(self, *, airmass_lessthan=99,
601601
('EXTRA_PREC', {True: 'YES', False: 'NO'}[extra_precision])])
602602

603603
if isinstance(self.location, dict):
604-
request_payload['CENTER'] = 'coord@{:s}'.format(
605-
str(self.location['body']))
606-
request_payload['COORD_TYPE'] = 'GEODETIC'
607-
request_payload['SITE_COORD'] = "'{:f},{:f},{:f}'".format(
608-
self.location['lon'], self.location['lat'],
609-
self.location['elevation'])
604+
request_payload = dict(
605+
**request_payload, **self._location_to_params(self.location)
606+
)
610607
else:
611608
request_payload['CENTER'] = "'" + str(self.location) + "'"
612609

@@ -1046,17 +1043,21 @@ def vectors_async(self, *, get_query_payload=False,
10461043

10471044
URL = conf.horizons_server
10481045

1049-
# check for required information
1046+
# check for required information and assemble commandline stub
10501047
if self.id is None:
10511048
raise ValueError("'id' parameter not set. Query aborted.")
1049+
elif isinstance(self.id, dict):
1050+
commandline = (
1051+
f"g:{self.id['lon']},{self.id['lat']},"
1052+
f"{self.id['elevation']}@{self.id['body']}"
1053+
)
1054+
else:
1055+
commandline = str(self.id)
10521056
if self.location is None:
10531057
self.location = '500@10'
10541058
if self.epochs is None:
10551059
self.epochs = Time.now().jd
1056-
1057-
# assemble commandline based on self.id_type
1058-
commandline = str(self.id)
1059-
1060+
# expand commandline based on self.id_type
10601061
if self.id_type in ['designation', 'name',
10611062
'asteroid_name', 'comet_name']:
10621063
commandline = ({'designation': 'DES=',
@@ -1074,18 +1075,17 @@ def vectors_async(self, *, get_query_payload=False,
10741075
commandline += ' CAP{:s};'.format(closest_apparition)
10751076
if no_fragments:
10761077
commandline += ' NOFRAG;'
1077-
1078-
if isinstance(self.location, dict):
1079-
raise ValueError(('cannot use topographic position in state'
1080-
'vectors query'))
1078+
#
1079+
# if isinstance(self.location, dict):
1080+
# raise ValueError(('cannot use topographic position in state'
1081+
# 'vectors query'))
10811082

10821083
# configure request_payload for ephemerides query
10831084
request_payload = OrderedDict([
10841085
('format', 'text'),
10851086
('EPHEM_TYPE', 'VECTORS'),
10861087
('OUT_UNITS', 'AU-D'),
10871088
('COMMAND', '"' + commandline + '"'),
1088-
('CENTER', ("'" + str(self.location) + "'")),
10891089
('CSV_FORMAT', ('"YES"')),
10901090
('REF_PLANE', {'ecliptic': 'ECLIPTIC',
10911091
'earth': 'FRAME',
@@ -1100,7 +1100,12 @@ def vectors_async(self, *, get_query_payload=False,
11001100
('VEC_DELTA_T', {True: 'YES', False: 'NO'}[delta_T]),
11011101
('OBJ_DATA', 'YES')]
11021102
)
1103-
1103+
if isinstance(self.location, dict):
1104+
request_payload = dict(
1105+
**request_payload, **self._location_to_params(self.location)
1106+
)
1107+
else:
1108+
request_payload['CENTER'] = "'" + str(self.location) + "'"
11041109
# parse self.epochs
11051110
if isinstance(self.epochs, (list, tuple, ndarray)):
11061111
request_payload['TLIST'] = "\n".join([str(epoch) for epoch in
@@ -1148,6 +1153,7 @@ def vectors_async(self, *, get_query_payload=False,
11481153
# ---------------------------------- parser functions
11491154
@staticmethod
11501155
def _prep_loc_dict(loc_dict, attr_name):
1156+
"""prepare coord specification dict for 'location' or 'id'"""
11511157
if {'lat', 'lon', 'elevation'} - set(loc_dict.keys()) != set():
11521158
raise ValueError(
11531159
f"dict values for '{attr_name}' must contain 'lat', 'lon', "
@@ -1157,6 +1163,19 @@ def _prep_loc_dict(loc_dict, attr_name):
11571163
loc_dict['body'] = 399
11581164
return loc_dict
11591165

1166+
@staticmethod
1167+
def _location_to_params(loc_dict):
1168+
"""translate a 'location' dict to a dict of request parameters"""
1169+
loc_dict = {
1170+
"CENTER": f"coord@{loc_dict['body']}",
1171+
"COORD_TYPE": "GEODETIC",
1172+
"SITE_COORD": ",".join(
1173+
str(float(loc_dict[k])) for k in ['lat', 'lon', 'elevation']
1174+
)
1175+
}
1176+
loc_dict["SITE_COORD"] = f"'{loc_dict['SITE_COORD']}'"
1177+
return loc_dict
1178+
11601179
def _parse_result(self, response, verbose=None):
11611180
"""
11621181
Parse query result to a `~astropy.table.Table` object.

0 commit comments

Comments
 (0)