-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from JuliaGeodynamics/bk-waterflow
Waterflow routing algorithm
- Loading branch information
Showing
7 changed files
with
245 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
MOR = "MOR" | ||
dum = "dum" | ||
Shepard = "Shepard" | ||
nin = "nin" | ||
|
||
[files] | ||
extend-exclude = ["tutorials/*.pvsm"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# These are routes to perform waterflow calculations (upstream area etc.) on a DEM | ||
|
||
using WhereTheWaterFlows | ||
import WhereTheWaterFlows: waterflows | ||
|
||
export waterflows | ||
|
||
""" | ||
dlon, dlat = spacing(lon,lat) | ||
Computes the spacing with central differences | ||
""" | ||
function spacing(lon,lat) | ||
dlon = zeros(size(lon.val)[1:2]) | ||
dlat = zeros(size(lat.val)[1:2]) | ||
@views dlon[2:end-1,:] = (lon.val[3:end,:,1] - lon.val[1:end-2,:,1])/2 | ||
dlon[1,:] = dlon[2,:] | ||
dlon[end,:] = dlon[end-1,:] | ||
dlat[:,2:end-1] = (lat.val[:,3:end,1] - lat.val[:,1:end-2,1])/2 | ||
dlat[:,1] = dlat[:,2] | ||
dlat[:,end] = dlat[:,end-1] | ||
|
||
return dlon, dlat | ||
end | ||
|
||
""" | ||
area_m2 = cell_area(Topo::GeoData) | ||
Returns the cell area for a Topographic dataset in m² (required for upstream area calculation) | ||
""" | ||
function cell_area(Topo::GeoData) | ||
|
||
proj = ProjectionPoint(Lon=mean(Topo.lon.val[:]), Lat=mean(Topo.lat.val[:])) | ||
Topo_cart = convert2CartData(Topo, proj) | ||
dx, dy = spacing(Topo_cart.x, Topo_cart.y) | ||
|
||
area_m2 = dx.*dy*1e6 | ||
return area_m2 | ||
end | ||
|
||
|
||
""" | ||
Topo_water, sinks, pits, bnds = waterflows(Topo::GeoData; | ||
flowdir_fn=WhereTheWaterFlows.d8dir_feature, feedback_fn=nothing, drain_pits=true, bnd_as_sink=true, | ||
rainfall = nothing, | ||
minsize=300) | ||
Takes a GMG GeoData object of a topographic map and routes water through the grid. Optionally, | ||
you can specify `rainfall` in which case we accumulate the rain as specified in this 2D array instead of the cellarea. | ||
This allows you to, for example, sum, up water if you have variable rainfall in the area. | ||
The other options are as in the `waterflows` function of the package `WhereTheWaterFlows`. | ||
Example | ||
=== | ||
```julia | ||
# Download some topographic data | ||
julia> Topo = import_topo([6.5,7.3,50.2,50.6], file="@earth_relief_03s"); | ||
# Flow the water through the area: | ||
julia> Topo_water, sinks, pits, bnds = waterflows(Topo) | ||
julia> Topo_water | ||
GeoData | ||
size : (961, 481, 1) | ||
lon ϵ [ 6.5 : 7.3] | ||
lat ϵ [ 50.2 : 50.59999999999999] | ||
depth ϵ [ 0.045 : 0.724] | ||
fields : (:Topography, :area, :slen, :dir, :nout, :nin, :c) | ||
``` | ||
""" | ||
function waterflows(Topo::GeoData, flowdir_fn= WhereTheWaterFlows.d8dir_feature; feedback_fn=nothing, drain_pits=true, bnd_as_sink=true, rainfall=nothing, minsize=300) | ||
|
||
cellarea = cell_area(Topo) | ||
cellarea_m2 = cellarea | ||
if !isnothing(rainfall) | ||
@assert typeof(rainfall) == Array{Float64,2} | ||
cellarea = rainfall | ||
end | ||
|
||
dem = Topo.depth.val[:,:,1] | ||
|
||
ni = size(Topo.depth.val) | ||
area = zeros(ni) | ||
slen = zeros(Int64, ni) | ||
dir = zeros(Int8, ni) | ||
nout = zeros(Int8, ni) | ||
nin = zeros(Int8, ni) | ||
c = zeros(Int64, ni) | ||
|
||
area[:,:,1], slen[:,:,1], dir[:,:,1], nout[:,:,1], nin[:,:,1], sinks, pits, c[:,:,1], bnds = waterflows(dem, cellarea, flowdir_fn; | ||
feedback_fn=feedback_fn, drain_pits=drain_pits, bnd_as_sink=bnd_as_sink) | ||
|
||
catchment_large = prune_catchments(c, minsize; val=0) | ||
largest_catchment = catchment_large .== maximum(catchment_large) | ||
largest_area = copy(area) | ||
largest_area[.!largest_catchment] .= NaN | ||
|
||
log10_area = log10.(area) | ||
|
||
Topo_water = addfield(Topo,(;area, slen, dir, nout, nin, c, cellarea_m2, catchment_large, log10_area, largest_catchment, largest_area)) | ||
return Topo_water, sinks, pits, bnds | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Test, GMT | ||
|
||
|
||
# Download some topographic data | ||
Topo = import_topo([6.5,7.3,50.2,50.6], file="@earth_relief_03s"); | ||
|
||
# Flow the water through the area: | ||
Topo_water, sinks, pits, bnds = waterflows(Topo) | ||
|
||
@test maximum(Topo_water.fields.area) ≈ 9.309204547276944e8 | ||
@test sum(Topo_water.fields.c) == 834501044 | ||
@test sum(Topo_water.fields.nin) == 459361 | ||
@test sum(Topo_water.fields.dir) == 2412566 | ||
|
||
# With rain in m3/s per cell | ||
rainfall = ones(size(Topo.lon.val[:,:,1]))*1e-3 # 2D array with rainfall per cell area | ||
Topo_water1, sinks, pits, bnds = waterflows(Topo, rainfall=rainfall) | ||
|
||
@test maximum(Topo_water1.fields.area) ≈ 169.79800000000208 | ||
|
2878771
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
2878771
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/110366
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: