重新订购FITS标题中的卡片

问题描述 投票:0回答:1

我正在尝试修改FITS图像并相应地修改其标题,然后将其保存到新的FITS文件中

from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

# Open FITS file
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
cube = fits.open(image_file)[0]

# Modify the cube
cube.header.remove('NAXIS2')

# ...

cube.header['NAXIS2'] = 5

# Save the new FITS file
hdu = fits.PrimaryHDU(cube.data)
hdu.header = cube.header
hdu.writeto('new.fits', overwrite=True)

但是,这会返回以下错误:

VerifyError: 
Verification reported errors:
HDU 0:
    'NAXIS2' card at the wrong place (card 159).
    'EXTEND' card at the wrong place (card 160).
Note: astropy.io.fits uses zero-based indexing.

如何在标题中按正确的顺序放置卡片?或者有没有办法直接在正确的地方设置卡?


注意:这显然不是修改标题中字段的最佳方法,但它是重现我得到的错误的最短示例。我不想知道如何修改字段,我想知道如何在标题中的正确位置获取新字段。

python-3.x astropy fits
1个回答
0
投票

Header类具有insert功能,可以在给定卡之前或之后添加卡。

cube.header.insert('EXTEND', ('NAXIS2', 5))

将在'NAXIS2'卡之前添加价值5'EXTEND'卡。

通过使用after=True关键字,还可以在给定的卡片后面设置新卡片。所以另一种做同样事情的方法是:

cube.header.insert('NAXIS1', ('NAXIS2', 5), after=True)

通过这个修改,hdu.writeto(...)不再抛出错误。

© www.soinside.com 2019 - 2024. All rights reserved.