-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCliffDelineaTool.m
408 lines (362 loc) · 19.2 KB
/
CliffDelineaTool.m
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
function [] = CliffDelineaTool(myFolder,delimiterIn,headerlinesIn,c1,c2,c3,c4,c5,c6,NVert,BaseMaxElev,BaseSea,BaseLand,TopSea,TopLand,PropConvex,SmoothWindow)
% CliffDelineaTool v1.2.0
% https://github.com/zswirad/CliffDelineaTool
% Zuzanna M Swirad ([email protected]), Scripps Institution of Oceanography, UC San Diego
% Last updated on 2021-11-24 (MATLAB R2019a)
% % Indicate input data parameters:
% myFolder = 'C:\data';
% delimiterIn = ',';
% headerlinesIn = 1;
% c1 = 1; % Column containing unique point ID
% c2 = 2; % Column containing unique transect ID
% c3 = 4; % Column containing distance from the seaward end (m)
% c4 = 3; % Column containing elevation (m)
% c5 = 0; % Column containing X coordinate (type '0' if there is none; it does not impact the algorithm)
% c6 = 0; % Column containing Y coordinate (type '0' if there is none; it does not impact the algorithm)
%
% % Set the calibrated input variables:
% NVert = 20; % How many adjacent points to consider as a local scale?
% BaseMaxElev = 5; % What is the top limit for cliff base elevation (m)?
% BaseSea = 15; % What is the max seaward slope at the cliff base (deg)?
% BaseLand = 25; % What is the min landward slope at the cliff base (deg)?
% TopSea = 20; % What is the min seaward slope at the cliff top (deg)?
% TopLand = 15; % What is the max landward slope at the cliff top (deg)?
% PropConvex = 0.5; % What is the minimal proportion of the distance from trendline #2 to replace modelled cliff top location in case of composed cliff profile (0-1)?
% SmoothWindow = 10; % What is the alongshore moving window for cross-shore cliff top smoothing (points)?
filePattern = fullfile(myFolder, '*.txt');
theFiles = dir(filePattern);
for k = 1:size(theFiles,1)
clear sub
clear data
clear modelled_base
clear modelled_top
clear table
baseFileName = theFiles(k).name;
display(baseFileName)
fullFileName = fullfile(myFolder, baseFileName);
base = baseFileName([1:end-4]);
data = importdata(fullFileName,delimiterIn,headerlinesIn);
data=data.data;
dataTemp = data(:,c1);
dataTemp(:,2) = data(:,c2);
dataTemp(:,3) = data(:,c3);
dataTemp(:,4) = data(:,c4);
dataTemp(dataTemp<-50) = NaN;
data = dataTemp;
clear dataTemp
% Add information about local slopes and deviation from a straight line:
countRows=0;
for n = min(data(:,2)):max(data(:,2))
sub = data(data(:,2)==n,:);
if size(sub,1) > 0
sub = (sortrows(sub,3));
elev = sub(:,4);
% Fill data gaps:
% 1. Extrapolate elevations at transect peripheries:
valueFirst = find(elev>-50,1,'first');
valueLast = find(elev>-50,1,'last');
sub(1:valueFirst,4) = sub(valueFirst,4);
sub(valueLast:end,4) = sub(valueLast,4);
% 2. Interpolate elevations:
if size(valueFirst) > 0
sum_nan = sum(isnan(elev(valueFirst:end)));
if sum_nan ~= 0
for m = valueFirst:size(sub,1)
if isnan(sub(m,4))
for p = (m+1):size(sub,1)
if sub(p,4) > -500
break;
end
end
dist = (sub(m,3)-sub(m-1,3))/(sub(p,3)-sub(m-1,3));
sub(m,4) = sub(m-1,4)+dist*(sub(p,4)-sub(m-1,4));
end
end
end
end
% Calculate local slopes:
sub(:,5:8) = zeros;
for z = (NVert+1):(size(sub,1)-NVert)
count = 0;
for s = 1:NVert
if sub(z,4) ~= sub(z-s,4)
angle = radtodeg(atan((sub(z,4) - sub(z-s,4))/(sub(z,3) - sub(z-s,3))));
if angle < 0
angle = 0;
end
sub(z,5) = sub(z,5) + angle;
count = count + 1;
end
end
% Seaward slope = average slope between the point and NVert consecutive seaward points:
sub(z,5) = sub(z,5)/count;
count = 0;
for s = 1:NVert
if sub(z,4) ~= sub(z+s,4)
angle = radtodeg(atan((sub(z+s,4) - sub(z,4))/(sub(z+s,3) - sub(z,3))));
if angle < 0
angle = 0;
end
sub(z,6) = sub(z,6) + angle;
count = count + 1;
end
end
% Landward slope = average slope between the point and NVert consecutive landward points:
sub(z,6) = sub(z,6)/count;
end
% Limit the transect landwards to the higest point + NVert:
[maxElev, indMax] = max(sub(:,4));
if size(sub,1) > indMax + NVert
sub((indMax+NVert+1):end,:) = [];
end
% Draw trendline #1 (straight line between the seaward and landward transect ends):
sub(1,7) = sub(1,4);
sub(end,7) = sub(end,4);
for z = 2:(size(sub,1)-1)
sub(z,7) = (sub(z,3)-sub(1,3))*(sub(end,4)-sub(1,4))/(sub(end,3)-sub(1,3))+sub(1,4);
end
% Calculate vertical distance between actual elevations and trendline #1:
sub(:,8) = sub(:,4)-sub(:,7);
table((countRows+1):(countRows+size(sub,1)),:) = sub;
countRows = size(table,1);
end
end
% Find potential cliff base locations:
base_count = 0;
potential_base = zeros(0,8);
for n = 1:size(table,1)
if table(n,4) < BaseMaxElev && table(n,5) < BaseSea && table(n,6) > BaseLand && table(n,8) < 0
potential_base(base_count+1,:) = table(n,:);
base_count = base_count+1;
end
end
% From the points that satisfy the criteria, for each transect select
% one with the largest vertical difference between the elevation and trendline #1:
modelled_base = zeros(0,8);
if size(potential_base,1) > 0
count = 0;
modelled_base = zeros(0,8);
cliffed_profiles = unique(potential_base(:,2));
for n = min(potential_base(:,2)):max(potential_base(:,2))
for m = 1:size(cliffed_profiles)
if n == cliffed_profiles(m)
sub = potential_base(potential_base(:,2)==n,:);
sub = sortrows(sub,8);
modelled_base(count+1,:) = sub(1,:);
count = count+1;
end
end
end
end
% Find cliff top locations for transects with cliff base:
if size(modelled_base,1) > 0
count = 0;
modelled_top = zeros(0,10);
for n = min(modelled_base(:,2)):max(modelled_base(:,2))
for c = 1:size(cliffed_profiles)
if n == cliffed_profiles(c)
sub = table(table(:,2)==n,:);
% Remove points seawards from the cliff base:
for m = 1:size(modelled_base,1)
if modelled_base(m,2) == n
sub_base = modelled_base(m,:);
end
end
sub = sortrows(sub,3);
sub(sub(:,3) < sub_base(3),:)=[];
% Draw trendline #2 between cliff base and landward transect end:
sub(:,9) = 0;
sub(1,9) = sub(1,4);
sub(end,9) = sub(end,4);
for z = 2:(size(sub,1)-1)
sub(z,9) = (sub(z,3)-sub(1,3))*(sub(end,4)-sub(1,4))/(sub(end,3)-sub(1,3))+sub(1,4);
end
sub(:,10) = sub(:,4)-sub(:,9);
% Find potential cliff top locations:
top_count = 0;
potential_top = zeros(0,10);
for m = 1:size(sub,1)
if sub(m,5) > TopSea && sub(m,6) < TopLand && sub(m,10) > 0
potential_top(top_count+1,:) = sub(m,:);
top_count = top_count+1;
end
end
if size(potential_top,1) > 0
potential_top = sortrows(potential_top,10);
% From the points that satisfy the criteria, for each transect select
% one with the largest vertical difference between the elevation and trendline #2:
modelled_top0 = potential_top(end,:);
% Check whether the selected point is part of within-cliff flattening:
if max(potential_top(:,3)) > potential_top(end,3) + NVert
subNew = sub;
subNew(subNew(:,3) < potential_top(end,3),:) = [];
subNew(:,11:12) = zeros;
subNew(1,11) = subNew(1,4);
subNew(end,11) = subNew(end,4);
for z = 2:(size(subNew,1)-1)
subNew(z,11) = (subNew(z,3)-subNew(1,3))*(subNew(end,4)-subNew(1,4))/(subNew(end,3)-subNew(1,3))+subNew(1,4);
end
subNew(:,12) = subNew(:,4) - subNew(:,11);
potential_top2 = potential_top;
potential_top2(potential_top2(:,3) < potential_top(end,3),:) = [];
potential_top2(:,11) = zeros;
for pp = 1:size(subNew,1)
for p = 1:size(potential_top2,1)
if potential_top2(p,3) == subNew(pp,3)
potential_top2(p,11) = subNew(pp,12);
end
end
end
potential_top2 = potential_top2(potential_top2(:,11)>0,:);
potential_top2(potential_top2(:,10) < (modelled_top0(10)*PropConvex),:) = [];
potential_top2(potential_top2(:,3) < (modelled_top0(3) + NVert),:) = [];
if size(potential_top2,1) > 0
potential_top2 = sortrows(potential_top2,10);
modelled_top0 = potential_top2(end,1:10);
end
end
modelled_top(count+1,:) = modelled_top0;
count = count+1;
end
end
end
end
% Remove alongshore outliers:
% 1. Find outliers:
modelled_base = sortrows(modelled_base,2);
modelled_top = sortrows(modelled_top,2);
modelled_top(:,11:15) = zeros;
modelled_top(:,11) = smoothdata(modelled_top(:,3),'movmedian',SmoothWindow);
modelled_top(:,12) = modelled_top(:,3) - modelled_top(:,11); % Calculate residuals
MSE = mean((modelled_top(:,12)).^2);
modelled_top(:,13) = leverage(modelled_top(:,11));
modelled_top(:,14) = modelled_top(:,12)./(sqrt(MSE*(1-modelled_top(:,13)))); % Calculate standardized residuals
for n = 1:size(modelled_top,1)
if abs(modelled_top(n,14)) > 2 % Flag outliers (https://online.stat.psu.edu/stat462/node/172/; accessed on 2021/06/04)
modelled_top(n,15) = 1;
end
end
fix = modelled_top(modelled_top(:,15)==1,:);
% 2. Delete or replace outliers with more suitable potential cliff tops:
% (Repeat cliff top detection for the transects with outliers.)
if size(fix,1) > 0
good_count = 0;
for n = min(modelled_base(:,2)):max(modelled_base(:,2))
for c = 1:size(fix,1)
outlier = modelled_top(modelled_top(:,2)==fix(c,2),:);
if n == fix(c,2)
sub = table(table(:,2)==n,:);
% Remove points seawards from the cliff base:
for m = 1:size(modelled_base,1)
if modelled_base(m,2) == n
sub_base = modelled_base(m,:);
end
end
sub = sortrows(sub,3);
sub(sub(:,3) < sub_base(3),:) = [];
% Draw trendline #2 between cliff base and landward transect end:
sub(:,9) = 0;
sub(1,9) = sub(1,4);
sub(end,9) = sub(end,4);
for z = 2:(size(sub,1)-1)
sub(z,9) = (sub(z,3)-sub(1,3))*(sub(end,4)-sub(1,4))/(sub(end,3)-sub(1,3))+sub(1,4);
end
sub(:,10) = sub(:,4)-sub(:,9);
% Find potential cliff top locations:
top_count = 0;
potential_top2 = zeros(0,10);
for m = 1:size(sub,1)
if sub(m,5) > TopSea && sub(m,6) < TopLand && sub(m,10) > 0
potential_top2(top_count+1,:) = sub(m,:);
top_count = top_count+1;
end
end
potential_top2(:,11) = abs(potential_top2(:,3)-fix(c,11));
potential_top2 = sortrows(potential_top2,11);
potential_top2 = potential_top2(1,:);
standardResid = potential_top2(11)/(sqrt(MSE*(1-outlier(:,13))));
if abs(standardResid) <= 2
good(good_count+1,:) = potential_top2;
good_count = good_count+1;
end
end
end
end
if good_count > 0
outliersDelete = setdiff(fix(:,2), good(:,2));
modelled_top(ismember(modelled_top(:,2),outliersDelete),:) = [];
for n = 1:size(modelled_top,1)
for c = 1:size(good,1)
if modelled_top(n,2) == good(c,2)
modelled_top(n,1:10) = good(c,1:10);
modelled_top(n,15) = 2;
end
end
end
else
modelled_top(ismember(modelled_top(:,2),fix(:,2)),:) = [];
end
end
% Save the cliff base data:
% 1. Remove redundant properties (all but point and transect IDs):
modelled_base_save = modelled_base(:,1:2);
% 2. Add the coordinates if present:
if c5 ~= 0 & c6 ~= 0
modelled_base_save(:,3:4) = zeros;
for m = 1:size(data,1)
for n = 1 : size(modelled_base_save,1)
if data(m,1) == modelled_base_save(n,1)
modelled_base_save(n,3) = data(m,c5);
modelled_base_save(n,4) = data(m,c6);
end
end
end
end
savename1 = [fullFileName(1:(end-4)) '_base.txt'];
dlmwrite(savename1, modelled_base_save, 'delimiter', ',', 'precision', 8)
% % 3. Addtionally save cliff base with header:
% fidin = fopen(savename1, 'rt');
% savename1header = [fullFileName(1:(end-4)) '_base_header.txt'];
% fidout = fopen(savename1header, 'wt');
% fprintf(fidout, '%s\n', 'PointID,TransectID'); % Add column names with comma separator if saving with XY coordinates
% while true
% thisline = fgets(fidin);
% if ~ischar(thisline); break; end
% fwrite(fidout, thisline);
% end
% fclose(fidout);
% fclose(fidin);
% Save the cliff top data (if present):
if size(modelled_top,1) > 0
% 1. Remove redundant properties (all but point and transect IDs):
modelled_top_save = modelled_top(:,1:2);
% 2. Add the coordinates if present:
if c5 ~= 0 & c6 ~= 0
modelled_top_save(:,3:4) = zeros;
for m = 1:size(data,1)
for n = 1 : size(modelled_top_save,1)
if data(m,1) == modelled_top_save(n,1)
modelled_top_save(n,3) = data(m,c5);
modelled_top_save(n,4) = data(m,c6);
end
end
end
end
savename2 = [fullFileName(1:(end-4)) '_top.txt'];
dlmwrite(savename2, modelled_top_save, 'delimiter', ',', 'precision', 8)
% % 3. Additionally save cliff top with header:
% fidin = fopen(savename2, 'rt');
% savename2header = [fullFileName(1:(end-4)) '_top_header.txt'];
% fidout = fopen(savename2header, 'wt');
% fprintf(fidout, '%s\n', 'PointID,TransectID'); % Add column names with comma separator if saving with XY coordinates
% while true
% thisline = fgets(fidin);
% if ~ischar(thisline); break; end
% fwrite(fidout, thisline);
% end
% fclose(fidout);
% fclose(fidin);
end
end
end
return