From cad4e60c439deca60632071c732dc3aa1df77c58 Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Tue, 6 Aug 2024 17:04:21 +0200 Subject: [PATCH 1/2] Added test for NWCSAF GEO type promotion Added a test to make sure that cloud type (or others) are not accidentally transformed to an inflated dtype. This currently fails with numpy 2.0; see https://github.com/pytroll/satpy/issues/2872 --- satpy/tests/reader_tests/test_nwcsaf_nc.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/satpy/tests/reader_tests/test_nwcsaf_nc.py b/satpy/tests/reader_tests/test_nwcsaf_nc.py index 6a509f023f..922c47ed9a 100644 --- a/satpy/tests/reader_tests/test_nwcsaf_nc.py +++ b/satpy/tests/reader_tests/test_nwcsaf_nc.py @@ -103,7 +103,7 @@ def create_nwcsaf_geo_ct_file(directory, attrs=global_attrs_geo): nc_file.attrs.update(attrs) var_name = "ct" - var = nc_file.create_variable(var_name, ("ny", "nx"), np.uint16, + var = nc_file.create_variable(var_name, ("ny", "nx"), np.uint8, chunks=(256, 256)) var[:] = RANDOM_GEN.integers(0, 255, size=(928, 1530), dtype=np.uint8) @@ -353,6 +353,13 @@ def test_end_time(self, nwcsaf_geo_ct_filehandler): """Test the end time property.""" assert nwcsaf_geo_ct_filehandler.end_time == read_nwcsaf_time(END_TIME) + def test_uint8_remains_uint8(self, nwcsaf_geo_ct_filehandler): + """Test that loading uint8 remains uint8.""" + ct = nwcsaf_geo_ct_filehandler.get_dataset( + {"name": "ct"}, + {"name": "ct", "file_type": "nc_nwcsaf_geo"}) + assert ct.dtype == np.dtype("uint8") + class TestNcNWCSAFPPS: """Test the NcNWCSAF reader for PPS products.""" From 7a294639a395d1f2c3ad8019730a642a8914c21c Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Tue, 6 Aug 2024 17:15:31 +0200 Subject: [PATCH 2/2] Fix dtype for nwcsaf geo Make sure that integer dtypes when reading NWCSAF GEO are not accidentally cast to int64. --- satpy/readers/nwcsaf_nc.py | 4 ++-- satpy/tests/reader_tests/test_nwcsaf_nc.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/satpy/readers/nwcsaf_nc.py b/satpy/readers/nwcsaf_nc.py index 64a284200d..ef68e830fa 100644 --- a/satpy/readers/nwcsaf_nc.py +++ b/satpy/readers/nwcsaf_nc.py @@ -205,8 +205,8 @@ def scale_dataset(self, variable, info): """ variable = remove_empties(variable) - scale = variable.attrs.get("scale_factor", np.array(1)) - offset = variable.attrs.get("add_offset", np.array(0)) + scale = variable.attrs.get("scale_factor", np.array(1, dtype=variable.dtype)) + offset = variable.attrs.get("add_offset", np.array(0, dtype=variable.dtype)) if "_FillValue" in variable.attrs: variable.attrs["scaled_FillValue"] = variable.attrs["_FillValue"] * scale + offset if np.issubdtype((scale + offset).dtype, np.floating) or np.issubdtype(variable.dtype, np.floating): diff --git a/satpy/tests/reader_tests/test_nwcsaf_nc.py b/satpy/tests/reader_tests/test_nwcsaf_nc.py index 922c47ed9a..d4f6dffedc 100644 --- a/satpy/tests/reader_tests/test_nwcsaf_nc.py +++ b/satpy/tests/reader_tests/test_nwcsaf_nc.py @@ -326,6 +326,14 @@ def test_scale_dataset_floating_nwcsaf_geo_ctth(self, nwcsaf_geo_ct_filehandler) assert "add_offset" not in var.attrs np.testing.assert_equal(var.attrs["valid_range"], (-2000., 25000.)) + def test_scale_dataset_uint8_noop(self, nwcsaf_geo_ct_filehandler): + """Test that uint8 is not accidentally casted when no scaling is done.""" + attrs = {} + var = xr.DataArray(np.array([1, 2, 3], dtype=np.uint8), attrs=attrs) + var = nwcsaf_geo_ct_filehandler.scale_dataset(var, "dummy") + np.testing.assert_equal(var, np.array([1, 2, 3], dtype=np.uint8)) + assert var.dtype == np.uint8 + def test_orbital_parameters_are_correct(self, nwcsaf_geo_ct_filehandler): """Test that orbital parameters are present in the dataset attributes.""" dsid = {"name": "ct"}