Skip to content

HazardForecast.from_xarray_raster fails when forecast has dimensions number or step of length 1 #1361

Done

Description

@luseverin

Describe the bug
Loading a HazardForecast using from_xarray_raster fails when if one of step, number, or both dimensions has a length of 1. This happens due to the dset.squeeze() https://github.com/CLIMADA-project/climada_python/blob/main/climada/hazard/forecast.py#L511C1-L511C39. I do not think this is desired, at least for forecasts that have only one of step or number with a length of 1.

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.

Activity

  1. peanutfun commented on Nov 24, 2026

    @peanutfun
    Member

    @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 ✌️

  2. luseverin commented on Nov 24, 2026

    @luseverin
    CollaboratorAuthor

    Great, I will set-up a PR asap

  3. peanutfun commented on Dec 3, 2026

    @peanutfun
    Member

    Fixed by #1362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions