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

Fix the way we set date on WCS #7606

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog/7606.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in `~.GenericMap.wcs` where the ``.dateobs`` property of the `~astropy.wcs.WCS` object was being set from the `~.GenericMap.date` property which preferrs the ``DATE-OBS`` header key over the more correct ``DATE-AVG`` if both exist.
16 changes: 13 additions & 3 deletions sunpy/map/mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,23 @@ def wcs(self):
w2.wcs.crval = u.Quantity([self._reference_longitude, self._reference_latitude])
w2.wcs.ctype = self.coordinate_system
w2.wcs.pc = self.rotation_matrix
w2.wcs.set_pv(self._pv_values)
# FITS standard doesn't allow both PC_ij *and* CROTA keywords
w2.wcs.crota = (0, 0)
w2.wcs.cunit = self.spatial_units
w2.wcs.dateobs = self.date.isot
w2.wcs.aux.rsun_ref = self.rsun_meters.to_value(u.m)
w2.wcs.set_pv(self._pv_values)

# If date average exists we should use it. If it doesn't then we
# fallback to dateobs because it can come from many places
if self.date_average is not None:
w2.wcs.dateavg = self.date_average.isot
else:
w2.wcs.dateobs = self.date.isot
if self.date_start is not None:
w2.wcs.datebeg = self.date_start.isot
if self.date_end is not None:
w2.wcs.dateend = self.date_end.isot

w2.wcs.aux.rsun_ref = self.rsun_meters.to_value(u.m)
# Set observer coordinate information except when we know it is not appropriate (e.g., HGS)
sunpy_frame = sunpy.coordinates.wcs_utils._sunpy_frame_class_from_ctypes(w2.wcs.ctype)
if sunpy_frame is None or hasattr(sunpy_frame, 'observer'):
Expand Down
15 changes: 15 additions & 0 deletions sunpy/map/tests/test_mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ def test_date(generic_map, keys, expected_date):
else:
assert generic_map.date == expected_date

# Validate that the WCS date properties reflect the same information as the
# map properties
if "DATE-AVG" in keys or ("DATE-BEG" in keys and "DATE-END" in keys):
assert parse_time(generic_map.wcs.wcs.dateavg) - expected_date < 1*u.s
assert not generic_map.wcs.wcs.dateobs
else:
assert parse_time(generic_map.wcs.wcs.dateobs) - expected_date < 1*u.s

if "DATE-BEG" in keys:
assert generic_map.wcs.wcs.datebeg

if "DATE-END" in keys:
assert generic_map.wcs.wcs.dateend



def test_date_scale(generic_map):
# Check that default time scale is UTC
Expand Down