Skip to content

Commit

Permalink
Merge pull request #2950 from pnuu/bugfix-sunzenithreducer-dtype-prom…
Browse files Browse the repository at this point in the history
…otion

Fix dtype promotion in `SunZenithReduction`
  • Loading branch information
mraspaud authored Nov 8, 2024
2 parents bc944ec + 6193040 commit 4fbc20c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
3 changes: 1 addition & 2 deletions satpy/modifiers/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ def sunzen_reduction(data: da.Array,
return da.map_blocks(_sunzen_reduction_ndarray, data, sunz, limit, max_sza, strength,
meta=np.array((), dtype=data.dtype), chunks=data.chunks)


def _sunzen_reduction_ndarray(data: np.ndarray,
sunz: np.ndarray,
limit: float,
Expand All @@ -584,7 +583,7 @@ def _sunzen_reduction_ndarray(data: np.ndarray,

# invert the reduction factor such that minimum reduction is done at `limit` and gradually increases towards max_sza
with np.errstate(invalid="ignore"): # we expect space pixels to be invalid
reduction_factor = 1. - np.log(reduction_factor + 1) / np.log(2)
reduction_factor = 1. - np.log2(reduction_factor + 1)

# apply non-linearity to the reduction factor for a non-linear reduction of the signal. This can be used for a
# slower or faster transision to higher/lower fractions at the ndvi extremes. If strength equals 1.0, this
Expand Down
28 changes: 19 additions & 9 deletions satpy/tests/test_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,28 @@ def setup_class(cls):
cls.custom = SunZenithReducer(name="sza_reduction_test_custom", modifiers=tuple(),
correction_limit=70, max_sza=95, strength=3.0)

def test_default_settings(self, sunz_ds1, sunz_sza):
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_default_settings(self, sunz_ds1, sunz_sza, dtype):
"""Test default settings with sza data available."""
res = self.default((sunz_ds1, sunz_sza), test_attr="test")
np.testing.assert_allclose(res.values,
np.array([[0.02916261, 0.02839063], [0.02949383, 0.02871911]]),
rtol=1e-5)
res = self.default((sunz_ds1.astype(dtype), sunz_sza.astype(dtype)), test_attr="test")
expected = np.array([[0.02916261, 0.02839063], [0.02949383, 0.02871911]], dtype=dtype)
assert res.dtype == dtype
values = res.values
assert values.dtype == dtype
np.testing.assert_allclose(values,
expected,
rtol=2e-5)

def test_custom_settings(self, sunz_ds1, sunz_sza):
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_custom_settings(self, sunz_ds1, sunz_sza, dtype):
"""Test custom settings with sza data available."""
res = self.custom((sunz_ds1, sunz_sza), test_attr="test")
np.testing.assert_allclose(res.values,
np.array([[0.01041319, 0.01030033], [0.01046164, 0.01034834]]),
res = self.custom((sunz_ds1.astype(dtype), sunz_sza.astype(dtype)), test_attr="test")
expected = np.array([[0.01041319, 0.01030033], [0.01046164, 0.01034834]], dtype=dtype)
assert res.dtype == dtype
values = res.values
assert values.dtype == dtype
np.testing.assert_allclose(values,
expected,
rtol=1e-5)

def test_invalid_max_sza(self, sunz_ds1, sunz_sza):
Expand Down

0 comments on commit 4fbc20c

Please sign in to comment.