1

I have a fits files of event data, and I need to modify one of the tables by adding a new column of data derived by the data stored in a preexisting column of the same table. The problem I have is in closing the modified file. This is the code:

data = fits.open(events, extname='events')
t1 = data[1].data.field('time')
table = Table.read(events, format='fits')
t2 = Column(name='T2', data=t1)
table.add_column(t2)

How can I close the file writing on the same file as in input? If I try with table.write(events, format='fits') I receive an error due to the writing on an existing file, while if I try to close data the modifications are not written in the file.

Py-ser
  • 1,475
  • 6
  • 24
  • 46
  • 1
    It seems this question is a duplicate: http://stackoverflow.com/questions/21046500/adding-a-new-column-to-a-fits-file-via-python?rq=1 sorry – Py-ser Feb 07 '14 at 06:22
  • There is also `add_col` in newer versions, but still unable to use it... – Py-ser Feb 07 '14 at 08:43
  • 1
    This should be closed as a duplicate, yes. But I would also recommend against trying to use pyfits for any kind of table manipulation. Instead use astropy to read a FITS file into astropy's table class, make the changes there, and write it out to a new file. Adding columns to a FITS table is inherently difficult due to the fact that tables are always stored in row order in FITS. – Iguananaut Feb 08 '14 at 05:54
  • Iguananaut, thanks. However the issue has not been really answered in the other topic as well. Why do you suggest `astropy` instead of `pyfits`? Could you please furnish an example of `astropy` doing my same/similar task? – Py-ser Feb 09 '14 at 02:44
  • I thought you ment `pyastro` instead of `astropy` (for pyastro I couldnt find anything related to my task). Here are some good (untested) examples: http://docs.astropy.org/en/latest/table/index.html – Py-ser Feb 09 '14 at 02:52
  • I tried to flag the post as duplicate but I can't because the original has no answers as well. Should I write my own answer? – Py-ser Feb 10 '14 at 04:50

1 Answers1

0

They just recently added an overwrite option (similar to the usual clobber):

table.write(events, format='fits', overwrite='True')
Py-ser
  • 1,475
  • 6
  • 24
  • 46
  • That's fine that you added an accepted your own answer since you presumably got what you needed to do working. But this does *not* actually answer the question you originally posed in any way that would be helpful for future readers. – Iguananaut Feb 10 '14 at 17:36
  • I modified the original topic since I solved part of my original question. Then I put the "final" answer for it could be useful for other users. – Py-ser Feb 11 '14 at 01:40
  • As @Iguananaut alludes to, please be careful about changing the meaning of your question. If someone has answered, in particular, it's a very bad thing to do. In *this case*, your original question wasn't so great, but this one is a little better - but just keep in mind to be careful about changing questions like this. – Andrew Barber Mar 21 '14 at 15:25
  • 1
    This should be `overwrite=True` (the Python built-in constant [True](https://docs.python.org/2/library/constants.html#True), not the string `'True`'). This happens to work because non-empty strings are treated as true-like in Python. So `overwrite='False'` would still enable overwrite. Something to be careful about... – Iguananaut Jul 01 '14 at 22:01