[AstroPy] Read FITS headers without loading data

Erik Bray embray at stsci.edu
Mon Sep 8 11:14:25 EDT 2014


On 09/08/2014 10:40 AM, Gary Bernstein wrote:
> I would like to extract the header information from a large multi-extension FITS file using astropy.io.fits.  This runs very slowly, and I suspect because it is reading the data of each extension when the header is accessed e.g. via
>
> import astropy.io.fits as pf
> f = pf.open(‘mef.fits’)
> for hdu in f:
>      h = hdu.header
>      … do stuff with header…
>
> It is *much* faster using e.g. cfitsio utilities (<<1 second compared to 10’s of seconds for the above on a multiple-GB file).  Am I correct that the data is being loaded for each extension in the above method?  If so is there a workaround?  Neither using getheader nor (un)setting memmap seems to make a difference.

No, it doesn't touch the data when just reading headers.  The difference depends 
largely on what you're doing with the headers, though in general it's due to the 
fact that CFITSIO is written in C while PyFITS is written in pure Python, and 
does a lot more to parse headers into an in-memory data structure.

That said, how many headers are in this file?  It shouldn't take "10's of 
seconds" though again that might depend in part on what you're doing.

Erik




More information about the AstroPy mailing list