forked from amforte/Topographic-Analysis-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentPicker.m
1322 lines (1154 loc) · 40.7 KB
/
SegmentPicker.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [Sc]=SegmentPicker(DEM,FD,A,S,basin_num,varargin)
%
% Usage:
% [Sc]=SegmentPicker(DEM,FD,A,S,basin_num);
% [Sc]=SegmentPicker(DEM,FD,A,S,basin_num,'name',value,...);
%
% Description:
% Function to select a segment of a stream network from the top of the stream, and plot the long profile
% and chi-Z relationship of that segment,also outputs the extraced portion of the stream network and chi structure
% (out of 'chiplot'). Allows user to iteratively select different parts of the stream network and display.
% Keeps running dataset of all the streams you pick and accept.
%
% Required Inputs:
% DEM - Digital Elevation as a GRIDobj, assumes unconditioned DEM (e.g. DEMoc from ProcessRiverBasins)
% FD - Flow direction as FLOWobj
% A - Flow accumulation GRIDobj
% S - Stream network as STREAMobj
% basin_num - basin number from process river basins for output name or other identifying number for the set of streams you will pick
%
% Optional Inputs
% conditioned_DEM [] - option to provide a hydrologically conditioned DEM for use in this function (do not provide a conditoned DEM
% for the main required DEM input!) which will be used for extracting elevations. See 'ConditionDEM' function for options for making a
% hydrological conditioned DEM. If no input is provided the code defaults to using the mincosthydrocon function.
% direction ['down'] - expects either 'up' or 'down', default is 'down', if 'up' assumes individual selections are points above
% which you wish to extract and view stream profiles (i.e. a pour point), if 'down' assumes individual
% selections are channel heads if specific streams you wish to extract and view stream profiles.
% method ['new_picks'] - expects either 'new_picks' or 'prev_picks', default is 'new_picks' if no input is provided. If 'prev_picks' is
% given, the user must also supply an input for the 'picks' input (see below)
% plot_style ['refresh'] - expects either 'refresh' or 'keep', default is 'refresh' if no input is provided. If 'refresh' is given, the plots reset
% after each new stream pick, but if 'keep' is given, all selected streams remain on both the map (as thick red lines) and the
% chi-z/longitudinal profile/slope-area plots.
% plot_type ['vector'] - expects either 'vector' or 'grid', default is 'vector'. Controls whether all streams are drawn as individual lines ('vector') or if
% the stream network is plotted as a grid and downsampled ('grid'). The 'grid' option is much faster with large datasets,
% but can result in inaccurate choices. The 'vector' option is easier to see, but can be very slow to load and interact with.
% calc_full_slope_area [false] - logical flag to either calculate and display the slope area data for just the trunk stream in the network (false, default),
% or to calculate and display slope area data for all streams in the network (true). If direction is set to 'up' and you are choosing large stream
% networks, it is strongly recommended that you leave this parameter set to false to speed code completion.
% complete_networks_only [false] - if true, the code will filter out portions of the stream network that are incomplete prior to choosing
% streams
% picks - expects a m x 3 matrix with columns as x coordinates, y coordinates, and an identifying number OR the name of a point shapefile with a single value column of
% identifying numbers. Will interpret this input as a list of channel heads if 'direction' is 'down' and a list of channel outlets if 'direction' is 'up'.
% ref_concavity [0.50] - reference concavity for calculating Chi-Z, default is 0.50
% min_elev [] - minimum elevation below which the code stops extracting channel information, only used if 'direction'
% is 'down'
% max_area [] - maximum drainage area above which the code stops extracting channel information, only used if 'direction'
% is 'down'
% recalc [false] - only valid if either min_elev or max_area are specified. If recalc is false (default) then extraction of
% streams stops downstream of the condition specified in either min_elev or max_area, but chi is not recalculated
% and distances will remain tied to the original stream (i.e. distances from the outlet will be relative to the outlet
% of the stream if it continued to the edge of the DEM, not where it stops extracting the stream profile). If recalc is true,
% then chi and distance are recalculated (i.e. the outlet as determined by the min_elev or max_area condition
% will have a chi value of zero and a distance from mouth value of zero).
% threshold_area [1e6] - used to redraw downsampled stream network if 'plot_type' is set to 'grid'
% interp_value [0.1] - value (between 0 and 1) used for interpolation parameter in mincosthydrocon (not used if user provides a conditioned DEM)
% bin_size [500] - bin size (in map units) for binning slope area data.
%
% Outputs:
% Sc - STREAMobj containing all the stream segments chosen.
%
% Saves an output called 'PickedSegements_*.mat' with the provided basin number containing these results:
% StreamSgmnts - Cell array of selected stream segments as STREAMobj
% ChiSgmnts - Cell array of selected chi structures
% SlpAreaSgmnts - Cell array of slope area data
% Sc - Single STREAMobj containing all the streams chosen.
% and if 'down' is selected:
% Heads - nx3 matrix of channel heads you picked with x cooord, y coordinate, and pick number as the columns
% and if 'up' is selected:
% Outlets - nx3 matrix of outlets you picked with x cooord, y coordinate, and pick number as the columns (valid input to 'ProcessRiverBasins'
% as 'river_mouths' parameter)
%
% Examples:
% [Sc]=SegmentPicker(DEM,FD,A,S,2);
% [Sc]=SegmentPicker(DEM,FD,A,S,2,'direction','up','theta_ref',0.6,'min_grad',0.00001);
% [Sc]=SegmentPicker(DEM,FD,A,S,32,'direction','down','theta_ref',0.5,'min_elev',300,'recalc',true);
% [Sc]=SegmentPicker(DEM,FD,A,S,1,'method','prev_picks','picks',channel_heads); % If inputing a matrix named channel_heads
% [Sc]=SegmentPicker(DEM,FD,A,S,1,'method','prev_picks','picks','channel_heads'); % If inputing a shapefile named channel_heads.shp
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function Written by Adam M. Forte - Updated : 06/18/18 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parse Inputs
p = inputParser;
p.FunctionName = 'SegmentPicker';
addRequired(p,'DEM',@(x) isa(x,'GRIDobj'));
addRequired(p,'FD',@(x) isa(x,'FLOWobj'));
addRequired(p,'A',@(x) isa(x,'GRIDobj'));
addRequired(p,'S',@(x) isa(x,'STREAMobj'));
addRequired(p,'basin_num',@(x) isnumeric(x));
addParameter(p,'direction','down',@(x) ischar(validatestring(x,{'down','up'})));
addParameter(p,'method','new_picks',@(x) ischar(validatestring(x,{'new_picks','prev_picks'})));
addParameter(p,'plot_type','vector',@(x) ischar(validatestring(x,{'vector','grid'})));
addParameter(p,'plot_style','refresh',@(x) ischar(validatestring(x,{'refresh','keep'})));
addParameter(p,'calc_full_slope_area',false,@(x) isscalar(x) && islogical(x));
addParameter(p,'complete_networks_only',false,@(x) isscalar(x) && islogical(x));
addParameter(p,'ref_concavity',0.50,@(x) isscalar(x) && isnumeric(x));
addParameter(p,'min_elev',[],@(x) isscalar(x) && isnumeric(x) || isempty(x));
addParameter(p,'max_area',[],@(x) isscalar(x) && isnumeric(x) || isempty(x));
addParameter(p,'recalc',false,@(x) isscalar(x));
addParameter(p,'picks',[],@(x) (isnumeric(x) && size(x,2)==3) | ischar(x));
addParameter(p,'threshold_area',1e6,@(x) isnumeric(x));
addParameter(p,'conditioned_DEM',[],@(x) isa(x,'GRIDobj') || isempty(x));
addParameter(p,'interp_value',0.1,@(x) isnumeric(x) && x>=0 && x<=1);
addParameter(p,'bin_size',500,@(x) isscalar(x) && isnumeric(x));
addParameter(p,'out_dir',[],@(x) isdir(x));
parse(p,DEM,FD,A,S,basin_num,varargin{:});
DEM=p.Results.DEM;
FD=p.Results.FD;
S=p.Results.S;
A=p.Results.A;
basin_num=p.Results.basin_num;
cno=p.Results.complete_networks_only;
csa=p.Results.calc_full_slope_area;
direction=p.Results.direction;
method=p.Results.method;
theta_ref=p.Results.ref_concavity;
plot_type=p.Results.plot_type;
plot_style=p.Results.plot_style;
threshold_area=p.Results.threshold_area;
points=p.Results.picks;
iv=p.Results.interp_value;
DEMc=p.Results.conditioned_DEM;
bin_size=p.Results.bin_size;
out_dir=p.Results.out_dir;
% Catch errors
if strcmp(direction,'up') && ~isempty(p.Results.min_elev)
if isdeployed
warndlg('Input for minimum elevation is ignored when picking streams up from a pour point')
else
warning('Input for minimum elevation is ignored when picking streams up from a pour point')
end
elseif strcmp(direction,'up') && ~isempty(p.Results.max_area)
if isdeployed
warndlg('Input for maximum drainage area is ignored when picking streams up from a pour point')
else
warning('Input for maximum drainage area is ignored when picking streams up from a pour point')
end
elseif ~isempty(p.Results.min_elev) && ~isempty(p.Results.max_area)
if isdeployed
errordlg('Cannot specify both a minimum elevation and a maximum area, please provide one input only')
end
error('Cannot specify both a minimum elevation and a maximum area, please provide one input only')
elseif strcmp(method,'prev_picks') && isempty(p.Results.picks)
if isdeployed
errordlg('If you choose the previous picks method you must provide a list of outlets or channel heads')
end
error('If you choose the previous picks method you must provide a list of outlets or channel heads')
end
% Parse different inputs
if strcmp(direction,'down') & strcmp(plot_style,'keep');
plot_switch='down_keep';
elseif strcmp(direction,'down') & strcmp(plot_style,'refresh');
plot_switch='down_ref';
elseif strcmp(direction,'up') & strcmp(plot_style,'keep');
plot_switch='up_keep';
elseif strcmp(direction,'up') & strcmp(plot_style,'refresh');
plot_switch='up_ref';
end
if strcmp(method,'prev_picks') & isempty(points)
if isdeployed
errordlg('Please provide a m x 3 array of points or the valid path to a shapefile of points');
end
error('Please provide a m x 3 array of points or the valid path to a shapefile of points');
elseif strcmp(method,'prev_picks') & ischar(points)
disp('Reading shapefile')
try
pt_name=[points '.shp'];
pt_shp=shaperead(pt_name);
fn=fieldnames(pt_shp);
pt=horzcat([pt_shp.X]',[pt_shp.Y]',[pt_shp.(fn{4})]');
catch
if isdeployed
errordlg('Error reading shapefile, make sure the name provided did NOT include .shp, the shapefile is a point file with a single value column, and you have a license for the Mapping Toolbox')
end
error('Error reading shapefile, make sure the name provided did NOT include .shp, the shapefile is a point file with a single value column, and you have a license for the Mapping Toolbox');
end
elseif strcmp(method,'prev_picks')
pt=points;
end
% Remove edges if flag is thrown
if cno
S=removeedgeeffects(S,FD,DEM);
end
if isempty(out_dir)
out_dir=pwd;
end
% Hydrologically condition dem
if isempty(DEMc)
zc=mincosthydrocon(S,DEM,'interp',iv);
DEMc=GRIDobj(DEM);
DEMc.Z(DEMc.Z==0)=NaN;
DEMc.Z(S.IXgrid)=zc;
end
switch plot_type
case 'grid'
DA=A.*(A.cellsize^2);
DA.Z(DA.Z<threshold_area)=0;
LA=log10(DA);
end
colcol=colorcube(25);
% Strip out greys and whites
colcol=colcol(1:20,:);
switch method
case 'new_picks'
switch plot_switch
% Extract points downstream from a channel head selection
case 'down_keep'
str1='N';
str2='Y';
ii=1;
close all
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
switch plot_type
case 'grid'
hold on
imageschs(DEM,LA,'colormap','parula','colorbar',false);
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
case 'vector'
hold on
imageschs(DEM,DEM,'colormap','parula','colorbar',false);
plot(S,'-w');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
end
while strcmpi(str2,'Y')
while strcmpi(str1,'N')
% Reset short circuit switch
short_circ=0;
figure(f1)
hold on
title('Zoom and pan to area of interest and press "return/enter" when ready to pick')
hold off
pause()
hold on
title('Choose point near channel head of interest')
hold off
[x,y]=ginput(1);
pOI=[x y];
% Find nearest channel head
[ch]=streampoi(S,'channelheads','xy');
distance=sqrt(sum(bsxfun(@minus, ch, pOI).^2,2));
chOI=ch(distance==min(distance),:);
% Build logical raster
ix=coord2ind(DEM,chOI(:,1),chOI(:,2));
IX=GRIDobj(DEM);
IX.Z(ix)=1;
[ixmat,X,Y]=GRIDobj2mat(IX);
ixmat=logical(ixmat);
IX=GRIDobj(X,Y,ixmat);
% Extract stream from channel head to outlet
Sn_t=modify(S,'downstreamto',IX);
% Check if additional constraints have been specified
if ~isempty(p.Results.max_area)
AR=A.*(A.cellsize^2);
ar=getnal(Sn_t,AR);
sx=Sn_t.x; sy=Sn_t.y;
d=Sn_t.distance;
[d_s,d_ix]=sort(d,'ascend');
ar_s=ar(d_ix);
sx=sx(d_ix); sy=sy(d_ix);
ma=p.Results.max_area;
ix2=find(ar_s>=ma,1,'last');
if isempty(ix2)
if isdeployed
warndlg('Input maximum drainage area is too large, extracting full stream')
else
warning('Input maximum drainage area is too large, extracting full stream')
end
Sn=Sn_t;
short_circ=1;
elseif ix2==numel(ar)
if isdeployed
errordlg('Input maximum drainage area is too small, no portion of the stream selected')
end
error('Input maximum drainage area is too small, no portion of the stream selected')
else
xn=sx(ix2);
yn=sy(ix2);
ix2=coord2ind(DEM,xn,yn);
IX2=GRIDobj(DEM);
IX2.Z(ix2)=1;
[ix2mat,X,Y]=GRIDobj2mat(IX2);
ix2mat=logical(ix2mat);
IX2=GRIDobj(X,Y,ix2mat);
Sn=modify(Sn_t,'upstreamto',IX2);
end
elseif ~isempty(p.Results.min_elev);
el=getnal(Sn_t,DEMc);
sx=Sn_t.x; sy=Sn_t.y;
d=Sn_t.distance;
[d_s,d_ix]=sort(d,'ascend');
el_s=el(d_ix);
sx=sx(d_ix); sy=sy(d_ix);
me=p.Results.min_elev;
ix2=find(el_s>=me,1,'first');
if ix2==1
if isdeployed
warndlg('Input minimum elevation is too low, extracting full stream')
else
warning('Input minimum elevation is too low, extracting full stream')
end
Sn=Sn_t;
short_circ=1;
elseif isempty(ix2)
if isdeployed
errordlg('Input minimum elevation is too high, no portion of the stream selected')
end
error('Input minimum elevation is too high, no portion of the stream selected')
else
xn=sx(ix2);
yn=sy(ix2);
ix2=coord2ind(DEM,xn,yn);
IX2=GRIDobj(DEM);
IX2.Z(ix2)=1;
[ix2mat,X,Y]=GRIDobj2mat(IX2);
ix2mat=logical(ix2mat);
IX2=GRIDobj(X,Y,ix2mat);
Sn=modify(Sn_t,'upstreamto',IX2);
end
else
Sn=Sn_t;
end
hold on
SP=plot(Sn,'-r','LineWidth',2);
hold off
qa=questdlg('Is this the stream segment you wanted?','Stream Selection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
delete(SP);
end
end
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif short_circ==1;
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
C_t=chiplot(Sn_t,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
C_n=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
% Find extents
txyz=[C_t.x C_t.y C_t.elev];
nxyz=[C_n.x C_n.y C_n.elev];
ix3=ismember(txyz,nxyz,'rows');
% Remake chi structure
C=struct;
C.mn=C_n.mn;
C.beta=C_n.beta;
C.betase=C_n.betase;
C.a0=C_n.a0;
C.ks=C_n.ks;
C.R2=C_n.R2;
C.chi=C_t.chi(ix3);
C.x=C_t.x(ix3); C.y=C_t.y(ix3);
C.elev=C_t.elev(ix3); C.elevbl=C_t.elevbl(ix3);
C.distance=C_t.distance(ix3); C.pred=C_t.pred(ix3);
C.area=C_t.area(ix3); C.res=C_t.res(ix3);
end
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'Color',colcol(mod(ii,20)+1,:));
xlabel('\chi')
ylabel('Elevation (m)')
title('\chi - Z')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(3,1,2);
hold on
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color',colcol(mod(ii,20)+1,:));
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color',colcol(mod(ii,20)+1,:));
elseif short_circ==1;
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color',colcol(mod(ii,20)+1,:));
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
Cu=chiplot(Sn_t,DEM,A,'a0',1,'mn',theta_ref,'plot',false);
plot((Cu.distance(ix3))./1000,Cu.elev(ix3),'Color',[0.5 0.5 0.5]);
% plot(C.distance./1000,C.elev,'-k');
plot(C.distance./1000,C.elev,'Color',colcol(mod(ii,20)+1,:));
end
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
% legend('Unconditioned DEM','Conditioned DEM','location','best');
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
saax=subplot(3,1,3);
hold on
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif short_circ==1;
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn_t,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn_t),A,bin_size);
end
end
scatter(aa,ag,5,[0.5 0.5 0.5],'+');
scatter(ba,bs,20,colcol(mod(ii,20)+1,:),'filled');
set(saax,'Xscale','log','Yscale','log','XDir','reverse');
xlabel('Log Drainage Area');
ylabel('Log Slope');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(saax);
end
hold off
StreamSgmnts{ii}=Sn;
ChiSgmnts{ii}=C;
SlpAreaSgmnts{ii,1}=[bs ba];
SlpAreaSgmnts{ii,2}=[ag aa];
Heads(ii,1)=chOI(:,1);
Heads(ii,2)=chOI(:,2);
Heads(ii,3)=ii;
ii=ii+1;
qa2=questdlg('Continue picking streams?','Stream Selection','No','Yes','Yes');
switch qa2
case 'Yes'
str2 = 'Y';
str1 = 'N';
case 'No'
str2 = 'N';
end
end
% Extract segements upstream from a pour point selection
case 'up_keep'
str1='N';
str2='Y';
ii=1;
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
switch plot_type
case 'grid'
hold on
imageschs(DEM,LA,'colormap','parula','colorbar',false);
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
case 'vector'
hold on
imageschs(DEM,DEM,'colormap','parula','colorbar',false);
plot(S,'-w');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
end
while strcmpi(str2,'Y')
while strcmpi(str1,'N')
figure(f1)
hold on
title('Zoom and pan to area of interest and press "return/enter" when ready to pick')
hold off
pause()
hold on
title('Choose point above to which calculate Chi-Z')
hold off
[x,y]=ginput(1);
% Build logical raster
[xn,yn]=snap2stream(S,x,y);
ix=coord2ind(DEM,xn,yn);
IX=GRIDobj(DEM);
IX.Z(ix)=1;
[ixmat,X,Y]=GRIDobj2mat(IX);
ixmat=logical(ixmat);
IX=GRIDobj(X,Y,ixmat);
Sn=modify(S,'upstreamto',IX);
hold on
SP=plot(Sn,'-r','LineWidth',2);
hold off
qa=questdlg('Is this the stream segment you wanted?','Stream Selection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
delete(SP);
end
end
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'Color',colcol(mod(ii,20)+1,:));
xlabel('\chi')
ylabel('Elevation (m)')
title('\chi - Z')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(3,1,2);
hold on
plotdz(Sn,DEMc,'dunit','km','Color',colcol(mod(ii,20)+1,:));
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
saax=subplot(3,1,3);
hold on
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
scatter(aa,ag,5,[0.5 0.5 0.5],'+');
scatter(ba,bs,20,colcol(mod(ii,20)+1,:),'filled');
set(saax,'Xscale','log','Yscale','log','XDir','reverse');
xlabel('Log Drainage Area');
ylabel('Log Slope');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(saax);
end
hold off
StreamSgmnts{ii}=Sn;
ChiSgmnts{ii}=C;
SlpAreaSgmnts{ii,1}=[bs ba];
SlpAreaSgmnts{ii,2}=[ag aa];
Outlets(ii,1)=xn;
Outlets(ii,2)=yn;
Outlets(ii,3)=ii;
ii=ii+1;
qa2=questdlg('Continue picking streams?','Stream Selection','No','Yes','Yes');
switch qa2
case 'Yes'
str2 = 'Y';
str1 = 'N';
case 'No'
str2 = 'N';
end
end
case 'down_ref'
str1='N';
str2='Y';
ii=1;
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
switch plot_type
case 'grid'
hold on
imageschs(DEM,LA,'colormap','parula','colorbar',false);
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
case 'vector'
hold on
imageschs(DEM,DEM,'colormap','parula','colorbar',false);
plot(S,'-w');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
end
while strcmpi(str2,'Y')
while strcmpi(str1,'N')
% Reset short circuit switch
short_circ=0;
figure(1)
hold on
title('Zoom and pan to area of interest and press "return/enter" when ready to pick')
hold off
pause()
hold on
title('Choose point near channel head of interest')
hold off
[x,y]=ginput(1);
pOI=[x y];
% Find nearest channel head
[ch]=streampoi(S,'channelheads','xy');
distance=sqrt(sum(bsxfun(@minus, ch, pOI).^2,2));
chOI=ch(distance==min(distance),:);
% Build logical raster
ix=coord2ind(DEM,chOI(:,1),chOI(:,2));
IX=GRIDobj(DEM);
IX.Z(ix)=1;
[ixmat,X,Y]=GRIDobj2mat(IX);
ixmat=logical(ixmat);
IX=GRIDobj(X,Y,ixmat);
% Extract stream from channel head to outlet
Sn_t=modify(S,'downstreamto',IX);
% Check if additional constraints have been specified
if ~isempty(p.Results.max_area)
AR=A.*(A.cellsize^2);
ar=getnal(Sn_t,AR);
sx=Sn_t.x; sy=Sn_t.y;
d=Sn_t.distance;
[d_s,d_ix]=sort(d,'ascend');
ar_s=ar(d_ix);
sx=sx(d_ix); sy=sy(d_ix);
ma=p.Results.max_area;
ix2=find(ar_s>=ma,1,'last');
if isempty(ix2)
if isdeployed
warndlg('Input maximum drainage area is too large, extracting full stream')
else
warning('Input maximum drainage area is too large, extracting full stream')
end
Sn=Sn_t;
short_circ=1;
elseif ix2==numel(ar)
if isdeployed
errordlg('Input maximum drainage area is too small, no portion of the stream selected')
end
error('Input maximum drainage area is too small, no portion of the stream selected')
else
xn=sx(ix2);
yn=sy(ix2);
ix2=coord2ind(DEM,xn,yn);
IX2=GRIDobj(DEM);
IX2.Z(ix2)=1;
[ix2mat,X,Y]=GRIDobj2mat(IX2);
ix2mat=logical(ix2mat);
IX2=GRIDobj(X,Y,ix2mat);
Sn=modify(Sn_t,'upstreamto',IX2);
end
elseif ~isempty(p.Results.min_elev);
el=getnal(Sn_t,DEMc);
sx=Sn_t.x; sy=Sn_t.y;
d=Sn_t.distance;
[d_s,d_ix]=sort(d,'ascend');
el_s=el(d_ix);
sx=sx(d_ix); sy=sy(d_ix);
me=p.Results.min_elev;
ix2=find(el_s>=me,1,'first');
if ix2==1
if isdeployed
warndlg('Input minimum elevation is too low, extracting full stream')
else
warning('Input minimum elevation is too low, extracting full stream')
end
Sn=Sn_t;
short_circ=1;
elseif isempty(ix2)
if isdeployed
errordlg('Input minimum elevation is too high, no portion of the stream selected')
end
error('Input minimum elevation is too high, no portion of the stream selected')
else
xn=sx(ix2);
yn=sy(ix2);
ix2=coord2ind(DEM,xn,yn);
IX2=GRIDobj(DEM);
IX2.Z(ix2)=1;
[ix2mat,X,Y]=GRIDobj2mat(IX2);
ix2mat=logical(ix2mat);
IX2=GRIDobj(X,Y,ix2mat);
Sn=modify(Sn_t,'upstreamto',IX2);
end
else
Sn=Sn_t;
end
hold on
SP=plot(Sn,'-r','LineWidth',2);
hold off
qa=questdlg('Is this the stream segment you wanted?','Stream Selection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
delete(SP);
end
end
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif short_circ==1;
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
C_t=chiplot(Sn_t,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
C_n=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
% Find extents
txyz=[C_t.x C_t.y C_t.elev];
nxyz=[C_n.x C_n.y C_n.elev];
ix3=ismember(txyz,nxyz,'rows');
% Remake chi structure
C=struct;
C.mn=C_n.mn;
C.beta=C_n.beta;
C.betase=C_n.betase;
C.a0=C_n.a0;
C.ks=C_n.ks;
C.R2=C_n.R2;
C.chi=C_t.chi(ix3);
C.x=C_t.x(ix3); C.y=C_t.y(ix3);
C.elev=C_t.elev(ix3); C.elevbl=C_t.elevbl(ix3);
C.distance=C_t.distance(ix3); C.pred=C_t.pred(ix3);
C.area=C_t.area(ix3); C.res=C_t.res(ix3);
end
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'-k');
xlabel('\chi')
ylabel('Elevation (m)')
title('\chi - Z')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(3,1,2);
hold on
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color','k');
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color','k');
elseif short_circ==1;
plotdz(Sn,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
plotdz(Sn,DEMc,'dunit','km','Color','k');
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
Cu=chiplot(Sn_t,DEM,A,'a0',1,'mn',theta_ref,'plot',false);
plot((Cu.distance(ix3))./1000,Cu.elev(ix3),'Color',[0.5 0.5 0.5]);
plot(C.distance./1000,C.elev,'-k');
end
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
legend('Unconditioned DEM','Conditioned DEM','location','best');
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
saax=subplot(3,1,3);
hold on
if isempty(p.Results.min_elev) && isempty(p.Results.max_area)
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif short_circ==1;
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn),A,bin_size);
end
elseif ~isempty(p.Results.min_elev) | ~isempty(p.Results.max_area) && p.Results.recalc==0
if csa
[bs,ba,aa,ag]=sa(DEMc,Sn_t,A,bin_size);
else
[bs,ba,aa,ag]=sa(DEMc,trunk(Sn_t),A,bin_size);
end
end
scatter(aa,ag,5,[0.5 0.5 0.5],'+');
scatter(ba,bs,20,'k','filled');
set(saax,'Xscale','log','Yscale','log','XDir','reverse');
xlabel('Log Drainage Area');
ylabel('Log Slope');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(saax);
end
hold off
StreamSgmnts{ii}=Sn;
ChiSgmnts{ii}=C;
SlpAreaSgmnts{ii,1}=[bs ba];
SlpAreaSgmnts{ii,2}=[ag aa];
Heads(ii,1)=chOI(:,1);
Heads(ii,2)=chOI(:,2);
Heads(ii,3)=ii;
ii=ii+1;
qa2=questdlg('Continue picking streams?','Stream Selection','No','Yes','Yes');
switch qa2
case 'Yes'
str2 = 'Y';
str1 = 'N';
close figure 2
case 'No'
str2 = 'N';
end
end
% Extract segements upstream from a pour point selection
case 'up_ref'
str1='N';
str2='Y';
ii=1;
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
switch plot_type
case 'grid'
hold on
imageschs(DEM,LA,'colormap','parula','colorbar',false);
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
case 'vector'
hold on
imageschs(DEM,DEM,'colormap','parula','colorbar',false);
plot(S,'-w');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(gca);
end
hold off
end
while strcmpi(str2,'Y')
while strcmpi(str1,'N')
figure(1);
hold on
title('Zoom and pan to area of interest and press "return/enter" when ready to pick')
hold off
pause()
hold on
title('Choose point above to which calculate Chi-Z')
hold off
[x,y]=ginput(1);
% Build logical raster
[xn,yn]=snap2stream(S,x,y);
ix=coord2ind(DEM,xn,yn);
IX=GRIDobj(DEM);
IX.Z(ix)=1;
[ixmat,X,Y]=GRIDobj2mat(IX);
ixmat=logical(ixmat);
IX=GRIDobj(X,Y,ixmat);
Sn=modify(S,'upstreamto',IX);
hold on
SP=plot(Sn,'-r','LineWidth',2);
hold off
qa=questdlg('Is this the stream segment you wanted?','Stream Selection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
delete(SP);
end
end
C=chiplot(Sn,DEMc,A,'a0',1,'mn',theta_ref,'plot',false);
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');