-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwasp.py
418 lines (357 loc) · 12.2 KB
/
wasp.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
"""This module is a wrapper for libhydrogeo (WASP 8)"""
import os
import datetime
import ctypes as ct
import constants as co
class Wasp(object):
def __init__(self, libname=co.LIB_NAME, libpath=None):
"""Initialization for class
Seach and load the library ... else exit.
Args:
libname (str): The library name (libhydrolink.so on GNU/Linux)
libpath (str): The path where we search for the file
"""
if not libpath:
# Get LD_LIBRARY_PATH
ldp = list()
if 'LD_LIBRARY_PATH' in os.environ:
ldp = os.environ['LD_LIBRARY_PATH'].split(':')
# Append current directory
ldp.append(os.getcwd())
# Is there?
for p in ldp:
fp = os.path.join(p, libname)
if os.path.exists(fp):
libpath = p
break
if not libpath:
print('Can NOT find {}'.format(co.LIB_NAME))
sys.exit(-1)
self.lpath = os.path.join(libpath, libname)
self.hydro = ct.cdll.LoadLibrary(self.lpath)
self.cfpath = None
self.hndl = None
self.dlevel = -1
def setDebug(self, dlevel=0):
"""Set the libhydrogeo debug level
Seach and load the library.
Args:
dlevel (int): Set the level. Greather than O to get log outputs in log files.
"""
d = ct.c_int(dlevel)
self.hydro.hlsetdebug(ct.byref(d))
self.dlevel = dlevel
def getLastError(self):
"""Get last libhydrogeo error.
Returns:
str: The string with the error
"""
m = ct.c_char_p(b''*72)
self.hydro.hlgetlasterror(m)
return m.value.decode('utf-8')
def open(self, fpath, fmode=co.OPEN_WRITE):
"""Open file for reading or writing
Args:
fpath (str): The file path for open.
fmode (int): 0 = read; 1 = write.
"""
fp = ct.c_char_p(fpath.encode())
fm = ct.c_int(fmode)
fh = ct.c_int(0)
ie = ct.c_int(0)
self.hydro.hlopen(fp, ct.byref(fm), ct.byref(fh), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
self.hndl = -1
else:
# Raw fh
self.cfpath = fpath
# By ref!
self.hndl = ct.byref(fh)
def close(self):
"""Close an opened file"""
ie = ct.c_int(0)
if self.hndl:
self.hydro.hlclose(self.hndl, ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
def compactFactor(self):
"""Get compression factor for the file.
Returns:
int: The compact factor
"""
cf = ct.c_int(1)
ie = ct.c_int(0)
if self.hndl:
self.hydro.hlgetcompfact(self.hndl, ct.byref(cf), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
else:
self.cf = cf.value*100
def setLang(self, lang=co.LANG_C):
"""Set programming language for the library
Fortran use base 1 arrays. C, C++, python use base 0.
Args:
lang (int): 0 = C; 1 = Fortran.
"""
fl = ct.c_int(lang)
ie = ct.c_int(0)
self.hydro.hlsetlanguage(self.hndl, ct.byref(fl), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
def setCreator(self, creator=co.CREATOR_EFDCMPI):
"""Inform the library who is the creator for this HYD file
libhydrolink recognizes EFDC, HECRAS, DYNHYD, EPDRIV1 = 3
Args:
creator (int): 1 = EFDC and EFDCMPI ; 2 = HECRAS and DYNHYD, 3=EPDRIV1.
"""
fc = ct.c_int(creator)
ie = ct.c_int(0)
self.hydro.hlsetcreator(self.hndl, ct.byref(fc), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
def setDescriptions(self, desc=list()):
"""Add descriptions to the HYD file
Args:
desc (list): List of string with descriptions
"""
if len(desc) > 0:
n = len(desc)
for i in range(n):
fd = ct.c_char_p(desc[i].encode())
# 0 for decriptions
dd = ct.c_int(0)
ie = ct.c_int(0)
self.hydro.hladddescription(
self.hndl,
ct.byref(dd),
fd,
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
self.desc = desc
def setAuthor(self, author):
"""Add autor to the HYD file
Args:
author (str): The autor name
"""
fd = ct.c_char_p(author.encode())
# 1 for modeller name
dd = ct.c_int(1)
ie = ct.c_int(0)
self.hydro.hladddescription(
self.hndl,
ct.byref(dd),
fd,
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
else:
self.author = author
def setMoment(self, dt):
"""Set the initial time and date for th hydrodynamic information in the linkage file.
Args:
dt (datetime.datetime): The date and time.
"""
ida = ct.c_int(dt.day)
imo = ct.c_int(dt.month)
iye = ct.c_int(dt.year)
iho = ct.c_int(dt.hour)
imi = ct.c_int(dt.minute)
ise = ct.c_int(dt.second)
ie = ct.c_int(0)
self.hydro.hlsetseedmoment(
self.hndl,
ct.byref(imo),
ct.byref(ida),
ct.byref(iye),
ct.byref(iho),
ct.byref(imi),
ct.byref(ise),
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
else:
self.moment = dt
def setNumLay(self, nlays=1):
"""Set the number of layers for the hydrodynamic information in the linkage file.
Args:
nlays (int): The number of layers in the model.
"""
fn = ct.c_int(nlays)
ie = ct.c_int(0)
self.hydro.hlsetnumlayers(self.hndl, ct.byref(fn), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
else:
self.nlays = nlays
def setNumSeg(self, nsegs):
"""Set the number of segments for the hydrodynamic information in the linkage file.
Args:
nsegs (int): The number of segments in the model.
"""
fn = ct.c_int(nsegs)
ie = ct.c_int(0)
self.hydro.hlsetnumsegments(self.hndl, ct.byref(fn), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
else:
self.nsegs = nsegs
def setSegNames(self, segsn=list()):
"""Set segments names.
Args:
segsn (list): A list with segments names.
"""
if len(segsn) > 0:
n = len(segsn)
for i in range(n):
sn = ct.c_char_p(segsn[i].encode())
se = ct.c_int(i)
ie = ct.c_int(0)
self.hydro.hlsetsegname(
self.hndl,
ct.byref(se),
sn,
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
self.segsn = segsn
def setTimeStep(self, ts):
"""Specifies the timestep that was used for the hydrodynamic model simulation.
This value only needs to be set once
Args:
ts (int): The time step used in the hydrodynamic models.
"""
ts = ct.c_int(ts)
ie = ct.c_int(0)
self.hydro.hlsethydtimestep(self.hndl, ct.byref(ts), ct.byref(ie))
if ie.value > 0:
print(self.getLastError())
else:
self.ts = ts
def setNumSegConst(self, n):
"""This function is used to set the number of segment constituents that will be written to the hydrodynamic linkage file
The current version of the HYDROLINK API assumes a particular order. To get to a particular constituent you must define the earlier ones. Segment constituents are: volume, depth, velocity, temperature and salinity
Args:
n (int): The number constituents to be passed to the file
"""
nn = ct.c_int(n)
ie = ct.c_int(0)
self.hydro.hlsetnumsegconsts(
self.hndl,
ct.byref(nn),
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
self.nsc = n
def setNumFPConst(self, n):
"""This function is used to specify the number of flow path constituents. The number of flow path constituents that are passed by the hydrodynamic model is typically a function of the dimensionality of the model
For models like EFDC the number of flow path constituents is three: 1) Flow 2) Dispersion/residual flow, 3) Direction of Flow. For simple 1 dimensional models like DYNHYD the number of flow path constituents is one, Flow.
Args:
n (int): The number constituents to be passed to the file
"""
nn = ct.c_int(n)
ie = ct.c_int(0)
self.hydro.hlsetnumfpconsts(
self.hndl,
ct.byref(nn),
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
self.nfpc = n
def _setSegData(self, stype, data):
"""Set segments data.
Args:
stype (int): Type information. 1=Segment Volume (m3), 2=Segment Depth (m),
3=Segment Velocity (m/sec), 4=Segment Temperature (C), 5=Segment Salinity (!)
data (list): List of floats with data
"""
n = len(data)
ad = (ct.c_double * n)()
for i in range(n):
ad[i] = data[i]
si = ct.c_int(stype)
ie = ct.c_int(0)
self.hydro.hlsetseginfo(
self.hndl,
ct.byref(si),
ct.byref(ad),
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
def setSegVolume(self, vols):
"""Set segments volumes.
Args:
vols (list): List of floats with segments volumes
"""
self._setSegData(co.SEG_VOL, vols)
self.vols = vols
def setSegDepth(self, deps):
"""Set segments depths.
Args:
deps (list): List of floats with segments depths
"""
self._setSegData(co.SEG_DEP, deps)
self.deps = deps
def setSegVelocity(self, vels):
"""Set segments velocities.
Args:
vels (list): List of floats with segments velocities
"""
self._setSegData(co.SEG_VEL, vels)
self.vels = vels
def setSegTemperature(self, temps):
"""Set segments velocities.
Args:
temps (list): List of floats with segments temperatures
"""
self._setSegData(co.SEG_TEM, temps)
self.temps = temps
def setSegSalinity(self, sals):
"""Set segments velocities.
Args:
sals (list): List of floats with segments salinities
"""
self._setSegData(co.SEG_SAL, sals)
self.sals = sals
def _setFlowData(self, ftype, data):
"""Set flow data.
Args:
ftype (int): Type information. 1=Advective flow, 2=Dispersive flow
data (list): List of floats with data
"""
n = len(data)
ad = (ct.c_double * n)()
for i in range(n):
ad[i] = data[i]
si = ct.c_int(ftype)
ie = ct.c_int(0)
self.hydro.hlsetflowinfo(
self.hndl,
ct.byref(si),
ct.byref(ad),
ct.byref(ie)
)
if ie.value > 0:
print(self.getLastError())
def setFlowAdvect(self, fadvs):
"""Set advective flows
Args:
fadvs (list): List of floats with advective flows
"""
self._setFlowData(co.FLOWP_ADV, fadvs)
self.fadvs = fadvs
def setFlowDisps(self, fdisps):
"""Set dispersive flows
Args:
fdisps (list): List of floats with advective flows
"""
self._setFlowData(co.FLOWP_DIS, fdisps)
self.fdisps = fdisps