Skip to content

Commit d6e5686

Browse files
Merge pull request #2050 from OceanParcels/patch-no-time-interval-index-search
Patch no time interval index search
2 parents ce4024b + 15c1f59 commit d6e5686

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

parcels/_datasets/unstructured/generic.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def _stommel_gyre_delaunay():
4444
uxgrid.attrs["Conventions"] = "UGRID-1.0"
4545

4646
# Define arrays U (zonal), V (meridional) and P (sea surface height)
47-
U = np.zeros((1, nz1, lat.size), dtype=np.float64)
48-
V = np.zeros((1, nz1, lat.size), dtype=np.float64)
47+
U = np.zeros((1, nz1, uxgrid.n_face), dtype=np.float64)
48+
V = np.zeros((1, nz1, uxgrid.n_face), dtype=np.float64)
4949
W = np.zeros((1, nz, lat.size), dtype=np.float64)
50-
P = np.zeros((1, nz1, lat.size), dtype=np.float64)
50+
P = np.zeros((1, nz1, uxgrid.n_face), dtype=np.float64)
5151

52-
for i, (x, y) in enumerate(zip(lon_flat, lat_flat, strict=False)):
52+
for i, (x, y) in enumerate(zip(uxgrid.face_lon, uxgrid.face_lat, strict=False)):
5353
xi = x / 60.0
5454
yi = y / 60.0
5555

@@ -61,10 +61,10 @@ def _stommel_gyre_delaunay():
6161
data=U,
6262
name="U",
6363
uxgrid=uxgrid,
64-
dims=["time", "nz1", "n_node"],
64+
dims=["time", "nz1", "n_face"],
6565
coords=dict(
6666
time=(["time"], [TIME[0]]),
67-
nz1=(["nz1"], [0]),
67+
nz1=(["nz1"], zc),
6868
),
6969
attrs=dict(
7070
description="zonal velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
@@ -74,7 +74,7 @@ def _stommel_gyre_delaunay():
7474
data=V,
7575
name="V",
7676
uxgrid=uxgrid,
77-
dims=["time", "nz1", "n_node"],
77+
dims=["time", "nz1", "n_face"],
7878
coords=dict(
7979
time=(["time"], [TIME[0]]),
8080
nz1=(["nz1"], zc),
@@ -100,7 +100,7 @@ def _stommel_gyre_delaunay():
100100
data=P,
101101
name="p",
102102
uxgrid=uxgrid,
103-
dims=["time", "nz1", "n_node"],
103+
dims=["time", "nz1", "n_face"],
104104
coords=dict(
105105
time=(["time"], [TIME[0]]),
106106
nz1=(["nz1"], zc),

parcels/_index_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _search_time_index(field: Field, time: datetime):
3838
if the sampled value is outside the time value range.
3939
"""
4040
if field.time_interval is None:
41-
return 0
41+
return 0, 0
4242

4343
if time not in field.time_interval:
4444
_raise_time_extrapolation_error(time, field=None)

tests/v4/test_field.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ def test_field_unstructured_z_linear():
147147
assert np.isclose(W.eval(time=ds.time[0].values, z=900.0, y=30.0, x=30.0, applyConversion=False), 900.0)
148148

149149

150+
def test_field_constant_in_time():
151+
"""Tests field evaluation for a field with no time interval (i.e., constant in time)."""
152+
ds = datasets_unstructured["stommel_gyre_delaunay"]
153+
grid = UxGrid(ds.uxgrid, z=ds.coords["nz"])
154+
# Note that the vertical coordinate is required to be the position of the layer interfaces ("nz"), not the mid-layers ("nz1")
155+
P = Field(name="p", data=ds.p, grid=grid, interp_method=UXPiecewiseConstantFace)
156+
157+
# Assert that the field can be evaluated at any time, and returns the same value
158+
time = np.datetime64("2000-01-01T00:00:00")
159+
P1 = P.eval(time=time, z=10.0, y=30.0, x=30.0, applyConversion=False)
160+
P2 = P.eval(time=time + np.timedelta64(1, "D"), z=10.0, y=30.0, x=30.0, applyConversion=False)
161+
assert np.isclose(P1, P2)
162+
163+
150164
def test_field_unstructured_grid_creation(): ...
151165

152166

0 commit comments

Comments
 (0)