Skip to content

[ndcube migration] Add a .wcs setter which updates .meta - #8819

Open
Cadair wants to merge 18 commits into
map-refactor/ndcube-basefrom
map-refactor/wcs-setter
Open

Cadair wants to merge 18 commits into
map-refactor/ndcube-basefrom
map-refactor/wcs-setter

Conversation

@Cadair

@Cadair Cadair commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

This functionality is needed to support NDCube methods which modify the WCS (with APE 14 wrappers), i.e. rebin, __getitem__, crop etc.

AI Assistance Disclosure

AI tools were used for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding
  • No AI tools were used

Regardless of AI use, the human contributor remains fully responsible for correctness, design choices, licensing compatibility, and long-term maintainability.

@Cadair
Cadair added this pull request to stack #8810 September 15, 2026 20:50
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from 0060326 to ef2727b Compare September 15, 2026 20:54
@Cadair
Cadair removed this pull request from stack #8810 September 15, 2026 20:57
@Cadair
Cadair changed the base branch from map-refactor/ndcube-constructor to map-refactor/ndcube-base September 15, 2026 20:57
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from ef2727b to c1aa6ad Compare September 15, 2026 20:57
@Cadair
Cadair added this pull request to stack #8820 September 15, 2026 20:57
@Cadair Cadair changed the title Add wcs setter and test with SlicedLowLevelWCS Add a .wcs setter and test with SlicedLowLevelWCS Sep 15, 2026
@Cadair Cadair changed the title Add a .wcs setter and test with SlicedLowLevelWCS Add a .wcs setter which updates .meta Sep 15, 2026
@Cadair Cadair changed the title Add a .wcs setter which updates .meta [ndcube-migration] Add a .wcs setter which updates .meta Sep 15, 2026
@Cadair Cadair added map Affects the map submodule No Backport A PR that isn't to be backported to any release branch. (To be used as a flag to other maintainers) labels Sep 15, 2026
@alasdairwilson alasdairwilson changed the title [ndcube-migration] Add a .wcs setter which updates .meta [ndcube migration] Add a .wcs setter which updates .meta Sep 16, 2026
@samaloney
samaloney force-pushed the map-refactor/wcs-setter branch from 4564caa to 432ee94 Compare October 1, 2026 09:18
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from 432ee94 to 617b012 Compare October 14, 2026 18:15
@alasdairwilson
alasdairwilson marked this pull request as ready for review October 14, 2026 18:26
@samaloney
samaloney force-pushed the map-refactor/wcs-setter branch from 617b012 to c79f74a Compare October 15, 2026 15:03
@Cadair Cadair added the pre-commit.ci autofix Automatically fix pre-commit issues on this PR. label Oct 15, 2026
@pre-commit-ci pre-commit-ci Bot removed the pre-commit.ci autofix Automatically fix pre-commit issues on this PR. label Oct 15, 2026
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from 55476fc to f4a336c Compare October 15, 2026 15:36
@samaloney
samaloney force-pushed the map-refactor/wcs-setter branch from f4a336c to 26ab995 Compare October 16, 2026 09:51
@samaloney
samaloney force-pushed the map-refactor/wcs-setter branch 2 times, most recently from 157e570 to b8864d5 Compare October 16, 2026 15:00
Comment thread pytest.ini
# oldestdeps - asdf-astropy 0.7 uses astropy's deprecated TestRunner on import. This must
# come before any filter that imports sunpy/astropy (e.g. SunpyConnectionWarning) as
# resolving those categories triggers the import and the "error" filter is already active.
ignore:The TestRunner(Base)? class will be deprecated:PendingDeprecationWarning

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the alternative is bump asdf-astropy

@alasdairwilson
alasdairwilson force-pushed the map-refactor/wcs-setter branch from 23018de to de58112 Compare October 20, 2026 21:47
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from de58112 to d1f0cfc Compare October 21, 2026 16:21
Comment thread sunpy/map/mapbase.py Outdated
Comment on lines +698 to +723
# relax=True so non-standard keywords (e.g. SIP) are kept rather than warned about
new_header = unwrapped.to_header(relax=True)
for n in range(1, unwrapped.naxis + 1):
new_header[f"NAXIS{n}"] = unwrapped._naxis[n - 1]
# The new WCS may not have preserved units (e.g. it is in degrees), so
# convert it into the units the metadata already uses.
meta_unit = self.meta.get(f"cunit{n}")
# Sources declare how their CUNIT should be read by overriding
# `spatial_units`, e.g. EIT omits it and LASCO uses a non-FITS 'ARCSEC',
# so convert to that rather than parsing the raw keyword.
target_unit = self.spatial_units[n - 1] if n <= len(self.spatial_units) else None
if target_unit is None and meta_unit is not None:
target_unit = u.Unit(meta_unit)
if meta_unit is None and target_unit is not None:
meta_unit = target_unit.to_string()
header_unit = new_header.get(f"CUNIT{n}")
if target_unit is None or header_unit is None:
continue
factor = u.Unit(header_unit).to(target_unit)
if factor != 1:
keys = [f"CDELT{n}", f"CRVAL{n}"]
keys += [f"CD{n}_{m}" for m in range(1, unwrapped.naxis + 1)]
for key in keys:
if key in new_header:
new_header[key] *= factor
new_header[f"CUNIT{n}"] = meta_unit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samaloney Am I right in thinking that all this section can be removed if we bump an astropy bugfix release or two?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of it still need a special case for EIT I think

@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from d1f0cfc to 89db5f5 Compare October 21, 2026 16:47
@alasdairwilson
alasdairwilson force-pushed the map-refactor/wcs-setter branch 2 times, most recently from 19141be to d55c4fe Compare October 27, 2026 19:07
@Cadair
Cadair force-pushed the map-refactor/wcs-setter branch from ce4e7ba to f164a0a Compare November 3, 2026 16:10
Comment thread sunpy/map/mapbase.py Resolved
Comment thread sunpy/map/mapbase.py Resolved
alasdairwilson and others added 3 commits November 5, 2026 14:25
Replace unit conversion with a guard that rejects differing spatial units
before modifying metadata. Retain source handling for updated GONG scales
and EIT radius calculations.
@@ -0,0 +1,3 @@
`~sunpy.map.GenericMap.wcs` now keeps the units of the map's metadata (usually arcseconds) instead of converting to degrees.
Raw world values, such as ``map.wcs.wcs.cdelt`` or values passed to ``ax.get_transform('world')``, are now in those units.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"values pass to ax.get_transform("world")" is a weird way to write this, we should say "when plotting values with ax.get_transform" or something.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

map Affects the map submodule No Backport A PR that isn't to be backported to any release branch. (To be used as a flag to other maintainers)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants