-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathinflow.py
More file actions
executable file
·151 lines (117 loc) · 6.03 KB
/
Copy pathinflow.py
File metadata and controls
executable file
·151 lines (117 loc) · 6.03 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
"""
Copyright 2019 European Union
Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at:
https://joinup.ec.europa.eu/sites/default/files/inline-files/EUPL%20v1_2%20EN(1).txt
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
"""
from __future__ import print_function, absolute_import, unicode_literals
from nine import iteritems
import warnings
from pcraster import numpy2pcr, Nominal, pcr2numpy, timeinputscalar
import numpy as np
from ..global_modules.settings import LisSettings, MaskInfo
from ..global_modules.add1 import loadmap, read_tss_header, compressArray
from ..global_modules.errors import LisfloodWarning
from . import HydroModule
class inflow(HydroModule):
"""
# ************************************************************
# ***** READ INFLOW HYDROGRAPHS (OPTIONAL)****************
# ************************************************************
# If option "inflow" is set to 1 the inflow hydrograph code is used
# otherwise dummy code is used
"""
input_files_keys = {'inflow': ['InflowPoints', 'QInTS']}
module_name = 'InFlow'
def __init__(self, inflow_variable):
self.var = inflow_variable
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
def initial(self):
""" Initialization part of the inflow module.
"""
# ************************************************************
# ***** INFLOW INIT
# ************************************************************
settings = LisSettings.instance()
option = settings.options
if option['inflow']:
self.var.InflowPoints = loadmap('InflowPoints') #1D array size is pixels belonging to basin mask
self.var.QInM3Old = np.where(self.var.InflowPoints > 0, self.var.ChanQAvgDt * self.var.DtSec, 0)
# inflow volume for model step
# read inflow map
inflowmapprc = loadmap('InflowPoints', pcr=True)
inflowmapnp = pcr2numpy(inflowmapprc, -9999) #2D array
inflowmapnp = np.where(inflowmapnp > 0, inflowmapnp, 0)
# get outlets ids from outlets map
inflow_ids = np.unique(inflowmapnp)
# drop negative values (= missing data in pcraster map)
inflow_ids = inflow_ids[inflow_ids > 0]
# read tss ids from tss file
settings = LisSettings.instance()
tss_ids = read_tss_header(settings.binding['QInTS'])
# create a dictionary of tss id : tss id index
id_dict = {}
for i in range(len(tss_ids)):
id_dict[tss_ids[i]] = tss_ids.index(tss_ids[i]) + 1
# remove inflow point if not available in tss file
for inf_id in inflow_ids:
if inf_id not in tss_ids:
id_dict[inf_id] = 0
warnings.warn(LisfloodWarning("Inflow point was removed ID: %d\n" % inf_id))
# substitute indexes to id in map
self.var.InflowPointsMap = np.copy(inflowmapnp) #np
for k, v in iteritems(id_dict):
self.var.InflowPointsMap[inflowmapnp == k] = v #np
# convert map to pcraster format
self.var.InflowPointsMap = numpy2pcr(Nominal, self.var.InflowPointsMap, -9999) #pcr
# Initialising cumulative output variables
# These are all needed to compute the cumulative mass balance error
def dynamic_init(self):
""" Initialization of the dynamic part of the inflow module
init inflow before sub step routing
"""
# ************************************************************
# ***** INLETS INIT
# ************************************************************
settings = LisSettings.instance()
option = settings.options
if option['inflow']:
self.var.QDelta = (self.var.QInM3 - self.var.QInM3Old) * self.var.InvNoRoutSteps
# difference between old and new inlet flow per sub step
# in order to calculate the amount of inlet flow in the routing loop
def dynamic(self):
""" dynamic part of the inflow module
"""
settings = LisSettings.instance()
option = settings.options
if option['inflow']:
settings = LisSettings.instance()
QIn = timeinputscalar(str(settings.binding['QInTS']), self.var.InflowPointsMap)
# Get inflow hydrograph at each inflow point [m3/s]
QIn = compressArray(QIn)
QIn[np.isnan(QIn)] = 0
self.var.QInM3 = QIn * self.var.DtSec
# Convert to [m3] per time step
self.var.TotalQInM3 += self.var.QInM3
# Map of total inflow from inflow hydrographs [m3]
def dynamic_inloop(self, NoRoutingExecuted):
""" dynamic part of the inflow routine
inside the sub time step routing routine
"""
if (NoRoutingExecuted < 1):
maskinfo = MaskInfo.instance()
self.var.QinADDEDM3 = maskinfo.in_zero()
# ************************************************************
# ***** INLFLOW **********************************************
# ************************************************************
settings = LisSettings.instance()
option = settings.options
if option['inflow']:
self.var.QInDt = (self.var.QInM3Old + (NoRoutingExecuted + 1) * self.var.QDelta) * self.var.InvNoRoutSteps
# flow from inlets per sub step
self.var.QinADDEDM3 += self.var.QInDt