From 90237da8f3485b22cdb794274f5da929788478bc Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Mon, 19 Aug 2024 11:39:10 +0200 Subject: [PATCH] Adapt fill value after palettizing. Adapt fill value after palettizing. --- trollimage/tests/test_image.py | 10 ++++++++++ trollimage/xrimage.py | 1 + 2 files changed, 11 insertions(+) diff --git a/trollimage/tests/test_image.py b/trollimage/tests/test_image.py index 0aaf6a9..1883323 100644 --- a/trollimage/tests/test_image.py +++ b/trollimage/tests/test_image.py @@ -2600,6 +2600,16 @@ def test_palettize_geotiff_tag(self, tmp_path, colormap_tag, keep_palette): np.testing.assert_allclose(new_brbg.values, loaded_brbg.values) np.testing.assert_allclose(new_brbg.colors, loaded_brbg.colors) + def test_palettize_fill_value(self): + """Test that fill values are adapted.""" + + arr = np.arange(25, dtype="float32").reshape(5, 5)/25 + arr[2, 2] = np.nan + data = xr.DataArray(arr.copy(), dims=['y', 'x'], attrs={"_FillValue": np.nan}) + img = xrimage.XRImage(data) + img.palettize(brbg) + assert img.data[0, 2, 2] == img.data.attrs["_FillValue"] + class TestXRImageSaveScaleOffset: """Test case for saving an image with scale and offset tags.""" diff --git a/trollimage/xrimage.py b/trollimage/xrimage.py index 6c56b3e..afd70ff 100644 --- a/trollimage/xrimage.py +++ b/trollimage/xrimage.py @@ -1483,6 +1483,7 @@ def palettize(self, colormap): self.data.data = new_data self.data.coords['bands'] = list(mode) + self.data.attrs["_FillValue"] = colormap.values.shape[0]-1 # See docstring notes above for how scale/offset should be used scale_factor, offset = self._get_colormap_scale_offset(colormap) self.data.attrs.setdefault('enhancement_history', []).append({