GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Downloadsevery file, by dataset
  • Find by areabasins, regions and source tiles
  • HydroSHEDS v2.0 · 30 m
  • 2D Mapslook at the data
  • FullBasin
  • 3D Mapscurrently just for fun
  • ToolsPython packages
  • FlowDivide 1.1.5
  • FlowTopo 1.1.3
  • FlowMorph 1.1.2
  • FlowNest 1.0.0
  • Applicationsstudies using the data
  • Videos and presentationstalks and slides

This view

  • Original page
  • New address

GeezerVison is showing fullhydro.org — FlowTopo — FullHydro

Get it

Python 3.10 or newer. The structures and the serial kernels run in pure Python without numba; the threaded kernels need numba.

git clone https://github.com/hellolulujiang/flowtopo.git
cd flowtopo
pip install -e .            # numpy, rasterio, scipy, pymetis: structures, serial kernels, partitions
pip install -e ".[speed]"   # adds numba: the threaded kernels need it

Quick start

import flowtopo

topo = flowtopo.FlowTopo.from_raster("data/dir_example.tif")

upa = topo.upstream_area(ordering="dfs")           # km², one serial pass
ldn = topo.distance_to_outlet(ordering="dfs")      # flow length downstream, m
strord = topo.strahler_order(                      # on cells draining ≥ 10 km²
    ordering="dfs", channel_mask=topo.channel_mask(upa, 10.0))

upa_grid = topo.to_2d(upa)                         # back on the raster grid

Three of the kernels also run on threads, a layer at a time (needs numba); Strahler stream order runs serially only:

from flowtopo import parallel
upa = parallel.upstream_area(topo, layering="cfds", manner="push")   # push is safe only under cfds

For a FullTopo v1.1 region, use the following command to compute each pixel’s upstream interval from the region’s seq_dfs raster and MERIT Hydro’s upg raster clipped to the same window. Supply the regional basin table to identify the outlets. The intervals are numbered within each basin, as in FullBasin v1.1. Pixel q drains through p exactly when dfs_in[p] ≤ dfs_in[q] ≤ dfs_out[p].

flowtopo.dfs_interval_geotiff("seq_dfs_region.tif", "upg_region.tif",
                              "dfs_in_region.tif", "dfs_out_region.tif",
                              outlets="bat_region.csv")

Example data

data/dir_example.tif is the example basin of the paper: 292 rows by 614 columns at 3 arc-seconds, 93,432 valid pixels, 729 km² (the upstream area MERIT Hydro gives at the outlet; summing FlowTopo’s default spherical pixel areas gives 731 km²), cut from MERIT Hydro (Yamazaki et al., 2019) and kept under its CC BY-NC 4.0 terms. The GeoJSON files are the basin boundary and the outlet.

Any D8 GeoTIFF in the same convention works, north up, in longitude and latitude or in a projection whose unit is the metre: the CRS decides whether the area and distance kernels read the grid as degrees or as metres, and a rotated grid or a projection in another unit is refused. Direction codes are powers of two clockwise from east. Codes 0 and 255 mark terminal pixels; 247 is always nodata, and a nodata value declared in the file is excluded as well. Process one region or basin at a time: indices are int32, so a raster must stay under 2.1 billion pixels, nodata included, as the regions of MERIT-FullBasin do.

What you get

Eight structures, grouped by the question each one answers, and three ways to pass a value from a pixel to its receiver. All are built from the D8 grid. Every clip runs on the same example basin; it plays while it is on screen, and opens full size on a click.

One core: in what order?

Three serial orderings. A kernel is computed in one pass over any of them. They differ in memory access, and so in speed.

Topological sort from the sources ordering="topo"a pixel is appended once all its donors are done
Breadth-first search from the pit ordering="bfs"pixels in order of hop count from the pit
Depth-first search from the pit ordering="dfs"one tributary subtree at a time; best cache locality

Many threads: which pixels together?

Three parallel layerings. Layers run in order; the pixels of one layer run at the same time.

As-soon-as-possible layering layering="asap"every pixel in the earliest layer its donors allow
Conflict-free downstream layering layering="cfds"as-soon-as-possible, plus: no two pixels in a layer share a receiver
As-late-as-possible layering layering="alap"every pixel as late as the longest flow path allows; the most evenly filled layers in practice

