Skip to content

Commit f3c59ab

Browse files
Adding check on decreasing depth
1 parent b2da378 commit f3c59ab

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/parcels/_core/xgrid.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ def __init__(self, grid: xgcm.Grid, mesh="flat"):
104104
if "lat" in ds:
105105
ds.set_coords("lat")
106106

107-
if len(set(grid.axes) & {"X", "Y", "Z"}) > 0: # Only if spatial grid is >0D (see #2054 for further development)
107+
if len(set(grid.axes) & {"X", "Y"}) > 0: # Only if spatial grid is >0D (see #2054 for further development)
108108
assert_valid_lat_lon(ds["lat"], ds["lon"], grid.axes)
109109

110+
if "Z" in grid.axes:
111+
assert_valid_depth(ds["depth"])
112+
110113
assert_valid_mesh(mesh)
111114
self._ds = ds
112115

@@ -474,6 +477,11 @@ def assert_valid_lat_lon(da_lat, da_lon, axes: _XGCM_AXES):
474477
)
475478

476479

480+
def assert_valid_depth(da_depth):
481+
if not np.all(np.diff(da_depth.values) > 0):
482+
raise ValueError(f"Depth DataArray {da_depth.name!r} with dims {da_depth.dims} must be strictly increasing.")
483+
484+
477485
def _convert_center_pos_to_fpoint(
478486
*, index: int, bcoord: float, xgcm_position: _XGCM_AXIS_POSITION, f_points_xgcm_position: _XGCM_AXIS_POSITION
479487
) -> tuple[int, float]:

tests/test_xgrid.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def test_invalid_lon_lat():
126126
XGrid.from_dataset(ds)
127127

128128

129+
def test_invalid_depth():
130+
ds = datasets["ds_2d_left"].copy()
131+
ds = ds.reindex({"ZG": -ds.ZG})
132+
133+
with pytest.raises(ValueError, match="Depth DataArray .* must be strictly increasing*"):
134+
XGrid.from_dataset(ds)
135+
136+
129137
@pytest.mark.parametrize(
130138
"ds",
131139
[

0 commit comments

Comments
 (0)