I'm looking for a way to locate the pixel coordinates on my FITS image that correspond to ra and dec positions of an object in degrees, after oversampling. This would be simple if I wasn't oversampling, but I need to. Given an unaltered FITS image, I can do:
from astropy.wcs import WCS
ra, dec = (43.603, 31.029)
w = WCS('myimage.fits')
x, y = w.all_world2pix(ra, dec, 1) #this gives me the pixel coordinates of the object at (ra, dec) position
However, when I oversample it and THEN try to find the pixel coordinates, it obviously isn't accurate since the (ra, dec) is no longer accurate for the oversampled image. Since I'm oversampling 5x5, I tried simply multiplying my x, y
above by 5. But when I zoom in on this point in ds9, it shows the object off-center, so I don't think this is working. Below is my oversampling part of the code, since it may help to see that. Here, data
is just the 2D numpy array of the data contained in my original FITS image.
from astropy.nddata import Cutout2D
import numpy as np
from scipy import interpolate
def oversample(data_set, N):
size = 120 #pixel size of my box cutout
geom_ctr = (np.shape(data_set)[0]//2, np.shape(data_set)[1]//2)
cutout = Cutout2D(data_set, geom_ctr, size).data
Y, X = np.shape(cutout)
x = np.linspace(0, 0.5, X)
y = np.linspace(0, 0.5, Y)
f = interpolate.interp2d(x, y, cutout, kind='cubic')
Xnew = np.linspace(0, 0.5, X*N)
Ynew = np.linspace(0, 0.5, Y*N)
new_data = f(Xnew, Ynew)
return new_data
resampled_data = oversample(data, 5)
If anyone has any ideas on how I could recover the accurate pixel coordinates after oversampling, that would be great. Thank you!
In principle the solution you describe "multiply x, y by 5" is correct. There must be a bug in your implementation.
You are not showing the code you are using to compute coordinates, so I can only guess at the problem.
Some tips:
cutout.data
that you use in your example, but there's also a cutout.wcs
that represents the WCS of your cutout. If you want to work with the cutout, you have to use that WCS (and not the one from your original image) to transform between world and pixel coordinates. origin=1
in the w.all_world2pix(ra, dec, 1)
call as third argument, then the center of the first pixel will be at pixel position 1. You have to get the x and y interpolation positions to interpolate, as well as the line of code you use to compute coordinates exactly right. It might be easier to pass origin=0.5
, ie put the center of the first pixel at pixel position 0.5 and thus the corner of the image at pixel position 0.0? That way it might be easier to write the lines where to put the interpolation nodes, and to compute the coordinates in the interpolated image correctly. If you separate the task into separate steps or functions (instead of one function which does both cutout and upsample) and use a well-chosen test case where you know the expected output (eg an image that's just a few pixels where you know the expected result, like data = np.array([[1, 2, 3], [4, 5, 6]])
), you'll quickly find and fix the issue in your code that computes the coordinates.
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.