astropy: Incorrect checksum/datasum in some cases for compressed FITS images?

While experimenting with checksums for compressed FITS images, I came across the following strange behavior. First, I load up the comp.fits file from our test suite and write it out, forcing a checksum calculation:

In [1]: import numpy as np
   ...: from astropy.io import fits

In [2]: from astropy.io.fits.hdu.base import _ValidHDU
   ...: _ValidHDU._get_timestamp = lambda self: "2013-12-20T13:36:10"  # Ensure deterministic checksums

In [3]: from astropy.utils.data import get_pkg_data_filename
   ...: 

In [4]: hdulist = fits.open(get_pkg_data_filename('data/comp.fits', package='astropy.io.fits.tests'))
   ...: hdulist.writeto('test_checksum.fits', overwrite=True, checksum=True)

Then I can try and load it up as a raw binary table:

In [5]: bintable_hdu = fits.open('test_checksum.fits', disable_image_compression=True)[1]

In [6]: bintable_hdu.header['CHECKSUM']
Out[6]: 'ZeAbdb8aZbAabb7a'

In [7]: bintable_hdu.header['DATASUM']
Out[7]: '113055149'

and I can also load it up decompressed:

In [8]: comp_image_hdu = fits.open('test_checksum.fits')[1]

In [9]: comp_image_hdu.header['CHECKSUM']
Out[9]: 'ZeAbdb8aZbAabb7a'

In [10]: comp_image_hdu.header['DATASUM']
Out[10]: '113055149'

The checksums and datasums are the same, but they shouldn’t be - the ones for bintable_hdu should be the ones for the actual binary table in the FITS file, while the comp_image_hdu ones should be the ones for the decompressed image.

Furthermore, the image checksums and datasums should be present in the binary table as ZHECKSUM and ZDATASUM but they are not.

It looks like the checksum and datasum for comp_image_hdu should actually be the following:

In [14]: image_hdu = fits.ImageHDU(comp_image_hdu.data, header=comp_image_hdu.header)
    ...: image_hdu.writeto('image.fits', overwrite=True, checksum=True)

In [15]: image_hdu.header['CHECKSUM']
Out[15]: 'D2GXD29VD2EVD29V'

In [16]: image_hdu.header['DATASUM']
Out[16]: '2189405276'

I am currently working on a possible refactor of CompImageHDU and in my version I get the D2GXD29VD2EVD29V checksum, so I wanted to double check whether we agree that the current behavior is buggy and that the following test is incorrect:

    def test_compressed_image_data(self):
        with fits.open(self.data("comp.fits")) as h1:
            h1.writeto(self.temp("tmp.fits"), overwrite=True, checksum=True)
            with fits.open(self.temp("tmp.fits"), checksum=True) as h2:
                ...
                assert "CHECKSUM" in h2[1].header
                assert h2[1].header["CHECKSUM"] == "ZeAbdb8aZbAabb7a"
                assert "DATASUM" in h2[1].header
                assert h2[1].header["DATASUM"] == "113055149"

Specifically, the last checksum and datasum should be updated to D2GXD29VD2EVD29V and 2189405276?

It seems that it is also a bug that ZHECKSUM and ZDATASUM are missing from the binary table on disk - this is also fixed in my branch but I wanted to check that we agree this is also a bug in the current code?

Any ideas on how to validate the checksums with third-party tools? (fitsverify doesn’t throw an error because the actual binary table checksum is correct and I don’t think it checks ZHECKSUM, which is missing anyway)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

Ok sounds good, but if we do 2) I think we have to do 3), i.e. if CHECKSUM is present in CompImageHDU.header it must be written as ZHECKSUM. It’s already done on main, so we should keep that. And users already have a way to have the image CHECKSUM, which is to create a CompImageHDU from an ImageHDU containing the checksum, which is consistent with the Standard definition of ZHECKSUM.

I don’t think so. If I understand correctly, the problem seem to have gotten worse, actually. But @astrofrog 's PR might fix it ? https://github.com/astropy/astropy/pull/15474