You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HazardForecast.from_xarray_raster fails when forecast has dimensions number or step of length 1 #1361
To Reproduce
A small example to reproduce the bug with toy forecast; loading the HazardForecast fails if one of step, number, or both dimensions has a length of 1.
import numpy as np
import xarray as xr
from climada.hazard.forecast import HazardForecast
def toy_forecast(n_members, n_lead_times):
"""Toy forecast on a 2 x 2 grid with the given number of members and lead times"""
data = np.random.default_rng(0).random((n_members, n_lead_times, 2, 2))
return xr.Dataset(
{"intensity": (("number", "step", "latitude", "longitude"), data)},
coords={
"number": np.arange(n_members),
"step": np.arange(1, n_lead_times + 1) * np.timedelta64(1, "D"),
"latitude": [10.0, 9.9],
"longitude": [12.0, 12.1],
},
)
for n_members, n_lead_times in [(3, 2), (1, 2), (3, 1), (1, 1)]:
try:
haz = HazardForecast.from_xarray_raster(
toy_forecast(n_members, n_lead_times),
hazard_type="RF",
intensity_unit="m",
coordinate_vars={"lead_time": "step", "member": "number"},
)
print(f"{n_members} member(s) x {n_lead_times} lead time(s): OK, {haz.size} events")
except Exception as err:
print(f"{n_members} member(s) x {n_lead_times} lead time(s): {type(err).__name__}: {err}")
Expected behavior
Ideally loading forecast which have either a step dimension with a length of one or just one ensemble member should work. We need to decide if we still accept forecast for which these two dimensions have a dimension of one (I would vote for yes).
Sugested fix
Do not squeeze along either of the leadtime_var or member_var dimensions:
dset_squeezed = dset.squeeze(
[d for d, n in dset.sizes.items() if n == 1 and d not in (leadtime_var, member_var)]
)
Let me know if you agree with this fix and I will set up a PR.
@luseverin Thanks for reporting! Yes, I agree with your solution. I would like to keep squeeze in because it's much more comfortable from a caller's perspective ✌️
Activity
peanutfun commentedon Nov 24, 2026
@luseverin Thanks for reporting! Yes, I agree with your solution. I would like to keep
squeezein because it's much more comfortable from a caller's perspective ✌️luseverin commentedon Nov 24, 2026
Great, I will set-up a PR asap
peanutfun commentedon Dec 3, 2026
Fixed by #1362