Several processors: which pixels where?

Two spatial partitions, one subregion per processor. Both cut along the drainage hierarchy, so no value crosses a boundary while a kernel runs.

Two basins assigned to two subregions: (a) whole basins, unbalanced; (b) the dominant basin cut along its mainstem and one tributary subtree moved; (c) balanced
Two basins assigned to two subregions: (a) whole basins, unbalanced; (b) the dominant basin cut along its mainstem and one tributary subtree moved; (c) balanced.
Basin-level level="basin"

Whole basins go to subregions; one dominant basin leaves the partition unbalanced.

Subbasin-level level="subbasin"

The basins that keep the subregions from balancing, usually the dominant one, are opened along their mainstems; their tributary subtrees balance the load, and the held-back mainstem section runs as its own stage.

At a confluence: how does a value reach the receiver?

Three propagation manners. The structure decides when a pixel runs; the manner decides who writes the result.

Pull manner="pull"each receiver reads its donors and writes only its own value; needs the donor table
Atomic push manner="atomic_push"donors write through atomics; correct, but floating-point sums are not bit-reproducible
Push manner="push"donors write directly; deterministic, no locks; safe only under cfds

Three things to know

  • An ordering is built in one direction and can be read in either direction. Kernels that accumulate at the receiver (upstream drainage area, flow length upstream, Strahler stream order) walk upstream to downstream; flow length downstream walks the other way. The kernels pick the direction themselves; topo.ordering("dfs", "u2d") asks for one explicitly.
  • The layer count is set by the longest flow path, and the conflict-free rule may add layers (on the example basin it adds none: 949 for all three). In as-soon-as-possible layer 0 holds every headwater; conflict-free downstream holds back a headwater whose receiver another pixel of the layer already drains into, so its layer 0 can hold fewer; in as-late-as-possible it holds only the farthest ones.
  • topo.partition(n_parts, level) returns a label per pixel and the load per subregion. At subbasin level it labels the held-back mainstem section (from Pmin down, Pmin included) flowtopo.MAINSTEM; it runs as its own stage, after the tributary subregions for kernels that accumulate downstream and before them for flow length downstream.

Four kernels come with the package: upstream drainage area, flow length downstream (distance_to_outlet), flow length upstream (longest_upstream_path) and Strahler stream order. Each runs on every supported combination of structure and manner, and the results are checked against each other.

Write conflicts and cache locality on the example basin

A push is only safe if no two pixels in a layer write to the same receiver. Conflicting writes inside a layer, example basin (93,432 pixels):

layeringconflicting writes
as-soon-as-possible12,122
conflict-free downstream0
as-late-as-possible39,130

The count is a property of the layering and can be checked before running. A test run cannot replace that check, because a race does not show up every time. Under the conflict-free downstream layering, no two pixels in a layer share a receiver, so the push adds in the same order every time and gives bit-identical results at any thread count. Strahler stream order cannot be done with one atomic operation, because its confluence rule is a comparison and a count, not an addition; the only parallel push for it is under the conflict-free layering. When manner is not given, the FlowTopo methods pick a safe one for the layering; parallel.upstream_area defaults to push, which is safe only under the conflict-free downstream layering (cfds).

The three serial orderings differ in memory access. Simulated L1 miss rate on the example basin: depth-first 10.5%, breadth-first 21.8%, topological sort 37.1% (flowtopo.locality.miss_rates). The numbers change by a few points depending on how the two arrays are laid out in memory; the ranking does not.

Which structure to use

From the paper’s benchmark of the C implementation on the 90 m network (22.2 billion pixels, 65 continental regions):

  • One pass on one core — the depth-first search from the pit. Up to 5.1 times faster than the slowest ordering, because each pixel’s downstream receiver stays close in memory (L3 miss rate 6% versus 37%). Measure parallel speedup against it; a slower baseline inflates the speedup.
  • Repeated passes (calibration, ensembles) — the as-late-as-possible layering, fastest in parallel for all four kernels in the C benchmark, because each layer fits in cache. Under it, atomic push beat pull for sums and maxima (71.1 s versus 85.1 s for flow length upstream). Use pull when the kernel has no atomic form or the result must be reproducible; pull needs the donor table.
  • Push for a non-linear kernel, or little RAM — the conflict-free downstream layering with push. No locks, deterministic, only the receiver pointer stored, and the only push that can run Strahler stream order. Pull also runs Strahler stream order under any layering and was faster (4.8 s versus 9.1 s); push wins when the donor table does not fit in memory.
  • Across processors — the subbasin-level partition, one subregion per processor, with as many threads per subregion as the processor’s memory bandwidth can feed. That number depends on the processor; measure it on yours.

