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

Backport PR #7106 on branch 5.0 (Fix bug in get_horizons_coord time dictionary arg) #7181

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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'

Check warning on line 307 in sunpy/coordinates/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

sunpy/coordinates/ephemeris.py#L307

Added line #L307 was not covered by tests
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 @@
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')

Check warning on line 331 in sunpy/coordinates/ephemeris.py

View check run for this annotation

Codecov / codecov/patch

sunpy/coordinates/ephemeris.py#L330-L331

Added lines #L330 - L331 were not covered by tests
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