Skip to content

Commit

Permalink
Fix dtype promotion in sunzen_reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
lahtinep committed Oct 24, 2024
1 parent 6814f75 commit eb8e21c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 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.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
Expand Down
20 changes: 13 additions & 7 deletions satpy/tests/test_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit eb8e21c

Please sign in to comment.