Skip to content

Commit 0d0b0e5

Browse files
committed
Add test_xgrid_localize
1 parent b1f8169 commit 0d0b0e5

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

parcels/_datasets/structured/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def _unrolled_cone_curvilinear_grid():
136136

137137
datasets = {
138138
"2d_left_rotated": _rotated_curvilinear_grid(),
139-
"ds_2d_left": xr.Dataset(
139+
"ds_2d_left": xr.Dataset( # MITgcm indexing style
140140
{
141141
"data_g": (["time", "ZG", "YG", "XG"], np.random.rand(T, Z, Y, X)),
142142
"data_c": (["time", "ZC", "YC", "XC"], np.random.rand(T, Z, Y, X)),
@@ -178,7 +178,7 @@ def _unrolled_cone_curvilinear_grid():
178178
"time": (["time"], TIME, {"axis": "T"}),
179179
},
180180
),
181-
"ds_2d_right": xr.Dataset(
181+
"ds_2d_right": xr.Dataset( # NEMO indexing style
182182
{
183183
"data_g": (["time", "ZG", "YG", "XG"], np.random.rand(T, Z, Y, X)),
184184
"data_c": (["time", "ZC", "YC", "XC"], np.random.rand(T, Z, Y, X)),

tests/v4/test_xgrid.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,57 @@ def test_search_1d_array(array, x, expected_xi, expected_xsi):
145145
xi, xsi = _search_1d_array(array, x)
146146
assert xi == expected_xi
147147
assert np.isclose(xsi, expected_xsi)
148+
149+
150+
@pytest.mark.parametrize(
151+
"grid, da_name, expected",
152+
[
153+
pytest.param(
154+
XGrid(xgcm.Grid(datasets["ds_2d_left"], periodic=False)),
155+
"U (C grid)",
156+
{
157+
"XG": (np.int64(0), np.float64(0.0)),
158+
"YC": (np.int64(-1), np.float64(0.5)),
159+
"ZG": (np.int64(0), np.float64(0.0)),
160+
},
161+
id="MITgcm indexing style U (C grid)",
162+
),
163+
pytest.param(
164+
XGrid(xgcm.Grid(datasets["ds_2d_left"], periodic=False)),
165+
"V (C grid)",
166+
{
167+
"XC": (np.int64(-1), np.float64(0.5)),
168+
"YG": (np.int64(0), np.float64(0.0)),
169+
"ZG": (np.int64(0), np.float64(0.0)),
170+
},
171+
id="MITgcm indexing style V (C grid)",
172+
),
173+
pytest.param(
174+
XGrid(xgcm.Grid(datasets["ds_2d_right"], periodic=False)),
175+
"U (C grid)",
176+
{
177+
"XG": (np.int64(0), np.float64(0.0)),
178+
"YC": (np.int64(0), np.float64(0.5)),
179+
"ZG": (np.int64(0), np.float64(0.0)),
180+
},
181+
id="NEMO indexing style U (C grid)",
182+
),
183+
pytest.param(
184+
XGrid(xgcm.Grid(datasets["ds_2d_right"], periodic=False)),
185+
"V (C grid)",
186+
{
187+
"XC": (np.int64(0), np.float64(0.5)),
188+
"YG": (np.int64(0), np.float64(0.0)),
189+
"ZG": (np.int64(0), np.float64(0.0)),
190+
},
191+
id="NEMO indexing style V (C grid)",
192+
),
193+
],
194+
)
195+
def test_xgrid_localize_zero_position(grid, da_name, expected):
196+
"""Test localize function using left and right datasets."""
197+
position = grid.search(0, 0, 0)
198+
da = grid.xgcm_grid._ds[da_name]
199+
200+
local_position = grid.localize(position, da.dims)
201+
assert local_position == expected, f"Expected {expected}, got {local_position}"

0 commit comments

Comments
 (0)