Skip to content

Commit bb37185

Browse files
[#2031] Change z to be ux.UxDataArray; require 1d vertical coordinate
1 parent 3f5afe7 commit bb37185

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

parcels/uxgrid.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@ class UxGrid(BaseGrid):
1515
for interpolation on unstructured grids.
1616
"""
1717

18-
def __init__(self, grid: ux.grid.Grid, z: np.ndarray) -> UxGrid:
18+
def __init__(self, grid: ux.grid.Grid, z: ux.UxDataArray) -> UxGrid:
1919
"""
2020
Initializes the UxGrid with a uxarray grid and vertical coordinate array.
2121
2222
Parameters
2323
----------
2424
grid : ux.grid.Grid
2525
The uxarray grid object containing the unstructured grid data.
26-
z : np.ndarray
26+
z : ux.UxDataArray
2727
A 1D array of vertical coordinates (depths) corresponding to the vertical position of layer faces of the grid
28+
While uxarray allows nz to be spatially and temporally varying, the parcels.UxGrid class considers the case where
29+
the vertical coordinate is constant in time and space. This implies flat bottom topography and no moving ALE vertical grid.
2830
"""
2931
self.uxgrid = grid
32+
if not isinstance(z, ux.UxDataArray):
33+
raise TypeError("z must be an instance of ux.UxDataArray")
34+
if z.ndim != 1:
35+
raise ValueError("z must be a 1D array of vertical coordinates")
3036
self.z = z
3137

3238
def search(
@@ -41,10 +47,12 @@ def try_face(fid):
4147
return None, None
4248

4349
def find_vertical_index() -> int:
44-
if len(self.z) == 1:
50+
nz = self.z.shape[0]
51+
if nz == 1:
4552
return 0
46-
zi = np.searchsorted(self.z, z, side="right") - 1 # Search assumes that z is positive and increasing with i
47-
if zi < 0 or zi >= len(self.z) - 1:
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:
4856
raise FieldOutOfBoundError(z, y, x)
4957
return zi
5058

0 commit comments

Comments
 (0)