diff --git a/changes/440.feature.rst b/changes/440.feature.rst new file mode 100644 index 00000000..63a3e4be --- /dev/null +++ b/changes/440.feature.rst @@ -0,0 +1 @@ +Change default compression to lz4. The previous default was no compression. diff --git a/pyproject.toml b/pyproject.toml index c184fbd9..070d823b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ classifiers = [ ] dependencies = [ "asdf >=3.3.0", + "lz4 >= 4.3.0", "asdf-astropy >=0.5.0", "gwcs >=0.19.0", "numpy >=1.24", diff --git a/src/roman_datamodels/datamodels/_core.py b/src/roman_datamodels/datamodels/_core.py index 9ab38c23..4fd271ea 100644 --- a/src/roman_datamodels/datamodels/_core.py +++ b/src/roman_datamodels/datamodels/_core.py @@ -230,10 +230,11 @@ def open_asdf(self, init=None, **kwargs): return asdf.AsdfFile(init, **kwargs) def to_asdf(self, init, *args, **kwargs): + all_array_compression = kwargs.pop("all_array_compression", "lz4") with validate.nuke_validation(), _temporary_update_filename(self, Path(init).name): asdf_file = self.open_asdf(**kwargs) asdf_file["roman"] = self._instance - asdf_file.write_to(init, *args, **kwargs) + asdf_file.write_to(init, *args, all_array_compression=all_array_compression, **kwargs) def get_primary_array_name(self): """ diff --git a/src/roman_datamodels/maker_utils/_base.py b/src/roman_datamodels/maker_utils/_base.py index 4091849c..9571de80 100644 --- a/src/roman_datamodels/maker_utils/_base.py +++ b/src/roman_datamodels/maker_utils/_base.py @@ -29,6 +29,6 @@ def save_node(node, filepath=None): af = asdf.AsdfFile() af.tree = {"roman": node} - af.write_to(filepath) + af.write_to(filepath, all_array_compression="lz4") return node diff --git a/tests/test_models.py b/tests/test_models.py index 298e4bf1..14a5e601 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1109,3 +1109,28 @@ def test_model_assignment_access_types(model_class): assert type(model["meta"]["filename"]) == type(model2.meta["filename"]) # noqa: E721 assert type(model["meta"]["filename"]) == type(model2.meta.filename) # noqa: E721 assert type(model.meta.filename) == type(model2.meta["filename"]) # noqa: E721 + + +def test_default_array_compression(tmp_path): + """ + Test that saving a model results in compressed arrays + for default options. + """ + fn = tmp_path / "foo.asdf" + model = utils.mk_datamodel(datamodels.ImageModel) + model.save(fn) + with asdf.open(fn) as af: + assert af.get_array_compression(af["roman"]["data"]) == "lz4" + + +@pytest.mark.parametrize("compression", [None, "bzp2"]) +def test_array_compression_override(tmp_path, compression): + """ + Test that providing a compression argument changes the + array compression. + """ + fn = tmp_path / "foo.asdf" + model = utils.mk_datamodel(datamodels.ImageModel) + model.save(fn, all_array_compression=compression) + with asdf.open(fn) as af: + assert af.get_array_compression(af["roman"]["data"]) == compression