Skip to content

Commit

Permalink
Make header update work for SLLWCS
Browse files Browse the repository at this point in the history
This doesn't work properly if SIP but 🤷
  • Loading branch information
Cadair committed Apr 24, 2024
1 parent 98f42c9 commit d7ff851
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions sunpy/map/mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,27 @@ def wcs(self):

@wcs.setter
def wcs(self, wcs):
"""
Map uses the meta dict as the source of truth.
When setting the wcs of the map we convert it to a header and then update the header of the map.
"""
# Unwrap any wrapper classes to FITS WCS
unwrapped, _ = unwrap_wcs_to_fitswcs(wcs)

Check warning on line 738 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L738

Added line #L738 was not covered by tests
header = unwrapped.to_header()
changed_header = dict(set(self.wcs.to_header().items()).difference(header.items()))
if {"CUNIT1", "CUNIT2"}.intersection(changed_header.keys()):
raise NotImplementedError("Sorry wcslib needs you to do more programming")
# Convert to a header
new_header = unwrapped.to_header()

Check warning on line 740 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L740

Added line #L740 was not covered by tests
# Reduce the new header to just the keys which differ from the current WCS
# We do this to figure out what's been changed post wcslib doing any
# conversion (such as arcsec -> deg)
changed_header = dict(set(new_header.items()).difference(self.wcs.to_header().items()))

Check warning on line 744 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L744

Added line #L744 was not covered by tests
# If any of the keys in spatial units are modified wcslib will have
# almost certainly changed their units to deg if they were arcsec, so we
# have to explicitly check this and convert them back to the original
# header units to not confuse people.
naughty_key_prefixes = {"CDELT", "CRVAL", "CD"}
for prefix in naughty_key_prefixes:
if any(k.startswith(prefix) for k in changed_header.keys()):
if new_header["CUNIT1"] != self.meta["CUNIT1"]:

Check warning on line 752 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L749-L752

Added lines #L749 - L752 were not covered by tests
raise NotImplementedError("Sorry wcslib needs you to do more programming")
self.meta.update(MetaDict(changed_header))

Check warning on line 754 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L754

Added line #L754 was not covered by tests

# #### Miscellaneous #### #
Expand Down

0 comments on commit d7ff851

Please sign in to comment.