Skip to content

Commit 87c45c9

Browse files
authored
Merge pull request #113 from GeoOcean/88-document-and-test-greensurge-and-ocsmesh
[EJP] add tests vortex and mesh utils
2 parents 646322e + 43b2589 commit 87c45c9

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

tests/tcs/test_vortex.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import unittest
2+
3+
import numpy as np
4+
import pandas as pd
5+
import xarray as xr
6+
7+
from bluemath_tk.tcs.vortex import vortex_model_grid
8+
9+
10+
class TestVortexModelGrid(unittest.TestCase):
11+
"""Test the vortex_model_grid function."""
12+
13+
def test_vortex_model_grid(self):
14+
storm_track = pd.DataFrame(
15+
{
16+
"vfx": [10, 12],
17+
"vfy": [5, 6],
18+
"p0": [1000, 990],
19+
"pn": [980, 970],
20+
"vmax": [50, 55],
21+
"rmw": [30, 35],
22+
"lon": [10.001, 12.001],
23+
"lat": [20.001, 22.001],
24+
},
25+
index=pd.date_range("2023-10-01", periods=2),
26+
)
27+
cg_lon = np.array([9.5, 10.0, 10.5])
28+
cg_lat = np.array([19.5, 20.0, 20.5])
29+
30+
ds = vortex_model_grid(storm_track, cg_lon, cg_lat, coords_mode="SPHERICAL")
31+
32+
W_vals = np.array(
33+
[
34+
[
35+
[17.09417413, 0.82665737],
36+
[22.66057334, 1.14495022],
37+
[19.54808437, 1.54414607],
38+
],
39+
[[15.94360075, 1.1403561], [0.0, 1.68962993], [20.625051, 2.44988633]],
40+
[
41+
[10.76028132, 1.52098785],
42+
[12.98617365, 2.42132863],
43+
[14.33530855, 3.80841364],
44+
],
45+
]
46+
)
47+
Dir_vals = np.array(
48+
[
49+
[
50+
[1.29496987e02, 1.29942357e02],
51+
[9.01036600e01, 1.19620263e02],
52+
[5.08422102e01, 1.11400370e02],
53+
],
54+
[
55+
[1.79763735e02, 1.41716653e02],
56+
[1.34656325e02, 1.30581118e02],
57+
[2.39753156e-01, 1.19617666e02],
58+
],
59+
[
60+
[2.30162978e02, 1.52609284e02],
61+
[2.69894691e02, 1.43594058e02],
62+
[3.09495882e02, 1.32127127e02],
63+
],
64+
]
65+
)
66+
p_vals = np.array(
67+
[
68+
[
69+
[98722.50246466, 97023.7119469],
70+
[99257.26417213, 97029.78898728],
71+
[98725.18999552, 97036.80829455],
72+
],
73+
[
74+
[99371.28980686, 97030.96909655],
75+
[100000.0, 97041.30637199],
76+
[99378.55955678, 97054.71911504],
77+
],
78+
[
79+
[98727.68758907, 97040.02996857],
80+
[99264.62166108, 97057.51709726],
81+
[98730.39083852, 97083.99580787],
82+
],
83+
]
84+
)
85+
lat = np.array([19.5, 20.0, 20.5])
86+
lon = np.array([9.5, 10.0, 10.5])
87+
time = np.array(
88+
["2023-10-01T00:00:00.000000000", "2023-10-02T00:00:00.000000000"],
89+
dtype="datetime64[ns]",
90+
)
91+
92+
ds_expected = xr.Dataset(
93+
{
94+
"W": (["lat", "lon", "time"], W_vals, {"units": "m/s"}),
95+
"Dir": (["lat", "lon", "time"], Dir_vals, {"units": "º"}),
96+
"p": (["lat", "lon", "time"], p_vals, {"units": "Pa"}),
97+
},
98+
coords={"lat": lat, "lon": lon, "time": time},
99+
)
100+
101+
xr.testing.assert_allclose(ds, ds_expected, rtol=1e-5, atol=1e-5)
102+
103+
104+
if __name__ == "__main__":
105+
unittest.main()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import unittest
2+
3+
import numpy as np
4+
5+
from bluemath_tk.topo_bathy.mesh_utils import (
6+
calculate_edges,
7+
detect_circumcenter_too_close,
8+
get_raster_resolution_meters,
9+
)
10+
11+
12+
class TestDetectCircumcenterTooClose(unittest.TestCase):
13+
"""Test the detect_circumcenter_too_close function."""
14+
15+
def test_function(self):
16+
"""Test the function with a simple case."""
17+
# Define the input arrays
18+
X = np.array([0, 1, 0, 1, 2, 3, 3])
19+
Y = np.array([0, 0, 1, 1, 1, 0, 1])
20+
elements = np.array([[0, 1, 2], [1, 3, 2], [3, 4, 5], [4, 5, 6]])
21+
aj_threshold = 0.1
22+
23+
# Call the function
24+
bad_elements_mask = detect_circumcenter_too_close(
25+
X=X, Y=Y, elements=elements, aj_threshold=aj_threshold
26+
)
27+
28+
# Check the result
29+
expected_mask = np.array([True, True, False, False])
30+
np.testing.assert_array_equal(bad_elements_mask, expected_mask)
31+
32+
33+
class TestCalculateEdges(unittest.TestCase):
34+
"""Test the calculate_edges function."""
35+
36+
def test_function(self):
37+
"""Test the function with a simple case."""
38+
# Define the input arrays
39+
elements = np.array([[0, 1, 2], [1, 2, 3], [2, 3, 0]])
40+
41+
# Call the function
42+
edges = calculate_edges(elements)
43+
44+
# Check the result
45+
expected_edges = np.array([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]])
46+
np.testing.assert_array_equal(edges, expected_edges)
47+
48+
49+
class TestGetRasterResolutionMeters(unittest.TestCase):
50+
"""Test the get_raster_resolution_meters function."""
51+
52+
def test_function(self):
53+
"""Test the function with a simple case."""
54+
55+
lon_center = 0
56+
lat_center = 0
57+
raster_resolution = 0.1
58+
project = lambda x, y: (x * 100000, y * 100000)
59+
60+
resolution = get_raster_resolution_meters(
61+
lon_center=lon_center,
62+
lat_center=lat_center,
63+
raster_resolution=raster_resolution,
64+
project=project,
65+
)
66+
67+
# Check the result
68+
expected_resolution = np.float64(7071.067811865475)
69+
np.testing.assert_almost_equal(resolution, expected_resolution, decimal=5)
70+
71+
72+
if __name__ == "__main__":
73+
unittest.main()

0 commit comments

Comments
 (0)