glue-viz / glue-astronomy

Plugin to add astronomy-specific functionality to glue
https://glue-astronomy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
11 stars 12 forks source link

Spectrum1D translator cannot handle spectral_axis in 3D cube #60

Closed pllim closed 2 years ago

pllim commented 2 years ago

I am not sure what is the proper fix. I don't want to spend time hacking it only to be told it is wrong. Please advise. Thanks!

import numpy as np
from astropy import units as u
from specutils import Spectrum1D

from glue_astronomy.translators.spectrum1d import Specutils1DHandler

sh = Specutils1DHandler()
spec = Spectrum1D(spectral_axis=np.arange(10) * u.pix, flux=np.ones((2, 2, 10)) * u.Jy)
obj = sh.to_data(spec)
.../glue_astronomy/translators/spectrum1d.py in to_data(self, obj)
    127         # Spectrum1D does it automatically on initialization.
    128         if len(obj.flux.shape) == 3:
--> 129             data = Data(coords=obj.wcs.swapaxes(-1, 0))
    130             data['flux'] = np.swapaxes(obj.flux, -1, 0)
    131             data.get_component('flux').units = str(obj.unit)

AttributeError: 'SpectralGWCS' object has no attribute 'swapaxes'

This is because when I pass spectral_axis into Spectrum1D, it creates a GWCS using https://github.com/astropy/specutils/blob/08de408d18d709a857b67e10ca2752d548eb3100/specutils/utils/wcs_utils.py#L185 and GWCS does not seem to understand swapaxes.

cc @astrofrog @rosteen

This blocks spacetelescope/jdaviz#1040

Also see spacetelescope/gwcs#397

astrofrog commented 2 years ago

If only specutils did not mess with the axis order :laughing:

One solution might be to add a model to the GWCS to reorder the axes, though @nden will have to give advice on this as I'm not familiar enough with GWCS.

nden commented 2 years ago

Here's the relevant discussion in gwcs: https://github.com/spacetelescope/gwcs/issues/269 IIRC I ran into difficulties due to the way the HighLevelWCSAPI was defined in astropy but I don't remember the details.

rosteen commented 2 years ago

@astrofrog I have a draft PR (https://github.com/glue-viz/glue-astronomy/pull/61) open to fix this but it needs more work. I'm going to tag you in some questions there in a moment.

pllim commented 2 years ago

Also might be relevant: