Skip to content

io.fits add ability to efficiently load single extension header #4589

Closed

Description

@juliantaylor

Currently io.fits/pyfits always loads all extension headers regardless whether one wants them all or not.
At least the obvious api function getheader has this behaviour.

This slows down simple tasks like just checking the primary extension headers keys considerably when the number of extensions is large as it needs to seek through the full file.
Obviously seeking the file is required to load the later extensions but it should only try to read as much as it must to satisfy the users request.

Activity

  1. pllim commented on Feb 9, 2016

    @pllim
    Member

    What version of Astropy/PyFITS are you using? When I use getheader() (Astropy 1.1.1), I don't see the behavior you reported. It loads only the extension header that I asked for. See below.

    First, I create a simple FITS file for testing:

    from astropy.io import fits
    hdulist = fits.HDUList(fits.PrimaryHDU())
    hdulist.append(fits.ImageHDU([[0, 0, 0], [0, 1, 0], [0, 0, 0]]))
    hdulist[0].header['FOO_0'] = 'foo_0'
    hdulist[1].header['FOO_1'] = 'foo_1'
    hdulist.writeto('zfoo.fits')

    Now, I try to read each extension header back in:

    >>> hdr0 = fits.getheader('zfoo.fits')  # Should be only PRIMARY header
    >>> hdr0
    SIMPLE  =                    T / conforms to FITS standard                      
    BITPIX  =                    8 / array data type                                
    NAXIS   =                    0 / number of array dimensions                     
    EXTEND  =                    T                                                  
    FOO_0   = 'foo_0   '                                                            
    >>> hdr1 = fits.getheader('zfoo.fits', ext=1)  # Only EXT=1 header
    >>> hdr1
    XTENSION= 'IMAGE   '           / Image extension                                
    BITPIX  =                   64 / array data type                                
    NAXIS   =                    2 / number of array dimensions                     
    NAXIS1  =                    3                                                  
    NAXIS2  =                    3                                                  
    PCOUNT  =                    0 / number of parameters                           
    GCOUNT  =                    1 / number of groups           
    FOO_1   = 'foo_1   '   
  2. juliantaylor commented on Feb 9, 2016

    @juliantaylor
    Author

    I'm using git head, check the seeks astropy actually performs, it reads every header in the file, regardless of what is asked.

  3. pllim commented on Feb 9, 2016

    @pllim
    Member

    Maybe @embray can clarify when he gets the chance...

  4. juliantaylor commented on Feb 9, 2016

    @juliantaylor
    Author

    see https://github.com/astropy/astropy/blob/master/astropy/io/fits/convenience.py#L102
    the getheader function it simply loads the full hdulist via the same code as open uses and throws everything else away.
    Maybe I'm using the wrong function, but getheader is probably the first thing you will find when only interested in the header.

  5. added a commit that references this issue on Feb 24, 2016
    dd49934
  6. added 3 commits that reference this issue on Dec 2, 2016
    49ace64
    5e6d8d8
    e526694
  7. eteq commented on Dec 9, 2016

    @eteq
    Member

    @juliantaylor - I'm going to close this because I think #5065 has acheived the same affect. But feel free to re-open if you don't agree!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions