question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

astropy handles spectral wcs units with logs incorrectly

See original GitHub issue

EDIT: Solution given at https://github.com/astropy/astropy/issues/11215#issuecomment-755906300

It looks either wcslib or astropy’s wrapping of it do not handle the units on the wcs object correctly, and instead override the spectral units to metres, when "WAVE-LOG" is used (note the change to metres in both the output of pixel_to_world and on the w.wcs object):

from astropy.wcs import WCS
import numpy as np
from astropy.units import Unit
w0 = 3.57880000000000E+00
w1 = 1.00000000000000E-04
crval = 10 ** w0
cdelt = crval * w1 * np.log(10)
cunit = Unit('Angstrom')
ctype = "WAVE-LOG"
w = WCS(naxis=1)
w.wcs.crval[0] = crval
w.wcs.cdelt[0] = cdelt
w.wcs.ctype[0] = ctype
w.wcs.cunit[0] = cunit
print(w)
# OUT: WCS Keywords
# OUT: Number of WCS axes: 1
# OUT: CTYPE : 'WAVE-LOG'  
# OUT: CRVAL : 3791.4034418330666  
# OUT: CRPIX : 0.0  
# OUT: PC1_1  : 1.0  
# OUT: CDELT : 0.8730029046691138  
# OUT: NAXIS : 0  0
print(w.wcs.cunit)
# OUT: ['Angstrom']
print(w.pixel_to_world(np.arange(10)))
# OUT: [3792.27644474 3793.14944764 3794.02245055 3794.89545345 3795.76845636
# OUT:  3796.64145926 3797.51446217 3798.38746507 3799.26046798 3800.13347088] m
print(w.wcs.cunit)
# OUT: ['m']
### 

System Details

>>> import platform; print(platform.platform())
Linux-5.10.0-1-amd64-x86_64-with-glibc2.31
>>> import sys; print("Python", sys.version)
Python 3.9.1 (default, Dec  8 2020, 07:51:42)
[GCC 10.2.0]
>>> import numpy; print("Numpy", numpy.__version__)
Numpy 1.19.5
>>> import astropy; print("astropy", astropy.__version__)
astropy 4.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
aragilarcommented, Jan 7, 2021

Oh, ok 🤦. Thanks for the help @mcara, I didn’t realise I needed to use set(), and was wondering why I was the first person to find the “bug”. I’ll read the docs, and see if there’s a point where I can add a patch which makes the need for calling set() more obvious, so hopefully the next person who tries to do this doesn’t make the same mistake I did.

0reactions
pllimcommented, Jan 7, 2021

I am glad a solution was found. Thanks, @mcara ! @aragilar , if a documentation update is indeed necessary, feel free to re-open the issue or even better, submit a PR. 😸

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for astropy.wcs.wcs
wcs is using the SIP distortion coefficients, therefore the coordinates calculated here might be incorrect. If you do not want to apply the...
Read more >
spectral-cube v0.6.1.dev244+g13f4ea9
However, we do want to keep track of what units the spectral axis # should be returned in, otherwise astropy's WCS can change...
Read more >
astropy.wcs.wcs — Astropy v0.3.dev3797
Wcsprm `: Is a direct wrapper of the core WCS functionality in `wcslib`_. - `~astropy.wcs.Sip`: Handles polynomial distortion as defined in the `SIP`_ ......
Read more >
Astropy Documentation
astropy.units also handles equivalencies, such as that between ... but by passing an equivalency list, in this case spectral(), it does: > ...
Read more >
python - Converting the spectral axis of a FITS file using ...
Then spectral-cube can handle the conversion back to velocity with ... from astropy.io import fits as pyfits from astropy.wcs import WCS ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found