diff --git a/src/grib2io/xarray_backend.py b/src/grib2io/xarray_backend.py index 24abdc0..4367c07 100755 --- a/src/grib2io/xarray_backend.py +++ b/src/grib2io/xarray_backend.py @@ -525,6 +525,21 @@ def build_da_without_coords(index, cube, filename) -> xr.DataArray: constant_meta_names = [k for k in cube.__dataclass_fields__.keys() if cube[k] is None] dims = {k: len(cube[k]) for k in dim_names} + dims_total = 1 + dims_to_filter = [] + for dim_name, dim_len, in dims.items(): + if dim_name not in {'x','y','station'}: + dims_total *= dim_len + dims_to_filter.append(dim_name) + + # Check number of GRIB2 message indexed compared to non-X/Y + # dimensions. + if dims_total != len(index): + raise ValueError( + f"DataArray dimensions are not compatible with number of GRIB2 messages; DataArray has {dims_total} " + f"and GRIB2 index has {len(index)}. Consider applying a filter for dimensions: {dims_to_filter}" + ) + data = OnDiskArray(filename, index, cube) lock = LOCK data = GribBackendArray(data, lock) diff --git a/tests/data/2024101012_Milton_Adv22_e70_cum_dat.grb b/tests/data/2024101012_Milton_Adv22_e70_cum_dat.grb new file mode 100644 index 0000000..5a330fd Binary files /dev/null and b/tests/data/2024101012_Milton_Adv22_e70_cum_dat.grb differ diff --git a/tests/test_xarray_backend.py b/tests/test_xarray_backend.py index 19b0362..5420951 100755 --- a/tests/test_xarray_backend.py +++ b/tests/test_xarray_backend.py @@ -46,3 +46,8 @@ def test_interp_with_openmp_threads(request): assert da.shape == (1, 1597, 2345) except(ModuleNotFoundError): pytest.skip() + +def test_valueerror_multiple_durations_to_filter(request): + data = request.config.rootdir / 'tests' / 'data' + with pytest.raises(ValueError, match=r"DataArray dimensions are not compatible with number of GRIB2 messages; DataArray has 4 and GRIB2 index has 2. Consider applying a filter for dimensions: \['leadTime', 'duration'\]"): + ds = xr.open_dataset(data / "2024101012_Milton_Adv22_e70_cum_dat.grb", engine="grib2io")