Skip to content

Commit

Permalink
Merge pull request pytroll#2080 from mherbertson/update-sandwich-comp…
Browse files Browse the repository at this point in the history
…ositor

Ignore alpha when adding luminance in Sandwich compositor
  • Loading branch information
mraspaud committed Apr 6, 2022
2 parents 683df59 + 0a97b74 commit fd483d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,8 @@ def __call__(self, projectables, *args, **kwargs):

# Get the enhanced version of the RGB composite to be sharpened
rgb_img = enhance2dataset(projectables[1])
rgb_img *= luminance
# Ignore alpha band when applying luminance
rgb_img = rgb_img.where(rgb_img.bands == 'A', rgb_img * luminance)
return super(SandwichCompositor, self).__call__(rgb_img, *args, **kwargs)


Expand Down
29 changes: 22 additions & 7 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,25 @@ def test_compositor(self):
np.testing.assert_allclose(res.data, 0.0, atol=1e-9)


class TestSandwichCompositor(unittest.TestCase):
class TestSandwichCompositor:
"""Test sandwich compositor."""

# Test RGB and RGBA
@pytest.mark.parametrize(
"input_shape,bands",
[
((3, 2, 2), ['R', 'G', 'B']),
((4, 2, 2), ['R', 'G', 'B', 'A'])
]
)
@mock.patch('satpy.composites.enhance2dataset')
def test_compositor(self, e2d):
def test_compositor(self, e2d, input_shape, bands):
"""Test luminance sharpening compositor."""
from satpy.composites import SandwichCompositor

rgb_arr = da.from_array(np.random.random((3, 2, 2)), chunks=2)
rgb = xr.DataArray(rgb_arr, dims=['bands', 'y', 'x'])
rgb_arr = da.from_array(np.random.random(input_shape), chunks=2)
rgb = xr.DataArray(rgb_arr, dims=['bands', 'y', 'x'],
coords={'bands': bands})
lum_arr = da.from_array(100 * np.random.random((2, 2)), chunks=2)
lum = xr.DataArray(lum_arr, dims=['y', 'x'])

Expand All @@ -467,9 +476,15 @@ def test_compositor(self, e2d):

res = comp([lum, rgb])

for i in range(3):
np.testing.assert_allclose(res.data[i, :, :],
rgb_arr[i, :, :] * lum_arr / 100.)
for band in rgb:
if band.bands != 'A':
# Check compositor has modified this band
np.testing.assert_allclose(res.loc[band.bands].to_numpy(),
band.to_numpy() * lum_arr / 100.)
else:
# Check Alpha band remains intact
np.testing.assert_allclose(res.loc[band.bands].to_numpy(),
band.to_numpy())
# make sure the compositor doesn't modify the input data
np.testing.assert_allclose(lum.values, lum_arr.compute())

Expand Down

0 comments on commit fd483d3

Please sign in to comment.