API reference¶
An upstream river basin and everything open that can be clipped to it.
Create one from an outlet coordinate and every layer method afterwards is masked to the polygon, not to its bounding box::
import basinkit as bk
basin = bk.Basin.from_point(26.87, 87.15) # Sapta Koshi at Chatara
basin.area_km2
dem = basin.dem() # xarray, clipped + masked
lc = basin.landcover()
rain = basin.precipitation(2000, 2024) # basin-mean monthly series
basin.download_all("koshi/") # the whole default stack
Attributes:
| Name | Type | Description |
|---|---|---|
geometry |
shapely geometry
|
Basin polygon in EPSG:4326. |
provenance |
dict
|
Which backend and which dataset version produced the polygon. This travels with the basin so that a result is always attributable, and it is written into every export. |
Source code in basinkit/basin.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
area_km2
property
¶
Basin area via an equal-area projection centred on the basin itself.
bbox_efficiency
property
¶
Basin area as a fraction of its bounding-box area.
This is the number that justifies polygon clipping. A compact basin scores near 0.7; a long dendritic one can drop below 0.25, meaning a bbox-based download wastes three quarters of everything it transfers and biases every basin average with a neighbour's pixels.
attributes(prefixes=None, **kwargs)
¶
281 pre-computed BasinATLAS attributes for this basin.
The row returned belongs to the outlet's HydroBASINS unit, and its
_u columns are already aggregated over everything upstream -- so
this characterises the whole catchment without touching a raster.
Costs one 2.7 GB download the first time, then nothing.
Source code in basinkit/basin.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
available_water_capacity(depth='0-5cm')
¶
Plant-available water capacity (field capacity minus wilting point).
Source code in basinkit/basin.py
156 157 158 159 160 | |
check_license(key, *, commercial=False, redistribute=False)
staticmethod
¶
Raise if a dataset's licence forbids the intended use.
Source code in basinkit/basin.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
dem(product='cop30', **kwargs)
¶
Elevation, clipped and masked to the basin.
Source code in basinkit/basin.py
124 125 126 127 128 | |
download_all(outdir, layers=('dem', 'landcover', 'soil', 'surface_water', 'precipitation', 'rivers'), *, start=2000, end=None, progress=True)
¶
Fetch the default stack and write it to outdir.
This is the "give me everything" button. Each layer is attempted independently, so one failing source (a polar basin with no CHIRPS, say) does not abort the rest -- failures are recorded in the manifest alongside the successes.
Source code in basinkit/basin.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
explore(**kwargs)
¶
Interactive map of the basin. Needs pip install 'basinkit[viz]'.
Source code in basinkit/basin.py
509 510 511 512 513 | |
export_3d(path, **kwargs)
¶
Write an interactive 3D page for this basin: terrain, imagery, rivers.
One self-contained HTML file with everything embedded, so it opens with
no network. See :func:basinkit.viz3d.export_3d for the options.
basin.export_3d("koshi.html")
basin.export_3d("koshi.html", texture=None) # elevation only, small
This is a way of looking at the layers this package fetches. It makes no claim the other methods do not already make.
Source code in basinkit/basin.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
from_file(path)
classmethod
¶
Load a basin from any vector file geopandas can read.
Source code in basinkit/basin.py
71 72 73 74 75 76 77 78 79 | |
from_geometry(geometry, provenance=None)
classmethod
¶
Wrap a polygon you already have (a gauge basin, an official boundary).
Source code in basinkit/basin.py
66 67 68 69 | |
from_point(lat, lon, *, backend='auto', **kwargs)
classmethod
¶
Delineate the basin upstream of an outlet coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
|
'auto'
|
Source code in basinkit/basin.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
lakes(min_area_km2=0.0, **kwargs)
¶
HydroLAKES water bodies inside the basin.
Source code in basinkit/basin.py
217 218 219 220 221 | |
landcover(year=None, source='worldcover', **kwargs)
¶
Land cover, as a 2-D array with its class legend in .attrs.
worldcover is ESA WorldCover at 10 m (2020 or 2021); esri is the
ESRI / Impact Observatory annual series. Both return the same shape of
object, and each carries its own legend -- the two number their classes
differently, and code 10 is tree cover in one and cloud in the other.
year=None means 2021 for WorldCover and the latest year published
for this location for ESRI.
Source code in basinkit/basin.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
landsat(start, end, *, cloud_cover=20, bands=None, composite='median', **kwargs)
¶
Landsat Collection 2 Level-2 over the basin (1982 to present).
Source code in basinkit/basin.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
license_report(layers=None)
¶
Attribution and licence text for the layers you used.
Print this into your methods section. Every layer basinkit fetches by default is CC BY 4.0 or more permissive, which means it can be redistributed and used commercially -- but only if it is attributed.
Source code in basinkit/basin.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
morphometry(**kwargs)
¶
The classical Horton-Strahler-Schumm morphometric parameters.
m = basin.morphometry()
m["areal"]["drainage_density_km_per_km2"]
m["network"] # one row per Strahler order
Counts Strahler streams, not the reaches a river dataset splits them into, and measures area, perimeter and every length in one equal-area projection. Both matter: on the Koshi, counting reaches turns the bifurcation ratios into values that are not physically possible.
See :mod:basinkit.morphometry for what each parameter is and for what
the numbers can and cannot be compared against.
Source code in basinkit/basin.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
plot(**kwargs)
¶
Static matplotlib figure: hypsometry, boundary and river network.
Source code in basinkit/basin.py
515 516 517 518 519 | |
precipitation(start=2000, end=None, source='chirps', **kwargs)
¶
Basin-mean rainfall time series. chirps, persiann or terraclimate.
Source code in basinkit/basin.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
rivers(min_order=0, **kwargs)
¶
HydroRIVERS reaches inside the basin, with discharge and stream order.
Source code in basinkit/basin.py
211 212 213 214 215 | |
sentinel1(start, end, *, bands=None, composite='median', **kwargs)
¶
Sentinel-1 RTC: terrain-corrected radar, so it works through cloud.
Source code in basinkit/basin.py
253 254 255 256 257 258 259 260 261 262 263 264 | |
sentinel2(start, end, *, cloud_cover=20, bands=None, composite='median', **kwargs)
¶
Sentinel-2 L2A over the basin, cloud-filtered and optionally composited.
Source code in basinkit/basin.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
soil(prop='clay', depth='0-5cm', **kwargs)
¶
A SoilGrids property. See :data:basinkit.sources.soil.PROPERTIES.
Source code in basinkit/basin.py
150 151 152 153 154 | |
summary(*, terrain=True, landcover=True)
¶
A one-call characterisation of the basin.
Source code in basinkit/basin.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
surface_water(layer='occurrence', **kwargs)
¶
JRC Global Surface Water: a pre-reduced 37-year Landsat water stack.
Source code in basinkit/basin.py
183 184 185 186 187 | |
terrain_stats()
¶
Elevation, relief and mean slope: the standard morphometry.
Source code in basinkit/basin.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
water_balance(start=2000, end=None)
¶
Monthly P / AET / PET / Q / soil-moisture balance from TerraClimate.
Source code in basinkit/basin.py
177 178 179 180 181 | |
Upstream basin delineation backends.
Three backends, because no single one is right for every basin:
hydrobasins
Graph traversal over HydroBASINS level-12 sub-basins. Global, CC BY 4.0,
offline once cached, and fast at any basin size because upstream
aggregation is a walk over the NEXT_DOWN field rather than a raster
fill.
**Base grid: 15 arc-seconds, about 460 m, from February 2000 SRTM.**
HydroBASINS is extracted from the HydroSHEDS core layers at that
resolution, so this backend inherits it. Two consequences worth stating
plainly: the resolution floor is the level-12 unit (~130 km2 median), and
the flow network is a quarter-century-old DEM. For most basins that is
fine. For a small or heavily modified catchment it is not, and the other
two backends exist for exactly that case.
dem
D8 flow routing with pyflwdir over a freshly downloaded Copernicus DEM
window. Base grid: 1 arc-second, about 30 m, from 2011-2015 radar.
Fifteen times finer than the default and a decade newer, so it is the right
answer for small catchments, and the wrong one for large ones: cost grows
with basin area and the window must contain the whole basin.
api
The public Global Watersheds service, backed by MERIT-Hydro.
Base grid: 3 arc-seconds, about 90 m, multi-error-removed. Five times
finer than the default and hydrologically conditioned rather than raw SRTM.
No download at all, so it is the fastest first look -- but it is one
research group's server and MERIT-Hydro's licence is non-commercial, so
basinkit never makes it the default and records both facts in provenance.
auto picks between them from the drainage area implied by the outlet.
Base resolution, side by side:
=============== ================= ================== ====================
backend grid source conditioned
=============== ================= ================== ====================
hydrobasins 15 arc-sec, 460 m SRTM, Feb 2000 HydroSHEDS
api 3 arc-sec, 90 m MERIT-Hydro yes, error-removed
dem 1 arc-sec, 30 m Copernicus, 2011-15 no, routed on the fly
=============== ================= ================== ====================
delineate(lat, lon, backend='auto', **kwargs)
¶
Delineate the upstream basin of (lat, lon).
Returns:
| Type | Description |
|---|---|
(geometry, dict)
|
The basin polygon in EPSG:4326 and a provenance dict recording which backend and dataset version produced it. The provenance travels with the basin so a result is always attributable. |
Source code in basinkit/delineate/__init__.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
delineate_api(lat, lon, *, precision='high', **_)
¶
Fetch an upstream basin polygon from the Global Watersheds service.
No local data is downloaded, which makes this the quickest way to look at a basin. The service auto-downgrades to low precision above ~50,000 km2.
This is a courtesy endpoint run by one research group, not managed
infrastructure. basinkit therefore never selects it automatically, and
stamps backend='api' into provenance so a downstream reader can tell
that the geometry did not come from a versioned dataset.
Source code in basinkit/delineate/api.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
delineate_dem(lat, lon, *, window_deg=0.5, product='cop30', snap_px=12, min_uparea_km2=1.0, max_window_deg=4.0, progress=True, **_)
¶
Delineate the upstream basin by D8 routing on a Copernicus DEM window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_deg
|
float
|
Half-width of the initial DEM window in degrees. Grown automatically if the basin reaches the edge. |
0.5
|
snap_px
|
int
|
Radius, in pixels, of the search for the true channel cell. |
12
|
max_window_deg
|
float
|
Stop growing at this half-width and raise instead of silently downloading the continent. |
4.0
|
Source code in basinkit/delineate/dem.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
delineate_hydrobasins(lat, lon, *, level=12, snap_km=5.0, river_snap_km=1.0, river_snap_ratio=10.0, progress=True, **_)
¶
Delineate the upstream basin from HydroBASINS level-12 units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Pfafstetter level of the source file. 12 is finest and the default. |
12
|
snap_km
|
float
|
If the outlet is not inside any unit at all, snap to the nearest one within this distance. Coastal outlets often sit just offshore. |
5.0
|
river_snap_km
|
float
|
Guard against the bank-of-a-big-river problem: if a unit within
|
1.0
|
river_snap_ratio
|
float
|
Guard against the bank-of-a-big-river problem: if a unit within
|
1.0
|
Source code in basinkit/delineate/hydrobasins.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
Clip and mask rasters to a basin polygon.
This is the part every generic downloader skips. eodag, earthaccess,
pystac-client and friends all take a bounding box and give you whole
scenes or tiles. For a river basin that is the wrong shape by a wide margin: a
long dendritic catchment can occupy under a third of its own bbox, so two
thirds of what you download, store and average over belongs to a neighbouring
basin. Everything here works on the polygon.
basin_area_km2(geometry, crs='EPSG:4326')
¶
Area of a lon/lat polygon in km2, via an equal-area projection.
Computing area in degrees is a common and badly wrong shortcut. This reprojects to a Lambert azimuthal equal-area centred on the basin itself, which is accurate to well under a percent at catchment scale.
Source code in basinkit/clip.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
clip_raster(path_or_ds, geometry, *, crs='EPSG:4326', all_touched=False, nodata=None, drop_empty=True)
¶
Open a raster, clip it to geometry and mask everything outside.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_ds
|
Any
|
File path, URL, or an already-open |
required |
geometry
|
Any
|
Basin polygon in |
required |
all_touched
|
bool
|
Include pixels merely touched by the boundary. Set |
False
|
drop_empty
|
bool
|
Raise a helpful error instead of returning an all-nodata array. |
True
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Clipped to the polygon envelope and masked outside the polygon itself. |
Source code in basinkit/clip.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
clip_stack(da, geometry, *, crs='EPSG:4326', all_touched=False)
¶
Clip an already-loaded (possibly multi-temporal) DataArray to a polygon.
Source code in basinkit/clip.py
162 163 164 | |
zonal_mean(da, geometry=None, *, dims=('y', 'x'))
¶
Area-weighted-ish spatial mean over a clipped array.
On a geographic grid, pixel area shrinks with cos(latitude). Ignoring
that biases a basin mean toward its poleward end. For a small basin the
error is negligible; for a Nile- or Ob-sized one it is not, so this
weights by cos(lat) whenever a latitude coordinate is present.
Source code in basinkit/clip.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
Tile mosaicking with a hard ceiling on how much comes into memory.
The naive version of this -- merge every tile at native resolution, then clip -- is fine for a 200 km2 catchment and fatal for a 50,000 km2 one. ESA WorldCover over the Koshi basin is nine tiles and a five-degree extent; at 10 m that is about 2.5 billion pixels, which no laptop will hold.
So the mosaic is budgeted. basinkit estimates the output size first and, if it
exceeds max_pixels, coarsens by an integer factor and records that in the
array's attributes. Nearest-neighbour resampling is used for categorical layers
so class codes are never averaged into meaningless intermediates.
merge_tiles(paths, bounds, *, max_pixels=DEFAULT_MAX_PIXELS, categorical=False, nodata_below=None, src_nodata=None)
¶
Merge tiles into one DataArray, coarsening if the result would be huge.
Returns:
| Type | Description |
|---|---|
(DataArray, dict)
|
The mosaic, and a dict describing whether and by how much it was
coarsened. The caller writes that into |
Source code in basinkit/mosaic.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
Machine-readable catalogue of the open datasets basinkit can fetch.
Every entry records not just where the data lives but the two facts that
decide whether a pipeline is reproducible: whether an account is needed, and
what the licence permits. basinkit.catalog.table() prints it; the CLI and
Basin.license_report() read from the same dict, so the documentation can
never drift from the code.
Verified live 2026-08-25.
Dataset
dataclass
¶
One fetchable open dataset.
Source code in basinkit/catalog.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
anonymous()
¶
Datasets needing no account of any kind.
Source code in basinkit/catalog.py
392 393 394 | |
implemented()
¶
Datasets basinkit can actually fetch today.
Source code in basinkit/catalog.py
372 373 374 | |
require(key)
¶
Return a dataset, raising with instructions if it cannot be fetched.
Source code in basinkit/catalog.py
382 383 384 385 386 387 388 389 | |
table(datasets=None)
¶
Render the catalogue as a plain-text table.
Source code in basinkit/catalog.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
unimplemented()
¶
Datasets basinkit documents but cannot fetch.
Source code in basinkit/catalog.py
377 378 379 | |