Closed weaverba137 closed 8 months ago
Even if I manually add BZERO
and BSCALE
back into the copied ImageHDU header, it is still written out without those keywords.
The fits.open(f, uint=False, do_not_scale_image_data=True)
has the effect of ignoring the BZERO and BSCALE on read, which means you get the wrong datatype in h1['Z_MASK'].data
. Instead of np.uint32
which is the correct datatype, you get np.int32
.
In [13]: hdulist = fits.open("coadd-sv3-dark-9994.fits", memmap=False, lazy_load_hdus=False)
In [19]: hdulist["Z_MASK"].data.dtype.name
Out[19]: 'uint32'
but
In [22]: h1 = fits.open("coadd-sv3-dark-9994.fits", memmap=False, lazy_load_hdus=False, uint=False, do_not_scale_image_data=True)
In [23]: h1['Z_MASK'].data.dtype.name
Out[23]: 'int32'
At this point, all bets are off.
Is there a reason to be ignoring BZERO and BSCALE on read, especially when some of the data HDUs are clearly unsigned integers?
$ fitsinfo coadd-sv3-dark-9994.fits | grep Z_MASK
16 Z_MASK 1 ImageHDU 12 (2881, 2058) int32 (rescales to uint32)
The point is to not allow fits.open()
to do any "helpful" transformations of the data. I want the low-level bytes, not any interpretation of what the bytes actually mean.
After further testing, I now realize I may have over-thought the options to fits.open()
, and that fewer options may actually do what I need. A bit more testing is needed, because #15256 seems to be a genuine bug, and I need to isolate that from other tests.
I'm closing this. I think I over-thought the options to fits.open()
and when I use fewer options and take care with HDUList.copy()
the problem goes away.
Description
ImageHDUs that contain
BZERO
andBSCALE
are written out withBSCALE
and possiblyBZERO
missing. Internally it appears thatImageHDU.copy()
does not write theBZERO
andBSCALE
headers to the copy.This happens even if the
HDUList
is immediately written out, without even making a copy.Expected behavior
ImageHDU.copy()
should make an exact copy. This is preventing files opened withastropy.io.fits.open()
from being written out as an exact copy.How to Reproduce
I'm using public data from DESI here, but basically this is a very simple ImageHDU that contains
BZERO
andBSCALE
.In this case, when
z_mask
is written out, the data payload is unchanged from the original (DATASUM
is still valid), but becauseBZERO
andBSCALE
are missing, the HDU checksum doesn't match.Another example, showing an attempt to just write out a
HDUList
to a different file.In the second case,
BSCALE
is missing from the output file, and the data payload is modified.Versions
I also checked astropy 5.3.1 with the same result.