重新合并时保留 FITS 文件的 WCS 信息

Preserving the WCS information of a FITS file when rebinned

Aim:重新绑定现有图像(FITS 文件)并将新条目写入新的重新绑定图像(也是 FITS 文件)。

问题:重新装箱的 FITS 文件和原始 FITS 文件似乎不匹配co-ordinates(问题后面显示的图)。

过程:我将简要描述我的过程以阐明更多信息。第一步是读取现有的拟合文件并定义 numpy 数组

from math import *
import numpy as np
import matplotlib.pyplot as plt 
from astropy.visualization import astropy_mpl_style
from astropy.io import fits 
import matplotlib.pyplot as plt
%matplotlib notebook
import aplpy
from aplpy import FITSFigure



file = 'F0621_HA_POL_0700471_HAWDHWPD_PMP_070-199Augy20.fits'
hawc = fits.open(file)

stokes_i = np.array(hawc[0].data)
stokes_i_rebinned = congrid(stokes_i,newdim,method="neighbour", centre=False, minusone=False)

这里的“congrid”是我用于 near-neigbhour 重新分箱的函数,它将原始数组重新分箱到“newdim”给出的新维度。现在的目标是将这个重新组合的数组作为一个新文件写回 FITS 文件格式。我还有几个这样的数组,但为了简洁起见,我只以一个数组为例。为了保持 header 信息不变,我从现有的 FITS 文件中读取了该数组的 header 信息,并使用它来将新数组写入新的 FITS 文件。写入后,可以像原始文件一样读取重新合并的文件:-

header_0= hawc[0].header
fits.writeto("CasA_HAWC+_rebinned_congrid.fits", rebinned_stokes_i, header_0, overwrite=True)

rebinned_file = 'CasA_HAWC+_rebinned_congrid.fits'
hawc_rebinned= fits.open(rebinned_file)

为了检查重新合并后的图像现在的样子,我绘制了它们

cmap = 'rainbow'

stokes_i = hawc[0]
stokes_i_rebinned = hawc_rebinned[0]

axi = FITSFigure(stokes_i, subplot=(1,2,1))  # generate FITSFigure as subplot to have two axes together
axi.show_colorscale(cmap=cmap)              # show I


axi_rebinned = FITSFigure(stokes_i_rebinned, subplot=(1,2,2),figure=plt.gcf())
axi_rebinned.show_colorscale(cmap=cmap)              # show I rebinned

# FORMATTING
axi.set_title('Stokes I (146 x 146)')
axi_rebinned.set_title('Rebinned Stokes I (50 x 50)')
axi_rebinned.axis_labels.set_yposition('right')
axi_rebinned.tick_labels.set_yposition('right')
axi.tick_labels.set_font(size='small')
axi.axis_labels.set_font(size='small')
axi_rebinned.tick_labels.set_font(size='small')
axi_rebinned.axis_labels.set_font(size='small')

正如您在原始图像和重新合并后的图像中看到的那样,X、Y co-ordinates 似乎不匹配,我最好的猜测是原始 FITS 文件的 WCS(世界 co-ordinate 系统)不是为新的 FITS 文件正确复制,从而导致任何不匹配。那么如何对齐这些 co-ordinates ?

任何帮助将不胜感激!谢谢

如果这对其他人有用,我会在此处的 astropy slack 频道中发布我的答案。

congrid 将不起作用,因为它不包含有关 WCS 的信息。例如,您的 CD 矩阵与原始图像相关联,而不是 re-binned 集。有多种方法可以使用适当的 WCS re-bin 数据。您可能会考虑 reproject,尽管这通常需要另一个 WCS header 到 re-bin 到。

Montage(虽然不是 Python 工具,但有 Python 包装器)可能是另一种方式。

正如@astrochun 所说,您的重新合并功能不会调整重新合并图像的 WCS。除了reprojectMontageastropy.wcs.WCS对象还有slice()方法。您可以尝试使用它来“重新分类”WCS,如下所示:

from astropy.wcs import WCS
import numpy as np
wcs = WCS(hawc[0].header, hawc)
wcs_rebinned = wcs.slice((np.s_[::2], np.s_[::2]))
wcs_hdr = wcs_rebinned.to_header()
header_0.update(wcs_hdr)  # but watch out for CD->PC conversion

您还应该在 header_0= hawc[0].header 中制作 hawc[0].header 的“真实”副本,例如 header_0= hawc[0].header.copy() 否则 header_0.update(wcs_hdr) 会将 hawc[0].header 修改为嗯