-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHypsometry_of_all_basins_js.m
85 lines (72 loc) · 2.61 KB
/
Hypsometry_of_all_basins_js.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
%% this script computes the hypsometry, area and mask for all the basins in the DEM 'hs'
Filled = fillsinks(hs);
P_all = Filled-hs;
waterVolume = hs - hs_original;
if ~any(P_all.Z(:)>0)
disp("DEM completely filled")
DEMfilled = 1;
return
end
FD = FLOWobj(hs,'preprocess','none','internaldrainage',false); % flow directions
% FD = FLOWobj(hs,'preprocess','none','verbose',true); % flow directions
A = flowacc(FD).*(FD.cellsize^2); % flow accumulation
S = STREAMobj(FD,A>2e5); % stream locations(places where flow accumulation is larger than some threshold
DB = drainagebasins(FD);
BasinNumbers = unique(DB.Z(hs.Z>0));
b = [];
%% hypsometry for each basin
for kk = 2:length(BasinNumbers)
b(kk).BasinNumber = BasinNumbers(kk);
Mask = DB == BasinNumbers(kk);
BasinArea = sum(Mask.Z,'all')*cellArea;
b(kk).BasinArea = BasinArea; % basin area in m^2
b(kk).MaskLogical = Mask;
b(kk).MaskI = find(Mask.Z(:)); % mask for the basin
%b(kk).meanElev = mean(DEMr.Z(b(kk).MaskI));
depths = P_all.Z(b(kk).MaskI);
water_volume = hs.Z(b(kk).MaskI) - hs_original.Z(b(kk).MaskI);
depths = depths(~isnan(depths) & depths~=0);
if ~any(depths) %added it to prevent loop with tinier and tinier depressions
b(kk).skip = 1;
b(kk).h = nan;
b(kk).maxdepth = nan;
b(kk).Volume = nan;
b(kk).p = nan;
continue
else
b(kk).skip = 0;
end
b(kk).Volume = sum(depths)*cellArea;
b(kk).WaterVolume = nansum(water_volume)*cellArea;
heights = max(depths) - depths;
heights_sorted = sort(heights);
I = find(diff(heights_sorted)==0);
heights_sorted(I+1) = heights_sorted(I+1) +0.0001; % nudge the similar values up a tiny amount to avoid issues with the interpolation
b(kk).hw = heights_sorted; % heights for hypsometry
b(kk).maxdepth = max(depths); % not actually the same as max(Heights) the smallest value of depths is not equal to zero
olderbasin = unique(DB_old.Z(Mask.Z));
h=[];
for f=1:length(unique(DB_old.Z(Mask.Z)))
h = cat(1,h,b_old(olderbasin(1)+1).h);
end
b(kk).h = mean(h);%0; % initial water depth is zero
b(kk).VAratio = b(kk).Volume/b(kk).BasinArea;
end
if plotWhenReComputeBasins
figure(776+summer_count)
DB.imagesc
%water = hs-hs_original;
% imagesc(water)
% colormap cool%flowcolor
% colorbar
% caxis([0 3])
%if ~isempty(S.x)
%[x_s,y_s] = STREAMobj2XY(S);
% hold on
%plot(x_s,y_s,'r','LineWidth',0.01)
%end
hold off
drawnow
title({'Summer ' ,num2str(summer_count)});
% pause
end