Skip to content

Commit

Permalink
When using file-level compression, allow setting compression paramete…
Browse files Browse the repository at this point in the history
…rs (#422)

* if compression is set at the file level, then do set the compression-related keywords during writing an image.

* add unit test that in-memory mem:// files with compression set can get the dither_seed set.

* Update fitsio/fitsio_pywrap.c

* fix flake complaints

---------

Co-authored-by: Dustin Lang <[email protected]>
Co-authored-by: Matthew R. Becker <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2025
1 parent 94595f1 commit 273a00c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fitsio/fitsio_pywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions fitsio/tests/test_image_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,42 @@ def test_compressed_seed_bad(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

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']
assert dither1 == dither2


if __name__ == '__main__':
test_compressed_seed(
compress='rice',
Expand Down

0 comments on commit 273a00c

Please sign in to comment.