Skip to content

Commit

Permalink
Merge pull request #2694 from ameraner/bugfix_match_arrays_in_ndvi
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud authored Dec 18, 2023
2 parents 6d652a6 + fb2ec9e commit 09b90fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions satpy/composites/spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def __call__(self, projectables, optional_datasets=None, **attrs):
LOG.info(f"Applying NDVI-weighted hybrid-green correction with limits [{self.limits[0]}, "
f"{self.limits[1]}] and strength {self.strength}.")

ndvi_input = self.match_data_arrays([projectables[1], projectables[2]])
projectables = self.match_data_arrays(projectables)

ndvi = (ndvi_input[1] - ndvi_input[0]) / (ndvi_input[1] + ndvi_input[0])
ndvi = (projectables[2] - projectables[1]) / (projectables[2] + projectables[1])

ndvi = ndvi.clip(self.ndvi_min, self.ndvi_max)

Expand Down
21 changes: 18 additions & 3 deletions satpy/tests/compositor_tests/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ class TestNdviHybridGreenCompositor:

def setup_method(self):
"""Initialize channels."""
coord_val = [1.0, 2.0]
self.c01 = xr.DataArray(
da.from_array(np.array([[0.25, 0.30], [0.20, 0.30]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C02"})
dims=("y", "x"), coords=[coord_val, coord_val], attrs={"name": "C02"})
self.c02 = xr.DataArray(
da.from_array(np.array([[0.25, 0.30], [0.25, 0.35]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C03"})
dims=("y", "x"), coords=[coord_val, coord_val], attrs={"name": "C03"})
self.c03 = xr.DataArray(
da.from_array(np.array([[0.35, 0.35], [0.28, 0.65]], dtype=np.float32), chunks=25),
dims=("y", "x"), attrs={"name": "C04"})
dims=("y", "x"), coords=[coord_val, coord_val], attrs={"name": "C04"})

def test_ndvi_hybrid_green(self):
"""Test General functionality with linear scaling from ndvi to blend fraction."""
Expand Down Expand Up @@ -123,3 +124,17 @@ def test_invalid_strength(self):
with pytest.raises(ValueError, match="Expected strength greater than 0.0, got 0.0."):
_ = NDVIHybridGreen("ndvi_hybrid_green", strength=0.0, prerequisites=(0.51, 0.65, 0.85),
standard_name="toa_bidirectional_reflectance")

def test_with_slightly_mismatching_coord_input(self):
"""Test the case where an input (typically the red band) has a slightly different coordinate.
If match_data_arrays is called correctly, the coords will be aligned and the array will have the expected shape.
"""
comp = NDVIHybridGreen("ndvi_hybrid_green", limits=(0.15, 0.05), prerequisites=(0.51, 0.65, 0.85),
standard_name="toa_bidirectional_reflectance")

c02_bad_shape = self.c02.copy()
c02_bad_shape.coords["y"] = [1.1, 2.]
res = comp((self.c01, c02_bad_shape, self.c03))
assert res.shape == (2, 2)

0 comments on commit 09b90fb

Please sign in to comment.