esheldon / fitsio

A python package for FITS input/output wrapping cfitsio
GNU General Public License v2.0
133 stars 57 forks source link

writing bool image is silently ignored #137

Open ngbusca opened 7 years ago

ngbusca commented 7 years ago

Writing an np.array of bool is silently ignored. Is this on purpose? It would be useful if at least a warning were returned.

ngbusca commented 7 years ago

example code: ` In [1]: import fitsio

In [2]: h=fitsio.FITS("example_bool.fits","rw")

In [3]: a=rand(10)

In [4]: h.write(a)

In [5]: a=rand(10)>0.5

In [6]: h.write(a)

In [7]: h.close()

In [8]: h=fitsio.FITS("example_bool.fits")

In [9]: h Out[9]:

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

In [10]: `

esheldon commented 6 years ago

I'm sorry I missed your report earlier. I think I was traveling at that time and missed a number of things.

The code you posted does produce an error for me

h=fitsio.FITS("example_bool.fits","rw")
a=numpy.random.random(10)
h.write(a)
a=numpy.random.random(10)>0.5
h.write(a)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: Unsupported numpy image datatype 0

The above exception was the direct cause of the following exception:

SystemError                               Traceback (most recent call last)
<ipython-input-8-b3fad0e0b795> in <module>()
----> 1 h.write(a)

~/miniconda3/lib/python3.6/site-packages/fitsio/fitslib.py in write(self, data, units, extname, extver, compress, tile_dims, header, names, table_type, write_bitcols, **keys)
    483             self.write_image(data, extname=extname, extver=extver, 
    484                              compress=compress, tile_dims=tile_dims,
--> 485                              header=header)
    486         else:
    487             self.write_table(data, units=units, 

~/miniconda3/lib/python3.6/site-packages/fitsio/fitslib.py in write_image(self, img, extname, extver, compress, tile_dims, header)
    537                               header=header,
    538                               extname=extname, extver=extver,
--> 539                               compress=compress, tile_dims=tile_dims)
    540 
    541         if header is not None:

~/miniconda3/lib/python3.6/site-packages/fitsio/fitslib.py in create_image_hdu(self, img, dims, dtype, extname, extver, compress, tile_dims, header)
    689                                     tile_dims=tile_dims,
    690                                     extname=extname,
--> 691                                     extver=extver)
    692 
    693 

SystemError: <built-in method create_image_hdu of _fitsio.FITS object at 0x7f4901b75d08> returned a result with an error set
ngbusca commented 6 years ago

I'm sorry I missed your report earlier.

no problem!

I've just tried again at nersc with python 2.7.14, fitsio 0.9.11 and I get the behavior I initially reported. What version of fitsio did you try for your test? I'll see if I can reproduce your error on the same python (3.6?) and fitsio as you.

esheldon commented 6 years ago

Yes, that was 3.6 and numpy 1.13.3, fitsio from recent master

esheldon commented 6 years ago

Have you tried with python 3 or recent fitsio master?

ngbusca commented 6 years ago

I've just tried in python3 with fitsio 0.9.11 and I get the same error as you. Is this the expected behavior? Thanks!

esheldon commented 6 years ago

Maybe; I didn't add the boolean support, maybe @dstndstn or @rainwoodman can comment

dstndstn commented 6 years ago

Huh, that's confusing because it looks like we handle NPY_BOOL here,

https://github.com/esheldon/fitsio/blob/0f0f34982341e9746a4f78d6df21af3b73998777/fitsio/fitsio_pywrap.c#L819

(and I just checked the numpy headers and NPY_BOOL is enumeration value 0, matching your error message.)

On Tue, Mar 20, 2018 at 2:38 PM, Erin Sheldon notifications@github.com wrote:

Maybe; I didn't add the boolean support, maybe @dstndstn https://github.com/dstndstn or @rainwoodman https://github.com/rainwoodman can comment

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/esheldon/fitsio/issues/137#issuecomment-374711025, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBD_VrWHxWUnZ3ILvPL9jDLrbmKz7rxks5tgUy0gaJpZM4PxV9f .

esheldon commented 5 years ago

Where do we stand on this one?

esheldon commented 3 years ago

ping

dstndstn commented 3 years ago

Hi Erin,

I still get an error with FITSIO 1.1.3 and Python 3.9.0

import fitsio
from numpy.random import rand
h=fitsio.FITS("example_bool.fits","rw", clobber=True)
a=rand(10)
h.write(a)
a=rand(10)>0.5
print('Writing boolean image')
h.write(a)
print('Wrote boolean image')
h.close()
h=fitsio.FITS("example_bool.fits")
print(h)

fails with

Writing boolean image
TypeError: Unsupported numpy image datatype 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/private/tmp/tst.py", line 8, in <module>
    h.write(a)
  File "/usr/local/lib/python3.9/site-packages/fitsio/fitslib.py", line 693, in write
    self.write_image(data, extname=extname, extver=extver,
  File "/usr/local/lib/python3.9/site-packages/fitsio/fitslib.py", line 781, in write_image
    self.create_image_hdu(
  File "/usr/local/lib/python3.9/site-packages/fitsio/fitslib.py", line 990, in create_image_hdu
    self._FITS.create_image_hdu(
SystemError: <built-in method create_image_hdu of _fitsio.FITS object at 0x1134edd50> returned a result with an error set