Astropy 将校验和添加到现有的拟合文件中

Astropy add checksum to existing fits file

我已经编写了一个适合的文件(有人发给我),我想向 header 添加校验和和数据和。我发现的唯一示例是在将每个部分添加到 HDU 时使用 astropy.io.fits involve building a new fits HDU and verifying it 添加校验和。我可以做到,但这似乎需要更多的开销。

有没有办法向现有 HDU 添加校验和和数据和?

ImageHDU 个对象有一个名为 add_checksum() 的方法。那应该完全符合您的要求。


因此您可以在更新模式下打开一个 FITS 文件,然后调用它并再次关闭该文件。

from astropy.io import fits

with fits.open(filename, mode='update') as hdus:
    hdus[0].add_checksum() # Fill in the arguments like you need them

with 是首选,因为它会在 with 上下文退出时自动 closes 文件(即使发生异常),但您也可以打开和关闭它而无需with:

from astropy.io import fits

hdul = fits.open(filename, mode='update')
hdul[0].add_checksum()
hdul.close()