问题描述
我正在尝试将新卡插入/附加到FITS文件的现有标头(PRIMARY标头)中。使用下面的代码,我可以在终端上看到我“成功”执行了此操作。但是,当我在DS9中打开FITS文件并检查标题信息时,我的新卡不存在。所以我的动作没有被保存。我在这里想念什么?
from astropy.io import fits
with fits.open('my.fits') as hdul:
hdr = hdul[0].header
hdr.append(('NEWCARD','value','A comment.'),end=True)
hdul.info()
print(repr(hdr))
谢谢!
另外,最好创建一个新的页眉并在其中附加我的新卡吗?无需回答,我很好奇。由于我是处理Fits文件的新手,所以我想知道这是否是更好的方法。
解决方法
实际上,您并没有保存更新的文件。您必须使用更新模式并调用.flush()
:
from astropy.io import fits
with fits.open('my.fits',mode='update') as hdul:
hdr = hdul[0].header
hdr.append(('NEWCARD','value','A comment.'),end=True)
hdul.flush() # changes are written back to original.fits
https://docs.astropy.org/en/latest/io/fits/#save-file-changes