esheldon/fitsio

Image return None value

Closed this issue · 13 comments

vvinuv commented

Hi All,

There is an issue when I read a fits image. fitsio gives None value.

fitsio.__version__
'1.2.0'

f = fitsio.FITS('check.fits', 'r')
data = f[0].read()
f.close()
print(data)

None

On the other hand, astropy gives the actual data.

from astropy.io import fits
with fits.open('check.fits') as hdu1:
    data = hdu1[2].data
print(data)

array([[0, 0, 0, 0, 0....

looks like you are using extension 0. Maybe the data is in another extension, maybe 1?

with fitsio.FITS('check.fits') as fits:
    data = fits[1].read()
vvinuv commented

I used that. It says 'extension not found: 1'.

I see for astropy it is 2. Can you print it?

with fitsio.FITS('check.fits') as fits:
    print(fits)
vvinuv commented

check.fits seems to have 7 hdus. Astropy can read until hdu[7]

with fits.open('check.fits') as hdu1:
    data = hdu1[7].data

Here are the fitsio outputs

with fitsio.FITS('check.fits') as fits:
    print(fits)

file: check.fits
mode: READONLY
extnum hdutype hduname[v]
0 IMAGE_HDU

with fitsio.FITS('check.fits') as fits:
    print(fits[0].read())

None

with fitsio.FITS('check.fits') as fits:
    print(fits[1].read())

IndexError Traceback (most recent call last)
File ~/python_lib/lib/python3.9/site-packages/fitsio/fitslib.py:1413, in FITS.getitem(self, item)
1411 try:
1412 # if it is an int
-> 1413 hdu = self.hdu_list[ext]
1414 except Exception:
1415 # might be a string

IndexError: list index out of range

During handling of the above exception, another exception occurred:

OSError Traceback (most recent call last)
Cell In [41], line 2
1 with fitsio.FITS('check.fits') as fits:
----> 2 print(fits[1].read())

File ~/python_lib/lib/python3.9/site-packages/fitsio/fitslib.py:1431, in FITS.getitem(self, item)
1429 else:
1430 if ext not in self.hdu_map:
-> 1431 raise IOError("extension not found: %s %s" % (ext, mess))
1432 hdu = self.hdu_map[ext]
1434 return hdu

OSError: extension not found: 1 (case insensitive)

Would you mind making the file available for download?

This file is misformatted.

It has a first HDU with no data, and the second HDU (which has data) does not start with the keyword XTENSION

Should we / can we add or run a utility to check for misformatted files?

vvinuv commented

This file is the output of sextractor. I'm not quite sure why this file opens in astropy and ds9 viewer.

I think we might be able to check for this particular error.

Looks like there was a bug and it wrote to primary HDUs.

sorry "wrote two primary HDUs"

I'm going to merge a change where this will raise an exception #380

CFITSIO will not read this file, so I think this is our only way forward