这里有一个如何做到这一点的例子。因此,您可以执行以下操作:
import numpy as np
from astropy.io import fits
ta = Table()
ta['test1'] = np.arange(0, 100.)
col1 = fits.Column(name=ta.colnames[0], format='E', array=ta)
tb = Table()
tb['test2'] = np.arange(0, 100.)
col2 = fits.Column(name=tb.colnames[0], format='E', array=tb)
cols = fits.ColDefs([col1, col2])
hdu = fits.BinTableHDU.from_columns(cols)
hdu.writeto('table.fits')
它只有一个二进制表 HDU,但有两列。或者,要将它们添加为单独的 HDU,您可以执行类似的操作
ta = Table()
ta['test1'] = np.arange(0, 100.)
col1 = fits.Column(name=ta.colnames[0], format='E', array=ta)
hdu1 = fits.BinTableHDU.from_columns(fits.ColDefs([col1]))
tb = Table()
tb['test2'] = np.arange(0, 100.)
col2 = fits.Column(name=tb.colnames[0], format='E', array=tb)
hdu2 = fits.BinTableHDU.from_columns(fits.ColDefs([col2]))
# create a header
hdr = fits.Header()
hdr['Author'] = 'Me'
primary_hdu = fits.PrimaryHDU(header=hdr)
# put all the HDUs together
hdul = fits.HDUList([primary_hdu, hdu1, hdu2])
# write it out
hdul.writeto('table.fits')