Skip to content

Instantly share code, notes, and snippets.

@pllim
Last active April 5, 2017 18:38
Show Gist options
  • Save pllim/2cd88d54e7090f285f99a7be3cd3fbe0 to your computer and use it in GitHub Desktop.
Save pllim/2cd88d54e7090f285f99a7be3cd3fbe0 to your computer and use it in GitHub Desktop.
Modify FITS table
"""
Ref: https://gist.github.com/bhilbert4/1d61054a4aeb2692831c629c28f811c8
"""
from astropy.io import fits
pf = fits.open('jwst_nircam_photom_0031.fits')
col_list = []
# Copy existing columns to new table. Here, I am only showing two as example.
for colname in ('filter', 'pupil'):
col_list.append(fits.Column(name=colname, format=pf[1].columns[colname].format, array=pf[1].data[colname]))
# Create new column. For example, your bigger wavelength column.
col_list.append(fits.Column(name='wavelength', format='3000E', array=np.zeros((15, 3000))))
# Create new binary table from your columns.
tbhdu = fits.BinTableHDU.from_columns(col_list)
# Create new HDU list with existing primary header and new table.
hdulist = fits.HDUList()
hdulist.append(pf[0])
hdulist.append(tbhdu)
# Write to new table.
hdulist.writeto('newtab.fits')
# Clean up.
pf.close()
# Outside Python session, you can simply use "mv" to replace old table with new table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment