I'm trying to convert some RA and Dec for objects I have in a .fits image into pixel values but every time I run WCS.all_world to pix I get the following error:
NoConvergence: 'WCS.all_world2pix' failed to converge to the requested accuracy.
After 2 iterations, the solution is diverging at least for one input point.
How do I avoid this? I've tried changing the tolerance but nothing seems to help.
This is the code i'm using:
coordinates_ID = zip(ID, RA, Dec)
pixRA = []
pixDec = []
#read in the fits file
insciim = fits.open('/home/myname/image.fits')
#working from a copy to avoid overwriting the images
sciim = np.copy(insciim)
#getting the header from the fits file
header = sciim[0].header
header["CTYPE1"] = "RA---TAN-SIP"
header["CTYPE2"] = "DEC--TAN-SIP"
w = wcs.WCS(insciim[0].header)
for i in coordinates_ID:
pixra, pixdec = w.all_world2pix(i[1], i[2], 1)
pixRA.append(pixra)
pixDec.append(pixdec)
test = (pixRA, pixDec)
size = (200, 200)
cut = Cutout2D(sciim[0].data, test, size).data
plt.imshow(cut, origin='lower', cmap='viridis')
plt.colorbar()
img_file = "/home/myname/test.fits"
img_hdu = fits.PrimaryHDU(cut, header=sciimheader)
img_hdu.writeto(img_file, overwrite=True)
I must state that coordinates_id is 3 zipped tables (like three lists) of the coordinates of objects in my image and the corresponding names. Can anyone spot what could cause me to get the error i'm getting?