Skip to content

Hazard.from_xarray_raster requires that "event" dimension can be interpreted as datetime #829

@peanutfun

Description

@peanutfun

#795 has an unexpected repercussion (for me): The inserted hazard data now has to provide an "event" dimension that can be interpreted as datetime. I think this is unnecessarily strict. Consider the following code:

import xarray as xr
import numpy as np
from climada.hazard.base import Hazard

intensity = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]])
number = np.array([1, 2])
latitude = np.array([0, 1])
longitude = np.array([0, 1, 2])
dset = xr.Dataset(
    {
        "intensity": (["number", "latitude", "longitude"], intensity),
    },
    dict(number=number, latitude=latitude, longitude=longitude),
)

Hazard.from_xarray_raster(
    dset, "", "", intensity="intensity", coordinate_vars={"event": "number"}
)

This results in

TypeError: '.dt' accessor only available for DataArray with datetime64 timedelta64 dtype or for arrays containing cftime datetime objects.

Providing event names explicitly does not help, because the default values are always evaluated in the current implementation.

dset["event_name"] = ["a", "b"]
Hazard.from_xarray_raster(
    dset, "", "", intensity="intensity", coordinate_vars={"event": "number"}
)  # Same error

Note that following line is always evaluated, even if the default value is not used:

data[coords["event"]].dt.strftime("%Y-%m-%d").values.flatten().tolist(),

So, could we maybe just drop this default value altogehter? The default event name could just be a list of empty strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions