1+ # These are routes to perform waterflow calculations (upstream area etc.) on a DEM
2+
3+ using WhereTheWaterFlows
4+ import WhereTheWaterFlows: waterflows
5+
6+ export waterflows
7+
8+ """
9+ dlon, dlat = spacing(lon,lat)
10+
11+ Computes the spacing with central differences
12+ """
13+ function spacing (lon,lat)
14+ dlon = zero (lon. val[:,:,1 ])
15+ dlat = zero (lat. val[:,:,1 ])
16+ dlon[2 : end - 1 ,:] = (lon. val[3 : end ,:,1 ] - lon. val[1 : end - 2 ,:,1 ])/ 2
17+ dlon[1 ,:], dlon[end ,:] = dlon[2 ,:], dlon[end - 1 ,:]
18+ dlat[:,2 : end - 1 ] = (lat. val[:,3 : end ,1 ] - lat. val[:,1 : end - 2 ,1 ])/ 2
19+ dlat[:,1 ], dlat[:,end ] = dlat[:,2 ], dlat[:,end - 1 ]
20+ return dlon, dlat
21+ end
22+
23+ """
24+ area_m2 = cell_area(Topo::GeoData)
25+ Returns the cell area for a Topographic dataset in m² (required for upstream area calculation)
26+ """
27+ function cell_area (Topo:: GeoData )
28+
29+ proj = ProjectionPoint (Lon= mean (Topo. lon. val[:]), Lat= mean (Topo. lat. val[:]))
30+ Topo_cart = convert2CartData (Topo, proj)
31+ dx, dy = spacing (Topo_cart. x, Topo_cart. y)
32+
33+ area_m2 = dx.* dy* 1e6
34+ return area_m2
35+ end
36+
37+
38+ """
39+ Topo_water, sinks, pits, bnds = waterflows(Topo::GeoData;
40+ flowdir_fn=WhereTheWaterFlows.d8dir_feature, feedback_fn=nothing, drain_pits=true, bnd_as_sink=true)
41+
42+ Takes a GMG GeoData object of a topographic map and routes water through the grid. We add a number of
43+
44+ Example
45+ ===
46+ ```julia
47+ # Download some topographic data
48+ julia> Topo = import_topo([6.5,7.3,50.2,50.6], file="@earth_relief_03s");
49+
50+ # Flow the water through the area:
51+ julia> Topo_water, sinks, pits, bnds = waterflows(Topo)
52+ julia> Topo_water
53+ GeoData
54+ size : (961, 481, 1)
55+ lon ϵ [ 6.5 : 7.3]
56+ lat ϵ [ 50.2 : 50.59999999999999]
57+ depth ϵ [ 0.045 : 0.724]
58+ fields : (:Topography, :area, :slen, :dir, :nout, :nin, :c)
59+
60+ ```
61+
62+ """
63+ function waterflows (Topo:: GeoData , flowdir_fn= WhereTheWaterFlows. d8dir_feature; feedback_fn= nothing , drain_pits= true , bnd_as_sink= true )
64+
65+ cellarea = cell_area (Topo)
66+ dem = Topo. depth. val[:,:,1 ]
67+
68+ area = zeros (Float64,size (Topo. depth. val))
69+ slen = zeros (Int64,size (Topo. depth. val))
70+ dir = zeros (Int8,size (Topo. depth. val))
71+ nout = zeros (Int8,size (Topo. depth. val))
72+ nin = zeros (Int8,size (Topo. depth. val))
73+ c = zeros (Int64,size (Topo. depth. val))
74+
75+ area[:,:,1 ], slen[:,:,1 ], dir[:,:,1 ], nout[:,:,1 ], nin[:,:,1 ], sinks, pits, c[:,:,1 ], bnds = waterflows (dem, cellarea, flowdir_fn;
76+ feedback_fn= feedback_fn, drain_pits= drain_pits, bnd_as_sink= bnd_as_sink)
77+
78+ Topo_water = addfield (Topo,(;area, slen, dir, nout, nin, c ))
79+ return Topo_water, sinks, pits, bnds
80+ end
0 commit comments