Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ examples/.ipynb_checkpoints
examples/tmp*
examples/*.png
examples/*.tif
examples/*.gpkg
examples/*.xml

# Sphinx documentation
docs/_build/
Expand Down
45 changes: 39 additions & 6 deletions examples/upscaling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
" flwdir = src.read(1)\n",
" crs = src.crs\n",
" extent = np.array(src.bounds)[[0, 2, 1, 3]]\n",
" prof = src.profile\n",
" flw = pyflwdir.from_array(\n",
" flwdir,\n",
" ftype=\"d8\",\n",
Expand Down Expand Up @@ -109,7 +110,7 @@
"metadata": {},
"outputs": [],
"source": [
"# get the contibuting unit area to each upscaled cell & accumulate\n",
"# get the contributing unit area to each upscaled cell & accumulate\n",
"subareas = flw.ucat_area(idxs_out=idxs_out, unit=\"km2\")[1]\n",
"uparea1 = flw1.accuflux(subareas)"
]
Expand Down Expand Up @@ -144,14 +145,46 @@
" gdfs=[(gdf_stream1, gdf_plt_kwds)], title=title, filename=f\"flw_upscale{s:2d}\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## save upscaled flow directions to file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# update the profile and write the upscaled flow direction to a new raster file\n",
"prof.update(\n",
" width=flw1.shape[1],\n",
" height=flw1.shape[0],\n",
" transform=flw1.transform,\n",
" nodata=247,\n",
Copy link

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using a hard-coded nodata value like 247. Consider using the original dataset's nodata value (e.g., prof['nodata'] or src.nodata) to maintain consistency and avoid magic numbers.

Suggested change
" nodata=247,\n",
" nodata=prof.get('nodata', 247),\n",

Copilot uses AI. Check for mistakes.
")\n",
"with rasterio.open(f\"rhine_d8_upscale{s}.tif\", \"w\", **prof) as src:\n",
" src.write(flw1.to_array(\"d8\"), 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# save upscaled network to a vector file\n",
"gdf_stream1.to_file(f\"rhine_d8_upscale{s}.gpkg\", driver=\"GPKG\")"
]
}
],
"metadata": {
"interpreter": {
"hash": "1d6565df54f631b3318aa7a4d6d8532e5ca49bf98056c57553f6cb4464998323"
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit ('pyflwdir': conda)",
"display_name": "default",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -164,7 +197,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.11.13"
}
},
"nbformat": 4,
Expand Down
Loading