Utilities and visualization

SurfaceWaterIntegratedModeling.saturated_spillgraphMethod
saturated_spillgraph(tstruct)

Create the spillgraph corresponding to the provided trapping structure, considering that all traps are already filled.

The result will be returned as a SimpleDiGraph showing which traps are spilling into which traps or regions.

The trapping structure tstruct may have regions with negative indices (referring to regions that spill out of the terrain). Since SimpleDiGraph does not handle nodes with negative indices, a remapping is necessary between the indices used in tstruct and the set 1:N used in the returned graph. These mappings are returned as the second and third return value:

  • The second return value is a Vector{Int} mapping from 1:N to the indices in tstruct
  • The third return value is a Dict mapping from the indices in tstruct to 1:N.
source
SurfaceWaterIntegratedModeling.watercoursesMethod
watercourses(tstruct, full_traps, precipitation, infiltration)

Compute the total intensity of flow of water across all non-lake grid cells in the terrain.

Determine the areas containing high amounts of flowing water, and return a grid with the corresponding values.

Arguments

  • tstruct::TrapStructure: a TrapStructure object containing all the information from the topographical analysis, as returned by the spillanalysis function.
  • full_traps::Vector{Bool}: A vector with one entry per (sub)trap, indicating whether this trap has been filled yet or not. Only full traps will overflow and forward the water from upstream.
  • precipitation::Matrix{<:Real}: A grid expressing precipitation rate per grid cell. If left empty, it will be substituted by a grid filled with ones.
  • infiltration::Matrix{<:Real}: A grid expressing the maximum infiltration rate per grid cell. If left empty, it will be substituted by a grid filled with zeros.

Returns

  • runoff::Matrix{Float64}: Grid expressing infiltration excess runoff rate (positive values) or remaining infiltration capacity (negative values)
  • region_accum::Vector{Float64}: Vector with one entry per spill region, indicating net water accumulation rate for that region (also including water spilling into the region from upstream filled traps). Note that if the region is associated with a filled trap, the accumulation value is set to zero, unless it spills into a filled 'sibling' trap. The logic is in any case such that the accumulation of any still unfilled trap should equal the sum of accumulation of its regions.
  • offregion_runoff::Float64: Total rate of water flowing off the domain
  • used_infiltration::Float64: Total infiltration rate across the terrain
source
SurfaceWaterIntegratedModeling.all_subtraps_ofMethod
all_subtraps_of(subtrap_graph, trap_ixs)

Return all subtraps (at any level) of a specified set of traps.

The graph representing the supertrap/subtrap tree is given by subtrap_graph. The indices to the set of traps is specified by trap_ixs. This argument may be an integer refering to a single trap, or a Vector{Int} referring to one or more traps.

The result is returned as a Vector{Int} giving the indices of all traps that are subtraps of the trap(s) referred to by trap_ixs.

source
SurfaceWaterIntegratedModeling.all_upstream_regionsMethod
all_upstream_regions(tstruct, region)

Identify all spill regions that will eventually become part of the extended watershed of a specified region.

When traps are empty, water accumulates locally. As traps fill up, water starts to spill from upstream to downstream regions. This function identifies all the spill regions that will flow into a specified region/trap when all upstream traps have been completely filled.

The result is returned as a Vector{Int}, giving the indices of all regions that will be upstream of the specified region when all traps are filled.

Arguments

  • tstruct::TrapStructure{<:Real}: trap structure object describing the terrain traps
  • region::Int: index of the region in question

See also upstream_area

source
SurfaceWaterIntegratedModeling.flatten_grid!Method
flatten_grid!(grid::Matrix{<:Real}, mask::Matrix{<:Bool}, height_choice::Symbol)

Flatten indicated areas of a terrain grid.

The grid grid represents height values of a terrain, and will be modified by the function. The mask is a similarly sized, boolean grid that identifies which areas should be flattened (all cells with value true). Each such connected region is then assigned a fixed height value and grid is modified accordingly.

