[AstroPy] what changed in astropy.io.fits?

Erik Bray embray at stsci.edu
Tue Sep 3 11:05:17 EDT 2013

On 09/02/2013 10:39 AM, Eduardo Bañados Torres wrote:
> Hi all,
> I recently upgraded to astropy version '0.3.dev5121'. I tried to run one of my
> old codes and it failed because was using hdr.has_key('CDELT1'). And now seems
> that the header does not have that attribute anymore: AttributeError:
> 'CompImageHeader' object has no attribute 'has_key'

The `has_key` method on Headers has been deprecated for some time, and is slated 
for removal in the next release (along with some other deprecated interfaces) so 
they have already been removed in the development version.

If you are on Python 2.7 it may not display deprecation warnings by default.  To 
run Python with deprecation warnings forced on run `python -Wd`.  See


for more information as well as




for notices of deprecated interfaces.

> If I use the old pyfits I don't get any error.
> Trying to understand what happened, I open the same file using astropy.io.fits
> and pyfits 2.3.2 and found that the hdu objects were different, they had
> different number of Cards and different formats.
> I solved my original problem replacing
> if hdr.has_key('CDELT1'): by if 'CDELT1' in hdr.keys():

The correct way to do this is:

`if 'CDELT1' in hdr:`

just like with Python dicts.  No need for the `.keys()` call.

> However I am still puzzled by the other changes I see in the HDUs, and I wonder
> if they have some other implications.
> Here is a code showing that the HDUs are different using astropy.fits and pyfits
> (I think the problem should be independent of the image used so I am not
> attaching it)

The development versions of pyfits and astropy.io.fits are not exactly in sync 
at the moment, though my plan is for pyfits 3.2 and astropy 0.3 to be identical. 
  Indeed, since pyfits is slowly being deprecated in favor of astropy, future 
pyfits releases will be more closely pinned to astropy releases.

Hope that clarifies things,


> ================
> from astropy.io <http://astropy.io> import fits
> import pyfits
> imgname = 'image.fits'
> hdu1 = pyfits.open(imgname)
> hdu2 = fits.open(imgname)
> hdr1 = hdu1[1].header
> hdr2 = hdu2[1].header
> print "hdu1 == hdu2? ", hdu1 == hdu2
> print hdu1.info <http://hdu1.info>()
> print hdu2.info <http://hdu2.info>()
> ================
> The output is:
> hdu1 == hdu2?  False
> Filename: image.fits
> No.    Name         Type      Cards   Dimensions   Format
> 0    PRIMARY     PrimaryHDU       6  ()            int16
> 1                CompImageHDU   172  (1200, 1200)  float32
> None
> Filename: image.fits
> No.    Name         Type      Cards   Dimensions   Format
> 0    PRIMARY     PrimaryHDU       6   ()           int16
> 1                CompImageHDU    174   (1200, 1200)   int16
> None
> The Cards number changes from 172 to 174 and the Format from float32 to int16.
> And the attributes of the headers are different
> Cheers,
> --
> Eduardo Bañados
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

More information about the AstroPy mailing list