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

Avoid accidental NWCSAF-GEO type promotion #2874

Merged
merged 2 commits into from
Aug 15, 2024
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: 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
Loading