astropy: FITS I/O bug

Am pretty sure the following represent a bug in the FITS I/O, but am not certain. The failing code is below. The write statement works and the read fails with an:

IOError : Header missing END card.

dum = np.empty((5,100), dtype=[('w','>f8'),('f','>f4')])
prihdu = fits.PrimaryHDU()
tblhdu = fits.BinTableHDU(dum, name='DATA’)
hdulist = fits.HDUList([prihdu, tblhdu])

hdulist.writeto('file.fits', clobber=True) # Does not fail

tmp = fits.open('file.fits')  # Fails

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I am surprised, however, that it was accepted at all as the data for a BinTableHDU.

The bug here, as far is Astropy is concerned, is that it was accepting Numpy arrays that do not represent row-oriented tables as the HDU data (currently this is the only type of array that would be accepted)

I don’t know exactly when behavior changed but as of astropy 6.0.1, OP’s script tracebacks as

Traceback (most recent call last):
  File "/Users/clm/dev/astropy-project/coordinated/astropy/issues/4580/t.py", line 6, in <module>
    tblhdu = fits.BinTableHDU(dum, name='DATA')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/clm/dev/astropy-project/coordinated/astropy/issues/4580/.venv/lib/python3.12/site-packages/astropy/io/fits/hdu/table.py", line 872, in __init__
    super().__init__(
  File "/Users/clm/dev/astropy-project/coordinated/astropy/issues/4580/.venv/lib/python3.12/site-packages/astropy/io/fits/hdu/table.py", line 351, in __init__
    self.data = self._data_type.from_columns(data)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/clm/dev/astropy-project/coordinated/astropy/issues/4580/.venv/lib/python3.12/site-packages/astropy/io/fits/fitsrec.py", line 470, in from_columns
    outarr[:n] = inarr.ravel()
    ~~~~~~^^^^
ValueError: could not broadcast input array from shape (500,) into shape (5,)

It seems that we’re de-facto not accepting the ill-formed input array anymore (good !), so maybe all we need now is a slightly clearer error message. @saimn @astrofrog do you think that’d be sufficient to close this issue ?