Skip to content
forked from sunpy/sunpy

Commit

Permalink
Merge pull request sunpy#3116 from Cadair/fix_wcs-to-frame
Browse files Browse the repository at this point in the history
More reliably determine lon/lat from WCS
  • Loading branch information
nabobalis committed May 24, 2019
1 parent 9c23ebc commit 15ac661
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
3 changes: 3 additions & 0 deletions changelog/3116.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 15ac661

Please sign in to comment.