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

时间:2021-01-23 23:31:46

标签: python astropy fits

目标:重新组合现有图像(FITS 文件)并将新条目写入新的重新组合图像(也是 FITS 文件)。

问题:重新组合的 FITS 文件和原始 FITS 文件的坐标似乎不匹配(问题后面显示的图)。

过程:我将简要描述我的过程以阐明更多信息。第一步是读取现有的fits文件并定义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”是我用于近邻重新组合的函数,它将原始数组重新组合为“newdim”给定的新维度。现在的目标是将这个重新组合的数组作为新文件写回 FITS 文件格式。我还有几个这样的数组,但为简洁起见,我只包含一个数组作为示例。为了保持头信息相同,我从现有的 FITS 文件中读取该数组的头信息,并使用它将新数组写入新的 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 坐标似乎不匹配,我最好的猜测是原始 FITS 文件的 WCS(世界坐标系)没有为新 FITS 正确复制文件,从而导致任何不匹配。那么如何对齐这些坐标?

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

2 个答案:

答案 0 :(得分:2)

如果这对其他人有用,我将在一个 astropy slack 频道中发布我的答案。

congrid 将不起作用,因为它不包含有关 WCS 的信息。例如,您的 CD 矩阵与原始图像相关联,而不是重新合并的集。有多种方法可以使用适当的 WCS 重新装箱数据。您可以考虑 reproject,尽管这通常需要另一个 WCS 标头来重新合并。

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

答案 1 :(得分:1)

正如@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

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

相关问题