-
Notifications
You must be signed in to change notification settings - Fork 8
/
CalculateChi.m
48 lines (40 loc) · 1.85 KB
/
CalculateChi.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
function chi = CalculateChi(x,y,rec_array,area_array,m)
%%%%%%%%%%% LIRAN GOREN, [email protected], 07/11/2019 %%%%%%%%%%%%%%%%
% Function to calculate the chi for river pixels of a fluvial network.
% Input parameters:
% x - vector of size n of the x coordinate [L] of each pixel.
% y - vector of size n of the y coordinate [L] of each pixel.
% rec_array - vector of size n of the reciever relation. A value j in row i
% means that the reciever of the pixel in row i
% (for which x, y, and drainage area are supplied) is in row j.
% The reciever of the outlet is the pixel itself.
% area_array - vector of size n of upstream drainage area [L^2]
% along fluvial pixels.
% m - scalar. The area power in the stream power law. The analysis assumes
% that n = 1.
% OUtput:
% chi - vector of size n with the chi values [L] of the pixels.
%
% Note: row i in x,y,area_array and chi refers to the same pixel.
A0 = 1; %reference drainage area
%The easisest way to calculate chi is to sort the network from outlet to
%heads. For that all the data is grouped and sorted based on drainage area.
%such that we start with the largest area.
DataMat = [(1:length(x))',x,y,area_array];
SortedDataMat = sortrows(DataMat,-4);
chi = zeros(size(x));
for i = 1:length(x)
my_id = SortedDataMat(i,1);
my_rec = rec_array(my_id);
if my_id==my_rec % outlets are defined as their own receivers
chi(i) = 0;
else
j = find(SortedDataMat(:,1) == my_rec); % find the row of my receiver
dist = sqrt((SortedDataMat(i,2)-SortedDataMat(j,2))^2 +...
(SortedDataMat(i,3)-SortedDataMat(j,3))^2);
chi(i) = chi(j) + dist*(A0/SortedDataMat(i,4))^m;
end
end
SortedDataMat = [SortedDataMat chi]; %add chi to the data
DeSortedDataMat = sortrows(SortedDataMat,1); %resort based on ids
chi = DeSortedDataMat(:,end);