Skip to content

Commit

Permalink
Merge pull request #2874 from gerritholl/bugfix-nwcsafgeo-type-promotion
Browse files Browse the repository at this point in the history
Avoid accidental NWCSAF-GEO type promotion
  • Loading branch information
mraspaud authored Aug 15, 2024
2 parents e9bcf75 + 7a29463 commit 8d5a6a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions satpy/readers/nwcsaf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 16 additions & 1 deletion satpy/tests/reader_tests/test_nwcsaf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -353,6 +361,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."""
Expand Down

0 comments on commit 8d5a6a2

Please sign in to comment.