I am trying to convert pixel coordinates in celestial coordinates in a fits image (in binary format). My code was working before I have update both python (now python 3.7.1) and astropy (4.0), but now it does not work. It seems my WCS
object has something wrong. This is my WCS
object:
>>> wcs
WCS Keywords
Number of WCS axes: 4
CTYPE : 'DETX_ANG' 'DETY_ANG' 'RA---TAN' 'DEC--TAN'
CRVAL : 0.0 0.0 20.37 -0.365
CRPIX : 0.0 0.0 25921.0 25921.0
PC1_1 PC1_2 PC1_3 PC1_4 : 1.0 0.0 0.0 0.0
PC2_1 PC2_2 PC2_3 PC2_4 : 0.0 1.0 0.0 0.0
PC3_1 PC3_2 PC3_3 PC3_4 : 0.0 0.0 1.0 0.0
PC4_1 PC4_2 PC4_3 PC4_4 : 0.0 0.0 0.0 1.0
CDELT : 1.38888888888889e-05 1.38888888888889e-05 -1.38888888888889e-05 1.38888888888889e-05
NAXIS : 34 4776
My code is the following:
from astropy.wcs import WCS
from astropy.table import Table
from astropy.io import fits
hdul = fits.open(filename.fits)
wcs = WCS(header=hdul[1].header,keysel=['image','binary','pixel'])
events = Table.read(filename, hdu=1)
skyc = utils.pixel_to_skycoord(events.columns['X'], events.columns['Y'], wcs=wcs, origin=1, mode='all')
the error is the following:
skyc = utils.pixel_to_skycoord(events.columns['X'], events.columns['Y'], wcs=wcs, origin=1, mode='all')
File "/Users/Alessandro/anaconda3/lib/python3.7/site-packages/astropy/wcs/utils.py", line 613, in pixel_to_skycoord
wcs = wcs.sub([WCSSUB_LONGITUDE, WCSSUB_LATITUDE])
File "/Users/Alessandro/anaconda3/lib/python3.7/site-packages/astropy/wcs/wcs.py", line 584, in sub
copy.pixel_shape = tuple([None if i is None else self.pixel_shape[i] for i in keep])
File "/Users/Alessandro/anaconda3/lib/python3.7/site-packages/astropy/wcs/wcs.py", line 584, in <listcomp>
copy.pixel_shape = tuple([None if i is None else self.pixel_shape[i] for i in keep])
IndexError: tuple index out of range
It seems like I need only the third and fourth WCS axes (with the CTYPE
keywords 'RA---TAN' and 'DEC--TAN'), but I don't know how to fix it. Any ideas?