Interop#
Everything that hands a result to something outside the package.
Hydrodynamic-model export#
Six writers, each expecting a different shape of the same surface. Each takes an ExportGrid — the elevation
array plus the georeferencing the headers need — and returns a dict mapping an artefact label to the path it
wrote, so a caller that does not know which target produces two files still learns both names.
DEM.export validates the target and dispatches through write.
digitalrivers.interop.export
#
Writers that hand a DEM to a hydrodynamic model.
Six targets, each wanting a different shape of the same surface:
| target | files written | format |
|---|---|---|
lisflood_fp |
the path as given | Arc ASCII grid, six-line header |
hec_ras |
.tif |
single-band float32 GeoTIFF in the dataset CRS |
tuflow |
.flt + .hdr |
ESRI float grid, little-endian, with a text header |
sfincs |
.dep + .msk |
headerless float32, plus a 0/1 validity mask |
gmsh |
.geo |
a bounds rectangle with a uniform characteristic length |
iber |
.dat |
ASCII boundary file for the Iber pre-processor |
Every writer takes an :class:ExportGrid — the surface plus the georeferencing the
headers need — and returns a dict mapping an artefact label to the path written, so a
caller that does not know which target produces two files still learns both names.
Splitting these out of DEM.export is what makes them testable: a writer can be handed a
synthetic 3x3 grid and its bytes checked, with no raster on disk and no DEM instance.
DEM.export validates the target and dispatches through :func:write.
ExportGrid
dataclass
#
The surface and georeferencing every writer needs.
Built once by DEM.export so the no-data substitution and the corner arithmetic
happen in one place rather than in each of six writers.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
ndarray
|
2-D elevation array with the no-data sentinel already substituted for
|
nan_mask |
ndarray
|
|
geotransform |
tuple
|
The six-element GDAL geotransform. |
epsg |
int
|
EPSG code of the dataset CRS. |
no_data |
float
|
The no-data sentinel, written into the ASCII headers. |
Source code in src/digitalrivers/interop/export.py
cell_size
property
#
Cell size in CRS units, from the geotransform's x pitch.
shape
property
#
(rows, columns) of the grid.
xllcorner
property
#
X of the lower-left corner, the origin Arc-style headers count from.
yllcorner
property
#
Y of the lower-left corner.
The geotransform's y origin is the top edge and its y pitch is negative, so
the bottom edge is y0 + rows * dy.
write(target, grid, path, **kwargs)
#
Dispatch to the writer for target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
One of the keys of :data: |
required |
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path; each writer appends its own extension when missing. |
required |
**kwargs
|
Passed through to the writer. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
For an unknown |
Source code in src/digitalrivers/interop/export.py
write_gmsh(grid, path, **kwargs)
#
Write a minimal Gmsh .geo script describing the DEM's bounds.
The script is the grid extent as a rectangle with a uniform characteristic length
equal to the cell size — enough for gmsh -2 <path> to produce a mesh over the
footprint. It carries no elevations; the surface itself is not exported here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface whose bounds are written. |
required |
path
|
str
|
Output path; |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
write_hec_ras(grid, path, **kwargs)
#
Write a single-band float32 GeoTIFF for HEC-RAS Mapper.
Mapper wants the dataset CRS and a consistent geotransform, which is exactly what
Dataset.from_array(path=...) writes — the driver is resolved from the extension,
so .tif is appended when the caller did not supply it. Without that, a bare path
fails inside pyramids with a driver error that names neither this writer nor the
export target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path; |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
write_iber(grid, path, **kwargs)
#
Write an ASCII boundary file for Iber's pre-processor.
Iber ultimately wants a .dat mesh. Until mesh generation lands (P33) this writes
the grid as an uppercase-header ASCII file the pre-processor can import and refine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path; |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
write_lisflood_fp(grid, path, **kwargs)
#
Write an Arc ASCII grid, the canonical LISFLOOD-FP elevation input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path, used exactly as given. |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
write_sfincs(grid, path, **kwargs)
#
Write the SFINCS pair: a headerless .dep and its .msk companion.
.dep is row-major little-endian float32 with no header at all — SFINCS takes the
shape from its own control file. .msk is uint8, 0 where the source was
no-data and 1 elsewhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path; |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
write_tuflow(grid, path, **kwargs)
#
Write an ESRI floating-point grid: .flt binary plus .hdr text header.
The binary is row-major little-endian float32, top-left cell first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ExportGrid
|
The surface to write. |
required |
path
|
str
|
Output path; |
required |
**kwargs
|
Ignored; accepted so every writer shares one signature. |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/digitalrivers/interop/export.py
Sub-grid bathymetry#
Per-coarse-cell depth / wetted-fraction tables, the representation SFINCS and similar reduced-order solvers use to recover small-scale topography they do not resolve.
digitalrivers.interop.subgrid
#
Sub-grid bathymetry tables for reduced-order 2D models.
SFINCS and models like it run on a coarse grid but still want the small-scale topography
that grid cannot resolve. The compromise is a per-coarse-cell lookup table: for each
block of fine cells, what fraction of the block lies below each of n_bins depth
levels. The solver reads that table instead of resolving the topography, recovering most
of the storage and conveyance behaviour at coarse-grid cost.
subgrid_table is the array kernel; DEM.subgrid_bathymetry wraps it.
subgrid_table(elev, scale_factor, n_bins)
#
Build the per-coarse-cell depth / wetted-fraction table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elev
|
ndarray
|
2-D elevation array with |
required |
scale_factor
|
int
|
Fine cells per coarse cell along each axis. Must be >= 2. |
required |
n_bins
|
int
|
Number of depth bins per coarse cell. Must be >= 1. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
are omitted; a flat block reports |
DataFrame
|
bin edge sits at or above its single elevation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
For |
Source code in src/digitalrivers/interop/subgrid.py
ANUDEM-lite gap fill#
Relaxation gap-fill for a surface with holes — cloud shadow, survey gaps, vegetation occlusion. Two solvers: a Laplacian membrane, and a biharmonic plate that matches slope as well as value at the hole edge.
digitalrivers.interop.anudem
#
ANUDEM-lite: relaxation gap-fill for a surface with holes.
A pragmatic subset of Hutchinson (1989) covering the case that actually comes up — a DEM
with NaN holes from cloud shadow, survey gaps or vegetation occlusion — rather than
the full drainage-enforcing thin-plate spline.
Two solvers, both Gauss-Seidel sweeps holding the known cells fixed:
"laplacian"solves the 4-neighbour mean, the discrete form of a membrane stretched across the hole. Fast, and it cannot overshoot, but it meets the known surface with a visible kink: it matches values at the boundary without matching slopes."biharmonic"solves the plate equation instead, matching slope as well as value at the hole edge, so the seam disappears. Slower, and it can overshoot slightly. Implemented as damped Jacobi on the 13-point stencil; an earlier two-Laplacian approximation diverged for holes of 3x3 and larger.
relax_gaps is the array kernel; DEM.anudem_interpolate wraps it and records the
operation in the DEM's conditioning history.
relax_gaps(elev, mask=None, max_iter=200, tol=0.001, method='laplacian')
#
Fill the unknown cells of elev by relaxation, holding the known ones fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elev
|
ndarray
|
2-D elevation array; |
required |
mask
|
Optional bool array of the same shape. |
None
|
|
max_iter
|
int
|
Maximum relaxation sweeps. Defaults to 200. |
200
|
tol
|
float
|
Stop once the largest change in a sweep falls below this. Defaults to 1e-3. |
0.001
|
method
|
str
|
|
'laplacian'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
For an unknown |
Source code in src/digitalrivers/interop/anudem.py
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 | |