Ask Your Question
1

Cannot index an array - causing key error , context astropy.io

asked 2023-11-10 20:31:42 +0100

PatB gravatar image

updated 2023-11-12 20:01:29 +0100

FrédéricC gravatar image

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

edit retag flag offensive close merge delete

Comments

1

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!

PatB gravatar imagePatB ( 2023-11-13 05:05:39 +0100 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2023-11-10 21:02:22 +0100

rburing gravatar image

You can do hdul[int(0)] to work around Sage's Integer wrapper for literal integers.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-11-10 20:31:42 +0100

Seen: 191 times

Last updated: Nov 13 '23