The Python package is meant for far smaller grids, and there the ranking is not the same: on 16 million pixels, push under cfds is the fastest threaded form and pull is slower than the serial pass, because building and reading the donor table costs more time than ten threads save. Run benchmark.py on your machine before choosing; it times upstream drainage area on synthetic grids of the side length you give it.

Global products

Using the C implementation over the 90 m MERIT Hydro network, the same methods produced two Zenodo records of per-region GeoTIFFs. Both cover the 65 continental regions of FullBasin v1.0; the 29 island groups and the two regions that straddle the antimeridian are not included.

FullTopo v1.0 10.5281/zenodo.20653059
On Zenodo as MERIT-FlowTopo. The structures: for each of the 65 continental regions the depth-first search from the pit sequence, the conflict-free downstream and as-late-as-possible layerings and the subbasin-level partition; for Region 43 (southern China) all eight, so the alternatives can be compared in one region. 978 GB uncompressed, 50 GB compressed.
FullAttr v1.0 10.5281/zenodo.20686665
On Zenodo as MERIT-DrainAttr. The kernels run over those structures: flow length downstream, flow length upstream and Strahler stream order for each of the 65 continental regions, and upstream drainage area for Region 43 only, since MERIT Hydro already distributes it globally. 622 GB uncompressed, 60 GB compressed.
FullBasin v1.0 10.5281/zenodo.20344113
On Zenodo as MERIT-FullBasin. The companion dataset that divides the network into 96 hydrologically independent regions, of which the 65 continental ones are those above. On this site: FullBasin on MERIT Hydro v1.0.1 · 90 m.
FlowTopo code 10.5281/zenodo.23732152
The record the paper cites for code: the C implementation that produced the two records above, the scripts and data behind every figure, and an archived copy of the Python package. Version 1.1.3 adds the tool that writes dfs_in and dfs_out within each basin, in C and in Python (FlowTopo 1.1.3), and the C code of the partitions; version 1.0.0, titled MERIT-FlowTopo code, is at 10.5281/zenodo.22227621.

Version 1.1 of the three datasets, computed on the 90 regions of FullDivide v1.1 (global, the island groups and the two regions across 180° included), is on Zenodo too: FullTopo v1.1 10.5281/zenodo.23697404, FullAttr v1.1 10.5281/zenodo.23730210 and FullBasin v1.1 (FullDivide and FullMorph) 10.5281/zenodo.22895352. On this site the v1.1 files can be downloaded one by one, in Downloads.

Four maps over the 65 regions

Serial orderings: the sequence index of each ordering, globally and in the example basin The three serial orderings mapped over the 65 regions and in the example basin

(a) topological sort from the sources, (b) breadth-first search from the pit, (c) depth-first search from the pit. The topological sort rises smoothly from headwaters to outlets, breadth-first forms bands of equal hop count from the outlet, depth-first breaks the basin into one compact block per tributary. Global maps share one scale from 0 to the total pixel count; each example-basin map uses its own scale; the red triangle is the outlet. All three are stored upstream to downstream; breadth-first and depth-first search from the pit are built from the outlet but re-stored upstream to downstream, so the colour shows each order’s structure, not where its construction began.

Parallel layerings: the layer index of each layering; only as-late-as-possible fills its layers evenly The three parallel layerings mapped over the 65 regions and in the example basin

(a) as-soon-as-possible, (b) conflict-free downstream, (c) as-late-as-possible. Under (a) and (b) most pixels sit in the first few layers and only the main rivers reach high indices, so the global scale is cut at 3,000; (c) spreads the pixels over all layers and uses the full range, tens of thousands in the largest basins. The example basin has 949 layers under all three. All three are stored upstream to downstream.

