简体   繁体   中英

Writing fits files with astropy.io.fits

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM