-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmodflowapi.py
More file actions
83 lines (70 loc) · 2.54 KB
/
modflowapi.py
File metadata and controls
83 lines (70 loc) · 2.54 KB
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
from ctypes import (
byref,
c_char_p,
c_int,
create_string_buffer,
)
from typing import Tuple
from xmipy import XmiWrapper
class ModflowApi(XmiWrapper):
"""
This class extends eXtended Model Interface (XMI) Wrapper (XmiWrapper)
for the MODFLOW API. XMI extends the CSDMS Basic Model Interface
The extension to the XMI does not change anything in the XMI or BMI
interfaces, so models implementing the ModflowApi interface is compatible
with the XmiWrapper which provides XMI and BMI functionality.
"""
def __init__(
self,
lib_path: str,
lib_dependency: str = None,
working_directory: str = ".",
timing: bool = False,
):
super().__init__(
lib_path,
lib_dependency=lib_dependency,
working_directory=working_directory,
timing=timing,
)
def get_attrs_item_count(self) -> int:
count = c_int(0)
self.execute_function(self.lib.get_attrs_item_count, byref(count))
return count.value
def get_attrs_keys(self) -> Tuple[str]:
len_attr_name = self.get_constant_int("BMI_LENATTRNAME")
nr_output_vars = self.get_attrs_item_count()
len_names = nr_output_vars * len_attr_name
names = create_string_buffer(len_names)
# get a (1-dim) char array (char*) containing the output variable
# names as \x00 terminated sub-strings
self.execute_function(self.lib.get_attrs_keys, byref(names))
# decode
output_vars = [
names[i * len_attr_name : (i + 1) * len_attr_name]
.split(b"\0", 1)[0]
.decode("ascii")
for i in range(nr_output_vars)
]
return tuple(output_vars)
def get_var_attrs(self, name: str) -> Tuple[str]:
len_attr_name = self.get_constant_int("BMI_LENATTRNAME")
nr_output_vars = self.get_attrs_item_count()
len_attrs = nr_output_vars * len_attr_name
attrs = create_string_buffer(len_attrs)
# get a (1-dim) char array (char*) containing the output variable
# names as \x00 terminated sub-strings
self.execute_function(
self.lib.get_var_attrs,
c_char_p(name.encode()),
byref(attrs),
detail="for variable " + name
)
# decode
output_vars = [
attrs[i * len_attr_name : (i + 1) * len_attr_name]
.split(b"\0", 1)[0]
.decode("ascii")
for i in range(nr_output_vars)
]
return tuple(output_vars)