Skip to content

Commit f8a4483

Browse files
committed
updated test to test xarray input
1 parent 665f23c commit f8a4483

1 file changed

Lines changed: 73 additions & 3 deletions

File tree

mhkit/tests/river/test_io_d3d.py

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,41 @@ def setUpClass(self):
2626

2727
filename = "turbineTest_map.nc"
2828
self.d3d_flume_data = netCDF4.Dataset(join(d3ddatadir, filename))
29+
self.d3d_flume_data_xr = xr.open_dataset(join(d3ddatadir, filename))
2930

3031
@classmethod
3132
def tearDownClass(self):
3233
pass
3334

3435
def test_get_all_time(self):
3536
data = self.d3d_flume_data
37+
data_xr = self.d3d_flume_data_xr
3638
seconds_run = river.io.d3d.get_all_time(data)
39+
seconds_run_xr = river.io.d3d.get_all_time(data_xr)
3740
seconds_run_expected = np.ndarray(
3841
shape=(5,), buffer=np.array([0, 60, 120, 180, 240]), dtype=int
3942
)
4043
np.testing.assert_array_equal(seconds_run, seconds_run_expected)
44+
np.testing.assert_array_equal(seconds_run_xr, seconds_run_expected)
4145

4246
def test_convert_time(self):
4347
data = self.d3d_flume_data
48+
data_xr = self.d3d_flume_data_xr
4449
time_index = 2
4550
seconds_run = river.io.d3d.index_to_seconds(data, time_index=time_index)
51+
seconds_run_xr = river.io.d3d.index_to_seconds(data_xr, time_index=time_index)
4652
seconds_run_expected = 120
4753
self.assertEqual(seconds_run, seconds_run_expected)
54+
self.assertEqual(seconds_run_xr, seconds_run_expected)
4855
seconds_run = 60
4956
time_index = river.io.d3d.seconds_to_index(data, seconds_run=seconds_run)
57+
time_index_xr = river.io.d3d.seconds_to_index(data_xr, seconds_run=seconds_run)
5058
time_index_expected = 1
5159
self.assertEqual(time_index, time_index_expected)
60+
self.assertEqual(time_index_xr, time_index_expected)
5261
seconds_run = 62
5362
time_index = river.io.d3d.seconds_to_index(data, seconds_run=seconds_run)
63+
time_index_xr = river.io.d3d.seconds_to_index(data_xr, seconds_run=seconds_run)
5464
time_index_expected = 1
5565
output_expected = f"ERROR: invalid seconds_run. Closest seconds_run found {time_index_expected}"
5666
self.assertWarns(UserWarning)
@@ -60,13 +70,17 @@ def test_convert_time_from_tidal(self):
6070
Test the conversion of time from using tidal import of d3d
6171
"""
6272
data = self.d3d_flume_data
73+
data_xr = self.d3d_flume_data_xr
6374
time_index = 2
6475
seconds_run = tidal.io.d3d.index_to_seconds(data, time_index=time_index)
76+
seconds_run_xr = tidal.io.d3d.index_to_seconds(data_xr, time_index=time_index)
6577
seconds_run_expected = 120
6678
self.assertEqual(seconds_run, seconds_run_expected)
79+
self.assertEqual(seconds_run_xr, seconds_run_expected)
6780

6881
def test_layer_data(self):
6982
data = self.d3d_flume_data
83+
data_xr = self.d3d_flume_data_xr
7084
variable = ["ucx", "s1"]
7185
for var in variable:
7286
layer = 2
@@ -77,10 +91,22 @@ def test_layer_data(self):
7791
layer_data_expected = river.io.d3d.get_layer_data(
7892
data, var, layer_compare, time_index_compare
7993
)
80-
8194
assert_array_almost_equal(layer_data.x, layer_data_expected.x, decimal=2)
8295
assert_array_almost_equal(layer_data.y, layer_data_expected.y, decimal=2)
8396
assert_array_almost_equal(layer_data.v, layer_data_expected.v, decimal=2)
97+
for var in variable:
98+
layer = 2
99+
time_index = 3
100+
layer_data_xr = river.io.d3d.get_layer_data(data_xr, var, layer, time_index)
101+
layer_compare = 2
102+
time_index_compare = 4
103+
layer_data_expected_xr = river.io.d3d.get_layer_data(
104+
data_xr, var, layer_compare, time_index_compare
105+
)
106+
assert_array_almost_equal(layer_data_xr.x, layer_data_expected_xr.x, decimal=2)
107+
assert_array_almost_equal(layer_data_xr.y, layer_data_expected_xr.y, decimal=2)
108+
assert_array_almost_equal(layer_data_xr.v, layer_data_expected_xr.v, decimal=2)
109+
84110

85111
def test_create_points_three_points(self):
86112
"""
@@ -209,6 +235,32 @@ def test_variable_interpolation(self):
209235
np.size(transformes_data["ucx"]), np.size(transformes_data["turkin1"])
210236
)
211237

