多个表保存在同一个.fits文件中

Jul*_*tte 2 python fits pyfits astropy

我有多个表创建astropy.table.Table,例如:

 from astropy.table import Table
 import numpy as np
 #table 1
 ta=Table()
 ta["test1"]=np.arange(0,100.)
 #table 2
 tb=Table()
 tb["test2"]=np.arange(0,100.)
Run Code Online (Sandbox Code Playgroud)

我可以将它们单独保存到.fits文件中

ta.write('table1.fits')
tb.write('table2.fits')
Run Code Online (Sandbox Code Playgroud)

但是我希望将它们保存到同一个.fits文件中,每个文件都有不同的文件hdu.我怎样才能做到这一点?

Chr*_*oph 5

有一个实用功能astropy.io.fits.table_to_hdu.

如果您有两个表对象ta,则继续您的示例tb:

from astropy.io import fits
hdu_list = fits.HDUList([
    fits.PrimaryHDU(),
    fits.table_to_hdu(ta),
    fits.table_to_hdu(tb), 
])
hdu_list.writeto('tables.fits')
Run Code Online (Sandbox Code Playgroud)