-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
350 lines (299 loc) · 12.8 KB
/
utils.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
344
345
346
347
348
349
350
import numpy as np
from pylab import show, figure, plot
import time
from landlab import RasterModelGrid
from landlab.plot.imshow import imshow_grid
from landlab.io import read_esri_ascii
import matplotlib.pyplot as plt
import os
import geopandas as gpd
from scipy import stats
from osgeo import gdal
import glob
from PIL import Image
import matplotlib.cbook as cbook
import scipy.optimize
from scipy.optimize import curve_fit
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib as mpl
from landlab.components import TaylorNonLinearDiffuser
### TO DO:
# Test evolution of 2 standard deviations of slopes
# DEM lighting, constant limits for hillshade
def plot_evolution_time_linear(n_iter, DEM, shapefiles_input, epsg_code, save_YN, D=0.001):
fig, ax = plt.subplots(
len(n_iter),4,
tight_layout=True,
figsize=(8,10),
dpi=300)
# set overall title
fig.suptitle(str(DEM))
# to save in run
coeff_t = []
years_t = []
line_length = [] # for later plot
# landlab grid from DEM
DEM_name = 'DEMS/' + DEM + '.asc'
mg, z = read_esri_ascii(DEM_name, name='topographic__elevation')
mg.set_closed_boundaries_at_grid_edges(True, True, True, True)
slope_t0 = mg.calc_slope_at_node(z)
slope_t0 = np.array(slope_t0)
z_t0 = z[mg.nodes]
# model set-up for 2D linear diffusion
dt = 0.2 * mg.dx * mg.dx / D # default time step is 50 years
qs = mg.add_zeros('sediment_flux', at='link')
# run linear model over time
plot_counter=0
for p in range(max(n_iter)+1):
if np.any(p == n_iter):
if p*dt<dt:
ValueError("The total time is smaller than the time-step!!")
# plot hillshade
fig.sca(ax[plot_counter,0])
hillshade = mg.calc_hillshade_at_node(elevs=z, alt=30., az=100.)
imshow_grid(mg,hillshade,cmap='gray') # plot_type, 'Hillshade'
ax[plot_counter,0].set_xticklabels([])
ax[plot_counter,0].set_yticklabels([])
ax[plot_counter,0].set_xticks([])
ax[plot_counter,0].set_yticks([])
ax[plot_counter,0].set_ylabel('')
ax[plot_counter,0].set_xlabel('')
colorbar = plt.gci().colorbar
colorbar.remove()
slope_t = mg.calc_slope_at_node(z)
# plot elevation difference between t and t0
zfin = z[mg.nodes]
z_diff = zfin - z_t0
zchange = mg.node_vector_to_raster(z_diff, flip_vertically=True)
im = ax[plot_counter,2].imshow(zchange,cmap='cividis',vmin=-0.8, vmax=0.8)
if plot_counter == 0:
fig.colorbar(im, ax=ax[plot_counter,2],label='$\Delta$ z (m)',orientation='horizontal')
colorbar = plt.gci().colorbar
ax[plot_counter,2].set_yticks([])
ax[plot_counter,2].set_xticks([])
slope_t = np.array(slope_t)
slope_t0 = np.array(slope_t0)
# calculate degradation coefficient
info_loss = estimate_degradation_coefficient(slope_t0,slope_t,plot_counter,ax)
coeff_t.append(info_loss)
ax[plot_counter,0].set_title('t = %.0f years' %(p*dt),fontsize=8)
years_t.append(p*dt)
ax[plot_counter,3].set_xlabel('Slope',fontsize=8)
ax[plot_counter,3].set_ylabel('')
ax[plot_counter,3].set_yticks([])
ax[plot_counter,3].set_yscale('log')
ax[plot_counter,3].set_xlim([0,1])
# plot shapefile
gdf = shapefiles_input[plot_counter]
gdf = gdf.to_crs(epsg=epsg_code)
if gdf.empty:
print("The shapefile is empty.")
print(shapefiles_input[plot_counter])
gdf.plot(ax=ax[plot_counter, 1],linewidth=0.8, color='slategrey')
ax[plot_counter,1].set_ylabel('')
ax[plot_counter,1].set_yticks([])
ax[plot_counter,1].set_xlabel('')
ax[plot_counter,1].set_xticks([])
ax[plot_counter,1].set_aspect('equal')
gdf['length'] = gdf.geometry.length
total_length = gdf.geometry.length.sum()
ax[plot_counter,1].set_title(r"$L$ = {:.2f} m".format(total_length),fontsize=8)
line_length.append(total_length)
plot_counter += 1
g = mg.calc_grad_at_link(z)
qs[mg.active_links] = -D * g[mg.active_links]
dzdt = -mg.calc_flux_div_at_node(qs)
z[mg.core_nodes] += dzdt[mg.core_nodes] * dt
scalebar = ScaleBar(
0.5,
units="m",
dimension="si-length",
label=None,
length_fraction=None,
height_fraction=None,
width_fraction=None,
location=None,
pad=None,
border_pad=None,
sep=None,
frameon=None,
color=None,
box_color=None,
box_alpha=0,
scale_loc=None,
label_loc=None,
font_properties=None,
label_formatter=None,
scale_formatter=None,
fixed_value=None,
fixed_units=None,
animated=False,
rotation=None)
ax[0,0].add_artist(scalebar)
plt.subplots_adjust(left=0.05, right=1, bottom=0.05, top=0.95, wspace=0.3, hspace=0.3)
plt.tight_layout()
if save_YN == 'Yes':
DEMname = str(DEM)
first_char = DEMname[0]
numeric_chars = ''.join(filter(str.isdigit, DEMname))
DEMID = first_char + numeric_chars
txtname = 'Figures/' + DEMID + '_information_loss_analysis_linear.pdf'
plt.savefig(txtname)
return line_length, coeff_t, years_t
def plot_evolution_time_nonlinear(n_iter, DEM, shapefiles_input, epsg_code, save_YN, D=0.001):
fig, ax = plt.subplots(
len(n_iter),4,
tight_layout=True,
figsize=(8,10),
dpi=300)
# set overall title
fig.suptitle(str(DEM))
# make variables to save from this run
coeff_t = []
years_t = []
line_length = [] # for later plot
# landlab grid from DEM
DEM_name = 'DEMS/' + DEM + '.asc'
mg, z = read_esri_ascii(DEM_name, name='topographic__elevation')
mg.set_closed_boundaries_at_grid_edges(True, True, True, True)
slope_t0 = mg.calc_slope_at_node(z)
slope_t0 = np.array(slope_t0)
z_t0 = z[mg.nodes]
# model set-up for 2D non-linear diffusion
dt = 0.2 * mg.dx * mg.dx / D # default time step is 50 years in linear model - using here for plotting reference
cubicflux = TaylorNonLinearDiffuser(mg, linear_diffusivity=D, if_unstable="warn",dynamic_dt=True,nterms=2) #nterms from Ganti et al. (2012)
# run nonlinear model over time
plot_counter=0
for p in range(max(n_iter)+1):
if np.any(p == n_iter):
# plot hillshade
total_time = int(p * dt) # hack to get total time under loop plotting structure we built for the linear case
cubicflux.run_one_step(total_time)
fig.sca(ax[plot_counter,0])
hillshade = mg.calc_hillshade_at_node(elevs=z, alt=30., az=100.)
imshow_grid(mg,hillshade,cmap='gray') # plot_type, 'Hillshade'
ax[plot_counter,0].set_xticklabels([])
ax[plot_counter,0].set_yticklabels([])
ax[plot_counter,0].set_xticks([])
ax[plot_counter,0].set_yticks([])
ax[plot_counter,0].set_ylabel('')
ax[plot_counter,0].set_xlabel('')
colorbar = plt.gci().colorbar
colorbar.remove()
slope_t = mg.calc_slope_at_node(z)
# plot elevation difference between t and t0
zfin = z[mg.nodes]
z_diff = zfin - z_t0
zchange = mg.node_vector_to_raster(z_diff, flip_vertically=True)
im = ax[plot_counter,2].imshow(zchange,cmap='cividis',vmin=-0.8, vmax=0.8)
if plot_counter == 0:
fig.colorbar(im, ax=ax[plot_counter,2],label='$\Delta$ z (m)',orientation='horizontal')
colorbar = plt.gci().colorbar
ax[plot_counter,2].set_yticks([])
ax[plot_counter,2].set_xticks([])
slope_t = np.array(slope_t)
slope_t0 = np.array(slope_t0)
# calculate degradation coefficient
info_loss = estimate_degradation_coefficient(slope_t0,slope_t,plot_counter,ax)
coeff_t.append(info_loss)
ax[plot_counter,0].set_title('t = %.0f years' %(p*dt),fontsize=8)
years_t.append(p*dt)
ax[plot_counter,3].set_xlabel('Slope',fontsize=8)
ax[plot_counter,3].set_ylabel('')
ax[plot_counter,3].set_yticks([])
ax[plot_counter,3].set_yscale('log')
ax[plot_counter,3].set_xlim([0,1])
# plot shapefile
gdf = shapefiles_input[plot_counter]
gdf = gdf.to_crs(epsg=epsg_code)
if gdf.empty:
print("The shapefile is empty.")
print(shapefiles_input[plot_counter])
gdf.plot(ax=ax[plot_counter, 1],linewidth=0.8, color='slategrey')
ax[plot_counter,1].set_ylabel('')
ax[plot_counter,1].set_yticks([])
ax[plot_counter,1].set_xlabel('')
ax[plot_counter,1].set_xticks([])
ax[plot_counter,1].set_aspect('equal')
gdf['length'] = gdf.geometry.length
total_length = gdf.geometry.length.sum()
ax[plot_counter,1].set_title(r"$L$ = {:.2f} m".format(total_length),fontsize=8)
line_length.append(total_length)
plot_counter += 1
scalebar = ScaleBar(
0.5,
units="m",
dimension="si-length",
label=None,
length_fraction=None,
height_fraction=None,
width_fraction=None,
location=None,
pad=None,
border_pad=None,
sep=None,
frameon=None,
color=None,
box_color=None,
box_alpha=0,
scale_loc=None,
label_loc=None,
font_properties=None,
label_formatter=None,
scale_formatter=None,
fixed_value=None,
fixed_units=None,
animated=False,
rotation=None)
ax[0,0].add_artist(scalebar)
plt.subplots_adjust(left=0.05, right=1, bottom=0.05, top=0.95, wspace=0.3, hspace=0.3)
plt.tight_layout()
if save_YN == 'Yes':
DEMname = str(DEM)
first_char = DEMname[0]
numeric_chars = ''.join(filter(str.isdigit, DEMname))
DEMID = first_char + numeric_chars
txtname = 'Figures/' + DEMID + '_information_loss_analysis_nonlinear.pdf'
plt.savefig(txtname)
return line_length, coeff_t, years_t
def estimate_degradation_coefficient(slope_t0,slope_t,plot_counter,ax,nbins=20):
cleaned_slope_t0 = slope_t0[~np.isnan(slope_t0)]
cleaned_slope_t = slope_t[~np.isnan(slope_t)]
percentile_threshold = 68
threshold_t0 = np.percentile(cleaned_slope_t0, percentile_threshold)
threshold_t = np.percentile(cleaned_slope_t, percentile_threshold)
info_loss = threshold_t0/threshold_t
ax[plot_counter,3].hist(cleaned_slope_t0, color='teal', histtype='step', alpha=0.9)
ax[plot_counter,3].hist(cleaned_slope_t, color='darkorange',histtype='step', alpha=0.9)
ax[plot_counter,3].set_title(r"$\phi$ = {:.2f}".format(info_loss),fontsize=8)
return info_loss
def load_shapefiles_for_DEMs(DEM_dir, shapefile_dir):
DEMs_shapefiles = {}
for dem_file in os.listdir(DEM_dir):
dem_name = os.path.splitext(dem_file)[0]
shapefile_subdir = os.path.join(shapefile_dir, dem_name)
if os.path.isdir(shapefile_subdir):
shapefiles_input = os.listdir(shapefile_subdir)
shapefiles = [file for file in shapefiles_input if file.endswith(".shp")]
shapefiles_sorted = sorted(shapefiles, key=lambda x: int(x.split("_")[1].split(".")[0]))
shapefiles_data = []
for shp_file in shapefiles_sorted:
shapefile_path = os.path.join(shapefile_subdir, shp_file)
shapefile = gpd.read_file(shapefile_path)
shapefiles_data.append(shapefile)
DEMs_shapefiles[dem_name] = shapefiles_data
return DEMs_shapefiles
def func_deg_coeff(x,a,c):
return x**a * c
def func_line_length(x,a,b,c):
return b - (c * x) / (x + a)
def normalize_length(row,length_at_time_zero):
return row['Length (m)'] / length_at_time_zero[row['DEM ID']]
def estimate_degradation_coefficient_noplot(slope_t0,slope_t):
cleaned_slope_t0 = slope_t0[~np.isnan(slope_t0)]
cleaned_slope_t = slope_t[~np.isnan(slope_t)]
percentile_threshold = 68
threshold_t0 = np.percentile(cleaned_slope_t0, percentile_threshold)
threshold_t = np.percentile(cleaned_slope_t, percentile_threshold)
info_loss = threshold_t0/threshold_t
return info_loss