Skip to content

Commit

Permalink
Fix DayNightCompositor compatibility with numpy 2
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jun 26, 2024
1 parent 5c80fa6 commit 609d894
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,8 @@ def _get_coszen_blending_weights(
self,
projectables: Sequence[xr.DataArray],
) -> xr.DataArray:
lim_low = np.cos(np.deg2rad(self.lim_low))
lim_high = np.cos(np.deg2rad(self.lim_high))
lim_low = float(np.cos(np.deg2rad(self.lim_low)))
lim_high = float(np.cos(np.deg2rad(self.lim_high)))
try:
coszen = np.cos(np.deg2rad(projectables[2 if self.day_night == "day_night" else 1]))
self._has_sza = True
Expand All @@ -775,8 +775,8 @@ def _get_coszen_blending_weights(
# Get chunking that matches the data
coszen = get_cos_sza(projectables[0])
# Calculate blending weights
coszen -= np.min((lim_high, lim_low))
coszen /= np.abs(lim_low - lim_high)
coszen -= min(lim_high, lim_low)
coszen /= abs(lim_low - lim_high)
return coszen.clip(0, 1)

def _get_data_for_single_side_product(
Expand Down
8 changes: 5 additions & 3 deletions satpy/tests/modifier_tests/test_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,17 @@ def test_relative_azimuth_calculation(self):
assert isinstance(raa, xr.DataArray)
np.testing.assert_allclose(expected_raa, raa)

def test_solazi_correction(self):
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_solazi_correction(self, dtype):
"""Test that solar azimuth angles are corrected into the right range."""
from satpy.modifiers.angles import _get_sun_azimuth_ndarray

lats = np.array([-80, 40, 0, 40, 80])
lons = np.array([-80, 40, 0, 40, 80])
lats = np.array([-80, 40, 0, 40, 80], dtype=dtype)
lons = np.array([-80, 40, 0, 40, 80], dtype=dtype)

date = dt.datetime(2022, 1, 5, 12, 50, 0)

azi = _get_sun_azimuth_ndarray(lats, lons, date)

assert np.all(azi > 0)
assert azi.dtype == dtype
9 changes: 5 additions & 4 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,12 @@ def test_day_only_area_without_alpha(self):
"""Test compositor with day portion without alpha_band when SZA data is not provided."""
from satpy.composites import DayNightCompositor

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name="dn_test", day_night="day_only", include_alpha=False)
res = comp((self.data_a,))
res = res.compute()
# with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name="dn_test", day_night="day_only", include_alpha=False)
res_dask = comp((self.data_a,))
res = res_dask.compute()
expected = np.array([[0., 0.33164983], [0.66835017, 1.]], dtype=np.float32)
assert res_dask.dtype == res.dtype
assert res.dtype == np.float32
np.testing.assert_allclose(res.values[0], expected)
assert "A" not in res.bands
Expand Down

0 comments on commit 609d894

Please sign in to comment.