使用PyFIT向FITS表添加列

2024-10-01 09:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个扩展名为fits的文件,数据由两列组成,每个列有100个元素

data = pyfits.open('path2myfile')[1].data
head = pyfits.open('path2myfile')[1].header
print data['field1'] # print an array with 100 elements
print data['field2'] # print another array with 100 elements

现在我想在我的表中添加一个新列,比如data['field3'],这是另一个由100个元素组成的数组。在

我到底该怎么做?在


Tags: 文件数据元素datawithelementsopenarray
2条回答

@Cehem的回答是正确的。但我只想补充一点,Astropy有一个更好的通用Table接口,您可能会发现它更易于使用(用于在其他用例中插入列)。在

Astropy在astropy.io.fits模块中加入了PyFITS。但是,由于您还可以访问更好的表接口,因此您可以阅读大多数将表放入Astropy Table类,如下所示:

>>> from astropy.table import Table
>>> table = Table.read('path/to/fits_file.fits')

就这样。也可以将表写回FITS文件。诚然,这目前不支持all类型的FITS表,但它将适用于大多数和将来的所有类型。在

正如iguanaut指出的,答案可以在这里找到:http://pyfits.readthedocs.org/en/latest/users_guide/users_table.html#merging-tables 但要把这个问题标记为已回答:

cols = [] 
cols.append(
    pyfits.Column(name='field3', format='D', array= arrayContainingTheData)
    )
orig_cols = data.columns
new_cols = pyfits.ColDefs(cols)
hdu = pyfits.BinTableHDU.from_columns(orig_cols + new_cols)
hdu.writeto('newtable.fits')

相关问题 更多 >