-
Notifications
You must be signed in to change notification settings - Fork 35
Waterflow routing algorithm #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
23dca3d
add waterflow algorithm that flows water over topography
boriskaus 8e36c35
update spellcheck
boriskaus 798af1f
bump version
boriskaus 6283405
restruict GMT version
boriskaus 6232aba
allow specifying rainfall on the grid; also returns the area of each …
boriskaus aaacb02
fix GDAL_jll version
boriskaus af993c4
add large catchment areas
boriskaus c17239c
Update src/WaterFlow.jl
boriskaus 07deceb
Update src/WaterFlow.jl
boriskaus 9324c16
Update src/WaterFlow.jl
boriskaus 8069bd9
Update src/WaterFlow.jl
boriskaus c6c28b2
fix CI
boriskaus ed2b3c5
another fix
boriskaus 004c293
add GLMakie heatmap and heatmap! multiple dispatch for surfaces
boriskaus af53201
add a few more fields to simplify plotting the largest catchment
boriskaus ac66971
allow plotting an array computed on top of a surface
boriskaus 64f4b9a
remove log10 of largest area
boriskaus 1821f87
typo
boriskaus e4ad4a7
Merge remote-tracking branch 'origin/main' into bk-waterflow
boriskaus 087a273
undo last change. dot is required
boriskaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
boriskaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.