Skip to content

Commit c1f42cb

Browse files
[#2010] Add Conventions to sample dataset; add associated test
1 parent bc80e81 commit c1f42cb

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

parcels/_datasets/unstructured/generic.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from datetime import datetime, timedelta
23

34
import numpy as np
45
import pandas as pd
@@ -35,6 +36,7 @@ def _stommel_gyre_delaunay():
3536
method="regional_delaunay",
3637
boundary_points=boundary_points,
3738
)
39+
uxgrid.attrs["Conventions"] = "UGRID-1.0"
3840

3941
# Define arrays U (zonal), V (meridional) and P (sea surface height)
4042
U = np.zeros((1, 1, lat.size), dtype=np.float64)
@@ -106,6 +108,8 @@ def _fesom2_square_delaunay_uniform_z_coordinate():
106108
zc = 0.5 * (zf[:-1] + zf[1:]) # Vertical element centers
107109
nz = zf.size
108110
nz1 = zc.size
111+
num_days = 5
112+
date_array = [datetime(2000, 1, 1) + timedelta(days=i) for i in range(num_days)]
109113

110114
# mask any point on one of the boundaries
111115
mask = (
@@ -119,26 +123,29 @@ def _fesom2_square_delaunay_uniform_z_coordinate():
119123
method="regional_delaunay",
120124
boundary_points=boundary_points,
121125
)
126+
uxgrid.attrs["Conventions"] = "UGRID-1.0"
122127

123128
# Define arrays U (zonal), V (meridional) and P (sea surface height)
124129
U = np.ones(
125-
(1, nz1, uxgrid.n_face), dtype=np.float64
130+
(num_days, nz1, uxgrid.n_face), dtype=np.float64
126131
) # Lateral velocity is on the element centers and face centers
127132
V = np.ones(
128-
(1, nz1, uxgrid.n_face), dtype=np.float64
133+
(num_days, nz1, uxgrid.n_face), dtype=np.float64
129134
) # Lateral velocity is on the element centers and face centers
130135
W = np.zeros(
131-
(1, nz, uxgrid.n_node), dtype=np.float64
136+
(num_days, nz, uxgrid.n_node), dtype=np.float64
132137
) # Vertical velocity is on the element faces and face vertices
133-
P = np.ones((1, nz1, uxgrid.n_node), dtype=np.float64) # Pressure is on the element centers and face vertices
138+
P = np.ones(
139+
(num_days, nz1, uxgrid.n_node), dtype=np.float64
140+
) # Pressure is on the element centers and face vertices
134141

135142
u = ux.UxDataArray(
136143
data=U,
137144
name="U",
138145
uxgrid=uxgrid,
139146
dims=["time", "nz1", "n_face"],
140147
coords=dict(
141-
time=(["time"], pd.to_datetime(["2000-01-01"])),
148+
time=(["time"], date_array),
142149
nz1=(["nz1"], zc),
143150
),
144151
attrs=dict(
@@ -151,7 +158,7 @@ def _fesom2_square_delaunay_uniform_z_coordinate():
151158
uxgrid=uxgrid,
152159
dims=["time", "nz1", "n_face"],
153160
coords=dict(
154-
time=(["time"], pd.to_datetime(["2000-01-01"])),
161+
time=(["time"], date_array),
155162
nz1=(["nz1"], zc),
156163
),
157164
attrs=dict(
@@ -164,7 +171,7 @@ def _fesom2_square_delaunay_uniform_z_coordinate():
164171
uxgrid=uxgrid,
165172
dims=["time", "nz", "n_node"],
166173
coords=dict(
167-
time=(["time"], pd.to_datetime(["2000-01-01"])),
174+
time=(["time"], date_array),
168175
nz1=(["nz"], zf),
169176
),
170177
attrs=dict(
@@ -177,7 +184,7 @@ def _fesom2_square_delaunay_uniform_z_coordinate():
177184
uxgrid=uxgrid,
178185
dims=["time", "nz1", "n_node"],
179186
coords=dict(
180-
time=(["time"], pd.to_datetime(["2000-01-01"])),
187+
time=(["time"], date_array),
181188
nz1=(["nz1"], zc),
182189
),
183190
attrs=dict(description="pressure", units="N/m^2", location="node", mesh="delaunay", Conventions="UGRID-1.0"),

tests/v4/test_uxarray_fieldset.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
VectorField,
1414
download_example_dataset,
1515
)
16+
from parcels._datasets.unstructured.generic import datasets as datasets_unstructured
1617

1718

1819
@pytest.fixture
@@ -88,3 +89,26 @@ def test_fesom_channel(ds_fesom_channel, uvw_fesom_channel):
8889

8990
pset = ParticleSet(fieldset, pclass=Particle)
9091
pset.execute(endtime=timedelta(days=1), dt=timedelta(hours=1))
92+
93+
94+
@pytest.mark.skip(reason="_index_search.search_time_indices needs major refactoring")
95+
def test_fesom2_square_delaunay_uniform_z_coordinate_eval():
96+
"""
97+
Test the evaluation of a fieldset with a FESOM2 square Delaunay grid and uniform z-coordinate.
98+
Ensures that the fieldset can be created and evaluated correctly.
99+
Since the underlying data is constant, we can check that the values are as expected.
100+
"""
101+
ds = datasets_unstructured["fesom2_square_delaunay_uniform_z_coordinate"]
102+
UVW = VectorField(
103+
name="UVW",
104+
U=Field(name="U", data=ds.U, grid=ds.uxgrid, interp_method=UXPiecewiseConstantFace),
105+
V=Field(name="V", data=ds.V, grid=ds.uxgrid, interp_method=UXPiecewiseConstantFace),
106+
W=Field(name="W", data=ds.W, grid=ds.uxgrid, interp_method=UXPiecewiseLinearNode),
107+
)
108+
P = Field(name="p", data=ds.p, grid=ds.uxgrid, interp_method=UXPiecewiseConstantFace)
109+
fieldset = FieldSet([UVW, P, UVW.U, UVW.V, UVW.W])
110+
111+
assert fieldset.U.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0
112+
assert fieldset.V.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0
113+
assert fieldset.W.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 0.0
114+
assert fieldset.P.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0

0 commit comments

Comments
 (0)