Skip to content

Commit

Permalink
Update for xarray backend (#165)
Browse files Browse the repository at this point in the history
* Update for xarray backend

This commit adds more strict checks to coord/dim attrs to make sure
the Dataset and/or DataArray dimension sizes matches the what is
in the input files.

* Adding test

The test is for a ValueError being raised when opening a test grib2
file, 2024101012_Milton_Adv22_e70_cum_dat.grb, using the backend.

The error should be raised when no filter for duration is used.

* Update xarray_backend.py

This commit offers a different method suggested by Adam.

---------

Co-authored-by: Eric Engle <[email protected]>
  • Loading branch information
EricEngle-NOAA and EricEngle-NOAA authored Jan 31, 2025
1 parent 45f0c80 commit 97a12f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/grib2io/xarray_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_xarray_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 97a12f3

Please sign in to comment.