-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
375 lines (313 loc) · 11.8 KB
/
test.py
File metadata and controls
375 lines (313 loc) · 11.8 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
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
import re
import sys
import datetime
from dateutil import parser as parser
from dataclasses import dataclass, field
from typing import List
import datetime
import numpy as np
import pandas as pd
import h5py
re_num = r"[\+|-]?\d*\.*\d+(?:[e|E][\+|-]?\d+)?"
ppl_regex_strings = [
r"\'(OLGA) ([\d+.]*\d)\'",
r"(INPUT FILE)\n\'(.*)\'",
r"(PVT FILE)\n\'(.*)\'",
r"(RESTART FILE)\n\'(.*)\'",
r"(DATE)\n\'(.*)\'",
r"(PROJECT)\n\'(.*)\'",
r"(TITLE)\n\'(.*)\'",
r"(AUTHOR)\n\'(.*)\'",
f"(NETWORK)\n({re_num})",
r"(GEOMETRY)\s*\'\s*\((.*)\)\s*\'",
rf"(BRANCH)\n\'(.*)\'\n(\d*)((?:\s*{re_num})*)",
r"(CATALOG)\s*(\d*)((?:\n.+\s*\'(?:BOUNDARY|SECTION):\'\s*\'BRANCH:\'\s*\'.*\'\s*\'\(.*\)\'\s*\'.*\')*)",
rf"(TIME SERIES)\s*\'\s*\((.*)\)\s*\'((?:\s*{re_num})*)",
]
def ppl_to_hdf5(ppl_file_path, hdf5_file_name):
ppl_matches = parse_ppl_file(ppl_file_path)
with h5py.File(f"{hdf5_file_name}.h5", "w") as hdf5:
build_hdf5_from_regex_matches(hdf5, ppl_matches)
def parse_ppl_file(ppl_file_path):
regex = re.compile("|".join(ppl_regex_strings))
with open(ppl_file_path, "r") as f:
data = f.read()
matches = [tuple(v for v in m if v != "") for m in regex.findall(data)]
return matches
keywords = [
"olga",
"input_file",
"pvt_file",
"restart_file",
"date",
"project",
"title",
"author",
"network",
"geometry",
]
def build_hdf5_from_regex_matches(hdf5_file, matches):
for match in matches:
keyword = match[0].lower().replace(" ", "_")
if keyword in keywords:
hdf5_file.attrs.create(keyword, match[1])
branches = [match for match in matches if match[0].lower() == "branch"]
catalog = [match for match in matches if match[0].lower() == "catalog"][0]
time_series = [match for match in matches if match[0].lower() == "time series"][0]
add_time_series_data_to_hdf5_file(hdf5_file, branches, catalog, time_series)
def add_time_series_data_to_hdf5_file(hdf5_file, branches, catalog, time_series):
create_branches_in_hdf5_file(hdf5_file, branches)
add_ts_as_dataframes(hdf5_file, catalog, time_series)
def create_branches_in_hdf5_file(hdf5_file, branches):
for branch in branches:
geometry = pd.DataFrame(
np.reshape(branch[3].strip().split(), (2, int(branch[2]) + 1)).T.astype(
np.float64
),
columns=["Length", "Elevation"],
)
hdf5_file.create_group(branch[1])
hdf5_file[f"/{branch[1]}"].attrs.create("geometry", geometry)
def add_ts_as_dataframes(hdf5_file, catalog, time_series):
catalog, time_steps = build_catalog_with_ts(catalog, time_series)
hdf5_file.attrs.create("time_steps", np.array(time_steps))
add_ts_to_branches(hdf5_file, catalog)
def build_catalog_with_ts(catalog, time_series):
cat = build_catalog(catalog)
cat_inc = len(cat) + 1
time_steps = []
for n, v in enumerate(time_series[2].strip().split("\n")):
row = n % cat_inc
if row == 0:
time_steps.append(float(v))
else:
cat[row - 1]['time_series'].append(np.array(v.split(), dtype=np.float))
return cat, time_steps
def build_catalog(catalog):
p = re.compile(
r"(.*) \'(SECTION|BOUNDARY):\' \'BRANCH:\' \'(.*)\' \'\((.*)\)\' \'(.*)\'"
)
cat = []
cat_list = catalog[2].strip().split("\n")
for c in cat_list:
m = p.findall(c)[0]
cat.append({"symbol": m[0], "kind": m[1], "branch": m[2], "units": m[3], "description": m[4], "time_series": []})
return cat
def add_ts_to_branches(hdf5_file, catalog):
for branch in hdf5_file.keys():
print(hdf5_file[f"/{branch}"].attrs.get("geometry"))
def _generate_branch_data(self):
for c in self.catalog:
self.branches[c.branch].catalogs.append(c)
times = len(self.time_series)
for _, branch in self.branches.items():
nodes = branch.geometry.shape[0]
sections = len([c for c in branch.catalogs if c.kind == "SECTION"])
s = np.zeros((nodes - 1, times, sections))
b = np.zeros((nodes, times, len(branch.catalogs) - sections))
i_bound = i_sec = 0
for cat in branch.catalogs:
if cat.kind == "SECTION":
s[:, :, i_sec] = np.array(cat.time_series).T
i_sec += 1
else:
b[:, :, i_bound] = np.array(cat.time_series).T
i_bound += 1
b = b.reshape((np.prod(b.shape[:-1]), b.shape[-1]))
midx_b = pd.MultiIndex.from_product(
[branch.geometry["Length"], self.time_series]
)
branch.boundaries = pd.DataFrame(
data=b,
index=midx_b,
columns=[c.symbol for c in branch.catalogs if c.kind == "BOUNDARY"],
)
s = s.reshape((np.prod(s.shape[:-1]), s.shape[-1]))
midx_s = pd.MultiIndex.from_product(
[branch.geometry["Length"][:-1], self.time_series]
)
branch.sections = pd.DataFrame(
data=s,
index=midx_s,
columns=[c.symbol for c in branch.catalogs if c.kind == "SECTION"],
)
ppl_to_hdf5("tests\\test_files\\SD1H_35.ppl", "test")
hdf = h5py.File("test.h5", "r")
print(hdf.keys())
hdf.close()
"""
class PPLReader:
pass
regex_strings = [
r"\'(OLGA) ([\d+.]*\d)\'",
r"(INPUT FILE)\n\'(.*)\'",
r"(PVT FILE)\n\'(.*)\'",
r"(RESTART FILE)\n\'(.*)\'",
r"(DATE)\n\'(.*)\'",
r"(PROJECT)\n\'(.*)\'",
r"(TITLE)\n\'(.*)\'",
r"(AUTHOR)\n\'(.*)\'",
f"(NETWORK)\n({re_num})",
r"(GEOMETRY)\s*\'\s*\((.*)\)\s*\'",
rf"(BRANCH)\n\'(.*)\'\n(\d*)((?:\s*{re_num})*)",
r"(CATALOG)\s*(\d*)((?:\n.+\s*\'(?:BOUNDARY|SECTION):\'\s*\'BRANCH:\'\s*\'.*\'\s*\'\(.*\)\'\s*\'.*\')*)",
rf"(TIME SERIES)\s*\'\s*\((.*)\)\s*\'((?:\s*{re_num})*)",
]
def __init__(self, file_path):
self.file_path = file_path
def read(self):
regex = re.compile("|".join(self.regex_strings))
with open(self.file_path, "r") as f:
data = f.read()
m = [tuple(v for v in m if v != "") for m in regex.findall(data)]
ppl = PPL()
ppl.build_from_regex_matches(m)
return ppl
class PPL:
processors = {
"OLGA": "_process_olga_version",
"INPUT FILE": "_process_standard_string",
"PVT FILE": "_process_standard_string",
"RESTART FILE": "_process_standard_string",
"DATE": "_process_date",
"PROJECT": "_process_standard_string",
"TITLE": "_process_standard_string",
"AUTHOR": "_process_standard_string",
"NETWORK": "_process_network",
"GEOMETRY": "_process_standard_string",
"BRANCH": "_process_branch",
"CATALOG": "_process_catalog",
"TIME SERIES": "_process_time_series",
}
def __init__(self):
self.olga = None
self.input_file = None
self.pvt_file = None
self.restart_file = None
self.date = None
self.project = None
self.title = None
self.author = None
self.network = None
self.geometry = None
self.branches = {}
self.catalog = []
self.time_series = []
def build_from_regex_matches(self, matches):
for match in matches:
f = getattr(self, self.processors[match[0].upper()])
f(match)
self._generate_branch_data()
def build_from_regex_matches(self, matches):
for match in matches:
f = getattr(self, self.processors[match[0].upper()])
f(match)
self._generate_branch_data()
def _process_standard_string(self, match):
attr = match[0].lower().replace(" ", "_")
setattr(self, attr, match[1])
def _process_olga_version(self, match):
self.olga_version = match[1]
def _process_date(self, match):
self.date = parser.parse(match[1], yearfirst=True)
def _process_network(self, match):
self.network = int(match[1])
def _process_branch(self, match):
name = match[1]
count = int(match[2])
geometry = pd.DataFrame(
np.reshape(match[3].split(), (2, count + 1)).T.astype(np.float64),
columns=["Length", "Elevation"],
)
self.branches[name] = Branch(name, count, geometry)
def _process_catalog(self, match):
p = re.compile(
r"(.*) \'(SECTION|BOUNDARY):\' \'BRANCH:\' \'(.*)\' \'\((.*)\)\' \'(.*)\'"
)
cat_list = match[2].strip().split("\n")
for cat in cat_list:
m = p.findall(cat)[0]
self.catalog.append(Catalog(m[0], m[1], m[2], m[3], m[4]))
if len(self.catalog) != int(match[1]):
raise Exception
def _process_time_series(self, match):
# convert time series data to a list of floats for time value and Pandas Series' for time series data
time_series = [
np.array(r.split(), dtype=np.float)
if n % (len(self.catalog) + 1) > 0
else float(r)
for n, r in enumerate(match[2].strip().split("\n"))
]
for n, v in enumerate(time_series):
row = n % (len(self.catalog) + 1)
if row == 0:
self.time_series.append(v)
else:
self.catalog[row - 1].time_series.append(v)
def _generate_branch_data(self):
for c in self.catalog:
self.branches[c.branch].catalogs.append(c)
times = len(self.time_series)
for _, branch in self.branches.items():
nodes = branch.geometry.shape[0]
sections = len([c for c in branch.catalogs if c.kind == "SECTION"])
s = np.zeros((nodes - 1, times, sections))
b = np.zeros((nodes, times, len(branch.catalogs) - sections))
i_bound = i_sec = 0
for cat in branch.catalogs:
if cat.kind == "SECTION":
s[:, :, i_sec] = np.array(cat.time_series).T
i_sec += 1
else:
b[:, :, i_bound] = np.array(cat.time_series).T
i_bound += 1
b = b.reshape((np.prod(b.shape[:-1]), b.shape[-1]))
midx_b = pd.MultiIndex.from_product(
[branch.geometry["Length"], self.time_series]
)
branch.boundaries = pd.DataFrame(
data=b,
index=midx_b,
columns=[c.symbol for c in branch.catalogs if c.kind == "BOUNDARY"],
)
s = s.reshape((np.prod(s.shape[:-1]), s.shape[-1]))
midx_s = pd.MultiIndex.from_product(
[branch.geometry["Length"][:-1], self.time_series]
)
branch.sections = pd.DataFrame(
data=s,
index=midx_s,
columns=[c.symbol for c in branch.catalogs if c.kind == "SECTION"],
)
def to_hdf5(self, filename):
with h5py.File(f"{filename}.h5", 'w') as hdf5:
hdf5.attrs.create("olga", str(self.olga))
hdf5.attrs.create("input_file", self.input_file)
hdf5.attrs.create("pvt_file", self.pvt_file)
hdf5.attrs.create("restart_file", self.restart_file)
hdf5.attrs.create("date", str(self.date))
hdf5.attrs.create("project", self.project)
hdf5.attrs.create("title", self.title)
hdf5.attrs.create("author", self.author)
@dataclass
class Catalog:
symbol: str
kind: str
branch: str
units: str
description: str
time_series: List[np.ndarray] = field(default_factory=list)
def __repr__(self):
return self.symbol
@dataclass
class Branch:
name: str
count: int
geometry: pd.DataFrame
catalogs: List[Catalog] = field(default_factory=list)
ppl = PPLReader("tests\\test_files\\SD1H_35.ppl").read()
ppl.to_hdf5("test")
hdf = h5py.File("test.h5", 'r')
print(hdf.attrs["olga"])
hdf.close()
"""