-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqa_common.py
272 lines (227 loc) · 7.12 KB
/
qa_common.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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 09:41:07 2018
@author: gehammo
"""
import re
import sys
import numpy as np
from qa_debug import *
header_ = re.compile('-')
units_dict = {}
units_dict['y'] = 3600.*24.*365.
units_dict['yr'] = units_dict['y']
units_dict['yrs'] = units_dict['y']
units_dict['year'] = units_dict['y']
units_dict['years'] = units_dict['y']
units_dict['mo'] = units_dict['y']/12.
units_dict['month'] = units_dict['mo']
units_dict['d'] = 3600.*24.
units_dict['day'] = units_dict['d']
units_dict['h'] = 3600.
units_dict['hr'] = units_dict['h']
units_dict['hour'] = units_dict['h']
units_dict['m'] = 60.
units_dict['min'] = units_dict['m']
units_dict['minute'] = units_dict['m']
units_dict['s'] = 1.
units_dict['sec'] = units_dict['s']
units_dict['second'] = units_dict['s']
def dict_to_string(dictionary):
string = ''
for key, value in dictionary.items():
string += ' {0} : {1}\n'.format(key,value)
return string
def list_to_string(list_):
return ','.join(list_)
def time_strings_to_float_list(strings):
list_of_times = []
try:
w = strings[-1].split()
if len(w) > 1:
units = w[1]
elif float(w[0]) == -999.:
units = 's'
else:
print_err_msg('Must specify units on last time or all times in options file')
except:
print_err_msg('error converting float in '+
time_strings_to_float_list.__name__)
for string in strings:
w = string.split()
# time in seconds
time = float(w[0])
if len(w) > 1:
time *= unit_conversion(w[1])
else:
time *= unit_conversion(units)
list_of_times.append(time)
return list_of_times
def time_strings_to_float_list_for_documentation(strings):
list_of_times = []
for string in strings:
try:
w = string.split()
# time in seconds
time = float(w[0])
except:
print('error converting float in list_to_floats'+
time_strings_to_float_list_for_documentation.__name__)
raise
list_of_times.append(time)
return list_of_times
def location_strings_to_float_list(loc):
locations=[x.strip() for x in loc.split(',')]
list_of_locations=[]
for i in range(len(locations)):
w = locations[i].split()
w = [float(n) for n in w]
list_of_locations.append(w)
return list_of_locations
def unit_conversion(string):
# scales to seconds
scale = 1.
if string.startswith('y'):
scale = 3600.*24.*365.
elif string.startswith('mo'):
scale = 3600.*24.*365./12.
elif string.startswith('d'):
scale = 3600.*24.
elif string.startswith('h'):
scale = 3600.
elif string.startswith('m'):
scale = 60.
elif string.startswith('s'):
scale = 1.
else:
print('error converting time units: {}'.format(string))
raise
return scale
def list_to_dict(input_list):
"""Converts list of items to a dict"""
debug_push('list_to_dict')
dictionary = {}
for item in input_list:
dictionary[item[0]] = item[1]
debug_pop()
return dictionary
def print_header(char,title):
string = '---------------------------------------------------------'
string=header_.sub(char,string)
tab = 15
clip_len = 78-len(title)-tab-2
print('\n {} {} {} \n'.format(string[:tab],title,string[:clip_len]))
def string_to_number(string):
try:
number = int(string)
except:
try:
number = float(string)
except:
print('cannot use a string for values in options')
sys.exit(0)
return number
def exponent_conversion(s):
try:
val = float(s)
except ValueError:
s = s.replace('+', 'E+').replace('-', 'E-')
return s
def get_h5_output_filename(input_filename,simulator_name):
strings = []
strings.append(input_filename.rsplit('.',1)[0])
if len(simulator_name) > 1:
strings.append('_')
strings.append(simulator_name)
strings.append('.h5')
return ''.join(strings)
def qa_lookup(dictionary,keyword,default_value):
value = default_value
if keyword in dictionary:
value = dictionary[keyword]
else:
if default_value == "fail_on_missing_keyword":
print_err_msg('Need to specify {} in options file'.format(keyword))
if value == 'True':
value = True
elif value == 'False':
value = False
return value
def find_axis_1D(x,y,z):
x_axis = None
if len(x) > 1:
if check_array_equal(x)==False:
x_axis = x
elif len(y)>1:
if check_array_equal(y)==False:
x_axis = y
elif len(z)>1:
if check_array_equal(z)==False:
x_axis = z
try:
if x_axis == None:
raise Exception('Invalid coordinates, check x,y, and z') ###better error message here
except:
return x_axis
def find_axis_2D(x,y,z):
# x_axis = None
# y_axis = None
if len(x) == 1:
x_axis = y
y_axis = z
elif len(y)==1:
x_axis = x
y_axis = z
elif len(z)==1:
x_axis = x
y_axis = y
# if x_axis == None and y_axis == None:
# raise Exception('Invalid coordinates, check x,y, and z')
return x_axis, y_axis
def print_err_msg(*strings):
list = []
for string in strings:
list.append(string)
string = ''.join(list)
if debug_verbose_error():
raise Exception(string)
else:
print(string)
sys.exit(1)
def check_array_equal(x):
x = iter(x)
try:
first = next(x)
except StopIteration:
return True
return all(first == rest for rest in x)
def check_coordinates_2D(x,x2,y,y2):
coord_eps = 1.e-6
if x.size == x2.size:
for i in range(x.size):
if abs(x[i]-x2[i]) > coord_eps:
print_err_msg('X coordinate between two solutions mismatch at index {}. \
Grids must match for 2D solutions'.format(i+1))
else:
print_err_msg('X coordinate dimension mismatch between two solutions: {} {} \
Grids must match for 2D solutions'. \
format(x.size,x2.size))
if y.size == y2.size:
for i in range(y.size):
if abs(y[i]-y2[i]) > coord_eps:
print_err_msg('Y coordinate between two solutions mismatch at index {}. \
Grids must match for 2D solutions'.format(i+1))
else:
print_err_msg('Y coordinate dimension mismatch between two solutions: {} {} \
Grids must match for 2D solutions'. \
format(y.size,y2.size))
if debug_verbose():
print('Coordinates match')
def format_floating_number(value):
if value < 1:
value = ("%.2e" % value)
elif value >= 1 and value < 1000:
value = ("%.2f" % value)
elif value >= 1000:
value = ("%.2e" % value)
return value