From a07b628819a6b76c187dc88f996ba8da01e44e61 Mon Sep 17 00:00:00 2001 From: Dustin Lang Date: Fri, 21 Feb 2025 16:23:44 +0000 Subject: [PATCH 1/4] if compression is set at the file level, then do set the compression-related keywords during writing an image. --- fitsio/fitsio_pywrap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fitsio/fitsio_pywrap.c b/fitsio/fitsio_pywrap.c index caee0c7..c667a8a 100644 --- a/fitsio/fitsio_pywrap.c +++ b/fitsio/fitsio_pywrap.c @@ -1482,6 +1482,10 @@ PyFITSObject_create_image_hdu(struct PyFITSObject* self, PyObject* args, PyObjec write_data=1; } + if (comptype == 0) { + comptype = (*(self->fits)->Fptr).request_compress_type; + } + // 0 means NOCOMPRESS but that wasn't defined in the bundled version of cfitsio // if (comptype >= 0) { if (comptype > 0) { From 364f47950a12a3466b94749069266c021220b067 Mon Sep 17 00:00:00 2001 From: Dustin Lang Date: Fri, 21 Feb 2025 17:51:18 -0500 Subject: [PATCH 2/4] add unit test that in-memory mem:// files with compression set can get the dither_seed set. --- fitsio/tests/test_image_compression.py | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/fitsio/tests/test_image_compression.py b/fitsio/tests/test_image_compression.py index c12df4e..adb1d08 100644 --- a/fitsio/tests/test_image_compression.py +++ b/fitsio/tests/test_image_compression.py @@ -347,6 +347,41 @@ def test_compressed_seed_bad(dither_seed): dither_seed=dither_seed, ) +def test_memory_compressed_seed(): + import fitsio + + dtype = 'f4' + nrows = 300 + ncols = 500 + + seed = 1919 + rng = np.random.RandomState(seed) + + with tempfile.TemporaryDirectory() as tmpdir: + fname1 = os.path.join(tmpdir, 'test1.fits') + fname2 = os.path.join(tmpdir, 'test2.fits') + + data = rng.normal(size=(nrows, ncols)) + data = data.astype(dtype) + + fitsio.write(fname1, data.copy(), dither_seed='checksum', + compress='RICE', qlevel=1e-4, tile_dims=(100,100), clobber=True) + hdr = fitsio.read_header(fname1, ext=1) + dither1 = hdr['ZDITHER0'] + assert dither1 == 8269 + #print('File #1: ZDITHER0:', dither1) + + fits = fitsio.FITS('mem://[compress R 100,100; qz -1e-4]', 'rw') + fits.write(data.copy(), dither_seed='checksum') + data = fits.read_raw() + fits.close() + f = open(fname2, 'wb') + f.write(data) + f.close() + hdr = fitsio.read_header(fname2, ext=1) + dither2 = hdr['ZDITHER0'] + #print('File #2: ZDITHER0:', dither2) + assert dither1 == dither2 if __name__ == '__main__': test_compressed_seed( From 2bf362a01a7aaf2dcad49057cce66f5e6bb8bc64 Mon Sep 17 00:00:00 2001 From: "Matthew R. Becker" Date: Fri, 21 Feb 2025 16:54:51 -0600 Subject: [PATCH 3/4] Update fitsio/fitsio_pywrap.c --- fitsio/fitsio_pywrap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fitsio/fitsio_pywrap.c b/fitsio/fitsio_pywrap.c index c667a8a..8bba6be 100644 --- a/fitsio/fitsio_pywrap.c +++ b/fitsio/fitsio_pywrap.c @@ -1482,9 +1482,9 @@ PyFITSObject_create_image_hdu(struct PyFITSObject* self, PyObject* args, PyObjec write_data=1; } - if (comptype == 0) { - comptype = (*(self->fits)->Fptr).request_compress_type; - } + if (comptype == 0) { + comptype = (*(self->fits)->Fptr).request_compress_type; + } // 0 means NOCOMPRESS but that wasn't defined in the bundled version of cfitsio // if (comptype >= 0) { From 619ef7bcd5dfde30f0d9fbcb5e6d812afa674c4d Mon Sep 17 00:00:00 2001 From: Dustin Lang Date: Sun, 23 Feb 2025 08:43:17 -0500 Subject: [PATCH 4/4] fix flake complaints --- fitsio/tests/test_image_compression.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fitsio/tests/test_image_compression.py b/fitsio/tests/test_image_compression.py index adb1d08..fe0a1c3 100644 --- a/fitsio/tests/test_image_compression.py +++ b/fitsio/tests/test_image_compression.py @@ -347,6 +347,7 @@ def test_compressed_seed_bad(dither_seed): dither_seed=dither_seed, ) + def test_memory_compressed_seed(): import fitsio @@ -365,11 +366,11 @@ def test_memory_compressed_seed(): data = data.astype(dtype) fitsio.write(fname1, data.copy(), dither_seed='checksum', - compress='RICE', qlevel=1e-4, tile_dims=(100,100), clobber=True) + compress='RICE', qlevel=1e-4, tile_dims=(100, 100), + clobber=True) hdr = fitsio.read_header(fname1, ext=1) dither1 = hdr['ZDITHER0'] assert dither1 == 8269 - #print('File #1: ZDITHER0:', dither1) fits = fitsio.FITS('mem://[compress R 100,100; qz -1e-4]', 'rw') fits.write(data.copy(), dither_seed='checksum') @@ -380,9 +381,9 @@ def test_memory_compressed_seed(): f.close() hdr = fitsio.read_header(fname2, ext=1) dither2 = hdr['ZDITHER0'] - #print('File #2: ZDITHER0:', dither2) assert dither1 == dither2 + if __name__ == '__main__': test_compressed_seed( compress='rice',