-
Notifications
You must be signed in to change notification settings - Fork 5
/
budyko_geometry_file_creation.py
51 lines (38 loc) · 1.28 KB
/
budyko_geometry_file_creation.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
##Budyko=group
##Prepare subreaches' geometry file=name
##ParameterFile|MODEL_FILE|Model setup file|False|False
##OutputFile|GEOMETRY_FILE|Output file with model subreaches' geometry|txt
import pandas as pd
import sys
from contextlib import contextmanager
from processing.tools.system import getTempFilename
from budyko_model import geometry_file_creation
from budyko_model.river_network import RiverNetwork
from budyko_model.modelfile import ModelFile
class ProgressLogger:
def __init__(self, progress):
self.progress = progress
def write(self, msg):
self.progress.setConsoleInfo(msg)
@contextmanager
def redirect_stdout(progress):
oldout = sys.stdout
sys.stdout = ProgressLogger(progress)
try:
yield
finally:
sys.stdout = oldout
with redirect_stdout(progress):
routing = RiverNetwork(MODEL_FILE)
model = ModelFile(MODEL_FILE)
# Get drainage network
drains_to = routing.drains_to
temp_file = getTempFilename("csv")
geometry_file_creation.read_network_file(model, routing, temp_file)
subreaches = pd.read_csv(temp_file, index_col="Subbasin")
geometry_file_creation.write_geometry_file(
subreaches=subreaches,
model=model,
drains_to=drains_to,
geometry_file=GEOMETRY_FILE,
)