Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

No code for getting number of extentions #108

Closed
pietrow opened this issue Apr 6, 2016 · 3 comments
Closed

No code for getting number of extentions #108

pietrow opened this issue Apr 6, 2016 · 3 comments

Comments

@pietrow
Copy link

pietrow commented Apr 6, 2016

There should be a way to get the number of extensions of a fits file, you'd expect HDUList to be able to do that.

Something like this might be handy.

def nextentions(im):
    '''
    Returns the number of extentions inside of a fits file.

    INPUT: im - filepath to fits file.
    OUTPUT: number of extentions in the file.    
    '''
    i = 0
    fit = pyfits.open(im)
    while True:
        try:
            trythis = fit[i].header

        except IndexError:
            fit.close()
            return i
        i+=1
@philhodge
Copy link
Collaborator

If you open the file like this:

import astropy.io.fits as fits
hdulist = fits.open(im)

then the total number of header/data units (including the primary) is
len(hdulist), so the number of extensions is len(hdulist) - 1.

Phil

On 04/06/2016 11:48 AM, pietrow wrote:

There should be a way to get the number of extensions of a fits file,
you'd expect HDUList to be able to do that.

Something like this might be handy.

|def nextentions(im): ''' Returns the number of extentions inside of a
fits file. INPUT: im - filepath to fits file. OUTPUT: number of
extentions in the file. ''' i = 0 fit = pyfits.open(im) while True:
try: trythis = fit[i].header except IndexError: fit.close() return i
i+=1 |


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#108

@mcara
Copy link
Member

mcara commented Apr 6, 2016

+1 @philhodge

@pllim
Copy link
Contributor

pllim commented Apr 6, 2016

@philhodge 's answer is the way to go.

By the way, PyFITS is deprecated. Please use astropy.io.fits. See http://astropy.readthedocs.org/en/stable/io/fits/index.html for details.

@pllim pllim closed this as completed Apr 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants