这个问题可能有点专家,但是希望有人可以提供帮助。我通常使用IDL,但为了开发管道,我希望使用python来改善运行时间。

我的fits文件处理设置如下:

import numpy as numpy
from astropy.io import fits

#Directory: /Users/UCL_Astronomy/Documents/UCL/PHASG199/M33_UVOT_sum/UVOTIMSUM/M33_sum_epoch1_um2_norm.img
with fits.open('...') as ima_norm_um2:
    #Open UVOTIMSUM file once and close it after extracting the relevant values:
    ima_norm_um2_hdr  = ima_norm_um2[0].header
    ima_norm_um2_data = ima_norm_um2[0].data
    #Individual dimensions for number of x pixels and number of y pixels:
    nxpix_um2_ext1 = ima_norm_um2_hdr['NAXIS1']
    nypix_um2_ext1 = ima_norm_um2_hdr['NAXIS2']
    #Compute the size of the images (you can also do this manually rather than calling these keywords from the header):
    #Call the header and data from the UVOTIMSUM file with the relevant keyword extensions:
    corrfact_um2_ext1 = numpy.zeros((ima_norm_um2_hdr['NAXIS2'], ima_norm_um2_hdr['NAXIS1']))
    coincorr_um2_ext1 = numpy.zeros((ima_norm_um2_hdr['NAXIS2'], ima_norm_um2_hdr['NAXIS1']))

#Check that the dimensions are all the same:
print(corrfact_um2_ext1.shape)
print(coincorr_um2_ext1.shape)
print(ima_norm_um2_data.shape)

# Make a new image file to save the correction factors:
hdu_corrfact = fits.PrimaryHDU(corrfact_um2_ext1, header=ima_norm_um2_hdr)
fits.HDUList([hdu_corrfact]).writeto('.../M33_sum_epoch1_um2_corrfact.img')

# Make a new image file to save the corrected image to:
hdu_coincorr = fits.PrimaryHDU(coincorr_um2_ext1, header=ima_norm_um2_hdr)
fits.HDUList([hdu_coincorr]).writeto('.../M33_sum_epoch1_um2_coincorr.img')


我期待然后进行以下更正:

    # Define the variables from Poole et al. (2008) "Photometric calibration of the Swift ultraviolet/optical telescope":

alpha =  0.9842000
ft    =  0.0110329
a1    =  0.0658568
a2    = -0.0907142
a3    =  0.0285951
a4    =  0.0308063

for i in range(nxpix_um2_ext1 - 1): #do begin
    for j in range(nypix_um2_ext1 - 1): #do begin
        if (numpy.less_equal(i, 4) | numpy.greater_equal(i, nxpix_um2_ext1-4) | numpy.less_equal(j, 4) | numpy.greater_equal(j, nxpix_um2_ext1-4)): #then begin
            #UVM2
            corrfact_um2_ext1[i,j] == 0
            coincorr_um2_ext1[i,j] == 0
        else:
            xpixmin = i-4
            xpixmax = i+4
            ypixmin = j-4
            ypixmax = j+4
            #UVM2
            ima_UVM2sum = total(ima_norm_um2[xpixmin:xpixmax,ypixmin:ypixmax])
            xvec_UVM2 = ft*ima_UVM2sum
            fxvec_UVM2 = 1 + (a1*xvec_UVM2) + (a2*xvec_UVM2*xvec_UVM2) + (a3*xvec_UVM2*xvec_UVM2*xvec_UVM2) + (a4*xvec_UVM2*xvec_UVM2*xvec_UVM2*xvec_UVM2)
            Ctheory_UVM2 = - alog(1-(alpha*ima_UVM2sum*ft))/(alpha*ft)
            corrfact_um2_ext1[i,j] = Ctheory_UVM2*(fxvec_UVM2/ima_UVM2sum)
            coincorr_um2_ext1[i,j] = corrfact_um2_ext1[i,j]*ima_sk_um2[i,j]


上面的代码片段很混乱,因为我混合使用了IDL语法和python语法。我只是不确定如何将IDL的某些方面转换为python。例如,ima_UVM2sum = total(ima_norm_um2[xpixmin:xpixmax,ypixmin:ypixmax])我不太确定如何处理。

我要说的是,我也缺少它将更新校正因子和重合校正图像文件的部分。如果有人可以耐心地用细齿梳子梳理一下,并提出必要的改动,那我将是非常好的。

原始规范化图像可以在此处下载:Replace ... in above code with this file

最佳答案

关于numpy的一个非常重要的事情是,它在元素基础上执行所有数学或比较功能。因此,您可能不需要遍历数组。

因此,也许从convolvesum-filter开始您的图片。可以通过astropy.convolution.convolvescipy.ndimage.filters.uniform_filter对2D图像进行此操作

我不确定您要什么,但我想您需要一个9x9的求和过滤器,该过滤器可以通过

from scipy.ndimage.filters import uniform_filter
ima_UVM2sum = uniform_filter(ima_norm_um2_data, size=9)


由于您要丢弃边界处的任何像素(4个像素),因此可以简单地slice将其删除:

ima_UVM2sum_valid = ima_UVM2sum[4:-4,4:-4]


这将忽略前4行和后4行以及前4行(最后通过将停止值设置为负数来实现)

现在您要计算校正:

xvec_UVM2 = ft*ima_UVM2sum_valid
fxvec_UVM2 = 1 + (a1*xvec_UVM2) + (a2*xvec_UVM2**2) + (a3*xvec_UVM2**3) + (a4*xvec_UVM2**4)
Ctheory_UVM2 = - np.alog(1-(alpha*ima_UVM2sum_valid*ft))/(alpha*ft)


这些都是数组,因此您仍然不需要循环。

但是然后您要填充两个图像。请注意,因为校正量较小(我们插入了第一行和最后一行/列),因此您必须在校正图像中采用相同的区域:

corrfact_um2_ext1[4:-4,4:-4] = Ctheory_UVM2*(fxvec_UVM2/ima_UVM2sum_valid)
coincorr_um2_ext1[4:-4,4:-4]  = corrfact_um2_ext1[4:-4,4:-4] *ima_sk_um2


仅使用numpys数学函数仍然没有循环。这意味着它要快得多(更快!),并且执行相同的操作。

也许我忘记了切片,如果这样的话会产生一个Not broadcastable error,请报告。



只是关于循环的注释:Python的第一个轴是FITS中的第二个轴,第二个轴是第一个FITS轴。因此,如果您需要在轴上循环,请记住这一点,这样就不会导致IndexErrors或意外结果。

关于python - 使用astropy.fits和numpy将重合校正应用于SWIFT适合图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35412235/

10-13 06:59