From 4842e9ac1bbfcdeed8e6ebf18bb70f77220d67eb Mon Sep 17 00:00:00 2001 From: lucas-santos-f <162367088+lucas-santos-f@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:47:07 -0300 Subject: [PATCH] Raise GridsMismatchError when open_dataset data is not on the grid open_dataset and open_mfdataset accepted data from another grid without complaint: dimensions that did not match the grid stayed unmapped (e.g. ncol), and a grid dimension with the wrong size went through as well. Check the mapped dataset against the grid and raise GridsMismatchError when a face, node or edge dimension has a different size, or when no data variable is on any of them. Part of #1853 --- test/core/test_api.py | 24 ++++++++++++++++++++++++ uxarray/core/api.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/test/core/test_api.py b/test/core/test_api.py index 871ebe68c..501a041c4 100644 --- a/test/core/test_api.py +++ b/test/core/test_api.py @@ -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")) diff --git a/uxarray/core/api.py b/uxarray/core/api.py index c0a6336c1..2f750bcef 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -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 @@ -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 = ( @@ -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 -------- @@ -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,