Skip to content

Commit

Permalink
Fixed a bug with transform_with_sun_center that assumed that the glob…
Browse files Browse the repository at this point in the history
…al variable would be False
  • Loading branch information
ayshih committed Apr 13, 2020
1 parent 3b4fad6 commit 7a64843
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog/4015.bugfix.rst
@@ -0,0 +1,2 @@
Fixed a bug with :func:`~sunpy.coordinates.transformations.transform_with_sun_center` where the global variable was sometimes restored incorrectly.
This bug was most likely encountered if there was a nested use of this context manager.
11 changes: 10 additions & 1 deletion sunpy/coordinates/tests/test_transformations.py
Expand Up @@ -782,7 +782,7 @@ def test_transform_with_sun_center():


def test_transform_with_sun_center_reset():
# This test sequence ensures that the context manager does not change anything permanently
# This test sequence ensures that the context manager resets propoerly

sun_center = SkyCoord(0*u.deg, 0*u.deg, 0*u.AU,
frame=HeliographicStonyhurst(obstime="2001-01-01"))
Expand All @@ -801,6 +801,15 @@ def test_transform_with_sun_center_reset():
assert_quantity_allclose(result2.lat, sun_center.lat)
assert_quantity_allclose(result2.distance, sun_center.radius)

# Exiting a nested context manager should not affect the outer context manager
with transform_with_sun_center():
with transform_with_sun_center():
pass
result2a = sun_center.transform_to(end_frame)
assert_quantity_allclose(result2a.lon, result2.lon)
assert_quantity_allclose(result2a.lat, result2.lat)
assert_quantity_allclose(result2a.distance, result2.distance)

# After the context manager, the coordinate should have the same result as the first transform
result3 = sun_center.transform_to(end_frame)
assert_quantity_allclose(result3.lon, result1.lon)
Expand Down
4 changes: 3 additions & 1 deletion sunpy/coordinates/transformations.py
Expand Up @@ -114,11 +114,13 @@ def transform_with_sun_center():
try:
global _ignore_sun_motion

old_ignore_sun_motion = _ignore_sun_motion # nominally False

log.debug("Ignore the motion of the center of the Sun for transformations")
_ignore_sun_motion = True
yield
finally:
_ignore_sun_motion = False
_ignore_sun_motion = old_ignore_sun_motion


# Global counter to keep track of the layer of transformation
Expand Down

0 comments on commit 7a64843

Please sign in to comment.