Skip to content

Commit 8c01bf0

Browse files
Scope assert_valid_lat_lon for >0D grids (#2058)
* Remove XGrid.lonlat_minmax This was in v3 of parcels and isn't needed anymore * Only run XGrid lon/lat assertion on non 0D grids Adds a `.axes` attribute to help with the check (in line with development - see #2054) * Add test stubs * Update varname
1 parent 7baf916 commit 8c01bf0

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

parcels/fieldset.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,6 @@ def add_constant_field(self, name: str, value, mesh: Mesh = "flat"):
161161
da = xr.DataArray(
162162
data=np.full((1, 1, 1, 1), value),
163163
dims=["time", "ZG", "YG", "XG"],
164-
coords={
165-
"ZG": (["ZG"], np.arange(1), {"axis": "Z"}),
166-
"YG": (["YG"], np.arange(1), {"axis": "Y"}),
167-
"XG": (["XG"], np.arange(1), {"axis": "X"}),
168-
"lon": (["XG"], np.arange(1), {"axis": "X"}),
169-
"lat": (["YG"], np.arange(1), {"axis": "Y"}),
170-
"depth": (["ZG"], np.arange(1), {"axis": "Z"}),
171-
"time": (["time"], np.arange(1), {"axis": "T"}),
172-
},
173164
)
174165
grid = XGrid(xgcm.Grid(da))
175166
self.add_field(

parcels/xgrid.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from parcels.basegrid import BaseGrid
1111
from parcels.tools.converters import TimeConverter
1212

13+
_XGRID_AXES_ORDERING = "ZYX"
14+
_XGRID_AXES = Literal["X", "Y", "Z"]
15+
1316
_XGCM_AXIS_DIRECTION = Literal["X", "Y", "Z", "T"]
1417
_XGCM_AXIS_POSITION = Literal["center", "left", "right", "inner", "outer"]
1518
_AXIS_DIRECTION = Literal["X", "Y", "Z"]
@@ -29,6 +32,11 @@ def get_time(axis: xgcm.Axis) -> npt.NDArray:
2932
return axis._ds[axis.coords["center"]].values
3033

3134

35+
def _get_xgrid_axes(grid: xgcm.Grid) -> list[_XGRID_AXES]:
36+
spatial_axes = [a for a in grid.axes.keys() if a in ["X", "Y", "Z"]]
37+
return sorted(spatial_axes, key=_XGRID_AXES_ORDERING.index)
38+
39+
3240
class XGrid(BaseGrid):
3341
"""
3442
Class to represent a structured grid in Parcels. Wraps a xgcm-like Grid object (we use a trimmed down version of the xgcm.Grid class that is vendored with Parcels).
@@ -44,17 +52,13 @@ def __init__(self, grid: xgcm.Grid, mesh="flat"):
4452
self.xgcm_grid = grid
4553
self.mesh = mesh
4654
ds = grid._ds
47-
assert_valid_lat_lon(ds["lat"], ds["lon"], grid.axes)
48-
49-
# ! Not ideal... Triggers computation on a throwaway item. Keeping for now for v3 compat, will be removed in v4.
50-
self.lonlat_minmax = np.array(
51-
[
52-
np.nanmin(self.xgcm_grid._ds["lon"]),
53-
np.nanmax(self.xgcm_grid._ds["lon"]),
54-
np.nanmin(self.xgcm_grid._ds["lat"]),
55-
np.nanmax(self.xgcm_grid._ds["lat"]),
56-
]
57-
)
55+
56+
if len(set(grid.axes) & {"X", "Y", "Z"}) > 0: # Only if spatial grid is >0D (see #2054 for further development)
57+
assert_valid_lat_lon(ds["lat"], ds["lon"], grid.axes)
58+
59+
@property
60+
def axes(self) -> list[_XGRID_AXES]:
61+
return _get_xgrid_axes(self.xgcm_grid)
5862

5963
@property
6064
def lon(self):

tests/v4/test_xgrid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ def test_xgrid_against_old(ds, attr):
7878

7979

8080
@pytest.mark.parametrize("ds", [pytest.param(ds, id=key) for key, ds in datasets.items()])
81-
def test_grid_init_on_generic_datasets(ds):
81+
def test_xgrid_init_on_generic_datasets(ds):
8282
XGrid(xgcm.Grid(ds, periodic=False))
8383

8484

85+
def test_xgrid_axes():
86+
# Tests that the xgrid.axes property correctly identifies the axes and ordering
87+
...
88+
89+
8590
def test_invalid_xgrid_field_array():
8691
"""Stress test initialiser by creating incompatible datasets that test the edge cases"""
8792
...

0 commit comments

Comments
 (0)