-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathtest_uxarray_fieldset.py
More file actions
140 lines (119 loc) · 5.61 KB
/
Copy pathtest_uxarray_fieldset.py
File metadata and controls
140 lines (119 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pytest
import uxarray as ux
from parcels import (
Field,
FieldSet,
Particle,
ParticleSet,
UXPiecewiseConstantFace,
UXPiecewiseLinearNode,
VectorField,
download_example_dataset,
)
from parcels._datasets.unstructured.generic import datasets as datasets_unstructured
from parcels.uxgrid import UxGrid
@pytest.fixture
def ds_fesom_channel() -> ux.UxDataset:
fesom_path = download_example_dataset("FESOM_periodic_channel")
grid_path = f"{fesom_path}/fesom_channel.nc"
data_path = [
f"{fesom_path}/u.fesom_channel.nc",
f"{fesom_path}/v.fesom_channel.nc",
f"{fesom_path}/w.fesom_channel.nc",
]
ds = ux.open_mfdataset(grid_path, data_path).rename_vars({"u": "U", "v": "V", "w": "W"})
return ds
@pytest.fixture
def uv_fesom_channel(ds_fesom_channel) -> VectorField:
UV = VectorField(
name="UV",
U=Field(
name="U",
data=ds_fesom_channel.U,
grid=UxGrid(ds_fesom_channel.uxgrid, z=ds_fesom_channel.coords["nz"]),
interp_method=UXPiecewiseConstantFace,
),
V=Field(
name="V",
data=ds_fesom_channel.V,
grid=UxGrid(ds_fesom_channel.uxgrid, z=ds_fesom_channel.coords["nz"]),
interp_method=UXPiecewiseConstantFace,
),
)
return UV
@pytest.fixture
def uvw_fesom_channel(ds_fesom_channel) -> VectorField:
UVW = VectorField(
name="UVW",
U=Field(
name="U",
data=ds_fesom_channel.U,
grid=UxGrid(ds_fesom_channel.uxgrid, z=ds_fesom_channel.coords["nz"]),
interp_method=UXPiecewiseConstantFace,
),
V=Field(
name="V",
data=ds_fesom_channel.V,
grid=UxGrid(ds_fesom_channel.uxgrid, z=ds_fesom_channel.coords["nz"]),
interp_method=UXPiecewiseConstantFace,
),
W=Field(
name="W",
data=ds_fesom_channel.W,
grid=UxGrid(ds_fesom_channel.uxgrid, z=ds_fesom_channel.coords["nz"]),
interp_method=UXPiecewiseLinearNode,
),
)
return UVW
def test_fesom_fieldset(ds_fesom_channel, uv_fesom_channel):
fieldset = FieldSet([uv_fesom_channel, uv_fesom_channel.U, uv_fesom_channel.V])
# Check that the fieldset has the expected properties
assert (fieldset.U.data == ds_fesom_channel.U).all()
assert (fieldset.V.data == ds_fesom_channel.V).all()
def test_fesom_in_particleset(ds_fesom_channel, uv_fesom_channel):
fieldset = FieldSet([uv_fesom_channel, uv_fesom_channel.U, uv_fesom_channel.V])
# Check that the fieldset has the expected properties
assert (fieldset.U.data == ds_fesom_channel.U).all()
assert (fieldset.V.data == ds_fesom_channel.V).all()
pset = ParticleSet(fieldset, pclass=Particle)
assert pset.fieldset == fieldset
def test_set_interp_methods(ds_fesom_channel, uv_fesom_channel):
fieldset = FieldSet([uv_fesom_channel, uv_fesom_channel.U, uv_fesom_channel.V])
# Check that the fieldset has the expected properties
assert (fieldset.U.data == ds_fesom_channel.U).all()
assert (fieldset.V.data == ds_fesom_channel.V).all()
# Set the interpolation method for each field
fieldset.U.interp_method = UXPiecewiseConstantFace
fieldset.V.interp_method = UXPiecewiseConstantFace
def test_fesom2_square_delaunay_uniform_z_coordinate_eval():
"""
Test the evaluation of a fieldset with a FESOM2 square Delaunay grid and uniform z-coordinate.
Ensures that the fieldset can be created and evaluated correctly.
Since the underlying data is constant, we can check that the values are as expected.
"""
ds = datasets_unstructured["fesom2_square_delaunay_uniform_z_coordinate"]
UVW = VectorField(
name="UVW",
U=Field(name="U", data=ds.U, grid=UxGrid(ds.uxgrid, z=ds.coords["nz"]), interp_method=UXPiecewiseConstantFace),
V=Field(name="V", data=ds.V, grid=UxGrid(ds.uxgrid, z=ds.coords["nz"]), interp_method=UXPiecewiseConstantFace),
W=Field(name="W", data=ds.W, grid=UxGrid(ds.uxgrid, z=ds.coords["nz"]), interp_method=UXPiecewiseLinearNode),
)
P = Field(name="p", data=ds.p, grid=UxGrid(ds.uxgrid, z=ds.coords["nz"]), interp_method=UXPiecewiseLinearNode)
fieldset = FieldSet([UVW, P, UVW.U, UVW.V, UVW.W])
assert fieldset.U.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0
assert fieldset.V.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0
assert fieldset.W.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 0.0
assert fieldset.p.eval(time=ds.time[0].values, z=1.0, y=30.0, x=30.0, applyConversion=False) == 1.0
def test_fesom2_square_delaunay_antimeridian_eval():
"""
Test the evaluation of a fieldset with a FESOM2 square Delaunay grid that crosses the antimeridian.
Ensures that the fieldset can be created and evaluated correctly.
Since the underlying data is constant, we can check that the values are as expected.
"""
ds = datasets_unstructured["fesom2_square_delaunay_antimeridian"]
P = Field(name="p", data=ds.p, grid=UxGrid(ds.uxgrid, z=ds.coords["nz"]), interp_method=UXPiecewiseLinearNode)
fieldset = FieldSet([P])
assert fieldset.p.eval(time=ds.time[0].values, z=1.0, y=30.0, x=-170.0, applyConversion=False) == 1.0
assert fieldset.p.eval(time=ds.time[0].values, z=1.0, y=30.0, x=-180.0, applyConversion=False) == 1.0
assert fieldset.p.eval(time=ds.time[0].values, z=1.0, y=30.0, x=180.0, applyConversion=False) == 1.0
assert fieldset.p.eval(time=ds.time[0].values, z=1.0, y=30.0, x=170.0, applyConversion=False) == 1.0