Spatial partitions: every region split four ways, Region 43 before and after the mainstem cut Basin-level and subbasin-level partitions of the 65 regions and of Region 43

(a, c) all 65 regions; (b, d) Region 43, subregions 4301 to 4304. Top row basin-level: the dominant basin of Region 43 fills one subregion on its own. Bottom row subbasin-level: the basin is cut along its mainstem (purple) and the four subregions come out balanced. The same four colours are used in every region.

Kernel products: the four FullAttr v1.0 variables mapped Flow length downstream, flow length upstream, Strahler stream order and upstream drainage area mapped

(a) flow length downstream, km; (b) flow length upstream, km; (c) Strahler stream order on channels draining at least 10 km²; (d) upstream drainage area, km², colour scale cut at 100 km². The red box is the example basin. (d) is released for Region 43 only, since MERIT Hydro distributes it globally.

Using the released structures

The structures index into the MERIT Hydro flow-direction grid, which is not redistributed: get it from the MERIT Hydro page under its own terms. The MERIT Hydro tiles a region needs, and its row and column offsets, are listed in the table of the 96 regions of version 1.0, those of the version 1.0 records on Zenodo (CSV); the 90 regions of FullDivide v1.1, those of the version 1.1 records, are in rat_global_90m_with_tiles. Region boxes sit on whole degrees and one degree is 1,200 pixels, so a region is cut from the global rasters by integer arithmetic, with no resampling.

You do not need a whole region. Keep any subset of pixels: the released sequence restricted to them is still a topological sort, and a restricted layering still has independent layers, conflict-free rule included, because removing pixels cannot put a pixel before something it depends on, nor make two remaining pixels depend on each other. A receiver outside the clip becomes an outlet when the clip is loaded. Only the kernel values change: upstream drainage area on a clip counts only the area inside the clip. Use whole basins when the values must match the global ones, or take them from MERIT-DrainAttr.

The released 90 m structures were computed with the same methods by a program written for the 90 m grid. On the example basin the two agree, the accumulated area to within floating-point rounding; this is the only basin checked.

Documentation and checks

  • User guide — choosing an ordering, a layering and a manner; threads; raster I/O.
  • Methods — each method with its origin and its complexity, and what is borrowed from pyflwdir.
  • Quick-start notebook — on the bundled data, stored with its output.
pytest                  # needs pytest; numba for the threaded tests
python example.py       # every structure, kernel and manner on the example basin; ends PASS or FAIL
python benchmark.py     # serial vs threaded on synthetic grids

Every ordering, layering, partition, kernel and manner is cross-checked on the example basin, write-conflict counts included. The structures are checked against their definitions: an ordering is a topological sort, no pixel in a layer depends on another pixel of the same layer, and in an upstream-to-downstream sequence a receiver comes after its donors. These properties still hold after clipping a basin out of a region. The tests and example.py run on Python 3.10, 3.11 and 3.12 on every push.

Cite

The companion manuscript, FlowTopo v1.0: a reusable computational foundation for hyperresolution hydrology on the global 90 m MERIT Hydro drainage network (Jiang et al.), has been in open discussion for Geoscientific Model Development since 3 November 2026 as an EGUsphere preprint, 10.5194/egusphere-2026-5497. Cite it for the method; for the code cite the code record 10.5281/zenodo.23732152, or the repository with the commit you used; for a downloaded product cite its version DOI above.

Acknowledgements

MERIT Hydro (Yamazaki et al., 2019) is the flow-direction grid everything here runs on. The flat downstream-pointer representation, the D8 decoding conventions, the donor-count array, the chain-tracing rank and the breadth-first sequence builder follow pyflwdir (D. Eilander, Deltares; MIT licence; 10.5281/zenodo.4287337). No pyflwdir source is included; the code was written against those conventions, and FlowTopo.from_d8 accepts the same D8 array as pyflwdir.from_array(d8, ftype="d8"). The methods and the results are the authors’ own, described in the companion manuscript.

Licence

The code is under the MIT licence. The bundled MERIT Hydro excerpt keeps its own CC BY-NC 4.0 terms (DATA_NOTICE.md). Questions: [email protected], or a GitHub issue; email reaches us faster.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.