Skip to content

Commit e6ffe01

Browse files
committed
Update UxGrid ravelling to match BaseGrid API
1 parent ce60db7 commit e6ffe01

2 files changed

Lines changed: 9 additions & 50 deletions

File tree

parcels/uxgrid.py

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from uxarray.grid.neighbors import _barycentric_coordinates
88

99
from parcels.field import FieldOutOfBoundError # Adjust import as necessary
10+
from parcels.xgrid import _search_1d_array
1011

1112
from .basegrid import BaseGrid
1213

@@ -62,18 +63,7 @@ def try_face(fid):
6263
return bcoords, fid
6364
return None, None
6465

65-
def find_vertical_index() -> int:
66-
nz = self.z.shape[0]
67-
if nz == 1:
68-
return 0
69-
zf = self.z.values
70-
# Return zi such that zf[zi] <= z < zf[zi+1]
71-
zi = np.searchsorted(zf, z, side="right") - 1 # Search assumes that z is positive and increasing with i
72-
if zi < 0 or zi >= nz - 1:
73-
raise FieldOutOfBoundError(z, y, x)
74-
return zi
75-
76-
zi = find_vertical_index() # Find the vertical cell center nearest to z
66+
zi, zeta = _search_1d_array(self.z.values, z)
7767

7868
if ei is not None:
7969
_, fi = self.unravel_index(ei)
@@ -93,7 +83,7 @@ def find_vertical_index() -> int:
9383
if fi == -1:
9484
raise FieldOutOfBoundError(z, y, x)
9585

96-
return bcoords[0], self.ravel_index(zi, fi[0])
86+
return {"Z": (zi, zeta), "CELL": (fi, bcoords[0])}
9787

9888
def _get_barycentric_coordinates(self, y, x, fi):
9989
"""Checks if a point is inside a given face id on a UxGrid."""
@@ -112,40 +102,10 @@ def _get_barycentric_coordinates(self, y, x, fi):
112102
err = abs(np.dot(bcoord, nodes[:, 0]) - coord[0]) + abs(np.dot(bcoord, nodes[:, 1]) - coord[1])
113103
return bcoord, err
114104

115-
def ravel_index(self, zi, fi):
116-
"""
117-
Converts a face index and a vertical index into a single encoded index.
118-
119-
Parameters
120-
----------
121-
zi : int
122-
Vertical index (not used in unstructured grids, but kept for compatibility).
123-
fi : int
124-
Face index.
125-
126-
Returns
127-
-------
128-
int
129-
Encoded index combining the face index and vertical index.
130-
"""
131-
return fi + self.uxgrid.n_face * zi
105+
def ravel_index(self, axis_indices: dict[_UXGRID_AXES, int]):
106+
return axis_indices["CELL"] + self.uxgrid.n_face * axis_indices["Z"]
132107

133-
def unravel_index(self, ei):
134-
"""
135-
Converts a single encoded index back into a vertical index and face index.
136-
137-
Parameters
138-
----------
139-
ei : int
140-
Encoded index to be unraveled.
141-
142-
Returns
143-
-------
144-
zi : int
145-
Vertical index.
146-
fi : int
147-
Face index.
148-
"""
108+
def unravel_index(self, ei) -> dict[_UXGRID_AXES, int]:
149109
zi = ei // self.uxgrid.n_face
150110
fi = ei % self.uxgrid.n_face
151-
return zi, fi
111+
return {"Z": zi, "CELL": fi}

parcels/xgrid.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
_XGCM_AXIS_DIRECTION = Literal["X", "Y", "Z", "T"]
1717
_XGCM_AXIS_POSITION = Literal["center", "left", "right", "inner", "outer"]
18-
_AXIS_DIRECTION = Literal["X", "Y", "Z"]
1918
_XGCM_AXES = Mapping[_XGCM_AXIS_DIRECTION, xgcm.Axis]
2019

2120

@@ -196,13 +195,13 @@ def search(self, z, y, x, ei=None):
196195

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

199-
def ravel_index(self, axis_indices: dict[_AXIS_DIRECTION, int]) -> int:
198+
def ravel_index(self, axis_indices: dict[_XGRID_AXES, int]) -> int:
200199
xi = axis_indices.get("X", 0)
201200
yi = axis_indices.get("Y", 0)
202201
zi = axis_indices.get("Z", 0)
203202
return xi + self.xdim * yi + self.xdim * self.ydim * zi
204203

205-
def unravel_index(self, ei) -> dict[_AXIS_DIRECTION, int]:
204+
def unravel_index(self, ei) -> dict[_XGRID_AXES, int]:
206205
zi = ei // (self.xdim * self.ydim)
207206
ei = ei % (self.xdim * self.ydim)
208207

0 commit comments

Comments
 (0)