适合 header 中的未定义值

undefined value in a fits header

感谢您花时间帮助我!以下是我面临的问题。假设我正在阅读适合图像的 header。其中一张 header 卡片未定义 object,我不知道如何处理。

 from astropy.io import fits as pf

 hdu = pf.open('myfitsfile')
 hdu.info()
 img_hd= hdu[0].header
 print(img_hd['IMAGEZPT'])
 <astropy.io.fits.card.Undefined object at 0x109b35a20>

我 运行 我的代码迭代了数千张图像,几乎所有图像都有一些值。例如

 hdu_base = pf.open(a_lof[38])
 img_hd= hdu_base[0].header
 print(img_hd['IMAGEZPT'])
 23.61687

我只想为此分配一些其他值并继续。所以,我尝试了

if img_hd['IMAGEZPT'] == None:
    img_hd['IMAGEZPT'] = base_ZPT #some value I know
    print(img_hd['IMAGEZPT'])

但正如预期的那样,这不起作用,因为未定义关键字。所以 '== None' 不能工作。关键字存在,但未定义。

任何帮助将不胜感激! 最好, 阿比

如果 header 未被 AstroPy 定义

从 astropy

中导入 Undefined class
from astropy.io.fits.card import Undefined, UNDEFINED

然后这样比较

if isinstance(img_hd['IMAGEZPT'], Undefined):

它将 img_hd['IMAGEZPT'] 的实例类型与未定义类型进行比较。

或者那样

if img_hd['IMAGEZPT'] == UNDEFINED:

第二个选项应该更有效,因为它比较 img_hd['IMAGEZPT'] 的值而不是实例的类型。


如果 header 卡不在 HDU 中 Table

只需替换

if img_hd['IMAGEZPT'] == None:

来自

if 'IMAGEZPT' not in img_hd:

in 关键字检查条目 'IMAGEZPT' 是否存在于哈希表 img_hd.