-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_res1d_lts_chronological.py
280 lines (218 loc) · 9.85 KB
/
test_res1d_lts_chronological.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import pytest
import numpy as np
import pandas as pd
import datetime
from mikeio1d.custom_exceptions import NoDataForQuery, InvalidQuantity
from mikeio1d.res1d import Res1D
from mikeio1d.query import QueryDataReach
from mikeio1d.query import QueryDataNode
from mikeio1d.query import QueryDataGlobal
from mikeio1d.dotnet import to_numpy
from mikeio1d.result_reader_writer.result_reader import ColumnMode
@pytest.fixture
def test_file_path():
test_folder_path = os.path.dirname(os.path.abspath(__file__))
# Original file name was Exam6Base.res1d
return os.path.join(test_folder_path, "testdata", "lts_monthly_statistics.res1d")
@pytest.fixture(params=[False])
def test_file(test_file_path, request):
return Res1D(test_file_path, lazy_load=request.param)
def test_file_does_not_exist():
with pytest.raises(FileExistsError):
assert Res1D("tests/testdata/not_a_file.res1d")
def test_read(test_file):
df = test_file.read()
assert len(df) == 73
# TODO: assert not df.columns.duplicated().any() - add this, but it fails since columns are not guaranteed unique
def test_quantities(test_file):
quantities = test_file.quantities
assert len(quantities) == 21
def test_info(test_file):
res1d = test_file
res1d_info = res1d._get_info()
res1d_info_ref = (
"Start time: 1957-01-01 00:00:00\n"
+ "End time: 1963-01-01 00:00:00\n"
+ "# Timesteps: 73\n"
+ "# Catchments: 0\n"
+ "# Nodes: 16\n"
+ "# Reaches: 17\n"
+ "# Globals: 9\n"
+ "0 - DischargeIntegratedMonthly <m^3>\n"
+ "1 - DischargeIntegratedMonthlyCount <()>\n"
+ "2 - DischargeIntegratedMonthlyDuration <h>\n"
+ "3 - Component_1TransportIntegratedMonthly <kg>\n"
+ "4 - Component_1TransportIntegratedMonthlyCount <()>\n"
+ "5 - Component_1TransportIntegratedMonthlyDuration <h>\n"
+ "6 - Component_2TransportIntegratedMonthly <kg>\n"
+ "7 - Component_2TransportIntegratedMonthlyCount <()>\n"
+ "8 - Component_2TransportIntegratedMonthlyDuration <h>\n"
+ "9 - SurchargeIntegratedMonthly <m^3>\n"
+ "10 - SurchargeIntegratedMonthlyCount <()>\n"
+ "11 - SurchargeIntegratedMonthlyDuration <h>\n"
+ "12 - DischargeIntegratedMonthlyOutlets <m^3>\n"
+ "13 - DischargeIntegratedMonthlyWeirs <m^3>\n"
+ "14 - DischargeIntegratedMonthlyTotalOutflow <m^3>\n"
+ "15 - Component_1TransportIntegratedMonthlyTotalEmission <kg>\n"
+ "16 - Component_2TransportIntegratedMonthlyTotalEmission <kg>\n"
+ "17 - Component_1TransportIntegratedMonthlyOutlets <kg>\n"
+ "18 - Component_2TransportIntegratedMonthlyOutlets <kg>\n"
+ "19 - Component_1TransportIntegratedMonthlyWeirs <kg>\n"
+ "20 - Component_2TransportIntegratedMonthlyWeirs <kg>"
)
assert res1d_info == res1d_info_ref
def test_data_item_dicts(test_file):
res1d = test_file
assert len(res1d.catchments) == 0
assert len(res1d.nodes) == 16
assert len(res1d.reaches) == 17
assert len(res1d.global_data) == 9
@pytest.mark.parametrize(
"query,expected",
[
(QueryDataReach("DischargeIntegratedMonthly", "B4.1320l1", 0), True),
(QueryDataReach("DischargeIntegratedMonthly", "B4.1320l1", 42424242), False),
(QueryDataReach("DischargeIntegratedMonthly", "wrong_reach_name", 0), False),
],
)
def test_valid_reach_data_queries(test_file, query, expected):
res1d = test_file
with pytest.raises(InvalidQuantity):
invalid_query = QueryDataReach("InvalidQuantity", "104l1", 34.4131)
assert res1d.read(invalid_query)
if expected:
res1d.read(query)
else:
with pytest.raises(NoDataForQuery):
assert res1d.read(query)
@pytest.mark.parametrize(
"query,expected_max",
[
(QueryDataReach("DischargeIntegratedMonthly", "B4.1320l1", 135.001), 1215.915),
(QueryDataReach("DischargeIntegratedMonthly", "B4.1491l1", 144), 563.973),
(QueryDataReach("DischargeIntegratedMonthlyCount", "B4.1320l1", 270.002), 3),
(QueryDataReach("DischargeIntegratedMonthlyCount", "B4.1491l1", 239.999), 3),
],
)
def test_read_reach_with_queries(test_file, query, expected_max):
data = test_file.read(query)
assert pytest.approx(round(data.max().values[0], 3)) == expected_max
@pytest.mark.parametrize(
"quantity,reach_id,chainage,expected_max",
[
("DischargeIntegratedMonthly", "B4.1320l1", 135.001, 1215.915),
("DischargeIntegratedMonthly", "B4.1491l1", 144, 563.973),
("DischargeIntegratedMonthlyCount", "B4.1320l1", 270.002, 3),
("DischargeIntegratedMonthlyCount", "B4.1491l1", 239.999, 3),
],
)
def test_read_reach(test_file, quantity, reach_id, chainage, expected_max):
data = test_file.query.GetReachValues(reach_id, chainage, quantity)
data = to_numpy(data)
actual_max = round(np.max(data), 3)
assert pytest.approx(actual_max) == expected_max
@pytest.mark.parametrize(
"quantity,node_id,expected_max",
[
("SurchargeIntegratedMonthly", "B4.1200", 97.434),
("SurchargeIntegratedMonthlyCount", "B4.1200", 1),
],
)
def test_read_node(test_file, quantity, node_id, expected_max):
data = test_file.query.GetNodeValues(node_id, quantity)
data = to_numpy(data)
actual_max = round(np.max(data), 3)
assert pytest.approx(actual_max) == expected_max
def test_time_index(test_file):
assert len(test_file.time_index) == 73
def test_start_time(test_file):
time = test_file.start_time
date = datetime.datetime(time.year, time.month, 1, 0, 0)
assert date == test_file.time_index.min()
def test_get_node_values(test_file):
values = test_file.get_node_values("B4.1200", "SurchargeIntegratedMonthly")
assert len(values) == 73
def test_get_reach_values(test_file):
values = test_file.get_reach_values("B4.1491l1", 144, "DischargeIntegratedMonthlyCount")
time_series = pd.Series(values, index=test_file.time_index)
assert len(values) == 73
assert len(time_series.index) == 73
# Just try to call the methods
test_file.get_reach_end_values("B4.1491l1", "DischargeIntegratedMonthlyCount")
test_file.get_reach_start_values("B4.1491l1", "DischargeIntegratedMonthlyCount")
test_file.get_reach_sum_values("B4.1491l1", "DischargeIntegratedMonthlyCount")
def test_get_reach_value(test_file):
value = test_file.get_reach_value(
"B4.1491l1", 144, "DischargeIntegratedMonthlyCount", test_file.start_time
)
assert value == 0
def test_res1d_filter(test_file_path, helpers):
nodes = ["B4.1320", "A0.0327"]
reaches = ["B4.1491l1"]
res1d = Res1D(test_file_path, nodes=nodes, reaches=reaches)
res1d.result_reader.column_mode = ColumnMode.ALL
df_b4_14 = res1d.read(QueryDataReach("DischargeIntegratedMonthly", "B4.1491l1", 144))
df_b4_13 = res1d.read(QueryDataNode("SurchargeIntegratedMonthly", "B4.1320"))
df_a0 = res1d.read(QueryDataNode("DischargeIntegratedMonthly", "A0.0327"))
res1d_full = Res1D(test_file_path)
res1d_full.result_reader.column_mode = ColumnMode.ALL
df_full = res1d_full.read()
helpers.assert_shared_columns_equal(df_full, df_b4_14)
helpers.assert_shared_columns_equal(df_full, df_b4_13)
helpers.assert_shared_columns_equal(df_full, df_a0)
# Release the .NET object
res1d = None
def test_res1d_filter_readall(test_file_path, helpers):
# Make sure read all can be used with filters
nodes = ["B4.1320", "A0.0327"]
reaches = ["B4.1491l1"]
res1d = Res1D(test_file_path, nodes=nodes, reaches=reaches)
df = res1d.read()
res1d_full = Res1D(test_file_path)
df_full = res1d_full.read()
helpers.assert_shared_columns_equal(df_full, df)
# Release the .NET object
res1d = None
@pytest.mark.parametrize(
"query,expected_max",
[
(QueryDataGlobal("DischargeIntegratedMonthlyOutlets"), 5562.719),
(QueryDataGlobal("DischargeIntegratedMonthlyWeirs"), 437.729),
(QueryDataGlobal("DischargeIntegratedMonthlyTotalOutflow"), 5971.352),
],
)
def test_read_global_items_with_queries(test_file, query, expected_max):
data = test_file.read(query)
assert pytest.approx(round(data.max().values[0], 3)) == expected_max
def test_global_data_attributes(test_file):
res1d = test_file
global_data = res1d.global_data
global_data.DischargeIntegratedMonthlyOutlets.add()
global_data.DischargeIntegratedMonthlyWeirs.add()
global_data.DischargeIntegratedMonthlyTotalOutflow.add()
df = res1d.read()
actual_max = round(df["DischargeIntegratedMonthlyOutlets"].max(), 3)
assert pytest.approx(actual_max) == 5562.719
actual_max = round(df["DischargeIntegratedMonthlyWeirs"].max(), 3)
assert pytest.approx(actual_max) == 437.729
actual_max = round(df["DischargeIntegratedMonthlyTotalOutflow"].max(), 3)
assert pytest.approx(actual_max) == 5971.352
def test_res1d_merging_same_file(test_file_path):
# Use the same file twice to create a merged LTS statistics file
file_names = [test_file_path, test_file_path]
merged_file_name = test_file_path.replace(".res1d", ".merged.res1d")
Res1D.merge(file_names, merged_file_name)
# Read the merged file
res1d = Res1D(merged_file_name)
# Test one reach location for particular values
df_reach = res1d.reaches.B4_1320l1.m_101_251.DischargeIntegratedMonthly.read()
assert pytest.approx(np.round(df_reach.max(), 3)) == 2 * 1215.915
df_reach_count = res1d.reaches.B4_1320l1.m_101_251.DischargeIntegratedMonthlyCount.read()
assert pytest.approx(np.round(df_reach_count.max(), 3)) == 2 * 3
df_reach_duration = res1d.reaches.B4_1320l1.m_101_251.DischargeIntegratedMonthlyDuration.read()
assert pytest.approx(np.round(df_reach_duration.max(), 3)) == 2 * 10.703
res1d_ori = Res1D(test_file_path)
df_ori = res1d_ori.read()
df_merged = res1d.read()
pd.testing.assert_frame_equal(2 * df_ori, df_merged)