forked from NOAA-OWP/lstm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_bmi_unit_test.py
422 lines (364 loc) · 13.9 KB
/
run_bmi_unit_test.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""Run BMI Unit Testing.
Author: jgarrett
Date: 08/31/2021"""
import os
import sys
import numpy as np
#import torch
#from torch import nn
from pathlib import Path
from netCDF4 import Dataset
import bmi_lstm # This is the BMI LSTM that we will be running
# setup a "success counter" for number of passing and failing bmi functions
# keep track of function def fails (vs function call)
pass_count = 0
fail_count = 0
var_name_counter = 0
fail_list = []
def bmi_except(fstring):
"""Prints message and updates counter and list
Parameters
----------
fstring : str
Name of failing BMI function
"""
global fail_count, fail_list, var_name_counter
print("**BMI ERROR** in " + fstring)
if (var_name_counter == 0):
fail_count += 1
fail_list.append(fstring)
bmi=bmi_lstm.bmi_LSTM()
print("\nBEGIN BMI UNIT TEST\n*******************\n");
# Define config path
cfg_file=Path('./bmi_config_files/01022500_hourly_all_attributes_forcings.yml')
#cfg_file=Path('./bmi_config_files/01022500_hourly_slope_mean_precip_temp.yml')
if os.path.exists(cfg_file):
print(" configuration found: " + str(cfg_file))
else:
print(" no configuration found, exiting...")
sys.exit()
#-------------------------------------------------------------------
# initialize()
try:
bmi.initialize(cfg_file)
print(" initializing...")
pass_count += 1
except:
bmi_except('initialize()')
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Model Information Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print("\nMODEL INFORMATION\n*****************")
#-------------------------------------------------------------------
# get_component_name()
try:
print (" component name: " + bmi.get_component_name())
pass_count += 1
except:
bmi_except('get_component_name()')
#-------------------------------------------------------------------
# get_input_item_count()
try:
print (" input item count: " + str(bmi.get_input_item_count()))
pass_count += 1
except:
bmi_except('get_input_item_count()')
#-------------------------------------------------------------------
# get_output_item_count()
try:
print (" output item count: " + str(bmi.get_output_item_count()))
pass_count += 1
except:
bmi_except('get_output_item_count()')
#-------------------------------------------------------------------
# get_input_var_names()
try:
# only print statement if names exist
test_get_input_var_names = bmi.get_input_var_names()
if len(test_get_input_var_names) > 0:
print (" input var names: ")
for var_in in test_get_input_var_names:
print (" " + var_in)
pass_count += 1
except:
bmi_except('get_input_var_names()')
#-------------------------------------------------------------------
# get_input_var_names()
try:
# only print statement if out var list not null
test_get_output_var_names = bmi.get_output_var_names()
if len(test_get_output_var_names) > 0:
print (" output var names: ")
for var_out in test_get_output_var_names:
print (" " + var_out)
pass_count += 1
except:
bmi_except('get_output_item_count()')
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Variable Information Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print("\nVARIABLE INFORMATION\n********************")
for var_name in (bmi.get_output_var_names() + bmi.get_input_var_names()):
print (" " + var_name + ":")
#-------------------------------------------------------------------
# get_var_units()
try:
print (" units: " + bmi.get_var_units(var_name))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_units()')
#-------------------------------------------------------------------
# get_var_itemsize()
try:
print (" itemsize: " + str(bmi.get_var_itemsize(var_name)))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_itemsize()')
#-------------------------------------------------------------------
# get_var_type()
try:
print (" type: " + str(bmi.get_var_type(var_name)))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_type()')
#-------------------------------------------------------------------
# get_var_nbytes()
try:
print (" nbytes: " + str(bmi.get_var_nbytes(var_name)))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_nbytes()')
#-------------------------------------------------------------------
# get_var_grid
try:
print (" grid id: " + str(bmi.get_var_grid(var_name)))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_grid()')
#-------------------------------------------------------------------
# get_var_location
try:
print (" location: " + bmi.get_var_location(var_name))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_var_location()')
var_name_counter += 1
# reset back to zero
var_name_counter = 0
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Time Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print("\nTIME INFORMATION\n****************")
#-------------------------------------------------------------------
# get_start_time()
try:
print (" start time: " + str(bmi.get_start_time()))
pass_count += 1
except:
bmi_except('get_start_time()')
#-------------------------------------------------------------------
# get_end_time()
try:
print (" end time: " + str(bmi.get_end_time()))
pass_count += 1
except:
bmi_except('get_end_time()')
#-------------------------------------------------------------------
# get_current_time()
try:
print (" current time: " + str(bmi.get_current_time()))
pass_count += 1
except:
bmi_except('get_current_time()')
#-------------------------------------------------------------------
# get_time_step()
try:
print (" time step: " + str(bmi.get_time_step()))
pass_count += 1
except:
bmi_except('get_time_step()')
#-------------------------------------------------------------------
# get_time_units()
try:
print (" time units: " + bmi.get_time_units())
pass_count += 1
except:
bmi_except('get_time_units()')
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Model Grid Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print("\nGRID INFORMATION\n****************")
grid_id = 0 # there is only 1
print (" grid id: " + str(grid_id))
#-------------------------------------------------------------------
# get_grid_rank()
try:
print (" rank: " + str(bmi.get_grid_rank(grid_id)))
pass_count += 1
except:
bmi_except('get_grid_rank()')
#-------------------------------------------------------------------
# get_grid_size()
try:
print (" size: " + str(bmi.get_grid_size(grid_id)))
pass_count += 1
except:
bmi_except('get_grid_size()')
#-------------------------------------------------------------------
# get_grid_type()
try:
print (" type: " + bmi.get_grid_type(grid_id))
pass_count += 1
except:
bmi_except('get_grid_type()')
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Variable Getter and Setter Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print ("\nGET AND SET VALUES\n******************")
for var_name in (bmi.get_output_var_names() + bmi.get_input_var_names()):
print (" " + var_name + ":" )
#-------------------------------------------------------------------
# set_value()
try:
this_set_value = -99.0
bmi.set_value(var_name, np.array([this_set_value]))
print (" set value: " + str(this_set_value))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('set_value()')
#-------------------------------------------------------------------
# get_value()
try:
dest_array = np.zeros(1)
bmi.get_value(var_name, dest_array)
that_get_value = dest_array[0]
# check if set_value() passed then see if get/set values match
if 'set_value()' not in fail_list:
if that_get_value == this_set_value:
print (" get value: " + str(that_get_value) + " (values match)")
else:
print (" get value: " + str(that_get_value) + " (values DO NOT match)")
else:
print (" get value: " + str(that_get_value))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_value()')
#-------------------------------------------------------------------
# get_value_ptr()
try:
that_get_value_ptr = bmi.get_value_ptr(var_name)
# check if set_value() passed then see if get/set values match
if 'set_value()' not in fail_list:
if that_get_value_ptr == this_set_value:
print (" get value ptr: " + str(that_get_value) + " (values match)")
else:
print (" get value ptr: " + str(that_get_value) + " (values DO NOT match)")
else:
print (" get value ptr: " + str(that_get_value_ptr))
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_value_ptr()')
#-------------------------------------------------------------------
# set_value_at_indices()
try:
this_set_value_at_indices = -11.0
bmi.set_value_at_indices(var_name, np.array([0]), np.array([this_set_value_at_indices]))
#print (" set value at indices: -9.0, and got value:", bmi.get_value(var_name))
print (" set value at indices: " + str(this_set_value_at_indices))
if var_name_counter == 0:
pass_count += 1
except Exception as e:
bmi_except('set_value_at_indices()')
#-------------------------------------------------------------------
# get_value_at_indices()
try:
dest_array = np.zeros(1)
bmi.get_value_at_indices(var_name, dest_array, np.array([0]))
that_get_value_at_indices = dest_array[0]
# check if set_value_at_indices() passed then see if get/set values match
if 'set_value_at_indices()' not in fail_list:
if that_get_value_at_indices == this_set_value_at_indices:
print (" get value at indices: " + str(that_get_value_at_indices) + " (values match)")
else:
print (" get value at indices: " + str(that_get_value_at_indices) + " (values DO NOT match)")
# prob worth while to bounce against set_value() if get_value_at_indices() failed..
elif 'set_value()' not in fail_list:
if that_get_value_at_indices == this_set_value:
print (" get value at indices: " + str(that_get_value_at_indices) + " (values match)")
else:
print (" get value at indices: " + str(that_get_value_at_indices) + " (values DO NOT match)")
else:
# JMFrame NOTE: converting a list/array to a string probably won't work
#print (" get value at indices: " + str(bmi.get_value_at_indices(var_name, dest0, [0])))
print (" get value at indices: ", that_get_value_at_indices)
if var_name_counter == 0:
pass_count += 1
except:
bmi_except('get_value_at_indices()')
var_name_counter += 1
# set back to zero
var_name_counter = 0
#-------------------------------------------------------------------
#-------------------------------------------------------------------
# BMI: Control Functions
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print ("\nCONTROL FUNCTIONS\n*****************")
#-------------------------------------------------------------------
# update()
try:
bmi.update()
# go ahead and print time to show iteration
# wrap another try/except incase get_current_time() failed
try:
print (" updating... timestep: " + str(bmi.get_current_time()));
except:
print (" updating...");
pass_count += 1
except:
bmi_except('update()')
#-------------------------------------------------------------------
# update_until()
try:
bmi.update_until(100)
# go ahead and print time to show iteration
# wrap another try/except incase get_current_time() failed
try:
print (" updating until... timestep: " + str(bmi.get_current_time()));
except:
print (" updating until...");
pass_count += 1
except:
bmi_except('update_until()')
#-------------------------------------------------------------------
# finalize()
try:
bmi.finalize()
print (" finalizing...")
pass_count += 1
except:
bmi_except('finalize()')
# lastly - print test summary
print ("\n Total BMI function PASS: " + str(pass_count))
print (" Total BMI function FAIL: " + str(fail_count))
for ff in fail_list:
print (" " + ff)