radio-astro-tools / spectral-cube

Library for reading and analyzing astrophysical spectral data cubes
http://spectral-cube.rtfd.org
BSD 3-Clause "New" or "Revised" License
97 stars 65 forks source link

with_spectral_unit doesn't work. with astropy 4.3.1 #747

Closed rfranceschi closed 2 years ago

rfranceschi commented 3 years ago

Python 3.8 Astropy 4.3.1 spectral-cube 0.5.0

I recently updated to astropy 4.3.1 and there is an issue with SpectralCube.with_spectral_unit method. Expected behavior (on astropy 4.2)

import numpy as np
import astropy.wcs
from astropy import units as u
import spectral_cube

wcs_dict = {
   'CTYPE1': 'FREQ    ', 'CUNIT1': 'HZ', 'CDELT1': 0.2, 'CRPIX1': 0, 'CRVAL1': 10, 'NAXIS1': 5,
   'CTYPE2': 'HPLT-TAN', 'CUNIT2': 'deg', 'CDELT2': 0.5, 'CRPIX2': 2, 'CRVAL2': 0.5, 'NAXIS2': 4,
   'CTYPE3': 'HPLN-TAN', 'CUNIT3': 'deg', 'CDELT3': 0.4, 'CRPIX3': 2, 'CRVAL3': 1, 'NAXIS3': 3}
input_wcs = astropy.wcs.WCS(wcs_dict)

mycube = spectral_cube.SpectralCube(data=np.arange(1000).reshape((10,20,-1)), wcs=input_wcs)

#SpectralCube with shape=(5, 20, 10):
# n_x:     10  type_x: HPLN-TAN  unit_x: deg    range:     0.599442 deg:    4.201180 deg
# n_y:     20  type_y: HPLT-TAN  unit_y: deg    range:     0.000013 deg:    9.426829 deg
# n_s:      5  type_s: FREQ      unit_s: Hz     range:       10.200 Hz:      11.000 Hz

mycube.with_spectral_unit(u.km/u.s, velocity_convention="radio", rest_value=10*u.Hz)

#SpectralCube with shape=(5, 20, 10):
# n_x:     10  type_x: HPLN-TAN  unit_x: deg    range:     0.599442 deg:    4.201180 deg
# n_y:     20  type_y: HPLT-TAN  unit_y: deg    range:     0.000013 deg:    9.426829 deg
# n_s:      5  type_s: VRAD      unit_s: km / s  range:   -29979.246 km / s:   -5995.849 km / s

On astropy 4.3.1, the last line crashes with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python3.8/site-packages/spectral_cube/spectral_cube.py", line 1293, in with_spectral_unit
    newwcs,newmeta = self._new_spectral_wcs(unit=unit,
  File ".../python3.8/site-packages/spectral_cube/base_class.py", line 334, in _new_spectral_wcs
    out_ctype = ctype_from_vconv(self._wcs.wcs.ctype[self._wcs.wcs.spec],
  File ".../python3.8/site-packages/spectral_cube/spectral_axis.py", line 119, in determine_ctype_from_vconv
    in_physchar = PHYSICAL_TYPE_TO_CHAR[lin_cunit.physical_type]
KeyError: PhysicalType('frequency')
keflavich commented 3 years ago

Huh, yeah, I can reproduce this. I was sure we'd fixed it. I'll dig.

keflavich commented 3 years ago

We fixed this once before here: https://github.com/radio-astro-tools/spectral-cube/pull/709 so now we have to figure out where the regression has happened.

keflavich commented 3 years ago

Oh, I see, we did fix it. Update to the dev version of spectral cube.

We need to release!

rfranceschi commented 3 years ago

Perfect, thank you for taking the time to check this issue!