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

More reliably determine lon/lat from WCS #3116

Merged
merged 3 commits into from May 24, 2019
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
3 changes: 3 additions & 0 deletions changelog/3116.bugfix.rst
@@ -0,0 +1,3 @@
Make a correction to `sunpy.coordinates.wcs_utils.solar_wcs_frame_mapping` so
that `astropy.wcs.WCS` objects are correctly converted to
`sunpy.coordinates.frames` objects irrespective of the ordering of the axes.
9 changes: 9 additions & 0 deletions sunpy/coordinates/tests/test_wcs_utils.py
Expand Up @@ -18,6 +18,15 @@ def test_hpc():
assert isinstance(result, Helioprojective)


def test_hpc_flipped():
wcs = WCS(naxis=2)
wcs.wcs.ctype = ['HPLT', 'HPLN']

result = solar_wcs_frame_mapping(wcs)

assert isinstance(result, Helioprojective)


def test_hgs():
wcs = WCS(naxis=2)
wcs.wcs.ctype = ['HGLN', 'HGLT']
Expand Down
22 changes: 7 additions & 15 deletions sunpy/coordinates/wcs_utils.py
Expand Up @@ -14,7 +14,7 @@ def solar_wcs_frame_mapping(wcs):
type values in the `astropy.wcs.utils.wcs_to_celestial_frame` registry.
"""

dateobs = wcs.wcs.dateobs if wcs.wcs.dateobs else None
dateobs = wcs.wcs.dateobs or None

# SunPy Map adds 'heliographic_observer' and 'rsun' attributes to the WCS
# object. We check for them here, and default to None.
Expand All @@ -28,27 +28,19 @@ def solar_wcs_frame_mapping(wcs):
else:
rsun = None

# First we try the Celestial sub, which rectifies the order.
# It will return anything matching ??LN*, ??LT*
wcss = wcs.sub([WCSSUB_CELESTIAL])
# Truncate the ctype to the first four letters
ctypes = {c[:4] for c in wcs.wcs.ctype}

# If the SUB works, use it.
if wcss.naxis == 2:
wcs = wcss

xcoord = wcs.wcs.ctype[0][0:4]
ycoord = wcs.wcs.ctype[1][0:4]

if xcoord == 'HPLN' and ycoord == 'HPLT':
if {'HPLN', 'HPLT'} <= ctypes:
return Helioprojective(obstime=dateobs, observer=observer, rsun=rsun)

if xcoord == 'HGLN' and ycoord == 'HGLT':
if {'HGLN', 'HGLT'} <= ctypes:
return HeliographicStonyhurst(obstime=dateobs)

if xcoord == 'CRLN' and ycoord == 'CRLT':
if {'CRLN', 'CRLT'} <= ctypes:
return HeliographicCarrington(obstime=dateobs)

if xcoord == 'SOLX' and ycoord == 'SOLY':
if {'SOLX', 'SOLY'} <= ctypes:
return Heliocentric(obstime=dateobs, observer=observer)


Expand Down