Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PyFITS向FITS表中添加列_Python_Fits_Pyfits - Fatal编程技术网

Python 使用PyFITS向FITS表中添加列

Python 使用PyFITS向FITS表中添加列,python,fits,pyfits,Python,Fits,Pyfits,假设我有一个扩展名为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 现在我想在我的表中

假设我有一个扩展名为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个元素的数组


我该怎么做呢?

正如Iguanaaut所指出的,答案可以在这里找到: 但我只想将这个问题标记为已回答:

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')

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

Astropy在模块中包含PyFITS。但是,由于您还可以访问更好的表格界面,因此可以将最适合的表格读入Astropy表格类,如下所示:

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

就这样。您还可以将表格写回FITS文件。诚然,这目前并不支持所有类型的FITS表,但它将适用于大多数情况——将来也适用于所有情况。

在阅读了几次您的问题后,我意识到您实际上拥有的是一个FITS表,您希望向其中添加一列。请看:啊,谢谢你,我没有正确的词汇。成功了!