From 5e91bb89ac64b252f9c5cc8f6477d1a34c97fea3 Mon Sep 17 00:00:00 2001 From: "Albert Y. Shih" Date: Fri, 8 Sep 2023 09:27:58 -0400 Subject: [PATCH] Use obstime from observer if obstime not otherwise provided --- changelog/7186.feature.rst | 1 + sunpy/coordinates/ephemeris.py | 12 ------------ sunpy/coordinates/frames.py | 4 ++++ sunpy/coordinates/tests/test_frames.py | 11 +++++++++++ 4 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 changelog/7186.feature.rst diff --git a/changelog/7186.feature.rst b/changelog/7186.feature.rst new file mode 100644 index 00000000000..dfef101eaf3 --- /dev/null +++ b/changelog/7186.feature.rst @@ -0,0 +1 @@ +When creating a coordinate or coordinate frame without specifying ``obstime``, the ``obstime`` value from the ``observer`` frame attribute will be used if present. diff --git a/sunpy/coordinates/ephemeris.py b/sunpy/coordinates/ephemeris.py index 4023ef96d86..47d40627ad9 100644 --- a/sunpy/coordinates/ephemeris.py +++ b/sunpy/coordinates/ephemeris.py @@ -95,18 +95,6 @@ def get_body_heliographic_stonyhurst(body, time='now', observer=None, *, include (63.03105777, -5.20656151, 1.6251161) (d_lon, d_lat, d_radius) in (arcsec / s, arcsec / s, km / s) (-0.02323686, 0.00073376, -1.4798387)> - - Transform that same location and velocity of Mars to a different frame using - `~astropy.coordinates.SkyCoord`. - - >>> from astropy.coordinates import SkyCoord - >>> from sunpy.coordinates import Helioprojective - >>> SkyCoord(mars).transform_to(Helioprojective(observer=earth)) - ): (Tx, Ty, distance) in (arcsec, arcsec, AU) - (-298654.73268523, -21726.6154073, 1.40134156) - (d_Tx, d_Ty, d_distance) in (arcsec / s, arcsec / s, km / s) - (-0.01663438, -0.00058027, -15.08908184)> """ obstime = parse_time(time) diff --git a/sunpy/coordinates/frames.py b/sunpy/coordinates/frames.py index 9a3acecf0b8..4606188946f 100644 --- a/sunpy/coordinates/frames.py +++ b/sunpy/coordinates/frames.py @@ -133,6 +133,10 @@ def __init__(self, *args, **kwargs): if not kwargs.pop('wrap_longitude', True): self._wrap_angle = None + # If obstime is not provided but observer has an obstime, use that as the obstime + if 'obstime' not in kwargs and 'observer' in kwargs and getattr(kwargs['observer'], 'obstime', None) is not None: + kwargs['obstime'] = kwargs['observer'].obstime + super().__init__(*args, **kwargs) # If obstime is specified, treat the default observer (None) as explicitly set diff --git a/sunpy/coordinates/tests/test_frames.py b/sunpy/coordinates/tests/test_frames.py index e65a96fa8b6..b8db7236356 100644 --- a/sunpy/coordinates/tests/test_frames.py +++ b/sunpy/coordinates/tests/test_frames.py @@ -231,6 +231,17 @@ def test_hpc_low_precision_float_warning(): hpc.make_3d() +def test_hpc_obstime_from_observer(): + # Test that observer.obstime is used for obstime if obstime is not provided + observer = HeliographicStonyhurst(obstime='2023-09-08') + hpc = Helioprojective(observer=observer) + assert hpc.obstime == observer.obstime + + # Test that obstime is None if observer does not have an obstime + hpc = Helioprojective(observer='earth') + assert hpc.obstime is None + + # ============================================================================== # ## Heliographic Tests # ==============================================================================