Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in coordinate transform with WCSLib 5 #3891

Closed
astrofrog opened this issue Jun 26, 2015 · 5 comments
Closed

Bug in coordinate transform with WCSLib 5 #3891

astrofrog opened this issue Jun 26, 2015 · 5 comments
Assignees
Labels
Affects-dev PRs and issues that do not impact an existing Astropy release Bug 🔥 Critical wcs
Milestone

Comments

@astrofrog
Copy link
Member

The change to WCSLib 5 has led to a bug in the following use case:

import numpy as np
from astropy.wcs import WCS
from astropy.utils.data import get_pkg_data_filename

wcs = WCS(get_pkg_data_filename('galactic_center/gc_msx_e.fits'))

world = np.array(
    [[-0.58995335, -0.5       ],
     [ 0.00664326, -0.5       ],
     [-0.58995335, -0.25      ],
     [ 0.00664326, -0.25      ],
     [-0.58995335,  0.        ],
     [ 0.00664326,  0.        ],
     [-0.58995335,  0.25      ],
     [ 0.00664326,  0.25      ],
     [-0.58995335,  0.5       ],
     [ 0.00664326,  0.5       ]]
)

pixel = wcs.wcs_world2pix(world, 1)

print(pixel)

Before 7bac470

[[  1.64400000e+02  -1.51498185e-01]
 [  7.49105110e+01  -1.51498185e-01]
 [  1.64400000e+02   3.73485009e+01]
 [  7.49105110e+01   3.73485009e+01]
 [  1.64400000e+02   7.48485000e+01]
 [  7.49105110e+01   7.48485000e+01]
 [  1.64400000e+02   1.12348499e+02]
 [  7.49105110e+01   1.12348499e+02]
 [  1.64400000e+02   1.49848498e+02]
 [  7.49105110e+01   1.49848498e+02]]

After 7bac470

[[  1.64400000e+02  -1.51498185e-01]
 [  1.50906998e+02  -1.51498185e-01]
 [  7.49105110e+01   3.73485009e+01]
 [  1.50906998e+02   3.73485009e+01]
 [  1.64400000e+02   7.48485000e+01]
 [  1.13406999e+02   7.48485000e+01]
 [  7.49105110e+01   1.12348499e+02]
 [  1.13406999e+02   1.12348499e+02]
 [  1.64400000e+02   1.49848498e+02]
 [  7.59070000e+01   1.49848498e+02]]

This was discovered using the WCSAxes test suite: astrofrog/wcsaxes#168

cc @mdboom

@astrofrog astrofrog added Bug Affects-dev PRs and issues that do not impact an existing Astropy release wcs labels Jun 26, 2015
@astrofrog astrofrog added the 🔥 Critical label Jun 26, 2015
@mdboom mdboom added this to the v1.1.0 milestone Jun 26, 2015
@mdboom
Copy link
Contributor

mdboom commented Jun 26, 2015

Wow. That's sure something. Looking into it now.

@cdeil
Copy link
Member

cdeil commented Jun 29, 2015

The change to WCSLib 5 also broke a test in Gammapy:
https://travis-ci.org/gammapy/gammapy/jobs/68707705#L1115

Here's the print output corresponding to this test case that shows that the issue is in the WCS transformations, it doesn't round-trip:

import numpy as np
from astropy.wcs import WCS
from gammapy.image import make_header

header = make_header(nxpix=2, nypix=1, binsz=10, xref=0, yref=0, proj='CAR')
# GLON pixel edges: (+10, 0, -10)
# GLAT pixel edges: (-5, +5)

EPS = 0.1
data = [
        ( 5,  5,  1),        # in image[0, 0]
        ( 0,  0 + EPS,  2),  # in image[1, 0]
        ( 5, -5 + EPS,  3),  # in image[0, 0]
        ( 5,  5 + EPS, 99),  # outside image
        (10 + EPS, 0, 99),   # outside image
        ]
lon, lat, weights = np.array(data).T

wcs = WCS(header)
origin = 0
x, y = wcs.wcs_world2pix(lon, lat, origin)
lon2, lat2 = wcs.wcs_pix2world(x, y, origin)
print(wcs)
print()
print('weights : ', weights)
print('lon     : ', lon)
print('lon2    : ', lon2)
print('lat     : ', lat)
print('lat2    : ', lat2)
print('x       : ', x)
print('y       : ', y)

Output:

WCS Keywords

Number of WCS axes: 2
CTYPE : 'GLON-CAR'  'GLAT-CAR'  
CRVAL : 0.0  0.0  
CRPIX : 1.5  1.0  
PC1_1 PC1_2  : 1.0  0.0  
PC2_1 PC2_2  : 0.0  1.0  
CDELT : -10.0  10.0  
NAXIS    : 2 1

weights :  [  1.   2.   3.  99.  99.]
lon     :  [  5.    0.    5.    5.   10.1]
lon2    :  [ 5.   5.   0.   0.1  5. ]
lat     :  [ 5.   0.1 -4.9  5.1  0. ]
lat2    :  [ 5.   0.1 -4.9  5.1  0. ]
x       :  [ 0.    0.    0.5   0.49  0.  ]
y       :  [ 0.5   0.01 -0.49  0.51  0.  ]

I didn't check if #3893 fixes this case also.

@astrofrog
Copy link
Member Author

@cdeil - if you don't want to try out #3893, you can also just check whether converting the coordinates one by one works (the bug here is related to iteration over coordinate arrays)

@cdeil
Copy link
Member

cdeil commented Jun 29, 2015

#3893 did fix the issue in Gammapy also. Thanks, @mdboom !

@mdboom
Copy link
Contributor

mdboom commented Jun 30, 2015

Fixed by #3893.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Affects-dev PRs and issues that do not impact an existing Astropy release Bug 🔥 Critical wcs
Projects
None yet
Development

No branches or pull requests

3 participants