|
1 |
| -import numpy as np |
2 |
| - |
3 |
| -from .custom_exceptions import NoDataForQuery, InvalidQuantity |
4 |
| -from .various import NAME_DELIMITER |
5 |
| - |
6 |
| - |
7 |
| -class QueryData: |
8 |
| - """ |
9 |
| - Base query class that declares what data to extract from a .res1d file. |
10 |
| - """ |
11 |
| - |
12 |
| - def __init__(self, quantity, name=None, validate=True): |
13 |
| - self._name = name |
14 |
| - self._quantity = quantity |
15 |
| - |
16 |
| - if validate: |
17 |
| - self._validate() |
18 |
| - |
19 |
| - def _validate(self): |
20 |
| - if not isinstance(self.quantity, str): |
21 |
| - raise TypeError("Quantity must be a string.") |
22 |
| - |
23 |
| - if self.name is not None and not isinstance(self.name, str): |
24 |
| - raise TypeError("Argument 'name' must be either None or a string.") |
25 |
| - |
26 |
| - @staticmethod |
27 |
| - def from_dotnet_to_python(array): |
28 |
| - """Convert .NET array to numpy.""" |
29 |
| - return np.fromiter(array, np.float64) |
30 |
| - |
31 |
| - @property |
32 |
| - def quantity(self): |
33 |
| - return self._quantity |
34 |
| - |
35 |
| - @property |
36 |
| - def name(self): |
37 |
| - return self._name |
38 |
| - |
39 |
| - def _check_invalid_quantity(self, res1d): |
40 |
| - if self._quantity not in res1d.quantities: |
41 |
| - raise InvalidQuantity(f"Undefined quantity {self._quantity}. " |
42 |
| - f"Allowed quantities are: {', '.join(res1d.quantities)}.") |
43 |
| - |
44 |
| - def _check_invalid_values(self, values): |
45 |
| - if values is None: |
46 |
| - raise NoDataForQuery(str(self)) |
47 |
| - |
48 |
| - def __repr__(self): |
49 |
| - return NAME_DELIMITER.join([self._quantity, self._name]) |
50 |
| - |
51 |
| - |
52 |
| -class QueryDataReach(QueryData): |
53 |
| - """A query object that declares what reach data to extract from a .res1d file. |
54 |
| -
|
55 |
| - Parameters |
56 |
| - ---------- |
57 |
| - quantity: str |
58 |
| - e.g. 'WaterLevel' or 'Discharge'. Call mike1d_quantities() to get all quantities. |
59 |
| - name: str, optional |
60 |
| - Reach name |
61 |
| -
|
62 |
| - Examples |
63 |
| - -------- |
64 |
| - `QueryDataReach('WaterLevel', 'reach1', 10)` is a valid query. |
65 |
| - """ |
66 |
| - |
67 |
| - def __init__(self, quantity, name=None, chainage=None, validate=True): |
68 |
| - super().__init__(quantity, name, validate=False) |
69 |
| - self._chainage = chainage |
70 |
| - |
71 |
| - if validate: |
72 |
| - self._validate() |
73 |
| - |
74 |
| - def _validate(self): |
75 |
| - super()._validate() |
76 |
| - |
77 |
| - if self.chainage is not None and not isinstance(self.chainage, (int, float)): |
78 |
| - raise TypeError("Argument 'chainage' must be either None or a number.") |
79 |
| - |
80 |
| - if self.name is None and self.chainage is not None: |
81 |
| - raise ValueError("Argument 'chainage' cannot be set if name is None.") |
82 |
| - |
83 |
| - def get_values(self, res1d): |
84 |
| - self._check_invalid_quantity(res1d) |
85 |
| - |
86 |
| - name = self._name |
87 |
| - chainage = self._chainage |
88 |
| - quantity = self._quantity |
89 |
| - |
90 |
| - values = ( res1d.query.GetReachValues(name, chainage, quantity) |
91 |
| - if chainage is not None else |
92 |
| - res1d.query.GetReachStartValues(name, quantity) ) |
93 |
| - |
94 |
| - self._check_invalid_values(values) |
95 |
| - |
96 |
| - return self.from_dotnet_to_python(values) |
97 |
| - |
98 |
| - @property |
99 |
| - def chainage(self): |
100 |
| - return self._chainage |
101 |
| - |
102 |
| - def __repr__(self): |
103 |
| - name = self._name |
104 |
| - chainage = self._chainage |
105 |
| - quantity = self._quantity |
106 |
| - |
107 |
| - return ( NAME_DELIMITER.join([quantity, name, f'{chainage:g}']) |
108 |
| - if chainage is not None else |
109 |
| - NAME_DELIMITER.join([quantity, name]) ) |
110 |
| - |
111 |
| - |
112 |
| -class QueryDataNode(QueryData): |
113 |
| - """A query object that declares what node data to extract from a .res1d file. |
114 |
| -
|
115 |
| - Parameters |
116 |
| - ---------- |
117 |
| - quantity: str |
118 |
| - e.g. 'WaterLevel' or 'Discharge'. Call mike1d_quantities() to get all quantities. |
119 |
| - name: str, optional |
120 |
| - Node name |
121 |
| -
|
122 |
| - Examples |
123 |
| - -------- |
124 |
| - `QueryDataNode('WaterLevel', 'node1')` is a valid query. |
125 |
| - """ |
126 |
| - |
127 |
| - def __init__(self, quantity, name=None, validate=True): |
128 |
| - super().__init__(quantity, name, validate) |
129 |
| - |
130 |
| - def get_values(self, res1d): |
131 |
| - self._check_invalid_quantity(res1d) |
132 |
| - |
133 |
| - values = res1d.query.GetNodeValues(self._name, self._quantity) |
134 |
| - |
135 |
| - self._check_invalid_values(values) |
136 |
| - |
137 |
| - return self.from_dotnet_to_python(values) |
138 |
| - |
139 |
| - |
140 |
| -class QueryDataCatchment(QueryData): |
141 |
| - """A query object that declares what catchment data to extract from a .res1d file. |
142 |
| -
|
143 |
| - Parameters |
144 |
| - ---------- |
145 |
| - quantity: str |
146 |
| - e.g. 'TotalRunoff'. Call mike1d_quantities() to get all quantities. |
147 |
| - name: str, optional |
148 |
| - Catchment name |
149 |
| -
|
150 |
| - Examples |
151 |
| - -------- |
152 |
| - `QueryDataCatchment('TotalRunoff', 'catchment1')` is a valid query. |
153 |
| - """ |
154 |
| - |
155 |
| - def __init__(self, quantity, name=None, validate=True): |
156 |
| - super().__init__(quantity, name, validate) |
157 |
| - |
158 |
| - def get_values(self, res1d): |
159 |
| - self._check_invalid_quantity(res1d) |
160 |
| - |
161 |
| - values = res1d.query.GetCatchmentValues(self._name, self._quantity) |
162 |
| - |
163 |
| - self._check_invalid_values(values) |
164 |
| - |
165 |
| - return self.from_dotnet_to_python(values) |
| 1 | +from .result_query import QueryData |
| 2 | +from .result_query import QueryDataCatchment |
| 3 | +from .result_query import QueryDataGlobal |
| 4 | +from .result_query import QueryDataNode |
| 5 | +from .result_query import QueryDataReach |
| 6 | +from .result_query import QueryDataStructure |
0 commit comments