There are three ways of computing the height, indicated by the height_choice argument. Valid options are:

  • :min - the height of each flat area becomes the minimum value of its grid cells
  • :max - the height of each flat area becomes the maximum value of its grid cells
  • :mean - the height of each flat area becomes the mean value of its grid cells

See also: identify_flat_areas

source
SurfaceWaterIntegratedModeling.identify_flat_areasFunction
identify_flat_areas(grid, rel_tol, min_cluster_size, lengths=nothing)

Identify areas in the grid that are flat within a specified tolerance. Can be used to detect lakes in a terrain.

Returns a boolean grid of same shape as the input grid, where cells that are considered to belong to flat areas are flagged as 'true' (rest are false).

Arguments

  • grid::Matrix{<:Real}: terrain raster grid with height values
  • rel_tol::Real: tolerance to use in determine when a slope between two grid cells may be considered 'flat'. The tolerance is specified relatively to the maximum slope present in the grid.
  • min_cluster_size::Int: Specify how large an agglomeration of "flat" cells needs to be (in terms of number of cells) in order to be included in the result. This is used to filter out small fragments that are not considered important.
  • lengths::Tuple{Int, Int}: Specify the length of the grid in x and y directions. This is needed to correctly handle the aspect ratio when computing slopes. If no argument provided, the resolution of the grid is used as a substitute.

See also: flatten_grid!

source
SurfaceWaterIntegratedModeling.interpolate_timeseriesMethod
interpolate_timeseries(tstruct, seq, timepoints; 
                       filled_color=1, trap_color=2, river_color=3)

Compute the exact terrain fill states for a sequence of timepoints, given a trapping structure and sequence of SpillEvents.

Each generated fill state is represented as an integer matrix (Matrix{Int}), where submerged cells are set to filled_color, the parts of traps that are not (yet) submerged set to trap_color and intermittent streams set to river_color. Other cells are attributed the value zero.

The result is returned as a Vector{Matrix{Int}}, of the same length as timepoints. A second return value provides a corresponding Vector{Int} that for each timepoint provides the index for the latest preceding event in seq.

Arguments

  • tstruct::TrapStructure{<:Real}: trap structure object describing the terrain traps
  • seq::Vector{SpillEvent}: the sequence of events, as computed by fill_sequence for a given weather scenario
  • timepoints::Vector{<:Real}: the timepoints for which we want to compute the exact terrain fill states. Should be given in ascending order.
  • filled_color::Int: The 'color' value to represent filled areas (default: 1).
  • trap_color::Int: The 'color' to represent unfilled parts of traps (default: 2).
  • river_color::Int: The 'color' to represent intermittent rivers (default: 3).
  • verbose::Bool: Whether to print progess information during computations (default: true).

See also spillanalysis, fill_sequence, trap_states_at_timepoints.

source
SurfaceWaterIntegratedModeling.show_region_selectionMethod
show_region_selection(tstruct; selection=nothing, 
                      region_color=1, trap_color=2, river_color=3)

Create a texture identifying the requested regions, traps and related rivers.

The information about the terrain, including topography, spillpoints, etc., is provided by the structure tstruct, which can be obtained from the spillanalysis function. The selected regions are identified by selection, a vector with the indices of the selected regions. If left empty, all toplevel traps/regions will be selected.

The result is returned as a matrix of integers, where the integers can be thought of as 'colors'. The integer values used to indicate region, trap or river are given by the arguments region_color, trap_color and river_color, respectively. If region_color is a negative integer, each region will be assigned an unique color, starting from abs(region_color) and incrementing by one per region.

Arguments

  • selection: integer vector containing indices of the selected regions
  • region_color: integer to assign to the cells of all selected regions
  • trap_color: integer to assign to all selected traps
  • river_color: integer to assign to all river cells related to the selected regions

See also: spillanalysis

source
SurfaceWaterIntegratedModeling.toplevel_trapsMethod
toplevel_traps(subtrap_graph)

Identify all top-level traps in the given subtrap graph.

