Skip to content

hydro

Depression filling, D8, flow accumulation, slope and the compound topographic index.

quakecascade.hydro

Terrain hydrology on a NumPy DEM grid: depression filling, D8, flow accumulation, CTI.

These kernels feed the Zhu et al. (2015) liquefaction model in quakecascade.liquefaction, which needs a compound topographic index (CTI) computed on ~1 km cells. Grids are 2-D float arrays, row 0 at the top (north), NaN for nodata; the cell size is given in metres.

Ported from the PlanX engine/hydro.py (same author). The D8 pass is vectorised over the eight neighbours with the same arithmetic and the same fixed tie-break order; filling and accumulation keep the original queue algorithms.

HYDRO1K_FLAT_TAN_SLOPE module-attribute

HYDRO1K_FLAT_TAN_SLOPE = 0.001

tan(slope) HYDRO1k substitutes where the slope is zero (USGS HYDRO1k README, Sec. 3.2.5): "smaller than the smallest slope obtainable from a 1000 m data set with a 1 m vertical resolution".

CTI_CONVENTIONS module-attribute

CTI_CONVENTIONS = ('hydro1k', 'metres')

Accepted convention values of cti.

D8_NEIGHBORS module-attribute

D8_NEIGHBORS = ...

The eight D8 neighbours as (row offset, column offset, distance in cells, direction code), in the fixed order the tie-break depends on: E, SE, S, SW, W, NW, N, NE (ESRI/Jenson & Domingue 1988 power-of-two coding).

D8_OFFSETS module-attribute

D8_OFFSETS = {
    code: (dr, dc) for dr, dc, _d, code in D8_NEIGHBORS
}

Direction code -> (row offset, column offset).

fill_depressions

fill_depressions(dem)

Fill closed depressions by priority flood (Wang & Liu 2006).

Every cell is raised to the lowest spill elevation on a path to the grid edge, with 8-connectivity; interior nodata cells are not outlets. The result has flats where pits were; no epsilon gradient is imposed.

Parameters:

Name Type Description Default
dem (rows, cols) array_like

Elevations; NaN is nodata and is left unchanged.

required

Returns:

Type Description
ndarray

The filled DEM.

References

Wang, L. & Liu, H. (2006). An efficient method for identifying and filling surface depressions in digital elevation models for hydrologic analysis and modelling. IJGIS 20(2), 193-213. doi:10.1080/13658810500433453

d8_steepest

d8_steepest(dem, cell_size_m=1.0)

D8 steepest-descent direction and the gradient behind it, in one pass.

For each cell the neighbour with the largest drop / distance (distance 1 or sqrt(2) cells) is chosen; ties go to the first in D8_NEIGHBORS order. A cell with no strictly lower finite neighbour (pit, flat, or nodata edge) gets code 0 and gradient 0.0. Nodata cells get code 0 and gradient 0.0 as well.

Parameters:

Name Type Description Default
dem (rows, cols) array_like

Elevations in metres; NaN is nodata.

required
cell_size_m float

Cell width in metres. It scales the gradient only; the direction grid does not depend on it (O'Callaghan & Mark 1984).

1.0

Returns:

Name Type Description
dirs ndarray of uint8

D8 codes (1, 2, 4, ..., 128; 0 for no outflow).

gradient ndarray of float

Steepest-descent gradient, rise over run (m/m).

d8_flow

d8_flow(dem)

D8 direction codes only (the direction half of d8_steepest).

flow_accumulation

flow_accumulation(dirs)

D8 flow accumulation in cells (each cell counts itself), by Kahn's topological order.

Parameters:

Name Type Description Default
dirs (rows, cols) array_like of int

D8 codes from d8_steepest; 0 (or any unknown code) is an outlet.

required

Returns:

Type Description
ndarray of float

Number of cells draining through each cell, including itself.

slope

slope(dem, cell_size_m)

D8 steepest-descent gradient in m/m (rise over run).

The same quantity cti uses, so a slope fed to quakecascade.liquefaction.vs30_from_slope agrees with the wetness index. This is not a 3x3 Horn (1981) fit; on rough terrain the two differ. Nodata cells return NaN.

slope_radians

slope_radians(dem, cell_size_m)

arctan of slope: topographic slope in radians.

cti

cti(dem, cell_size_m, fill=False, *, convention='hydro1k')

Compound topographic index (wetness index), on the scale Zhu et al. (2015) need.

\[ CTI = \ln\left(\frac{a}{\tan\beta}\right) \]

with a the specific catchment area (upstream area A w^2 per unit contour width w, A the D8 flow accumulation in cells including the cell itself) and tan(beta) the D8 steepest-descent gradient.

convention="hydro1k" (default) reproduces the USGS HYDRO1k layer that the Zhu et al. (2015) liquefaction model was fitted on (USGS gfail zhu_2015.ini reads it): CTI = ln((FA + 1) / tan(slope)) with FA counted in 1-km cells (ISLSCP II HYDRO1k guide, Eq. 1) and tan(slope) = 0.001 where the slope is zero (HYDRO1k README, Sec. 3.2.5). For any cell size this is a in kilometres (A * w / 1000, equal to FA + 1 at 1 km) with tan(beta) floored at HYDRO1K_FLAT_TAN_SLOPE, so no cell is flatter than the calibration data could be. Every cell with data gets a finite index.

convention="metres" is the textbook form (Beven & Kirkby 1979) with a in metres and no floor: +inf on cells without a downslope neighbour, and ln(1000) = 6.91 above the HYDRO1k scale elsewhere. Do not feed it to Zhu et al. (2015): at Zhu's CTI coefficient (0.355) the offset adds 2.45 to the logit.

Parameters:

Name Type Description Default
dem (rows, cols) array_like

Elevations in metres; NaN is nodata.

required
cell_size_m float

Cell width in metres. A DEM described in feet raises CTI by ln(3.28084) = 1.19 everywhere.

required
fill bool

Fill depressions (fill_depressions) before routing. Filled pits become flats.

False
convention ('hydro1k', 'metres')

See above.

"hydro1k"

Returns:

Name Type Description
cti ndarray of float

NaN on nodata cells.

gradient ndarray of float

The D8 gradient (m/m) before any floor; NaN on nodata cells.

References

Beven, K. J. & Kirkby, M. J. (1979). A physically based, variable contributing area model of basin hydrology. Hydrological Sciences Bulletin 24(1), 43-69. doi:10.1080/02626667909491834

U.S. Geological Survey. HYDRO1k Elevation Derivative Database, README, Sec. 3.2.3 and 3.2.5; ISLSCP II HYDRO1k Elevation-derived Products, Eq. 1. doi:10.3334/ORNLDAAC/1007