Skip to content

Commit b1f5f1c

Browse files
[#2031] Add vertical search for layer index
1 parent 90e73df commit b1f5f1c

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

parcels/uxgrid.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, grid: ux.grid.Grid, z: ux.UxDataArray) -> UxGrid:
2424
grid : ux.grid.Grid
2525
The uxarray grid object containing the unstructured grid data.
2626
z : ux.UxDataArray
27-
A 1D array of vertical coordinates (depths) corresponding to the vertical position of layer faces of the grid
27+
A 1D array of vertical coordinates (depths) associated with the layer interface heights (not the mid-layer depths).
2828
While uxarray allows nz to be spatially and temporally varying, the parcels.UxGrid class considers the case where
2929
the vertical coordinate is constant in time and space. This implies flat bottom topography and no moving ALE vertical grid.
3030
"""
@@ -47,22 +47,23 @@ def try_face(fid):
4747
return None, None
4848

4949
def find_vertical_index() -> int:
50-
nz = self.z.shape[0]
51-
if nz == 1:
50+
if search2D:
5251
return 0
53-
zf = self.z.values
54-
zi = np.searchsorted(zf, z, side="right") - 1 # Search assumes that z is positive and increasing with i
55-
if zi < 0 or zi >= nz - 1:
56-
raise FieldOutOfBoundError(z, y, x)
57-
return zi
58-
59-
if not search2D:
60-
zi = find_vertical_index() # Find the vertical cell center nearest to z
61-
else:
62-
zi = 0
52+
else:
53+
nz = self.z.shape[0]
54+
if nz == 1:
55+
return 0
56+
zf = self.z.values
57+
# Return zi such that zf[zi] <= z < zf[zi+1]
58+
zi = np.searchsorted(zf, z, side="right") - 1 # Search assumes that z is positive and increasing with i
59+
if zi < 0 or zi >= nz - 1:
60+
raise FieldOutOfBoundError(z, y, x)
61+
return zi
62+
63+
zi = find_vertical_index() # Find the vertical cell center nearest to z
6364

6465
if ei is not None:
65-
zi, fi = self.unravel_index(ei)
66+
_, fi = self.unravel_index(ei)
6667
bcoords, fi_new = try_face(fi)
6768
if bcoords is not None:
6869
return bcoords, self.ravel_index(zi, fi_new)
@@ -79,7 +80,7 @@ def find_vertical_index() -> int:
7980
if fi == -1:
8081
raise FieldOutOfBoundError(z, y, x)
8182

82-
return bcoords, self.ravel_index(zi, fi)
83+
return bcoords[0], self.ravel_index(zi, fi[0])
8384

8485
def _get_barycentric_coordinates(self, y, x, fi):
8586
"""Checks if a point is inside a given face id on a UxGrid."""

0 commit comments

Comments
 (0)