[AstroPy] astropy Table to table in a multi-extension fits file

Evert Rol evert.rol at gmail.com
Sun Mar 26 02:17:07 EDT 2017


> 
> I have several related astropy Tables (inside a python script) which I want to write to a multi-extension fits file.  Is there a simple way to do this using astropy.io.fits?

Hi Knox,

astropy.io.fits has a function `table_to_hdu` which may make things easier for you.
Here's an example:

from astropy.io.fits import table_to_hdu, HDUList, PrimaryHDU
from astropy.table import Table

HDUList([
   PrimaryHDU(),
   table_to_hdu( Table({'a': [1,2]})),
   table_to_hdu( Table({'b': [1,2,3]}))
]).writeto('test.fits')

(possibly appending to a HDU list inside a loop in your case.)


Evert




More information about the AstroPy mailing list