Cannot index an array - causing key error , context astropy.io
There seems to be a problem indexing the object class 'astropy.io.fits.hdu.hdulist.HDUList
When I try to index it, I get a key Error as if it were a dictionary:
fits_image_filename = "/tmp/chiCyGQHY.fits"
from astropy.io import fits
hdul = fits.open(fits_image_filename)
print(type(hdul))
len(hdul)
<class 'astropy.io.fits.hdu.hdulist.HDUList'>
1
I need to extract hdul[0]. I succeed ONLY if I do hdul.pop()
hdu=hdul.pop()
hdu
<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f707f89c510>
However if instead I do hdu = hdul[0]
**KeyError** Traceback (most recent call last)
<ipython-input-25-e993ae5dfe6a> in <module>()
----> 1 hdul**[Integer(0)]**
/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in __getitem__(self, key)
299 try:
300 return self._try_while_unread_hdus(super(HDUList, self).__getitem__,
--> 301 self._positive_index_of(key))
302 except IndexError as e:
303 # Raise a more helpful IndexError if the file was not fully read.
/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _positive_index_of(self, key)
719 """
720
--> 721 index = self.index_of(key)
722
723 if index >= 0:
/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in index_of(self, key)
684 '{} indices must be integers, extension names as strings, '
685 'or (extname, version) tuples; got {}'
--> 686 ''.format(self.__class__.__name__, _key))
687
688 _key = (_key.strip()).upper()
KeyError: 'HDUList indices must be integers, extension names as strings, or (extname, version) tuples; got 0'
Question: What causes this confusion in sage between dictionaries and lists. Is it the wrapper Integer() ? If so, what is a more elegant work-around ?
Thanks to all you mavens out there!
Pat
Un gros MERCI Ricardo. That is a great fix! It worked in a context of a more subtle nature: I had to declare an index externally
image_data = fits.getdata(fichier, ext=int(0))
Case dismissed!