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

Match all projectables in NDVIHybridGreen.__call__ to avoid coordinate mismatch errors #2694

Merged
merged 4 commits into from
Dec 18, 2023
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/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)
Loading