def zoom_fits(fitsfile, scalefactor, preserve_bad_pixels=True, **kwargs):
"""
This function is used to zoom in on a FITS image by interpolating using scipy.ndimage.interpolation.zoom.
It takes the following arguments:
:param fitsfile: the FITS file name
:param scalefactor: the zoom factor along all axes
:param preserve_bad_pixels: try to set NaN pixels to NaN in the zoomed image. Otherwise, bad pixels will be set to
zero.
:param kwargs:
:return:
"""
# Get the data array and the header of the FITS file
arr = pyfits.getdata(fitsfile)
h = pyfits.getheader(fitsfile)
h['CRPIX1'] = (h['CRPIX1']-1)*scalefactor + scalefactor/2. + 0.5
h['CRPIX2'] = (h['CRPIX2']-1)*scalefactor + scalefactor/2. + 0.5
if 'CD1_1' in h:
for ii in (1,2):
for jj in (1,2):
k = "CD%i_%i" % (ii,jj)
if k in h: # allow for CD1_1 but not CD1_2
h[k] = h[k]/scalefactor
elif 'CDELT1' in h:
h['CDELT1'] = h['CDELT1']/scalefactor
h['CDELT2'] = h['CDELT2']/scalefactor
bad_pixels = np.isnan(arr) + np.isinf(arr)
arr[bad_pixels] = 0
upscaled = scipy.ndimage.zoom(arr,scalefactor,**kwargs)
if preserve_bad_pixels:
bp_up = scipy.ndimage.zoom(bad_pixels,scalefactor,mode='constant',cval=np.nan,order=0)
upscaled[bp_up] = np.nan
up_hdu = pyfits.PrimaryHDU(data=upscaled, header=h)
return up_hdu
# -----------------------------------------------------------------
评论列表
文章目录