astropy: HDUList Cannot Handle EXTVER
I am trying to create a multi extension .fits file that has extensions with the same name. I expected that the EXTVER
would be populated when the HDUList is made because I have no way of making the version of the ImageHDU that I can see in http://docs.astropy.org/en/stable/io/fits/api/images.html#images . Here is an example of my issue in Python 2.7 using astopy 1.3.3:
from astropy.io import fits
import numpy as np
def testissue():
# Make a primary header
prihdr = fits.PrimaryHDU()
# Make 2 extensions, both named 'SCI'
sci1 = fits.ImageHDU(data=np.ones((100,100)), name='SCI')
sci2 = fits.ImageHDU(data=np.ones((100,100)), name='SCI')
# Make and HDULIST with all of the extensions I want
hdulist = fits.HDUList([prihdr, sci1, sci2])
# Write that new fits file
hdulist.writeto('testIssue.fits', overwrite=True)
# Now try to use extver to get information per example at
# http://docs.astropy.org/en/stable/io/fits/
# >>> getheader('in.fits', ('sci', 2)) # use a tuple to do the same
fits.getheader('testIssue.fits', ('SCI', 2))
Gives:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-140-3d136e50da12> in <module>()
15 # http://docs.astropy.org/en/stable/io/fits/
16 # >>> getheader('in.fits', ('sci', 2)) # use a tuple to do the same
---> 17 fits.getheader('testIssue.fits', ('SCI', 2))
/Users/dborncamp/miniconda2/envs/astro/lib/python2.7/site-packages/astropy/io/fits/convenience.pyc in getheader(filename, *args, **kwargs)
108 hdulist, extidx = _getext(filename, mode, *args, **kwargs)
109 try:
--> 110 hdu = hdulist[extidx]
111 header = hdu.header
112 finally:
/Users/dborncamp/miniconda2/envs/astro/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in __getitem__(self, key)
312 # instead
313 return self._try_while_unread_hdus(super(HDUList, self).__getitem__,
--> 314 self._positive_index_of(key))
315
316 def __contains__(self, item):
/Users/dborncamp/miniconda2/envs/astro/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _positive_index_of(self, key)
700 """
701
--> 702 index = self.index_of(key)
703
704 if index >= 0:
/Users/dborncamp/miniconda2/envs/astro/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in index_of(self, key)
681
682 if (found is None):
--> 683 raise KeyError('Extension {} not found.'.format(repr(key)))
684 else:
685 return found
KeyError: "Extension ('SCI', 2) not found."
Is this expected behavior? It seems a little confusing that I would need to add the extension version information after the fact.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (16 by maintainers)
Sorry I keep missing a lot of these pings. I’d like to start paying more attention again when I can. No promises though. But if there’s ever anything that definitely needs my help (and this one clearly didn’t) don’t hesitate to e-mail me either.
@embray ? 🙏
I was thinking that
HDUList
could handle this automatically, but after reading the standard it is allowed to have a non-unique combination of EXTNAME/EXTVER. So, #6454 is indeed a good mean to make it easier to create this kind of file, and I don’t think we can do more ? (Even a warning would not really be justified with respect to the standard).I’ll see if allowing to pass in
ver
makes sense (and doesn’t break tests). I’ll report back with a PR in case I got it working.Yes, that’s why I asked if that would be “useful” 😃
It’s much easier than trying to keep track of the names of all HDUs in a HDUList.