我正在尝试修改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.
如何在标题中按正确的顺序放置卡片?或者有没有办法直接在正确的地方设置卡?
注意:这显然不是修改标题中字段的最佳方法,但它是重现我得到的错误的最短示例。我不想知道如何修改字段,我想知道如何在标题中的正确位置获取新字段。
Header
类具有insert
功能,可以在给定卡之前或之后添加卡。
cube.header.insert('EXTEND', ('NAXIS2', 5))
将在'NAXIS2'
卡之前添加价值5
的'EXTEND'
卡。
通过使用after=True
关键字,还可以在给定的卡片后面设置新卡片。所以另一种做同样事情的方法是:
cube.header.insert('NAXIS1', ('NAXIS2', 5), after=True)
通过这个修改,hdu.writeto(...)
不再抛出错误。