From a1afc1e4d1dca2880d7d3ae7a69310eb91c586c9 Mon Sep 17 00:00:00 2001 From: Jayraj Dulange Date: Sun, 24 Mar 2024 19:15:31 +0000 Subject: [PATCH 1/4] fixes the solar_rotate_coord --- sunpy/physics/differential_rotation.py | 7 ++++++- sunpy/physics/tests/test_differential_rotation.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sunpy/physics/differential_rotation.py b/sunpy/physics/differential_rotation.py index ff7f33009c9..2afeaaf0969 100644 --- a/sunpy/physics/differential_rotation.py +++ b/sunpy/physics/differential_rotation.py @@ -309,8 +309,13 @@ def solar_rotate_coordinate(coordinate, observer=None, time=None, **diff_rot_kwa # Calculate where the rotated coordinate appears as seen by new observer # for the coordinate system of the input coordinate. The translational # motion of the Sun will be ignored for the transformation. - frame_newobs = coordinate.frame.replicate_without_data(observer=new_observer, + + # Check if the original frame has the observer attribute. + if "observer" in coordinate.frame.frame_attributes.keys(): + frame_newobs = coordinate.frame.replicate_without_data(observer=new_observer, obstime=new_observer.obstime) + else: + frame_newobs = coordinate.frame.replicate_without_data(obstime=new_observer.obstime) with transform_with_sun_center(): return heliographic_rotated.transform_to(frame_newobs) diff --git a/sunpy/physics/tests/test_differential_rotation.py b/sunpy/physics/tests/test_differential_rotation.py index da32bac9908..499ce5328f1 100644 --- a/sunpy/physics/tests/test_differential_rotation.py +++ b/sunpy/physics/tests/test_differential_rotation.py @@ -5,7 +5,7 @@ import astropy.units as u from astropy.coordinates import Longitude, SkyCoord from astropy.tests.helper import assert_quantity_allclose -from astropy.time import TimeDelta +from astropy.time import Time, TimeDelta import sunpy.map from sunpy.coordinates import frames, transform_with_sun_center @@ -171,6 +171,19 @@ def test_solar_rotate_coordinate(): # Test that the SkyCoordinate is Helioprojective assert isinstance(d.frame, frames.Helioprojective) + # Test that the function works correctly with a HGS coordinate. + earth_coord = get_earth(Time("2022-03-30")) + coord_hpc = SkyCoord(100*u.arcsec, 100*u.arcsec, frame=frames.Helioprojective(observer=earth_coord)) + + # Transform the Helioprojective coordinate to a HeliographicStonyhurst coordinate + coord_hgs = coord_hpc.transform_to(frames.HeliographicStonyhurst) + # Call the function with the HeliographicStonyhurst coordinate + with pytest.warns(UserWarning, match="Using 'time' assumes an Earth-based observer"): + rotated_coord_hgs = solar_rotate_coordinate(coord_hgs, time=Time("2022-03-31")) + + # Check that the function returns a coordinate in the same frame as the input coordinate + assert isinstance(rotated_coord_hgs.frame, frames.HeliographicStonyhurst) + def test_consistency_with_rotatedsunframe(): old_observer = frames.HeliographicStonyhurst(10*u.deg, 20*u.deg, 1*u.AU, obstime='2001-01-01') From 7e1d2cfbfacc93af53aa3e0edd806be892b5d3a2 Mon Sep 17 00:00:00 2001 From: Jayraj Dulange Date: Sun, 24 Mar 2024 19:29:26 +0000 Subject: [PATCH 2/4] added changelog --- changelog/7526.bugfix.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog/7526.bugfix.rst diff --git a/changelog/7526.bugfix.rst b/changelog/7526.bugfix.rst new file mode 100644 index 00000000000..2cc61fd6923 --- /dev/null +++ b/changelog/7526.bugfix.rst @@ -0,0 +1,2 @@ +Added a check in `sunpy.physics.differential_rotation.solar_rotate_coordinate` to ensure the input frame has an "observer" attribute before replicating frame +attributes, preventing potential issues with frames lacking this attribute. From 7a502cf05bcfb1b58f5820effef0d0f787a7fcd3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:30:32 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/7526.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/7526.bugfix.rst b/changelog/7526.bugfix.rst index 2cc61fd6923..fdfd26f9b5a 100644 --- a/changelog/7526.bugfix.rst +++ b/changelog/7526.bugfix.rst @@ -1,2 +1,2 @@ -Added a check in `sunpy.physics.differential_rotation.solar_rotate_coordinate` to ensure the input frame has an "observer" attribute before replicating frame +Added a check in `sunpy.physics.differential_rotation.solar_rotate_coordinate` to ensure the input frame has an "observer" attribute before replicating frame attributes, preventing potential issues with frames lacking this attribute. From ac1175b8cb44a53cc524467d52e2e5613f4be212 Mon Sep 17 00:00:00 2001 From: Jayraj Dulange <117574289+Deus1704@users.noreply.github.com> Date: Fri, 24 May 2024 13:33:37 +0530 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Nabil Freij --- sunpy/physics/differential_rotation.py | 1 - sunpy/physics/tests/test_differential_rotation.py | 3 --- 2 files changed, 4 deletions(-) diff --git a/sunpy/physics/differential_rotation.py b/sunpy/physics/differential_rotation.py index 2ce29b7a8ac..fde24a40dc0 100644 --- a/sunpy/physics/differential_rotation.py +++ b/sunpy/physics/differential_rotation.py @@ -256,7 +256,6 @@ def solar_rotate_coordinate(coordinate, observer=None, time=None, **diff_rot_kwa # for the coordinate system of the input coordinate. The translational # motion of the Sun will be ignored for the transformation. - # Check if the original frame has the observer attribute. if "observer" in coordinate.frame.frame_attributes.keys(): frame_newobs = coordinate.frame.replicate_without_data(observer=new_observer, obstime=new_observer.obstime) diff --git a/sunpy/physics/tests/test_differential_rotation.py b/sunpy/physics/tests/test_differential_rotation.py index c69ac4861df..6f9372e872c 100644 --- a/sunpy/physics/tests/test_differential_rotation.py +++ b/sunpy/physics/tests/test_differential_rotation.py @@ -136,13 +136,10 @@ def test_solar_rotate_coordinate(): earth_coord = get_earth(Time("2022-03-30")) coord_hpc = SkyCoord(100*u.arcsec, 100*u.arcsec, frame=frames.Helioprojective(observer=earth_coord)) - # Transform the Helioprojective coordinate to a HeliographicStonyhurst coordinate coord_hgs = coord_hpc.transform_to(frames.HeliographicStonyhurst) - # Call the function with the HeliographicStonyhurst coordinate with pytest.warns(UserWarning, match="Using 'time' assumes an Earth-based observer"): rotated_coord_hgs = solar_rotate_coordinate(coord_hgs, time=Time("2022-03-31")) - # Check that the function returns a coordinate in the same frame as the input coordinate assert isinstance(rotated_coord_hgs.frame, frames.HeliographicStonyhurst)