Problems with astropy.wcs and Scamp / TPV

Astropy’s WCS functions are not properly transforming from RA/Dec to image coordinates. I have two images of the same field, but offset by ~10ish pixels each in x and y. If I use ds9 and tell it to align the images by a WCS created with scamp (aligning to the Gaia catalog), the stars line up very well. (Centroids of stars show the same RA and Dec to much better than a pixel.) However, if I convert the RA/Dec to x and y using astropy, the position it gives me on one image is visibly off by a pixel or more, whereas it’s lined up on the other image (the one where I found the star in the first place).

My suspicion is that astropy is not handling TPV distortions correctly. I’m using astropy 5.1 installed just a few minutes ago from PyPI.

The images I’m using have a WCS created by scamp, with CTYPE1 and CTYPE2 equal to ‘RA–TPV’ and ‘DEC-TPV’. What I’ve read suggests that astropy should properly handle this, but that it needs to be compiled with wcslib.

Is the astropy that comes from PyPi compiled with wcslib? Is there a way i can check if the astropy that I’m using is properly compiled with wcslib and supports TPV?

The reason I suspect it’s not getting the right WCS from the header is that if I just print the WCS, it tells me that the CTYPE1 is ‘RA–TAN’:

>>> f[0].header['CTYPE1']
'RA---TPV'
>>> wcs = WCS(f[0].header)
>>> wcs
WCS Keywords

Number of WCS axes: 2
CTYPE : 'RA---TAN'  'DEC--TAN'  
CRVAL : 270.8910025612  -29.53460421215  
CRPIX : 11168.8  8437.0  
CD1_1 CD1_2  : -1.869209278298e-07  7.285681803276e-05  
CD2_1 CD2_2  : -7.285593827158e-05  -1.777778364502e-07  
NAXIS : 2048  4096

Did you manage to figure this one out? I see it’s been a long time but I have a similar issue at the moment. I’m trying to create a wcs object with distortion but I’m failing to figure out how WCS handles TVP or even SIP.

The WCS does not look generally bad.
The CTYPE1/2 define that you have gnomic tangential projection (only). No distortion as such.
It defines that the coordinates are in decimal degrees (e.g. RA not in hours).
Thus these coordinates in CRVAL are 18:03:34 -29:32:05
However, the WCS does not define the coordinates (automatically) in the center of the frame. They are at pixle CRPIX1/2
That is the only thing which looks a bit special - it is far outside the 2048 times 4096 frame - did you use some special centering as you intended to make a mosaic ?
the rotation matrix (the CDn_n values) are much larger in 1_2 and 2_1 than in 1_1, 2_2 - that defines a nearly 90 degrees rotation (however around the point defined wit h CRPIX : 11168.8 8437.0 significantly outside the real frame).
The value of 7.285e-5 corresponds to a scale of 0.26"/pixel (7.285e-5*3600) approximately.
For testing, it might be an idea to use the same frame and do the astrometric solution with astrometry.net (online - asking for center coordinates and high order polynomial (SIP) switched off)). Then you open both versions in overlay in SAOds9 (sites google com/cfa harvard edu/saoimageds9).
Or alternativly you can also load stacks over each other in the aladin tool (aladin cds unistra fr) using “load local file” They should overlap properly.

Cheers
Stefan