Skip to content
Open
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
24 changes: 24 additions & 0 deletions test/core/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ def test_open_mf_dataset(gridpath, datasetpath, mesh_constants):
nt.assert_equal(len(uxds_mf_ne30.uxgrid._ds.data_vars), mesh_constants['DATAVARS_outCSne30'])
nt.assert_equal(uxds_mf_ne30.source_datasets, str(dsfiles_mf_ne30))


def test_open_dataset_rejects_data_from_another_grid(gridpath, datasetpath):
"""outCSne30 data (ncol=5400) does not lie on the 4 faces of quad-hexagon."""
grid_path = gridpath("ugrid", "quad-hexagon", "grid.nc")
data_paths = datasetpath(
"ugrid", "outCSne30", ["outCSne30_var2.nc", "outCSne30_vortex.nc"]
)

with pytest.raises(ux.errors.GridsMismatchError, match="ncol=5400"):
ux.open_dataset(grid_path, data_paths[1])
with pytest.raises(ux.errors.GridsMismatchError, match="ncol=5400"):
ux.open_mfdataset(grid_path, data_paths)


@pytest.mark.parametrize("size", [2, 50])
@pytest.mark.parametrize("dim", ["n_face", "n_node", "n_edge"])
def test_open_dataset_rejects_wrong_grid_dimension_size(gridpath, dim, size):
grid_path = gridpath("ugrid", "quad-hexagon", "grid.nc")
data = xr.Dataset({"var": ((dim,), np.zeros(size))})

with pytest.raises(ux.errors.GridsMismatchError, match=f"{dim}={size} in the data"):
ux.open_dataset(grid_path, data)


def test_open_grid(gridpath, mesh_constants):
"""Loads only a grid topology file using uxarray's open_grid call."""
uxgrid = ux.open_grid(gridpath("ugrid", "geoflow-small", "grid.nc"))
Expand Down
43 changes: 43 additions & 0 deletions uxarray/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ def open_dataset(
uxds : uxarray.UxDataset
Dataset with linked `uxgrid` property of type `Grid`.

Raises
------
GridsMismatchError (subclass of ValueError)
If the data does not lie on the grid: a face, node or edge dimension of
the data has a different size than in the grid, or no data variable is
on any of these dimensions.

Notes
-----
The grid need not be a complete or fully UGRID-compliant mesh, nor represent
Expand Down Expand Up @@ -530,6 +537,7 @@ def open_dataset(

# Map original dimensions to the UGRID conventions
ds = _map_dataset_to_ugrid(ds, uxgrid)
_check_data_on_grid(ds, uxgrid, "open_dataset")

# Create a UXarray Dataset by linking the Xarray Dataset with a UXarray Grid
source_datasets = (
Expand Down Expand Up @@ -598,6 +606,13 @@ def open_mfdataset(
object : uxarray.UxDataset
Dataset with the unstructured grid.

Raises
------
GridsMismatchError (subclass of ValueError)
If the data does not lie on the grid: a face, node or edge dimension of
the data has a different size than in the grid, or no data variable is
on any of these dimensions.

Examples
--------

Expand Down Expand Up @@ -637,11 +652,39 @@ def open_mfdataset(

# Map original dimensions to the UGRID conventions
ds = _map_dataset_to_ugrid(ds, uxgrid)
_check_data_on_grid(ds, uxgrid, "open_mfdataset")

# Create a UXarray Dataset by linking the Xarray Dataset with a UXarray Grid
return UxDataset(ds, uxgrid=uxgrid, source_datasets=str(paths))


def _check_data_on_grid(ds, uxgrid, func_name):
"""Raise ``GridsMismatchError`` if the data mapped to UGRID dimensions does
not lie on ``uxgrid``."""
grid_dims = ("n_face", "n_node", "n_edge")
for dim in grid_dims:
if dim in ds.dims and ds.sizes[dim] != getattr(uxgrid, dim):
raise GridsMismatchError(
f"{func_name}(grid, data) expects data on the grid, but got "
f"{dim}={ds.sizes[dim]} in the data and {dim}={getattr(uxgrid, dim)} "
"in the grid."
)

on_grid = any(dim in grid_dims for var in ds.data_vars.values() for dim in var.dims)
if ds.data_vars and not on_grid:
data_sizes = ", ".join(f"{dim}={size}" for dim, size in ds.sizes.items())
grid_sizes = ", ".join(
f"{dim}={uxgrid._ds.sizes[dim]}"
for dim in grid_dims
if dim in uxgrid._ds.dims
)
raise GridsMismatchError(
f"{func_name}(grid, data) expects data on the grid, but none of the data "
f"dimensions ({data_sizes}) matches a face, node or edge dimension of the "
f"grid ({grid_sizes})."
)


def _get_grid(
grid_filename_or_obj,
chunks,
Expand Down
Loading