I have a need to get the converted x,y pixel values of a matplotlib projection. In particular this is an astropy world coordinate system conversion, from a fits datafile. The data file provides a header which provides the projection information, but I know of no one to use it directly without knowing information I do not have. Here's the current code:
image_detection = fits.open("hst_12311_08_wfc3_uvis_total_drz.fits")['SCI'].data
wlist = fits.open("hst_12311_08_wfc3_uvis_total_drz.fits")['SCI']
w = wcs.WCS(wlist.header)
mean, median, std = sigma_clipped_stats(image_detection, sigma=3.0)
iraffind = IRAFStarFinder(fwhm=3.0, threshold=5*std, exclude_border=True)
sources = iraffind(image_detection - median)
positions = (sources['xcentroid'], sources['ycentroid'])
apertures = CircularAperture(positions, r = 4.0)
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection = w)
ax.imshow(transform(image_detection), cmap='gray_r', origin='lower')
# ax.colorbar()
apertures.plot(color='blue', lw=1.5, alpha=0.5)
plt.savefig("apertures.pdf")
ax.xlabel('Right Ascension')
ax.ylabel('Declination')
plt.show()
What I'm after is those position values, given in x,y, converted to the world coordinates, which are plotted by the projection. I've checked the astropy documentation on WCS and it's not clear how certain values, such as those to do with the origin are obtained. The fits file used is freely available from the Hubble Legacy Archive, though any x,y data should be technically suitable. The fits header contains all of the values for the conversion as mentioned, though I do not fully understand their usage. This is a bit of a long shot here I think, but thank you if you can help.
Once you have the WCS
object then you can use two methods to transform from x,y->RA,Dec or the reverse. These are w.all_pix2world()
and w.all_world2pix()
respectively eg
from astropy.io import fits
from astropy import wcs
wlist = fits.open("hst_12311_08_wfc3_uvis_total_drz.fits")['SCI']
w = wcs.WCS(wlist.header)
# Convert chip center from pixels to RA, Dec
radec_coords = w.all_pix2world(3057, 3045, 1)
print("RA, Dec=", radec_coords[0], radec_coords[1])
# Convert a RA, Dec pair to x,y
coords = [(279.1017383673262, -23.90274423755417)] # CRVAL1, 2 from header
pix_coords = w.all_world2pix(coords, 1)
print("X,Y=", pix_coords[0][0], pix_coords[0][1])
This will produce output (rounding permitting...) of
RA, Dec= 279.10174439, -23.90274424
X,Y= 3057.5, 3045.0
where the RA, Dec are in degrees. It's best to use the all_pix2world
rather than wcs_pix2world
to apply all the transformations (the core WCS transform from pixels to RA,Dec given by the CDi_j
matrix and CRPIXi/CRVALi
, along with any -SIP
polynomials) if there are likely to be image distortions in the image (normally the case for Hubble instruments)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.