238+
def test_variable_interpolation_xr(self):
239+
data_xr = self.d3d_flume_data_xr
240+
variables = ["ucx", "turkin1"]
241+
transformes_data = river.io.d3d.variable_interpolation(
242+
data_xr, variables, points="faces", edges="nearest"
243+
)
244+
self.assertEqual(
245+
np.size(transformes_data["ucx"]), np.size(transformes_data["turkin1"])
246+
)
247+
transformes_data = river.io.d3d.variable_interpolation(
248+
data_xr, variables, points="cells", edges="nearest"
249+
)
250+
self.assertEqual(
251+
np.size(transformes_data["ucx"]), np.size(transformes_data["turkin1"])
252+
)
253+
x = np.linspace(1, 3, num=3)
254+
y = np.linspace(1, 3, num=3)
255+
waterdepth = 1
256+
points = river.io.d3d.create_points(x, y, waterdepth)
257+
transformes_data = river.io.d3d.variable_interpolation(
258+
data_xr, variables, points=points
259+
)
260+
self.assertEqual(
261+
np.size(transformes_data["ucx"]), np.size(transformes_data["turkin1"])
262+
)
263+
212264
def test_get_all_data_points(self):
213265
data = self.d3d_flume_data
214266
variable = "ucx"
@@ -222,6 +274,19 @@ def test_get_all_data_points(self):
222274
size_output_expected = np.size(output_expected)
223275
self.assertEqual(size_output, size_output_expected)
224276

277+
def test_get_all_data_points_xr(self):
278+
data_xr = self.d3d_flume_data_xr
279+
variable = "ucx"
280+
time_step = 3
281+
output = river.io.d3d.get_all_data_points(data_xr, variable, time_step)
282+
size_output = np.size(output)
283+
time_step_compair = 4
284+
output_expected = river.io.d3d.get_all_data_points(
285+
data_xr, variable, time_step_compair
286+
)
287+
size_output_expected = np.size(output_expected)
288+
self.assertEqual(size_output, size_output_expected)
289+
225290
def test_unorm(self):
226291
x = np.linspace(1, 3, num=3)
227292
y = np.linspace(1, 3, num=3)
@@ -236,6 +301,7 @@ def test_unorm(self):
236301

237302
def test_turbulent_intensity(self):
238303
data = self.d3d_flume_data
304+
data_xr = self.d3d_flume_data_xr
239305
time_index = -1
240306
x_test = np.linspace(1, 17, num=10)
241307
y_test = np.linspace(3, 3, num=10)
@@ -250,7 +316,7 @@ def test_turbulent_intensity(self):
250316
points = pd.DataFrame(test_points, columns=["x", "y", "waterdepth"])
251317

252318
TI = river.io.d3d.turbulent_intensity(data, points, time_index)
253-
319+
TI_xr = river.io.d3d.turbulent_intensity(data_xr, points, time_index)
254320
TI_vars = ["turkin1", "ucx", "ucy", "ucz"]
255321
TI_data_raw = {}
256322
for var in TI_vars:
@@ -284,12 +350,16 @@ def test_turbulent_intensity(self):
284350
assert_array_almost_equal(
285351
TI.turbulent_intensity, turbulent_intensity_expected, decimal=2
286352
)
287-
353+
assert_array_almost_equal(
354+
TI_xr.turbulent_intensity, turbulent_intensity_expected, decimal=2
355+
)
288356
TI = river.io.d3d.turbulent_intensity(data, points="faces")
357+
TI_xr = river.io.d3d.turbulent_intensity(data_xr, points="faces")
289358
TI_size = np.size(TI["turbulent_intensity"])
290359
turkin1 = river.io.d3d.get_all_data_points(data, "turkin1", time_index)
291360
turkin1_size = np.size(turkin1["turkin1"])
292361
self.assertEqual(TI_size, turkin1_size)
362+
self.assertEqual(TI_size, np.size(TI_xr["turbulent_intensity"]))
293363

294364
TI = river.io.d3d.turbulent_intensity(data, points="cells")
295365
TI_size = np.size(TI["turbulent_intensity"])

0 commit comments

Comments
 (0)