Top-level traps are those that those that are not subtraps of yet larger lakes. The result is returned as a vector of indices to those traps.

Arguments

  • subtrap_graph::Graphs.SimpleDiGraph: oriented graph expressing the trap/subtrap graph structure. If A is a subtrap of B, there will be an edge pointing from A to B. As such, the toplevel traps are those with not outwards- pointing edges.

See also: sshierarchy!

source
SurfaceWaterIntegratedModeling.trap_states_at_timepointsMethod
trap_states_at_timepoints(tstruct, seq, timepoints)

Compute the exact amount of water in all traps for a specific set of timepoints, given a trapping structure and sequence of SpillEvents.

The result is returned as a vector of three-element tuples, with one entry per timepoint.

  • The first element of the tuple is a Vector{Bool} with one entry per trap, indicating if the trap is filled or not at the specified timepoint.
  • The second element is a Vector{Real} with one entry per trap, giving the amount of water in that trap at the specified timepoint.
  • The third element is an index into seq, pointing to the last SpillEvent to occur before the timepoint.

Arguments

  • tstruct::TrapStructure{<:Real}: trap structure object describing the terrain traps
  • seq::Vector{SpillEvent}: the sequence of events, as computed by fill_sequence for a given weather scenario
  • timepoints::Vector{<:Real}: the timepoints for which we want to compute the exact water amounts of the traps. Should be given in ascending order.
  • verbose::Bool: Whether to print progess information during computations (default: true).

See also spillanalysis, fill_sequence, interpolate_timeseries.

source
SurfaceWaterIntegratedModeling.upstream_areaMethod
upstream_area(tstruct, point, local_only=true)

Determine all grid cells belonging to the upstream area of a given point location in the terrain grid.

Result is returned as a Vector{Int}, giving the linear indices of the terrain grid cells that constitute the upstream area of the specified point.

Arguments

  • tstruct::TrapStructure{<:Real}: trap structure object describing the terrain traps
  • point::Int: linear index to the terrain grid cell for which the upstream area is requested
  • local_only::Bool: if true, consider only the immediate spill region. If otherwise, consider also upstream spill regions.

See also all_upstream_regions.

source
SurfaceWaterIntegratedModeling.plotgridMethod
plotgrid(grid, texture=nothing, colormap=:lightrainbow, wireframe=false, 
         downsamplefac=1, heightfac=1, colorrange=nothing)

Plot a texture-mapped 2½D surface of a given grid, using Makie.

The function returns three values in the following order:

  • the surface object
  • the figure
  • the scene

Arguments

  • grid::AbstractArray{<:Real, 2}: 2D array representing the grid
  • texture::Union{Matrix{<:Union{Real, Colorant}}, Nothing}: optional texture for the grid. This can be presented as a matrix of Colorant (directly representing colors), or of numbers (in which case the prevailing colormap and colorrange are used to specify the actual colors). If texture is left to nothing, the height values of grid will be directly used as a substitute.
  • colormap::Union{Symbol, ColorScheme}=:lightrainbow: specifies the colormap to use if the texture is provided as a matrix of numbers. It can either be a symbol referring to a specific colormap used by Makie (call Makie.available_gradients() for a list of available colormap symbols), or directly provided as a ColorScheme. Note that if texture is given as a Matrix{Colorant}, then colormap has no effect.
  • wireframe::Bool: option to display wireframe grid
  • downsamplefac::Real: scaling factor for downsampling the grid. Useful for rapid visualization of very large grids.
  • heightfac::Real: Scaling factor for height values of the grid
  • colorrange::Tuple{<:Real, <:Real}: specify the values representing the start and end points of colormap

See also drape_surface

source
SurfaceWaterIntegratedModeling.set_cameraposMethod

set_camerapos(figure, scene, cpos, ctarget, czoom)

Set the camera position, target and zoom level for a given scene.

This function is provided as a workaround to smooth over different idiosyncracies in camera handling for the Makie backends GLMakie and CairoMakie.

source