Skip to content

Commit

Permalink
Adapt fill value after palettizing.
Browse files Browse the repository at this point in the history
Adapt fill value after palettizing.
  • Loading branch information
gerritholl committed Aug 19, 2024
1 parent f3eae46 commit 90237da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions trollimage/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions trollimage/xrimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 90237da

Please sign in to comment.