esheldon / fitsio

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

Compressing floating point image with NaN values #356

Open astrofrog opened 1 year ago

astrofrog commented 1 year ago

The following example shows that if a NaN value is present in the original data being compressed, the whole tile will be NaN when read in again:

In [1]: import numpy as np
   ...: data = np.arange(36).reshape((6, 6)).astype(float)
   ...: data[1, 1] = np.nan
   ...: data
Out[1]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.],
       [ 6., nan,  8.,  9., 10., 11.],
       [12., 13., 14., 15., 16., 17.],
       [18., 19., 20., 21., 22., 23.],
       [24., 25., 26., 27., 28., 29.],
       [30., 31., 32., 33., 34., 35.]])

In [2]: import fitsio
   ...: ft = fitsio.FITS('test_masking.fits', "rw")
   ...: ft.write(data, compress='RICE_1', tile_dims=(3, 3))

In [3]: ft2 = fitsio.FITS('test_masking.fits', 'r')
   ...: ft2[1].read()
Out[3]: 
array([[nan, nan, nan,  3.,  4.,  5.],
       [nan, nan, nan,  9., 10., 11.],
       [nan, nan, nan, 15., 16., 17.],
       [18., 19., 20., 21., 22., 23.],
       [24., 25., 26., 27., 28., 29.],
       [30., 31., 32., 33., 34., 35.]])

The FITS standard seems to suggest that ZBLANK can be used to circumvent this, and store NaN values (once quantized) as a specific integer value, which can then be converted back to NaN when de-quantizing. Is this something that can be done with fitsio?

esheldon commented 1 year ago

I'm sorry for the very late reply. I don't know if it can be done, but I would be very happy to accept a pull request for this..