astropy / astropy/specutils

Round tripping read/write does not preserve WCS

Open
#1,096 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
204
Forks
135
Avg merge
12h 55m
Merged PRs (30d)
1

Description

There are some issues with roundtripping of WCS when using Spectrum1D read and write.

If you create a spectrum 1D with a wcs:
from astropy.wcs import WCS
from specutils import Spectrum1D
import astropy.units as u

wcs = WCS(header={ 'CDELT1': 1, 'CRVAL1': 6562.8, 'CUNIT1': 'Angstrom', 'CTYPE1': 'WAVE', 'RESTFRQ': 1400000000, 'CRPIX1': 25})
spec1d = Spectrum1D(spectral_axis = (1, 2, 3)*u.nm, flux=(1, 2, 3)*u.Jy, wcs=wcs)

Use that wcs to transform the spectral axis:
spec1d.wcs.pixel_to_world(spec1d.spectral_axis).value
output: array([6.5398e-07, 6.5408e-07, 6.5418e-07])

Then write out that Spectrum1D to file:
spec1d.write('test_output.fits')

And it back in (either using Spectrum1D.read OR fits.open. Neither work because the header keywords weren't preserved),
the wcs on the resulting Spectrum1D isn't saved.
spec1d.read('test_output.fits').wcs.pixel_to_world(spec1d.spectral_axis.value).value
output: array([ 2., 3., nan])

This issue was discovered when working on https://github.com/astropy/specutils/issues/905#issuecomment-1811320860 at a hack day with @kelle

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Spectrum1D WCS example, then inspect Spectrum1D.read, Spectrum1D.write, and the FITS handling used for the round trip. The issue is done when the WCS header information is preserved and pixel_to_world produces the same result before and after writing and reading the file.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.