From 5fe82fc485425569e57cdd15639379e5c254efba Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Wed, 29 Apr 2026 15:39:25 +0200 Subject: [PATCH 1/4] Fix 2590 --- src/parcels/_core/xgrid.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/parcels/_core/xgrid.py b/src/parcels/_core/xgrid.py index efa69dce9..3bc4242c2 100644 --- a/src/parcels/_core/xgrid.py +++ b/src/parcels/_core/xgrid.py @@ -52,6 +52,19 @@ def assert_all_field_dims_have_axis(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> N f'HINT: You may want to add an {{"axis": A}} to your DataSet["{dim[1]}"], where A is one of "X", "Y", "Z" or "T"' ) + ax_dims = cast(dict[str, str], ax_dims) + + seen_axes: dict[str, str] = {} + for ax, dim_name in ax_dims: + if ax in seen_axes: + raise ValueError( + f"Two dimensions ({dim_name!r} and {seen_axes[ax]!r}) provide values in the axis direction {ax!r}. " + "This is not possible, a field cannot have two dimensions on a single axis." + ) + seen_axes[ax] = dim_name + assert len(ax_dims) <= 4 + return + def _transpose_xfield_data_to_tzyx(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> xr.DataArray: """ From 628efe245a9830b361a481313c36d658faf53bdd Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Thu, 30 Apr 2026 13:40:42 +0200 Subject: [PATCH 2/4] Add test_dim_with_duplicate_axis --- tests/test_xgrid.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/test_xgrid.py b/tests/test_xgrid.py index 0029e6328..53b0124e8 100644 --- a/tests/test_xgrid.py +++ b/tests/test_xgrid.py @@ -6,7 +6,7 @@ import xarray as xr from numpy.testing import assert_allclose -from parcels import Field +from parcels import Field, FieldSet from parcels._core.index_search import ( LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS, @@ -17,7 +17,7 @@ XGrid, _transpose_xfield_data_to_tzyx, ) -from parcels._datasets.structured.generic import X, Y, Z, datasets +from parcels._datasets.structured.generic import X, Y, Z, datasets, datasets_sgrid from parcels.interpolators import XLinear from tests import utils @@ -151,6 +151,29 @@ def test_dim_without_axis(): Field("z1d", ds["z1d"], grid, XLinear) +def test_dim_with_duplicate_axis(): + ds = datasets_sgrid["ds_2d_padded_low"].copy() + + # Add an extra Z axis + ds = ds[["data_g", "grid"]] + ds["data_g"] = ds["data_g"].expand_dims("vertical_dimensions_dim2", 1) + + z = ds["vertical_dimensions_dim2"] + z.attrs.update({"axis": "Z", "c_grid_axis_shift": 0}) + ds["vertical_dimensions_dim2"] = z + + # TODO: Clean up this attribute setting (really, this should be on the source datasets) + lon = ds["lon"] + lat = ds["lat"] + lon.attrs.update({"units": "metres"}) + lat.attrs.update({"units": "metres"}) + ds["lon"] = lon + ds["lat"] = lat + + with pytest.raises(ValueError, match="Two dimensions .*provide values in the axis direction 'Z'."): + FieldSet.from_sgrid_conventions(ds) + + def test_vertical1D_field(): nz = 11 ds = xr.Dataset( From c927fa70cee349f6de638dca88dddecc4e99ffab Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 4 May 2026 14:48:23 +0200 Subject: [PATCH 3/4] Update assert statement --- src/parcels/_core/xgrid.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parcels/_core/xgrid.py b/src/parcels/_core/xgrid.py index 3bc4242c2..ff4195de4 100644 --- a/src/parcels/_core/xgrid.py +++ b/src/parcels/_core/xgrid.py @@ -62,7 +62,9 @@ def assert_all_field_dims_have_axis(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> N "This is not possible, a field cannot have two dimensions on a single axis." ) seen_axes[ax] = dim_name - assert len(ax_dims) <= 4 + assert len(ax_dims) <= 4, ( + "Execution should never reach this point. Please file an issue sharing more about your input dataset." + ) return From af73d52c4f39b9edd37e03fb72172c3d50d6ea77 Mon Sep 17 00:00:00 2001 From: Nick Hodgskin <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 4 May 2026 16:07:26 +0200 Subject: [PATCH 4/4] Update src/parcels/_core/xgrid.py Co-authored-by: Erik van Sebille --- src/parcels/_core/xgrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parcels/_core/xgrid.py b/src/parcels/_core/xgrid.py index ff4195de4..3edd0875c 100644 --- a/src/parcels/_core/xgrid.py +++ b/src/parcels/_core/xgrid.py @@ -63,7 +63,7 @@ def assert_all_field_dims_have_axis(da: xr.DataArray, xgcm_grid: xgcm.Grid) -> N ) seen_axes[ax] = dim_name assert len(ax_dims) <= 4, ( - "Execution should never reach this point. Please file an issue sharing more about your input dataset." + "The input dataset appears to have more than 4 dimensions after conversion. Execution should never reach this point. Please file an issue sharing more about your input dataset." ) return