pyfits: read compressed fits file

1.5k views Asked by At

How does one open a compressed fits file with pyfits?

The code below reads in the primary hdu, which is an image. The result is a NoneType object.

    # read in file
    file_input_fit = "myfile.fits.fz"
    hdulist = pyfits.open(file_input_fit)
    img     = hdulist[0].data

Usage of keyword in pyfits.open() "disable_image_compression=True" appears ineffective.

2

There are 2 answers

0
Iguananaut On

If the .data attribute on the primary HDU is None that means the primary HDU contains no data. You can confirm this by checking the file info:

hdulist.info()

Chances are you're trying to read a multi-extension FITS file, and the data you're looking for is in another castle, I mean, HDU. disable_image_compression=True wouldn't help since that disables support for compressed images :)

ETA: In fact, a tile-compressed FITS image can never be in the primary HDU, since it's stored internally as a binary table, which can only be an extension HDU.

0
astronomerdave On

This would be better as a comment but I don't have the reputation to make a comment so I'm forced to write an answer. However, the answer is the same -- namely that the compressed data is stored in the second HDU. The comment was just to show what this looks like on a compressed image I have here (after using the exact lines of the OP to open the file):

>>> hdulist.info()
Filename: /tmp/test.fits.fz
No.    Name         Type      Cards   Dimensions     Format
0    PRIMARY     PrimaryHDU       6   ()              
1                CompImageHDU     9   (24576, 6160)  float32