Skip to content

Pyfits: Failure to write compressed np.int64 compressed images. #2153

Description

@pchanial

Using astropy: 0.3.dev4315, the following code crashes (note that other types np.uint8, np.int16, np.int32, np.float32 and np.float64 are fine):

    orig = np.arange(100, dtype=int)
    primary = fits.PrimaryHDU()
    hdu = fits.CompImageHDU(orig)
    hdulist = fits.HDUList([primary, hdu])
    hdulist.writeto('test.fits', clobber=True)

ERROR: RuntimeError: writing to compressed image is not supported [astropy.io.fits.hdu.compressed]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-32-ac346fc5abe9> in <module>()
----> 1 hdulist.writeto('test.fits', clobber=True)

/home/chanial/.local/lib/python2.7/site-packages/astropy-0.3.dev4315-py2.7-linux-x86_64.egg/astropy/io/fits/hdu/hdulist.pyc in writeto(self, fileobj, output_verify, clobber, checksum)
    671 
    672         for hdu in self:
--> 673             hdu._prewriteto(checksum=checksum)
    674             try:
    675                 hdu._writeto(hdulist.__file)

/home/chanial/.local/lib/python2.7/site-packages/astropy-0.3.dev4315-py2.7-linux-x86_64.egg/astropy/io/fits/hdu/compressed.pyc in _prewriteto(self, checksum, inplace)
   1500             self.scale(_ImageBaseHDU.NumCode[self._orig_bitpix])
   1501         if self._data_loaded and self.data is not None:
-> 1502             self._update_compressed_data()
   1503         # Doesn't call the super's _prewriteto, since it calls
   1504         # self.data._scale_back(), which is meaningless here.

/home/chanial/.local/lib/python2.7/site-packages/astropy-0.3.dev4315-py2.7-linux-x86_64.egg/astropy/io/fits/hdu/compressed.pyc in _update_compressed_data(self)
   1289             # compress_hdu returns the size of the heap for the written
   1290             # compressed image table
-> 1291             heapsize, self.compressed_data = compression.compress_hdu(self)
   1292         finally:
   1293             # if data was byteswapped return it to its original order

RuntimeError: writing to compressed image is not supported

Activity

  1. embray commented on Feb 28, 2014

    @embray
    Member

    For whatever reason the FITS Tile Compression convention still does not allow compression of 64-bit integer data #931.

    This could still use a better error message or warning though.

  2. self-assigned this
    on Feb 28, 2014
  3. added this to the v0.4.0 milestone on Mar 16, 2014
  4. eteq commented on May 27, 2014

    @eteq
    Member

    @embray - should this be pushed to 0.4.1?

  5. modified the milestones: v0.4.1, v0.4.0 on May 27, 2014
  6. modified the milestones: v0.4.2, v0.4.3 on Sep 23, 2014
  7. modified the milestone: v0.4.3 on Nov 21, 2014
  8. embray commented on Oct 20, 2015

    @embray
    Member

    Although, it seems that according to the current tile compression spec, specifically section 6.1, RICE_1 compression should support 64-bit ints if I read correctly (even though other parts of the spec are a little vague about that).

    More importantly though, the current version of CFITSIO that is bundled in Astropy does not support this. I haven't checked CFITSIO updates in a while though so let me see if this has been improved...

  9. embray commented on Oct 20, 2015

    @embray
    Member

    From the CFITSIO changelog it's not clear that this has been fixed yet. Might be worth trying out though. I don't remember the details of why this didn't work before (though I know it definitely didn't).

    Probably this should either give an error, or cast the data to 32-bit (where possible, with warnings and overflow errors).

    It would probably be good to update the convention and update CFITSIO to support this. Though if any effort were put into this I'd rather it be in the form of #3895.

  10. embray commented on Jul 6, 2021

    @embray
    Member

    AFAICT CFITS (or at least the version vendored in Astropy) still does not support this, though it does give a slightly better error message:

    RuntimeError: writing TLONGLONG to compressed image is not supported
    
  11. astrofrog commented on Jan 21, 2023

    @astrofrog
    Member

    This seems to have been fixed by #14252:

    In [11]: import numpy as np
    
    In [12]: from astropy.io import fits
    
    In [13]: orig = np.arange(100, dtype=int)
        ...: primary = fits.PrimaryHDU()
        ...: hdu = fits.CompImageHDU(orig)
        ...: hdulist = fits.HDUList([primary, hdu])
        ...: hdulist.writeto('test.fits', overwrite=True)
    
    In [14]: hdulist = fits.open('test.fits')
    
    In [15]: hdulist[1].data
    Out[15]: 
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  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, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
           51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
           68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
           85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
    
    In [16]: hdulist[1].data.dtype
    Out[16]: dtype('int64')
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions