Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Updated:

### Fixed

- Fix undesired squeezing of the member and lead time dimensions when they have a length of 1 in `HazardForecast.from_xarray_raster` [#1361](https://github.com/CLIMADA-project/climada_python/issues/1361).
- Preserve explicitly mapped dates in `HazardForecast.from_xarray_raster` [#1305](https://github.com/CLIMADA-project/climada_python/issues/1305).
- Fixed asset count in impact logging message [#1195](https://github.com/CLIMADA-project/climada_python/pull/1195).
- `Hazard.from_raster_xarray` now returns a sparse matrix instead of a sparse array [#1261](https://github.com/CLIMADA-project/climada_python/pull/1261).
Expand Down
8 changes: 7 additions & 1 deletion climada/hazard/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,13 @@ def from_xarray_raster(
)
)

dset_squeezed = dset.squeeze()
dset_squeezed = dset.squeeze(
[
dim
for dim, size in dset.sizes.items()
if size == 1 and dim not in (leadtime_var, member_var)
]
)

# Prepare coordinate_vars for parent call
parent_coord_vars = {
Expand Down
51 changes: 51 additions & 0 deletions climada/hazard/test/test_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,57 @@ def test_from_xarray_raster_dates_from_data(self, forecast_netcdf_file):
]
npt.assert_array_equal(haz_fc.date, expected_dates)

@pytest.mark.parametrize(
"eps_idx, lead_time_idx",
[([0], [0, 1, 2, 3]), ([0, 1, 2, 3, 4], [0]), ([0], [0])],
ids=["single_member", "single_lead_time", "single_member_and_lead_time"],
)
@xarray_leadtime
def test_from_xarray_raster_size_one_dims(
self, forecast_netcdf_file, eps_idx, lead_time_idx
):
"""Test that member and lead time dimensions of size 1 are kept

The other dimensions of size 1 (here ``ref_time``) are still squeezed.
"""
coordinate_vars = {
"longitude": "lon",
"latitude": "lat",
"lead_time": "lead_time",
"member": "eps",
}
haz_fc_full = HazardForecast.from_xarray_raster(
forecast_netcdf_file["path"],
hazard_type="PR",
intensity_unit="mm/h",
coordinate_vars=coordinate_vars,
)
with xr.open_dataset(forecast_netcdf_file["path"]) as dset:
dset = dset.isel(eps=eps_idx, lead_time=lead_time_idx).load()
haz_fc = HazardForecast.from_xarray_raster(
dset,
hazard_type="PR",
intensity_unit="mm/h",
coordinate_vars=coordinate_vars,
)

eps = forecast_netcdf_file["eps"][eps_idx]
lead_time = forecast_netcdf_file["lead_time"][lead_time_idx]
n_events = len(eps) * len(lead_time)
assert len(haz_fc.event_id) == n_events
npt.assert_array_equal(np.unique(haz_fc.member), eps)
npt.assert_array_equal(np.unique(haz_fc.lead_time), lead_time)
assert haz_fc.intensity.shape == (n_events, haz_fc_full.centroids.size)

# Same events as in the hazard read from the full file
haz_fc_sel = haz_fc_full.select(member=list(eps), lead_time=list(lead_time))
npt.assert_array_equal(haz_fc.member, haz_fc_sel.member)
npt.assert_array_equal(haz_fc.lead_time, haz_fc_sel.lead_time)
npt.assert_array_equal(haz_fc.event_name, haz_fc_sel.event_name)
npt.assert_array_equal(
haz_fc.intensity.todense(), haz_fc_sel.intensity.todense()
)


class TestSelect:
@pytest.mark.parametrize(
Expand Down
Loading