-
Notifications
You must be signed in to change notification settings - Fork 36
/
SegmentProjector.m
1293 lines (1078 loc) · 35.7 KB
/
SegmentProjector.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 [OUT]=SegmentProjector(DEM,FD,A,S,basin_num,varargin);
%
% Usage:
% [OUT]=SegmentProjector(DEM,FD,A,S,basin_num);
% [OUT]=SegmentProjector(DEM,FD,A,S,basin_num,'name',value,...);
%
% Description:
% Function to interactively select segments of a channel profile you wish to project (e.g. projecting a portion of the profile with a different ksn).
% You can use the 'SegmentPicker' function to interactively choose channels to provide to the StreamProjector function. To do this, load the
% PickedSegments_*.mat file and supply the 'Sc' STREAMobj in place of 'S' when calling Segment Projector. If the STREAMobj has more than one
% channel head, this code will iterate through all channel heads (i.e. make sure you're only providing it stream you want to project, not an entire
% network!). It calculates and will display 95% confidence bounds on this fit.
%
% 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 - Streams you wish to project saved as a 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.
% concavity_method ['ref']- options for concavity
% 'ref' - uses a reference concavity, the user can specify this value with the reference concavity option (see below)
% 'auto' - function finds a best fit concavity for the provided stream
% pick_method ['chi'] - choice of how you want to pick the stream segment to be projected:
% 'chi' - select segments on a chi - z plot
% 'stream' - select segments on a longitudinal profile
% ref_concavity [0.50] - refrence concavity used if 'theta_method' is set to 'auto'
% refit_streams [false] - option to recalculate chi based on the concavity of the picked segment (true), useful if you want to try to precisely
% match the shape of the picked segment of the profile. Only used if 'theta_method' is set to 'auto'
% save_figures [false] - option to save (if set to true) figures at the end of the projection process
% interp_value [0.1] - value (between 0 and 1) used for interpolation parameter in mincosthydrocon (not used if user provides a conditioned DEM)
%
% Output:
% Produces a 2 x n cell array with a column for each stream segment provided (or channel head if a network is provided). The first row is the x-y
% coordinate of the channel head for that stream. The second row is an array containing the following information about the segment of interest:
% x coordinate, y coordinate, distance from mouth, drainage area, chi, concavity, true elevation, projected elevation, upper bound of projected elevation,
% and lower bound of projected elevation. This output is also saved in a mat file called 'ProjectedSegments.mat'.
%
% Examples:
% [OUT]=StreamProjector(DEM,FD,A,S)
% [OUT]=StreamProjector(DEM,FD,A,S,'ref_concavity',0.55);
% [OUT]=StreamProjector(DEM,FD,A,S,'theta_method','auto','pick_method','stream','refit_streams',true);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function Written by Adam M. Forte - Updated : 06/18/18 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parse Inputs
p = inputParser;
p.FunctionName = 'SegmentProjector';
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,'concavity_method','ref',@(x) ischar(validatestring(x,{'ref','auto'})));
addParameter(p,'pick_method','chi',@(x) ischar(validatestring(x,{'chi','stream'})));
addParameter(p,'smooth_distance',1000,@(x) isscalar(x) && isnumeric(x));
addParameter(p,'ref_concavity',0.50,@(x) isscalar(x) && isnumeric(x));
addParameter(p,'refit_streams',false,@(x) isscalar(x) && islogical(x));
addParameter(p,'save_figures',false,@(x) isscalar(x) && islogical(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,'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;
smooth_distance=p.Results.smooth_distance;
theta_method=p.Results.concavity_method;
ref_theta=p.Results.ref_concavity;
refit_streams=p.Results.refit_streams;
pick_method=p.Results.pick_method;
save_figures=p.Results.save_figures;
iv=p.Results.interp_value;
DEMc=p.Results.conditioned_DEM;
out_dir=p.Results.out_dir;
if isempty(out_dir)
out_dir=pwd;
end
% Find channel heads
ST=S;
chix=streampoi(ST,'channelheads','ix');
num_ch=numel(chix);
% Hydrologically condition dem
if isempty(DEMc)
zc=mincosthydrocon(ST,DEM,'interp',iv);
DEMc=GRIDobj(DEM);
DEMc.Z(DEMc.Z==0)=NaN;
DEMc.Z(ST.IXgrid)=zc;
end
% Parse Switches
if strcmp(theta_method,'ref') & strcmp(pick_method,'chi');
method=1;
elseif strcmp(theta_method,'auto') & strcmp(pick_method,'chi');
method=2;
elseif strcmp(theta_method,'ref') & strcmp(pick_method,'stream');
method=3;
elseif strcmp(theta_method,'auto') & strcmp(pick_method,'stream');
method=4;
end
OUT=cell(2,num_ch);
% Set string input
str1='N';
switch method
case 1
% Autocalculate ksn for comparison purposes
[auto_ksn]=KSN_Quick(DEM,A,ST,ref_theta);
for ii=1:num_ch
CHIX=GRIDobj(DEM);
CHIX.Z(chix(ii))=1; CHIX.Z=logical(CHIX.Z);
S=modify(ST,'downstreamto',CHIX);
C=ChiCalc(S,DEMc,A,1,ref_theta);
ak=getnal(S,auto_ksn);
[DAvg,KsnAvg]=BinAverage(S.distance,ak,smooth_distance);
[~,CAvg]=BinAverage(C.distance,C.chi,smooth_distance);
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
ax3=subplot(3,1,3);
hold on
pl1=plotdz(S,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
pl2=plotdz(S,DEMc,'dunit','km','Color','k');
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
legend([pl1 pl2],'Unconditioned DEM','Conditioned DEM','location','best');
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax3);
end
hold off
ax2=subplot(3,1,2);
hold on
scatter(CAvg,KsnAvg,20,'k','filled');
xlabel('Chi')
ylabel('Auto k_{sn}');
title('Chi - Auto k_{sn}');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax2);
end
hold off
ax1=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'-k');
scatter(C.chi,C.elev,10,'k');
xlabel('\chi','Color','r')
ylabel('Elevation (m)','Color','r')
title(['\chi - Z : \theta = ' num2str(C.mn) ' : Select bounds of stream segment you want to project'],'Color','r')
ax1.XColor='Red';
ax1.YColor='Red';
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax1);
end
hold off
linkaxes([ax1,ax2],'x');
while strcmpi(str1,'N')
[cv,e]=ginput(2);
% Sort knickpoint list and construct bounds list
cvs=sortrows(cv);
rc=C.chi;
rx=C.x;
ry=C.y;
re=C.elev;
lb=cvs(1);
rb=cvs(2);
% Clip out stream segment
lb_chidist=sqrt(sum(bsxfun(@minus, rc, lb).^2,2));
rb_chidist=sqrt(sum(bsxfun(@minus, rc, rb).^2,2));
[~,lbix]=min(lb_chidist);
[~,rbix]=min(rb_chidist);
rcC=rc(rbix:lbix);
reC=re(rbix:lbix);
hold on
p1=scatter(ax1,rcC,reC,20,'r','filled');
hold off
qa=questdlg('Is this the stream segment you wanted to project?','Stream Projection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
end
delete(p1);
end
f=fit(rcC,reC,'poly1');
cf=coeffvalues(f);
ci=confint(f);
ksn=cf(1);
eint=cf(2);
ksnl=ci(1,1);
ksnu=ci(2,1);
eintl=ci(1,2);
eintu=ci(2,2);
pred_el=(rc.*ksn)+eint;
pred_el_u=(rc.*ksnl)+eintu;
pred_el_l=(rc.*ksnu)+eintl;
subplot(3,1,1)
hold on
plot(C.chi,pred_el,'-r','LineWidth',2);
plot(C.chi,pred_el_u,'--r');
plot(C.chi,pred_el_l,'--r');
hold off
subplot(3,1,3)
hold on
pl3=plot(C.distance./1000,pred_el,'-r','LineWidth',2);
pl4=plot(C.distance./1000,pred_el_u,'--r');
plot(C.distance./1000,pred_el_l,'--r');
legend([pl1 pl2 pl3 pl4],'Unconditioned DEM','Conditioned DEM','Projected Stream','Uncertainty','location','best');
hold off
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(2,1,1);
hold on
plot([0,max(C.chi)],[0,0],'--k');
scatter(C.chi,pred_el-C.elev,10,'k')
xlabel('\chi');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(2,1,2);
hold on
plot([0,max(C.distance)/1000],[0,0],'--k');
scatter(C.distance./1000,pred_el-C.elev,10,'k')
xlabel('Distance (km)');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
[chx,chy]=ind2coord(DEM,chix(ii));
OUT{1,ii}=[chx chy];
OUT{2,ii}=[C.x C.y C.distance C.area C.chi ones(size(C.chi)).*C.mn C.elev pred_el pred_el_u pred_el_l];
if save_figures
print(f1,'-dpdf',fullfile(out_dir,['ProjectedProfile_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
print(f2,'-dpdf',fullfile(out_dir,['Residual_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
else
if ii<num_ch
uiwait(msgbox('Click OK when ready to continue'))
end
end
if ii<num_ch
close(f1);
close(f2);
end
% Reset string
str1='N';
end
case 2
for ii=1:num_ch
CHIX=GRIDobj(DEM);
CHIX.Z(chix(ii))=1; CHIX.Z=logical(CHIX.Z);
S=modify(ST,'downstreamto',CHIX);
C=ChiCalc(S,DEMc,A,1);
% Autocalculate ksn for comparison purposes
[auto_ksn]=KSN_Quick(DEM,A,S,C.mn);
ak=getnal(S,auto_ksn);
[DAvg,KsnAvg]=BinAverage(S.distance,ak,smooth_distance);
[~,CAvg]=BinAverage(C.distance,C.chi,smooth_distance);
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
ax3=subplot(3,1,3);
hold on
pl1=plotdz(S,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
pl2=plotdz(S,DEMc,'dunit','km','Color','k');
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
legend([pl1 pl2],'Unconditioned DEM','Conditioned DEM','location','best');
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax3);
end
hold off
ax2=subplot(3,1,2);
hold on
scatter(CAvg,KsnAvg,20,'k','filled');
xlabel('\chi')
ylabel('Auto k_{sn}');
title('\chi - Auto k_{sn}');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax2);
end
hold off
ax1=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'-k');
scatter(C.chi,C.elev,10,'k');
xlabel('\chi','Color','r')
ylabel('Elevation (m)','Color','r')
title(['\chi - Z : \theta = ' num2str(C.mn) ' : Select bounds of stream segment you want to project'],'Color','r')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax1);
end
hold off
linkaxes([ax1,ax2],'x');
switch refit_streams
case false
while strcmpi(str1,'N')
[cv,e]=ginput(2);
% Sort knickpoint list and construct bounds list
cvs=sortrows(cv);
rc=C.chi;
rx=C.x;
ry=C.y;
re=C.elev;
lb=cvs(1);
rb=cvs(2);
% Clip out stream segment
lb_chidist=sqrt(sum(bsxfun(@minus, rc, lb).^2,2));
rb_chidist=sqrt(sum(bsxfun(@minus, rc, rb).^2,2));
[~,lbix]=min(lb_chidist);
[~,rbix]=min(rb_chidist);
rcC=rc(rbix:lbix);
reC=re(rbix:lbix);
hold on
p1=scatter(ax1,rcC,reC,20,'r','filled');
hold off
qa=questdlg('Is this the stream segment you wanted to project?','Stream Projection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
end
delete(p1);
end
f=fit(rcC,reC,'poly1');
cf=coeffvalues(f);
ci=confint(f);
ksn=cf(1);
eint=cf(2);
ksnl=ci(1,1);
ksnu=ci(2,1);
eintl=ci(1,2);
eintu=ci(2,2);
pred_el=(rc.*ksn)+eint;
pred_el_u=(rc.*ksnl)+eintu;
pred_el_l=(rc.*ksnu)+eintl;
subplot(3,1,1)
hold on
pl3=plot(C.chi,pred_el,'-r','LineWidth',2);
pl4=plot(C.chi,pred_el_u,'--r');
plot(C.chi,pred_el_l,'--r');
legend([pl1 pl2 pl3 pl4],'Unconditioned DEM','Conditioned DEM','Projected Stream','Uncertainty','location','best');
hold off
subplot(3,1,3)
hold on
plot(C.distance./1000,pred_el,'-r','LineWidth',2);
plot(C.distance./1000,pred_el_u,'--r');
plot(C.distance./1000,pred_el_l,'--r');
hold off
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(2,1,1);
hold on
plot([0,max(C.chi)],[0,0],'--k');
scatter(C.chi,pred_el-C.elev,10,'k')
xlabel('\chi');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(2,1,2);
hold on
plot([0,max(C.distance)/1000],[0,0],'--k');
scatter(C.distance./1000,pred_el-C.elev,10,'k')
xlabel('Distance (km)');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
[chx,chy]=ind2coord(DEM,chix(ii));
OUT{1,ii}=[chx chy];
OUT{2,ii}=[C.x C.y C.distance C.area C.chi ones(size(C.chi)).*C.mn C.elev pred_el pred_el_u pred_el_l];
case true
while strcmpi(str1,'N')
[cv,e]=ginput(2);
% Sort knickpoint list and construct bounds list
cvs=sortrows(cv);
rc0=C.chi;
rx0=C.x;
ry0=C.y;
re0=C.elev;
lb=cvs(1);
rb=cvs(2);
% Clip out stream segment
lb_chidist=sqrt(sum(bsxfun(@minus, rc0, lb).^2,2));
rb_chidist=sqrt(sum(bsxfun(@minus, rc0, rb).^2,2));
[~,lbix]=min(lb_chidist);
[~,rbix]=min(rb_chidist);
% Clip out stream segment
lbx=rx0(lb_chidist==min(lb_chidist));
lby=ry0(lb_chidist==min(lb_chidist));
rbx=rx0(rb_chidist==min(rb_chidist));
rby=ry0(rb_chidist==min(rb_chidist));
lix=coord2ind(DEM,lbx,lby);
LIX=GRIDobj(DEM);
LIX.Z(lix)=1;
[lixmat,X,Y]=GRIDobj2mat(LIX);
lixmat=logical(lixmat);
LIX=GRIDobj(X,Y,lixmat);
rix=coord2ind(DEM,rbx,rby);
RIX=GRIDobj(DEM);
RIX.Z(rix)=1;
[rixmat,X,Y]=GRIDobj2mat(RIX);
rixmat=logical(rixmat);
RIX=GRIDobj(X,Y,rixmat);
Seg=modify(S,'downstreamto',RIX);
Seg=modify(Seg,'upstreamto',LIX);
% Find stream segment concavity
Csegrf=ChiCalc(Seg,DEMc,A,1);
% Recalculate chi over entire stream with new concavity
CN=ChiCalc(S,DEMc,A,1,Csegrf.mn);
rc=CN.chi;
rx=CN.x;
ry=CN.y;
re=CN.elev;
rcC0=rc0(rbix:lbix);
reC0=re0(rbix:lbix);
rcC=rc(rbix:lbix);
reC=re(rbix:lbix);
hold on
p1=scatter(ax1,rcC0,reC0,20,'r','filled');
hold off
qa=questdlg('Is this the stream segment you wanted to project?','Stream Projection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
end
delete(p1);
end
% Autocalculate ksn for comparison purposes
[auto_ksn]=KSN_Quick(DEM,A,S,Csegrf.mn);
ak=getnal(S,auto_ksn);
[DAvg,KsnAvg]=BinAverage(S.distance,ak,smooth_distance);
[~,CAvg]=BinAverage(CN.distance,CN.chi,smooth_distance);
f=fit(rcC,reC,'poly1');
cf=coeffvalues(f);
ci=confint(f);
ksn=cf(1);
eint=cf(2);
ksnl=ci(1,1);
ksnu=ci(2,1);
eintl=ci(1,2);
eintu=ci(2,2);
pred_el=(rc.*ksn)+eint;
pred_el_u=(rc.*ksnl)+eintu;
pred_el_l=(rc.*ksnu)+eintl;
f1=figure(1);
clf; cla;
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
ax1=subplot(3,1,1);
hold on
plot(CN.chi,CN.elev,'-k');
scatter(CN.chi,CN.elev,10,'k');
plot(CN.chi,pred_el,'-r','LineWidth',2);
plot(CN.chi,pred_el_u,'--r');
plot(CN.chi,pred_el_l,'--r');
xlabel('\chi')
ylabel('Elevation (m)')
title(['\chi - Z : \theta = ' num2str(CN.mn)],'Color','r')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax1);
end
hold off
ax2=subplot(3,1,2);
hold on
scatter(CAvg,KsnAvg,20,'k','filled');
xlabel('Chi')
ylabel('Auto k_{sn}');
title('Chi - Auto k_{sn}');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax2);
end
hold off
ax3=subplot(3,1,3);
hold on
pl1=plotdz(S,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
pl2=plotdz(S,DEMc,'dunit','km','Color','k');
pl3=plot(CN.distance./1000,pred_el,'-r','LineWidth',2);
pl4=plot(CN.distance./1000,pred_el_u,'--r');
plot(CN.distance./1000,pred_el_l,'--r');
xlabel('Distance from Mouth (km)')
ylabel('Elevation (m)')
legend([pl1 pl2 pl3 pl4],'Unconditioned DEM','Conditioned DEM','Projected Stream','Uncertainty','location','best');
title('Long Profile')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax3);
end
hold off
linkaxes([ax1,ax2],'x');
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(2,1,1);
hold on
plot([0,max(CN.chi)],[0,0],'--k');
scatter(CN.chi,pred_el-CN.elev,10,'k')
xlabel('\chi');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(2,1,2);
hold on
plot([0,max(CN.distance)/1000],[0,0],'--k');
scatter(CN.distance./1000,pred_el-CN.elev,10,'k')
xlabel('Distance (km)');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
[chx,chy]=ind2coord(DEM,chix(ii));
OUT{1,ii}=[chx chy];
OUT{2,ii}=[CN.x CN.y CN.distance CN.area CN.chi ones(size(CN.chi)).*CN.mn CN.elev pred_el pred_el_u pred_el_l];
end
if save_figures
print(f1,'-dpdf',fullfile(out_dir,['ProjectedProfile_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
print(f2,'-dpdf',fullfile(out_dir,['Residual_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
else
if ii<num_ch
uiwait(msgbox('Click OK when ready to continue'))
end
end
if ii<num_ch
close(f1);
close(f2);
end
% Reset output string 1
str1='N';
end
case 3
% Autocalculate ksn for comparison purposes
[auto_ksn]=KSN_Quick(DEM,A,ST,ref_theta);
for ii=1:num_ch
CHIX=GRIDobj(DEM);
CHIX.Z(chix(ii))=1; CHIX.Z=logical(CHIX.Z);
S=modify(ST,'downstreamto',CHIX);
C=ChiCalc(S,DEMc,A,1,ref_theta);
ak=getnal(S,auto_ksn);
[DAvg,KsnAvg]=BinAverage(S.distance,ak,smooth_distance);
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
ax3=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'-k');
scatter(C.chi,C.elev,10,'k');
xlabel('\chi')
ylabel('Elevation (m)')
title('\chi - Z')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax3);
end
hold off
ax2=subplot(3,1,2);
hold on
scatter(DAvg./1000,KsnAvg,20,'k','filled');
xlabel('Distance (km)')
ylabel('Auto k_{sn}');
title('\chi - Auto k_{sn}');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax2);
end
hold off
ax1=subplot(3,1,3);
hold on
pl1=plotdz(S,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
pl2=plotdz(S,DEMc,'dunit','km','Color','k');
xlabel('Distance from Mouth (km)','Color','r')
ylabel('Elevation (m)','Color','r')
legend([pl1 pl2],'Unconditioned DEM','Conditioned DEM','location','best');
title(['Long Profile : \theta = ' num2str(C.mn) ' : Select bounds of stream segment you want to project'],'Color','r')
ax1.XColor='Red';
ax1.YColor='Red';
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax1);
end
hold off
linkaxes([ax1,ax2],'x');
while strcmpi(str1,'N')
[d,e]=ginput(2);
d=d*1000;
% Sort knickpoint list and construct bounds list
ds=sortrows(d);
rd=C.distance;
rx=C.x;
ry=C.y;
rc=C.chi;
re=C.elev;
lb=ds(1);
rb=ds(2);
lb_dist=sqrt(sum(bsxfun(@minus, rd, lb).^2,2));
rb_dist=sqrt(sum(bsxfun(@minus, rd, rb).^2,2));
[~,lbix]=min(lb_dist);
[~,rbix]=min(rb_dist);
rcC=rc(rbix:lbix);
reC=re(rbix:lbix);
rdC=rd(rbix:lbix);
hold on
p1=scatter(ax1,rdC/1000,reC,20,'r','filled');
hold off
qa=questdlg('Is this the stream segment you wanted to project?','Stream Projection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
end
delete(p1);
end
f=fit(rcC,reC,'poly1');
cf=coeffvalues(f);
ci=confint(f);
ksn=cf(1);
eint=cf(2);
ksnl=ci(1,1);
ksnu=ci(2,1);
eintl=ci(1,2);
eintu=ci(2,2);
pred_el=(rc.*ksn)+eint;
pred_el_u=(rc.*ksnl)+eintu;
pred_el_l=(rc.*ksnu)+eintl;
subplot(3,1,1)
hold on
plot(C.chi,pred_el,'-r','LineWidth',2);
plot(C.chi,pred_el_u,'--r');
plot(C.chi,pred_el_l,'--r');
hold off
subplot(3,1,3)
hold on
pl3=plot(C.distance./1000,pred_el,'-r','LineWidth',2);
pl4=plot(C.distance./1000,pred_el_u,'--r');
plot(C.distance./1000,pred_el_l,'--r');
legend([pl1 pl2 pl3 pl4],'Unconditioned DEM','Conditioned DEM','Projected Stream','Uncertainty','location','best');
hold off
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(2,1,1);
hold on
plot([0,max(C.chi)],[0,0],'--k');
scatter(C.chi,pred_el-C.elev,10,'k')
xlabel('\chi');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(2,1,2);
hold on
plot([0,max(C.distance)/1000],[0,0],'--k');
scatter(C.distance./1000,pred_el-C.elev,10,'k')
xlabel('Distance (km)');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
[chx,chy]=ind2coord(DEM,chix(ii));
OUT{1,ii}=[chx chy];
OUT{2,ii}=[C.x C.y C.distance C.area C.chi ones(size(C.chi)).*C.mn C.elev pred_el pred_el_u pred_el_l];
if save_figures
print(f1,'-dpdf',fullfile(out_dir,['ProjectedProfile_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
print(f2,'-dpdf',fullfile(out_dir,['Residual_' num2str(basin_num) '_' num2str(ii) '.pdf']),'-bestfit');
else
if ii<num_ch
uiwait(msgbox('Click OK when ready to continue'))
end
end
if ii<num_ch
close(f1);
close(f2);
end
% Reset string
str1='N';
end
case 4
for ii=1:num_ch
CHIX=GRIDobj(DEM);
CHIX.Z(chix(ii))=1; CHIX.Z=logical(CHIX.Z);
S=modify(ST,'downstreamto',CHIX);
C=ChiCalc(S,DEMc,A,1);
% Autocalculate ksn for comparison purposes
[auto_ksn]=KSN_Quick(DEM,A,S,C.mn);
ak=getnal(S,auto_ksn);
[DAvg,KsnAvg]=BinAverage(S.distance,ak,smooth_distance);
f1=figure(1);
set(f1,'Units','normalized','Position',[0.05 0.1 0.45 0.8],'renderer','painters');
clf
ax3=subplot(3,1,1);
hold on
plot(C.chi,C.elev,'-k');
scatter(C.chi,C.elev,10,'k');
xlabel('\chi')
ylabel('Elevation (m)')
title('\chi - Z')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax3);
end
hold off
ax2=subplot(3,1,2);
hold on
scatter(DAvg./1000,KsnAvg,20,'k','filled');
xlabel('Distance (km)')
ylabel('Auto k_{sn}');
title('\chi - Auto k_{sn}');
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax2);
end
hold off
ax1=subplot(3,1,3);
hold on
pl1=plotdz(S,DEM,'dunit','km','Color',[0.5 0.5 0.5]);
pl2=plotdz(S,DEMc,'dunit','km','Color','k');
xlabel('Distance from Mouth (km)','Color','r')
ylabel('Elevation (m)','Color','r')
legend([pl1 pl2],'Unconditioned DEM','Conditioned DEM','location','best');
title(['Long Profile : \theta = ' num2str(C.mn) ' : Select bounds of stream segment you want to project'],'Color','r')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(ax1);
end
hold off
linkaxes([ax1,ax2],'x');
switch refit_streams
case false
while strcmpi(str1,'N')
[d,e]=ginput(2);
d=d*1000;
% Sort knickpoint list and construct bounds list
ds=sortrows(d);
rd=C.distance;
rx=C.x;
ry=C.y;
rc=C.chi;
re=C.elev;
lb=ds(1);
rb=ds(2);
lb_dist=sqrt(sum(bsxfun(@minus, rd, lb).^2,2));
rb_dist=sqrt(sum(bsxfun(@minus, rd, rb).^2,2));
[~,lbix]=min(lb_dist);
[~,rbix]=min(rb_dist);
rcC=rc(rbix:lbix);
reC=re(rbix:lbix);
rdC=rd(rbix:lbix);
hold on
p1=scatter(ax1,rdC/1000,reC,20,'r','filled');
hold off
qa=questdlg('Is this the stream segment you wanted to project?','Stream Projection','No','Yes','Yes');
switch qa
case 'Yes'
str1 = 'Y';
case 'No'
str1 = 'N';
end
delete(p1);
end
f=fit(rcC,reC,'poly1');
cf=coeffvalues(f);
ci=confint(f);
ksn=cf(1);
eint=cf(2);
ksnl=ci(1,1);
ksnu=ci(2,1);
eintl=ci(1,2);
eintu=ci(2,2);
pred_el=(rc.*ksn)+eint;
pred_el_u=(rc.*ksnl)+eintu;
pred_el_l=(rc.*ksnu)+eintl;
subplot(3,1,1)
hold on
plot(C.chi,pred_el,'-r','LineWidth',2);
plot(C.chi,pred_el_u,'--r');
plot(C.chi,pred_el_l,'--r');
hold off
subplot(3,1,3)
hold on
pl3=plot(C.distance./1000,pred_el,'-r','LineWidth',2);
pl4=plot(C.distance./1000,pred_el_u,'--r');
plot(C.distance./1000,pred_el_l,'--r');
legend([pl1 pl2 pl3 pl4],'Unconditioned DEM','Conditioned DEM','Projected Stream','Uncertainty','location','best');
hold off
f2=figure(2);
set(f2,'Units','normalized','Position',[0.5 0.1 0.45 0.8],'renderer','painters');
sbplt1=subplot(2,1,1);
hold on
plot([0,max(C.chi)],[0,0],'--k');
scatter(C.chi,pred_el-C.elev,10,'k')
xlabel('\chi');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt1);
end
hold off
sbplt2=subplot(2,1,2);
hold on
plot([0,max(C.distance)/1000],[0,0],'--k');
scatter(C.distance./1000,pred_el-C.elev,10,'k')
xlabel('Distance (km)');
ylabel('Difference between projection and true profile (m)')
if ~verLessThan('matlab','9.5')
disableDefaultInteractivity(sbplt2);
end
hold off
[chx,chy]=ind2coord(DEM,chix(ii));
OUT{1,ii}=[chx chy];
OUT{2,ii}=[C.x C.y C.distance C.area C.chi ones(size(C.chi)).*C.mn C.elev pred_el pred_el_u pred_el_l];
case true
while strcmpi(str1,'N')
[d,e]=ginput(2);
d=d*1000;
% Sort knickpoint list and construct bounds list
ds=sortrows(d);
rd=C.distance;
rx=C.x;
ry=C.y;
rc=C.chi;
re=C.elev;
lb=ds(1);
rb=ds(2);
lb_dist=sqrt(sum(bsxfun(@minus, rd, lb).^2,2));
rb_dist=sqrt(sum(bsxfun(@minus, rd, rb).^2,2));
[~,lbix]=min(lb_dist);
[~,rbix]=min(rb_dist);
lbx=rx(lb_dist==min(lb_dist));