forked from csdms-contrib/topotoolbox
/
mapfromnal.m
82 lines (66 loc) · 1.61 KB
/
mapfromnal.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
function [V,IX] = mapfromnal(FD,S,nal,cl)
%MAPFROMNAL map values from node-attribute list to nearest upstream grid
%
% Syntax
%
% V = mapfromnal(FD,S,nal)
% [V,IX] = mapfromnal(FD,S,nal)
%
% Description
%
% mapfromnal takes a STREAMobj S and an associated node-attribute list
% nal and maps the values in the nal to the nearest grid values
% measured along flowpaths based on the FLOWobj FD. S should be have
% been derived from FD.
%
% Input arguments
%
% FD FLOWobj
% S STREAMobj
% nal node-attribute list
%
% Output arguments
%
% V GRIDobj with values derived from nal
% IX matrix with size V.size with linear indices into
% node-attributes of S. Elements in IX with no downstream
% stream pixel are zero.
%
% See also: FLOWobj, STREAMobj, flowdistance, vertdistance2stream
%
% Author: Wolfgang Schwanghart (w.schwanghart[at]geo.uni-potsdam.de)
% Date: 6. November, 2018
narginchk(3,4)
% If the variable nal is a GRIDobj than extract the nal
if isa(nal,'GRIDobj')
nal = getnal(S,nal);
else
tf = isnal(S,nal);
if ~tf
error('3rd input argument is not a node-attribute list')
end
end
% check class
if nargin == 3
cl = 'single';
end
nrnal = numel(S.x);
nalix = (1:nrnal)';
I = STREAMobj2GRIDobj(S);
IX = zeros(I.size);
IX(S.IXgrid) = nalix;
ix = FD.ix;
ixc = FD.ixc;
Z = I.Z;
for r = numel(ixc):-1:1
if ~(Z(ixc(r)) && Z(ix(r)))
IX(ix(r)) = IX(ixc(r));
end
end
I.Z = Z;
V = GRIDobj(I,cl);
if isfloat(V.Z)
V.Z(:,:) = nan(cl);
end
I = IX~=0;
V.Z(I) = cast(nal(IX(I)),cl);