1

I have created an array named distance that contains 1242 values. I want to add this array as the 11th column in an already existing FITS file that contains 10 columns.

I am using pyfits.

I tried pyfits.append(filename, distance) which showed no errors but did not add my column to the FITS file.

Any suggestions??

Srivatsan
  • 7,913
  • 6
  • 45
  • 76
  • Are you trying to add a column to a table? `pyfits.append` just appends an array as a new IMAGE HDU. – Iguananaut Jan 10 '14 at 14:27
  • Yes, I would like to add a column to a table. How can I do that? @Iguananaut – Srivatsan Jan 10 '14 at 16:31
  • Does it need to use pyfits? Otherwise I would s/python/PyFITS in the title :) – Iguananaut Jan 10 '14 at 16:38
  • Please can you explain your previous comment. If there is any other way of adding the column to the fits file, please let me know. – Srivatsan Jan 11 '14 at 17:48
  • Have you seen http://pyfits.readthedocs.org/en/v3.2.0/users_guide/users_table.html#merging-tables ? – Iguananaut Jan 29 '14 at 13:40
  • @Iguananaut that is exactly what I am doing at the moment. but I want to add my new column/list to an existing FITS table. The problem with the current method in my case is, I need to create a new FITS with just the list i created and then merge that table with the existing table. – Srivatsan Jan 29 '14 at 14:41
  • 1
    You can use a simplified version of that example without creating a new file. The way the columns are combined is by adding two [ColDefs](http://pyfits.readthedocs.org/en/v3.2.0/api_docs/api_tables.html#pyfits.ColDefs) lists. You can just create a `ColDefs` containing a single [Column](http://pyfits.readthedocs.org/en/v3.2.0/api_docs/api_tables.html#pyfits.Column) including the column data array and add those. You still need to write it to a new file. Adding columns to a FITS table is tricky since they are row-oriented and it requires moving all the rows around. – Iguananaut Jan 29 '14 at 19:06
  • 1
    It so happens I'm actually in the process right now of updating the PyFITS docs, so I've added a new example motivated by this question. See the second example here: https://github.com/embray/PyFITS/blob/f18853addec03cbae5b9db27653dcff15b687ec5/docs/source/users_guide/users_table.rst#merging-tables – Iguananaut Jan 29 '14 at 19:21
  • In the linked example, do you create a new fits file where to merge the new table? – Py-ser Feb 12 '14 at 05:19
  • @Py-ser In the linked example, a new FITS file is created wherein the new columns are appended. But yes, a new table needs to be created! I couldn't find a way to append a column and update an existing FITS – Srivatsan Feb 12 '14 at 10:20
  • @Iguananaut I am trying what you said from your website. I am using Python 2.6.6 and in hdu = pyfits.BinTableHDU.from_columns(orig_cols + new_cols) I am not having the option from_columns – Srivatsan Feb 12 '14 at 14:55
  • @srivatsan Sorry about that; the examples there are for the development version of PyFITS. The equivalent example for older versions would by `pyfits.new_table(orig_cols + new_cols)`. Give that a go. – Iguananaut Feb 12 '14 at 20:40
  • Thanks both. But still no way to add a column within an already existing FITS file, right? – Py-ser Feb 13 '14 at 08:07
  • @Iguananaut, 'raise TypeError('Wrong type of input.')' – Py-ser Jun 03 '14 at 03:13
  • @Iguananaut, what about if I still receive this error `File "/usr/local/lib/python2.7/site-packages/pyfits-3.2.4-py2.7-linux-x86_64.egg/pyfits/column.py", line 1023, in __add__ raise TypeError('Wrong type of input.') TypeError: Wrong type of input.` – Py-ser Jun 30 '14 at 05:42
  • I don't know. I'd need to see more code--that seems to suggest that some of the arrays you're trying to add are not compatible with FITS but it's really hard to tell. – Iguananaut Jun 30 '14 at 14:43

1 Answers1

0

Finally they released an updated library that allows the modification of a table extension in a human way!

Last release of FITSIO. You can easily add a column with a code looking like the following:

import fitsio
from fitsio import FITS,FITSHDR
...
fits = FITS('file.fits','rw')
fits[-1].insert_column(name = 'newcolumn', data = mydata)      # add the extra column
fits.close()
Py-ser
  • 1,475
  • 6
  • 24
  • 46