Skip to content

Commit 7baf916

Browse files
committed
Review feedback
1 parent e3bcfed commit 7baf916

5 files changed

Lines changed: 25 additions & 29 deletions

File tree

parcels/_index_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def _search_indices_curvilinear_2d(
352352
if not ((0 <= xsi <= 1) and (0 <= eta <= 1)):
353353
_raise_field_sampling_error(y, x)
354354

355-
return (eta, xsi, yi, xi)
355+
return (yi, eta, xi, xsi)
356356

357357

358358
## TODO : Still need to implement the search_indices_curvilinear

parcels/basegrid.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def search(self, z: float, y: float, x: float, ei=None) -> dict[str, tuple[int,
3838
A dictionary mapping spatial axis names to tuples of (index, barycentric_coordinates).
3939
The returned axes depend on the grid dimensionality and type:
4040
41-
- 3D structured grid: {"X": (xi, xsi), "Y": (yi, eta), "Z": (zi, zeta)}
42-
- 2D structured grid: {"X": (xi, xsi), "Y": (yi, eta)}
41+
- 3D structured grid: {"Z": (zi, zeta), "Y": (yi, eta), "X": (xi, xsi)}
42+
- 2D structured grid: {"Y": (yi, eta), "X": (xi, xsi)}
4343
- 1D structured grid (depth): {"Z": (zi, zeta)}
4444
- Unstructured grid: {"Z": (zi, zeta), "FACE": (fi, bcoords)}
4545
@@ -74,8 +74,8 @@ def ravel_index(self, axis_indices: dict[str, int]) -> int:
7474
A dictionary mapping axis names to their corresponding indices.
7575
The expected keys depend on the grid dimensionality and type:
7676
77-
- 3D structured grid: {"X": xi, "Y": yi, "Z": zi}
78-
- 2D structured grid: {"X": xi, "Y": yi}
77+
- 3D structured grid: {"Z": zi, "Y": yi, "X": xi}
78+
- 2D structured grid: {"Y": yi, "X": xi}
7979
- 1D structured grid: {"Z": zi}
8080
- Unstructured grid: {"Z": zi, "FACE": fi}
8181
@@ -114,8 +114,8 @@ def unravel_index(self, ei: int) -> dict[str, int]:
114114
A dictionary mapping axis names to their corresponding indices.
115115
The returned keys depend on the grid dimensionality and type:
116116
117-
- 3D structured grid: {"X": xi, "Y": yi, "Z": zi}
118-
- 2D structured grid: {"X": xi, "Y": yi}
117+
- 3D structured grid: {"Z": zi, "Y": yi, "X": xi}
118+
- 2D structured grid: {"Y": yi, "X": xi}
119119
- 1D structured grid: {"Z": zi}
120120
- Unstructured grid: {"Z": zi, "FACE": fi}
121121

parcels/xgrid.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
_XGCM_AXES = Mapping[_XGCM_AXIS_DIRECTION, xgcm.Axis]
1717

1818

19-
def get_n_cell_edges_along_dim(axis: xgcm.Axis | None) -> int:
19+
def get_cell_edge_count_along_dim(axis: xgcm.Axis | None) -> int:
2020
if axis is None:
2121
return 1
2222
first_coord = list(axis.coords.items())[0]
@@ -36,7 +36,7 @@ class XGrid(BaseGrid):
3636
This class provides methods and properties required for indexing and interpolating on the grid.
3737
3838
Assumptions:
39-
- If using Parcels in the context of a periodic simulation, the provided grid already has a halo
39+
- If using Parcels in the context of a spatially periodic simulation, the provided grid already has a halo
4040
4141
"""
4242

@@ -112,19 +112,19 @@ def time(self):
112112

113113
@property
114114
def xdim(self):
115-
return get_n_cell_edges_along_dim(self.xgcm_grid.axes.get("X"))
115+
return get_cell_edge_count_along_dim(self.xgcm_grid.axes.get("X"))
116116

117117
@property
118118
def ydim(self):
119-
return get_n_cell_edges_along_dim(self.xgcm_grid.axes.get("Y"))
119+
return get_cell_edge_count_along_dim(self.xgcm_grid.axes.get("Y"))
120120

121121
@property
122122
def zdim(self):
123-
return get_n_cell_edges_along_dim(self.xgcm_grid.axes.get("Z"))
123+
return get_cell_edge_count_along_dim(self.xgcm_grid.axes.get("Z"))
124124

125125
@property
126126
def tdim(self):
127-
return get_n_cell_edges_along_dim(self.xgcm_grid.axes.get("T"))
127+
return get_cell_edge_count_along_dim(self.xgcm_grid.axes.get("T"))
128128

129129
@property
130130
def time_origin(self):
@@ -177,7 +177,7 @@ def search(self, z, y, x, ei=None):
177177
if ds.lon.ndim == 1:
178178
yi, eta = _search_1d_array(ds.lat.values, y)
179179
xi, xsi = _search_1d_array(ds.lon.values, x)
180-
return {"X": (xi, xsi), "Y": (yi, eta), "Z": (zi, zeta)}
180+
return {"Z": (zi, zeta), "Y": (yi, eta), "X": (xi, xsi)}
181181

182182
yi, xi = None, None
183183
if ei is not None:
@@ -186,9 +186,9 @@ def search(self, z, y, x, ei=None):
186186
yi = axis_indices.get("Y")
187187

188188
if ds.lon.ndim == 2:
189-
eta, xsi, yi, xi = _search_indices_curvilinear_2d(self, y, x, yi, xi)
189+
yi, eta, xi, xsi = _search_indices_curvilinear_2d(self, y, x, yi, xi)
190190

191-
return {"X": (xi, xsi), "Y": (yi, eta), "Z": (zi, zeta)}
191+
return {"Z": (zi, zeta), "Y": (yi, eta), "X": (xi, xsi)}
192192

193193
raise NotImplementedError("Searching in >2D lon/lat arrays is not implemented yet.")
194194

@@ -204,11 +204,7 @@ def unravel_index(self, ei) -> dict[_AXIS_DIRECTION, int]:
204204

205205
yi = ei // self.xdim
206206
xi = ei % self.xdim
207-
return {
208-
"X": xi,
209-
"Y": yi,
210-
"Z": zi,
211-
}
207+
return {"Z": zi, "Y": yi, "X": xi}
212208

213209

214210
def get_axis_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_DIRECTION | None:
@@ -219,7 +215,7 @@ def get_axis_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_DIRECTION |
219215
return None
220216

221217

222-
def get_position_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_POSITION | None:
218+
def get_xgcm_position_from_dim_name(axes: _XGCM_AXES, dim: str) -> _XGCM_AXIS_POSITION | None:
223219
"""For a given dimension, returns the position of the variable in the grid."""
224220
for axis in axes.values():
225221
var_to_position = {var: position for position, var in axis.coords.items()}
@@ -281,16 +277,16 @@ def assert_valid_lat_lon(da_lat, da_lon, axes: _XGCM_AXES):
281277
assert_all_dimensions_correspond_with_axis(da_lon, axes)
282278
assert_all_dimensions_correspond_with_axis(da_lat, axes)
283279

284-
dim_to_position = {dim: get_position_from_dim_name(axes, dim) for dim in da_lon.dims}
285-
dim_to_position.update({dim: get_position_from_dim_name(axes, dim) for dim in da_lat.dims})
280+
dim_to_position = {dim: get_xgcm_position_from_dim_name(axes, dim) for dim in da_lon.dims}
281+
dim_to_position.update({dim: get_xgcm_position_from_dim_name(axes, dim) for dim in da_lat.dims})
286282

287283
for dim in da_lon.dims:
288-
if get_position_from_dim_name(axes, dim) == "center":
284+
if get_xgcm_position_from_dim_name(axes, dim) == "center":
289285
raise ValueError(
290286
f"Longitude DataArray {da_lon.name!r} with dims {da_lon.dims} is defined on the center of the grid, but must be defined on the F points."
291287
)
292288
for dim in da_lat.dims:
293-
if get_position_from_dim_name(axes, dim) == "center":
289+
if get_xgcm_position_from_dim_name(axes, dim) == "center":
294290
raise ValueError(
295291
f"Latitude DataArray {da_lat.name!r} with dims {da_lat.dims} is defined on the center of the grid, but must be defined on the F points."
296292
)

tests/v4/test_index_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_grid_indexing_fpoints(field_cone):
3030
x = grid.lon[yi_expected, xi_expected] + 0.00001
3131
y = grid.lat[yi_expected, xi_expected] + 0.00001
3232

33-
eta, xsi, yi, xi = _search_indices_curvilinear_2d(grid, y, x)
33+
yi, eta, xi, xsi = _search_indices_curvilinear_2d(grid, y, x)
3434
if eta > 0.9:
3535
yi_expected -= 1
3636
if xsi > 0.9:

tests/v4/test_xgrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_xgrid_ravel_unravel_index():
129129
for xi in range(xdim):
130130
for yi in range(ydim):
131131
for zi in range(zdim):
132-
axis_indices = {"X": xi, "Y": yi, "Z": zi}
132+
axis_indices = {"Z": zi, "Y": yi, "X": xi}
133133
ei = grid.ravel_index(axis_indices)
134134
axis_indices_test = grid.unravel_index(ei)
135135
assert axis_indices_test == axis_indices
@@ -155,7 +155,7 @@ def test_xgrid_search_cpoints(ds):
155155

156156
for xi in range(grid.xdim - 1):
157157
for yi in range(grid.ydim - 1):
158-
axis_indices = {"X": xi, "Y": yi, "Z": 0}
158+
axis_indices = {"Z": 0, "Y": yi, "X": xi}
159159

160160
lat, lon = lat_array[yi, xi], lon_array[yi, xi]
161161
axis_indices_bcoords = grid.search(0, lat, lon, ei=None)

0 commit comments

Comments
 (0)