Skip to content

Commit

Permalink
Merge pull request #7181 from meeseeksmachine/auto-backport-of-pr-710…
Browse files Browse the repository at this point in the history
…6-on-5.0
  • Loading branch information
nabobalis committed Aug 31, 2023
2 parents add91e5 + f881b6a commit 7c32446
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/7106.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in :func:`~sunpy.coordinates.get_horizons_coord` when specifying a time range via a dictionary that could cause the returned times to be slightly different from the supplied times.
6 changes: 4 additions & 2 deletions sunpy/coordinates/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CartesianRepresentation,
SphericalRepresentation,
)
from astropy.time import Time

from sunpy import log
from sunpy.time import parse_time
Expand Down Expand Up @@ -303,7 +304,7 @@ def get_horizons_coord(body, time='now', id_type=None, *, include_velocity=False
if set(time.keys()) != set(['start', 'stop', 'step']):
raise ValueError('time dictionary must have the keys ["start", "stop", "step"]')
epochs = time
jpl_fmt = '%Y-%m-%d %H:%M:%S'
jpl_fmt = '%Y-%m-%d %H:%M:%S.%f'
epochs['start'] = parse_time(epochs['start']).tdb.strftime(jpl_fmt)
epochs['stop'] = parse_time(epochs['stop']).tdb.strftime(jpl_fmt)
else:
Expand All @@ -326,7 +327,8 @@ def get_horizons_coord(body, time='now', id_type=None, *, include_velocity=False
log.debug(f"See the raw output from the JPL HORIZONS query at {query.uri}")

if isinstance(time, dict):
obstime = parse_time(result['datetime_jd'], format='jd', scale='tdb')
obstime_tdb = parse_time(result['datetime_jd'], format='jd', scale='tdb')
obstime = Time(obstime_tdb, format='isot', scale='utc')
else:
# JPL HORIZONS results are sorted by observation time, so this sorting needs to be undone.
# Calling argsort() on an array returns the sequence of indices of the unsorted list to put the
Expand Down
4 changes: 4 additions & 0 deletions sunpy/coordinates/tests/test_ephemeris.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import pytest
from hypothesis import HealthCheck, given, settings
from numpy.testing import assert_array_equal

import astropy.units as u
from astropy.constants import c as speed_of_light
Expand Down Expand Up @@ -126,6 +127,9 @@ def test_get_horizons_coord_dict_time():
e = get_horizons_coord('Geocenter', time_dict)
e_ref = get_horizons_coord('Geocenter', time_ref)

assert e.obstime.format == 'isot'
assert e.obstime.scale == 'utc'
assert_array_equal(e_ref.obstime.utc.isot, e.obstime.utc.isot)
assert_quantity_allclose(e.lon, e_ref.lon, atol=1e-9*u.deg)
assert_quantity_allclose(e.lat, e_ref.lat)
assert_quantity_allclose(e.radius, e_ref.radius)
Expand Down

0 comments on commit 7c32446

Please sign in to comment.