Skip to content

Commit ae416ad

Browse files
committed
Update Field.eval and VectorField eval to match new grid search API
1 parent e6ffe01 commit ae416ad

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

parcels/application_kernels/interpolation.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"""Collection of pre-built interpolation kernels."""
22

3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
37
import numpy as np
48

59
from parcels.field import Field
610

11+
if TYPE_CHECKING:
12+
from parcels.uxgrid import _UXGRID_AXES
13+
714
__all__ = [
815
"UXPiecewiseConstantFace",
916
"UXPiecewiseLinearNode",
@@ -13,8 +20,7 @@
1320
def UXPiecewiseConstantFace(
1421
field: Field,
1522
ti: int,
16-
ei: int,
17-
bcoords: np.ndarray,
23+
position: dict[_UXGRID_AXES, tuple[int, float | np.ndarray]],
1824
tau: np.float32 | np.float64,
1925
t: np.float32 | np.float64,
2026
z: np.float32 | np.float64,
@@ -26,15 +32,13 @@ def UXPiecewiseConstantFace(
2632
This interpolation method is appropriate for fields that are
2733
face registered, such as u,v in FESOM.
2834
"""
29-
zi, fi = field.grid.unravel_index(ei)
30-
return field.data.values[ti, zi, fi]
35+
return field.data.values[ti, position["Z"][0], position["CELL"][0]]
3136

3237

3338
def UXPiecewiseLinearNode(
3439
field: Field,
3540
ti: int,
36-
ei: int,
37-
bcoords: np.ndarray,
41+
position: dict[_UXGRID_AXES, tuple[int, float | np.ndarray]],
3842
tau: np.float32 | np.float64,
3943
t: np.float32 | np.float64,
4044
z: np.float32 | np.float64,
@@ -47,7 +51,8 @@ def UXPiecewiseLinearNode(
4751
velocity W in FESOM2. Effectively, it applies barycentric interpolation in the lateral direction
4852
and piecewise linear interpolation in the vertical direction.
4953
"""
50-
k, fi = field.grid.unravel_index(ei)
54+
k, fi = position["Z"][0], position["CELL"][0]
55+
bcoords = position["CELL"][1]
5156
node_ids = field.grid.uxgrid.face_node_connectivity[fi, :]
5257
# The zi refers to the vertical layer index. The field in this routine are assumed to be defined at the vertical interface levels.
5358
# For interface zi, the interface indices are [zi, zi+1], so we need to use the values at zi and zi+1.

parcels/field.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ class Field:
105105
def _interp_template(
106106
self,
107107
ti: int,
108-
ei: int,
109-
bcoords: np.ndarray,
108+
position: dict[str, tuple[int, float | np.ndarray]],
110109
tau: np.float32 | np.float64,
111110
t: np.float32 | np.float64,
112111
z: np.float32 | np.float64,
@@ -316,8 +315,8 @@ def eval(self, time: datetime, z, y, x, particle=None, applyConversion=True):
316315

317316
try:
318317
tau, ti = _search_time_index(self, time)
319-
bcoords, _ei = self.grid.search(z, y, x, ei=_ei)
320-
value = self._interp_method(self, ti, _ei, bcoords, tau, time, z, y, x)
318+
position = self.grid.search(z, y, x, ei=_ei)
319+
value = self._interp_method(self, ti, position, tau, time, z, y, x)
321320

322321
if np.isnan(value):
323322
# Detect Out-of-bounds sampling and raise exception
@@ -445,14 +444,14 @@ def eval(self, time: datetime, z, y, x, particle=None, applyConversion=True):
445444

446445
try:
447446
tau, ti = _search_time_index(self.U, time)
448-
bcoords, _ei = self.grid.search(z, y, x, ei=_ei)
447+
position = self.grid.search(z, y, x, ei=_ei)
449448
if self._vector_interp_method is None:
450-
u = self.U._interp_method(self.U, ti, _ei, bcoords, tau, time, z, y, x)
451-
v = self.V._interp_method(self.V, ti, _ei, bcoords, tau, time, z, y, x)
449+
u = self.U._interp_method(self.U, ti, position, tau, time, z, y, x)
450+
v = self.V._interp_method(self.V, ti, position, tau, time, z, y, x)
452451
if "3D" in self.vector_type:
453-
w = self.W._interp_method(self.W, ti, _ei, bcoords, tau, time, z, y, x)
452+
w = self.W._interp_method(self.W, ti, position, tau, time, z, y, x)
454453
else:
455-
(u, v, w) = self._vector_interp_method(self, ti, _ei, bcoords, time, z, y, x)
454+
(u, v, w) = self._vector_interp_method(self, ti, position, time, z, y, x)
456455

457456
# print(u,v)
458457
if applyConversion:

0 commit comments

Comments
 (0)