pythonfitspyfits

Adding columns to a FITS table with PyFITS


Let's assume I have a fits file with one extension, and the data consists of a table of two columns with 100 elements each

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

Now I want to add to my table a new column, let's say data['field3'], which is another array of 100 elements.

How exactly do I do that ?


Solution

  • As Iguananaut indicated, the answer can be found here : http://pyfits.readthedocs.org/en/latest/users_guide/users_table.html#merging-tables But just to mark this question as answered:

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