Writing fits files with astropy.io.fits
Asked Answered
C

1

5

I'm trying to append data to a fits file using astropy.io.

Here is an example of my code:

import numpy as np  
from astropy.io import fits  
a1 = np.array([1,2,4,8])  
a2 = np.array([0,1,2,3])  
hdulist = fits.BinTableHDU.from_columns(  
    [fits.Column(name='FIRST', format='E', array=a1),  
     fits.Column(name='SECOND', format='E', array=a2)])
hdulist.writeto('file.fits')

The error I get is

type object 'BinTableHDU' has no attribute 'from_columns'
  1. Could this be a problem with the astropy.io version I'm using?
  2. Is there an easier way to add extensions or columns to a fits file using astropy.io?

Any help would be appreciated.

Cryobiology answered 18/12, 2015 at 4:57 Comment(2)
To briefly answer your second question, if you want to create simple FITS tables use the high level astropy.table interface. The lower level FITS interface is only needed to make tables that use some of the more unusual FITS-specific features that aren't yet supported by the general Table interface.Locate
Renee, please remember to accept the answer if it solved your issue.Background
I
7

You'll have to upgrade astropy.
I can run your example fine; that's with the most recent astropy version.

Looking at the change log for 0.4, it's definitely looks like your astropy version is too old. The log says:

The astropy.io.fits.new_table function is now fully deprecated (though will not be removed for a long time, considering how widely it is used).

Instead please use the more explicit BinTableHDU.from_columns to create a new binary table HDU, and the similar TableHDU.from_columns to create a new ASCII table. These otherwise accept the same arguments as new_table which is now just a wrapper for these.

implying from_columns was newly introduced in 0.4


Overall, if you are indeed using astropy version 0.3, you may want to upgrade to version 1.0 or (current) 1.1:

  • while 0.3 is only about 1.5 years old (and a bit younger if you have a 0.3.x version), the rapid pace of astropy development makes it quite a bit out of date. A lot has changed in the interface, and examples you'll find online these days will rarely work your version.

  • Since astropy is now to a 1.x(.y) series, that should mean the API is relatively stable: there's only a slim change you'd run into backward compatibility issues.

  • Version 1.0(.x) is a long-term support release, with two years of bug fixes. Astropy 1.0 was released on 18 Feb 2015, so if you're looking for more stability, it will last until 18 Feb 2017. (Other versions support six months of bug fixes. But with the previous point, if you do minor release upgrades along the way, you should be fine as well.)

Ilonailonka answered 18/12, 2015 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.