Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using file-level compression, allow setting compression parameters #422

Merged
merged 5 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading