forked from avaframe/FlowPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_core.py
343 lines (280 loc) · 15.4 KB
/
flow_core.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 15:15:37 2019
This is the core function for Flow-Py, it handles:
- Sorting release pixels by altitude(get_start_idx)
- Splitting function of the release layer for multiprocessing(split_release)
- Back calculation if infrastructure is hit
- Calculation of run out, etc. (Creating the cell_list and iterating through
the release pixels, erasing release pixels that were hit, stop at the border
of DEM, return arrays)
Copyright (C) <2020> <Michael Neuhauser>
Michael.Neuhauser@bfw.gv.at
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/>.
"""
import sys
import numpy as np
from datetime import datetime
import logging
from flow_class import Cell
def get_start_idx(dem, release):
"""Sort Release Pixels by altitude and return the result as lists for the
Rows and Columns, starting with the highest altitude
Input parameters:
dem Digital Elevation Model to gain information about altitude
release The release layer, release pixels need int value > 0
Output parameters:
row_list Row index of release pixels sorted by altitude
col_list Column index of release pixels sorted by altitude
"""
row_list, col_list = np.where(release > 0) # Gives back the indices of the release areas
if len(row_list) > 0:
altitude_list = []
for i in range(len(row_list)):
altitude_list.append(dem[row_list[i], col_list[i]])
altitude_list, row_list, col_list = list(zip(*sorted(zip(altitude_list, row_list, col_list), reverse=True)))
# Sort this lists by altitude
return row_list, col_list
def back_calculation(back_cell):
"""Here the back calculation from a run out pixel that hits a infrastructure
to the release pixel is performed.
Input parameters:
hit_cell_list All cells that hit a Infrastructure
Output parameters:
Back_list List of pixels that are on the way to the start cell
Maybe change it to array like DEM?
"""
#start = time.time()
#if len(hit_cell_list) > 1:
#hit_cell_list.sort(key=lambda cell: cell.altitude, reverse=False)
#print("{} Elements sorted!".format(len(hit_cell_list)))
back_list = []
for parent in back_cell.parent:
if parent not in back_list:
back_list.append(parent)
for cell in back_list:
for parent in cell.parent:
# Check if parent already in list
if parent not in back_list:
back_list.append(parent)
#end = time.time()
#print('\n Backcalculation needed: ' + str(end - start) + ' seconds')
return back_list
def calculation(optTuple):
"""This is the core function where all the data handling and calculation is
done.
Input parameters:
dem The digital elevation model
header The header of the elevation model
forest The forest layer
process Which process to calculate (Avalanche, Rockfall, SoilSlides)
release The list of release arrays
alpha
exp
flux_threshold
max_z_delta
Output parameters:
z_delta Array like DEM with the max. kinetic Energy Height for every
pixel
flux_array Array with max. concentration factor saved
count_array Array with the number of hits for every pixel
elh_sum Array with the sum of Energy Line Height
back_calc Array with back calculation, still to do!!!
"""
temp_dir = optTuple[8]
dem = np.load(temp_dir + "dem_{}_{}.npy".format(optTuple[0], optTuple[1]))
release = np.load(temp_dir + "init_{}_{}.npy".format(optTuple[0], optTuple[1]))
infra = np.load(temp_dir + "infra_{}_{}.npy".format(optTuple[0], optTuple[1]))
alpha = float(optTuple[2])
exp = float(optTuple[3])
cellsize = float(optTuple[4])
nodata = float(optTuple[5])
flux_threshold = float(optTuple[6])
max_z_delta = float(optTuple[7])
z_delta_array = np.zeros_like(dem, dtype=np.float32)
z_delta_sum = np.zeros_like(dem, dtype=np.float32)
flux_array = np.zeros_like(dem, dtype=np.float32)
count_array = np.zeros_like(dem, dtype=np.int32)
backcalc = np.zeros_like(dem, dtype=np.int32)
fp_travelangle_array = np.zeros_like(dem, dtype=np.float32) # fp = Flow Path
sl_travelangle_array = np.zeros_like(dem, dtype=np.float32) * 90 # sl = Straight Line
back_list = []
# Core
start = datetime.now().replace(microsecond=0)
row_list, col_list = get_start_idx(dem, release)
startcell_idx = 0
while startcell_idx < len(row_list):
sys.stdout.write('\r' "Calculating Startcell: " + str(startcell_idx + 1) + " of " + str(len(row_list)) + " = " + str(
round((startcell_idx + 1) / len(row_list) * 100, 2)) + "%" '\r')
sys.stdout.flush()
cell_list = []
row_idx = row_list[startcell_idx]
col_idx = col_list[startcell_idx]
dem_ng = dem[row_idx - 1:row_idx + 2, col_idx - 1:col_idx + 2] # neighbourhood DEM
if (nodata in dem_ng) or np.size(dem_ng) < 9:
startcell_idx += 1
continue
startcell = Cell(row_idx, col_idx, dem_ng, cellsize, 1, 0, None,
alpha, exp, flux_threshold, max_z_delta, startcell=True)
# If this is a startcell just give a Bool to startcell otherwise the object startcell
cell_list.append(startcell)
for idx, cell in enumerate(cell_list):
row, col, flux, z_delta = cell.calc_distribution()
if len(flux) > 0:
# mass, row, col = list(zip(*sorted(zip( mass, row, col), reverse=False)))
z_delta, flux, row, col = list(zip(*sorted(zip(z_delta, flux, row, col), reverse=False)))
# Sort this lists by elh, to start with the highest cell
for i in range(idx, len(cell_list)): # Check if Cell already exists
k = 0
while k < len(row):
if row[k] == cell_list[i].rowindex and col[k] == cell_list[i].colindex:
cell_list[i].add_os(flux[k])
cell_list[i].add_parent(cell)
if z_delta[k] > cell_list[i].z_delta:
cell_list[i].z_delta = z_delta[k]
row = np.delete(row, k)
col = np.delete(col, k)
flux = np.delete(flux, k)
z_delta = np.delete(z_delta, k)
else:
k += 1
for k in range(len(row)):
dem_ng = dem[row[k] - 1:row[k] + 2, col[k] - 1:col[k] + 2] # neighbourhood DEM
if (nodata in dem_ng) or np.size(dem_ng) < 9:
continue
cell_list.append(
Cell(row[k], col[k], dem_ng, cellsize, flux[k], z_delta[k], cell, alpha, exp, flux_threshold, max_z_delta, startcell))
z_delta_array[cell.rowindex, cell.colindex] = max(z_delta_array[cell.rowindex, cell.colindex], cell.z_delta)
flux_array[cell.rowindex, cell.colindex] = max(flux_array[cell.rowindex, cell.colindex], cell.flux)
count_array[cell.rowindex, cell.colindex] += int(1)
z_delta_sum[cell.rowindex, cell.colindex] += cell.z_delta
fp_travelangle_array[cell.rowindex, cell.colindex] = max(fp_travelangle_array[cell.rowindex, cell.colindex], cell.max_gamma)
sl_travelangle_array[cell.rowindex, cell.colindex] = max(sl_travelangle_array[cell.rowindex, cell.colindex], cell.sl_gamma)
#Backcalculation
if infra[cell.rowindex, cell.colindex] > 0:
#backlist = []
back_list = back_calculation(cell)
for back_cell in back_list:
backcalc[back_cell.rowindex, back_cell.colindex] = max(backcalc[back_cell.rowindex, back_cell.colindex],
infra[cell.rowindex, cell.colindex])
release[z_delta_array > 0] = 0
# Check if i hit a release Cell, if so set it to zero and get again the indexes of release cells
row_list, col_list = get_start_idx(dem, release)
startcell_idx += 1
end = datetime.now().replace(microsecond=0)
# Save Calculated tiles
np.save(temp_dir + "./res_z_delta_{}_{}".format(optTuple[0], optTuple[1]), z_delta_array)
np.save(temp_dir + "./res_z_delta_sum_{}_{}".format(optTuple[0], optTuple[1]), z_delta_sum)
np.save(temp_dir + "./res_flux_{}_{}".format(optTuple[0], optTuple[1]), flux_array)
np.save(temp_dir + "./res_count_{}_{}".format(optTuple[0], optTuple[1]), count_array)
np.save(temp_dir + "./res_fp_{}_{}".format(optTuple[0], optTuple[1]), fp_travelangle_array)
np.save(temp_dir + "./res_sl_{}_{}".format(optTuple[0], optTuple[1]), sl_travelangle_array)
np.save(temp_dir + "./res_backcalc_{}_{}".format(optTuple[0], optTuple[1]), backcalc)
print('\n Time needed: ' + str(end - start))
print("Finished calculation {}_{}".format(optTuple[0], optTuple[1]))
def calculation_effect(optTuple):
"""This is the core function where all the data handling and calculation is
done.
Input parameters:
dem The digital elevation model
release The list of release arrays
Output parameters:
z_delta Array like DEM with the max. Energy Line Height for every
pixel
flux_array Array with max. concentration factor saved
count_array Array with the number of hits for every pixel
z_delta_sum Array with the sum of Energy Line Height
back_calc Array with back calculation, still to do!!!
"""
temp_dir = optTuple[8]
dem = np.load(temp_dir + "dem_{}_{}.npy".format(optTuple[0], optTuple[1]))
release = np.load(temp_dir + "init_{}_{}.npy".format(optTuple[0], optTuple[1]))
alpha = float(optTuple[2])
exp = float(optTuple[3])
cellsize = float(optTuple[4])
nodata = float(optTuple[5])
flux_threshold = float(optTuple[6])
max_z_delta = float(optTuple[7])
z_delta_array = np.zeros_like(dem, dtype=np.float32)
z_delta_sum = np.zeros_like(dem, dtype=np.float32)
flux_array = np.zeros_like(dem, dtype=np.float32)
count_array = np.zeros_like(dem, dtype=np.int32)
#backcalc = np.zeros_like(dem, dtype=np.int32)
fp_travelangle_array = np.zeros_like(dem, dtype=np.float32) # fp = Flow Path
sl_travelangle_array = np.ones_like(dem, dtype=np.float32) * 90 # sl = Straight Line
# Core
start = datetime.now().replace(microsecond=0)
row_list, col_list = get_start_idx(dem, release)
startcell_idx = 0
while startcell_idx < len(row_list):
sys.stdout.write('\r' "Calculating Startcell: " + str(startcell_idx + 1) + " of " + str(len(row_list)) + " = " + str(
round((startcell_idx + 1) / len(row_list) * 100, 2)) + "%" '\r')
sys.stdout.flush()
cell_list = []
row_idx = row_list[startcell_idx]
col_idx = col_list[startcell_idx]
dem_ng = dem[row_idx - 1:row_idx + 2, col_idx - 1:col_idx + 2] # neighbourhood DEM
if (nodata in dem_ng) or np.size(dem_ng) < 9:
startcell_idx += 1
continue
startcell = Cell(row_idx, col_idx, dem_ng, cellsize, 1, 0, None,
alpha, exp, flux_threshold, max_z_delta, True)
# If this is a startcell just give a Bool to startcell otherwise the object startcell
cell_list.append(startcell)
for idx, cell in enumerate(cell_list):
row, col, flux, z_delta = cell.calc_distribution()
if len(flux) > 0:
z_delta, flux, row, col = list(zip(*sorted(zip(z_delta, flux, row, col), reverse=False))) # reverse = True == descending
for i in range(idx, len(cell_list)): # Check if Cell already exists
k = 0
while k < len(row):
if row[k] == cell_list[i].rowindex and col[k] == cell_list[i].colindex:
cell_list[i].add_os(flux[k])
cell_list[i].add_parent(cell)
if z_delta[k] > cell_list[i].z_delta:
cell_list[i].z_delta = z_delta[k]
row = np.delete(row, k)
col = np.delete(col, k)
flux = np.delete(flux, k)
z_delta = np.delete(z_delta, k)
else:
k += 1
for k in range(len(row)):
dem_ng = dem[row[k] - 1:row[k] + 2, col[k] - 1:col[k] + 2] # neighbourhood DEM
if (nodata in dem_ng) or np.size(dem_ng) < 9:
continue
cell_list.append(
Cell(row[k], col[k], dem_ng, cellsize, flux[k], z_delta[k], cell, alpha, exp, flux_threshold, max_z_delta, startcell))
for cell in cell_list:
z_delta_array[cell.rowindex, cell.colindex] = max(z_delta_array[cell.rowindex, cell.colindex], cell.z_delta)
flux_array[cell.rowindex, cell.colindex] = max(flux_array[cell.rowindex, cell.colindex],
cell.flux)
count_array[cell.rowindex, cell.colindex] += 1
z_delta_sum[cell.rowindex, cell.colindex] += cell.z_delta
fp_travelangle_array[cell.rowindex, cell.colindex] = max(fp_travelangle_array[cell.rowindex, cell.colindex],
cell.max_gamma)
sl_travelangle_array[cell.rowindex, cell.colindex] = max(sl_travelangle_array[cell.rowindex, cell.colindex],
cell.sl_gamma)
startcell_idx += 1
# Save Calculated tiles
np.save(temp_dir + "./res_z_delta_{}_{}".format(optTuple[0], optTuple[1]), z_delta_array)
np.save(temp_dir + "./res_z_delta_sum_{}_{}".format(optTuple[0], optTuple[1]), z_delta_sum)
np.save(temp_dir + "./res_flux_{}_{}".format(optTuple[0], optTuple[1]), flux_array)
np.save(temp_dir + "./res_count_{}_{}".format(optTuple[0], optTuple[1]), count_array)
np.save(temp_dir + "./res_fp_{}_{}".format(optTuple[0], optTuple[1]), fp_travelangle_array)
np.save(temp_dir + "./res_sl_{}_{}".format(optTuple[0], optTuple[1]), sl_travelangle_array)
logging.info("finished calculation {}_{}".format(optTuple[0], optTuple[1])) #ToDo!
print("Finished calculation {}_{}".format(optTuple[0], optTuple[1]))
end = datetime.now().replace(microsecond=0)
print('\n Time needed: ' + str(end - start))
#return z_delta_array, flux_array, count_array, z_delta_sum, backcalc, fp_travelangle_array, sl_travelangle_array