Newer
Older
# PlotKPC.py -- Pamhyr
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from functools import reduce
from tools import timer
from View.Tools.PamhyrPlot import PamhyrPlot
from PyQt5.QtCore import (
QCoreApplication
)
_translate = QCoreApplication.translate
class PlotKPC(PamhyrPlot):
def __init__(self, canvas=None, trad=None, toolbar=None,
results=None, reach_id=0, profile_id=0,
parent=None):
super(PlotKPC, self).__init__(
canvas=canvas,
trad=trad,
data=results,
toolbar=toolbar,
parent=parent
self._timestamps = results.get("timestamps")
self._current_timestamp = max(self._timestamps)
self._current_reach_id = reach_id
self._current_profile_id = profile_id
self.label_x = _translate("Results", "KP (m)")
self.label_y = _translate("Results", "Elevation (m)")
self.label_bottom = _translate("Results", "River bottom")
self.label_water = _translate("Results", "Water elevation")
self.label_water_max = _translate("Results", "Max water elevation")
self._isometric_axis = False
@property
def results(self):
return self.data
@results.setter
def results(self, results):
self.data = results
self._current_timestamp = max(results.get("timestamps"))
@timer
def draw(self, highlight=None):
if self.results is None:
return
reach = self.results.river.reach(self._current_reach_id)
self.draw_bottom(reach)
self.draw_water_elevation(reach)
self.draw_water_elevation_max(reach)
self.draw_water_elevation_overflow(reach)
self.draw_current(reach)
# self.enable_legend()
self.idle()
self._init = True
def draw_bottom(self, reach):
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
if reach.has_sediment():
self.draw_bottom_with_bedload(reach)
else:
self.draw_bottom_geometry(reach)
def draw_bottom_with_bedload(self, reach):
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
z_max = reach.geometry.get_z_max()
initial_sl = self.sl_compute_initial(reach)
current_sl = self.sl_compute_current(reach)
max_sl_num = reduce(
lambda acc, sl: max(acc, len(sl)),
current_sl, 0
)
sl_init, sl = self.sl_completed_layers(
initial_sl, current_sl, max_sl_num
)
z_sl = self.sl_apply_z_min_to_initial(sl_init, z_min)
d_sl = self.sl_compute_diff(sl_init, sl)
final_z_sl = self.sl_apply_diff(reach, z_sl, d_sl)
final_z_sl = list(reversed(final_z_sl))
self.line_kp_sl = []
for i, z in enumerate(final_z_sl):
self.line_kp_sl.append(None)
self.line_kp_sl[i], = self.canvas.axes.plot(
kp, z,
linestyle="solid" if i == len(final_z_sl) - 1 else "--",
lw=1.,
color=(
self.color_plot_river_bottom if i == len(final_z_sl) - 1
else self.colors[i]
)
)
self._initial_sl = initial_sl
self._river_bottom = final_z_sl[-1]
def sl_compute_initial(self, reach):
"""
Get SL list for profile p at initial time (initial data)
"""
return list(
map(
lambda p: p.get_ts_key(min(self._timestamps), "sl")[0],
reach.profiles
)
)
def sl_compute_current(self, reach):
"""
Get SL list for profile p at current time
"""
return list(
map(
lambda p: p.get_ts_key(self._current_timestamp, "sl")[0],
reach.profiles
)
)
def sl_completed_layers(self, initial_sl, current_sl, max_sl_num):
sl = []
sl_init = []
for i in range(max_sl_num):
cur = []
cur_init = []
for current, initial in zip(current_sl, initial_sl):
if i < len(initial_sl):
cur.append(current[i][0])
cur_init.append(initial[i][0])
else:
cur.append(0)
cur_init.append(0)
sl.append(cur)
sl_init.append(cur_init)
return sl_init, sl
def sl_apply_z_min_to_initial(self, sl_init, z_min):
"""
Compute sediment layer from initial data in function to profile
z_min
"""
return reduce(
lambda acc, v: acc + [
list(
map(
lambda x, y: y - x,
v, acc[-1]
)
)
],
sl_init, [z_min]
)
def sl_compute_diff(self, sl_init, sl):
return list(
map(
lambda ln0, lni: list(
map(
lambda z0, zi: z0 - zi,
ln0, lni
)
),
sl_init, sl
)
)
def sl_apply_diff(self, reach, z_sl, d_sl):
f = list(map(lambda p: 0, reach.profiles))
return list(
map(
lambda z, d: list(
map(
lambda zn, dn: zn - dn,
z, d
)
),
z_sl, d_sl + [f] # HACK: Add dummy data for last layer
)
)
def draw_bottom_geometry(self, reach):
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
self.line_kp_zmin = self.canvas.axes.plot(
kp, z_min,
color=self.color_plot_river_bottom,
lw=1.
self._river_bottom = z_min
if len(reach.geometry.profiles) != 0:
kp = reach.geometry.get_kp()
water_z = list(
map(
lambda p: p.get_ts_key(
self._current_timestamp, "Z"
),
reach.profiles
)
)
self.water = self.canvas.axes.plot(
kp, water_z,
lw=1., color=self.color_plot_river_water,
self.water_fill = self.canvas.axes.fill_between(
kp, self._river_bottom, water_z,
color=self.color_plot_river_water_zone,
alpha=0.7,
interpolate=True
)
def draw_water_elevation_max(self, reach):
if len(reach.geometry.profiles) != 0:
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
water_z = list(
map(
lambda p: max(p.get_key("Z")),
reach.profiles
)
)
self.canvas.axes.plot(
kp, water_z, lw=1.,
color=self.color_plot_river_water,
linestyle='dotted',
)
def draw_current(self, reach):
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
z_max = reach.geometry.get_z_max()
[
kp[self._current_profile_id],
kp[self._current_profile_id]
],
[
z_max[self._current_profile_id],
z_min[self._current_profile_id]
],
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
def draw_water_elevation_overflow(self, reach):
overflow = []
for profile in reach.profiles:
z_max = max(profile.get_key("Z"))
z_max_ts = 0
for ts in self._timestamps:
z = profile.get_ts_key(ts, "Z")
if z == z_max:
z_max_ts = ts
break
pt_left, pt_right = profile.get_ts_key(z_max_ts, "water_limits")
if self.is_overflow_point(profile, pt_left):
overflow.append((profile, z_max))
elif self.is_overflow_point(profile, pt_right):
overflow.append((profile, z_max))
for profile, z in overflow:
self.canvas.axes.plot(
profile.kp, z,
lw=1.,
color=self.color_plot,
markersize=3,
marker='x'
)
def is_overflow_point(self, profile, point):
left_limit = profile.geometry.point(0)
right_limit = profile.geometry.point(
profile.geometry.number_points - 1
)
return (
point == left_limit
or point == right_limit
)
def set_reach(self, reach_id):
self._current_reach_id = reach_id
self._current_profile_id = 0
def set_profile(self, profile_id):
self._current_profile_id = profile_id
def set_timestamp(self, timestamp):
self._current_timestamp = timestamp
def update(self):
self.update_bottom_with_bedload()
self.update_water_elevation()
self.update_idle()
def update_water_elevation(self):
reach = self.results.river.reach(self._current_reach_id)
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
water_z = list(
map(
lambda p: p.get_ts_key(
self._current_timestamp, "Z"
),
reach.profiles
)
)
self.water[0].set_data(
kp, water_z
)
self.water_fill.remove()
self.water_fill = self.canvas.axes.fill_between(
kp, self._river_bottom, water_z,
color=self.color_plot_river_water_zone,
alpha=0.7, interpolate=True
)
reach = self.results.river.reach(self._current_reach_id)
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
z_max = reach.geometry.get_z_max()
cid = self._current_profile_id
[kp[cid], kp[cid]],
[z_max[cid], z_min[cid]]
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
def update_bottom_with_bedload(self):
reach = self.results.river.reach(self._current_reach_id)
kp = reach.geometry.get_kp()
z_min = reach.geometry.get_z_min()
initial_sl = self._initial_sl
current_sl = self.sl_compute_current(reach)
max_sl_num = reduce(
lambda acc, sl: max(acc, len(sl)),
current_sl, 0
)
sl_init, sl = self.sl_completed_layers(
initial_sl, current_sl, max_sl_num
)
z_sl = self.sl_apply_z_min_to_initial(sl_init, z_min)
d_sl = self.sl_compute_diff(sl_init, sl)
final_z_sl = self.sl_apply_diff(reach, z_sl, d_sl)
final_z_sl = list(reversed(final_z_sl))
for i, z in enumerate(final_z_sl):
self.line_kp_sl[i].set_data(kp, z)
self._river_bottom = final_z_sl[-1]