From eb8e21c41ca378c07370392df462c07d84f2b5e5 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Thu, 24 Oct 2024 09:16:32 +0300 Subject: [PATCH] Fix dtype promotion in sunzen_reduction --- satpy/modifiers/angles.py | 3 +-- satpy/tests/test_modifiers.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/satpy/modifiers/angles.py b/satpy/modifiers/angles.py index 5ea8530612..028d9357ab 100644 --- a/satpy/modifiers/angles.py +++ b/satpy/modifiers/angles.py @@ -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, @@ -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.log(reduction_factor + 1) / np.log(2, dtype=data.dtype) # 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 diff --git a/satpy/tests/test_modifiers.py b/satpy/tests/test_modifiers.py index 7e28a7456b..279b73a28d 100644 --- a/satpy/tests/test_modifiers.py +++ b/satpy/tests/test_modifiers.py @@ -179,18 +179,24 @@ 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") + 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 np.testing.assert_allclose(res.values, - np.array([[0.02916261, 0.02839063], [0.02949383, 0.02871911]]), - rtol=1e-5) + 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") + 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 np.testing.assert_allclose(res.values, - np.array([[0.01041319, 0.01030033], [0.01046164, 0.01034834]]), + expected, rtol=1e-5) def test_invalid_max_sza(self, sunz_ds1, sunz_sza):