From 6c9c1047074da3ce744327f7a6208a0270747d73 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Tue, 11 Aug 2026 11:48:48 -0400 Subject: [PATCH 01/11] tested v5 of cleaning stray data Time was getting into grid data, causing issues when subsetting down the line. This should clean the data in a way that shouldn't happen in the future --- uxarray/io/_ugrid.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/uxarray/io/_ugrid.py b/uxarray/io/_ugrid.py index a66dc8c18..4d17eb5bc 100644 --- a/uxarray/io/_ugrid.py +++ b/uxarray/io/_ugrid.py @@ -4,7 +4,7 @@ import uxarray.conventions.ugrid as ugrid from uxarray.constants import INT_DTYPE, INT_FILL_VALUE from uxarray.grid.connectivity import _replace_fill_values - +from uxarray.conventions.descriptors import DESCRIPTOR_NAMES def _read_ugrid(ds): """Parses an unstructured grid dataset and encodes it in the UGRID @@ -82,8 +82,30 @@ def _read_ugrid(ds): ds = ds.swap_dims(dim_dict) + # Strip non-grid extras (e.g. a stray scalar `time` coordinate, or unrelated data variables + ds = _keep_only_grid_vars(ds) + return ds, dim_dict +def _keep_only_grid_vars(ds): + """Return ``ds`` with only recognized UGRID grid variables/coordinates. + + Anything else on the dataset (a stray scalar ``time`` coordinate, unrelated + data variables, etc.) is dropped so it cannot leak onto ``grid._ds``. + + Runs on the file-read path only + """ + # uxarray's own canonical grid-variable names (the same lists Grid filters against) + keep = {"grid_topology"} + keep.update(ugrid.SPHERICAL_COORD_NAMES) # node/edge/face lon-lat + keep.update(ugrid.CARTESIAN_COORD_NAMES) # node/edge/face x-y-z + keep.update(ugrid.CONNECTIVITY_NAMES) # face_node_connectivity, edge_node_connectivity, ... + keep.update(DESCRIPTOR_NAMES) # n_nodes_per_face, face_areas, boundary_*_indices, ... + + # drop_vars removes variables/coords by name (never bare dimensions), so grid + # dims survive with their variables; errors="ignore" tolerates absent names. + drop = [name for name in ds.variables if name not in keep] + return ds.drop_vars(drop, errors="ignore") def _encode_ugrid(ds): """Encodes an unstructured grid represented under a ``Grid`` object as a From 621b0721696e531cc88d4c6446257a239fa23819 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Tue, 11 Aug 2026 15:52:28 -0400 Subject: [PATCH 02/11] reordered imports for pre-commit.ci --- uxarray/io/_ugrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uxarray/io/_ugrid.py b/uxarray/io/_ugrid.py index 4d17eb5bc..a1ddca448 100644 --- a/uxarray/io/_ugrid.py +++ b/uxarray/io/_ugrid.py @@ -3,8 +3,8 @@ import uxarray.conventions.ugrid as ugrid from uxarray.constants import INT_DTYPE, INT_FILL_VALUE -from uxarray.grid.connectivity import _replace_fill_values from uxarray.conventions.descriptors import DESCRIPTOR_NAMES +from uxarray.grid.connectivity import _replace_fill_values def _read_ugrid(ds): """Parses an unstructured grid dataset and encodes it in the UGRID From 36031ec593a7e3a1313019b85877ad23fe0cf60f Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Tue, 11 Aug 2026 16:43:09 -0400 Subject: [PATCH 03/11] ran `pre-commit run ...` pre-commit run --files uxarray/io/_ugrid.py Seemed to turn up a few issues with spaces and line lengths --- uxarray/io/_ugrid.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/uxarray/io/_ugrid.py b/uxarray/io/_ugrid.py index a1ddca448..91d4b7e6a 100644 --- a/uxarray/io/_ugrid.py +++ b/uxarray/io/_ugrid.py @@ -6,6 +6,7 @@ from uxarray.conventions.descriptors import DESCRIPTOR_NAMES from uxarray.grid.connectivity import _replace_fill_values + def _read_ugrid(ds): """Parses an unstructured grid dataset and encodes it in the UGRID conventions.""" @@ -87,6 +88,7 @@ def _read_ugrid(ds): return ds, dim_dict + def _keep_only_grid_vars(ds): """Return ``ds`` with only recognized UGRID grid variables/coordinates. @@ -97,16 +99,21 @@ def _keep_only_grid_vars(ds): """ # uxarray's own canonical grid-variable names (the same lists Grid filters against) keep = {"grid_topology"} - keep.update(ugrid.SPHERICAL_COORD_NAMES) # node/edge/face lon-lat - keep.update(ugrid.CARTESIAN_COORD_NAMES) # node/edge/face x-y-z - keep.update(ugrid.CONNECTIVITY_NAMES) # face_node_connectivity, edge_node_connectivity, ... - keep.update(DESCRIPTOR_NAMES) # n_nodes_per_face, face_areas, boundary_*_indices, ... + keep.update(ugrid.SPHERICAL_COORD_NAMES) # node/edge/face lon-lat + keep.update(ugrid.CARTESIAN_COORD_NAMES) # node/edge/face x-y-z + keep.update( + ugrid.CONNECTIVITY_NAMES + ) # face_node_connectivity, edge_node_connectivity, ... + keep.update( + DESCRIPTOR_NAMES + ) # n_nodes_per_face, face_areas, boundary_*_indices, ... # drop_vars removes variables/coords by name (never bare dimensions), so grid # dims survive with their variables; errors="ignore" tolerates absent names. drop = [name for name in ds.variables if name not in keep] return ds.drop_vars(drop, errors="ignore") + def _encode_ugrid(ds): """Encodes an unstructured grid represented under a ``Grid`` object as a ``xr.Dataset`` with an updated grid topology variable.""" From 6ad226b285e920646ab250d5aa3e42c5a1dd2506 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Mon, 17 Aug 2026 14:42:09 -0400 Subject: [PATCH 04/11] rewrite for fix in grid.py --- uxarray/grid/grid.py | 18 ++++++++++++++++-- uxarray/io/_ugrid.py | 29 ----------------------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index 3c86890f2..2638d0382 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -107,6 +107,18 @@ from uxarray.core.dataarray import UxDataArray +def _drop_non_grid_coords(ds): + """Drop coordinates that aren't recognized grid coordinates (e.g. a stray ``time`` + carried in from the source file). + + Coordinate-only, so grid data variables — connectivity, descriptors, and the + subset's ``subgrid_*_indices`` — are always left intact. + """ + grid_coords = set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) + stray = [coord for coord in ds.coords if coord not in grid_coords] + return ds.drop_vars(stray, errors="ignore") + + class Grid: """Represents a two-dimensional unstructured grid encoded following the UGRID conventions and provides grid-specific functionality. @@ -190,8 +202,10 @@ def __init__( # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) self.source_grid_spec = source_grid_spec - # internal xarray dataset for storing grid variables - self._ds = grid_ds + # internal xarray dataset for storing grid variables. + # drop stray coordinates (e.g. a `time` carried in from the source file) so they + # can't leak onto the grid and collide during subsetting (see #1444). + self._ds = _drop_non_grid_coords(grid_ds) # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) self.source_grid_spec = source_grid_spec diff --git a/uxarray/io/_ugrid.py b/uxarray/io/_ugrid.py index 91d4b7e6a..a66dc8c18 100644 --- a/uxarray/io/_ugrid.py +++ b/uxarray/io/_ugrid.py @@ -3,7 +3,6 @@ import uxarray.conventions.ugrid as ugrid from uxarray.constants import INT_DTYPE, INT_FILL_VALUE -from uxarray.conventions.descriptors import DESCRIPTOR_NAMES from uxarray.grid.connectivity import _replace_fill_values @@ -83,37 +82,9 @@ def _read_ugrid(ds): ds = ds.swap_dims(dim_dict) - # Strip non-grid extras (e.g. a stray scalar `time` coordinate, or unrelated data variables - ds = _keep_only_grid_vars(ds) - return ds, dim_dict -def _keep_only_grid_vars(ds): - """Return ``ds`` with only recognized UGRID grid variables/coordinates. - - Anything else on the dataset (a stray scalar ``time`` coordinate, unrelated - data variables, etc.) is dropped so it cannot leak onto ``grid._ds``. - - Runs on the file-read path only - """ - # uxarray's own canonical grid-variable names (the same lists Grid filters against) - keep = {"grid_topology"} - keep.update(ugrid.SPHERICAL_COORD_NAMES) # node/edge/face lon-lat - keep.update(ugrid.CARTESIAN_COORD_NAMES) # node/edge/face x-y-z - keep.update( - ugrid.CONNECTIVITY_NAMES - ) # face_node_connectivity, edge_node_connectivity, ... - keep.update( - DESCRIPTOR_NAMES - ) # n_nodes_per_face, face_areas, boundary_*_indices, ... - - # drop_vars removes variables/coords by name (never bare dimensions), so grid - # dims survive with their variables; errors="ignore" tolerates absent names. - drop = [name for name in ds.variables if name not in keep] - return ds.drop_vars(drop, errors="ignore") - - def _encode_ugrid(ds): """Encodes an unstructured grid represented under a ``Grid`` object as a ``xr.Dataset`` with an updated grid topology variable.""" From eb2960f28c5f229a080c79483910c12f58bc0de6 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Wed, 19 Aug 2026 15:25:41 -0400 Subject: [PATCH 05/11] added test for new "_drop_non_grid_coords" function --- test/test_subset.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/test_subset.py b/test/test_subset.py index 5f039bf13..88a4b4ff3 100644 --- a/test/test_subset.py +++ b/test/test_subset.py @@ -1,6 +1,8 @@ import uxarray as ux +import numpy as np import pytest +import xarray as xr def test_repr(gridpath, datasetpath): @@ -207,3 +209,31 @@ def test_empty_subset(gridpath, datasetpath): assert res.size == 0 # should still have all the same dim names even if resulting subset is empty: assert set(res.dims) == set(arr.dims) + + +def test_bounding_box_with_stray_grid_time(gridpath): + """Subsetting must work when the grid was built from a source that carried a stray + scalar ``time`` coordinate. Ensures issue #1444 has been fixed. + """ + # grid built from a source carrying a stray scalar `time` + grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) + uxgrid = ux.open_grid(grid_ds.assign_coords(time=np.datetime64("2016-01-01"))) + + # the stray coordinate is dropped during grid construction + assert "time" not in uxgrid._ds.coords + + # a face-centered variable carrying a *different* time coordinate + times = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") + uxda = ux.UxDataArray( + data=np.ones((times.size, uxgrid.n_face)), + dims=("time", "n_face"), + coords={"time": times}, + uxgrid=uxgrid, + ) + + # previously raised: "IndexError: dimension coordinate 'time' conflicts ..." + res = uxda.subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) + + assert isinstance(res, ux.UxDataArray) + assert res.sizes["time"] == times.size + assert "n_face" in res.dims From 30880fbbd069a243be7655ea296493ec0a5208d9 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Thu, 20 Aug 2026 15:07:02 -0400 Subject: [PATCH 06/11] stray coordinates on grid files change --- test/test_subset.py | 33 ++++++++++++++++++++++++++++++++- uxarray/grid/grid.py | 14 +------------- uxarray/grid/utils.py | 13 +++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/test/test_subset.py b/test/test_subset.py index 87a63bd75..ad4cf633b 100644 --- a/test/test_subset.py +++ b/test/test_subset.py @@ -4,7 +4,6 @@ import uxarray.grid.slice as slice_module from uxarray.grid.slice import _remap_dense, _remap_kernel, _remap_searchsorted -import numpy as np import pytest import xarray as xr @@ -361,3 +360,35 @@ def test_bounding_box_with_stray_grid_time(gridpath): assert isinstance(res, ux.UxDataArray) assert res.sizes["time"] == times.size assert "n_face" in res.dims + + +def test_bounding_box_with_stray_grid_time_dimension(gridpath): + """Subsetting must also work when the grid source carried a full ``time`` *dimension* + (a variable along ``time``) rather than the scalar coordinate of issue #1444. + + A dimensional coordinate has dims ``(time,)``, which is not a subset of the + ``(n_face,)`` ``subgrid_face_indices`` indexer, so — unlike a 0-d coordinate — it + never attaches to that indexer and cannot collide during ``.isel``. The stray + dimension harmlessly rides along in ``Grid._ds`` but never reaches the indexing path. + """ + # grid built from a source carrying a variable along a stray `time` dimension + grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) + uxgrid = ux.open_grid(grid_ds.assign(stray=("time", [0.0, 1.0]))) + + # the bare dimension is not a coordinate, so it is not dropped; that is harmless + assert "time" in uxgrid._ds.dims + + # a face-centered variable carrying its own time coordinate + times = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") + uxda = ux.UxDataArray( + data=np.ones((times.size, uxgrid.n_face)), + dims=("time", "n_face"), + coords={"time": times}, + uxgrid=uxgrid, + ) + + res = uxda.subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) + + assert isinstance(res, ux.UxDataArray) + assert res.sizes["time"] == times.size + assert "n_face" in res.dims diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index ab9677e49..2a2580ce9 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -66,7 +66,7 @@ _populate_edge_node_distances, ) from uxarray.grid.point_in_face import _point_in_face_query -from uxarray.grid.utils import make_setter +from uxarray.grid.utils import _drop_non_grid_coords, make_setter from uxarray.grid.validation import ( _check_area, _check_connectivity, @@ -108,18 +108,6 @@ from uxarray.core.dataarray import UxDataArray -def _drop_non_grid_coords(ds): - """Drop coordinates that aren't recognized grid coordinates (e.g. a stray ``time`` - carried in from the source file). - - Coordinate-only, so grid data variables — connectivity, descriptors, and the - subset's ``subgrid_*_indices`` — are always left intact. - """ - grid_coords = set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) - stray = [coord for coord in ds.coords if coord not in grid_coords] - return ds.drop_vars(stray, errors="ignore") - - class Grid: """Represents a two-dimensional unstructured grid encoded following the UGRID conventions and provides grid-specific functionality. diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index a33f50629..f99ca678c 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -3,6 +3,7 @@ from numba import njit, prange from uxarray.constants import INT_FILL_VALUE +from uxarray.conventions import ugrid from uxarray.utils.numba_math import ( _numba_add3, _numba_mul3_scalar, @@ -11,6 +12,18 @@ ) +def _drop_non_grid_coords(ds): + """Drop coordinates that aren't recognized grid coordinates (e.g. a stray ``time`` + carried in from the source file). + + Coordinate-only, so grid data variables — connectivity, descriptors, and the + subset's ``subgrid_*_indices`` — are always left intact. + """ + grid_coords = set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) + stray = [coord for coord in ds.coords if coord not in grid_coords] + return ds.drop_vars(stray, errors="ignore") + + @njit(cache=True) def _small_angle_of_2_vectors(u, v): """ From 4786101c44164ffc414a600c3b72b678dea6d86f Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Thu, 20 Aug 2026 15:55:48 -0400 Subject: [PATCH 07/11] size change to dropping coordinates --- uxarray/grid/grid.py | 4 ++-- uxarray/grid/utils.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index 2a2580ce9..39e181877 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -192,8 +192,8 @@ def __init__( self.source_grid_spec = source_grid_spec # internal xarray dataset for storing grid variables. - # drop stray coordinates (e.g. a `time` carried in from the source file) so they - # can't leak onto the grid and collide during subsetting (see #1444). + # drop stray scalar coordinates (e.g. a `time` carried in from the source file) + # so they can't leak onto the grid and collide during subsetting (see #1444). self._ds = _drop_non_grid_coords(grid_ds) # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index f99ca678c..5b3706012 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -13,14 +13,23 @@ def _drop_non_grid_coords(ds): - """Drop coordinates that aren't recognized grid coordinates (e.g. a stray ``time`` - carried in from the source file). + """Drop scalar (0-d) coordinates that aren't recognized grid coordinates + (e.g. a stray ``time`` carried in from the source file). + + Only 0-d strays can cause the subset collision behind issue #1444: a scalar + coordinate attaches to every variable, including the ``(n_face,)`` + ``subgrid_*_indices`` indexer, where it can clash with a like-named dimension + on the data being indexed. A coordinate with its own dimension (e.g. ICON's + 1-d ``clon``/``clat`` or FESOM2's ``lon``/``lat``) never attaches to that + indexer, so it is left in place and those grids round-trip unchanged. Coordinate-only, so grid data variables — connectivity, descriptors, and the subset's ``subgrid_*_indices`` — are always left intact. """ grid_coords = set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) - stray = [coord for coord in ds.coords if coord not in grid_coords] + stray = [ + coord for coord in ds.coords if coord not in grid_coords and ds[coord].ndim == 0 + ] return ds.drop_vars(stray, errors="ignore") From e1741e5c222cea42ae8daf40c6edbbad9d7bdb60 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:39:55 -0500 Subject: [PATCH 08/11] initial rework with new rules --- test/grid/grid/test_non_grid_data.py | 166 +++++++++++++++++++++++++++ test/test_subset.py | 32 ------ uxarray/core/api.py | 20 +++- uxarray/grid/grid.py | 31 ++++- uxarray/grid/slice.py | 8 +- uxarray/grid/utils.py | 58 +++++++--- 6 files changed, 259 insertions(+), 56 deletions(-) create mode 100644 test/grid/grid/test_non_grid_data.py diff --git a/test/grid/grid/test_non_grid_data.py b/test/grid/grid/test_non_grid_data.py new file mode 100644 index 000000000..beb5b9122 --- /dev/null +++ b/test/grid/grid/test_non_grid_data.py @@ -0,0 +1,166 @@ +"""Tests for dropping non-grid data during ``Grid`` construction (#1444, #1720).""" + +import numpy as np +import pytest +import xarray as xr + +import uxarray as ux +from uxarray.conventions.ugrid import DIM_NAMES + +TIMES = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") +STRAY_NAMES = {"time", "stray", "temp", "time_since_epoch", "special_index", "z"} + +# the shapes listed in #1720 +STRAY_CASES = { + "scalar_coord": lambda ds: ds.assign_coords(time=np.datetime64("2016-01-01")), + "dimension_coord": lambda ds: ds.assign_coords(time=("time", TIMES)), + "bare_dimension": lambda ds: ds.assign(stray=("time", [0.0, 1.0])), + "data_var_on_time_and_face": lambda ds: ds.assign( + temp=(("time", "n_face"), np.ones((2, ds.sizes["n_face"]))) + ).assign_coords(time=("time", TIMES)), + "non_dimension_coord": lambda ds: ds.assign_coords( + time=("time", TIMES), time_since_epoch=("time", [0, 1]) + ), + "coord_on_two_non_grid_dims": lambda ds: ds.assign_coords( + special_index=(("time", "elevation"), np.arange(6).reshape(2, 3)) + ), + "coord_mixing_grid_and_non_grid_dims": lambda ds: ds.assign_coords( + z=(("n_node", "elevation"), np.zeros((ds.sizes["n_node"], 3))) + ), + "scalar_data_var": lambda ds: ds.assign( + time=xr.DataArray(np.datetime64("2016-01-01")) + ), +} + + +@pytest.fixture +def quad_hexagon_ds(gridpath): + """Parsed quad-hexagon grid dataset (UGRID names and dims).""" + return ux.open_grid(gridpath("ugrid", "quad-hexagon", "grid.nc"))._ds + + +@pytest.mark.parametrize("case", STRAY_CASES) +def test_stray_variables_are_dropped(quad_hexagon_ds, case): + """Each stray ``time`` shape listed in #1720 should be gone after + ``Grid.__init__``, with the grid itself unchanged.""" + uxgrid = ux.Grid(STRAY_CASES[case](quad_hexagon_ds), source_grid_spec="UGRID") + + assert not STRAY_NAMES & set(uxgrid._ds.variables) + assert set(uxgrid._ds.dims) <= set(DIM_NAMES) + assert uxgrid.n_face == quad_hexagon_ds.sizes["n_face"] + + +def test_single_file_with_grid_and_time_varying_data(gridpath, tmp_path): + """A file holding the grid and data along ``time``: the grid gets neither.""" + raw = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() + single = raw.assign( + temp=(("time", "n_face"), np.ones((TIMES.size, raw.sizes["n_face"]))) + ).assign_coords(time=("time", TIMES)) + path = tmp_path / "grid_and_data.nc" + single.to_netcdf(path) + + uxds = ux.open_dataset(path) + + assert "temp" not in uxds.uxgrid._ds + assert "time" not in uxds.uxgrid._ds.dims + # the data keeps its own time axis + assert "time" in uxds["temp"].dims + + sub = ( + uxds["temp"] + .sel(time=TIMES[:1]) + .subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) + ) + assert sub.sizes["time"] == 1 + uxds.close() + + +@pytest.mark.parametrize( + "path, kept, dropped", + [ + (("mpas", "QU", "oQU480.231010.nc"), ["boundary_node_indices"], []), + ( + ("icon", "R02B04", "icon_grid_0010_R02B04_G.nc"), + ["clon", "clat", "vlon", "vlat", "elon", "elat"], + [], + ), + ( + ("ugrid", "fesom", "fesom.mesh.diag.nc"), + ["edge_node_connectivity", "grid_topology"], + ["nod_area", "nod_in_elem2D", "edge_cross_dxdy", "nz", "nz1"], + ), + ], + ids=["mpas", "icon", "fesom"], +) +def test_grid_files(gridpath, path, kept, dropped): + """Real grid files keep what uxarray reads from them (MPAS boundary nodes, ICON + coordinates, FESOM2 connectivity); FESOM2's extras on non-grid dims are dropped.""" + uxgrid = ux.open_grid(gridpath(*path)) + + assert set(kept) <= set(uxgrid._ds.variables) + assert not set(dropped) & set(uxgrid._ds.variables) + + +def test_known_variable_on_misnamed_dim_kept(quad_hexagon_ds): + """A known variable on a non-standard dim (the #1717 ``n2`` shape) is kept.""" + uxgrid = ux.Grid(quad_hexagon_ds, source_grid_spec="UGRID") + ds = uxgrid._ds.assign( + edge_node_connectivity=uxgrid.edge_node_connectivity.rename(two="n2") + ) + + rebuilt = ux.Grid(ds, source_grid_spec="UGRID") + + assert rebuilt._ds["edge_node_connectivity"].dims == ("n_edge", "n2") + + +def test_cached_values_kept(gridpath): + """Ensure cached data like ``bounds`` and ``max_face_radius`` is kept when a copy + or subset goes back through ``Grid.__init__``, so it isn't recomputed.""" + uxgrid = ux.open_grid(gridpath("ugrid", "outCSne30", "outCSne30.ug")) + uxgrid.bounds + uxgrid.face_bounds_lon + uxgrid.face_bounds_lat + uxgrid.max_face_radius + cached = {"bounds", "face_bounds_lon", "face_bounds_lat", "max_face_radius"} + + for grid in (uxgrid.copy(), uxgrid.isel(n_face=np.arange(50))): + assert cached <= set(grid._ds.variables) + + +@pytest.mark.parametrize("as_coord", [False, True], ids=["data_var", "scalar_coord"]) +def test_crs_variable_kept_for_projected_detection(gridpath, tmp_path, as_coord): + """A CRS variable named by ``grid_mapping`` is kept, stored either as a scalar + data variable (``crs``) or as a scalar coordinate (``spatial_ref``, as rioxarray + writes it), so projected-grid detection still sees it.""" + crs_name = "spatial_ref" if as_coord else "crs" + ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() + for name in ("node_lon", "node_lat"): + ds[name].attrs = {"grid_mapping": crs_name} + crs = xr.DataArray(0, attrs={"grid_mapping_name": "lambert_conformal_conic"}) + ds = ds.assign_coords({crs_name: crs}) if as_coord else ds.assign({crs_name: crs}) + path = tmp_path / "projected.nc" + ds.to_netcdf(path) + + with pytest.warns(UserWarning, match="Projected"): + uxgrid = ux.open_grid(path) + + assert crs_name in uxgrid._ds + uxgrid._ds.close() + + +def test_opt_out_keeps_everything(gridpath, datasetpath): + """With ``drop_non_grid_data=False`` nothing is dropped, and the setting carries + through ``open_dataset(grid_kwargs=...)``, ``Grid.copy()`` and subsets.""" + grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() + grid_ds = grid_ds.assign_coords(time=np.datetime64("2016-01-01")) + + uxgrid = ux.open_grid(grid_ds, drop_non_grid_data=False) + uxds = ux.open_dataset( + grid_ds, + datasetpath("ugrid", "quad-hexagon", "data.nc"), + grid_kwargs={"drop_non_grid_data": False}, + ) + + # copies and subsets keep the setting + for grid in (uxgrid, uxgrid.copy(), uxgrid.isel(n_face=[0, 1]), uxds.uxgrid): + assert "time" in grid._ds.coords diff --git a/test/test_subset.py b/test/test_subset.py index e9c1cbfeb..6e9c110c6 100644 --- a/test/test_subset.py +++ b/test/test_subset.py @@ -362,38 +362,6 @@ def test_bounding_box_with_stray_grid_time(gridpath): assert "n_face" in res.dims -def test_bounding_box_with_stray_grid_time_dimension(gridpath): - """Subsetting must also work when the grid source carried a full ``time`` *dimension* - (a variable along ``time``) rather than the scalar coordinate of issue #1444. - - A dimensional coordinate has dims ``(time,)``, which is not a subset of the - ``(n_face,)`` ``subgrid_face_indices`` indexer, so — unlike a 0-d coordinate — it - never attaches to that indexer and cannot collide during ``.isel``. The stray - dimension harmlessly rides along in ``Grid._ds`` but never reaches the indexing path. - """ - # grid built from a source carrying a variable along a stray `time` dimension - grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) - uxgrid = ux.open_grid(grid_ds.assign(stray=("time", [0.0, 1.0]))) - - # the bare dimension is not a coordinate, so it is not dropped; that is harmless - assert "time" in uxgrid._ds.dims - - # a face-centered variable carrying its own time coordinate - times = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") - uxda = ux.UxDataArray( - data=np.ones((times.size, uxgrid.n_face)), - dims=("time", "n_face"), - coords={"time": times}, - uxgrid=uxgrid, - ) - - res = uxda.subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) - - assert isinstance(res, ux.UxDataArray) - assert res.sizes["time"] == times.size - assert "n_face" in res.dims - - class TestBoundingBoxSubset: """``subset.bounding_box`` computes bounds for candidate faces only (#1778). diff --git a/uxarray/core/api.py b/uxarray/core/api.py index 9039aa5ae..1c555cf43 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -39,6 +39,7 @@ def open_grid( grid_filename_or_obj: str | os.PathLike[Any] | dict | xr.Dataset, chunks=None, use_dual: bool = False, + drop_non_grid_data: bool = True, **kwargs: dict[str, Any], ): """Constructs and returns a ``Grid`` from a grid file. @@ -64,6 +65,11 @@ def open_grid( See dask chunking for more details. use_dual : bool, optional Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is MPAS + drop_non_grid_data : bool, default=True + Drop variables that don't belong on the grid, such as a ``time`` coordinate or data variables + from a file that holds both the grid and its data. Pass ``False`` to keep every variable the + reader returns. ``ux.open_dataset`` and ``ux.open_mfdataset`` accept it through + ``grid_kwargs``. See :class:`~uxarray.Grid` for what is dropped and kept. **kwargs Additional arguments passed on to ``xarray.open_dataset``. Refer to the :func:`xarray docs ` @@ -106,7 +112,9 @@ def open_grid( nod2d_path = os.path.join(grid_filename_or_obj, "nod2d.out") elem2d_path = os.path.join(grid_filename_or_obj, "elem2d.out") if os.path.isfile(nod2d_path) and os.path.isfile(elem2d_path): - grid = Grid.from_dataset(grid_filename_or_obj) + grid = Grid.from_dataset( + grid_filename_or_obj, drop_non_grid_data=drop_non_grid_data + ) else: _missing = [] if not os.path.isfile(nod2d_path): @@ -128,14 +136,20 @@ def open_grid( elif isinstance(grid_filename_or_obj, xr.Dataset): # Construct a grid from a dataset file - grid = Grid.from_dataset(grid_filename_or_obj, use_dual=use_dual) + grid = Grid.from_dataset( + grid_filename_or_obj, + use_dual=use_dual, + drop_non_grid_data=drop_non_grid_data, + ) else: # Attempt to use Xarray directly for remaining input types grid_ds = _open_dataset_with_fallback( grid_filename_or_obj, chunks=grid_chunks, **kwargs ) - grid = Grid.from_dataset(grid_ds, use_dual=use_dual) + grid = Grid.from_dataset( + grid_ds, use_dual=use_dual, drop_non_grid_data=drop_non_grid_data + ) # Return the grid (and chunks, if requested) in a consistent manner. if return_chunks: diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index 1c41399b2..328d5b570 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -74,7 +74,7 @@ _populate_edge_node_distances, ) from uxarray.grid.point_in_face import _point_in_face_query -from uxarray.grid.utils import _drop_non_grid_coords, make_setter +from uxarray.grid.utils import _drop_non_grid_data, make_setter from uxarray.grid.validation import ( _check_area, _check_connectivity, @@ -149,6 +149,13 @@ class Grid: inverse_indices: xr.Dataset, default=None A dataset of indices that correspond to the original grid, if the grid being constructed is a subset + drop_non_grid_data : bool, default=True + Drop variables that don't belong on the grid, such as a ``time`` coordinate or data + variables carried in from a file that holds both the grid and its data. Variables with no + dimensions, or with any dimension outside the UGRID grid dimensions, are dropped; + variables uxarray reads or caches on the grid are always kept. Pass ``False`` to keep + ``grid_ds`` unchanged. + Examples -------- @@ -174,6 +181,7 @@ def __init__( source_dims_dict: dict | None = None, is_subset: bool = False, inverse_indices: xr.Dataset | None = None, + drop_non_grid_data: bool = True, ): # check if inputted dataset is a minimum representable 2D UGRID unstructured grid if source_grid_spec != "HEALPix": @@ -192,9 +200,11 @@ def __init__( self.source_grid_spec = source_grid_spec # internal xarray dataset for storing grid variables. - # drop stray scalar coordinates (e.g. a `time` carried in from the source file) - # so they can't leak onto the grid and collide during subsetting (see #1444). - self._ds = _drop_non_grid_coords(grid_ds) + # drop variables that don't belong on the grid (e.g. a `time` carried in from a + # file holding both grid and data), so they can't collide during subsetting + # (see #1444, #1720). Kept on the instance so copies and subsets do the same. + self._drop_non_grid_data = drop_non_grid_data + self._ds = _drop_non_grid_data(grid_ds) if drop_non_grid_data else grid_ds # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) self.source_grid_spec = source_grid_spec @@ -274,7 +284,11 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): containing ASCII files represents a FESOM2 grid. use_dual : bool, default=False When reading in MPAS formatted datasets, indicates whether to use the Dual Mesh + **kwargs + ``drop_non_grid_data`` (bool, default=True) is passed on to ``Grid``; see + :class:`Grid` for what it drops. """ + drop_non_grid_data = kwargs.get("drop_non_grid_data", True) if isinstance(dataset, xr.Dataset): # determine grid/mesh specification @@ -330,7 +344,12 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): # FESOM2 ASCII directory. grid_ds, source_dims_dict = _read_fesom2_asci(dataset) source_grid_spec = "FESOM2" - return cls(grid_ds, source_grid_spec, source_dims_dict) + return cls( + grid_ds, + source_grid_spec, + source_dims_dict, + drop_non_grid_data=drop_non_grid_data, + ) except TypeError as err: raise GridInvalidError( f"Expected FESOM2 ASCII directory format but could not parse directory " @@ -352,6 +371,7 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): source_dims_dict, is_subset=kwargs.get("is_subset", False), inverse_indices=kwargs.get("inverse_indices"), + drop_non_grid_data=drop_non_grid_data, ) @classmethod @@ -2072,6 +2092,7 @@ def copy(self): self._ds, source_grid_spec=self.source_grid_spec, source_dims_dict=self._source_dims_dict, + drop_non_grid_data=self._drop_non_grid_data, ) def compute_skewness(self, method: str = "equiangle", *, as_uxarray: bool = False): diff --git a/uxarray/grid/slice.py b/uxarray/grid/slice.py index c68fd6b6e..08a312024 100644 --- a/uxarray/grid/slice.py +++ b/uxarray/grid/slice.py @@ -253,6 +253,12 @@ def _slice_face_indices( source_grid_spec=grid.source_grid_spec, is_subset=True, inverse_indices=inverse_indices_ds, + drop_non_grid_data=grid._drop_non_grid_data, ) - return Grid.from_dataset(ds, source_grid_spec=grid.source_grid_spec, is_subset=True) + return Grid.from_dataset( + ds, + source_grid_spec=grid.source_grid_spec, + is_subset=True, + drop_non_grid_data=grid._drop_non_grid_data, + ) diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index 158b692cb..c1c9e2a7d 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -4,6 +4,7 @@ from uxarray.constants import EDGE_NODE_SORT_THRESHOLD, INT_DTYPE, INT_FILL_VALUE from uxarray.conventions import ugrid +from uxarray.conventions.descriptors import DESCRIPTOR_NAMES from uxarray.utils.numba_math import ( _numba_add3, _numba_cross3, @@ -15,25 +16,52 @@ from uxarray.utils.parallel import parallel_njit -def _drop_non_grid_coords(ds): - """Drop scalar (0-d) coordinates that aren't recognized grid coordinates - (e.g. a stray ``time`` carried in from the source file). +def _drop_non_grid_data(ds): + """Drop variables that don't belong on a ``Grid`` (e.g. a ``time`` carried in + from a file that holds both the grid and its data). - Only 0-d strays can cause the subset collision behind issue #1444: a scalar - coordinate attaches to every variable, including the ``(n_face,)`` - ``subgrid_*_indices`` indexer, where it can clash with a like-named dimension - on the data being indexed. A coordinate with its own dimension (e.g. ICON's - 1-d ``clon``/``clat`` or FESOM2's ``lon``/``lat``) never attaches to that - indexer, so it is left in place and those grids round-trip unchanged. + A variable or coordinate is dropped when it has no dimensions, or when any of + its dimensions is not a UGRID grid dimension (``ugrid.DIM_NAMES``). This covers + a scalar ``time`` coordinate (#1444), a ``time`` dimension coordinate, data + variables along ``time``, and coordinates mixing grid and non-grid dimensions + (#1720). - Coordinate-only, so grid data variables — connectivity, descriptors, and the - subset's ``subgrid_*_indices`` — are always left intact. + Variables uxarray reads or caches on the grid are always kept, whatever their + dimensions: grid coordinates, connectivity, descriptors (including MPAS + ``boundary_node_indices``), the cached ``bounds``, ``face_bounds_lon``, + ``face_bounds_lat`` and ``max_face_radius``, ``grid_topology``, and any CRS + variable named by a ``grid_mapping`` attribute (read by projected-grid + detection). """ - grid_coords = set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) - stray = [ - coord for coord in ds.coords if coord not in grid_coords and ds[coord].ndim == 0 + grid_dims = set(ugrid.DIM_NAMES) + + keep = ( + set(ugrid.SPHERICAL_COORD_NAMES) + | set(ugrid.CARTESIAN_COORD_NAMES) + | set(ugrid.CONNECTIVITY_NAMES) + | set(DESCRIPTOR_NAMES) + | { + "grid_topology", + "bounds", + "face_bounds_lon", + "face_bounds_lat", + "max_face_radius", + } + ) + # CRS variables, referenced by name from a ``grid_mapping`` attribute + # (CF also allows the extended form "crs: x y") + for name in ds.variables: + grid_mapping = ds[name].attrs.get("grid_mapping") + if isinstance(grid_mapping, str): + keep.update(token.rstrip(":") for token in grid_mapping.split()) + + drop = [ + name + for name in ds.variables + if name not in keep + and (ds[name].ndim == 0 or any(dim not in grid_dims for dim in ds[name].dims)) ] - return ds.drop_vars(stray, errors="ignore") + return ds.drop_vars(drop) @njit(cache=True) From bcd52255484b2ca220402b4b429b05150d35a7a5 Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:50:43 -0500 Subject: [PATCH 09/11] moved old test over to new test file --- test/grid/grid/test_non_grid_data.py | 28 +++++++++++++++++++++++++++ test/test_subset.py | 29 ---------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/test/grid/grid/test_non_grid_data.py b/test/grid/grid/test_non_grid_data.py index beb5b9122..709a07283 100644 --- a/test/grid/grid/test_non_grid_data.py +++ b/test/grid/grid/test_non_grid_data.py @@ -50,6 +50,34 @@ def test_stray_variables_are_dropped(quad_hexagon_ds, case): assert uxgrid.n_face == quad_hexagon_ds.sizes["n_face"] +def test_bounding_box_with_stray_grid_time(gridpath): + """Subsetting must work when the grid was built from a source that carried a stray + scalar ``time`` coordinate. Ensures issue #1444 has been fixed. + """ + # grid built from a source carrying a stray scalar `time` + grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) + uxgrid = ux.open_grid(grid_ds.assign_coords(time=np.datetime64("2016-01-01"))) + + # the stray coordinate is dropped during grid construction + assert "time" not in uxgrid._ds.coords + + # a face-centered variable carrying a *different* time coordinate + times = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") + uxda = ux.UxDataArray( + data=np.ones((times.size, uxgrid.n_face)), + dims=("time", "n_face"), + coords={"time": times}, + uxgrid=uxgrid, + ) + + # previously raised: "IndexError: dimension coordinate 'time' conflicts ..." + res = uxda.subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) + + assert isinstance(res, ux.UxDataArray) + assert res.sizes["time"] == times.size + assert "n_face" in res.dims + + def test_single_file_with_grid_and_time_varying_data(gridpath, tmp_path): """A file holding the grid and data along ``time``: the grid gets neither.""" raw = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() diff --git a/test/test_subset.py b/test/test_subset.py index 6e9c110c6..eb3df313e 100644 --- a/test/test_subset.py +++ b/test/test_subset.py @@ -5,7 +5,6 @@ from uxarray.grid.slice import _remap_dense, _remap_kernel, _remap_searchsorted import pytest -import xarray as xr def test_repr(gridpath, datasetpath): @@ -334,34 +333,6 @@ def open_with_edges(**kwargs): ) -def test_bounding_box_with_stray_grid_time(gridpath): - """Subsetting must work when the grid was built from a source that carried a stray - scalar ``time`` coordinate. Ensures issue #1444 has been fixed. - """ - # grid built from a source carrying a stray scalar `time` - grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) - uxgrid = ux.open_grid(grid_ds.assign_coords(time=np.datetime64("2016-01-01"))) - - # the stray coordinate is dropped during grid construction - assert "time" not in uxgrid._ds.coords - - # a face-centered variable carrying a *different* time coordinate - times = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") - uxda = ux.UxDataArray( - data=np.ones((times.size, uxgrid.n_face)), - dims=("time", "n_face"), - coords={"time": times}, - uxgrid=uxgrid, - ) - - # previously raised: "IndexError: dimension coordinate 'time' conflicts ..." - res = uxda.subset.bounding_box(lon_bounds=(-10, 10), lat_bounds=(-10, 10)) - - assert isinstance(res, ux.UxDataArray) - assert res.sizes["time"] == times.size - assert "n_face" in res.dims - - class TestBoundingBoxSubset: """``subset.bounding_box`` computes bounds for candidate faces only (#1778). From 2436673f46e686c6ef6bc3353171229679be765f Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:26:52 -0500 Subject: [PATCH 10/11] shorten docstring --- uxarray/grid/grid.py | 3 +-- uxarray/grid/utils.py | 5 +---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index 328d5b570..a3003c1b6 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -201,8 +201,7 @@ def __init__( # internal xarray dataset for storing grid variables. # drop variables that don't belong on the grid (e.g. a `time` carried in from a - # file holding both grid and data), so they can't collide during subsetting - # (see #1444, #1720). Kept on the instance so copies and subsets do the same. + # file holding both grid and data), so they can't collide during subsetting. self._drop_non_grid_data = drop_non_grid_data self._ds = _drop_non_grid_data(grid_ds) if drop_non_grid_data else grid_ds diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index c1c9e2a7d..5e14d527c 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -21,10 +21,7 @@ def _drop_non_grid_data(ds): from a file that holds both the grid and its data). A variable or coordinate is dropped when it has no dimensions, or when any of - its dimensions is not a UGRID grid dimension (``ugrid.DIM_NAMES``). This covers - a scalar ``time`` coordinate (#1444), a ``time`` dimension coordinate, data - variables along ``time``, and coordinates mixing grid and non-grid dimensions - (#1720). + its dimensions is not a UGRID grid dimension (``ugrid.DIM_NAMES``). Variables uxarray reads or caches on the grid are always kept, whatever their dimensions: grid coordinates, connectivity, descriptors (including MPAS From 92b9b54832de6bb8909b31f61a2d06d1dd8680cb Mon Sep 17 00:00:00 2001 From: Dylan Nelson <44685561+dylannelson@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:03:22 -0500 Subject: [PATCH 11/11] initial attempt at orhans and chris's suggestions --- test/grid/grid/test_non_grid_data.py | 88 ++++++++++++++++++---------- uxarray/core/api.py | 12 +--- uxarray/core/dataarray.py | 8 ++- uxarray/grid/grid.py | 32 +++------- uxarray/grid/slice.py | 8 +-- uxarray/grid/utils.py | 37 +++++------- 6 files changed, 88 insertions(+), 97 deletions(-) diff --git a/test/grid/grid/test_non_grid_data.py b/test/grid/grid/test_non_grid_data.py index 709a07283..0ff898075 100644 --- a/test/grid/grid/test_non_grid_data.py +++ b/test/grid/grid/test_non_grid_data.py @@ -6,6 +6,7 @@ import uxarray as ux from uxarray.conventions.ugrid import DIM_NAMES +from uxarray.grid.utils import _drop_non_grid_data TIMES = np.array(["2018-01-01", "2020-01-01"], dtype="datetime64[ns]") STRAY_NAMES = {"time", "stray", "temp", "time_since_epoch", "special_index", "z"} @@ -27,9 +28,6 @@ "coord_mixing_grid_and_non_grid_dims": lambda ds: ds.assign_coords( z=(("n_node", "elevation"), np.zeros((ds.sizes["n_node"], 3))) ), - "scalar_data_var": lambda ds: ds.assign( - time=xr.DataArray(np.datetime64("2016-01-01")) - ), } @@ -42,8 +40,8 @@ def quad_hexagon_ds(gridpath): @pytest.mark.parametrize("case", STRAY_CASES) def test_stray_variables_are_dropped(quad_hexagon_ds, case): """Each stray ``time`` shape listed in #1720 should be gone after - ``Grid.__init__``, with the grid itself unchanged.""" - uxgrid = ux.Grid(STRAY_CASES[case](quad_hexagon_ds), source_grid_spec="UGRID") + ``Grid.from_dataset``, with the grid itself unchanged.""" + uxgrid = ux.Grid.from_dataset(STRAY_CASES[case](quad_hexagon_ds)) assert not STRAY_NAMES & set(uxgrid._ds.variables) assert set(uxgrid._ds.dims) <= set(DIM_NAMES) @@ -78,6 +76,53 @@ def test_bounding_box_with_stray_grid_time(gridpath): assert "n_face" in res.dims +def test_scalar_time_data_var_does_not_block_subsetting(gridpath): + """A scalar ``time`` stored as a data variable stays on the grid, but it isn't + attached to the subset indexer, so subsetting keeps the data's own ``time``.""" + grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")) + uxgrid = ux.open_grid( + grid_ds.assign(time=xr.DataArray(np.datetime64("2016-01-01"))) + ) + uxda = ux.UxDataArray( + np.ones((2, uxgrid.n_face)), + dims=("time", "n_face"), + coords={"time": TIMES}, + uxgrid=uxgrid, + ) + + res = uxda.subset.bounding_box((-10, 10), (-10, 10)) + + np.testing.assert_array_equal(res["time"].values, TIMES) + + +def test_directly_built_grid_with_stray_time_can_be_subset(quad_hexagon_ds): + """``ux.Grid(ds)`` doesn't filter, so a stray scalar ``time`` stays on the grid; + subsetting data with a different ``time`` must still work.""" + ds = quad_hexagon_ds.assign_coords(time=np.datetime64("2016-01-01")) + uxgrid = ux.Grid(ds, source_grid_spec="UGRID") + uxda = ux.UxDataArray( + np.ones((2, uxgrid.n_face)), + dims=("time", "n_face"), + coords={"time": TIMES}, + uxgrid=uxgrid, + ) + + res = uxda.subset.bounding_box((-10, 10), (-10, 10)) + + np.testing.assert_array_equal(res["time"].values, TIMES) + + +def test_grid_coordinates_not_attached_to_data_subset(gridpath): + """ICON's ``clon``/``clat`` are coordinates on the grid; slicing data shouldn't + copy them onto the result.""" + uxgrid = ux.open_grid(gridpath("icon", "R02B04", "icon_grid_0010_R02B04_G.nc")) + uxda = ux.UxDataArray(np.ones(uxgrid.n_face), dims=("n_face",), uxgrid=uxgrid) + + res = uxda.isel(n_face=[0, 1, 2]) + + assert not {"clon", "clat"} & set(res.coords) + + def test_single_file_with_grid_and_time_varying_data(gridpath, tmp_path): """A file holding the grid and data along ``time``: the grid gets neither.""" raw = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() @@ -136,14 +181,14 @@ def test_known_variable_on_misnamed_dim_kept(quad_hexagon_ds): edge_node_connectivity=uxgrid.edge_node_connectivity.rename(two="n2") ) - rebuilt = ux.Grid(ds, source_grid_spec="UGRID") + cleaned = _drop_non_grid_data(ds) - assert rebuilt._ds["edge_node_connectivity"].dims == ("n_edge", "n2") + assert cleaned["edge_node_connectivity"].dims == ("n_edge", "n2") -def test_cached_values_kept(gridpath): - """Ensure cached data like ``bounds`` and ``max_face_radius`` is kept when a copy - or subset goes back through ``Grid.__init__``, so it isn't recomputed.""" +def test_cached_values_kept_on_reread(gridpath): + """Ensure cached data like ``bounds`` and ``max_face_radius`` survives writing the + grid out with ``to_xarray()`` and reading it back, so it isn't recomputed.""" uxgrid = ux.open_grid(gridpath("ugrid", "outCSne30", "outCSne30.ug")) uxgrid.bounds uxgrid.face_bounds_lon @@ -151,8 +196,9 @@ def test_cached_values_kept(gridpath): uxgrid.max_face_radius cached = {"bounds", "face_bounds_lon", "face_bounds_lat", "max_face_radius"} - for grid in (uxgrid.copy(), uxgrid.isel(n_face=np.arange(50))): - assert cached <= set(grid._ds.variables) + reread = ux.open_grid(uxgrid.to_xarray()) + + assert cached <= set(reread._ds.variables) @pytest.mark.parametrize("as_coord", [False, True], ids=["data_var", "scalar_coord"]) @@ -174,21 +220,3 @@ def test_crs_variable_kept_for_projected_detection(gridpath, tmp_path, as_coord) assert crs_name in uxgrid._ds uxgrid._ds.close() - - -def test_opt_out_keeps_everything(gridpath, datasetpath): - """With ``drop_non_grid_data=False`` nothing is dropped, and the setting carries - through ``open_dataset(grid_kwargs=...)``, ``Grid.copy()`` and subsets.""" - grid_ds = xr.open_dataset(gridpath("ugrid", "quad-hexagon", "grid.nc")).load() - grid_ds = grid_ds.assign_coords(time=np.datetime64("2016-01-01")) - - uxgrid = ux.open_grid(grid_ds, drop_non_grid_data=False) - uxds = ux.open_dataset( - grid_ds, - datasetpath("ugrid", "quad-hexagon", "data.nc"), - grid_kwargs={"drop_non_grid_data": False}, - ) - - # copies and subsets keep the setting - for grid in (uxgrid, uxgrid.copy(), uxgrid.isel(n_face=[0, 1]), uxds.uxgrid): - assert "time" in grid._ds.coords diff --git a/uxarray/core/api.py b/uxarray/core/api.py index d8518c2d7..c0a6336c1 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -39,7 +39,6 @@ def open_grid( grid_filename_or_obj: str | os.PathLike[Any] | dict | xr.Dataset, chunks=None, use_dual: bool = False, - drop_non_grid_data: bool = True, **kwargs: dict[str, Any], ): """Constructs and returns a ``Grid`` from a grid file. @@ -65,11 +64,6 @@ def open_grid( See dask chunking for more details. use_dual : bool, optional Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is MPAS - drop_non_grid_data : bool, default=True - Drop variables that don't belong on the grid, such as a ``time`` coordinate or data variables - from a file that holds both the grid and its data. Pass ``False`` to keep every variable the - reader returns. ``ux.open_dataset`` and ``ux.open_mfdataset`` accept it through - ``grid_kwargs``. See :class:`~uxarray.Grid` for what is dropped and kept. merge_coincident_nodes : bool, default=True Passed through ``**kwargs`` for dataset and file inputs. Set to False to preserve original node connectivity, including faces that would acquire @@ -118,9 +112,7 @@ def open_grid( nod2d_path = os.path.join(grid_filename_or_obj, "nod2d.out") elem2d_path = os.path.join(grid_filename_or_obj, "elem2d.out") if os.path.isfile(nod2d_path) and os.path.isfile(elem2d_path): - grid = Grid.from_dataset( - grid_filename_or_obj, drop_non_grid_data=drop_non_grid_data - ) + grid = Grid.from_dataset(grid_filename_or_obj) else: _missing = [] if not os.path.isfile(nod2d_path): @@ -145,7 +137,6 @@ def open_grid( grid = Grid.from_dataset( grid_filename_or_obj, use_dual=use_dual, - drop_non_grid_data=drop_non_grid_data, merge_coincident_nodes=merge_coincident_nodes, ) @@ -157,7 +148,6 @@ def open_grid( grid = Grid.from_dataset( grid_ds, use_dual=use_dual, - drop_non_grid_data=drop_non_grid_data, merge_coincident_nodes=merge_coincident_nodes, ) diff --git a/uxarray/core/dataarray.py b/uxarray/core/dataarray.py index dd3efc620..8c72b08d0 100644 --- a/uxarray/core/dataarray.py +++ b/uxarray/core/dataarray.py @@ -2488,19 +2488,21 @@ def _slice_from_grid(self, sliced_grid): """Slices a ``UxDataArray`` from a sliced ``Grid``, using cached indices to correctly slice the data variable.""" + # index with the underlying arrays, so coordinates on the sliced grid + # (e.g. a stray ``time``) aren't aligned against, or attached to, the data if self._face_centered(): da_sliced = self.isel( - n_face=sliced_grid._ds["_subgrid_face_indices"], ignore_grid=True + n_face=sliced_grid._ds["_subgrid_face_indices"].data, ignore_grid=True ) elif self._edge_centered(): da_sliced = self.isel( - n_edge=sliced_grid._ds["_subgrid_edge_indices"], ignore_grid=True + n_edge=sliced_grid._ds["_subgrid_edge_indices"].data, ignore_grid=True ) elif self._node_centered(): da_sliced = self.isel( - n_node=sliced_grid._ds["_subgrid_node_indices"], ignore_grid=True + n_node=sliced_grid._ds["_subgrid_node_indices"].data, ignore_grid=True ) else: diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index da3cf1f01..c27bbd532 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -151,13 +151,6 @@ class Grid: inverse_indices: xr.Dataset, default=None A dataset of indices that correspond to the original grid, if the grid being constructed is a subset - drop_non_grid_data : bool, default=True - Drop variables that don't belong on the grid, such as a ``time`` coordinate or data - variables carried in from a file that holds both the grid and its data. Variables with no - dimensions, or with any dimension outside the UGRID grid dimensions, are dropped; - variables uxarray reads or caches on the grid are always kept. Pass ``False`` to keep - ``grid_ds`` unchanged. - Examples -------- @@ -183,7 +176,6 @@ def __init__( source_dims_dict: dict | None = None, is_subset: bool = False, inverse_indices: xr.Dataset | None = None, - drop_non_grid_data: bool = True, merge_coincident_nodes: bool = True, ): # check if inputted dataset is a minimum representable 2D UGRID unstructured grid @@ -218,11 +210,8 @@ def __init__( # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) self.source_grid_spec = source_grid_spec - # internal xarray dataset for storing grid variables. - # drop variables that don't belong on the grid (e.g. a `time` carried in from a - # file holding both grid and data), so they can't collide during subsetting. - self._drop_non_grid_data = drop_non_grid_data - self._ds = _drop_non_grid_data(grid_ds) if drop_non_grid_data else grid_ds + # internal xarray dataset for storing grid variables + self._ds = grid_ds # source grid specification (i.e. UGRID, MPAS, SCRIP, etc.) self.source_grid_spec = source_grid_spec @@ -311,11 +300,7 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): Passed through ``**kwargs``. Set to False to preserve original node connectivity, including faces that would acquire nonconsecutive repeated corners after merging. Dual construction still rejects referenced duplicates. - **kwargs - ``drop_non_grid_data`` (bool, default=True) is passed on to ``Grid``; see - :class:`Grid` for what it drops. """ - drop_non_grid_data = kwargs.get("drop_non_grid_data", True) if isinstance(dataset, xr.Dataset): # determine grid/mesh specification @@ -353,6 +338,10 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): raise GridInvalidError( f"Unsupported source_grid_spec={source_grid_spec!r}, in {cls.__name__}.from_dataset()" ) + # drop variables that don't belong on the grid (e.g. a `time` carried in + # from a file holding both grid and data), so they can't collide during + # subsetting. + grid_ds = _drop_non_grid_data(grid_ds) else: # custom source grid spec is provided source_grid_spec = kwargs.get("source_grid_spec", None) @@ -371,12 +360,7 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): # FESOM2 ASCII directory. grid_ds, source_dims_dict = _read_fesom2_asci(dataset) source_grid_spec = "FESOM2" - return cls( - grid_ds, - source_grid_spec, - source_dims_dict, - drop_non_grid_data=drop_non_grid_data, - ) + return cls(grid_ds, source_grid_spec, source_dims_dict) except TypeError as err: raise GridInvalidError( f"Expected FESOM2 ASCII directory format but could not parse directory " @@ -398,7 +382,6 @@ def from_dataset(cls, dataset, use_dual: bool = False, **kwargs): source_dims_dict, is_subset=kwargs.get("is_subset", False), inverse_indices=kwargs.get("inverse_indices"), - drop_non_grid_data=drop_non_grid_data, merge_coincident_nodes=kwargs.get("merge_coincident_nodes", True), ) @@ -2119,7 +2102,6 @@ def copy(self): self._ds, source_grid_spec=self.source_grid_spec, source_dims_dict=self._source_dims_dict, - drop_non_grid_data=self._drop_non_grid_data, ) def compute_skewness(self, method: str = "equiangle", *, as_uxarray: bool = False): diff --git a/uxarray/grid/slice.py b/uxarray/grid/slice.py index 08a312024..c68fd6b6e 100644 --- a/uxarray/grid/slice.py +++ b/uxarray/grid/slice.py @@ -253,12 +253,6 @@ def _slice_face_indices( source_grid_spec=grid.source_grid_spec, is_subset=True, inverse_indices=inverse_indices_ds, - drop_non_grid_data=grid._drop_non_grid_data, ) - return Grid.from_dataset( - ds, - source_grid_spec=grid.source_grid_spec, - is_subset=True, - drop_non_grid_data=grid._drop_non_grid_data, - ) + return Grid.from_dataset(ds, source_grid_spec=grid.source_grid_spec, is_subset=True) diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index 5e14d527c..1948147fc 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -20,43 +20,38 @@ def _drop_non_grid_data(ds): """Drop variables that don't belong on a ``Grid`` (e.g. a ``time`` carried in from a file that holds both the grid and its data). - A variable or coordinate is dropped when it has no dimensions, or when any of - its dimensions is not a UGRID grid dimension (``ugrid.DIM_NAMES``). - - Variables uxarray reads or caches on the grid are always kept, whatever their - dimensions: grid coordinates, connectivity, descriptors (including MPAS - ``boundary_node_indices``), the cached ``bounds``, ``face_bounds_lon``, - ``face_bounds_lat`` and ``max_face_radius``, ``grid_topology``, and any CRS - variable named by a ``grid_mapping`` attribute (read by projected-grid - detection). + A variable or coordinate is dropped when any of its dimensions is not a grid + dimension (``ugrid.DIM_NAMES``, plus ``lat_lon`` and ``min_max`` used by the + cached face bounds). Scalar coordinates are dropped too; scalar data variables + are kept. + + Grid coordinates, connectivity, descriptors (including MPAS + ``boundary_node_indices``) and any CRS variable named by a ``grid_mapping`` + attribute (read by projected-grid detection) are always kept. """ - grid_dims = set(ugrid.DIM_NAMES) + grid_dims = set(ugrid.DIM_NAMES) | {"lat_lon", "min_max"} keep = ( set(ugrid.SPHERICAL_COORD_NAMES) | set(ugrid.CARTESIAN_COORD_NAMES) | set(ugrid.CONNECTIVITY_NAMES) | set(DESCRIPTOR_NAMES) - | { - "grid_topology", - "bounds", - "face_bounds_lon", - "face_bounds_lat", - "max_face_radius", - } ) # CRS variables, referenced by name from a ``grid_mapping`` attribute # (CF also allows the extended form "crs: x y") - for name in ds.variables: - grid_mapping = ds[name].attrs.get("grid_mapping") + for var in ds.variables.values(): + grid_mapping = var.attrs.get("grid_mapping") if isinstance(grid_mapping, str): keep.update(token.rstrip(":") for token in grid_mapping.split()) drop = [ name - for name in ds.variables + for name, var in ds.variables.items() if name not in keep - and (ds[name].ndim == 0 or any(dim not in grid_dims for dim in ds[name].dims)) + and ( + any(dim not in grid_dims for dim in var.dims) + or (var.ndim == 0 and name in ds.coords) + ) ] return ds.drop_vars(drop)