Skip to content

Commit

Permalink
Error when no UGRID topology is present in IO functions.
Browse files Browse the repository at this point in the history
Fixes #180
  • Loading branch information
Huite committed Feb 26, 2025
1 parent 18cc411 commit a687b97
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ The format is based on `Keep a Changelog`_, and this project adheres to
Unreleased
----------

Changed
-------

- :func:`xugrid.open_dataarray`, :func:`xugrid.load_dataarray`,
:func:`xugrid.open_dataset`, :func:`xugrid.load_dataset` now error when no
UGRID conventions data is present in the file or object.

Added
~~~~~

Expand Down
21 changes: 19 additions & 2 deletions tests/test_ugrid_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,13 @@ def test_open_dataset(tmp_path):
assert "mesh2d_face_nodes" in back.ugrid.grids[0].to_dataset()
assert "mesh2d_face_nodes" not in back.ugrid.obj

path = tmp_path / "no-topology-dataset.nc"
uds.to_netcdf(path)
with pytest.raises(
ValueError, match="The file or object does not contain UGRID conventions data."
):
xugrid.open_dataset(path)


def test_load_dataset(tmp_path):
path = tmp_path / "ugrid-dataset.nc"
Expand Down Expand Up @@ -1042,7 +1049,7 @@ def test_open_dataarray_roundtrip(tmp_path):
path = tmp_path / "ugrid-dataset.nc"
uds = xugrid.UgridDataset(UGRID_DS())
uds.ugrid.to_netcdf(path)
with pytest.raises(ValueError, match="Given file dataset contains more than one"):
with pytest.raises(ValueError, match="The file or object contains more than one"):
xugrid.open_dataarray(path)

path = tmp_path / "ugrid-dataarray.nc"
Expand All @@ -1052,11 +1059,21 @@ def test_open_dataarray_roundtrip(tmp_path):
assert back.name == "a"


def test_open_dataarray_ugrid_errors(tmp_path):
uds = ugrid1d_ds()
path = tmp_path / "no-topology-dataarray.nc"
uds.to_netcdf(path)
with pytest.raises(
ValueError, match="The file or object does not contain UGRID conventions data."
):
xugrid.open_dataarray(path)


def test_load_dataarray_roundtrip(tmp_path):
path = tmp_path / "ugrid-dataset.nc"
uds = xugrid.UgridDataset(UGRID_DS())
uds.ugrid.to_netcdf(path)
with pytest.raises(ValueError, match="Given file dataset contains more than one"):
with pytest.raises(ValueError, match="The file or object contains more than one"):
xugrid.load_dataarray(path)

path = tmp_path / "ugrid-dataarray.nc"
Expand Down
19 changes: 15 additions & 4 deletions xugrid/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@
DATAARRAY_VARIABLE = "__xarray_dataarray_variable__"


def _dataset_helper(ds: xr.Dataset):
n_topology = len(ds.ugrid_roles.topology)
if n_topology == 0:
raise ValueError(
"The file or object does not contain UGRID conventions data. "
"One or more UGRID topologies are required. Perhaps you wrote "
"the file using `data.to_netcdf()` instead of `data.ugrid.to_netcdf()`?"
)
return UgridDataset(ds)


def open_dataset(*args, **kwargs):
ds = xr.open_dataset(*args, **kwargs)
return UgridDataset(ds)
return _dataset_helper(ds)


def load_dataset(*args, **kwargs):
ds = xr.load_dataset(*args, **kwargs)
return UgridDataset(ds)
return _dataset_helper(ds)


def _dataarray_helper(ds: xr.Dataset):
dataset = UgridDataset(ds)
dataset = _dataset_helper(ds)
if len(dataset.data_vars) != 1:
raise ValueError(
"Given file dataset contains more than one data "
"The file or object contains more than one data "
"variable. Please read with xarray.open_dataset and "
"then select the variable you want."
)
Expand Down

0 comments on commit a687b97

Please sign in to comment.