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
95 stars 61 forks source link

cube.with_spectral_unit gives KeyError: PhysicalType('frequency') #830

Closed adeleplunkett closed 1 year ago

adeleplunkett commented 2 years ago

Following the examples on https://spectral-cube.readthedocs.io/en/latest/manipulating.html, when we run :

from astropy import units as u cube2 = cube.with_spectral_unit(u.km / u.s, velocity_convention='radio')

we see the following error:


KeyError Traceback (most recent call last) Input In [42], in <cell line: 3>() 1 cube.spectral_axis 2 #cube2 = cube.with_spectral_unit(u.km / u.s) ----> 3 cube2 = cube.with_spectral_unit(u.km / u.s,velocity_convention='radio')
4 cube2.spectral_axis

File /opt/anaconda3/envs/CH/lib/python3.10/site-packages/spectral_cube/spectral_cube.py:1293, in BaseSpectralCube.with_spectral_unit(self, unit, velocity_convention, rest_value) 1269 def with_spectral_unit(self, unit, velocity_convention=None, 1270 rest_value=None): 1271 """ 1272 Returns a new Cube with a different Spectral Axis unit 1273 (...) 1291 1292 """ -> 1293 newwcs,newmeta = self._new_spectral_wcs(unit=unit, 1294 velocity_convention=velocity_convention, 1295 rest_value=rest_value) 1297 if self._mask is not None: 1298 newmask = self._mask.with_spectral_unit(unit, 1299 velocity_convention=velocity_convention, 1300 rest_value=rest_value)

File /opt/anaconda3/envs/CH/lib/python3.10/site-packages/spectral_cube/base_class.py:334, in SpectralAxisMixinClass._new_spectral_wcs(self, unit, velocity_convention, rest_value) 331 meta['Original Unit'] = self._wcs.wcs.cunit[self._wcs.wcs.spec] 332 meta['Original Type'] = self._wcs.wcs.ctype[self._wcs.wcs.spec] --> 334 out_ctype = ctype_from_vconv(self._wcs.wcs.ctype[self._wcs.wcs.spec], 335 unit, 336 velocity_convention=velocity_convention) 338 newwcs = convert_spectral_axis(self._wcs, unit, out_ctype, 339 rest_value=rest_value) 341 newwcs.wcs.set()

File /opt/anaconda3/envs/CH/lib/python3.10/site-packages/spectral_cube/spectral_axis.py:119, in determine_ctype_from_vconv(ctype, unit, velocity_convention) 117 else: 118 lin_cunit = LINEAR_CUNIT_DICT[ctype] --> 119 in_physchar = PHYSICAL_TYPE_TO_CHAR[lin_cunit.physical_type] 121 if unit.physical_type == 'speed': 122 if velocity_convention is None and ctype[0] == 'V': 123 # Special case: velocity <-> velocity doesn't care about convention

KeyError: PhysicalType('frequency')

Any help is greatly appreciated! This works fine on my own (older) version of Python with Spectral-cube, but we get this error on a student's newer version.

keflavich commented 2 years ago

This looks like a duplicate of #709 and #747. Could you report your spectral-cube and astropy versions?

adeleplunkett commented 2 years ago

Thanks for finding those related tickets, I didn't see them because they are "closed". Should have looked!

Should we use dev version of spectral cube?

Will look into the versions. Thanks!

e-koch commented 2 years ago

Which version of spectral-cube do you have now? spectral_cube.version.version?

The newest release is v0.6.0 and should have this issue fixed now.

cch5myv commented 2 years ago

spectral-cube v0.5.0, astropy v5.1, and astropy-healpix v0.6

umitkavak commented 2 years ago

I had the same issue, but the latest release V0.6.0 appears to have fixed it. Thank you very much!

keflavich commented 1 year ago

Since the solution is to upgrade to v0.6, I'm closing this.