diff --git a/.gitignore b/.gitignore index e5d1047..306aa97 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ src/ribasim_nl/tests/temp/ src/peilbeheerst_model/tests/temp/ src/peilbeheerst_model/*.html -src/peilbeheerst_model/*.ipynb src/peilbeheerst_model/*.code-workspace src/peilbeheerst_model/.vscode diff --git a/pixi.lock b/pixi.lock index c573279..ead4181 100644 --- a/pixi.lock +++ b/pixi.lock @@ -16026,30 +16026,6 @@ packages: - pkg:pypi/numba-celltree?source=conda-forge-mapping size: 33109 timestamp: 1721292112265 -- kind: conda - name: numpy - version: 2.0.1 - build: py312h1103770_0 - subdir: linux-64 - url: https://conda.anaconda.org/conda-forge/linux-64/numpy-2.0.1-py312h1103770_0.conda - sha256: 0746a37d09036b4164ac14dd1328dd4e449a038383aac1e25e2d5f3a691518da - md5: 9f444595d8d9682891f2f078fc19da43 - depends: - - __glibc >=2.17,<3.0.a0 - - libblas >=3.9.0,<4.0a0 - - libcblas >=3.9.0,<4.0a0 - - libgcc-ng >=12 - - liblapack >=3.9.0,<4.0a0 - - libstdcxx-ng >=12 - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - constrains: - - numpy-base <0a0 - license: BSD-3-Clause - purls: - - pkg:pypi/numpy?source=conda-forge-mapping - size: 8345605 - timestamp: 1721966364929 - kind: conda name: numpy version: 2.0.1 @@ -16075,30 +16051,6 @@ packages: - pkg:pypi/numpy?source=conda-forge-mapping size: 8345605 timestamp: 1721966364929 -- kind: conda - name: numpy - version: 2.0.1 - build: py312h49bc9c5_0 - subdir: win-64 - url: https://conda.anaconda.org/conda-forge/win-64/numpy-2.0.1-py312h49bc9c5_0.conda - sha256: 13b38db57cefbbea0cb6a44a5c75df8010480dc6200eda3491c8d203072d1675 - md5: e7fed4e2639f3a0d58bd8b2164059e8d - depends: - - libblas >=3.9.0,<4.0a0 - - libcblas >=3.9.0,<4.0a0 - - liblapack >=3.9.0,<4.0a0 - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - - ucrt >=10.0.20348.0 - - vc >=14.2,<15 - - vc14_runtime >=14.29.30139 - constrains: - - numpy-base <0a0 - license: BSD-3-Clause - purls: - - pkg:pypi/numpy?source=conda-forge-mapping - size: 6945867 - timestamp: 1721966986321 - kind: conda name: numpy version: 2.0.1 @@ -16124,29 +16076,6 @@ packages: - pkg:pypi/numpy?source=conda-forge-mapping size: 6945867 timestamp: 1721966986321 -- kind: conda - name: numpy - version: 2.0.1 - build: py312h8813227_0 - subdir: osx-64 - url: https://conda.anaconda.org/conda-forge/osx-64/numpy-2.0.1-py312h8813227_0.conda - sha256: 3f73ed4464e3dc639c875b6cbe86e8095f88afe047bdfdc3d4b4ae120dd830e8 - md5: 7f239fbf9d9355f86529a35af0b24d29 - depends: - - __osx >=10.13 - - libblas >=3.9.0,<4.0a0 - - libcblas >=3.9.0,<4.0a0 - - libcxx >=16 - - liblapack >=3.9.0,<4.0a0 - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - constrains: - - numpy-base <0a0 - license: BSD-3-Clause - purls: - - pkg:pypi/numpy?source=conda-forge-mapping - size: 7464264 - timestamp: 1721966235928 - kind: conda name: numpy version: 2.0.1 @@ -18445,30 +18374,6 @@ packages: - pkg:pypi/pysocks?source=conda-forge-mapping size: 18981 timestamp: 1661604969727 -- kind: conda - name: pytest - version: 8.3.2 - build: pyhd8ed1ab_0 - subdir: noarch - noarch: python - url: https://conda.anaconda.org/conda-forge/noarch/pytest-8.3.2-pyhd8ed1ab_0.conda - sha256: 72c84a3cd9fe82835a88e975fd2a0dbf2071d1c423ea4f79e7930578c1014873 - md5: e010a224b90f1f623a917c35addbb924 - depends: - - colorama - - exceptiongroup >=1.0.0rc8 - - iniconfig - - packaging - - pluggy <2,>=1.5 - - python >=3.8 - - tomli >=1 - constrains: - - pytest-faulthandler >=2 - license: MIT - purls: - - pkg:pypi/pytest?source=conda-forge-mapping - size: 257671 - timestamp: 1721923749407 - kind: conda name: pytest version: 8.3.2 @@ -19591,7 +19496,7 @@ packages: name: ribasim-lumping version: 0.1.0 path: src/ribasim_lumping - sha256: dec80735ccc9bf83d5a0805f5081c910c8aa81dc76abdb7052253a657ba034b3 + sha256: 25f734682e8e1d8bbcea37b6147101f264a13471b82341daf989834955d1a942 requires_dist: - contextily - geopandas @@ -19659,25 +19564,6 @@ packages: - pkg:pypi/rioxarray?source=conda-forge-mapping size: 51306 timestamp: 1721412091165 -- kind: conda - name: rpds-py - version: 0.19.1 - build: py312h2615798_0 - subdir: win-64 - url: https://conda.anaconda.org/conda-forge/win-64/rpds-py-0.19.1-py312h2615798_0.conda - sha256: 892407686805709a37a6dd29da06042f891a35774b25cee51368a29be9ccac6b - md5: 80bb17e18169ac455444b8167a105059 - depends: - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - - ucrt >=10.0.20348.0 - - vc >=14.2,<15 - - vc14_runtime >=14.29.30139 - license: MIT - purls: - - pkg:pypi/rpds-py?source=conda-forge-mapping - size: 206243 - timestamp: 1721862293173 - kind: conda name: rpds-py version: 0.19.1 @@ -19698,25 +19584,6 @@ packages: - pkg:pypi/rpds-py?source=conda-forge-mapping size: 206243 timestamp: 1721862293173 -- kind: conda - name: rpds-py - version: 0.19.1 - build: py312ha47ea1c_0 - subdir: osx-64 - url: https://conda.anaconda.org/conda-forge/osx-64/rpds-py-0.19.1-py312ha47ea1c_0.conda - sha256: dc5ce3a63deffc69263a8e8699e43ae64b45663ce3f39799c10b35524cc3e861 - md5: c54025057789a55e07d585e743fc8744 - depends: - - __osx >=10.13 - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - constrains: - - __osx >=10.13 - license: MIT - purls: - - pkg:pypi/rpds-py?source=conda-forge-mapping - size: 295442 - timestamp: 1721861174737 - kind: conda name: rpds-py version: 0.19.1 @@ -19737,26 +19604,6 @@ packages: - pkg:pypi/rpds-py?source=conda-forge-mapping size: 295442 timestamp: 1721861174737 -- kind: conda - name: rpds-py - version: 0.19.1 - build: py312hf008fa9_0 - subdir: linux-64 - url: https://conda.anaconda.org/conda-forge/linux-64/rpds-py-0.19.1-py312hf008fa9_0.conda - sha256: 931d84722857bfdc9c1bbf8acc9c3bcf9aa294d8d9b4f26015569a3a0fbabefd - md5: ebdebabe560c06a70bc41221b9606945 - depends: - - __glibc >=2.17,<3.0.a0 - - libgcc-ng >=12 - - python >=3.12,<3.13.0a0 - - python_abi 3.12.* *_cp312 - constrains: - - __glibc >=2.17 - license: MIT - purls: - - pkg:pypi/rpds-py?source=conda-forge-mapping - size: 333274 - timestamp: 1721861124399 - kind: conda name: rpds-py version: 0.19.1 diff --git a/src/peilbeheerst_model/01_parse_crossings.ipynb b/src/peilbeheerst_model/01_parse_crossings.ipynb new file mode 100644 index 0000000..026f976 --- /dev/null +++ b/src/peilbeheerst_model/01_parse_crossings.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "import pathlib\n", + "\n", + "import geopandas as gpd\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import pandas as pd\n", + "from IPython.core.display import HTML\n", + "from peilbeheerst_model import ParseCrossings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"waterschappen.json\") as f:\n", + " waterschap_data = json.load(f)\n", + "\n", + "print_df = {}\n", + "for waterschap, waterschap_struct in waterschap_data.items():\n", + " for funcname, func_args in waterschap_struct.items():\n", + " if funcname not in print_df:\n", + " print_df[funcname] = []\n", + " print_df[funcname].append(pd.Series(func_args, name=waterschap))\n", + "\n", + "for funcname, df in print_df.items():\n", + " display(HTML(f\"

Function {funcname}:

\"))\n", + " display(pd.DataFrame(df))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "for waterschap, waterschap_struct in waterschap_data.items():\n", + " print(f\"\\n{waterschap}...\")\n", + "\n", + " init_settings, crossing_settings = waterschap_struct.values()\n", + " init_settings[\"logfile\"] = pathlib.Path(init_settings[\"output_path\"]).with_suffix(\"\").with_suffix(\".log\")\n", + "\n", + " if waterschap not in [\"HHNK\"]:\n", + " continue\n", + "\n", + " # if pathlib.Path(init_settings[\"output_path\"]).exists() and \"crossings_hydroobject\" in fiona.listlayers(init_settings[\"output_path\"]):\n", + " # continue\n", + "\n", + " # Crossings class initializeren\n", + " cross = ParseCrossings(**init_settings)\n", + "\n", + " # Crossings bepalen en wegschrijven\n", + " if crossing_settings[\"filterlayer\"] is None:\n", + " df_hydro = cross.find_crossings_with_peilgebieden(\"hydroobject\", **crossing_settings)\n", + " cross.write_crossings(df_hydro)\n", + " else:\n", + " df_hydro, df_dsf, df_hydro_dsf = cross.find_crossings_with_peilgebieden(\"hydroobject\", **crossing_settings)\n", + " cross.write_crossings(df_hydro, crossing_settings[\"filterlayer\"], df_dsf, df_hydro_dsf)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4", + "metadata": {}, + "outputs": [], + "source": [ + "plt.close(\"all\")\n", + "fig1, ax1 = plt.subplots(figsize=(12, 7.4), dpi=100)\n", + "fig2, ax2 = plt.subplots(figsize=(12, 7.4), dpi=100)\n", + "\n", + "for ax in [ax1, ax2]:\n", + " ax.spines[\"top\"].set_visible(False)\n", + " ax.spines[\"right\"].set_visible(False)\n", + " ax.spines[\"left\"].set_visible(False)\n", + " ax.spines[\"bottom\"].set_color(\"#dddddd\")\n", + " ax.tick_params(bottom=False, left=False)\n", + " ax.yaxis.grid(True, color=\"#eeeeee\")\n", + " ax.xaxis.grid(False)\n", + "\n", + "waterschappen = []\n", + "network_results = {\"Basins\": [], \"Edges\": [], \"Peilgebieden\": []}\n", + "# reduction_results = {\"initial\": [], \"in_use\": [], \"agg_links_in_use\": [], \"agg_areas_in_use\": []}\n", + "reduction_results = {\"in_use\": [], \"agg_links_in_use\": [], \"agg_areas_in_use\": []}\n", + "for waterschap, waterschap_struct in waterschap_data.items():\n", + " init_settings, crossing_settings = waterschap_struct.values()\n", + " df = gpd.read_file(init_settings[\"output_path\"], layer=\"crossings_hydroobject_filtered\")\n", + "\n", + " basins, edges, peilgebieden = None, None, None\n", + " init_cross, cross_inuse, cross_agglinks, cross_aggareas = None, None, None, None\n", + " try:\n", + " sub_df = df[df.agg_areas_in_use].copy()\n", + " all_nodes = np.hstack([sub_df.agg_area_from.to_numpy(), sub_df.agg_area_to.to_numpy()])\n", + " basins = len(np.unique(all_nodes[~pd.isna(all_nodes)]))\n", + " edges = len(sub_df) * 2\n", + " all_peilgebieden = np.hstack([sub_df.peilgebied_from.to_numpy(), sub_df.peilgebied_to.to_numpy()])\n", + " peilgebieden = len(np.unique(all_peilgebieden[~pd.isna(all_peilgebieden)]))\n", + "\n", + " init_cross = len(df)\n", + " cross_inuse = len(df[df.in_use])\n", + " cross_agglinks = len(df[df.agg_links_in_use])\n", + " cross_aggareas = len(df[df.agg_areas_in_use])\n", + " except Exception as e:\n", + " print(f\"{waterschap=}, {e=}\")\n", + "\n", + " # reduction_results[\"initial\"].append(init_cross)\n", + " reduction_results[\"in_use\"].append(cross_inuse)\n", + " reduction_results[\"agg_links_in_use\"].append(cross_agglinks)\n", + " reduction_results[\"agg_areas_in_use\"].append(cross_aggareas)\n", + "\n", + " network_results[\"Basins\"].append(basins)\n", + " network_results[\"Edges\"].append(edges)\n", + " network_results[\"Peilgebieden\"].append(peilgebieden)\n", + " waterschappen.append(waterschap)\n", + "\n", + "colours = [\"#0C3B5D\", \"#3EC1CD\", \"#EF3A4C\", \"#FCB94D\"]\n", + "\n", + "x1 = np.arange(len(waterschappen))\n", + "width = 1 / (1 + len(network_results))\n", + "multiplier = 0\n", + "for multiplier, (attribute, measurement) in enumerate(network_results.items()):\n", + " offset = width * multiplier\n", + " rects = ax1.bar(x1 + offset, measurement, width, label=attribute, color=colours[multiplier])\n", + " # ax1.bar_label(rects, padding=3)\n", + "ax1.set_axisbelow(True)\n", + "ax1.set_xticks(x1 + width, waterschappen, rotation=45)\n", + "ax1.legend(loc=\"upper left\", ncols=len(network_results))\n", + "\n", + "\n", + "x2 = np.arange(len(waterschappen))\n", + "width = 1 / (1 + len(reduction_results))\n", + "for multiplier, (attribute, measurement) in enumerate(reduction_results.items()):\n", + " offset = width * multiplier\n", + " rects = ax2.bar(x2 + offset, measurement, width, label=attribute, color=colours[multiplier])\n", + " # ax2.bar_label(rects, padding=3)\n", + "ax2.set_axisbelow(True)\n", + "ax2.set_xticks(x2 + width, waterschappen, rotation=45)\n", + "ax2.legend(loc=\"upper left\", ncols=len(reduction_results))\n", + "\n", + "fig1.tight_layout()\n", + "fig2.tight_layout()\n", + "\n", + "fig1.savefig(\"network_results.jpeg\", bbox_inches=\"tight\")\n", + "fig2.savefig(\"reduction_results.jpeg\", bbox_inches=\"tight\")\n", + "\n", + "display(pd.DataFrame(reduction_results, index=waterschappen))\n", + "display(pd.DataFrame(network_results, index=waterschappen))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/01_test_parse_crossings.ipynb b/src/peilbeheerst_model/01_test_parse_crossings.ipynb new file mode 100644 index 0000000..4df0fd6 --- /dev/null +++ b/src/peilbeheerst_model/01_test_parse_crossings.ipynb @@ -0,0 +1,599 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import pathlib\n", + "import warnings\n", + "\n", + "import geopandas as gpd\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import shapely.geometry\n", + "import shapely.validation\n", + "import tqdm.auto as tqdm\n", + "from IPython.core.display import HTML\n", + "from matplotlib.patches import Polygon\n", + "from peilbeheerst_model import ParseCrossings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "polygons = {\n", + " \"perfect fit\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ]\n", + " },\n", + " \"perfect fit star 1\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (1, 0), (2, 1.5), (1.5, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(3, 0), (4, 0), (4, 2), (2.5, 2), (2, 1.5)]),\n", + " shapely.geometry.Polygon([(1, 0), (3, 0), (2, 1.5)]),\n", + " shapely.geometry.Polygon([(1.5, 2), (2.5, 2), (2, 1.5)]),\n", + " ]\n", + " },\n", + " \"perfect fit star 2\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (1, 0), (2, 0.5), (1.5, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(3, 0), (4, 0), (4, 2), (2.5, 2), (2, 0.5)]),\n", + " shapely.geometry.Polygon([(1, 0), (3, 0), (2, 0.5)]),\n", + " shapely.geometry.Polygon([(1.5, 2), (2.5, 2), (2, 0.5)]),\n", + " ]\n", + " },\n", + " \"perfect fit on edge\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 1.5), (0, 1.5)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ]\n", + " },\n", + " \"narrow gap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2.1, 0), (4, 0), (4, 2), (2.1, 2)]),\n", + " ]\n", + " },\n", + " \"wide gap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(3, 0), (4, 0), (4, 2), (3, 2)]),\n", + " ]\n", + " },\n", + " \"narrow overlap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(1.9, 0), (4, 0), (4, 2), (1.9, 2)]),\n", + " ]\n", + " },\n", + " \"wide overlap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(1, 0), (4, 0), (4, 2), (1, 2)]),\n", + " ]\n", + " },\n", + " \"single cross wide\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " ]\n", + " },\n", + " \"single cross narrow\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (3.4, 0), (3.4, 2), (0, 2)]),\n", + " ]\n", + " },\n", + " \"single cross at edge\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (3.5, 0), (3.5, 2), (0, 2)]),\n", + " ]\n", + " },\n", + " \"single cross on edge\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 1.5), (0, 1.5)]),\n", + " ]\n", + " },\n", + " \"perfect fit with complete overlap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ]\n", + " },\n", + " \"single cross wide with complete overlap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " ]\n", + " },\n", + " \"single cross at edge with complete overlap\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (3.5, 0), (3.5, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0, 0), (3.5, 0), (3.5, 2), (0, 2)]),\n", + " ]\n", + " },\n", + " \"polygon within polygon 1\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (3.0, 0), (3.0, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0.1, 0.1), (2.8, 0.1), (2.8, 1.9), (0.1, 1.9)]),\n", + " ]\n", + " },\n", + " \"polygon within polygon 2\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (3.4, 0), (3.4, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0.1, 0.1), (2.8, 0.1), (2.8, 1.9), (0.1, 1.9)]),\n", + " ]\n", + " },\n", + " \"polygon within polygon 3\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (4.0, 0), (4.0, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(0.1, 0.1), (2.8, 0.1), (2.8, 1.9), (0.1, 1.9)]),\n", + " ]\n", + " },\n", + " \"polygon butterfly 1a\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.5), (4.0, 0.0), (4.0, 2.0), (2.0, 0.5), (0.0, 2.0)])\n", + " ),\n", + " ]\n", + " },\n", + " \"polygon butterfly 1b\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.5), (4.0, 0.0), (4.0, 2.0), (2.0, 0.5), (0.0, 2.0)])\n", + " ),\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.5), (4.0, 0.0)]),\n", + " ]\n", + " },\n", + " \"polygon butterfly 2a\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.4), (4.0, 0.0), (4.0, 2.0), (2.0, 0.4), (0.0, 2.0)])\n", + " ),\n", + " ]\n", + " },\n", + " \"polygon butterfly 2b\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.4), (4.0, 0.0), (4.0, 2.0), (2.0, 0.4), (0.0, 2.0)])\n", + " ),\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.4), (4.0, 0.0)]),\n", + " ]\n", + " },\n", + " \"polygon butterfly 2c\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.6), (4.0, 0.0), (4.0, 2.0), (2.0, 0.6), (0.0, 2.0)])\n", + " ),\n", + " ]\n", + " },\n", + " \"polygon butterfly 2d\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.6), (4.0, 0.0), (4.0, 2.0), (2.0, 0.6), (0.0, 2.0)])\n", + " ),\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.6), (4.0, 0.0)]),\n", + " ]\n", + " },\n", + " \"polygon butterfly 2e\": {\n", + " \"peilgebieden\": [\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.55), (4.0, 0.0), (4.0, 2.0), (2.0, 0.55), (0.0, 2.0)])\n", + " ),\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.55), (4.0, 0.0)]),\n", + " ]\n", + " },\n", + " \"polygon butterfly 3a\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.5), (0.0, 2.0)]),\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (4.0, 0.0), (2.0, 0.5), (4.0, 2.0), (0.0, 2.0), (2.0, 0.5)])\n", + " ),\n", + " ]\n", + " },\n", + " \"polygon butterfly 3b\": {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0.0, 0.0), (2.0, 0.4), (0.0, 2.0)]),\n", + " shapely.validation.make_valid(\n", + " shapely.geometry.Polygon([(0.0, 0.0), (4.0, 0.0), (2.0, 0.4), (4.0, 2.0), (0.0, 2.0), (2.0, 0.4)])\n", + " ),\n", + " ]\n", + " },\n", + "}\n", + "\n", + "linelist = [\n", + " shapely.geometry.LineString([(0.5, 0.5), (3.5, 0.5)]),\n", + " shapely.geometry.LineString([(0.5, 0.7), (2.0, 0.7)]),\n", + " shapely.geometry.LineString([(2.0, 0.7), (3.5, 0.7)]),\n", + " shapely.geometry.LineString([(0.5, 0.9), (2.0, 0.9)]),\n", + " shapely.geometry.LineString([(3.5, 0.9), (2.0, 0.9)]),\n", + " shapely.geometry.LineString([(3.8, 0.9), (3.5, 0.9)]),\n", + " shapely.geometry.LineString([(3.5, 1.5), (0.5, 1.5)]),\n", + "]\n", + "\n", + "filterlist = [\n", + " shapely.geometry.LineString([(1.0, 0.7), (2.0, 0.7)]),\n", + " shapely.geometry.LineString([(2.0, 0.7), (3.0, 0.7)]),\n", + " shapely.geometry.LineString([(1.0, 0.9), (3.0, 0.9)]),\n", + "]\n", + "\n", + "\n", + "nofilter = polygons.copy()\n", + "withfilter = polygons.copy()\n", + "for testname, options in nofilter.items():\n", + " options[\"hydroobjecten\"] = linelist.copy()\n", + " nofilter[testname] = options.copy()\n", + "\n", + " options[\"duikersifonhevel\"] = filterlist.copy()\n", + " withfilter[testname] = options.copy()\n", + "\n", + "\n", + "nofilter[\"driehoek 1a\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(0.5, 0.5), (2, 1)]),\n", + " shapely.geometry.LineString([(0.5, 1.5), (2, 1)]),\n", + " shapely.geometry.LineString([(2, 1), (3.5, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"driehoek 1b\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(0.5, 0.5), (2.1, 1)]),\n", + " shapely.geometry.LineString([(0.5, 1.5), (2.1, 1)]),\n", + " shapely.geometry.LineString([(2.1, 1), (3.5, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"driehoek 1c\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(0.5, 0.5), (1.9, 1)]),\n", + " shapely.geometry.LineString([(0.5, 1.5), (1.9, 1)]),\n", + " shapely.geometry.LineString([(1.9, 1), (3.5, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"driehoek 2\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(3.5, 1.5), (2, 1)]),\n", + " shapely.geometry.LineString([(3.5, 0.5), (2, 1)]),\n", + " shapely.geometry.LineString([(2, 1), (0.5, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"driehoek 3\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (4, 0), (4, 2), (2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(3.5, 1.5), (2, 1)]),\n", + " shapely.geometry.LineString([(3.5, 0.5), (2, 1)]),\n", + " shapely.geometry.LineString([(0.5, 0.5), (2, 1)]),\n", + " ],\n", + "}\n", + "nofilter[\"volgorde groep 1\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (2.2, 0), (2.2, 2), (2, 2)]),\n", + " shapely.geometry.Polygon([(2.2, 0), (4, 0), (4, 2), (2.2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(0.5, 0.5), (2.1, 0.5)]),\n", + " shapely.geometry.LineString([(2.1, 0.5), (3.5, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"volgorde groep 2\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (2.2, 0), (2.2, 2), (2, 2)]),\n", + " shapely.geometry.Polygon([(2.2, 0), (4, 0), (4, 2), (2.2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(2.1, 0.5), (0.5, 0.5)]),\n", + " shapely.geometry.LineString([(3.5, 0.5), (2.1, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"volgorde groep 3\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (2.2, 0), (2.2, 2), (2, 2)]),\n", + " shapely.geometry.Polygon([(2.2, 0), (4, 0), (4, 2), (2.2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(0.5, 0.5), (2.1, 0.5)]),\n", + " shapely.geometry.LineString([(3.5, 0.5), (2.1, 0.5)]),\n", + " ],\n", + "}\n", + "nofilter[\"volgorde groep 4\"] = {\n", + " \"peilgebieden\": [\n", + " shapely.geometry.Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),\n", + " shapely.geometry.Polygon([(2, 0), (2.2, 0), (2.2, 2), (2, 2)]),\n", + " shapely.geometry.Polygon([(2.2, 0), (4, 0), (4, 2), (2.2, 2)]),\n", + " ],\n", + " \"hydroobjecten\": [\n", + " shapely.geometry.LineString([(2.1, 0.5), (0.5, 0.5)]),\n", + " shapely.geometry.LineString([(2.1, 0.5), (3.5, 0.5)]),\n", + " ],\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "testdir = pathlib.Path(\"tests/data\")\n", + "if not testdir.exists():\n", + " testdir.mkdir()\n", + "\n", + "for testlist in tqdm.tqdm([nofilter, withfilter]):\n", + " for test_name, options in testlist.items():\n", + " polyid = [f\"poly_{i+1}\" for i in range(len(options[\"peilgebieden\"]))]\n", + " polywl = [float(i + 1) for i in range(len(options[\"peilgebieden\"]))]\n", + " # df_peil = gpd.GeoDataFrame(dict(globalid=polyid, geometry=options[\"peilgebieden\"]), crs=\"epsg:28992\")\n", + " # df_streef = gpd.GeoDataFrame(dict(globalid=polyid, waterhoogte=polywl, geometry=len(options[\"peilgebieden\"]) * [None]), crs=\"epsg:28992\")\n", + " # lineid = [f\"line_{i+1}\" for i in range(len(options[\"hydroobjecten\"]))]\n", + " # df_hydro = gpd.GeoDataFrame(dict(globalid=lineid, geometry=options[\"hydroobjecten\"]), crs=\"epsg:28992\")\n", + "\n", + " df_peil = gpd.GeoDataFrame({\"globalid\": polyid, \"geometry\": options[\"peilgebieden\"]}, crs=\"epsg:28992\")\n", + " df_streef = gpd.GeoDataFrame(\n", + " {\"globalid\": polyid, \"waterhoogte\": polywl, \"geometry\": len(options[\"peilgebieden\"]) * [None]},\n", + " crs=\"epsg:28992\",\n", + " )\n", + "\n", + " lineid = [f\"line_{i+1}\" for i in range(len(options[\"hydroobjecten\"]))]\n", + " df_hydro = gpd.GeoDataFrame({\"globalid\": lineid, \"geometry\": options[\"hydroobjecten\"]}, crs=\"epsg:28992\")\n", + "\n", + " if \"duikersifonhevel\" not in options:\n", + " # Empty filter\n", + " gpkg_path1 = testdir.joinpath(f\"nofilter_{test_name}.gpkg\")\n", + " df_hydro.to_file(gpkg_path1, layer=\"hydroobject\")\n", + " df_peil.to_file(gpkg_path1, layer=\"peilgebied\")\n", + " df_streef.to_file(gpkg_path1, layer=\"streefpeil\")\n", + " with warnings.catch_warnings():\n", + " warnings.simplefilter(action=\"ignore\", category=UserWarning)\n", + " gpd.GeoDataFrame(columns=[\"globalid\", \"geometry\"]).to_file(gpkg_path1, layer=\"stuw\")\n", + " gpd.GeoDataFrame(columns=[\"globalid\", \"geometry\"]).to_file(gpkg_path1, layer=\"gemaal\")\n", + " gpd.GeoDataFrame(columns=[\"globalid\", \"geometry\"]).to_file(gpkg_path1, layer=\"duikersifonhevel\")\n", + " else:\n", + " # With filter\n", + " gpkg_path2 = testdir.joinpath(f\"withfilter_{test_name}.gpkg\")\n", + " df_hydro.to_file(gpkg_path2, layer=\"hydroobject\")\n", + " df_peil.to_file(gpkg_path2, layer=\"peilgebied\")\n", + " df_streef.to_file(gpkg_path2, layer=\"streefpeil\")\n", + " polyfl = [f\"dsh_{i+1}\" for i in range(len(options[\"duikersifonhevel\"]))]\n", + " # df_filter = gpd.GeoDataFrame(dict(globalid=polyfl, geometry=options[\"duikersifonhevel\"]), crs=\"epsg:28992\")\n", + " df_filter = gpd.GeoDataFrame(\n", + " {\"globalid\": polyfl, \"geometry\": options[\"duikersifonhevel\"]}, crs=\"epsg:28992\"\n", + " )\n", + "\n", + " df_filter.to_file(gpkg_path2, layer=\"duikersifonhevel\")\n", + " with warnings.catch_warnings():\n", + " warnings.simplefilter(action=\"ignore\", category=UserWarning)\n", + " gpd.GeoDataFrame(columns=[\"globalid\", \"geometry\"]).to_file(gpkg_path2, layer=\"stuw\")\n", + " gpd.GeoDataFrame(columns=[\"globalid\", \"geometry\"]).to_file(gpkg_path2, layer=\"gemaal\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "def make_plot(df_peil, df_hydro, df_streef, df_filter, df_crossings):\n", + " plt.close(\"all\")\n", + " fig, ax = plt.subplots(figsize=(8, 5), dpi=100)\n", + "\n", + " # old_len = len(df_peil)\n", + " dfp = df_peil.copy().explode(index_parts=True)\n", + " dfs = df_streef.set_index(\"globalid\", inplace=False)\n", + " for i, row in enumerate(dfp.itertuples()):\n", + " coords = row.geometry.exterior.coords\n", + " # if old_len == len(dfp):\n", + " xtext = row.geometry.centroid.x - 0.5\n", + " ytext = row.geometry.centroid.y\n", + " # else:\n", + " # xtext = coords[0][0] + 0.05\n", + " # ytext = coords[0][1]\n", + " if (i % 2) == 0:\n", + " ytext += 0.05\n", + " else:\n", + " ytext -= 0.05\n", + "\n", + " ax.text(xtext, ytext, f\"{row.globalid}, wl={dfs.waterhoogte.at[row.globalid]}m\", alpha=0.5)\n", + " ax.add_patch(Polygon(coords, alpha=0.5, lw=1, facecolor=\"powderblue\", edgecolor=\"skyblue\"))\n", + "\n", + " dfh = df_hydro.explode(index_parts=True)\n", + " for row in dfh.itertuples():\n", + " coords = np.array(row.geometry.coords)\n", + " x, y = coords[:, 0], coords[:, 1]\n", + " offset = row.geometry.interpolate(0.1, normalized=True)\n", + " ax.arrow(\n", + " x[0],\n", + " y[0],\n", + " offset.x - x[0],\n", + " offset.y - y[0],\n", + " shape=\"full\",\n", + " lw=0,\n", + " length_includes_head=True,\n", + " head_width=0.05,\n", + " color=\"steelblue\",\n", + " )\n", + " ax.text(offset.x, offset.y + 0.05, row.globalid)\n", + " ax.plot(x, y, marker=\".\", markersize=5, lw=1, color=\"steelblue\")\n", + "\n", + " for row in df_crossings.itertuples():\n", + " if row.crossing_type == \"-10\":\n", + " ax.plot(row.geometry.x, row.geometry.y, marker=\"s\", markersize=5, color=\"olivedrab\")\n", + " elif row.crossing_type == \"00\":\n", + " ax.plot(row.geometry.x, row.geometry.y, marker=\"s\", markersize=5, color=\"indianred\")\n", + " else:\n", + " print(f\"{row.crossing_type=} not implemented\")\n", + " ax.plot(row.geometry.x, row.geometry.y, marker=\"s\", markersize=5, color=\"yellow\")\n", + "\n", + " dff = df_filter.explode(index_parts=True)\n", + " for row in dff.itertuples():\n", + " coords = np.array(row.geometry.coords)\n", + " x, y = coords[:, 0], coords[:, 1]\n", + " ax.plot(x, y, marker=\".\", markersize=5, lw=2, alpha=0.3, color=\"purple\")\n", + "\n", + " ax.set_xlim([-0.1, 4.1])\n", + " ax.set_ylim([-0.1, 2.1])\n", + " ax.set_aspect(\"equal\")\n", + " fig.tight_layout()\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4", + "metadata": {}, + "outputs": [], + "source": [ + "for i, gpkg_path in enumerate(sorted(testdir.glob(\"nofilter_*.gpkg\"))):\n", + " if gpkg_path.is_file() and gpkg_path.suffix == \".gpkg\":\n", + " # if \"butterfly 3b\" not in gpkg_path.stem and \"polygon within polygon 1\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"perfect fit on edge\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"polygon within polygon 1\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"narrow gap\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"driehoek\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"nofilter_narrow gap\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"volgorde groep\" not in gpkg_path.stem and \"nofilter_polygon butterfly 2e\" not in gpkg_path.stem and \"nofilter_perfect fit star 2\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"nofilter_polygon butterfly 2e\" not in gpkg_path.stem and \"nofilter_perfect fit star 2\" not in gpkg_path.stem:\n", + " # continue\n", + "\n", + " display(HTML(f\"

Test {i+1:02d}: {gpkg_path.stem}

\"))\n", + " cross = ParseCrossings(gpkg_path, disable_progress=True, show_log=True)\n", + " df_crossings = cross.find_crossings_with_peilgebieden(\n", + " \"hydroobject\", group_stacked=True, filterlayer=None, agg_links=False\n", + " )\n", + " df_crossings_valid = df_crossings[df_crossings.in_use].copy()\n", + "\n", + " test_output = df_crossings_valid.copy()\n", + " test_output[\"geom_x\"] = np.round(test_output.geometry.x, 8)\n", + " test_output[\"geom_y\"] = np.round(test_output.geometry.y, 8)\n", + " test_output = test_output.drop(columns=\"geometry\", inplace=False)\n", + "\n", + " # Make static test output\n", + " test_output.to_csv(testdir.joinpath(f\"output_{gpkg_path.stem}.csv\"), index=False)\n", + "\n", + " display(df_crossings)\n", + " display(df_crossings_valid)\n", + " make_plot(\n", + " cross.df_gpkg[\"peilgebied\"],\n", + " cross.df_gpkg[\"hydroobject\"],\n", + " cross.df_gpkg[\"streefpeil\"],\n", + " cross.df_gpkg[\"duikersifonhevel\"],\n", + " df_crossings_valid,\n", + " )\n", + " display(HTML(\"
\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "for i, gpkg_path in enumerate(sorted(testdir.glob(\"withfilter_*.gpkg\"))):\n", + " if gpkg_path.is_file() and gpkg_path.suffix == \".gpkg\":\n", + " # if \"withfilter_polygon butterfly 1a\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"withfilter_narrow gap\" not in gpkg_path.stem:\n", + " # continue\n", + " # if \"scheldestromen\" not in gpkg_path.stem:\n", + " # continue\n", + "\n", + " display(HTML(f\"

Test {i+1:02d}: {gpkg_path.stem}

\"))\n", + "\n", + " cross = ParseCrossings(gpkg_path, disable_progress=True, show_log=True)\n", + " _, df_filter, df_crossings = cross.find_crossings_with_peilgebieden(\n", + " \"hydroobject\", group_stacked=True, filterlayer=\"duikersifonhevel\", agg_links=False\n", + " )\n", + " df_crossings_valid = df_crossings[df_crossings.in_use].copy()\n", + "\n", + " test_output = df_crossings_valid.copy()\n", + " test_output[\"geom_x\"] = np.round(test_output.geometry.x, 8)\n", + " test_output[\"geom_y\"] = np.round(test_output.geometry.y, 8)\n", + " test_output = test_output.drop(columns=\"geometry\", inplace=False)\n", + "\n", + " # Make static test output\n", + " test_output.to_csv(testdir.joinpath(f\"output_{gpkg_path.stem}.csv\"), index=False)\n", + "\n", + " display(df_crossings)\n", + " display(df_filter)\n", + " display(df_crossings_valid)\n", + " if \"scheldestromen\" not in gpkg_path.stem:\n", + " make_plot(\n", + " cross.df_gpkg[\"peilgebied\"],\n", + " cross.df_gpkg[\"hydroobject\"],\n", + " cross.df_gpkg[\"streefpeil\"],\n", + " cross.df_gpkg[\"duikersifonhevel\"],\n", + " df_crossings_valid,\n", + " )\n", + " display(HTML(\"
\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/01b_ad_krw_to_peilgebieden.ipynb b/src/peilbeheerst_model/01b_ad_krw_to_peilgebieden.ipynb new file mode 100644 index 0000000..edb465e --- /dev/null +++ b/src/peilbeheerst_model/01b_ad_krw_to_peilgebieden.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "import pathlib\n", + "\n", + "import geopandas as gpd\n", + "import pandas as pd\n", + "from IPython.core.display import HTML\n", + "from peilbeheerst_model import ParseCrossings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"waterschappen.json\") as f:\n", + " waterschap_data = json.load(f)\n", + "\n", + "print_df = {}\n", + "for waterschap, waterschap_struct in waterschap_data.items():\n", + " for funcname, func_args in waterschap_struct.items():\n", + " if funcname not in print_df:\n", + " print_df[funcname] = []\n", + " print_df[funcname].append(pd.Series(func_args, name=waterschap))\n", + "\n", + "for funcname, df in print_df.items():\n", + " display(HTML(f\"

Function {funcname}:

\"))\n", + " display(pd.DataFrame(df))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "for waterschap, waterschap_struct in waterschap_data.items():\n", + " print(f\"\\n{waterschap}...\")\n", + "\n", + " init_settings, crossing_settings = waterschap_struct.values()\n", + " gpkg = pathlib.Path(init_settings[\"output_path\"])\n", + " if not gpkg.exists():\n", + " raise ValueError(gpkg)\n", + "\n", + " df_peilgebieden = gpd.read_file(gpkg, layer=\"peilgebied\")\n", + " org_shape = df_peilgebieden.shape\n", + " df_peilgebieden = ParseCrossings._make_valid_2dgeom(df_peilgebieden)\n", + "\n", + " df_peilgebieden = ParseCrossings.add_krw_to_peilgebieden(\n", + " df_peilgebieden,\n", + " init_settings[\"krw_path\"],\n", + " init_settings[\"krw_column_id\"],\n", + " init_settings[\"krw_column_name\"],\n", + " init_settings[\"krw_min_overlap\"],\n", + " \",\",\n", + " )\n", + "\n", + " assert df_peilgebieden.shape[0] == org_shape[0]\n", + " df_peilgebieden.to_file(gpkg, layer=\"peilgebied\")\n", + " print(gpkg)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/02_crossings_to_ribasim_notebook.ipynb b/src/peilbeheerst_model/02_crossings_to_ribasim_notebook.ipynb new file mode 100644 index 0000000..92082cb --- /dev/null +++ b/src/peilbeheerst_model/02_crossings_to_ribasim_notebook.ipynb @@ -0,0 +1,1195 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "import warnings\n", + "from pathlib import Path\n", + "\n", + "import pandas as pd\n", + "import ribasim\n", + "from crossings_to_ribasim import *\n", + "from ribasim import Model\n", + "\n", + "pd.set_option(\"display.max_columns\", None)\n", + "warnings.filterwarnings(\"ignore\") # Let op!!!!!!!!!!!!!! Waarschuwingen worden genegeerd\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "ribasim.__version__\n", + "\n", + "\n", + "# load ribasim_nl\n", + "module_path = Path.cwd() / \"../ribasim_nl/\"\n", + "sys.path.append(str(module_path))\n", + "\n", + "# load crossings_to_ribasim.py\n", + "sys.path.append(\"peilbeheerst_model\")" + ] + }, + { + "cell_type": "markdown", + "id": "1", + "metadata": {}, + "source": [ + "# Amstel, Gooi en Vecht" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"AmstelGooienVecht\",\n", + " \"modelname\": \"20240417_samenwerkdag\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/AmstelGooienVecht/AGV.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/AmstelGooienVecht/agv_crossings_v05.gpkg\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/AGV/AGV_shortest_path.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(model, post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "3", + "metadata": {}, + "source": [ + "# Delfland" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"Delfland\",\n", + " \"modelname\": \"20240423_omgedraaid\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Delfland/Delfland.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Delfland/delfland_crossings_v08.gpkg\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Delfland/Delfland_shortest_path.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "# Hollandse Delta" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"HollandseDelta\",\n", + " \"modelname\": \"20240417_samenwerkdag\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Hollandse_Delta/HD.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Hollandse_Delta/hd_crossings_v06.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Hollandse_Delta/HD_shortest_path.gpkg\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "# Hollands Noorderkwartier" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"HollandsNoorderkwartier\",\n", + " \"modelname\": \"20240502\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/HHNK/Noorderkwartier.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/HHNK/hhnk_crossings_v26.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/HHNK/HHNK_shortest_path.gpkg\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "edges = waterboard.change_boezems_manually(edges)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(model=model, post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "# Rijnland" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"Rijnland\",\n", + " \"modelname\": \"20240414_aggregated\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Rijnland/Rijnland.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Rijnland/rijnland_crossings_v04.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Rijnland/Rijnland_shortest_path.gpkg\",\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings, model=model)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "12", + "metadata": {}, + "source": [ + "# Rivierenland" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"Rivierenland\",\n", + " \"modelname\": \"20240402_bug_fix\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/WSRL/WSRL.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/WSRL/wsrl_crossings_v06.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/WSRL/WSRL_shortest_path.gpkg\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "# Scheldestromen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"Scheldestromen\",\n", + " \"modelname\": \"20240417_samenwerkdag\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Scheldestromen/Scheldestromen.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Scheldestromen/scheldestromen_crossings_v02.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Scheldestromen/Scheldestromen_shortest_path.gpkg\",\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(post_processed_data=post_processed_data, crossings=crossings, model=model)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "# stop\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "# Schieland en de Krimpenerwaard" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"SchielandendeKrimpenerwaard\",\n", + " \"modelname\": \"20240429_check\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/HHSK/HHSK.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/HHSK/hhsk_crossings_v04.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_boezem\": \"../../../../Data_shortest_path/HHSK/HHSK_shortest_path.gpkg\",\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(model=model, post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "model.tabulated_rating_curve.static" + ] + }, + { + "cell_type": "markdown", + "id": "19", + "metadata": {}, + "source": [ + "# Wetterskip" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"WetterskipFryslan\",\n", + " \"modelname\": \"20240417_samenwerkdag\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Wetterskip/Wetterskip.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Wetterskip/wetterskip_crossings_v06.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Wetterskip/Wetterskip_shortest_path.gpkg\",\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True, ############################ LET OP\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(model=model, post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "markdown", + "id": "21", + "metadata": {}, + "source": [ + "# Zuiderzeeland" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22", + "metadata": {}, + "outputs": [], + "source": [ + "model_characteristics = {\n", + " # model description\n", + " \"waterschap\": \"Zuiderzeeland\",\n", + " \"modelname\": \"20240417_samenwerkdag\",\n", + " \"modeltype\": \"boezemmodel\",\n", + " # define paths\n", + " \"path_postprocessed_data\": r\"../../../../Data_postprocessed/Waterschappen/Zuiderzeeland/Zuiderzeeland.gpkg\",\n", + " \"path_crossings\": \"../../../../Data_crossings/Zuiderzeeland/zzl_crossings_v05.gpkg\",\n", + " \"path_Pdrive\": None,\n", + " \"path_goodcloud_password\": \"../../../../Data_overig/password_goodcloud.txt\",\n", + " \"path_boezem\": \"../../../../Data_shortest_path/Zuiderzeeland/Zuiderzeeland_shortest_path.gpkg\",\n", + " # apply filters\n", + " \"crossings_layer\": \"crossings_hydroobject_filtered\",\n", + " \"in_use\": True,\n", + " \"agg_links_in_use\": True,\n", + " \"agg_areas_in_use\": True,\n", + " \"aggregation\": True,\n", + " # data storage settings\n", + " \"write_Pdrive\": False,\n", + " \"write_Zdrive\": True,\n", + " \"write_goodcloud\": True,\n", + " \"write_checks\": True,\n", + " \"write_symbology\": True,\n", + " # numerical settings\n", + " \"solver\": None,\n", + " \"logging\": None,\n", + " \"starttime\": \"2024-01-01 00:00:00\",\n", + " \"endtime\": \"2024-01-02 00:00:00\",\n", + "}\n", + "\n", + "waterboard = CrossingsToRibasim(model_characteristics=model_characteristics)\n", + "\n", + "post_processed_data, crossings = waterboard.read_files()\n", + "post_processed_data, crossings = waterboard.routing_processor(post_processed_data, crossings)\n", + "crossings = waterboard.assign_node_ids(crossings)\n", + "edges = waterboard.create_edges(crossings)\n", + "nodes, edges = waterboard.create_nodes(crossings, edges)\n", + "edges = waterboard.embed_boezems(edges, post_processed_data, crossings)\n", + "\n", + "\n", + "# create individual model parts of the network\n", + "network = RibasimNetwork(nodes=nodes, edges=edges, model_characteristics=model_characteristics)\n", + "\n", + "edge = network.edge()\n", + "basin_node, basin_profile, basin_static, basin_state, basin_area = network.basin()\n", + "pump_node, pump_static = network.pump()\n", + "tabulated_rating_curve_node, tabulated_rating_curve_static = network.tabulated_rating_curve()\n", + "level_boundary_node, level_boundary_static = network.level_boundary()\n", + "flow_boundary_node, flow_boundary_static = network.flow_boundary()\n", + "manning_resistance_node, manning_resistance_static = network.manning_resistance()\n", + "terminal_node = network.terminal()\n", + "\n", + "# linear_resistance = network.linear_resistance()\n", + "# fractional_flow = network.fractional_flow()\n", + "# outlet = network.outlet()\n", + "# discrete_control = network.discrete_control()\n", + "# pid_control = network.pid_control()\n", + "\n", + "# insert the individual model modules in an actual model\n", + "model = Model(starttime=model_characteristics[\"starttime\"], endtime=model_characteristics[\"endtime\"], crs=\"EPSG:28992\")\n", + "\n", + "model.edge = edge\n", + "\n", + "model.basin.node = basin_node\n", + "model.basin.profile = basin_profile\n", + "model.basin.static = basin_static\n", + "model.basin.state = basin_state\n", + "model.basin.area = basin_area\n", + "\n", + "model.pump.node = pump_node\n", + "model.pump.static = pump_static\n", + "\n", + "model.tabulated_rating_curve.node = tabulated_rating_curve_node\n", + "model.tabulated_rating_curve.static = tabulated_rating_curve_static\n", + "\n", + "model.level_boundary.node = level_boundary_node\n", + "model.level_boundary.static = level_boundary_static\n", + "\n", + "model.flow_boundary.node = flow_boundary_node\n", + "model.flow_boundary.static = flow_boundary_static\n", + "\n", + "model.manning_resistance.node = manning_resistance_node\n", + "model.manning_resistance.static = manning_resistance_static\n", + "\n", + "model.terminal.node = terminal_node\n", + "\n", + "# add checks and metadata\n", + "checks = network.check(model=model, post_processed_data=post_processed_data, crossings=crossings)\n", + "model = network.add_meta_data(model, checks, post_processed_data, crossings)\n", + "\n", + "# write the result\n", + "network.WriteResults(model=model, checks=checks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/03_test_outlets.ipynb b/src/peilbeheerst_model/03_test_outlets.ipynb new file mode 100644 index 0000000..d05157b --- /dev/null +++ b/src/peilbeheerst_model/03_test_outlets.ipynb @@ -0,0 +1,365 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "\n", + "from outlets import case1, case2\n", + "\n", + "sys.path.append(\"peilbeheerst_model\")\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "markdown", + "id": "1", + "metadata": {}, + "source": [ + "# Case 1" + ] + }, + { + "cell_type": "markdown", + "id": "2", + "metadata": {}, + "source": [ + "### Example 1: boundary and basin levels on target " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "case1_example1 = case1(\"case1_example1\")\n", + "case1_example1.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "### Example 2: boundary levels below target" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "case1_example2 = case1(\"case1_example2\")\n", + "case1_example2.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "### Example 3: boundary levels on target, initial state below target" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "case1_example3 = case1(\"case1_example3\")\n", + "case1_example3.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "### Example 4: boundary levels on target, initial state above target" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "case1_example1 = case1(\"case1_example4\")\n", + "case1_example1.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "# Case 2" + ] + }, + { + "cell_type": "markdown", + "id": "11", + "metadata": {}, + "source": [ + "### Example 1: boundary and basin levels on target " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12", + "metadata": {}, + "outputs": [], + "source": [ + "# first, load in the simple model of case 1. Copy it.\n", + "case2_example1 = case1(\"case2_example1\")\n", + "case2_base_model = case2_example1.create_model(copy=True)\n", + "\n", + "# then, change it to a case 2 category\n", + "case2_example1 = case2(\"case2_example1\", model=case2_base_model)\n", + "case2_example1.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "13", + "metadata": {}, + "source": [ + "### Example 2: boundary and basins below target, third basin above\n", + "The third basins should fill up the others, or else be pumped to target level by the second pump." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14", + "metadata": {}, + "outputs": [], + "source": [ + "# first, load in the simple model of case 1. Copy it.\n", + "case2_example1 = case1(\"case2_example2\")\n", + "case2_base_model = case2_example1.create_model(copy=True)\n", + "\n", + "case2_example2 = case2(\"case2_example2\", model=case2_base_model)\n", + "case2_example2.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "15", + "metadata": {}, + "source": [ + "### Example 3: boundary and basins below target, third basin above, pump rate of third peilgebied set to 0. \n", + "Quite similair to case 2 example 2, exvept that the pump rate of first pump set to a low rate, so a rising water level is expected in the other basins. The third basins should fill up the others." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16", + "metadata": {}, + "outputs": [], + "source": [ + "# first, load in the simple model of case 1. Copy it.\n", + "case2_example1 = case1(\"case2_example3\")\n", + "case2_base_model = case2_example1.create_model(copy=True)\n", + "\n", + "# implement the second model\n", + "case2_example3 = case2(\"case2_example3\", model=case2_base_model)\n", + "case2_example3.create_model()" + ] + }, + { + "cell_type": "markdown", + "id": "17", + "metadata": {}, + "source": [ + "### Example 4: low target level in third basin, results in incorrect flow direction\n", + "\n", + "The water level in each basin, including the boundaries, are too low. No water should be flowing. However, only the initial level is higher than the target level of the third basin. Water should only flow from the third basin to the last level boundary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "# first, load in the simple model of case 1. Copy it.\n", + "case2_example4 = case1(\"case2_example4\")\n", + "case2_base_model = case2_example4.create_model(copy=True)\n", + "\n", + "# implement the second model\n", + "case2_example4 = case2(\"case2_example4\", model=case2_base_model)\n", + "case2_example4.create_model()" + ] + }, + { + "cell_type": "raw", + "id": "19", + "metadata": {}, + "source": [ + "Volgende case zou kunnen zijn dat er tussen twee peilgebieden (met wel of niet andere target levels) er outlets zijn, die van de een naar de ander gaan, en andersom. Maar in principe verwacht ik daar geen gekke situaties: de outlet laat alleen water stromen als dit onder vrij verval kan, en pompt geen water omhoog. Het enige wat wel gek zou kunnen worden, is als beide peilen rond hetzelfde niveau komen. Dan zou het water de ene tijdstap van links naar rechts kunnen stromen, en de andere momenten de andere kant op. Ik kan me voorstellen dat dit tot instabiliteiten leidt. Weet alleen niet zeker of dit gaat optreden bij simpele voorbeelden als hier." + ] + }, + { + "cell_type": "raw", + "id": "20", + "metadata": {}, + "source": [ + "Wat wel interessant zou zijn is het toch wel toevoegen van ContinuousControls. Eerst leek dit niet een logische stap, omdat ik wilde dat de outlet zou luisteren naar boven- en benedenstroomse peil. Maar nu doet dat het eigenlijk alleen naar bovenstrooms. \n", + "\n", + "Punt van reflectie: is dat uberhaupt wel de goede aanpak? Hoe weet de basin dan dat het water moet doorlaten? Je kan dit doen door de crest level een stukje lager te zetten dan streefpeil, maar dat houdt dan wel in dat er ALTIJD water stroomt. Qua doorspoeling is dat opzich nog zo gek niet, maar het is niet de meest chique manier. Ook heb je hier dan weinig controle op, wat je misschien wel wil hebben.\n" + ] + }, + { + "cell_type": "raw", + "id": "21", + "metadata": {}, + "source": [ + "Conclusie(?): de discrete controls moeten OOK gaan luisteren naar benedenstroomse peil. Dit toch wel doen aan de hand van de vier verschillende opties, afhankelijk wat de streefpeil van peilgebied 1 en peilgebied 2 is. \n", + "\n", + "Om instabiliteiten tegen te gaan is het wellicht goed om de grenzen niet precies op streefpeil te zetten, maar juist met een bepaalde afwijking. De afwijking om water door te mogen voeren van peilgebied 1 naar peilgebied2 moet kunnen zolang het waterniveau van peilgebied 1 niet 5 cm onder streefpeil zakt. De inlaat vanaf de boezem naar peilgebied 1 moet in principe sturen op exact streefpeil. " + ] + }, + { + "cell_type": "raw", + "id": "22", + "metadata": {}, + "source": [ + "1) Hoe verhoudt dit zich tot de min_crest_level en een enkele listen_to_node?\n", + "\n", + "De min_crest_level voert met 1 listen_node_id altijd water door. In het geval van hierboven zou water altijd van peilgebied 1 naar peilgebied 2 gaan, terwijl peilgebied 2 wellicht helemaal geen water nodig heeft terwijl de inlaat van de boezem naar peilgebied 1 wel water aan het inlaten is voor peilgebied 2. \n", + "\n", + "\n", + "\n", + "2) Hoe verhoudt de min_crest_level zich met dubbele listen_to_nodes?\n", + "\n", + "Min_Crest_level lijkt me bijna overbodig worden. \n", + "\n", + "\n", + "3) Stel er komt alleen een min_crest_level op de genoemde 5 cm onder streefpeil van peilgebied 1, dat wordt als het ware een schaduw listen node. Dan hoeft de outlet alleen nog maar te luisteren naar de basin die benedenstrooms ligt?\n", + "\n", + "Dat is niet waar, want stel er is een minimum crest level gedefinieerd die 5 cm onder streefpeil ligt van peilgebied 1. Wat als de water stand 2 cm onder streefpeil ligt (dus wel nog boven crest level), en er is geen water nodig in peilgebied 2? --> dan blijft het stromen, terwijl dat niet moet." + ] + }, + { + "cell_type": "raw", + "id": "23", + "metadata": {}, + "source": [ + "Conclusie: vorige conclusie is correct. Luisteren naar zowel boven- als benedestrooms." + ] + }, + { + "cell_type": "raw", + "id": "24", + "metadata": {}, + "source": [ + "Stappenplan voor AGV:\n", + "\n", + "Loopen per DiscreteControl lijkt mij geen goed idee. \n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "25", + "metadata": {}, + "source": [ + "# Thrashbin" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26", + "metadata": {}, + "outputs": [], + "source": [ + "example1_characteristics = {}\n", + "\n", + "\n", + "# solver\n", + "example1_characteristics[\"starttime\"] = \"2024-01-01 00:00:00\"\n", + "example1_characteristics[\"endtime\"] = \"2024-01-03 00:00:00\"\n", + "example1_characteristics[\"saveat\"] = 60\n", + "\n", + "# boezem settings (LevelBoundary)\n", + "example1_characteristics[\"boezem1_level\"] = 3\n", + "example1_characteristics[\"boezem2_level\"] = 3\n", + "\n", + "# peilgebied settings (Basins)\n", + "example1_characteristics[\"basin1_profile_area\"] = [0.01, 10000.0]\n", + "example1_characteristics[\"basin1_profile_level\"] = [1.0, 5.0]\n", + "example1_characteristics[\"basin1_initial_level\"] = [3]\n", + "example1_characteristics[\"basin1_target_level\"] = [2]\n", + "\n", + "\n", + "example1_characteristics[\"basin2_profile_area\"] = [0.01, 10000.0]\n", + "example1_characteristics[\"basin2_profile_level\"] = [0.0, 5.0]\n", + "example1_characteristics[\"basin2_initial_level\"] = [2]\n", + "example1_characteristics[\"basin2_target_level\"] = [1]\n", + "\n", + "example1_characteristics[\"evaporation\"] = 5 # mm/day, will be converted later to m/s\n", + "example1_characteristics[\"precipitation\"] = 5 # mm/day, will be converted later to m/s\n", + "\n", + "# connection node settings (Outlets, Pumpts)\n", + "example1_characteristics[\"outlet1_flow_rate\"] = 0.010\n", + "example1_characteristics[\"outlet1_min_crest_level\"] = 2.90\n", + "\n", + "example1_characteristics[\"outlet2_flow_rate\"] = 0.010\n", + "example1_characteristics[\"outlet2_min_crest_level\"] = 1.90\n", + "\n", + "example1_characteristics[\"pump_flow_rate\"] = 10 / 60 # [x] m3 / minute\n", + "\n", + "# general settings\n", + "example1_characteristics[\"plot\"] = True\n", + "example1_characteristics[\"crs\"] = \"EPSG:4326\"\n", + "example1_characteristics[\"case\"] = \"case1\"\n", + "example1_characteristics[\"example\"] = \"example1\"\n", + "example1_characteristics[\"results_dir\"] = r\"../../../../Outlet_tests/\"\n", + "example1_characteristics[\"show_progress\"] = False\n", + "example1_characteristics[\"show_results\"] = True\n", + "\n", + "# solver settings\n", + "example1_characteristics[\"saveat\"] = 60" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/Parametrize/AmstelGooienVecht_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/AmstelGooienVecht_parametrize.ipynb index b8c646f..4a3f38b 100644 --- a/src/peilbeheerst_model/Parametrize/AmstelGooienVecht_parametrize.ipynb +++ b/src/peilbeheerst_model/Parametrize/AmstelGooienVecht_parametrize.ipynb @@ -12,16 +12,58 @@ "import pathlib\n", "import warnings\n", "\n", + "import load_ribasim # noqa: F401\n", "import pandas as pd\n", "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", "import ribasim\n", "import ribasim.nodes\n", "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", + "from peilbeheerst_model.controle_output import *\n", "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", "\n", + "%reload_ext autoreload\n", + "%autoreload 2\n", "warnings.filterwarnings(\"ignore\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# import datetime\n", + "# import logging\n", + "# import os\n", + "# import pathlib\n", + "# import sys\n", + "# import warnings\n", + "# from pathlib import Path\n", + "\n", + "# import pandas as pd\n", + "# import ribasim\n", + "# import ribasim.nodes\n", + "\n", + "# #load ribasim_nl\n", + "# module_path = Path.cwd() / '../../ribasim_nl/'\n", + "# sys.path.append(str(module_path))\n", + "\n", + "# current_dir = os.getcwd()\n", + "# parent_dir = os.path.dirname(current_dir)\n", + "# if parent_dir not in sys.path:\n", + "# sys.path.append(parent_dir)\n", + "\n", + "# %reload_ext autoreload\n", + "# %autoreload 2\n", + "# warnings.filterwarnings('ignore')\n", + "\n", + "# import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", + "# from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", + "# from peilbeheerst_model.controle_output import *\n", + "# from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", + "\n" + ] + }, { "cell_type": "raw", "metadata": {}, @@ -33,6 +75,13 @@ "\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -54,10 +103,9 @@ "outputs": [], "source": [ "# Set paths\n", - "work_dir = pathlib.Path(\n", - " \"../../../../../Ribasim_networks/Waterschappen/AmstelGooienVecht/modellen/AmstelGooienVecht_boezemmodel\"\n", - ")\n", - "# work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\")\n", + "# work_dir = pathlib.Path(\"../../../../../Ribasim_networks/Waterschappen/AmstelGooienVecht/modellen/AmstelGooienVecht_boezemmodel\")\n", + "waterschap = \"AmstelGooienVecht\"\n", + "work_dir = pathlib.Path(f\"../../../../../Ribasim_updated_models/{waterschap}/modellen/{waterschap}_parametrized\")\n", "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", "output_dir = work_dir.joinpath(\"results\")\n", @@ -92,18 +140,15 @@ "outputs": [], "source": [ "name = \"Ron Bruijns (HKV)\"\n", - "waterschap = \"AmstelGooienVecht\"\n", - "versie = \"2024_7_11\"\n", + "versie = \"2024_8_8\"\n", "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_AmstelGooienVecht.xlsx\"\n", - ")\n", + "feedback_excel = pathlib.Path(f\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_{waterschap}.xlsx\")\n", "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_AmstelGooienVecht_JA_processed.xlsx\"\n", + " f\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_{waterschap}_JA_processed.xlsx\"\n", ")\n", "\n", - "ribasim_toml = f\"../../../../../Ribasim_base_models/AmstelGooienVecht_boezemmodel_{versie}/ribasim.toml\"\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", + "ribasim_toml = f\"../../../../../Ribasim_base_models/{waterschap}_boezemmodel_{versie}/ribasim.toml\"\n", + "output_folder = work_dir # f\"../../../../../Ribasim_updated_models/{waterschap}\"\n", "\n", "processor = RibasimFeedbackProcessor(\n", " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", @@ -152,19 +197,8 @@ "metadata": {}, "outputs": [], "source": [ - "# # #add nodes for a correct schematisation\n", - "\n", - "# #line 26\n", - "# ribasim_model.tabulated_rating_curve.add(\n", - "# Node(new_node_id, Point(129099, 468239)),\n", - "# [tabulated_rating_curve.Static(level=[0.0, 1.0], flow_rate=[0.0, 10])])\n", - "\n", - "# ribasim_model.edge.add(ribasim_model.level_boundary[91180],ribasim_model.tabulated_rating_curve[new_node_id])\n", - "# ribasim_model.edge.add(ribasim_model.tabulated_rating_curve[new_node_id], ribasim_model.level_boundary[59])\n", - "\n", - "# for some reason a few edges are removed to the ARK\n", - "ribasim_model.edge.add(ribasim_model.basin[68], ribasim_model.pump[316])\n", - "ribasim_model.edge.add(ribasim_model.pump[316], ribasim_model.terminal[915])" + "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"node_id\"] == 195, \"meta_streefpeil\"] = -2.45\n", + "ribasim_model.basin.state.df.loc[ribasim_model.basin.state.df.node_id == 195, \"level\"] = -2.45" ] }, { @@ -173,22 +207,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Amstel Gooi en Vecht\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"node_id\"] == 184, \"meta_streefpeil\"] = -0.4\n", - "ribasim_model.basin.state.df.loc[ribasim_model.basin.state.df.node_id == 184, \"level\"] = -0.4\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"node_id\"] == 192, \"meta_streefpeil\"] = -0.4\n", - "ribasim_model.basin.state.df.loc[ribasim_model.basin.state.df.node_id == 192, \"level\"] = -0.4\n", - "\n", - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" + "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()" ] }, { @@ -220,11 +239,7 @@ "source": [ "# Define the initial state of each basin. Is set equal to the streefpeil\n", "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" + "\n" ] }, { @@ -236,13 +251,58 @@ "# Add storage basins\n", "model_name = \"AmstelGooienVecht_StorageBasins\"\n", "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", + "# processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", + "# ribasim_model = processor.run()\n", + "\n", + "processor = AddStorageBasin(\n", + " path_ribasim_toml, model_name, output_folder, include_hoofdwater=False, log=False, node_ids=None\n", + ")\n", "ribasim_model = processor.run()\n", "\n", "# Check basin area\n", "ribasim_param.validate_basin_area(ribasim_model)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# remove the basins of above in the feedback form" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# change unknown streefpeilen to a default streefpeil\n", + "ribasim_model.basin.area.df.loc[\n", + " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", + "] = unknown_streefpeil\n", + "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == -9.999, \"meta_streefpeil\"] = (\n", + " unknown_streefpeil\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", + "ribasim_param.insert_standard_profile(\n", + " ribasim_model,\n", + " unknown_streefpeil=unknown_streefpeil,\n", + " regular_percentage=regular_percentage,\n", + " boezem_percentage=boezem_percentage,\n", + " depth_profile=2,\n", + ")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -258,11 +318,11 @@ "source": [ "# Set static forcing\n", "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", + " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(10),\n", " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", + " # 'urban_runoff': ribasim_param.convert_mm_day_to_m_sec(0),\n", "}\n", "\n", "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" @@ -307,7 +367,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### FlowBoundary" + "### Add Outlet" ] }, { @@ -316,15 +376,14 @@ "metadata": {}, "outputs": [], "source": [ - "# Set FlowBoundary rate for each pump\n", - "# ribasim_model.flow_boundary.static.df['flow_rate'] = 0 #" + "ribasim_param.add_outlets(ribasim_model, delta_crest_level=0.10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Outlet" + "## Add control, based on the meta_categorie" ] }, { @@ -333,14 +392,16 @@ "metadata": {}, "outputs": [], "source": [ - "ribasim_param.add_outlets(ribasim_model, delta_crest_level=0.10)" + "ribasim_param.identify_node_meta_categorie(ribasim_model)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "### Add Discrete Control" + "ribasim_param.add_discrete_control(ribasim_model, waterschap, default_level)" ] }, { @@ -349,15 +410,16 @@ "metadata": {}, "outputs": [], "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" + "ribasim_model.discrete_control.node.df = ribasim_model.discrete_control.node.df.drop_duplicates(\n", + " subset=\"node_id\"\n", + ").reset_index(drop=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### TabulatedRatingCurve" + "### Manning Resistance" ] }, { @@ -366,23 +428,8 @@ "metadata": {}, "outputs": [], "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# #Adjust some TRC's by manually\n", - "# ribasim_model.tabulated_rating_curve.static.df.loc[ribasim_model.tabulated_rating_curve.static.df.node_id == 612, 'flow_rate'] = 0 #too small basin. This is a work around, so no water can flow in or out. Delete this basin later.\n", - "# ribasim_model.tabulated_rating_curve.static.df.loc[ribasim_model.tabulated_rating_curve.static.df.node_id == 613, 'flow_rate'] = 0 #too small basin. This is a work around, so no water can flow in or out. Delete this basin later.\n" + "# there is a MR without geometry and without edges for some reason\n", + "ribasim_model.manning_resistance.node.df = ribasim_model.manning_resistance.node.df.dropna(subset=\"geometry\")" ] }, { @@ -401,11 +448,18 @@ "# Write model output\n", "ribasim_param.index_reset(ribasim_model)\n", "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 1, 0, 0, 1)\n", + "ribasim_model.endtime = datetime.datetime(2024, 1, 30)\n", "ribasim_model.solver.saveat = 3600\n", "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -472,6 +526,15 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stop" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -489,12 +552,82 @@ "df_basin" ] }, + { + "cell_type": "markdown", + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, + "source": [ + "### FlowBoundary" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Set FlowBoundary rate for each pump\n", + "# ribasim_model.flow_boundary.static.df['flow_rate'] = 0 #\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, + "source": [ + "### Add Discrete Control" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Add discrete control nodes and control edges\n", + "# ribasim_param.add_discrete_control_nodes(ribasim_model)\n", + "\n" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### TabulatedRatingCurve" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# # First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", + "# ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# # The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", + "# # Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", + "# ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)\n", + "\n" + ] } ], "metadata": { @@ -513,7 +646,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/src/peilbeheerst_model/Parametrize/Delfland_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/Delfland_parametrize.ipynb deleted file mode 100644 index 80a7cc4..0000000 --- a/src/peilbeheerst_model/Parametrize/Delfland_parametrize.ipynb +++ /dev/null @@ -1,446 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", - "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Delfland\"\n", - "versie = \"2024_6_1\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name = \"Ron Bruijns (HKV)\"\n", - "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\"\n", - ")\n", - "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_RB_processed.xlsx\"\n", - ")\n", - "\n", - "ribasim_toml = pathlib.Path(\n", - " r\"../../../../../Ribasim_base_models\", waterschap + \"_boezemmodel_\" + versie, \"ribasim.toml\"\n", - ")\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "processor = RibasimFeedbackProcessor(\n", - " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", - ")\n", - "processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"].isna(), \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "model_name = \"AmstelGooienVecht_StorageBasins\"\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 1, 0, 0, 1)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/HollandsNoorderkwartier_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/HollandsNoorderkwartier_parametrize.ipynb deleted file mode 100644 index 817200b..0000000 --- a/src/peilbeheerst_model/Parametrize/HollandsNoorderkwartier_parametrize.ipynb +++ /dev/null @@ -1,434 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"HollandsNoorderkwartier\"\n", - "versie = \"2024_6_4\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# name = \"Ron Bruijns (HKV)\"\n", - "\n", - "# feedback_excel = pathlib.Path(r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\")\n", - "# feedback_excel_processed = r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - "\n", - "# ribasim_toml = pathlib.Path(r\"../../../../../Ribasim_base_models\", waterschap + '_boezemmodel_' + versie, 'ribasim.toml')\n", - "# output_folder = work_dir #r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "# processor = RibasimFeedbackProcessor(name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed)\n", - "# processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/HollandseDelta_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/HollandseDelta_parametrize.ipynb deleted file mode 100644 index 03911b0..0000000 --- a/src/peilbeheerst_model/Parametrize/HollandseDelta_parametrize.ipynb +++ /dev/null @@ -1,491 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", - "from ribasim import Node\n", - "from ribasim.nodes import (\n", - " tabulated_rating_curve,\n", - ")\n", - "from shapely.geometry import Point\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"HollandseDelta\"\n", - "versie = \"2024_6_1\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name = \"Ron Bruijns (HKV)\"\n", - "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\"\n", - ")\n", - "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - ")\n", - "\n", - "ribasim_toml = pathlib.Path(\n", - " r\"../../../../../Ribasim_base_models\", waterschap + \"_boezemmodel_\" + versie, \"ribasim.toml\"\n", - ")\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "processor = RibasimFeedbackProcessor(\n", - " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", - ")\n", - "processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Remove the TRC's without geometries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# sometimes the geometries are POINT EMPTY\n", - "ribasim_model.tabulated_rating_curve.node.df = ribasim_model.tabulated_rating_curve.node.df[\n", - " ~ribasim_model.tabulated_rating_curve.node.df[\"geometry\"].is_empty\n", - "]\n", - "\n", - "# sometimes the geometries are None\n", - "ribasim_model.tabulated_rating_curve.node.df.dropna(subset=\"geometry\", inplace=True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "new_node_id = max(ribasim_model.edge.df.from_node_id.max(), ribasim_model.edge.df.to_node_id.max()) + 1\n", - "\n", - "### First manual adjustment: insert Terminal with TRC\n", - "ribasim_model.terminal.add(Node(new_node_id, Point(98200, 426447)))\n", - "\n", - "new_node_id += 1\n", - "ribasim_model.tabulated_rating_curve.add(\n", - " Node(new_node_id, Point(98200, 426447)), [tabulated_rating_curve.Static(level=[0.1, 1.1], flow_rate=[0.0, 10])]\n", - ")\n", - "\n", - "ribasim_model.edge.add(ribasim_model.basin[389], ribasim_model.tabulated_rating_curve[new_node_id])\n", - "\n", - "ribasim_model.edge.add(ribasim_model.tabulated_rating_curve[new_node_id], ribasim_model.terminal[new_node_id - 1])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/Rijnland_nieuw.ipynb b/src/peilbeheerst_model/Parametrize/Rijnland_nieuw.ipynb deleted file mode 100644 index fedcaf5..0000000 --- a/src/peilbeheerst_model/Parametrize/Rijnland_nieuw.ipynb +++ /dev/null @@ -1,443 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Rijnland\"\n", - "versie = \"2024_6_2\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name = \"Ron Bruijns (HKV)\"\n", - "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\"\n", - ")\n", - "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - ")\n", - "\n", - "ribasim_toml = pathlib.Path(\n", - " r\"../../../../../Ribasim_base_models\", waterschap + \"_boezemmodel_\" + versie, \"ribasim.toml\"\n", - ")\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "processor = RibasimFeedbackProcessor(\n", - " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", - ")\n", - "processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/Rijnland_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/Rijnland_parametrize.ipynb deleted file mode 100644 index c399854..0000000 --- a/src/peilbeheerst_model/Parametrize/Rijnland_parametrize.ipynb +++ /dev/null @@ -1,451 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import matplotlib.pyplot as plt\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Rijnland\"\n", - "\n", - "# work_dir = pathlib.Path(\"Rekenend_Model_Test/Model_instanties/20240523\")\n", - "work_dir = pathlib.Path(f\"../../../../../Ribasim_networks/Waterschappen/{waterschap}/modellen/{waterschap}_boezemmodel\")\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 1, 0, 0, 10)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "NoI = 230 # Node_id of Interest\n", - "fig, ax = plt.subplots()\n", - "\n", - "for i in range(len(stored_trc)):\n", - " trc = stored_trc[str(i + 1)] # select the correct trc frame\n", - " trc = trc.loc[trc.node_id == NoI][[\"level\", \"flow_rate\"]] # select the level and flow_rate of the NoI\n", - "\n", - " # plot the Q(h) relation in the figure\n", - " ax.scatter(x=trc[\"level\"], y=trc[\"flow_rate\"], linewidths=1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Oud" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Define numerical settings in the .toml file" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# # Load the just created .toml file\n", - "# with open(path_ribasim_toml, 'r') as file:\n", - "# data = toml.load(file)\n", - "\n", - "# # Update the general section\n", - "# data.update({\n", - "# # \"starttime\": \"2024-01-01T00:00:00\",\n", - "# # \"endtime\": \"2024-01-02T00:00:00\",\n", - "# \"crs\": \"EPSG:28992\",\n", - "# \"input_dir\": \".\",\n", - "# \"results_dir\": \"results\",\n", - "# \"ribasim_version\": \"2024.8.0\"\n", - "# })\n", - "# # Update the solver section\n", - "# data['solver'] = {\n", - "# \"algorithm\": \"RK4\",\n", - "# \"saveat\": 86400.0,\n", - "# \"dt\": 60.0,\n", - "# \"dtmin\": 0.0,\n", - "# \"dtmax\": 1.0,\n", - "# \"force_dtmin\": False,\n", - "# \"abstol\": 1e-06,\n", - "# \"reltol\": 1e-05,\n", - "# \"maxiters\": 10000000,\n", - "# \"sparse\": True,\n", - "# \"autodiff\": True\n", - "# }\n", - "\n", - "# # Save the updated .toml file\n", - "# with open(path_ribasim_toml, 'w') as file:\n", - "# toml.dump(data, file)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/Rivierenland_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/Rivierenland_parametrize.ipynb deleted file mode 100644 index a60cb92..0000000 --- a/src/peilbeheerst_model/Parametrize/Rivierenland_parametrize.ipynb +++ /dev/null @@ -1,446 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Rivierenland\"\n", - "\n", - "# work_dir = pathlib.Path(\"Rekenend_Model_Test/Model_instanties/20240523\")\n", - "work_dir = pathlib.Path(f\"../../../../../Ribasim_networks/Waterschappen/{waterschap}/modellen/{waterschap}_boezemmodel\")\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 1, 0, 0, 10)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "NoI = 230 # Node_id of Interest\n", - "fig, ax = plt.subplots()\n", - "\n", - "for i in range(len(stored_trc)):\n", - " trc = stored_trc[str(i + 1)] # select the correct trc frame\n", - " trc = trc.loc[trc.node_id == NoI][[\"level\", \"flow_rate\"]] # select the level and flow_rate of the NoI\n", - "\n", - " # plot the Q(h) relation in the figure\n", - " ax.scatter(x=trc[\"level\"], y=trc[\"flow_rate\"], linewidths=1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Oud" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Define numerical settings in the .toml file" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# # Load the just created .toml file\n", - "# with open(path_ribasim_toml, 'r') as file:\n", - "# data = toml.load(file)\n", - "\n", - "# # Update the general section\n", - "# data.update({\n", - "# # \"starttime\": \"2024-01-01T00:00:00\",\n", - "# # \"endtime\": \"2024-01-02T00:00:00\",\n", - "# \"crs\": \"EPSG:28992\",\n", - "# \"input_dir\": \".\",\n", - "# \"results_dir\": \"results\",\n", - "# \"ribasim_version\": \"2024.8.0\"\n", - "# })\n", - "# # Update the solver section\n", - "# data['solver'] = {\n", - "# \"algorithm\": \"RK4\",\n", - "# \"saveat\": 86400.0,\n", - "# \"dt\": 60.0,\n", - "# \"dtmin\": 0.0,\n", - "# \"dtmax\": 1.0,\n", - "# \"force_dtmin\": False,\n", - "# \"abstol\": 1e-06,\n", - "# \"reltol\": 1e-05,\n", - "# \"maxiters\": 10000000,\n", - "# \"sparse\": True,\n", - "# \"autodiff\": True\n", - "# }\n", - "\n", - "# # Save the updated .toml file\n", - "# with open(path_ribasim_toml, 'w') as file:\n", - "# toml.dump(data, file)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/Scheldestromen_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/Scheldestromen_parametrize.ipynb deleted file mode 100644 index f7f49cc..0000000 --- a/src/peilbeheerst_model/Parametrize/Scheldestromen_parametrize.ipynb +++ /dev/null @@ -1,485 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", - "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Scheldestromen\"\n", - "versie = \"2024_6_1\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2\n", - "\n", - "default_boundary_level = 0" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name = \"Ron Bruijns (HKV)\"\n", - "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\"\n", - ")\n", - "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - ")\n", - "\n", - "ribasim_toml = pathlib.Path(\n", - " r\"../../../../../Ribasim_base_models\", waterschap + \"_boezemmodel_\" + versie, \"ribasim.toml\"\n", - ")\n", - "\n", - "# r\"../../../../../Ribasim_base_models/AmstelGooienVecht_boezemmodel_2024_6_8/ribasim.toml\"\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "processor = RibasimFeedbackProcessor(\n", - " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", - ")\n", - "processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "# ribasim_model.basin.area\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "model_name = \"Scheldestromen_StorageBasins\"\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Convert all boundary nodes to LevelBoundaries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ribasim_param.Terminals_to_LevelBoundaries(ribasim_model=ribasim_model, default_level=default_boundary_level)\n", - "\n", - "ribasim_param.FlowBoundaries_to_LevelBoundaries(ribasim_model=ribasim_model, default_level=default_boundary_level)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "# ribasim_model.flow_boundary.static.df['flow_rate'] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# !ribasim '../../../../../Ribasim_updated_models/Scheldestromen/ribasim.toml'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ribasim_model.plot()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/SchielandendeKrimpenerwaard_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/SchielandendeKrimpenerwaard_parametrize.ipynb deleted file mode 100644 index 5478e2f..0000000 --- a/src/peilbeheerst_model/Parametrize/SchielandendeKrimpenerwaard_parametrize.ipynb +++ /dev/null @@ -1,466 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", - "from peilbeheerst_model.ribasim_feedback_processor import RibasimFeedbackProcessor\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"SchielandendeKrimpenerwaard\"\n", - "versie = \"2024_7_1\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2\n", - "\n", - "default_boundary_level = 0" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name = \"Ron Bruijns (HKV)\"\n", - "\n", - "feedback_excel = pathlib.Path(\n", - " r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\"\n", - ")\n", - "feedback_excel_processed = (\n", - " r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - ")\n", - "\n", - "ribasim_toml = pathlib.Path(\n", - " r\"../../../../../Ribasim_base_models\", waterschap + \"_boezemmodel_\" + versie, \"ribasim.toml\"\n", - ")\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "processor = RibasimFeedbackProcessor(\n", - " name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed\n", - ")\n", - "processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "model_name = \"SchielandendeKrimpenerwaard_StorageBasins\"\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "### Convert all boundary nodes to LevelBoundaries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ribasim_param.Terminals_to_LevelBoundaries(ribasim_model=ribasim_model, default_level=default_boundary_level)\n", - "\n", - "ribasim_param.FlowBoundaries_to_LevelBoundaries(ribasim_model=ribasim_model, default_level=default_boundary_level)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# # Set FlowBoundary rate for each pump\n", - "# ribasim_model.flow_boundary.static.df['flow_rate'] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/WetterskipFryslan_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/WetterskipFryslan_parametrize.ipynb deleted file mode 100644 index 9bac655..0000000 --- a/src/peilbeheerst_model/Parametrize/WetterskipFryslan_parametrize.ipynb +++ /dev/null @@ -1,519 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", - "from ribasim import Node\n", - "from ribasim.nodes import (\n", - " pump,\n", - ")\n", - "from shapely.geometry import Point\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"WetterskipFryslan\"\n", - "versie = \"2024_6_17\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_base_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The form of Wetterskip has not been added, as there was only one line of feedback. This line included some operations which is not possible by making use of the feedback form. This has been added some code blocks belows." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# name = \"Ron Bruijns (HKV)\"\n", - "\n", - "# feedback_excel = pathlib.Path(r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\")\n", - "# feedback_excel_processed = r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - "\n", - "# ribasim_toml = pathlib.Path(r\"../../../../../Ribasim_base_models\", waterschap + '_boezemmodel_' + versie, 'ribasim.toml')\n", - "# output_folder = work_dir #r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "# processor = RibasimFeedbackProcessor(name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed)\n", - "# processor.run()\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# path_ribasim_toml = os.path.join(work_dir, waterschap + '_boezemmodel_' + versie, 'ribasim.toml')\n", - "path_ribasim_toml = \"../../../../../Ribasim_base_models/WetterskipFryslan_boezemmodel_2024_6_17/ribasim.toml\"\n", - "output_folder = work_dir # r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "let op! verwijder bovenstaand toml file als er meer feedback komt" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add a Terminal and a gemaal at the northernmost Zeepolder" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, add the gemaal\n", - "node_id_gemaal = ribasim_param.get_current_max_nodeid(ribasim_model) + 1 # define new node_id number\n", - "ribasim_model.pump.add(Node(node_id_gemaal, Point(170900, 591437)), [pump.Static(flow_rate=[700 / 60])]) # add pump\n", - "ribasim_model.edge.add(\n", - " ribasim_model.basin[328], ribasim_model.pump[node_id_gemaal]\n", - ") # add the egde from the basin to the pump\n", - "\n", - "node_id_terminal = ribasim_param.get_current_max_nodeid(ribasim_model) + 1 # define new node_id number\n", - "ribasim_model.terminal.add(Node(node_id_terminal, Point(170898, 591445))) # add Terminal\n", - "ribasim_model.edge.add(\n", - " ribasim_model.pump[node_id_gemaal], ribasim_model.terminal[node_id_terminal]\n", - ") # add the edge from the pump to the terminal\n", - "\n", - "# add the edges between the two nodes above" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# These nodes have suddenly been added, without apparent reason. Delete these nodes\n", - "ribasim_model.pump.node.df = ribasim_model.pump.node.df.loc[ribasim_model.pump.node.df.node_id != 94374]\n", - "ribasim_model.pump.node.df = ribasim_model.pump.node.df.loc[ribasim_model.pump.node.df.node_id != 94376]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 1, 0, 0, 1)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "path_ribasim_toml = \"../../../../../Ribasim_updated_models/WetterskipFryslan/ribasim.toml\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "pas wederom het pad hierboven aan" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/Zuiderzeeland_parametrize.ipynb b/src/peilbeheerst_model/Parametrize/Zuiderzeeland_parametrize.ipynb deleted file mode 100644 index 62f0146..0000000 --- a/src/peilbeheerst_model/Parametrize/Zuiderzeeland_parametrize.ipynb +++ /dev/null @@ -1,451 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import datetime\n", - "import logging\n", - "import os\n", - "import pathlib\n", - "import warnings\n", - "\n", - "import pandas as pd\n", - "import peilbeheerst_model.ribasim_parametrization as ribasim_param\n", - "import ribasim\n", - "import ribasim.nodes\n", - "from peilbeheerst_model.add_storage_basins import AddStorageBasin\n", - "\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "\"\"\"\n", - "Model Aannamens:\n", - "- Max area van hydroobjecten is 10% van basin area, 90% bij boezem\n", - "- Initial state op streefpeil\n", - "- \n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Define variables and model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Set Config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set paths\n", - "waterschap = \"Zuiderzeeland\"\n", - "versie = \"2024_6_1\"\n", - "\n", - "work_dir = pathlib.Path(r\"../../../../../Ribasim_updated_models\", waterschap)\n", - "ribasim_gpkg = work_dir.joinpath(\"database.gpkg\")\n", - "path_ribasim_toml = work_dir.joinpath(\"ribasim.toml\")\n", - "output_dir = work_dir.joinpath(\"results\")\n", - "\n", - "# Basin area percentage\n", - "regular_percentage = 10\n", - "boezem_percentage = 90\n", - "unknown_streefpeil = (\n", - " 0.00012345 # we need a streefpeil to create the profiles, Q(h)-relations, and af- and aanslag peil for pumps\n", - ")\n", - "\n", - "# Forcing settings\n", - "start_time = \"2024-01-01\"\n", - "timestep_size = \"d\"\n", - "timesteps = 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "path_ribasim_toml = r\"../../../../../Ribasim_base_models/Zuiderzeeland_boezemmodel_2024_6_1/ribasim.toml\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Let op: pas toml pad aan!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "zie hierboven!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Process the feedback form" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# name = \"Ron Bruijns (HKV)\"\n", - "\n", - "# feedback_excel = pathlib.Path(r\"../../../../../Ribasim_feedback/V1_formulieren/feedback_formulier_\" + waterschap + \".xlsx\")\n", - "# feedback_excel_processed = r\"../../../../..//Ribasim_feedback/V1_formulieren_verwerkt/feedback_formulier_\" + waterschap + \"_JA_processed.xlsx\"\n", - "\n", - "# ribasim_toml = pathlib.Path(r\"../../../../../Ribasim_base_models\", waterschap + '_boezemmodel_' + versie, 'ribasim.toml')\n", - "# output_folder = work_dir #r\"../../../../../Ribasim_updated_models/AmstelGooienVecht\"\n", - "\n", - "# processor = RibasimFeedbackProcessor(name, waterschap, versie, feedback_excel, ribasim_toml, output_folder, feedback_excel_processed)\n", - "# processor.run()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Load model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load Ribasim model\n", - "with warnings.catch_warnings():\n", - " warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n", - " ribasim_model = ribasim.Model(filepath=path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Model specific tweaks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "assert not pd.isnull(ribasim_model.basin.area.df.meta_streefpeil).any()\n", - "\n", - "ribasim_model.basin.area.df.loc[\n", - " ribasim_model.basin.area.df[\"meta_streefpeil\"] == \"Onbekend streefpeil\", \"meta_streefpeil\"\n", - "] = unknown_streefpeil\n", - "\n", - "ribasim_model.basin.area.df.loc[ribasim_model.basin.area.df[\"meta_streefpeil\"] == 9.999, \"meta_streefpeil\"] = (\n", - " unknown_streefpeil\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Parameterization" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Nodes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (characteristics)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Define the initial state of each basin. Is set equal to the streefpeil\n", - "# ribasim_param.set_initial_basin_state(ribasim_model) #the initial states of the basins are by default already set to the streefpeil!\n", - "\n", - "# Insert standard profiles to each basin. These are [depth_profiles] meter deep, defined from the streefpeil\n", - "ribasim_param.insert_standard_profile(\n", - " ribasim_model, regular_percentage=regular_percentage, boezem_percentage=boezem_percentage, depth_profile=2\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add storage basins\n", - "# model_name = 'AmstelGooienVecht_StorageBasins'\n", - "# node_ids = [1, 2, 3] # Specify node IDs to process or include_hoofdwater Boolean\n", - "processor = AddStorageBasin(ribasim_toml, model_name, output_folder, include_hoofdwater=True, log=True, node_ids=None)\n", - "ribasim_model = processor.run()\n", - "\n", - "# Check basin area\n", - "ribasim_param.validate_basin_area(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Basin (forcing)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set static forcing\n", - "forcing_dict = {\n", - " \"precipitation\": ribasim_param.convert_mm_day_to_m_sec(5),\n", - " \"potential_evaporation\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"drainage\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"infiltration\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - " \"urban_runoff\": ribasim_param.convert_mm_day_to_m_sec(0),\n", - "}\n", - "\n", - "ribasim_param.set_static_forcing(timesteps, timestep_size, start_time, forcing_dict, ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pumps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set pump capacity for each pump\n", - "ribasim_model.pump.static.df[\"flow_rate\"] = 0.16667 # 10 kuub per minuut" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### FlowBoundary" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set FlowBoundary rate for each pump\n", - "ribasim_model.flow_boundary.static.df[\"flow_rate\"] = 0 #" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Add Discrete Control" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add discrete control nodes and control edges\n", - "ribasim_param.add_discrete_control_nodes(ribasim_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### TabulatedRatingCurve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# First, set the Q(h)-relationship to a relationship which is at each TRC node the same\n", - "ribasim_param.set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=1)\n", - "\n", - "# The water level in the boezem is regulated heavily. There is a smaller tolerance for an increase in water level.\n", - "# Hence, the TRC's which discharge water from the boezem to a Terminal should have a higher capacity.\n", - "ribasim_param.set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.10, flow_rate=40)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Set numerical settings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Write model output\n", - "ribasim_param.index_reset(ribasim_model)\n", - "ribasim_model.starttime = datetime.datetime(2024, 1, 1)\n", - "ribasim_model.endtime = datetime.datetime(2024, 1, 5)\n", - "ribasim_model.solver.saveat = 3600\n", - "ribasim_param.write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Model" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterate over tabulated rating curves" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " ribasim_param.iterate_TRC(\n", - " ribasim_param=ribasim_param,\n", - " allowed_tolerance=0.02,\n", - " max_iter=1,\n", - " expected_difference=0.1,\n", - " max_adjustment=0.25,\n", - " cmd=[\"ribasim\", path_ribasim_toml],\n", - " output_dir=output_dir,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " )\n", - "\n", - "except Exception:\n", - " logging.error(\"The model was not able to run. Log file:\")\n", - " log_file_path = os.path.join(output_dir, \"ribasim.log\")\n", - " try:\n", - " with open(log_file_path) as log_file:\n", - " log_content = log_file.read()\n", - " print(log_content)\n", - " except Exception as log_exception:\n", - " logging.error(f\"Could not read the log file: {log_exception}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Write model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# control_dict = Control(work_dir = work_dir).run_all()\n", - "ribasim_param.write_ribasim_model_GoodCloud(\n", - " ribasim_model=ribasim_model,\n", - " path_ribasim_toml=path_ribasim_toml,\n", - " waterschap=waterschap,\n", - " modeltype=\"boezemmodel\",\n", - " include_results=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Open Output" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df_basin = pd.read_feather(os.path.join(output_dir, \"basin.arrow\"))\n", - "df_basin" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:ribasim]", - "language": "python", - "name": "conda-env-ribasim-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.13" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/src/peilbeheerst_model/Parametrize/load_ribasim.py b/src/peilbeheerst_model/Parametrize/load_ribasim.py new file mode 100644 index 0000000..08e1774 --- /dev/null +++ b/src/peilbeheerst_model/Parametrize/load_ribasim.py @@ -0,0 +1,12 @@ +import os +import sys +from pathlib import Path + +# load ribasim_nl +module_path = Path.cwd() / "../../ribasim_nl/" +sys.path.append(str(module_path)) + +current_dir = os.getcwd() +parent_dir = os.path.dirname(current_dir) +if parent_dir not in sys.path: + sys.path.append(parent_dir) diff --git a/src/peilbeheerst_model/Parametrize/sturing_AmstelGooienVecht.json b/src/peilbeheerst_model/Parametrize/sturing_AmstelGooienVecht.json new file mode 100644 index 0000000..58e2e06 --- /dev/null +++ b/src/peilbeheerst_model/Parametrize/sturing_AmstelGooienVecht.json @@ -0,0 +1,136 @@ +{ + "Inlaat boezem, stuw": { + "upstream_level_offset": 0.10, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 2.0, + "node_type": "outlet" + }, + + "Uitlaat boezem, stuw": { + "upstream_level_offset": 0.00, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 2.0, + "node_type": "outlet" + }, + + "Reguliere stuw": { + "upstream_level_offset": 0.15, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 1.0, + "node_type": "outlet" + }, + + "Inlaat buitenwater peilgebied, stuw": { + "upstream_level_offset": 0.10, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["pass", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 1.0, + "node_type": "outlet" + }, + + "Uitlaat buitenwater peilgebied, stuw": { + "upstream_level_offset": 0.10, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 1.0, + "node_type": "outlet" + }, + + "Boezem boezem, stuw": { + "upstream_level_offset": 0.10, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 10.0, + "node_type": "outlet" + }, + + "Inlaat boezem, gemaal": { + "upstream_level_offset": 0.10, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 0.2, + "node_type": "pump" + }, + + "Uitlaat boezem, gemaal": { + "upstream_level_offset": 0.00, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 0.2, + "node_type": "pump" + }, + + "Regulier afvoer gemaal": { + "upstream_level_offset": 0.00, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 0.2, + "node_type": "pump" + }, + + "Regulier aanvoer gemaal": { + "upstream_level_offset": 0.15, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["pass", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 0.1, + "node_type": "pump" + }, + + "Uitlaat buitenwater peilgebied, afvoer gemaal": { + "upstream_level_offset": 0.15, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 0.2, + "node_type": "pump" + }, + + "Uitlaat buitenwater peilgebied, aanvoer gemaal": { + "upstream_level_offset": 0.15, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["pass", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 0.1, + "node_type": "pump" + }, + + "Inlaat buitenwater peilgebied, afvoer gemaal": { + "upstream_level_offset": 0.00, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 0.2, + "node_type": "pump" + }, + + "Inlaat buitenwater peilgebied, aanvoer gemaal": { + "upstream_level_offset": 0.15, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["pass", "block", "pass", "block"], + "flow_rate_block": 0, + "flow_rate_pass": 0.1, + "node_type": "pump" + }, + + "Boezem boezem, afvoer gemaal": { + "upstream_level_offset": 0.00, + "truth_state": ["FF", "FT", "TF", "TT"], + "control_state": ["block", "block", "pass", "pass"], + "flow_rate_block": 0, + "flow_rate_pass": 10.0, + "node_type": "pump" + } +} diff --git a/src/peilbeheerst_model/compute_voronoi.ipynb b/src/peilbeheerst_model/compute_voronoi.ipynb new file mode 100644 index 0000000..1cc3562 --- /dev/null +++ b/src/peilbeheerst_model/compute_voronoi.ipynb @@ -0,0 +1,366 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "0", + "metadata": {}, + "outputs": [], + "source": [ + "import centerline.geometry\n", + "import geopandas as gpd\n", + "import networkx as nx\n", + "import numpy as np\n", + "import pandas as pd\n", + "import shapely\n", + "import shapely.geometry\n", + "import tqdm.auto as tqdm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "df = gpd.read_file(\"../../../../Data_postprocessed/Waterschappen/Wetterskip/Wetterskip.gpkg\", layer=\"peilgebied\")\n", + "df[\"geometry\"] = df.buffer(0)\n", + "df = df[~df.is_empty].copy()\n", + "df[\"geometry\"] = df.geometry.apply(shapely.force_2d)\n", + "df = df[df.peilgebied_cat == 1].copy()\n", + "\n", + "df_crossings = gpd.read_file(\n", + " \"../../../../Data_crossings/Wetterskip/wetterskip_crossings_v05.gpkg\", layer=\"crossings_hydroobject_filtered\"\n", + ")\n", + "df_crossings = df_crossings[df_crossings.agg_links_in_use].copy()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "# Merge polygons with a small buffer. Ook nodig om verbindingen te krijgen in sommige smalle watergangen.\n", + "df_merged = df.buffer(1.0).unary_union\n", + "df_merged = gpd.GeoDataFrame(geometry=list(df_merged.geoms), crs=df.crs)\n", + "\n", + "# add merged id to original polygons\n", + "merged_poly_ids = []\n", + "for row in tqdm.tqdm(df.itertuples(), total=len(df)):\n", + " idxs = df_merged.sindex.query(row.geometry, predicate=\"intersects\")\n", + " if len(idxs) == 0:\n", + " raise ValueError(\"no matches\")\n", + " elif len(idxs) > 1:\n", + " overlaps = []\n", + " for idx in idxs:\n", + " overlap = df_merged.iat[idx].intersection(row.geometry).area / row.geometry.area\n", + " overlaps.append(overlap)\n", + " idx = idxs.index(max(overlaps))\n", + " else:\n", + " idx = idxs[0]\n", + " merged_poly_ids.append(idx)\n", + "df[\"merged_poly_id\"] = merged_poly_ids" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "df_center = []\n", + "for idx, row in tqdm.tqdm(df_merged.iterrows(), total=len(df_merged)):\n", + " geom = row.geometry\n", + " interp_dist = 10\n", + " if geom.area < 1000:\n", + " interp_dist = 1\n", + " if geom.area < 100:\n", + " interp_dist = 0.1\n", + " if geom.area < 10:\n", + " interp_dist = 0.01\n", + " if geom.area < 1:\n", + " interp_dist = 0.001\n", + " centerpoly = centerline.geometry.Centerline(geom, interpolation_distance=interp_dist)\n", + " centerpoly = centerpoly.geometry\n", + " centerpoly = centerpoly.simplify(1, preserve_topology=True)\n", + " df_center.append(centerpoly)\n", + "df_center = gpd.GeoDataFrame(geometry=list(df_center), crs=df.crs)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4", + "metadata": {}, + "outputs": [], + "source": [ + "df_center_single = df_center.explode(index_parts=False)\n", + "df_center_single = df_center_single.set_index(np.arange(len(df_center_single)), append=True)\n", + "df_center_single.index.set_names([\"poly_id\", \"edge_id\"], inplace=True)\n", + "\n", + "df_center_single_boundary = df_center_single.copy()\n", + "df_center_single_boundary[\"geometry\"] = df_center_single.boundary\n", + "\n", + "# # Check of alles mooi verbonden is\n", + "# for i, row in tqdm.tqdm(enumerate(df_center_single_boundary.itertuples()), total=len(df_center_single_boundary), desc=\"check connections\"):\n", + "# idx = row.Index\n", + "# geom = row.geometry\n", + "\n", + "# idxs, dists = df_center_single_boundary.sindex.nearest(geom, return_distance=True, return_all=True)\n", + "# idxs = idxs[1, :]\n", + "# dists = dists[idxs != i]\n", + "# idxs = idxs[idxs != i]\n", + "# if dists.min() > 0:\n", + "# print(f\"no closed connection for {idx}, {dist.min()=}\")\n", + "# elif len(idxs) == 0:\n", + "# print(f\"No connection for {idx}: {df_center_single_boundary.iloc[idxs].index}\")\n", + "\n", + "df_center_single_boundary_points = df_center_single_boundary.explode(index_parts=True)\n", + "df_center_single_boundary_points[\"node_id\"] = None\n", + "df_center_single_boundary_points[\"connectivity\"] = None\n", + "\n", + "node_id = 0\n", + "idxs, node_ids, connectivity = [], [], []\n", + "for poly_id, poly_group in tqdm.tqdm(\n", + " df_center_single_boundary_points.groupby(\"poly_id\", sort=False), desc=\"assign node ids\"\n", + "):\n", + " for geom, group in tqdm.tqdm(poly_group.groupby(\"geometry\", sort=False), desc=f\"{poly_id=}\", leave=False):\n", + " idxs.append(group.index)\n", + " node_ids.append(len(group) * [node_id])\n", + " connectivity.append(len(group) * [len(group)])\n", + " node_id += 1\n", + "\n", + "df_center_single_boundary_points.loc[np.hstack(idxs), \"node_id\"] = np.hstack(node_ids)\n", + "df_center_single_boundary_points.loc[np.hstack(idxs), \"connectivity\"] = np.hstack(connectivity)\n", + "idxs, node_ids, connectivity = None, None, None\n", + "\n", + "assert not pd.isna(df_center_single_boundary_points.node_id).any()\n", + "assert not pd.isna(df_center_single_boundary_points.connectivity).any()\n", + "\n", + "df_center_single_boundary_points = df_center_single_boundary_points.droplevel(-1).set_index(\"node_id\", append=True)\n", + "df_center_single_boundary_points" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "# Alleen edges proberen te mergen waarvan beide uiteindes (nodes) connectivity 2 hebben\n", + "pot_reduce = []\n", + "for edge_id, group in tqdm.tqdm(\n", + " df_center_single_boundary_points.groupby(\"edge_id\", sort=False), desc=\"Find connectivity=2\"\n", + "):\n", + " if (group.connectivity == 2).all():\n", + " pot_reduce.append(edge_id)\n", + "pot_reduce = df_center_single_boundary.loc[pd.IndexSlice[:, pot_reduce], :].copy()\n", + "\n", + "# Identify merge groups\n", + "edges_visited = {}\n", + "merge_group = 0\n", + "pot_reduce[\"merge_group\"] = None\n", + "for poly_id, polygroup in tqdm.tqdm(pot_reduce.groupby(\"poly_id\", sort=False), desc=\"group edges per polygon\"):\n", + " for edge_id, group in tqdm.tqdm(polygroup.groupby(\"edge_id\", sort=False), leave=False, desc=f\"{poly_id=}\"):\n", + " if edge_id in edges_visited:\n", + " continue\n", + "\n", + " ivec = np.where(polygroup.index.isin(group.index))[0]\n", + " prev_len = 0\n", + " while len(ivec) != prev_len:\n", + " prev_len = len(ivec)\n", + " ivec = polygroup.sindex.query(polygroup.geometry.iloc[ivec], predicate=\"intersects\")\n", + " ivec = np.unique(ivec[1, :])\n", + "\n", + " lbls = polygroup.index[ivec]\n", + " assert len(pot_reduce.loc[lbls].index.get_level_values(\"poly_id\").unique()) == 1\n", + " pot_reduce.loc[lbls, \"merge_group\"] = merge_group\n", + "\n", + " for eid in lbls.get_level_values(\"edge_id\"):\n", + " edges_visited[eid] = True\n", + " merge_group += 1\n", + "\n", + "# Merge\n", + "df_center_single_red = df_center_single[~df_center_single.index.isin(pot_reduce.index)].copy()\n", + "add_rows = []\n", + "for group_id, group in tqdm.tqdm(pot_reduce.groupby(\"merge_group\", dropna=True, sort=False), desc=\"merge edges\"):\n", + " edges_to_merge = np.unique(group.index.get_level_values(\"edge_id\").to_numpy())\n", + " geoms = df_center_single.geometry.loc[pd.IndexSlice[:, edges_to_merge]].tolist()\n", + " geom = shapely.ops.linemerge(geoms)\n", + " assert geom.geom_type == \"LineString\"\n", + " single_row = df_center_single.loc[pd.IndexSlice[:, edges_to_merge[0]], :].copy()\n", + " single_row.loc[:, \"geometry\"] = geom\n", + " add_rows.append(single_row)\n", + "\n", + "# Overwrite dataframes\n", + "df_center_single = pd.concat([df_center_single_red] + add_rows)\n", + "\n", + "df_center_single_boundary = df_center_single.copy()\n", + "df_center_single_boundary[\"geometry\"] = df_center_single.boundary\n", + "\n", + "df_center_single_boundary_points = df_center_single_boundary.explode(index_parts=True)\n", + "df_center_single_boundary_points[\"node_id\"] = None\n", + "idxs, node_ids = [], []\n", + "for node_id, (geom, group) in enumerate(\n", + " tqdm.tqdm(df_center_single_boundary_points.groupby(\"geometry\", sort=False), desc=\"assign node ids\")\n", + "):\n", + " idxs.append(group.index)\n", + " node_ids.append(len(group) * [node_id])\n", + "df_center_single_boundary_points.loc[np.hstack(idxs), \"node_id\"] = np.hstack(node_ids)\n", + "assert not pd.isna(df_center_single_boundary_points.node_id).any()\n", + "df_center_single_boundary_points = df_center_single_boundary_points.droplevel(-1).set_index(\"node_id\", append=True)\n", + "\n", + "# # Check of alles mooi verbonden is\n", + "# for i, row in tqdm.tqdm(enumerate(df_center_single_boundary.itertuples()), total=len(df_center_single_boundary), desc=\"check connections\"):\n", + "# idx = row.Index\n", + "# geom = row.geometry\n", + "\n", + "# idxs, dists = df_center_single_boundary.sindex.nearest(geom, return_distance=True, return_all=True)\n", + "# idxs = idxs[1, :]\n", + "# dists = dists[idxs != i]\n", + "# idxs = idxs[idxs != i]\n", + "# if dists.min() > 0:\n", + "# print(f\"no closed connection for {idx}, {dist.min()=}\")\n", + "# elif len(idxs) == 0:\n", + "# print(f\"No connection for {idx}: {df_center_single_boundary.iloc[idxs].index}\")\n", + "\n", + "df_center_single_boundary_points" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6", + "metadata": {}, + "outputs": [], + "source": [ + "edge_lengths = dict(zip(df_center_single.index.get_level_values(\"edge_id\"), df_center_single.length))\n", + "shortest_paths = {\"poly_id\": [], \"start_node\": [], \"end_node\": [], \"geometry\": []}\n", + "for poly_id, row in tqdm.tqdm(df_merged.iterrows(), total=len(df_merged)):\n", + " merged_poly = row.geometry\n", + "\n", + " globalids = df.globalid.loc[df.merged_poly_id == poly_id].unique()\n", + " df_crossings_single = df_crossings[\n", + " df_crossings.peilgebied_from.isin(globalids) | df_crossings.peilgebied_to.isin(globalids)\n", + " ].copy()\n", + "\n", + " # End point\n", + " df_graph = df_center_single_boundary_points.loc[pd.IndexSlice[poly_id, :, :], :].copy()\n", + " idx_end, distance_end = df_graph.sindex.nearest(\n", + " merged_poly.representative_point(), return_distance=True, return_all=False\n", + " )\n", + " distance_end = distance_end[0]\n", + " idx_end = idx_end[1, 0]\n", + " idx_end = df_graph.index[idx_end]\n", + " end_node = idx_end[-1]\n", + " df_crossings\n", + " # print(f\"{poly_id=}, closest vertex for endpoint at {distance_end:.2f}m ({idx_end=})\")\n", + "\n", + " # Starting points\n", + " idxs, distances = df_graph.sindex.nearest(df_crossings_single.geometry, return_distance=True, return_all=False)\n", + " idx_cross = df_crossings_single.iloc[idxs[0, :]].index\n", + " df_crossings_single.loc[idx_cross, \"start_node\"] = df_graph.iloc[idxs[1, :]].index.get_level_values(\"node_id\")\n", + " df_crossings.loc[idx_cross, \"start_node\"] = df_graph.iloc[idxs[1, :]].index.get_level_values(\"node_id\")\n", + " start_nodes = df_crossings_single[\"start_node\"].dropna().unique().astype(int).tolist()\n", + "\n", + " # Make network for this polygon\n", + " node_ids = df_graph.index.get_level_values(\"node_id\")\n", + " graph = nx.Graph()\n", + "\n", + " # Add nodes and edges\n", + " graph.add_nodes_from(node_ids.unique().tolist())\n", + " for edge_id, group in df_graph.groupby(\"edge_id\", sort=False):\n", + " node1, node2 = group.index.get_level_values(\"node_id\").tolist()\n", + " graph.add_edge(node1, node2, weight=edge_lengths[edge_id])\n", + "\n", + " # Determine shortest path for each start node\n", + " for start_node in tqdm.tqdm(start_nodes, leave=False, desc=f\"{poly_id=}\"):\n", + " try:\n", + " # node_path = nx.dijkstra_path(graph, start_node, end_node)\n", + " node_path = nx.astar_path(graph, start_node, end_node)\n", + " edges = df_graph.loc[pd.IndexSlice[:, :, node_path]].index.get_level_values(\"edge_id\").to_numpy()\n", + " geom = shapely.ops.linemerge(df_center_single.geometry.loc[pd.IndexSlice[poly_id, edges]].tolist())\n", + " shortest_paths[\"poly_id\"].append(poly_id)\n", + " shortest_paths[\"start_node\"].append(start_node)\n", + " shortest_paths[\"end_node\"].append(end_node)\n", + " shortest_paths[\"geometry\"].append(geom)\n", + " except Exception as e:\n", + " print(e)\n", + " pass\n", + "\n", + "df_startcrossings = df_crossings[~pd.isna(df_crossings.start_node)].copy()\n", + "shortest_paths = gpd.GeoDataFrame(shortest_paths, geometry=\"geometry\", crs=df_crossings.crs)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "df_merged.to_file(\"test_voronoi.gpkg\", layer=\"merged_poly\")\n", + "df_center_single.to_file(\"test_voronoi.gpkg\", layer=\"edges\")\n", + "df_center_single_boundary_points.to_file(\"test_voronoi.gpkg\", layer=\"nodes\")\n", + "shortest_paths.to_file(\"test_voronoi.gpkg\", layer=\"shortest_paths\")\n", + "df_startcrossings.to_file(\"test_voronoi.gpkg\", layer=\"start_crossings\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8", + "metadata": {}, + "outputs": [], + "source": [ + "# # Check of alles mooi verbonden is\n", + "# for poly_id, polygroup in tqdm.tqdm(df_center_single_boundary.groupby(\"poly_id\", sort=False), desc=\"check connections\"):\n", + "# for i, row in enumerate(polygroup.itertuples()):\n", + "# idx = row.Index\n", + "# geom = row.geometry\n", + "\n", + "# idxs = polygroup.sindex.query(geom, predicate=\"intersects\")\n", + "# idxs = idxs[idxs != i]\n", + "# dists = polygroup.geometry.iloc[idxs].distance(row.geometry)\n", + "# if dists.min() > 0:\n", + "# print(f\"no closed connection for {idx}, {dist.min()=}\")\n", + "# if len(idxs) == 0:\n", + "# print(f\"No connection for {idx}: {polygroup.iloc[idxs].index}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:ribasim]", + "language": "python", + "name": "conda-env-ribasim-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/peilbeheerst_model/peilbeheerst_model/add_storage_basins.py b/src/peilbeheerst_model/peilbeheerst_model/add_storage_basins.py index db3cd94..5c7cf43 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/add_storage_basins.py +++ b/src/peilbeheerst_model/peilbeheerst_model/add_storage_basins.py @@ -1,7 +1,6 @@ import logging from pathlib import Path -# import numpy as np import pandas as pd from ribasim import Model from shapely.geometry import Point diff --git a/src/peilbeheerst_model/peilbeheerst_model/controle_output.py b/src/peilbeheerst_model/peilbeheerst_model/controle_output.py index ffdb118..2dbf817 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/controle_output.py +++ b/src/peilbeheerst_model/peilbeheerst_model/controle_output.py @@ -199,6 +199,8 @@ def is_stationary(group): return control_dict + # def inspect_individual_basins(self, data): + def store_data(self, data, output_path): """Store the control_dict""" for key in data.keys(): diff --git a/src/peilbeheerst_model/peilbeheerst_model/crossings_to_ribasim.py b/src/peilbeheerst_model/peilbeheerst_model/crossings_to_ribasim.py index a4ea5d8..6510a8c 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/crossings_to_ribasim.py +++ b/src/peilbeheerst_model/peilbeheerst_model/crossings_to_ribasim.py @@ -715,7 +715,8 @@ def discard_duplicate_boezems(boezems): # add a column if a shortest path is found edges["bool_SP"] = edges["line_geom"] edges["bool_SP"].loc[edges["bool_SP"].isna()] = False - edges["bool_SP"].loc[edges["bool_SP"]] = True + # edges["bool_SP"].loc[edges["bool_SP"]] = True + edges["bool_SP"].loc[edges["bool_SP"] != False] = True # noqa: E712 # fill the line geoms with the previous geoms if no shortest path is found edges.line_geom = edges.line_geom.fillna(edges.line_geom_oud) @@ -767,47 +768,51 @@ def change_edge(self, edges, from_node_id_to_change, to_node_id_to_change, from_ def change_boezems_manually(self, edges): if self.model_characteristics["waterschap"] == "HollandsNoorderkwartier": edges = self.change_edge( - edges, from_node_id_to_change=455, to_node_id_to_change=5, from_node_id_geom=456, to_node_id_geom=5 + edges, from_node_id_to_change=447, to_node_id_to_change=3, from_node_id_geom=83, to_node_id_geom=447 ) edges = self.change_edge( - edges, from_node_id_to_change=475, to_node_id_to_change=5, from_node_id_geom=476, to_node_id_geom=5 + edges, from_node_id_to_change=395, to_node_id_to_change=3, from_node_id_geom=68, to_node_id_geom=395 ) edges = self.change_edge( - edges, from_node_id_to_change=574, to_node_id_to_change=5, from_node_id_geom=575, to_node_id_geom=5 + edges, from_node_id_to_change=571, to_node_id_to_change=3, from_node_id_geom=126, to_node_id_geom=571 ) edges = self.change_edge( - edges, from_node_id_to_change=582, to_node_id_to_change=5, from_node_id_geom=584, to_node_id_geom=5 + edges, from_node_id_to_change=1287, to_node_id_to_change=3, from_node_id_geom=226, to_node_id_geom=1287 ) edges = self.change_edge( - edges, from_node_id_to_change=641, to_node_id_to_change=5, from_node_id_geom=642, to_node_id_geom=5 + edges, from_node_id_to_change=911, to_node_id_to_change=3, from_node_id_geom=101, to_node_id_geom=911 ) edges = self.change_edge( - edges, from_node_id_to_change=851, to_node_id_to_change=5, from_node_id_geom=854, to_node_id_geom=5 + edges, from_node_id_to_change=383, to_node_id_to_change=3, from_node_id_geom=65, to_node_id_geom=383 ) edges = self.change_edge( - edges, from_node_id_to_change=957, to_node_id_to_change=5, from_node_id_geom=959, to_node_id_geom=5 + edges, from_node_id_to_change=817, to_node_id_to_change=3, from_node_id_geom=119, to_node_id_geom=817 ) edges = self.change_edge( - edges, from_node_id_to_change=1061, to_node_id_to_change=5, from_node_id_geom=1065, to_node_id_geom=5 + edges, from_node_id_to_change=366, to_node_id_to_change=3, from_node_id_geom=57, to_node_id_geom=366 ) edges = self.change_edge( - edges, from_node_id_to_change=1198, to_node_id_to_change=5, from_node_id_geom=1201, to_node_id_geom=5 + edges, from_node_id_to_change=717, to_node_id_to_change=3, from_node_id_geom=13, to_node_id_geom=717 ) edges = self.change_edge( - edges, from_node_id_to_change=1295, to_node_id_to_change=5, from_node_id_geom=1299, to_node_id_geom=5 + edges, from_node_id_to_change=487, to_node_id_to_change=3, from_node_id_geom=98, to_node_id_geom=487 ) edges = self.change_edge( - edges, from_node_id_to_change=1513, to_node_id_to_change=5, from_node_id_geom=1516, to_node_id_geom=5 + edges, from_node_id_to_change=481, to_node_id_to_change=3, from_node_id_geom=96, to_node_id_geom=481 + ) + + edges = self.change_edge( + edges, from_node_id_to_change=1027, to_node_id_to_change=3, from_node_id_geom=197, to_node_id_geom=1027 ) return edges @@ -937,7 +942,7 @@ def basin(self): basin_static["potential_evaporation"] = 0 basin_static["infiltration"] = 0 basin_static["precipitation"] = 0 - basin_static["urban_runoff"] = 0 + # basin_static["urban_runoff"] = 0 # display(basin_nodes) basin_area = basin_nodes[["node_id", "streefpeil", "basins_area_geom"]] @@ -1604,7 +1609,7 @@ def add_meta_data(self, model, checks, post_processed_data, crossings): points_within = gpd.sjoin( basin_nodes, checks["boezem"], how="inner", predicate="within" ) # find the basins which are within a peilgebied (found in the checks) - model.basin.state.df.meta_categorie.loc[points_within.index] = ( + model.basin.state.df.meta_categorie.loc[points_within.index - 1] = ( "hoofdwater" # set these basins to become peilgebied_cat == 1, or 'doorgaand' ) diff --git a/src/peilbeheerst_model/peilbeheerst_model/outlets.py b/src/peilbeheerst_model/peilbeheerst_model/outlets.py new file mode 100644 index 0000000..b1c653b --- /dev/null +++ b/src/peilbeheerst_model/peilbeheerst_model/outlets.py @@ -0,0 +1,623 @@ +import os +import subprocess +import warnings + +import pandas as pd +import yaml +from ribasim import Model, Node +from ribasim.nodes import basin, discrete_control, level_boundary, outlet, pump +from shapely.geometry import Point + +warnings.filterwarnings("ignore") + + +class case1: + """ + Create the first Ribasim schematisation with outlets. + + It contains the simple case of a boezem, two cascaded peilgebieden with an outlet in between, + after which the water is pumped through a Pump node to the boezem again. + """ + + def __init__(self, case_example_name): + """Initialize the class. Convert the forcing from mm/day to m/s.""" + case_example_path = os.path.join("../../../../Outlet_tests/json", case_example_name + ".json") + + with open(case_example_path) as file: + characteristics = yaml.safe_load(file) + + self.characteristics = characteristics + + # add the example and case to the characteristics + case, example = case_example_name.split("_") + + self.characteristics["case"] = case + self.characteristics["example"] = example + + self.characteristics["evaporation"] = ( + self.characteristics["evaporation"] / 1000 / 3600 / 24 + ) # convert from mm/day to m/s + self.characteristics["precipitation"] = ( + self.characteristics["precipitation"] / 1000 / 3600 / 24 + ) # convert from mm/day to m/s + + def create_empty_model(self): + """Create an empty Ribasim model.""" + model = Model( + starttime=self.characteristics["starttime"], + endtime=self.characteristics["endtime"], + crs=self.characteristics["crs"], + ) + return model + + def add_boezems(self, model): + """Add the boezems to the model, based on the pre-defined model characteristics.""" + # add the boezems + model.level_boundary.add( + Node(node_id=1, geometry=Point(0.0, 0.0), name="boezem1"), + [level_boundary.Static(level=[self.characteristics["boezem1_level"]])], + ) + + model.level_boundary.add( + Node(node_id=2, geometry=Point(6.0, 0.0), name="boezem2"), + [level_boundary.Static(level=[self.characteristics["boezem2_level"]])], + ) + return model + + def add_peilgebieden(self, model): + """Add the peilgebieden to the model, based on the pre-defined model characteristics.""" + # add peilgebied1 + basin1_data = [ + basin.Profile( + area=self.characteristics["basin1_profile_area"], level=self.characteristics["basin1_profile_level"] + ), + basin.Time( + time=pd.date_range(self.characteristics["starttime"], self.characteristics["endtime"]), + drainage=0.0, + potential_evaporation=self.characteristics["evaporation"], + infiltration=0.0, + precipitation=self.characteristics["precipitation"], + ), + basin.State(level=self.characteristics["basin1_initial_level"]), + ] + + model.basin.add(Node(node_id=3, geometry=Point(2.0, 0.0), name="peilgebied1"), basin1_data) + + # add peilgebied2 + basin2_data = [ + basin.Profile( + area=self.characteristics["basin2_profile_area"], level=self.characteristics["basin2_profile_level"] + ), + basin.Time( + time=pd.date_range(self.characteristics["starttime"], self.characteristics["endtime"]), + drainage=0.0, + potential_evaporation=self.characteristics["evaporation"], + infiltration=0.0, + precipitation=self.characteristics["precipitation"], + ), + basin.State(level=self.characteristics["basin2_initial_level"]), + ] + + model.basin.add(Node(node_id=4, geometry=Point(4.0, 0.0), name="peilgebied2"), basin2_data) + + return model + + def add_connection_nodes(self, model): + """Add the "connection nodes" to the model (Outlets, Pumps), based on the pre-defined model characteristics.""" + # add the connection nodes + ( + model.outlet.add( + Node(node_id=5, geometry=Point(1.0, 0.0), name="Outlet1"), + [ + outlet.Static( + control_state=["pass", "block"], + flow_rate=[self.characteristics["outlet1_flow_rate"], 0], + min_crest_level=self.characteristics["outlet1_min_crest_level"], + ) + ], + ), + ) + + model.outlet.add( + Node(node_id=6, geometry=Point(3.0, 0.0), name="Outlet2"), + [ + outlet.Static( + control_state=["pass", "block"], + flow_rate=[self.characteristics["outlet2_flow_rate"], 0], + min_crest_level=self.characteristics["outlet2_min_crest_level"], + ) + ], + ) + + model.pump.add( + Node(node_id=7, geometry=Point(5.0, 0.0), name="Pump1"), + [pump.Static(control_state=["pass", "block"], flow_rate=[self.characteristics["pump1_flow_rate"], 0])], + ) + + return model + + def add_discrete_control(self, model): + """Add discrete control for each "connecton node", such as Pumps and Outlets.""" + # add the discrete control between the boezem and the basin + model.discrete_control.add( + Node(node_id=100, geometry=Point(1, 1), name="Outlet_DC_1"), + [ + discrete_control.Variable( + compound_variable_id=1, + listen_node_id=[1, 3], + listen_node_type=["LevelBoundary", "Basin"], + variable=["level", "Level"], + ), + discrete_control.Condition( + compound_variable_id=1, + greater_than=[2.95, self.characteristics["basin1_target_level"]], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], + control_state=[ + "block", + "block", + "pass", + "block", + ], # block 1 & 2: block when boezem level drops below 2.95 + ), + ], + ) + + # add the discrete control between the basins + model.discrete_control.add( + Node(node_id=101, geometry=Point(3, 1), name="Outlet_DC_2"), + [ + discrete_control.Variable( + compound_variable_id=2, + listen_node_id=[3, 4], + listen_node_type=["Basin", "Basin"], + variable=["level", "level"], + ), + discrete_control.Condition( + compound_variable_id=2, + greater_than=[ + self.characteristics["basin1_target_level"] - 0.05, + self.characteristics["basin2_target_level"], + ], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], control_state=["block", "block", "pass", "block"] + ), + ], + ) + + # add the discrete control between the basin and the boezem + model.discrete_control.add( + Node(node_id=102, geometry=Point(5, 1), name="Outlet_DC_1"), + [ + discrete_control.Variable( + compound_variable_id=3, + listen_node_id=[4], + listen_node_type=["Basin"], + variable=["level"], + ), + discrete_control.Condition( + compound_variable_id=3, + greater_than=[self.characteristics["basin2_target_level"]], + ), + discrete_control.Logic(truth_state=["F", "T"], control_state=["block", "pass"]), + ], + ) + + return model + + def add_edges(self, model): + """Add edges between each node.""" + # add the edges + model.edge.add(model.level_boundary[1], model.outlet[5]) + model.edge.add(model.outlet[5], model.basin[3]) + model.edge.add(model.basin[3], model.outlet[6]) + model.edge.add(model.outlet[6], model.basin[4]) + model.edge.add(model.basin[4], model.pump[7]) + model.edge.add(model.pump[7], model.level_boundary[2]) + + # add the edges for the discrete control + model.edge.add(model.discrete_control[100], model.outlet[5]) + model.edge.add(model.discrete_control[101], model.outlet[6]) + model.edge.add(model.discrete_control[102], model.pump[7]) + + return model + + def store_model(self, model): + """Plot and store the model.""" + # apply the settings for the solver + model.solver.saveat = self.characteristics["saveat"] + + # plot the model + if self.characteristics["plot"]: + model.plot() + + # toml_dir = os.path.join( + # self.characteristics["results_dir"], self.characteristics["case"], self.characteristics["example"] + # ) + # create the directory where the model should be stored + if not os.path.exists(os.path.join(self.characteristics["results_dir"], self.characteristics["case"])): + os.makedirs(os.path.join(self.characteristics["results_dir"], self.characteristics["case"])) + + # store the model + model.write( + filepath=os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ) + ) + + def run_model(self, model): + """Run the created Ribasim model.""" + if self.characteristics["show_progress"]: + # show progress of the Ribasim model + subprocess.run( + [ + "ribasim", + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ), + ], + check=False, + ) + else: + subprocess.run( + [ + "ribasim", + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ), + ], + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + def show_results(self, model): + """Load and plot some results.""" + if self.characteristics["show_results"]: + # load in the data + df_basin = pd.read_feather( + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "results", + "basin.arrow", + ) + ) + + # plot the levels + df_basin_wide = df_basin.pivot_table(index="time", columns="node_id", values=["level"]) + df_basin_wide["level"].plot() + + # display(df_basin) + + def create_model(self, copy=False): + """Create the model by running all the functions.""" + model = self.create_empty_model() + model = self.add_boezems(model) + model = self.add_peilgebieden(model) + model = self.add_connection_nodes(model) + model = self.add_discrete_control(model) + model = self.add_edges(model) + + if copy: + return model + + else: + self.store_model(model) + self.run_model(model) + self.show_results(model) + + +class case2: + """ + Create the second Ribasim schematisation case with an additional peilgebied. + + It builds upon case1 which had a simple case of a boezem, two cascaded peilgebieden with an outlet in between, + after which the water is pumped through a Pump node to the boezem again. + In this case, a third peilgebied is added with logical flow direction from and to the boezem and other peilgebieden. + """ + + def __init__(self, case_example_name, model): + """Initialize the class. Convert the forcing from mm/day to m/s.""" + case_example_path = os.path.join("../../../../Outlet_tests/json", case_example_name + ".json") + + with open(case_example_path) as file: + characteristics = yaml.safe_load(file) + + # define the characteristics and the base model + self.characteristics = characteristics + self.model = model + + # add the example and case to the characteristics + case, example = case_example_name.split("_") + + self.characteristics["case"] = case + self.characteristics["example"] = example + + self.characteristics["evaporation"] = ( + self.characteristics["evaporation"] / 1000 / 3600 / 24 + ) # convert from mm/day to m/s + self.characteristics["precipitation"] = ( + self.characteristics["precipitation"] / 1000 / 3600 / 24 + ) # convert from mm/day to m/s + + def add_peilgebied(self, model): + """Add the peilgebieden to the model, based on the pre-defined model characteristics.""" + # add peilgebied1 + basin3_data = [ + basin.Profile( + area=self.characteristics["basin3_profile_area"], level=self.characteristics["basin3_profile_level"] + ), + basin.Time( + time=pd.date_range(self.characteristics["starttime"], self.characteristics["endtime"]), + drainage=0.0, + potential_evaporation=self.characteristics["evaporation"], + infiltration=0.0, + precipitation=self.characteristics["precipitation"], + ), + basin.State(level=self.characteristics["basin3_initial_level"]), + ] + + model.basin.add(Node(node_id=8, geometry=Point(3.0, -2.0), name="peilgebied3"), basin3_data) + + return model + + def add_connection_nodes(self, model): + """Add the "connection nodes" to the model (Outlets, Pumps), based on the pre-defined model characteristics.""" + # add the connection nodes + model.outlet.add( + Node(node_id=9, geometry=Point(1.5, -1.0), name="Outlet3"), + [ + outlet.Static( + control_state=["pass", "block"], + flow_rate=[self.characteristics["outlet4_flow_rate"], 0], + min_crest_level=self.characteristics["outlet4_min_crest_level"], + ) + ], + ) + + ( + model.outlet.add( + Node(node_id=10, geometry=Point(2.5, -1.0), name="Outlet4"), + [ + outlet.Static( + control_state=["pass", "block"], + flow_rate=[self.characteristics["outlet3_flow_rate"], 0], + min_crest_level=self.characteristics["outlet3_min_crest_level"], + ) + ], + ), + ) + + model.outlet.add( + Node(node_id=11, geometry=Point(3.5, -1.0), name="Outlet5"), + [ + outlet.Static( + control_state=["pass", "block"], + flow_rate=[self.characteristics["outlet4_flow_rate"], 0], + min_crest_level=self.characteristics["outlet4_min_crest_level"], + ) + ], + ) + + model.pump.add( + Node(node_id=12, geometry=Point(4.5, -1.0), name="Pump2"), + [pump.Static(control_state=["pass", "block"], flow_rate=[self.characteristics["pump2_flow_rate"], 0])], + ) + + return model + + def add_discrete_control(self, model): + """Add discrete control for each "connecton node", such as Pumps and Outlets.""" + # add the discrete control between the boezem and the basin + model.discrete_control.add( + Node(node_id=103, geometry=Point(0, -2), name="Outlet_DC_4"), + [ + discrete_control.Variable( + compound_variable_id=4, + listen_node_id=[1, 8], + listen_node_type=["LevelBoundary", "Basin"], + variable=["level", "level"], + ), + discrete_control.Condition( + compound_variable_id=4, + greater_than=[2.95, self.characteristics["basin3_target_level"]], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], control_state=["block", "block", "pass", "block"] + ), + ], + ) + + # add the discrete control between the boezem and the basin + model.discrete_control.add( + Node(node_id=104, geometry=Point(2, -1), name="Outlet_DC_5"), + [ + discrete_control.Variable( + compound_variable_id=5, + listen_node_id=[8, 3], + listen_node_type=["Basin", "Basin"], + variable=["level", "level"], + ), + discrete_control.Condition( + compound_variable_id=5, + greater_than=[ + self.characteristics["basin3_target_level"] - 0.05, + self.characteristics["basin1_target_level"], + ], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], control_state=["block", "block", "pass", "block"] + ), + ], + ) + + # add the discrete control between the basins + model.discrete_control.add( + Node(node_id=105, geometry=Point(4, -1), name="Outlet_DC_6"), + [ + discrete_control.Variable( + compound_variable_id=6, + listen_node_id=[8, 4], + listen_node_type=["Basin", "Basin"], + variable=["level", "level"], + ), + discrete_control.Condition( + compound_variable_id=6, + greater_than=[ + self.characteristics["basin3_target_level"] - 0.05, + self.characteristics["basin2_target_level"], + ], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], control_state=["block", "block", "pass", "block"] + ), + ], + ) + + # add the discrete control between the basin and the boezem + model.discrete_control.add( + Node(node_id=106, geometry=Point(6, -2), name="Outlet_DC_7"), + [ + discrete_control.Variable( + compound_variable_id=7, + listen_node_id=[8, 2], + listen_node_type=["Basin", "LevelBoundary"], + variable=["level", "level"], + ), + discrete_control.Condition( + compound_variable_id=7, + greater_than=[self.characteristics["basin3_target_level"] - 0.05, 295], + ), + discrete_control.Logic( + truth_state=["FF", "FT", "TF", "TT"], control_state=["block", "block", "pass", "block"] + ), + ], + ) + + return model + + def store_model(self, model): + """Plot and store the model.""" + # apply the settings for the solver + model.solver.saveat = self.characteristics["saveat"] + + # plot the model + if self.characteristics["plot"]: + model.plot() + + # toml_dir = os.path.join( + # self.characteristics["results_dir"], self.characteristics["case"], self.characteristics["example"] + # ) + # create the directory where the model should be stored + if not os.path.exists(os.path.join(self.characteristics["results_dir"], self.characteristics["case"])): + os.makedirs(os.path.join(self.characteristics["results_dir"], self.characteristics["case"])) + + # store the model + model.write( + filepath=os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ) + ) + + def run_model(self, model): + """Run the created Ribasim model.""" + if self.characteristics["show_progress"]: + # show progress of the Ribasim model + subprocess.run( + [ + "ribasim", + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ), + ], + check=False, + ) + else: + subprocess.run( + [ + "ribasim", + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "ribasim.toml", + ), + ], + check=False, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + def show_results(self, model): + """Load and plot some results.""" + if self.characteristics["show_results"]: + # load in the data + df_basin = pd.read_feather( + os.path.join( + self.characteristics["results_dir"], + self.characteristics["case"], + self.characteristics["example"], + "results", + "basin.arrow", + ) + ) + + # plot the levels + df_basin_wide = df_basin.pivot_table(index="time", columns="node_id", values=["level"]) + df_basin_wide["level"].plot() + + # display(df_basin) + + def add_edges(self, model): + """Add edges between each node.""" + # add the edges + model.edge.add(model.level_boundary[1], model.outlet[9]) + model.edge.add(model.outlet[9], model.basin[8]) + model.edge.add(model.basin[8], model.outlet[10]) + model.edge.add(model.outlet[10], model.basin[3]) + model.edge.add(model.basin[8], model.outlet[11]) + model.edge.add(model.outlet[11], model.basin[4]) + model.edge.add(model.basin[8], model.pump[12]) + model.edge.add(model.pump[12], model.level_boundary[2]) + + # add the edges for the discrete control + model.edge.add(model.discrete_control[103], model.outlet[9]) + model.edge.add(model.discrete_control[104], model.outlet[10]) + model.edge.add(model.discrete_control[105], model.outlet[11]) + model.edge.add(model.discrete_control[106], model.pump[12]) + + return model + + def create_model(self, copy=False): + """Create the model by running all the functions.""" + model = self.add_peilgebied(self.model) + model = self.add_connection_nodes(model) + model = self.add_discrete_control(model) + model = self.add_edges(model) + + if copy: + return model + + else: + self.store_model(model) + self.run_model(model) + self.show_results(model) diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/general_functions.py b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/general_functions.py index 0e40153..d12a988 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/general_functions.py +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/general_functions.py @@ -1,5 +1,4 @@ # import packages and functions - import geopandas as gpd import matplotlib.pyplot as plt import pandas as pd diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_WSRL.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_WSRL.ipynb index e0569be..3fcfcff 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_WSRL.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_WSRL.ipynb @@ -28,7 +28,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -49,16 +54,16 @@ "# define relative paths\n", "waterschap = \"WSRL\"\n", "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_wsrl.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_wsrl.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -86,6 +91,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "WSRL[\"peilgebied\"] = WSRL[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -161,18 +167,10 @@ "print(f\"Number of overlapping shapes with filter: {len(overlap_ids)}\")" ] }, - { - "cell_type": "markdown", - "id": "11", - "metadata": {}, - "source": [ - "## Create peilgebied_cat column" - ] - }, { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -181,7 +179,9 @@ "\n", "for index, row in WSRL[\"peilgebied\"].iterrows():\n", " if row.CODE == \"LNG014-P\":\n", + " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", + "\n", " else:\n", " peilgebieden_cat.append(0)\n", "\n", @@ -192,9 +192,48 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, + "id": "12", + "metadata": {}, + "outputs": [], + "source": [ + "# list(WSRL['peilgebied'].code.unique())" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "13", "metadata": {}, + "outputs": [], + "source": [ + "# add boezems\n", + "codes_to_update = [\n", + " \"NDB004-P\",\n", + " \"LNG013-P\",\n", + " \"LNG012-P\",\n", + " \"LNG011-P\",\n", + " \"LNG010-P\",\n", + " \"LNG009-P\",\n", + " \"LNG008-P\",\n", + " \"LNG007-P\",\n", + " \"LNG006-P\",\n", + " \"LNG005-P\",\n", + " \"LNG304-P\",\n", + " \"LNG002-P\",\n", + " \"LNG001-P\",\n", + " \"LNG014-P_extra\",\n", + " \"NDW100-P\",\n", + " \"OVW200-P\",\n", + "]\n", + "WSRL[\"peilgebied\"].loc[WSRL[\"peilgebied\"][\"code\"].isin(codes_to_update), \"peilgebied_cat\"] = 1" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, "source": [ "## Adjust globalid, code, nen3610id ['streefpeil'], ['peilgebied']" ] @@ -202,7 +241,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -225,7 +264,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -234,7 +273,7 @@ }, { "cell_type": "markdown", - "id": "16", + "id": "17", "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" @@ -243,7 +282,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -261,7 +300,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "19", "metadata": {}, "outputs": [], "source": [ @@ -278,7 +317,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "20", "metadata": {}, "outputs": [], "source": [ @@ -287,7 +326,7 @@ }, { "cell_type": "markdown", - "id": "20", + "id": "21", "metadata": {}, "source": [ "### Create buffer layer that ensures spatial match between peilgebied and hws layers based on the buffer layer" @@ -296,25 +335,25 @@ { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "22", "metadata": {}, "outputs": [], "source": [ - "buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", - "buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", - "buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(WSRL[\"peilgebied\"].geometry.tolist()))\n", + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", + "# buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(WSRL['peilgebied'].geometry.tolist()))\n", "\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon = buffer_polygon.set_geometry(0)\n", - "buffer_polygon = buffer_polygon.dissolve()\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon.set_geometry(\"geometry\")\n", - "buffer_polygon = buffer_polygon.set_crs(\"EPSG:28992\")" + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')" ] }, { "cell_type": "markdown", - "id": "22", + "id": "23", "metadata": {}, "source": [ "## Add buffer to ['peilgebied']" @@ -322,7 +361,7 @@ }, { "cell_type": "markdown", - "id": "23", + "id": "24", "metadata": {}, "source": [ "## Add buffer to ['peilgebied','streefpeil']" @@ -331,33 +370,44 @@ { "cell_type": "code", "execution_count": null, - "id": "24", + "id": "25", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", - "WSRL[\"streefpeil\"] = pd.concat([streefpeil_buffer, WSRL[\"streefpeil\"]])\n", - "WSRL[\"streefpeil\"] = gpd.GeoDataFrame(WSRL[\"streefpeil\"])" + "# WSRL['streefpeil'] = pd.concat([streefpeil_buffer, WSRL['streefpeil']])\n", + "# WSRL['streefpeil'] = gpd.GeoDataFrame(WSRL['streefpeil'])" ] }, { "cell_type": "code", "execution_count": null, - "id": "25", + "id": "26", "metadata": {}, "outputs": [], "source": [ - "WSRL[\"peilgebied\"].globalid.is_unique" + "if remove_cat_2:\n", + " WSRL[\"peilgebied\"] = WSRL[\"peilgebied\"].loc[WSRL[\"peilgebied\"].peilgebied_cat != 2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27", + "metadata": {}, + "outputs": [], + "source": [ + "WSRL[\"peilgebied\"][\"peilgebied_cat\"].unique()" ] }, { "cell_type": "markdown", - "id": "26", + "id": "28", "metadata": {}, "source": [ "## Store output" @@ -366,7 +416,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "29", "metadata": {}, "outputs": [], "source": [ @@ -374,6 +424,38 @@ " print(key)\n", " WSRL[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "32", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_agv.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_agv.ipynb index 97993ba..675b020 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_agv.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_agv.ipynb @@ -26,10 +26,16 @@ "metadata": {}, "outputs": [], "source": [ + "# from itertools import combinations\n", + "\n", "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -51,22 +57,22 @@ "waterschap = \"AmstelGooienVecht\"\n", "waterschap2 = \"AGV\"\n", "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", - "\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", + "# \"Z:\\projects\\4750_20\\Data_postprocessed\\Waterschappen\\AmstelGooienVecht\\AGV.gpkg\"\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER//projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_agv.gpkg\"\n", + "buffer_path = r\"/DATAFOLDER//projects/4750_30/Data_overig/HWS/hws_buffer_agv.gpkg\"\n", "\n", - "# Buffer RWHS\n", - "rhws_path = \"../projects/4750_30/Data_overig/HWS/agv_rhws_buffer.gpkg\"\n", + "# # Buffer RWHS\n", + "# rhws_path = f\"/DATAFOLDER//projects/4750_30/Data_overig/HWS/agv_rhws_buffer.gpkg\"\n", "\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -94,6 +100,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "AVG[\"peilgebied\"] = AVG[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -111,61 +118,50 @@ "gdf_buffer = gdf_buffer.to_crs(\"EPSG:28992\")\n", "gdf_buffer = gdf_buffer.dissolve()\n", "\n", - "# Load rhws\n", - "gdf_rhws = gpd.read_file(rhws_path)\n", - "gdf_rhws = gdf_rhws.to_crs(\"EPSG:28992\")\n", - "gdf_rhws = gdf_rhws.dissolve()" - ] - }, - { - "cell_type": "markdown", - "id": "7", - "metadata": {}, - "source": [ - "## Select waterschap boundaries and clip hws layer" + "# # Load rhws\n", + "# gdf_rhws = gpd.read_file(rhws_path)\n", + "# gdf_rhws = gdf_rhws.to_crs('EPSG:28992')\n", + "# gdf_rhws = gdf_rhws.dissolve()" ] }, { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "7", "metadata": {}, "outputs": [], "source": [ - "# Select boundaries HH Amstel, Gooi en Vecht\n", - "gdf_grens = gdf_grens.loc[[\"HH Amstel, Gooi en Vecht\"]]\n", - "\n", - "# Use waterschap boudnaries to clip HWS layer\n", - "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")\n", - "\n", - "# Use waterschap boudnaries to clip HWS layer\n", - "gdf_rhws = gpd.overlay(gdf_grens, gdf_rhws, how=\"intersection\")" + "AVG[\"peilgebied\"].globalid.is_unique" ] }, { "cell_type": "markdown", - "id": "9", + "id": "8", "metadata": {}, "source": [ - "## RHWS is not included in AGV bron data, therefore create inverse difference layer to extract" + "## Select waterschap boundaries and clip hws layer" ] }, { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "9", "metadata": {}, "outputs": [], "source": [ - "# Select inverse of peilgebied\n", - "gdf_rhws = gpd.overlay(gdf_rhws, AVG[\"peilgebied\"], how=\"difference\")\n", - "gdf_rhws = gpd.overlay(gdf_rhws, gdf_hws, how=\"difference\")\n", - "gdf_rhws.plot()" + "# Select boundaries HH Amstel, Gooi en Vecht\n", + "gdf_grens = gdf_grens.loc[[\"HH Amstel, Gooi en Vecht\"]]\n", + "\n", + "# Use waterschap boudnaries to clip HWS layer\n", + "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")\n", + "\n", + "# # Use waterschap boudnaries to clip HWS layer\n", + "# gdf_rhws = gpd.overlay(gdf_grens, gdf_rhws, how='intersection')" ] }, { "cell_type": "markdown", - "id": "11", + "id": "10", "metadata": {}, "source": [ "## Peilgebied and HWS layer overlap:\n", @@ -178,7 +174,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -204,7 +200,7 @@ { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -212,16 +208,21 @@ "peilgebieden_cat = []\n", "\n", "for index, row in AVG[\"peilgebied\"].iterrows():\n", - " peilgebieden_cat.append(0)\n", + " # if row.code == \"Oosterpark\" or row.code == \"Vechtboezem\":\n", + " if \"Oosterpark\" in row.code or \"Vechtboezem\" in row.code or \"Stadsboezem Amsterdam\" in row.code:\n", + " print(\"true\")\n", + "\n", + " peilgebieden_cat.append(1)\n", + " else:\n", + " peilgebieden_cat.append(0)\n", "\n", "# Add new column and drop old HWS_BZM column\n", - "AVG[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat\n", - "# HD['peilgebied'] = HD['peilgebied'].drop(columns=['streefpeil'])" + "AVG[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat" ] }, { "cell_type": "markdown", - "id": "14", + "id": "13", "metadata": {}, "source": [ "## Add rhws to ['peilgebied','streefpeil']" @@ -230,41 +231,41 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "14", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "gdf_rhws[\"globalid\"] = \"dummy_globalid_rhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"code\"] = \"dummy_code_nhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"nen3610id\"] = \"dummy_nen3610id_rhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"peilgebied_cat\"] = 1\n", + "# # update peilgebied dict key\n", + "# gdf_rhws['globalid'] = 'dummy_globalid_rhws_' + gdf_rhws.index.astype(str)\n", + "# gdf_rhws['code'] = 'dummy_code_nhws_' + gdf_rhws.index.astype(str)\n", + "# gdf_rhws['nen3610id'] = 'dummy_nen3610id_rhws_' + gdf_rhws.index.astype(str)\n", + "# gdf_rhws['peilgebied_cat'] = 1\n", "\n", - "gdf_rhws = gdf_rhws[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# gdf_rhws = gdf_rhws[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "AVG[\"peilgebied\"] = pd.concat([gdf_rhws, AVG[\"peilgebied\"]])" + "# AVG['peilgebied'] = pd.concat([gdf_rhws, AVG['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "15", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_hws = pd.DataFrame()\n", - "streefpeil_hws[\"waterhoogte\"] = [np.nan] * len(gdf_rhws)\n", - "streefpeil_hws[\"globalid\"] = \"dummy_globalid_rhws_\" + gdf_rhws.index.astype(str)\n", - "streefpeil_hws[\"geometry\"] = [None] * len(gdf_rhws)\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_hws = pd.DataFrame()\n", + "# streefpeil_hws['waterhoogte'] = [np.nan] * len(gdf_rhws)\n", + "# streefpeil_hws['globalid'] = 'dummy_globalid_rhws_' + gdf_rhws.index.astype(str)\n", + "# streefpeil_hws['geometry'] = [None]* len(gdf_rhws)\n", "\n", - "AVG[\"streefpeil\"] = pd.concat([streefpeil_hws, AVG[\"streefpeil\"]])\n", - "AVG[\"streefpeil\"] = gpd.GeoDataFrame(AVG[\"streefpeil\"])" + "# AVG['streefpeil'] = pd.concat([streefpeil_hws, AVG['streefpeil']])\n", + "# AVG['streefpeil'] = gpd.GeoDataFrame(AVG['streefpeil'])" ] }, { "cell_type": "markdown", - "id": "17", + "id": "16", "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" @@ -273,7 +274,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -291,7 +292,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -307,8 +308,92 @@ }, { "cell_type": "markdown", + "id": "19", + "metadata": {}, + "source": [ + "### Create buffer polygon between NHWS and peilgebied/RHWS" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "20", "metadata": {}, + "outputs": [], + "source": [ + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", + "# buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(AVG['peilgebied'].geometry.tolist()))\n", + "\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')" + ] + }, + { + "cell_type": "markdown", + "id": "21", + "metadata": {}, + "source": [ + "### Add buffer to ['peilgebied','streefpeil']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22", + "metadata": {}, + "outputs": [], + "source": [ + "# # update peilgebied dict key\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", + "\n", + "# AVG['peilgebied'] = pd.concat([buffer_polygon, AVG['peilgebied']])\n", + "# AVG['peilgebied'] = gpd.GeoDataFrame(AVG['peilgebied'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [ + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# streefpeil_buffer['geometry'] = [None]\n", + "\n", + "\n", + "# AVG['streefpeil'] = pd.concat([streefpeil_buffer, AVG['streefpeil']])\n", + "# AVG['streefpeil'] = gpd.GeoDataFrame(AVG['streefpeil'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " AVG[\"peilgebied\"] = AVG[\"peilgebied\"].loc[AVG[\"peilgebied\"].peilgebied_cat != 2]" + ] + }, + { + "cell_type": "markdown", + "id": "25", + "metadata": {}, "source": [ "## Store output" ] @@ -316,7 +401,7 @@ { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -324,6 +409,24 @@ " print(key)\n", " AVG[str(key)].to_file(f\"{output_folder}/{waterschap2}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27", + "metadata": {}, + "outputs": [], + "source": [ + "AVG[\"peilgebied\"][\"peilgebied_cat\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_delfland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_delfland.ipynb index 8bd09ed..caad67e 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_delfland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_delfland.ipynb @@ -28,7 +28,13 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "from general_functions import read_gpkg_layers" + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "from general_functions import *\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -49,16 +55,16 @@ "# define relative paths\n", "waterschap = \"Delfland\"\n", "\n", - "data_path = f\"../projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_delfland.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_delfland.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}/\"" ] }, { @@ -86,8 +92,10 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", + "\n", "delfland[\"peilgebied\"] = delfland[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", "\n", "# Load waterschap boundaries\n", @@ -155,7 +163,7 @@ "overlaps[\"overlap_area\"] = overlaps.area\n", "\n", "# Step 4: Filter based on area Area Percentages\n", - "minimum_area = 20000\n", + "minimum_area = 200\n", "print(f\"Number of overlapping shapes without filter: {len(overlaps)}\")\n", "overlap_ids = overlaps.loc[overlaps[\"overlap_area\"] > minimum_area]\n", "overlap_ids = overlap_ids.globalid.to_list()\n", @@ -179,15 +187,19 @@ "source": [ "# Add occurence to geodataframe\n", "peilgebieden_cat = []\n", + "ids = []\n", "\n", "for index, row in delfland[\"peilgebied\"].iterrows():\n", - " if row.globalid in overlap_ids:\n", - " peilgebieden_cat.append(2)\n", - "\n", - " elif row.HWS_BZM is True:\n", + " if row.code.startswith(\"BZM\") or row.HWS_BZM:\n", + " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", "\n", - " elif row.HWS_BZM is False:\n", + " # Check if the row's globalid is in overlap_ids\n", + " elif row.globalid in overlap_ids:\n", + " peilgebieden_cat.append(2)\n", + "\n", + " # If none of the above conditions are met, append 0\n", + " else:\n", " peilgebieden_cat.append(0)\n", "\n", "# Add new column and drop old HWS_BZM column\n", @@ -253,10 +265,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Create buffer polygon\n", - "buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how=\"intersection\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how=\"difference\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, delfland[\"peilgebied\"], how=\"difference\", keep_geom_type=True)" + "# # Create buffer polygon\n", + "# buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how='intersection', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how='difference', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, delfland['peilgebied'], how='difference', keep_geom_type=True)" ] }, { @@ -274,15 +286,15 @@ "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"code\"] = \"dummy_code_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_buffer_1\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_buffer_1' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", "\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "delfland[\"peilgebied\"] = pd.concat([buffer_polygon, delfland[\"peilgebied\"]])" + "# delfland['peilgebied'] = pd.concat([buffer_polygon, delfland['peilgebied']])" ] }, { @@ -292,27 +304,29 @@ "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "delfland[\"streefpeil\"] = pd.concat([streefpeil_buffer, delfland[\"streefpeil\"]])\n", - "delfland[\"streefpeil\"] = gpd.GeoDataFrame(delfland[\"streefpeil\"])\n", + "# delfland['streefpeil'] = pd.concat([streefpeil_buffer, delfland['streefpeil']])\n", + "# delfland['streefpeil'] = gpd.GeoDataFrame(delfland['streefpeil'])\n", "\n", - "# Fix\n", - "delfland[\"streefpeil\"][\"waterhoogte\"] = delfland[\"streefpeil\"][\"waterhoogte\"].replace(\"N/A\", np.nan)\n", - "delfland[\"streefpeil\"][\"waterhoogte\"] = pd.to_numeric(delfland[\"streefpeil\"][\"waterhoogte\"])" + "# # Fix\n", + "# delfland['streefpeil']['waterhoogte'] = delfland['streefpeil']['waterhoogte'].replace('N/A', np.nan)\n", + "# delfland['streefpeil']['waterhoogte'] = pd.to_numeric(delfland['streefpeil']['waterhoogte'])" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "21", "metadata": {}, + "outputs": [], "source": [ - "## Write output" + "delfland[\"peilgebied\"].peilgebied_cat.unique()" ] }, { @@ -321,11 +335,48 @@ "id": "22", "metadata": {}, "outputs": [], + "source": [ + "delfland[\"peilgebied\"] = delfland[\"peilgebied\"][[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " delfland[\"peilgebied\"] = delfland[\"peilgebied\"].loc[delfland[\"peilgebied\"].peilgebied_cat != 2]" + ] + }, + { + "cell_type": "markdown", + "id": "24", + "metadata": {}, + "source": [ + "## Write output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25", + "metadata": {}, + "outputs": [], "source": [ "for key in delfland.keys():\n", " print(key)\n", " delfland[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_rijnland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_rijnland.ipynb index bbd7459..7801eab 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_rijnland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_rijnland.ipynb @@ -26,10 +26,16 @@ "metadata": {}, "outputs": [], "source": [ + "from itertools import combinations\n", + "\n", "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -47,19 +53,19 @@ "metadata": {}, "outputs": [], "source": [ - "# define relative paths\n", + "# HD['peilgebied'].globalid.is_unique#define relative paths\n", "waterschap = \"Rijnland\"\n", "\n", - "data_path = f\"../projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_rijnland.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_rijnland.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -87,6 +93,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -100,13 +107,26 @@ "gdf_hws = gpd.read_file(hws_path)\n", "\n", "# Load buffer\n", - "gdf_buffer = gpd.read_file(buffer_path)" + "gdf_buffer = gpd.read_file(buffer_path)\n", + "\n", + "# temp\n", + "Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"].drop(index=2, axis=1)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "7", "metadata": {}, + "outputs": [], + "source": [ + "Rijnland[\"peilgebied\"].globalid.is_unique" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, "source": [ "## Select waterschap boundaries and clip hws layer" ] @@ -114,7 +134,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -127,7 +147,7 @@ }, { "cell_type": "markdown", - "id": "9", + "id": "10", "metadata": {}, "source": [ "## Peilgebied and HWS layer overlap:\n", @@ -140,7 +160,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -156,7 +176,7 @@ "overlaps[\"overlap_area\"] = overlaps.area\n", "\n", "# Step 4: Filter based on area Area Percentages\n", - "minimum_area = 20000\n", + "minimum_area = 200\n", "print(f\"Number of overlapping shapes without filter: {len(overlaps)}\")\n", "overlap_ids = overlaps.loc[overlaps[\"overlap_area\"] > minimum_area]\n", "overlap_ids = overlap_ids.globalid.to_list()\n", @@ -165,7 +185,7 @@ }, { "cell_type": "markdown", - "id": "11", + "id": "12", "metadata": {}, "source": [ "## Create peilgebied_cat column" @@ -174,7 +194,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -182,17 +202,9 @@ "peilgebieden_cat = []\n", "\n", "for index, row in Rijnland[\"peilgebied\"].iterrows():\n", - " if row.code == \"dummy_code_peilgebied_18207\":\n", - " peilgebieden_cat.append(1)\n", - " print(\"yes\")\n", - " elif row.code == \"dummy_code_peilgebied_18322\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_18155\":\n", + " if \"RIJNLANDSBOEZEM\" in row.code:\n", + " print(\"true\")\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_18161\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_19451\":\n", - " peilgebieden_cat.append(2)\n", " else:\n", " peilgebieden_cat.append(0)\n", "\n", @@ -200,61 +212,10 @@ "Rijnland[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "13", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "14", "metadata": {}, - "source": [ - "## Add rhws to ['peilgebied','streefpeil']" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "15", - "metadata": {}, - "outputs": [], - "source": [ - "# update peilgebied dict key\n", - "gdf_rhws[\"globalid\"] = \"dummy_globalid_rhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"code\"] = \"dummy_code_nhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"nen3610id\"] = \"dummy_nen3610id_rhws_\" + gdf_rhws.index.astype(str)\n", - "gdf_rhws[\"peilgebied_cat\"] = 1\n", - "\n", - "gdf_rhws = gdf_rhws[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", - "\n", - "Rijnland[\"peilgebied\"] = pd.concat([gdf_rhws, AVG[\"peilgebied\"]])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "16", - "metadata": {}, - "outputs": [], - "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_hws = pd.DataFrame()\n", - "streefpeil_hws[\"waterhoogte\"] = [np.nan] * len(gdf_rhws)\n", - "streefpeil_hws[\"globalid\"] = \"dummy_globalid_rhws_\" + gdf_rhws.index.astype(str)\n", - "streefpeil_hws[\"geometry\"] = [None] * len(gdf_rhws)\n", - "\n", - "Rijnland[\"streefpeil\"] = pd.concat([streefpeil_hws, Rijnland[\"streefpeil\"]])\n", - "Rijnland[\"streefpeil\"] = gpd.GeoDataFrame(Rijnland[\"streefpeil\"])" - ] - }, - { - "cell_type": "markdown", - "id": "17", - "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" ] @@ -262,7 +223,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -280,7 +241,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -294,368 +255,267 @@ "Rijnland[\"streefpeil\"] = gpd.GeoDataFrame(Rijnland[\"streefpeil\"])" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "20", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "21", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "22", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "23", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "24", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "25", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "26", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "27", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "28", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "29", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "30", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", - "id": "31", + "id": "17", "metadata": {}, "source": [ - "### Select waterschap boundaries" + "### Create buffer polygon between NHWS and peilgebied/RHWS" ] }, { "cell_type": "code", "execution_count": null, - "id": "32", - "metadata": {}, - "outputs": [], - "source": [ - "# Select boundaries HH Amstel, Gooi en Vecht\n", - "gdf_grens = gdf_grens.loc[[\"HH van Rijnland\"]]" - ] - }, - { - "cell_type": "markdown", - "id": "33", + "id": "18", "metadata": {}, - "source": [ - "### Create inverse layer" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "34", - "metadata": { - "tags": [] - }, "outputs": [], "source": [ - "# Remove mixed geomtypes (lines)\n", - "data = []\n", + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", "\n", - "for index, row in Rijnland[\"peilgebied\"].iterrows():\n", - " # print(row.geometry.geom_type)\n", - " if row.geometry.geom_type != \"LineString\":\n", - " data.append(row)\n", + "# polygons = []\n", + "# for geom in gdf_hws.geometry:\n", + "# if isinstance(geom, MultiPolygon):\n", + "# for poly in geom:\n", + "# polygons.append(poly)\n", + "# elif isinstance(geom, Polygon):\n", + "# polygons.append(geom)\n", "\n", - "Rijnland[\"peilgebied\"] = gpd.GeoDataFrame(pd.concat(data, axis=1, ignore_index=True)).transpose()\n", - "Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"].set_geometry(\"geometry\")\n", - "Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"].set_crs(\"EPSG:28992\")" + "# buffer_polygon = buffer_polygon.difference(MultiPolygon(polygons))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(Rijnland['peilgebied'].geometry.tolist()))\n", + "# buffer_polygon_gdf = gpd.GeoDataFrame([{'geometry': geom} for geom in buffer_polygon], geometry='geometry', crs='EPSG:28992')\n", + "\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')\n" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "35", + "cell_type": "markdown", + "id": "19", "metadata": {}, - "outputs": [], "source": [ - "# Select inverse of peilgebied\n", - "gdf_boezem_out = gpd.overlay(gdf_grens, Rijnland[\"peilgebied\"].dissolve(), how=\"symmetric_difference\")" + "### Add buffer to ['peilgebied','streefpeil']" ] }, { "cell_type": "code", "execution_count": null, - "id": "36", + "id": "20", "metadata": {}, "outputs": [], "source": [ - "# Store unfiltered layer\n", - "gdf_boezem_out.to_file(f\"{output_folder}/boezem_unfiltered_{waterschap}.gpkg\")" + "# # update peilgebied dict key\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", + "\n", + "# Rijnland['peilgebied'] = pd.concat([buffer_polygon, Rijnland['peilgebied']])\n", + "# Rijnland['peilgebied'] = gpd.GeoDataFrame(Rijnland['peilgebied'])\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "37", + "id": "21", "metadata": {}, "outputs": [], "source": [ - "# Create separate polygons\n", - "gdf_boezem_out = gdf_boezem_out.explode()" + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# streefpeil_buffer['geometry'] = [None]\n", + "\n", + "\n", + "# Rijnland['streefpeil'] = pd.concat([streefpeil_buffer, Rijnland['streefpeil']])\n", + "# Rijnland['streefpeil'] = gpd.GeoDataFrame(Rijnland['streefpeil'])\n" ] }, { "cell_type": "markdown", - "id": "38", + "id": "22", "metadata": {}, "source": [ - "### Calculate area of polygons and filter" + "## Rijnland data contains many duplicate peilgebieden\n", + "### Calculate polygons that overlap with more than 90 % of their area" ] }, { "cell_type": "code", "execution_count": null, - "id": "39", + "id": "23", "metadata": {}, "outputs": [], - "source": [ - "# Calculate area of polygons\n", - "areas = []\n", - "\n", - "for index, row in gdf_boezem_out.iterrows():\n", - " areas.append(row.geometry.area)\n", - "\n", - "gdf_boezem_out[\"area\"] = areas" - ] + "source": [] }, { "cell_type": "code", "execution_count": null, - "id": "40", + "id": "24", "metadata": {}, "outputs": [], "source": [ - "# filter based on area of polygons\n", - "gdf_boezem_out.sort_values(by=\"area\").iloc[[-1]].to_file(f\"{output_folder}/boezem_filter_lvl_1_{waterschap}.gpkg\")\n", - "gdf_boezem_out.sort_values(by=\"area\").iloc[[-2]].to_file(f\"{output_folder}/boezem_filter_lvl_2_{waterschap}.gpkg\")" + "gdf = Rijnland[\"peilgebied\"][3:]\n", + "\n", + "# Initialize a list to store index pairs with more than 90% overlap\n", + "overlapping_pairs = []\n", + "\n", + "# Iterate through each unique pair of geometries\n", + "for idx1, idx2 in combinations(gdf.index, 2):\n", + " print(f\"Processing {idx1} out of {len(gdf)}...\", end=\"\\r\")\n", + " geom1 = gdf.at[idx1, \"geometry\"]\n", + " geom2 = gdf.at[idx2, \"geometry\"]\n", + "\n", + " # Calculate intersection\n", + " intersection = geom1.intersection(geom2)\n", + " intersection_area = intersection.area\n", + "\n", + " # Calculate original areas\n", + " area1 = geom1.area\n", + " area2 = geom2.area\n", + "\n", + " # Calculate intersection percentage for each geometry\n", + " intersection_percentage1 = (intersection_area / area1) * 100\n", + " intersection_percentage2 = (intersection_area / area2) * 100\n", + "\n", + " # Check if both geometries overlap more than 90%\n", + " if intersection_percentage1 > 90 and intersection_percentage2 > 90:\n", + " overlapping_pairs.append((idx1, idx2))\n", + "\n", + "idx1s = []\n", + "idx2s = []\n", + "\n", + "glob_1s = []\n", + "glob_2s = []\n", + "\n", + "\n", + "for idx1, idx2 in overlapping_pairs:\n", + " idx1s.append(idx1)\n", + " idx2s.append(idx2)\n", + "\n", + " glob_1s.append(gdf.iloc[idx1].globalid)\n", + " glob_2s.append(gdf.iloc[idx2].globalid)\n", + "\n", + "\n", + "df = pd.DataFrame()\n", + "df[\"idx1\"] = idx1s\n", + "df[\"idx2\"] = idx2s\n", + "df[\"globalid_1\"] = glob_1s\n", + "df[\"globalid_2\"] = glob_2s\n", + "\n", + "df.to_csv(\"../overlapping_Rijnland.csv\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "41", + "id": "25", "metadata": {}, "outputs": [], "source": [ - "# Store peilgebieden that do not connect properly\n", - "gdf_boezem_out.sort_values(by=\"area\").iloc[:-1].to_file(f\"{output_folder}/niet_goed_aansluitend_{waterschap}.gpkg\")" + "print(df)" ] }, { "cell_type": "markdown", - "id": "42", - "metadata": {}, - "source": [ - "### Add boezem when peilgebied is part of dm_netwerk" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "43", + "id": "26", "metadata": {}, - "outputs": [], "source": [ - "# Load Boezem network file (DM_netwerk)\n", - "gdf_dm_netwerk = gpd.read_file(dm_netwerk_path)" + "### Create list of duplicates for removal" ] }, { "cell_type": "code", "execution_count": null, - "id": "44", + "id": "27", "metadata": {}, "outputs": [], "source": [ - "# Select the peilgebieden that intersect with DM-netwerk\n", - "gdf = gpd.overlay(Rijnland[\"peilgebied\"], gdf_dm_netwerk, how=\"intersection\")" + "numbers_to_remove = []\n", + "\n", + "# Go loop unique index values\n", + "for number in df[\"idx1\"].unique():\n", + " if number in numbers_to_remove:\n", + " continue\n", + "\n", + " # Find all combinations\n", + " associated_idx2 = df[df[\"idx1\"] == number][\"idx2\"].tolist()\n", + " # Append combinations\n", + " numbers_to_remove.extend(associated_idx2)\n", + "\n", + "# Remove duplicates using set operation\n", + "numbers_to_remove = list(set(numbers_to_remove))" ] }, { "cell_type": "markdown", - "id": "45", + "id": "28", "metadata": {}, "source": [ - "### Add HWS_BZM flag to boezem polygons" + "### Remove duplicates" ] }, { "cell_type": "code", "execution_count": null, - "id": "46", + "id": "29", "metadata": {}, "outputs": [], "source": [ - "# Add occurence to geodataframe\n", - "boezems = []\n", - "\n", - "for index, row in Rijnland[\"peilgebied\"].iterrows():\n", - " if row.nen3610id in gdf.nen3610id.values:\n", - " boezems.append(True)\n", - " else:\n", - " boezems.append(False)\n", - "\n", - "Rijnland[\"peilgebied\"][\"HWS_BZM\"] = boezems" + "Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"][~Rijnland[\"peilgebied\"].index.isin(numbers_to_remove)]\n", + "Rijnland[\"streefpeil\"] = Rijnland[\"streefpeil\"][~Rijnland[\"streefpeil\"].index.isin(numbers_to_remove)]" ] }, { "cell_type": "code", "execution_count": null, - "id": "47", + "id": "30", "metadata": {}, "outputs": [], "source": [ - "for key in Rijnland.keys():\n", - " print(key)\n", - " Rijnland[str(key)].to_file(f\"{output_folder}/{waterschap}_bzm.gpkg\", layer=str(key), driver=\"GPKG\")" + "if remove_cat_2:\n", + " Rijnland[\"peilgebied\"] = Rijnland[\"peilgebied\"].loc[Rijnland[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "48", - "metadata": {}, - "source": [ - "### Merge boezem and peilgebied layers" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "49", - "metadata": {}, - "outputs": [], - "source": [ - "# Select globalids of boezem polygons\n", - "bzm_id = Rijnland[\"peilgebied\"].loc[Rijnland[\"peilgebied\"][\"HWS_BZM\"]].globalid\n", - "\n", - "# Match globalids with streefpeil layer globalids\n", - "bzm_waterhoogte = Rijnland[\"streefpeil\"].loc[Rijnland[\"streefpeil\"][\"globalid\"].isin(bzm_id)]\n", - "\n", - "print(len(bzm_id))\n", - "print(len(bzm_waterhoogte))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "50", + "id": "31", "metadata": {}, - "outputs": [], "source": [ - "# Create boezem layer\n", - "boezem = gdf_boezem_out.sort_values(by=\"area\").iloc[[-1]]\n", - "\n", - "boezem[\"code\"] = \"dummy_code_999999\"\n", - "boezem[\"globalid\"] = \"dummy_globalid_999999\"\n", - "boezem[\"nen3610id\"] = \"dummy_nen3610id_peilgebied_999999\"\n", - "boezem[\"HWS_BZM\"] = True\n", - "boezem = boezem[[\"code\", \"globalid\", \"nen3610id\", \"HWS_BZM\", \"geometry\"]]\n", - "\n", - "# Create boezem streefpeil layer\n", - "streefpeil_bzm = pd.DataFrame()\n", - "streefpeil_bzm[\"waterhoogte\"] = [None]\n", - "streefpeil_bzm[\"globalid\"] = [\"dummy_globalid_999999\"]\n", - "streefpeil_bzm[\"geometry\"] = [None]" + "### Store data" ] }, { "cell_type": "code", "execution_count": null, - "id": "51", + "id": "32", "metadata": {}, "outputs": [], "source": [ - "# Merge boezem layer with peilgebieden\n", - "Rijnland[\"peilgebied\"] = gpd.GeoDataFrame(pd.concat([boezem, Rijnland[\"peilgebied\"]], ignore_index=True))\n", - "Rijnland[\"streefpeil\"] = gpd.GeoDataFrame(pd.concat([streefpeil_bzm, Rijnland[\"streefpeil\"]], ignore_index=True))" + "for key in Rijnland.keys():\n", + " print(key)\n", + " Rijnland[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "52", + "id": "33", "metadata": {}, "outputs": [], - "source": [ - "for key in Rijnland.keys():\n", - " print(key)\n", - " Rijnland[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" - ] + "source": [] }, { "cell_type": "code", "execution_count": null, - "id": "53", + "id": "34", "metadata": {}, "outputs": [], "source": [] diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_wetterskip.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_wetterskip.ipynb index 490f844..a9a37f8 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_wetterskip.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_wetterskip.ipynb @@ -28,9 +28,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "import shapely\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -50,18 +53,18 @@ "source": [ "# define relative paths\n", "waterschap = \"Wetterskip\"\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_wetterskip.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_wetterskip.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"\n", + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}/\"\n", "# Dm netwerk\n", - "boezem_path = \"../projects/4750_30/Data_overig/DM_netwerk/waterschapsgrenzen_boezem_netwerk.shp\"" + "boezem_path = \"/DATAFOLDER/projects/4750_30/Data_overig/DM_netwerk/waterschapsgrenzen_boezem_netwerk.shp\"" ] }, { @@ -89,6 +92,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "Wetterskip[\"peilgebied\"] = Wetterskip[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -122,11 +126,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Select boundaries HH Amstel, Gooi en Vecht\n", - "gdf_grens = gdf_grens.loc[[\"Wetterskip Fryslân\"]]\n", + "# # Select boundaries HH Amstel, Gooi en Vecht\n", + "# gdf_grens = gdf_grens.loc[['Wetterskip Fryslân']]\n", "\n", - "# Use waterschap boudnaries to clip HWS layer\n", - "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")" + "# # Use waterschap boudnaries to clip HWS layer\n", + "# gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how='intersection')" ] }, { @@ -148,23 +152,23 @@ "metadata": {}, "outputs": [], "source": [ - "# Step 1: Identify the Overlapping Areas and clip\n", - "overlaps = gpd.overlay(Wetterskip[\"peilgebied\"], gdf_hws, how=\"intersection\", keep_geom_type=True)\n", - "\n", - "# # Step 2: Subtract Overlapping Areas from the original polygons in each DataFrame\n", - "non_overlapping_peilgebied = gpd.overlay(Wetterskip[\"peilgebied\"], overlaps, how=\"difference\", keep_geom_type=True)\n", - "overlaps = gpd.overlay(non_overlapping_peilgebied, gdf_hws, how=\"intersection\", keep_geom_type=False)\n", - "\n", - "# Step 3: Calculate Area Percentages\n", - "# Calculate the area of overlaps\n", - "overlaps[\"overlap_area\"] = overlaps.area\n", - "\n", - "# Step 4: Filter based on area Area Percentages\n", - "minimum_area = 500\n", - "print(f\"Number of overlapping shapes without filter: {len(overlaps)}\")\n", - "overlap_ids = overlaps.loc[overlaps[\"overlap_area\"] > minimum_area]\n", - "overlap_ids = overlap_ids.globalid.to_list()\n", - "print(f\"Number of overlapping shapes with filter: {len(overlap_ids)}\")" + "# # Step 1: Identify the Overlapping Areas and clip\n", + "# overlaps = gpd.overlay(Wetterskip['peilgebied'], gdf_hws, how='intersection', keep_geom_type=True)\n", + "\n", + "# # # Step 2: Subtract Overlapping Areas from the original polygons in each DataFrame\n", + "# non_overlapping_peilgebied = gpd.overlay(Wetterskip['peilgebied'], overlaps, how='difference', keep_geom_type=True)\n", + "# overlaps = gpd.overlay(non_overlapping_peilgebied, gdf_hws, how='intersection', keep_geom_type=False)\n", + "\n", + "# # Step 3: Calculate Area Percentages\n", + "# # Calculate the area of overlaps\n", + "# overlaps['overlap_area'] = overlaps.area\n", + "\n", + "# # Step 4: Filter based on area Area Percentages\n", + "# minimum_area = 500\n", + "# print(f'Number of overlapping shapes without filter: {len(overlaps)}')\n", + "# overlap_ids = overlaps.loc[overlaps['overlap_area'] > minimum_area]\n", + "# overlap_ids = overlap_ids.globalid.to_list()\n", + "# print(f'Number of overlapping shapes with filter: {len(overlap_ids)}')" ] }, { @@ -186,7 +190,7 @@ "peilgebieden_cat = []\n", "\n", "for index, row in Wetterskip[\"streefpeil\"].iterrows():\n", - " if row.waterhoogte == -0.52:\n", + " if round(row.waterhoogte, 2) == -0.52:\n", " peilgebieden_cat.append(1)\n", " else:\n", " peilgebieden_cat.append(0)\n", @@ -195,9 +199,17 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "13", "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" ] @@ -205,7 +217,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -223,7 +235,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -237,16 +249,6 @@ "Wetterskip[\"streefpeil\"] = gpd.GeoDataFrame(Wetterskip[\"streefpeil\"])" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "16", - "metadata": {}, - "outputs": [], - "source": [ - "Wetterskip.keys()" - ] - }, { "cell_type": "markdown", "id": "17", @@ -262,16 +264,16 @@ "metadata": {}, "outputs": [], "source": [ - "buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", - "buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", - "buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(Wetterskip[\"peilgebied\"].geometry.tolist()))\n", - "\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon = buffer_polygon.set_geometry(0)\n", - "buffer_polygon = buffer_polygon.dissolve()\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon.set_geometry(\"geometry\")\n", - "buffer_polygon = buffer_polygon.set_crs(\"EPSG:28992\")" + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", + "# buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(Wetterskip['peilgebied'].geometry.tolist()))\n", + "\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')" ] }, { @@ -289,17 +291,17 @@ "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + \"1\"\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", - "\n", - "Wetterskip[\"peilgebied\"] = pd.concat([buffer_polygon, Wetterskip[\"peilgebied\"]])\n", - "Wetterskip[\"peilgebied\"] = gpd.GeoDataFrame(Wetterskip[\"peilgebied\"])" + "# # update peilgebied dict key\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", + "\n", + "# Wetterskip['peilgebied'] = pd.concat([buffer_polygon, Wetterskip['peilgebied']])\n", + "# Wetterskip['peilgebied'] = gpd.GeoDataFrame(Wetterskip['peilgebied'])" ] }, { @@ -309,15 +311,15 @@ "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "Wetterskip[\"streefpeil\"] = pd.concat([streefpeil_buffer, Wetterskip[\"streefpeil\"]])\n", - "Wetterskip[\"streefpeil\"] = gpd.GeoDataFrame(Wetterskip[\"streefpeil\"])" + "# Wetterskip['streefpeil'] = pd.concat([streefpeil_buffer, Wetterskip['streefpeil']])\n", + "# Wetterskip['streefpeil'] = gpd.GeoDataFrame(Wetterskip['streefpeil'])" ] }, { @@ -341,71 +343,79 @@ "execution_count": null, "id": "24", "metadata": { - "jupyter": { - "outputs_hidden": true - }, "tags": [] }, "outputs": [], "source": [ - "from itertools import combinations\n", + "# from itertools import combinations\n", + "# gdf = Wetterskip['peilgebied']\n", + "# # gdf = test\n", + "# gdf = gdf[6:]\n", "\n", - "gdf = Wetterskip[\"peilgebied\"]\n", - "gdf = gdf[6:]\n", - "# Initialize a list to store index pairs with more than 90% overlap\n", - "overlapping_pairs = []\n", + "# # Initialize a list to store index pairs with more than 90% overlap\n", + "# overlapping_pairs = []\n", "\n", - "# Iterate through each unique pair of geometries\n", - "for idx1, idx2 in combinations(gdf.index, 2):\n", - " print(f\"Processing {idx1} out of {len(gdf)}...\", end=\"\\r\")\n", - " geom1 = gdf.at[idx1, \"geometry\"]\n", - " geom2 = gdf.at[idx2, \"geometry\"]\n", + "# # Iterate through each unique pair of geometries\n", + "# for idx1, idx2 in combinations(gdf.index, 2):\n", + "# print(f'Processing {idx1} out of {len(gdf)}...', end='\\r')\n", + "# geom1 = gdf.at[idx1, 'geometry']\n", + "# geom2 = gdf.at[idx2, 'geometry']\n", "\n", - " # Calculate intersection\n", - " intersection = geom1.intersection(geom2)\n", - " intersection_area = intersection.area\n", + "# # Calculate intersection\n", + "# intersection = geom1.intersection(geom2)\n", + "# intersection_area = intersection.area\n", "\n", - " # Calculate original areas\n", - " area1 = geom1.area\n", - " area2 = geom2.area\n", + "# # Calculate original areas\n", + "# area1 = geom1.area\n", + "# area2 = geom2.area\n", "\n", - " # Calculate intersection percentage for each geometry\n", - " intersection_percentage1 = (intersection_area / area1) * 100\n", - " intersection_percentage2 = (intersection_area / area2) * 100\n", + "# # Calculate intersection percentage for each geometry\n", + "# intersection_percentage1 = (intersection_area / area1) * 100\n", + "# intersection_percentage2 = (intersection_area / area2) * 100\n", "\n", - " # Check if both geometries overlap more than 90%\n", - " if intersection_percentage1 > 90 and intersection_percentage2 > 90:\n", - " print((idx1, idx2))\n", - " overlapping_pairs.append((idx1, idx2))\n", + "# # Check if both geometries overlap more than 90%\n", + "# if intersection_percentage1 > 90 and intersection_percentage2 > 90:\n", + "# # print((idx1, idx2))\n", + "# overlapping_pairs.append((idx1, idx2))\n", "\n", - "idx1s = []\n", - "idx2s = []\n", + "# idx1s = []\n", + "# idx2s = []\n", "\n", - "glob_1s = []\n", - "glob_2s = []\n", + "# glob_1s = []\n", + "# glob_2s = []\n", "\n", "\n", - "for idx1, idx2 in overlapping_pairs:\n", - " idx1s.append(idx1)\n", - " idx2s.append(idx2)\n", + "# for idx1, idx2 in overlapping_pairs:\n", "\n", - " glob_1s.append(gdf.iloc[idx1].globalid)\n", - " glob_2s.append(gdf.iloc[idx2].globalid)\n", + "# idx1s.append(idx1)\n", + "# idx2s.append(idx2)\n", "\n", + "# glob_1s.append(gdf.iloc[idx1].globalid)\n", + "# glob_2s.append(gdf.iloc[idx2].globalid)\n", "\n", - "df = pd.DataFrame()\n", - "df[\"idx1\"] = idx1s\n", - "df[\"idx2\"] = idx2s\n", - "df[\"globalid_1\"] = glob_1s\n", - "df[\"globalid_2\"] = glob_2s\n", + "# df = pd.DataFrame()\n", + "# df['idx1'] = idx1s\n", + "# df['idx2'] = idx2s\n", + "# df['globalid_1'] = glob_1s\n", + "# df['globalid_2'] = glob_2s\n", "\n", - "df.to_csv(\"overlapping_wetterskip.csv\")" + "# df.to_csv('./overlapping_wetterskip.csv')" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "25", "metadata": {}, + "outputs": [], + "source": [ + "# df = pd.read_csv('./overlapping_wetterskip.csv')" + ] + }, + { + "cell_type": "markdown", + "id": "26", + "metadata": {}, "source": [ "### Create list of duplicates for removal" ] @@ -413,29 +423,29 @@ { "cell_type": "code", "execution_count": null, - "id": "26", + "id": "27", "metadata": {}, "outputs": [], "source": [ - "numbers_to_remove = []\n", + "# numbers_to_remove = []\n", "\n", - "# Go loop unique index values\n", - "for number in df[\"idx1\"].unique():\n", - " if number in numbers_to_remove:\n", - " continue\n", + "# # Go loop unique index values\n", + "# for number in df['idx1'].unique():\n", + "# if number in numbers_to_remove:\n", + "# continue\n", "\n", - " # Find all combinations\n", - " associated_idx2 = df[df[\"idx1\"] == number][\"idx2\"].tolist()\n", - " # Append combinations\n", - " numbers_to_remove.extend(associated_idx2)\n", + "# # Find all combinations\n", + "# associated_idx2 = df[df['idx1'] == number]['idx2'].tolist()\n", + "# # Append combinations\n", + "# numbers_to_remove.extend(associated_idx2)\n", "\n", - "# Remove duplicates using set operation\n", - "numbers_to_remove = list(set(numbers_to_remove))" + "# # Remove duplicates using set operation\n", + "# numbers_to_remove = list(set(numbers_to_remove))" ] }, { "cell_type": "markdown", - "id": "27", + "id": "28", "metadata": {}, "source": [ "### Remove duplicates" @@ -444,46 +454,59 @@ { "cell_type": "code", "execution_count": null, - "id": "28", + "id": "29", "metadata": {}, "outputs": [], "source": [ - "Wetterskip[\"peilgebied\"] = Wetterskip[\"peilgebied\"][~Wetterskip[\"peilgebied\"].index.isin(numbers_to_remove)]\n", - "Wetterskip[\"streefpeil\"] = Wetterskip[\"streefpeil\"][~Wetterskip[\"streefpeil\"].index.isin(numbers_to_remove)]" + "# Wetterskip['peilgebied'] = Wetterskip['peilgebied'][~Wetterskip['peilgebied'].index.isin(numbers_to_remove)]\n", + "# Wetterskip['streefpeil'] = Wetterskip['streefpeil'][~Wetterskip['streefpeil'].index.isin(numbers_to_remove)]" ] }, { - "cell_type": "markdown", - "id": "29", + "cell_type": "code", + "execution_count": null, + "id": "30", "metadata": {}, + "outputs": [], "source": [ - "## Store data" + "Wetterskip[\"streefpeil\"][\"globalid\"].is_unique" ] }, { "cell_type": "code", "execution_count": null, - "id": "30", + "id": "31", "metadata": {}, "outputs": [], "source": [ - "for key in Wetterskip.keys():\n", - " print(key)\n", - " Wetterskip[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" + "if remove_cat_2:\n", + " Wetterskip[\"peilgebied\"] = Wetterskip[\"peilgebied\"].loc[Wetterskip[\"peilgebied\"].peilgebied_cat != 2]" + ] + }, + { + "cell_type": "markdown", + "id": "32", + "metadata": {}, + "source": [ + "## Store data" ] }, { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "33", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for key in Wetterskip.keys():\n", + " print(key)\n", + " Wetterskip[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" + ] }, { "cell_type": "code", "execution_count": null, - "id": "32", + "id": "34", "metadata": {}, "outputs": [], "source": [] diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_zuiderzeeland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_zuiderzeeland.ipynb index ae1a321..7625396 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_zuiderzeeland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-process_zuiderzeeland.ipynb @@ -30,9 +30,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "import shapely\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -53,17 +56,16 @@ "# define relative paths\n", "waterschap = \"Zuiderzeeland\"\n", "waterschap2 = \"Zuiderzeeland\"\n", - "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_zuiderzeeland.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_zuiderzeeland.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -91,6 +93,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "Zuiderzeeland[\"peilgebied\"] = Zuiderzeeland[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -108,9 +111,19 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "7", "metadata": {}, + "outputs": [], + "source": [ + "Zuiderzeeland[\"peilgebied\"].globalid.is_unique" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, "source": [ "## Select waterschap boundaries and clip hws layer" ] @@ -118,7 +131,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -129,9 +142,19 @@ "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "10", + "metadata": {}, + "outputs": [], + "source": [ + "Zuiderzeeland[\"peilgebied\"]" + ] + }, { "cell_type": "markdown", - "id": "9", + "id": "11", "metadata": {}, "source": [ "## Peilgebied and HWS layer overlap:\n", @@ -144,7 +167,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -170,7 +193,17 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "Zuiderzeeland[\"peilgebied\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14", "metadata": {}, "outputs": [], "source": [ @@ -178,17 +211,20 @@ "peilgebieden_cat = []\n", "\n", "for index, row in Zuiderzeeland[\"peilgebied\"].iterrows():\n", - " if row.code == \"dummy_code_peilgebied_18207\":\n", + " if \"LVA.01\" in row.code:\n", " peilgebieden_cat.append(1)\n", " print(\"yes\")\n", - " elif row.code == \"dummy_code_peilgebied_18322\":\n", + " elif \"3.01\" in row.code:\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_18155\":\n", + " print(\"yes\")\n", + " elif \"LAGE AFDELING\" in row.code:\n", + " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_18161\":\n", + "\n", + " elif \"HOGE AFDELING\" in row.code:\n", + " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_peilgebied_19451\":\n", - " peilgebieden_cat.append(2)\n", + "\n", " else:\n", " peilgebieden_cat.append(0)\n", "\n", @@ -200,7 +236,7 @@ }, { "cell_type": "markdown", - "id": "12", + "id": "15", "metadata": {}, "source": [ "## Add nhws to ['peilgebied']" @@ -209,7 +245,7 @@ { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -226,7 +262,7 @@ }, { "cell_type": "markdown", - "id": "14", + "id": "17", "metadata": {}, "source": [ "## Add HWS to ['streefpeil']" @@ -235,7 +271,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -251,7 +287,7 @@ }, { "cell_type": "markdown", - "id": "16", + "id": "19", "metadata": {}, "source": [ "### Create buffer layer that ensures spatial match between peilgebied and hws layers based on the buffer layer" @@ -260,25 +296,25 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "20", "metadata": {}, "outputs": [], "source": [ - "buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", - "buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", - "buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(Zuiderzeeland[\"peilgebied\"].geometry.tolist()))\n", + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", + "# buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(Zuiderzeeland['peilgebied'].geometry.tolist()))\n", "\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon = buffer_polygon.set_geometry(0)\n", - "buffer_polygon = buffer_polygon.dissolve()\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon.set_geometry(\"geometry\")\n", - "buffer_polygon = buffer_polygon.set_crs(\"EPSG:28992\")" + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')" ] }, { "cell_type": "markdown", - "id": "18", + "id": "21", "metadata": { "tags": [] }, @@ -289,42 +325,53 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "22", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", "\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "Zuiderzeeland[\"peilgebied\"] = pd.concat([buffer_polygon, Zuiderzeeland[\"peilgebied\"]])" + "# Zuiderzeeland['peilgebied'] = pd.concat([buffer_polygon, Zuiderzeeland['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "20", + "id": "23", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "Zuiderzeeland[\"streefpeil\"] = pd.concat([streefpeil_buffer, Zuiderzeeland[\"streefpeil\"]])\n", - "Zuiderzeeland[\"streefpeil\"] = gpd.GeoDataFrame(Zuiderzeeland[\"streefpeil\"])" + "# Zuiderzeeland['streefpeil'] = pd.concat([streefpeil_buffer, Zuiderzeeland['streefpeil']])\n", + "# Zuiderzeeland['streefpeil'] = gpd.GeoDataFrame(Zuiderzeeland['streefpeil'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " Zuiderzeeland[\"peilgebied\"] = Zuiderzeeland[\"peilgebied\"].loc[Zuiderzeeland[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "21", + "id": "25", "metadata": {}, "source": [ "## Store output" @@ -333,7 +380,7 @@ { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -343,7 +390,7 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "27", "metadata": {}, "outputs": [], "source": [ @@ -351,6 +398,22 @@ " print(key)\n", " Zuiderzeeland[str(key)].to_file(f\"{output_folder}/{waterschap2}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HD.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HD.ipynb index da0aa3f..edcae23 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HD.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HD.ipynb @@ -28,8 +28,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -51,16 +55,16 @@ "waterschap = \"Hollandse_Delta\"\n", "waterschap2 = \"HD\"\n", "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_hd.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_hd.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -88,6 +92,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "HD[\"peilgebied\"] = HD[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -107,9 +112,19 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "7", "metadata": {}, + "outputs": [], + "source": [ + "HD[\"peilgebied\"].globalid.is_unique" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, "source": [ "## Select waterschap boundaries and clip hws layer" ] @@ -117,7 +132,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "9", "metadata": { "tags": [] }, @@ -132,7 +147,7 @@ }, { "cell_type": "markdown", - "id": "9", + "id": "10", "metadata": {}, "source": [ "## Peilgebied and HWS layer overlap:\n", @@ -145,7 +160,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -170,7 +185,7 @@ }, { "cell_type": "markdown", - "id": "11", + "id": "12", "metadata": {}, "source": [ "## Create peilgebied_cat column" @@ -179,7 +194,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -187,26 +202,53 @@ "peilgebieden_cat = []\n", "\n", "for index, row in HD[\"peilgebied\"].iterrows():\n", - " if row.code == \"Zuiderdiepboezem en havenkanaal Dirksland\":\n", + " if row.code == \"Zuiderdiepboezem_164\":\n", + " peilgebieden_cat.append(1)\n", + " elif row.code == \"Zuiderdiepboezem_163\":\n", + " peilgebieden_cat.append(1)\n", + " elif row.code == \"Zoetwaterboezem_571\":\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"Havenkanaal van Goedereede\":\n", + " elif row.code == \"Kanaal door Voorne_570\":\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"Zoetwaterboezem\":\n", + " elif row.code == \"Binnenbedijkte Maas_290\":\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"Kanaal door Voorne\":\n", + " elif row.code == \"Boezemloozende door Strijensas_333\":\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"Binnenbedijkte Maas\":\n", + " # elif row.code == 'Zuiderdiepboezem':\n", + " # peilgebieden_cat.append(1)\n", + " elif row.code == \"Kreekkade_660\":\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"Boezemloozende door Strijensas\":\n", + " elif row.code == \"Zwijndrechtse Waard_703\":\n", " peilgebieden_cat.append(1)\n", " else:\n", - " peilgebieden_cat.append(0)" + " peilgebieden_cat.append(0)\n", + "\n", + " # if row.code == 'Zuiderdiepboezem en havenkanaal Dirksland':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Havenkanaal van Goedereede':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Zoetwaterboezem':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Kanaal door Voorne':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Binnenbedijkte Maas':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Boezemloozende door Strijensas':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Zuiderdiepboezem':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Kreekkade':\n", + " # peilgebieden_cat.append(1)\n", + " # elif row.code == 'Zwijndrechtse Waard':\n", + " # peilgebieden_cat.append(1)\n", + " # else:\n", + " # peilgebieden_cat.append(0)" ] }, { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "14", "metadata": {}, "outputs": [], "source": [ @@ -214,9 +256,19 @@ "HD[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "HD[\"peilgebied\"][\"peilgebied_cat\"].unique()" + ] + }, { "cell_type": "markdown", - "id": "14", + "id": "16", "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" @@ -225,7 +277,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -243,7 +295,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -259,7 +311,7 @@ }, { "cell_type": "markdown", - "id": "17", + "id": "19", "metadata": {}, "source": [ "### Create buffer layer that ensures spatial match between peilgebied and hws layers based on the buffer layer" @@ -268,19 +320,19 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "20", "metadata": {}, "outputs": [], "source": [ - "# Create buffer polygon\n", - "buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how=\"intersection\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how=\"difference\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, HD[\"peilgebied\"], how=\"difference\", keep_geom_type=True)" + "# # Create buffer polygon\n", + "# buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how='intersection', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how='difference', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, HD['peilgebied'], how='difference', keep_geom_type=True)" ] }, { "cell_type": "markdown", - "id": "19", + "id": "21", "metadata": {}, "source": [ "## Add buffer to ['peilgebied','streefpeil']" @@ -289,42 +341,54 @@ { "cell_type": "code", "execution_count": null, - "id": "20", + "id": "22", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", "\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "HD[\"peilgebied\"] = pd.concat([buffer_polygon, HD[\"peilgebied\"]])" + "# HD['peilgebied'] = pd.concat([buffer_polygon, HD['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "23", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "HD[\"streefpeil\"] = pd.concat([streefpeil_buffer, HD[\"streefpeil\"]])\n", - "HD[\"streefpeil\"] = gpd.GeoDataFrame(HD[\"streefpeil\"])" + "# HD['streefpeil'] = pd.concat([streefpeil_buffer, HD['streefpeil']])\n", + "# HD['streefpeil'] = gpd.GeoDataFrame(HD['streefpeil'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " HD[\"peilgebied\"] = HD[\"peilgebied\"].loc[HD[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "22", + "id": "25", "metadata": {}, "source": [ "## Store output" @@ -333,7 +397,7 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -341,6 +405,22 @@ " print(key)\n", " HD[str(key)].to_file(f\"{output_folder}/{waterschap2}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHNK.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHNK.ipynb index d21b4c7..8fdc1ba 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHNK.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHNK.ipynb @@ -28,8 +28,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -51,16 +55,16 @@ "waterschap = \"HHNK\"\n", "waterschap2 = \"Noorderkwartier\"\n", "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_hhnk.gpkg\"\n", + "buffer_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_hhnk.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -88,6 +92,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "HHNK[\"peilgebied\"] = HHNK[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -123,8 +128,7 @@ "gdf_grens = gdf_grens.loc[[\"HH Hollands Noorderkwartier\"]]\n", "\n", "# Use waterschap boudnaries to clip HWS layer\n", - "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")\n", - "gdf_hws.plot()" + "gdf_hws = gpd.overlay(gdf_grens, gdf_hws, how=\"intersection\")" ] }, { @@ -146,7 +150,43 @@ "metadata": {}, "outputs": [], "source": [ - "# Step 1: Identify the Overlapping Areas and clip\n", + "# Plot\n", + "fig, ax = plt.subplots()\n", + "\n", + "HHNK[\"peilgebied\"].plot(ax=ax)\n", + "gdf_grens.plot(ax=ax, color=\"red\", alpha=0.5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "HHNK[\"peilgebied\"].geometry.type.unique()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12", + "metadata": {}, + "outputs": [], + "source": [ + "HHNK[\"peilgebied\"].geometry = HHNK[\"peilgebied\"].buffer(0)\n", + "gdf_grens.geometry = gdf_grens.buffer(0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Identify the Overlapping Areas and clip.\n", + "HHNK[\"peilgebied\"] = gpd.overlay(HHNK[\"peilgebied\"], gdf_grens, how=\"intersection\", keep_geom_type=True)\n", "overlaps = gpd.overlay(HHNK[\"peilgebied\"], gdf_hws, how=\"intersection\", keep_geom_type=True)\n", "\n", "# # Step 2: Subtract Overlapping Areas from the original polygons in each DataFrame\n", @@ -167,7 +207,7 @@ }, { "cell_type": "markdown", - "id": "11", + "id": "14", "metadata": {}, "source": [ "## Create peilgebied_cat column" @@ -176,7 +216,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "15", "metadata": { "tags": [] }, @@ -185,24 +225,28 @@ "# Add occurence to geodataframe\n", "peilgebieden_cat = []\n", "\n", - "for index, row in HHNK[\"peilgebied\"].iterrows():\n", - " if row.globalid in overlap_ids:\n", - " peilgebieden_cat.append(2)\n", + "# code_list = [\"dummy_code_5188\",\"dummy_code_5161\",\"dummy_code_5210\",\"dummy_code_4352\",\"dummy_code_5164\",\"dummy_code_5200\",\"dummy_code_5167\",\"dummy_code_37\"]\n", + "\n", "\n", - " elif row.HWS_BZM is True:\n", + "for index, row in HHNK[\"peilgebied\"].iterrows():\n", + " if row.HWS_BZM:\n", + " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", "\n", - " elif row.HWS_BZM is False:\n", + " # elif row.HWS_BZM is True:\n", + " # peilgebieden_cat.append(1)\n", + "\n", + " else:\n", " peilgebieden_cat.append(0)\n", "\n", "# Add new column and drop old HWS_BZM column\n", "HHNK[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat\n", - "HHNK[\"peilgebied\"] = HHNK[\"peilgebied\"].drop(columns=[\"HWS_BZM\"])" + "# HHNK['peilgebied'] = HHNK['peilgebied'].drop(columns=['HWS_BZM'])" ] }, { "cell_type": "markdown", - "id": "13", + "id": "16", "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" @@ -211,7 +255,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -229,7 +273,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -245,7 +289,7 @@ }, { "cell_type": "markdown", - "id": "16", + "id": "19", "metadata": {}, "source": [ "### Create layer that ensures spatial match between peilgebied and hws layers based on the buffer layer" @@ -254,19 +298,30 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "20", "metadata": {}, "outputs": [], "source": [ - "# Create buffer polygon\n", - "buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how=\"intersection\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how=\"difference\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, HHNK[\"peilgebied\"], how=\"difference\", keep_geom_type=True)" + "# # Create buffer polygon\n", + "# buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how='intersection', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how='difference', keep_geom_type=True)\n", + "\n", + "\n", + "# # Apply a small buffer to both GeoDataFrames to \"clean\" the geometries\n", + "# buffer_polygon.geometry = buffer_polygon.buffer(0.001).buffer(-0.001)\n", + "# HHNK['peilgebied'].geometry = HHNK['peilgebied'].buffer(0.001).buffer(-0.001)\n", + "\n", + "# # Try the overlay operation again\n", + "# try:\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, HHNK['peilgebied'], how='difference', keep_geom_type=True)\n", + "# print(\"Overlay operation successful.\")\n", + "# except Exception as e:\n", + "# print(f\"Overlay operation failed: {e}\")" ] }, { "cell_type": "markdown", - "id": "18", + "id": "21", "metadata": {}, "source": [ "## Add buffer to ['peilgebied','streefpeil']" @@ -275,42 +330,64 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "22", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", "\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "HHNK[\"peilgebied\"] = pd.concat([buffer_polygon, HHNK[\"peilgebied\"]])" + "# HHNK['peilgebied'] = pd.concat([buffer_polygon, HHNK['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "20", + "id": "23", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "HHNK[\"streefpeil\"] = pd.concat([streefpeil_buffer, HHNK[\"streefpeil\"]])\n", - "HHNK[\"streefpeil\"] = gpd.GeoDataFrame(HHNK[\"streefpeil\"])" + "# HHNK['streefpeil'] = pd.concat([streefpeil_buffer, HHNK['streefpeil']])\n", + "# HHNK['streefpeil'] = gpd.GeoDataFrame(HHNK['streefpeil'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "HHNK[\"peilgebied\"] = HHNK[\"peilgebied\"][[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " HHNK[\"peilgebied\"] = HHNK[\"peilgebied\"].loc[HHNK[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "21", + "id": "26", "metadata": {}, "source": [ "## Write output" @@ -319,7 +396,7 @@ { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "27", "metadata": {}, "outputs": [], "source": [ @@ -331,7 +408,25 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "28", + "metadata": {}, + "outputs": [], + "source": [ + "HHNK[\"peilgebied\"][\"peilgebied_cat\"].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30", "metadata": {}, "outputs": [], "source": [] diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHSK.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHSK.ipynb index 2904edf..e48b119 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHSK.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_HHSK.ipynb @@ -27,10 +27,13 @@ "outputs": [], "source": [ "import geopandas as gpd\n", - "import numpy as np\n", - "import pandas as pd\n", "import shapely\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -50,17 +53,16 @@ "source": [ "# define relative paths\n", "waterschap = \"HHSK\"\n", - "\n", - "data_path = f\"../projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_HHSK.gpkg\"\n", + "buffer_path = r\"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_HHSK.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -88,6 +90,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "HHSK[\"peilgebied\"] = HHSK[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -107,11 +110,14 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "7", "metadata": {}, + "outputs": [], "source": [ - "## Select waterschap boundaries and clip hws layer" + "print(len(HHSK[\"duikersifonhevel\"].globalid.unique()))\n", + "print(len(HHSK[\"duikersifonhevel\"].globalid))" ] }, { @@ -120,6 +126,67 @@ "id": "8", "metadata": {}, "outputs": [], + "source": [ + "HHSK[\"peilgebied\"].globalid.is_unique" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "len(HHSK[\"hydroobject\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10", + "metadata": {}, + "outputs": [], + "source": [ + "# HHSK['hydroobject'] = HHSK['hydroobject'].explode(ignore_index=False, index_parts=True)\n", + "HHSK[\"hydroobject\"][\"geometry\"] = HHSK[\"hydroobject\"].make_valid()\n", + "HHSK[\"hydroobject\"][\"geometry\"] = HHSK[\"hydroobject\"].geometry.apply(shapely.force_2d)\n", + "HHSK[\"hydroobject\"] = HHSK[\"hydroobject\"][~HHSK[\"hydroobject\"].is_empty].copy()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "HHSK[\"hydroobject\"] = HHSK[\"hydroobject\"].drop_duplicates(subset=\"geometry\", keep=\"first\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12", + "metadata": {}, + "outputs": [], + "source": [ + "len(HHSK[\"hydroobject\"])" + ] + }, + { + "cell_type": "markdown", + "id": "13", + "metadata": {}, + "source": [ + "## Select waterschap boundaries and clip hws layer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14", + "metadata": {}, + "outputs": [], "source": [ "# Select boundaries HH Amstel, Gooi en Vecht\n", "gdf_grens = gdf_grens.loc[[\"Schieland en de Krimpenerwaard\"]]\n", @@ -130,7 +197,7 @@ }, { "cell_type": "markdown", - "id": "9", + "id": "15", "metadata": {}, "source": [ "## Check Peilgebied and HWS layer overlap:\n", @@ -143,7 +210,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -171,7 +238,7 @@ }, { "cell_type": "markdown", - "id": "11", + "id": "17", "metadata": {}, "source": [ "## Create peilgebied_cat column" @@ -180,34 +247,70 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "18", "metadata": {}, "outputs": [], "source": [ - "# Add to geodataframe\n", - "peilgebieden_cat = []\n", + "# list(HHSK['peilgebied'][HHSK['peilgebied'].code.str.contains('boezem')].code.unique())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19", + "metadata": {}, + "outputs": [], + "source": [ + "# # Add to geodataframe\n", + "# peilgebieden_cat = []\n", + "\n", "\n", - "for index, row in HHSK[\"peilgebied\"].iterrows():\n", - " if row.code == \"GPG-399\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"GPG-403\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"GPG-144_RV1\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"GPG-144_RV2\":\n", - " peilgebieden_cat.append(1)\n", - " elif row.code == \"GPG-144_RV3\":\n", - " peilgebieden_cat.append(1)\n", + "# # code_list = [\"dummy_id_78_dummy_id_78\",\"PPG-48_dummy_id_196_dummy_id_196\",\"PPG-49_dummy_id_85_dummy_id_85\",\"PPG-237_dummy_id_148_dummy_id_148\",\"PPG-1040_dummy_id_125_dummy_id_125\"]\n", + "# # code_list = [\"dummy_code_peilgebied_486\",\"dummy_code_peilgebied_450\",\"dummy_code_peilgebied_906\",\"dummy_code_peilgebied_1060\",\"dummy_code_peilgebied_552\",\"dummy_code_peilgebied_953\",\n", + "# # \"dummy_code_peilgebied_216\",\"dummy_code_peilgebied_544\",\"dummy_code_peilgebied_5\",\"dummy_code_peilgebied_480\",\"dummy_code_peilgebied_308\",\"dummy_code_peilgebied_677\",\n", + "# # \"dummy_code_peilgebied_1053\"]\n", "\n", - " else:\n", - " peilgebieden_cat.append(0)\n", + "# code_list = list(HHSK['peilgebied'][HHSK['peilgebied'].code.str.contains('boezem')].code.unique())\n", "\n", - "HHSK[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat" + "# for index, row in HHSK['peilgebied'].iterrows():\n", + "# # print(row.code)\n", + "# # if row.code in code_list:\n", + "# if 'boezem' in row.code:\n", + "# print('appending_boezem')\n", + "# peilgebieden_cat.append(1)\n", + "\n", + "# else:\n", + "# peilgebieden_cat.append(0)\n", + "\n", + "# HHSK['peilgebied']['peilgebied_cat'] = peilgebieden_cat" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [], + "source": [ + "HHSK[\"peilgebied\"][\"peilgebied_cat\"] = 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21", + "metadata": {}, + "outputs": [], + "source": [ + "HHSK[\"peilgebied\"].loc[HHSK[\"peilgebied\"].code.str.contains(\"GPG-399\"), \"peilgebied_cat\"] = 1\n", + "HHSK[\"peilgebied\"].loc[HHSK[\"peilgebied\"].code.str.contains(\"GPG-1005\"), \"peilgebied_cat\"] = 1\n", + "HHSK[\"peilgebied\"].loc[HHSK[\"peilgebied\"].code.str.contains(\"GPG-1360\"), \"peilgebied_cat\"] = 1\n", + "HHSK[\"peilgebied\"].loc[HHSK[\"peilgebied\"].code.str.contains(\"GPG-1012\"), \"peilgebied_cat\"] = 1" ] }, { "cell_type": "markdown", - "id": "13", + "id": "22", "metadata": { "tags": [] }, @@ -218,41 +321,51 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "23", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "gdf_hws[\"globalid\"] = \"dummy_globalid_nhws_\" + gdf_hws.index.astype(str)\n", - "gdf_hws[\"code\"] = \"dummy_code_nhws_\" + gdf_hws.index.astype(str)\n", - "gdf_hws[\"nen3610id\"] = \"dummy_nen3610id_nhws_\" + gdf_hws.index.astype(str)\n", - "gdf_hws[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# gdf_hws['globalid'] = 'dummy_globalid_nhws_' + gdf_hws.index.astype(str)\n", + "# gdf_hws['code'] = 'dummy_code_nhws_' + gdf_hws.index.astype(str)\n", + "# gdf_hws['nen3610id'] = 'dummy_nen3610id_nhws_' + gdf_hws.index.astype(str)\n", + "# gdf_hws['peilgebied_cat'] = 2\n", "\n", - "gdf_hws = gdf_hws[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# gdf_hws = gdf_hws[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "HHSK[\"peilgebied\"] = pd.concat([gdf_hws, HHSK[\"peilgebied\"]])" + "# HHSK['peilgebied'] = pd.concat([gdf_hws, HHSK['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "24", "metadata": {}, "outputs": [], "source": [ - "# update streefpeil dict key\n", - "streefpeil_hws = pd.DataFrame()\n", - "streefpeil_hws[\"waterhoogte\"] = [np.nan] * len(gdf_hws)\n", - "streefpeil_hws[\"globalid\"] = \"dummy_globalid_nhws_\" + gdf_hws.index.astype(str)\n", - "streefpeil_hws[\"geometry\"] = [None] * len(gdf_hws)\n", + "# # update streefpeil dict key\n", + "# streefpeil_hws = pd.DataFrame()\n", + "# streefpeil_hws['waterhoogte'] = [np.nan] * len(gdf_hws)\n", + "# streefpeil_hws['globalid'] = 'dummy_globalid_nhws_' + gdf_hws.index.astype(str)\n", + "# streefpeil_hws['geometry'] = [None]* len(gdf_hws)\n", "\n", - "HHSK[\"streefpeil\"] = pd.concat([streefpeil_hws, HHSK[\"streefpeil\"]])\n", - "HHSK[\"streefpeil\"] = gpd.GeoDataFrame(HHSK[\"streefpeil\"])" + "# HHSK['streefpeil'] = pd.concat([streefpeil_hws, HHSK['streefpeil']])\n", + "# HHSK['streefpeil'] = gpd.GeoDataFrame(HHSK['streefpeil'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25", + "metadata": {}, + "outputs": [], + "source": [ + "HHSK[\"peilgebied\"][\"peilgebied_cat\"].unique()" ] }, { "cell_type": "markdown", - "id": "16", + "id": "26", "metadata": {}, "source": [ "### Create buffer polygon between NHWS and peilgebied/RHWS" @@ -261,25 +374,25 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "27", "metadata": {}, "outputs": [], "source": [ - "buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", - "buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", - "buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(HHSK[\"peilgebied\"].geometry.tolist()))\n", + "# buffer_polygon = gdf_buffer.geometry.iat[0].intersection(gdf_grens.geometry.iat[0])\n", + "# buffer_polygon = buffer_polygon.difference(shapely.geometry.MultiPolygon(gdf_hws.geometry.tolist()))\n", + "# buffer_polygon = buffer_polygon.difference(shapely.ops.unary_union(HHSK['peilgebied'].geometry.tolist()))\n", "\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon = buffer_polygon.set_geometry(0)\n", - "buffer_polygon = buffer_polygon.dissolve()\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon.set_geometry(\"geometry\")\n", - "buffer_polygon = buffer_polygon.set_crs(\"EPSG:28992\")" + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon = buffer_polygon.set_geometry(0)\n", + "# buffer_polygon = buffer_polygon.dissolve()\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon.set_geometry('geometry')\n", + "# buffer_polygon = buffer_polygon.set_crs('EPSG:28992')" ] }, { "cell_type": "markdown", - "id": "18", + "id": "28", "metadata": {}, "source": [ "### Add buffer to ['peilgebied','streefpeil']" @@ -288,90 +401,55 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "29", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + \"1\"\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", - "buffer_polygon = buffer_polygon.rename(columns={0: \"geometry\"})\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# # update peilgebied dict key\n", + "# buffer_polygon = gpd.GeoDataFrame(buffer_polygon)\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + '1'\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", + "# buffer_polygon = buffer_polygon.rename(columns={0:'geometry'})\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "HHSK[\"peilgebied\"] = pd.concat([buffer_polygon, HHSK[\"peilgebied\"]])\n", - "HHSK[\"peilgebied\"] = gpd.GeoDataFrame(HHSK[\"peilgebied\"])" + "# HHSK['peilgebied'] = pd.concat([buffer_polygon, HHSK['peilgebied']])\n", + "# HHSK['peilgebied'] = gpd.GeoDataFrame(HHSK['peilgebied'])" ] }, { "cell_type": "code", "execution_count": null, - "id": "20", + "id": "30", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", "\n", - "HHSK[\"streefpeil\"] = pd.concat([streefpeil_buffer, HHSK[\"streefpeil\"]])\n", - "HHSK[\"streefpeil\"] = gpd.GeoDataFrame(HHSK[\"streefpeil\"])" - ] - }, - { - "cell_type": "markdown", - "id": "21", - "metadata": {}, - "source": [ - "### Fix duplicates hydroobjects" + "# HHSK['streefpeil'] = pd.concat([streefpeil_buffer, HHSK['streefpeil']])\n", + "# HHSK['streefpeil'] = gpd.GeoDataFrame(HHSK['streefpeil'])" ] }, { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "31", "metadata": {}, "outputs": [], "source": [ - "# Rename duplicates\n", - "# identify duplicates\n", - "HHSK[\"hydroobject\"][\"temp_globalid\"] = HHSK[\"hydroobject\"].groupby(\"globalid\").cumcount() + 1\n", - "HHSK[\"hydroobject\"][\"temp_code\"] = HHSK[\"hydroobject\"].groupby(\"code\").cumcount() + 1\n", - "HHSK[\"hydroobject\"][\"temp_nen3610id\"] = HHSK[\"hydroobject\"].groupby(\"nen3610id\").cumcount() + 1\n", - "\n", - "# AAdd _1 etc\n", - "HHSK[\"hydroobject\"][\"globalid_new\"] = HHSK[\"hydroobject\"].apply(\n", - " lambda x: f\"{x['globalid']}_{x['temp_globalid']}\" if x[\"temp_globalid\"] > 1 else x[\"globalid\"], axis=1\n", - ")\n", - "HHSK[\"hydroobject\"][\"code_new\"] = HHSK[\"hydroobject\"].apply(\n", - " lambda x: f\"{x['code']}_{x['temp_code']}\" if x[\"temp_code\"] > 1 else x[\"code\"], axis=1\n", - ")\n", - "HHSK[\"hydroobject\"][\"nen3610id_new\"] = HHSK[\"hydroobject\"].apply(\n", - " lambda x: f\"{x['nen3610id']}_{x['temp_nen3610id']}\" if x[\"temp_nen3610id\"] > 1 else x[\"nen3610id\"], axis=1\n", - ")\n", - "\n", - "# drop columns\n", - "HHSK[\"hydroobject\"] = HHSK[\"hydroobject\"].drop(\n", - " columns=[\"temp_globalid\", \"temp_code\", \"temp_nen3610id\", \"globalid\", \"nen3610id\", \"code\"]\n", - ")\n", - "# rename columns\n", - "HHSK[\"hydroobject\"] = HHSK[\"hydroobject\"].rename(\n", - " columns={\"globalid_new\": \"globalid\", \"code_new\": \"code\", \"nen3610id_new\": \"nen3610id\"}\n", - ")\n", - "# check\n", - "print(HHSK[\"hydroobject\"].globalid.is_unique)\n", - "print(HHSK[\"hydroobject\"].code.is_unique)\n", - "print(HHSK[\"hydroobject\"].nen3610id.is_unique)" + "if remove_cat_2:\n", + " HHSK[\"peilgebied\"] = HHSK[\"peilgebied\"].loc[HHSK[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "23", + "id": "32", "metadata": {}, "source": [ "### Store post-processed data" @@ -380,7 +458,7 @@ { "cell_type": "code", "execution_count": null, - "id": "24", + "id": "33", "metadata": {}, "outputs": [], "source": [ @@ -388,13 +466,29 @@ " print(key)\n", " HHSK[str(key)].to_file(f\"{output_folder}/{waterschap}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:stable]", + "display_name": "Python [conda env:ribasim]", "language": "python", - "name": "conda-env-stable-py" + "name": "conda-env-ribasim-py" }, "language_info": { "codemirror_mode": { @@ -406,7 +500,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.15" + "version": "3.10.13" } }, "nbformat": 4, diff --git a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_scheldestromen.ipynb b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_scheldestromen.ipynb index 5d7841f..226dc1f 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_scheldestromen.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/postprocess_data/post-processing_scheldestromen.ipynb @@ -28,8 +28,12 @@ "source": [ "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from general_functions import read_gpkg_layers" + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "remove_cat_2 = True" ] }, { @@ -51,16 +55,16 @@ "waterschap = \"Scheldestromen\"\n", "waterschap2 = \"Scheldestromen\"\n", "\n", - "data_path = f\"../projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", + "data_path = f\"/DATAFOLDER/projects/4750_20/Data_postprocessed/Waterschappen/{waterschap}/{waterschap2}.gpkg\"\n", "\n", "# Waterschaps boundaries\n", - "grens_path = \"../projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", + "grens_path = \"/DATAFOLDER/projects/4750_30/Data_overig/Waterschapsgrenzen/Waterschapsgrenzen.geojson\"\n", "# Hoofdwatersysteem boundaries\n", - "hws_path = \"../projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", + "hws_path = \"/DATAFOLDER/projects/4750_30/Data_overig/HWS/krw_basins_vlakken.gpkg\"\n", "# Buffer boundaries\n", - "buffer_path = \"../projects/4750_30/Data_overig/HWS/hws_buffer_Scheldestromen.gpkg\"\n", + "buffer_path = r\"/DATAFOLDER/projects/4750_30/Data_overig/HWS/hws_buffer_Scheldestromen.gpkg\"\n", "# Output folder\n", - "output_folder = f\"./Waterschappen/{waterschap}\"" + "output_folder = f\"/DATAFOLDER/projects/4750_30/Data_postprocessed/Waterschappen/{waterschap}\"" ] }, { @@ -88,6 +92,7 @@ " \"duikersifonhevel\",\n", " \"peilgebied\",\n", " \"streefpeil\",\n", + " \"aggregation_area\",\n", " ],\n", ")\n", "Scheldestromen[\"peilgebied\"] = Scheldestromen[\"peilgebied\"].to_crs(\"EPSG:28992\")\n", @@ -117,9 +122,19 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "8", "metadata": {}, + "outputs": [], + "source": [ + "Scheldestromen[\"peilgebied\"]" + ] + }, + { + "cell_type": "markdown", + "id": "9", + "metadata": {}, "source": [ "## Select waterschap boundaries and clip hws layer" ] @@ -127,7 +142,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9", + "id": "10", "metadata": {}, "outputs": [], "source": [ @@ -140,7 +155,7 @@ }, { "cell_type": "markdown", - "id": "10", + "id": "11", "metadata": {}, "source": [ "## Peilgebied and HWS layer overlap:\n", @@ -153,7 +168,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -178,7 +193,7 @@ }, { "cell_type": "markdown", - "id": "12", + "id": "13", "metadata": {}, "source": [ "## Create peilgebied_cat columnm" @@ -187,7 +202,7 @@ { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "14", "metadata": {}, "outputs": [], "source": [ @@ -196,13 +211,14 @@ "\n", "for index, row in Scheldestromen[\"peilgebied\"].iterrows():\n", " if row.nen3610id == \"dummy_nen3610id_peilgebied_549\":\n", + " print(True)\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"GPG437\":\n", + " elif \"GPG437\" in row.code:\n", " print(\"yes\")\n", " peilgebieden_cat.append(1)\n", - " elif row.code == \"dummy_code_nhws_3\":\n", + " elif \"dummy_code_nhws_3\" in row.code:\n", " peilgebieden_cat.append(1)\n", - " print(\"yes\")\n", + " print(\"yes2\")\n", " else:\n", " peilgebieden_cat.append(0)" ] @@ -210,7 +226,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -218,9 +234,19 @@ "Scheldestromen[\"peilgebied\"][\"peilgebied_cat\"] = peilgebieden_cat" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "16", + "metadata": {}, + "outputs": [], + "source": [ + "Scheldestromen[\"peilgebied\"][\"peilgebied_cat\"].unique()" + ] + }, { "cell_type": "markdown", - "id": "15", + "id": "17", "metadata": {}, "source": [ "## Add nhws to ['peilgebied','streefpeil']" @@ -229,7 +255,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -247,7 +273,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "19", "metadata": {}, "outputs": [], "source": [ @@ -263,7 +289,7 @@ }, { "cell_type": "markdown", - "id": "18", + "id": "20", "metadata": {}, "source": [ "### Create buffer layer that ensures spatial match between peilgebied and hws layers based on the buffer layer" @@ -272,19 +298,19 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "21", "metadata": {}, "outputs": [], "source": [ - "# Create buffer polygon\n", - "buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how=\"intersection\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how=\"difference\", keep_geom_type=True)\n", - "buffer_polygon = gpd.overlay(buffer_polygon, Scheldestromen[\"peilgebied\"], how=\"difference\", keep_geom_type=True)" + "# # Create buffer polygon\n", + "# buffer_polygon = gpd.overlay(gdf_buffer, gdf_grens, how='intersection', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, gdf_hws, how='difference', keep_geom_type=True)\n", + "# buffer_polygon = gpd.overlay(buffer_polygon, Scheldestromen['peilgebied'], how='difference', keep_geom_type=True)" ] }, { "cell_type": "markdown", - "id": "20", + "id": "22", "metadata": { "tags": [] }, @@ -295,41 +321,52 @@ { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "23", "metadata": {}, "outputs": [], "source": [ - "# update peilgebied dict key\n", - "buffer_polygon[\"globalid\"] = \"dummy_globalid_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"code\"] = \"dummy_code_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"nen3610id\"] = \"dummy_nen3610id_nhws_buffer_\" + buffer_polygon.index.astype(str)\n", - "buffer_polygon[\"peilgebied_cat\"] = 2\n", + "# # update peilgebied dict key\n", + "# buffer_polygon['globalid'] = 'dummy_globalid_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['code'] = 'dummy_code_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['nen3610id'] = 'dummy_nen3610id_nhws_buffer_' + buffer_polygon.index.astype(str)\n", + "# buffer_polygon['peilgebied_cat'] = 2\n", "\n", - "buffer_polygon = buffer_polygon[[\"globalid\", \"code\", \"nen3610id\", \"peilgebied_cat\", \"geometry\"]]\n", + "# buffer_polygon = buffer_polygon[['globalid', 'code', 'nen3610id', 'peilgebied_cat', 'geometry']]\n", "\n", - "Scheldestromen[\"peilgebied\"] = pd.concat([buffer_polygon, Scheldestromen[\"peilgebied\"]])" + "# Scheldestromen['peilgebied'] = pd.concat([buffer_polygon, Scheldestromen['peilgebied']])" ] }, { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "24", "metadata": {}, "outputs": [], "source": [ - "# Create boezem streefpeil layer\n", - "streefpeil_buffer = pd.DataFrame()\n", - "streefpeil_buffer[\"waterhoogte\"] = [np.nan]\n", - "streefpeil_buffer[\"globalid\"] = [\"dummy_globalid_nhws_buffer_1\"]\n", - "streefpeil_buffer[\"geometry\"] = [None]\n", + "# # Create boezem streefpeil layer\n", + "# streefpeil_buffer = pd.DataFrame()\n", + "# streefpeil_buffer['waterhoogte'] = [np.nan]\n", + "# streefpeil_buffer['globalid'] = ['dummy_globalid_nhws_buffer_1']\n", + "# streefpeil_buffer['geometry'] = [None]\n", "\n", - "Scheldestromen[\"streefpeil\"] = pd.concat([streefpeil_buffer, Scheldestromen[\"streefpeil\"]])\n", - "Scheldestromen[\"streefpeil\"] = gpd.GeoDataFrame(Scheldestromen[\"streefpeil\"])" + "# Scheldestromen['streefpeil'] = pd.concat([streefpeil_buffer, Scheldestromen['streefpeil']])\n", + "# Scheldestromen['streefpeil'] = gpd.GeoDataFrame(Scheldestromen['streefpeil'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25", + "metadata": {}, + "outputs": [], + "source": [ + "if remove_cat_2:\n", + " Scheldestromen[\"peilgebied\"] = Scheldestromen[\"peilgebied\"].loc[Scheldestromen[\"peilgebied\"].peilgebied_cat != 2]" ] }, { "cell_type": "markdown", - "id": "23", + "id": "26", "metadata": {}, "source": [ "## Store output" @@ -338,7 +375,7 @@ { "cell_type": "code", "execution_count": null, - "id": "24", + "id": "27", "metadata": { "tags": [] }, @@ -348,6 +385,54 @@ " print(key)\n", " Scheldestromen[str(key)].to_file(f\"{output_folder}/{waterschap2}.gpkg\", layer=str(key), driver=\"GPKG\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "32", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/AmstelGooienVecht.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/AmstelGooienVecht.ipynb index 8e478a8..ef0bc05 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/AmstelGooienVecht.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/AmstelGooienVecht.ipynb @@ -7,13 +7,12 @@ "metadata": {}, "outputs": [], "source": [ + "# import packages and functions\n", "import os\n", "\n", "import geopandas as gpd\n", - "import matplotlib.pyplot as plt\n", "import numpy as np\n", - "import pandas as pd\n", - "from shapely import wkt" + "import pandas as pd" ] }, { @@ -23,8 +22,10 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import show_layers_and_columns, store_data\n", + "from general_functions import *\n", "\n", + "%load_ext autoreload\n", + "%autoreload 2\n", "pd.set_option(\"display.max_columns\", None)" ] }, @@ -57,28 +58,22 @@ "metadata": {}, "outputs": [], "source": [ - "# AVG has delivered all data in CSV format. Load it in manually with some data mutations\n", - "AVG = {}\n", - "variables = [\n", - " \"stuw\",\n", - " \"gemaal\",\n", - " \"afsluitmiddel\",\n", - " \"duikersifonhevel\",\n", - " \"hydroobject\",\n", - "] # , 'peilgebiedpraktijk', 'peilafwijkinggebied']\n", - "for variable in variables:\n", - " path_variable = os.path.join(path_AVG, variable + \".csv\")\n", - " df_var = pd.read_csv(path_variable, delimiter=\";\")\n", - " geom_col = df_var.keys()[-1] # retrieve the column name\n", + "# #AVG has delivered all data in CSV format. Load it in manually with some data mutations\n", + "# AVG = {}\n", + "# variables = ['stuw', 'gemaal', 'afsluitmiddel', 'duikersifonhevel', 'hydroobject']#, 'peilgebiedpraktijk', 'peilafwijkinggebied']\n", + "# for variable in variables:\n", + "# path_variable = os.path.join(path_AVG, variable + '.csv')\n", + "# df_var = pd.read_csv(path_variable, delimiter=';')\n", + "# geom_col = df_var.keys()[-1] #retrieve the column name\n", "\n", - " if \"geometrie\" not in geom_col:\n", - " raise ValueError('No \"geometry\" string found in the last column of the dataframe. Check for existence')\n", + "# if not 'geometrie' in geom_col:\n", + "# raise ValueError('No \"geometry\" string found in the last column of the dataframe. Check for existence')\n", "\n", - " df_var[\"geometry\"] = df_var[geom_col].apply(lambda x: wkt.loads(x.split(\";\")[-1]))\n", - " AVG[variable] = df_var\n", + "# df_var['geometry'] = df_var[geom_col].apply(lambda x: wkt.loads(x.split(';')[-1]))\n", + "# AVG[variable] = df_var\n", "\n", - "# there is one last gpkg which contains the streefpeilen (and peilgebieden)\n", - "AVG[\"peilgebied\"] = gpd.read_file(os.path.join(path_AVG, \"vigerende_peilgebieden.gpkg\"))" + "# #there is one last gpkg which contains the streefpeilen (and peilgebieden)\n", + "# AVG['peilgebied'] = gpd.read_file(os.path.join(path_AVG, 'vigerende_peilgebieden.gpkg'))" ] }, { @@ -88,27 +83,34 @@ "metadata": {}, "outputs": [], "source": [ - "AVG[\"peilgebied\"][\"streefpeil\"] = np.nan\n", - "AVG[\"peilgebied\"][\"streefpeil\"] = AVG[\"peilgebied\"][\"streefpeil\"].fillna(value=AVG[\"peilgebied\"][\"GPGZMRPL\"])\n", - "AVG[\"peilgebied\"][\"streefpeil\"] = AVG[\"peilgebied\"][\"streefpeil\"].fillna(value=AVG[\"peilgebied\"][\"IWS_GPGVASTP\"])\n", - "AVG[\"peilgebied\"][\"streefpeil\"] = AVG[\"peilgebied\"][\"streefpeil\"].fillna(value=AVG[\"peilgebied\"][\"IWS_GPGONDP\"])\n", + "# AVG['peilgebied']['streefpeil'] = np.nan\n", + "# AVG['peilgebied']['streefpeil'] = AVG['peilgebied']['streefpeil'].fillna(value=AVG['peilgebied']['GPGZMRPL'])\n", + "# AVG['peilgebied']['streefpeil'] = AVG['peilgebied']['streefpeil'].fillna(value=AVG['peilgebied']['IWS_GPGVASTP'])\n", + "# AVG['peilgebied']['streefpeil'] = AVG['peilgebied']['streefpeil'].fillna(value=AVG['peilgebied']['IWS_GPGONDP'])\n", "\n", "\n", - "print(\n", - " \"Number of missing streefpeilen = \",\n", - " len(AVG[\"peilgebied\"][\"streefpeil\"].loc[AVG[\"peilgebied\"][\"streefpeil\"].isna()]),\n", - ")\n", + "# print('Number of missing streefpeilen = ', len(AVG['peilgebied']['streefpeil'].loc[AVG['peilgebied']['streefpeil'].isna()]))\n", "\n", - "fig, ax = plt.subplots()\n", - "AVG[\"peilgebied\"].geometry.plot(ax=ax, color=\"cornflowerblue\")\n", - "AVG[\"peilgebied\"].loc[AVG[\"peilgebied\"][\"streefpeil\"].isna()].geometry.plot(ax=ax, color=\"red\")\n", - "ax.legend()" + "# fig, ax = plt.subplots()\n", + "# AVG['peilgebied'].geometry.plot(ax=ax, color='cornflowerblue')\n", + "# AVG['peilgebied'].loc[AVG['peilgebied']['streefpeil'].isna()].geometry.plot(ax=ax, color='red')\n", + "# ax.legend()" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "id": "6", "metadata": {}, + "outputs": [], + "source": [ + "AVG = {}" + ] + }, + { + "cell_type": "markdown", + "id": "7", + "metadata": {}, "source": [ "# Nalevering" ] @@ -116,7 +118,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7", + "id": "8", "metadata": {}, "outputs": [], "source": [ @@ -146,7 +148,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -158,7 +160,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9", + "id": "10", "metadata": {}, "outputs": [], "source": [ @@ -168,7 +170,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -190,7 +192,9 @@ "AVG[\"gemaal\"].loc[AVG[\"gemaal\"].functiegemaal.str.contains(\"anvoergemaal|pmaling|an-|p-|pvoer\"), \"func_aanvoer\"] = True\n", "AVG[\"gemaal\"].loc[AVG[\"gemaal\"].functiegemaal.str.contains(\"irculatie\"), \"func_circulatie\"] = True\n", "AVG[\"gemaal\"].loc[\n", - " ~AVG[\"gemaal\"].func_afvoer & ~AVG[\"gemaal\"].func_aanvoer & ~AVG[\"gemaal\"].func_circulatie,\n", + " (AVG[\"gemaal\"].func_afvoer is False)\n", + " & (AVG[\"gemaal\"].func_aanvoer is False)\n", + " & (AVG[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] @@ -198,7 +202,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -256,7 +260,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -269,14 +273,14 @@ { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "14", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "14", + "id": "15", "metadata": {}, "source": [ "# Control, store" @@ -285,7 +289,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -295,7 +299,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -310,7 +314,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "18", "metadata": {}, "outputs": [], "source": [] diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Delfland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Delfland.ipynb index 6de7b81..19ff4db 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Delfland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Delfland.ipynb @@ -7,6 +7,7 @@ "metadata": {}, "outputs": [], "source": [ + "# import packages and functions\n", "import os\n", "\n", "import geopandas as gpd\n", @@ -20,7 +21,7 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { @@ -107,7 +108,9 @@ " Delfland[\"gemaal\"].FUNCTIEGEMAAL_resolved.str.contains(\"Overig|circulatie\"), \"func_circulatie\"\n", "] = True\n", "Delfland[\"gemaal\"].loc[\n", - " ~Delfland[\"gemaal\"].func_afvoer & ~Delfland[\"gemaal\"].func_aanvoer & ~Delfland[\"gemaal\"].func_circulatie,\n", + " (Delfland[\"gemaal\"].func_afvoer is False)\n", + " & (Delfland[\"gemaal\"].func_aanvoer is False)\n", + " & (Delfland[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown\n", "\n", diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHNK.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHNK.ipynb index 5a77de3..c3cf825 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHNK.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHNK.ipynb @@ -8,10 +8,13 @@ "outputs": [], "source": [ "# import packages and functions\n", - "\n", "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd" + "import pandas as pd\n", + "from general_functions import *\n", + "\n", + "%load_ext autoreload\n", + "%autoreload 2" ] }, { @@ -20,9 +23,7 @@ "id": "1", "metadata": {}, "outputs": [], - "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" - ] + "source": [] }, { "cell_type": "markdown", @@ -130,7 +131,9 @@ "HHNK[\"gemaal\"].loc[HHNK[\"gemaal\"][\"functiegemaal\"].isin(circulatie_values), \"func_circulatie\"] = True\n", "\n", "HHNK[\"gemaal\"].loc[\n", - " ~HHNK[\"gemaal\"].func_afvoer & ~HHNK[\"gemaal\"].func_aanvoer & ~HHNK[\"gemaal\"].func_circulatie,\n", + " (HHNK[\"gemaal\"].func_afvoer is False)\n", + " & (HHNK[\"gemaal\"].func_aanvoer is False)\n", + " & (HHNK[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHSK.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHSK.ipynb index 4ec315b..a336f57 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHSK.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/HHSK.ipynb @@ -26,7 +26,7 @@ }, "outputs": [], "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { @@ -187,7 +187,9 @@ "HHSK[\"gemaal\"].loc[HHSK[\"gemaal\"].functiegemaal.str.contains(\"1|3|5|\"), \"func_aanvoer\"] = True\n", "HHSK[\"gemaal\"].loc[HHSK[\"gemaal\"].functiegemaal.str.contains(\"8\"), \"func_circulatie\"] = True\n", "HHSK[\"gemaal\"].loc[\n", - " ~HHSK[\"gemaal\"].func_afvoer & ~HHSK[\"gemaal\"].func_aanvoer & ~HHSK[\"gemaal\"].func_circulatie,\n", + " (HHSK[\"gemaal\"].func_afvoer is False)\n", + " & (HHSK[\"gemaal\"].func_aanvoer is False)\n", + " & (HHSK[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] @@ -411,9 +413,7 @@ "metadata": {}, "outputs": [], "source": [ - "# peilgebied = burn_in_peilgebieden(base_layer = streefpeilen_PG_v,\n", - "# overlay_layer = streefpeilen_PG_a,\n", - "# plot = True)" + "peilgebied = burn_in_peilgebieden(base_layer=streefpeilen_PG_v, overlay_layer=streefpeilen_PG_a, plot=True)" ] }, { @@ -538,9 +538,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Routing", + "display_name": "Python [conda env:ribasim]", "language": "python", - "name": "routing" + "name": "conda-env-ribasim-py" }, "language_info": { "codemirror_mode": { @@ -552,7 +552,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Hollandse_Delta.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Hollandse_Delta.ipynb index 213ab49..d759394 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Hollandse_Delta.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Hollandse_Delta.ipynb @@ -22,7 +22,7 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { @@ -136,7 +136,9 @@ "HD[\"gemaal\"].loc[HD[\"gemaal\"].functiegemaal.str.contains(\"anvoergemaal|pmaling\"), \"func_aanvoer\"] = True\n", "HD[\"gemaal\"].loc[HD[\"gemaal\"].functiegemaal.str.contains(\"Doorspoel\"), \"func_circulatie\"] = True\n", "HD[\"gemaal\"].loc[\n", - " ~HD[\"gemaal\"].func_afvoer & ~HD[\"gemaal\"].func_aanvoer & ~HD[\"gemaal\"].func_circulatie,\n", + " (HD[\"gemaal\"].func_afvoer is False)\n", + " & (HD[\"gemaal\"].func_aanvoer is False)\n", + " & (HD[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rijnland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rijnland.ipynb index 164d8ff..67cc146 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rijnland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rijnland.ipynb @@ -23,7 +23,7 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import burn_in_peilgebieden, read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rivierenland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rivierenland.ipynb index 8de4cf1..1aedd84 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rivierenland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Rivierenland.ipynb @@ -23,7 +23,7 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { @@ -186,7 +186,9 @@ "WSRL[\"gemaal\"].loc[WSRL[\"gemaal\"].functiegemaal.str.contains(\"Aanvoer|Opmaling\"), \"func_aanvoer\"] = True\n", "WSRL[\"gemaal\"].loc[WSRL[\"gemaal\"].functiegemaal.str.contains(\"Doorspoelgemaal\"), \"func_circulatie\"] = True\n", "WSRL[\"gemaal\"].loc[\n", - " ~WSRL[\"gemaal\"].func_afvoer & ~WSRL[\"gemaal\"].func_aanvoer & ~WSRL[\"gemaal\"].func_circulatie,\n", + " (WSRL[\"gemaal\"].func_afvoer is False)\n", + " & (WSRL[\"gemaal\"].func_aanvoer is False)\n", + " & (WSRL[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Scheldestromen.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Scheldestromen.ipynb index 63c2ab8..de0aa81 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Scheldestromen.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Scheldestromen.ipynb @@ -12,8 +12,7 @@ "\n", "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd\n", - "from shapely.geometry import Polygon" + "import pandas as pd" ] }, { @@ -23,7 +22,7 @@ "metadata": {}, "outputs": [], "source": [ - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data" + "from general_functions import *" ] }, { @@ -259,7 +258,7 @@ "outputs": [], "source": [ "for i in range(len(Scheldestromen[\"peilgebied\"])):\n", - " if isinstance(Scheldestromen[\"peilgebied\"].loc[i, \"geometry\"], Polygon):\n", + " if Scheldestromen[\"peilgebied\"][\"geometry\"].at[i].geom_type == \"Polygon\":\n", " Scheldestromen[\"peilgebied\"].loc[i, \"geometry\"].plot()" ] }, diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Wetterskip.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Wetterskip.ipynb index 2fbe295..f5278bb 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Wetterskip.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Wetterskip.ipynb @@ -8,10 +8,9 @@ "outputs": [], "source": [ "# import packages and functions\n", - "\n", "import geopandas as gpd\n", "import pandas as pd\n", - "from general_functions import read_gpkg_layers, show_layers_and_columns, store_data\n", + "from general_functions import *\n", "\n", "pd.set_option(\"display.max_columns\", None)" ] @@ -160,7 +159,9 @@ "Wetterskip[\"gemaal\"].loc[Wetterskip[\"gemaal\"].functiegemaal.str.contains(\"Opmaling|Aanvoer\"), \"func_aanvoer\"] = True\n", "Wetterskip[\"gemaal\"].loc[Wetterskip[\"gemaal\"].functiegemaal.str.contains(\"Overig|circulatie\"), \"func_circulatie\"] = True\n", "Wetterskip[\"gemaal\"].loc[\n", - " ~Wetterskip[\"gemaal\"].func_afvoer & ~Wetterskip[\"gemaal\"].func_aanvoer & ~Wetterskip[\"gemaal\"].func_circulatie,\n", + " (Wetterskip[\"gemaal\"].func_afvoer is False)\n", + " & (Wetterskip[\"gemaal\"].func_aanvoer is False)\n", + " & (Wetterskip[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] diff --git a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Zuiderzeeland.ipynb b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Zuiderzeeland.ipynb index 92722a7..dbb81e7 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Zuiderzeeland.ipynb +++ b/src/peilbeheerst_model/peilbeheerst_model/preprocess_data/Zuiderzeeland.ipynb @@ -13,7 +13,8 @@ "import fiona\n", "import geopandas as gpd\n", "import numpy as np\n", - "import pandas as pd" + "import pandas as pd\n", + "from general_functions import *" ] }, { @@ -22,16 +23,6 @@ "id": "1", "metadata": {}, "outputs": [], - "source": [ - "from general_functions import show_layers_and_columns, store_data" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2", - "metadata": {}, - "outputs": [], "source": [ "pd.set_option(\"display.max_columns\", None)" ] @@ -39,7 +30,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3", + "id": "2", "metadata": {}, "outputs": [], "source": [ @@ -51,7 +42,7 @@ }, { "cell_type": "markdown", - "id": "4", + "id": "3", "metadata": {}, "source": [ "# Zuiderzeeland" @@ -60,7 +51,7 @@ { "cell_type": "code", "execution_count": null, - "id": "5", + "id": "4", "metadata": { "tags": [] }, @@ -87,7 +78,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6", + "id": "5", "metadata": {}, "outputs": [], "source": [ @@ -97,7 +88,7 @@ }, { "cell_type": "raw", - "id": "7", + "id": "6", "metadata": {}, "source": [ "ZZL: stuwen in KWKSOORT in overigekunstwerken.gpkg" @@ -106,7 +97,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "7", "metadata": {}, "outputs": [], "source": [ @@ -121,7 +112,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9", + "id": "8", "metadata": {}, "outputs": [], "source": [ @@ -137,7 +128,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -163,9 +154,9 @@ "] = True\n", "Zuiderzeeland[\"gemaal\"].loc[Zuiderzeeland[\"gemaal\"].functiegemaal.str.contains(\"circulatie\"), \"func_circulatie\"] = True\n", "Zuiderzeeland[\"gemaal\"].loc[\n", - " ~Zuiderzeeland[\"gemaal\"].func_afvoer\n", - " & ~Zuiderzeeland[\"gemaal\"].func_aanvoer\n", - " & ~Zuiderzeeland[\"gemaal\"].func_circulatie,\n", + " (Zuiderzeeland[\"gemaal\"].func_afvoer is False)\n", + " & (Zuiderzeeland[\"gemaal\"].func_aanvoer is False)\n", + " & (Zuiderzeeland[\"gemaal\"].func_circulatie is False),\n", " \"func_afvoer\",\n", "] = True # set to afvoergemaal is there the function is unknown" ] @@ -173,7 +164,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "10", "metadata": {}, "outputs": [], "source": [] @@ -181,7 +172,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -255,7 +246,7 @@ }, { "cell_type": "markdown", - "id": "13", + "id": "12", "metadata": {}, "source": [ "### Check for the correct keys and columns" @@ -264,7 +255,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -273,7 +264,7 @@ }, { "cell_type": "markdown", - "id": "15", + "id": "14", "metadata": {}, "source": [ "### Store data" @@ -282,7 +273,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -297,7 +288,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "16", "metadata": {}, "outputs": [], "source": [] @@ -305,7 +296,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "17", "metadata": {}, "outputs": [], "source": [] @@ -313,7 +304,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "18", "metadata": {}, "outputs": [], "source": [] diff --git a/src/peilbeheerst_model/peilbeheerst_model/ribasim_feedback_processor.py b/src/peilbeheerst_model/peilbeheerst_model/ribasim_feedback_processor.py index a1a11bd..ed4edab 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/ribasim_feedback_processor.py +++ b/src/peilbeheerst_model/peilbeheerst_model/ribasim_feedback_processor.py @@ -6,8 +6,6 @@ import pandas as pd import ribasim from pyproj import Proj, Transformer -from ribasim import Node -from ribasim.nodes import discrete_control from shapely.geometry import LineString, Point # Mapping between feedback form and model names @@ -51,12 +49,12 @@ def setup_logging(self): for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) - # logging.basicConfig( - # filename=self.log_filename, - # level=logging.DEBUG, - # format='%(asctime)s - %(levelname)s - %(message)s', - # datefmt='%Y-%m-%d %H:%M:%S' - # ) + logging.basicConfig( + filename=self.log_filename, + level=logging.DEBUG, + format="%(asctime)s - %(levelname)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + ) def load_feedback(self, feedback_excel): df = pd.read_excel(feedback_excel, sheet_name="Feedback_Formulier", skiprows=7) @@ -87,14 +85,15 @@ def get_current_max_nodeid(self): max_id = max(max_ids) return max_id - # def write_ribasim_model(self): - # outputdir = Path(self.output_folder) - # modelcase_dir = Path(f'updated_{self.waterschap.lower()}') + def write_ribasim_model(self): + outputdir = Path(self.output_folder) + # modelcase_dir = Path(f'updated_{self.waterschap.lower()}') - # full_path = outputdir / modelcase_dir - # full_path.mkdir(parents=True, exist_ok=True) + # full_path = outputdir / modelcase_dir + # full_path.mkdir(parents=True, exist_ok=True) - # self.model.write(full_path / "ribasim.toml") + # print(self.output_folder) + self.model.write(outputdir / "ribasim.toml") def update_dataframe_with_new_node_ids(self, node_id_map): for old_id, new_id in node_id_map.items(): @@ -197,96 +196,96 @@ def remove_node(self, row): except Exception as e: logging.error(f"Error removing node {row['Node ID']}: {e}") - def add_discrete_control_node_for_pump(self, pump_node_id, pump_geometry): - logging.info(f"Adding DiscreteControl node for Pump Node ID: {pump_node_id}") - - control_states = ["off", "on"] - dfs_pump = self.model.pump.static.df - - if "control_state" not in dfs_pump.columns.tolist() or pd.isna(dfs_pump.control_state).all(): - dfs_pump_list = [] - for control_state in control_states: - df_pump = dfs_pump.copy() - df_pump["control_state"] = control_state - if control_state == "off": - df_pump["flow_rate"] = 0.0 - dfs_pump_list.append(df_pump) - dfs_pump = pd.concat(dfs_pump_list, ignore_index=True) - self.model.pump.static.df = dfs_pump - - cur_max_nodeid = self.get_current_max_nodeid() - - if cur_max_nodeid < 90000: - new_nodeid = 90000 + cur_max_nodeid + 1 - else: - new_nodeid = cur_max_nodeid + 1 - - basin = self.model.edge.df[ - ((self.model.edge.df["to_node_id"] == pump_node_id) | (self.model.edge.df["from_node_id"] == pump_node_id)) - & ((self.model.edge.df["from_node_type"] == "Basin") | (self.model.edge.df["to_node_type"] == "Basin")) - ] - assert len(basin) >= 1 - basin = basin.iloc[0, :].copy() - if basin["from_node_type"] == "Basin": - compound_variable_id = basin["from_node_id"] - listen_node_id = basin["from_node_id"] - else: - compound_variable_id = basin["to_node_id"] - listen_node_id = basin["to_node_id"] - - df_streefpeilen = self.model.basin.area.df.set_index("node_id") - assert df_streefpeilen.index.is_unique - - try: - self.model.discrete_control.add( - Node(new_nodeid, pump_geometry), - [ - discrete_control.Variable( - compound_variable_id=compound_variable_id, - listen_node_type=["Basin"], - listen_node_id=listen_node_id, - variable=["level"], - ), - discrete_control.Condition( - compound_variable_id=compound_variable_id, - greater_than=[df_streefpeilen.at[listen_node_id, "meta_streefpeil"]], - ), - discrete_control.Logic( - truth_state=["F", "T"], - control_state=control_states, - ), - ], - ) - logging.info(f"Added DiscreteControl Node with ID: {new_nodeid}") - except Exception as e: - logging.error(f"Error adding DiscreteControl Node: {e}") - - try: - self.model.edge.add(self.model.discrete_control[new_nodeid], self.model.pump[pump_node_id]) - logging.info( - f"Added control edge from DiscreteControl Node ID: {new_nodeid} to Pump Node ID: {pump_node_id}" - ) - except Exception as e: - logging.error(f"Error adding control edge: {e}") - - new_node_type_row = pd.DataFrame( - [ - { - "fid": np.nan, - "name": np.nan, - "node_type": "discrete_control", - "subnetwork_id": np.nan, - } - ], - index=[new_nodeid], - ) - - self.df_node_types = pd.concat([self.df_node_types, new_node_type_row]) - - logging.info( - f"Added DiscreteControl node with Node ID: {new_nodeid} at the same location as Pump with Node ID: {pump_node_id}" - ) - logging.info(f"Added control edge from DiscreteControl Node ID: {new_nodeid} to Pump Node ID: {pump_node_id}") + # def add_discrete_control_node_for_pump(self, pump_node_id, pump_geometry): + # logging.info(f"Adding DiscreteControl node for Pump Node ID: {pump_node_id}") + + # control_states = ["off", "on"] + # dfs_pump = ribasim_model.pump.static.df + + # if "control_state" not in dfs_pump.columns.tolist() or pd.isna(dfs_pump.control_state).all(): + # dfs_pump_list = [] + # for control_state in control_states: + # df_pump = ribasim_model.pump.static.df.copy() + # df_pump["control_state"] = control_state + # if control_state == "off": + # df_pump["flow_rate"] = 0.0 + # dfs_pump_list.append(df_pump) + # dfs_pump = pd.concat(dfs_pump_list, ignore_index=True) + # ribasim_model.pump.static.df = dfs_pump + + # cur_max_nodeid = self.get_current_max_nodeid() + + # if cur_max_nodeid < 90000: + # new_nodeid = 90000 + cur_max_nodeid + 1 + # else: + # new_nodeid = cur_max_nodeid + 1 + + # basin = self.model.edge.df[ + # ((self.model.edge.df["to_node_id"] == pump_node_id) | (self.model.edge.df["from_node_id"] == pump_node_id)) + # & ((self.model.edge.df["from_node_type"] == "Basin") | (self.model.edge.df["to_node_type"] == "Basin")) + # ] + # assert len(basin) >= 1 + # basin = basin.iloc[0, :].copy() + # if basin["from_node_type"] == "Basin": + # compound_variable_id = basin["from_node_id"] + # listen_node_id = basin["from_node_id"] + # else: + # compound_variable_id = basin["to_node_id"] + # listen_node_id = basin["to_node_id"] + + # df_streefpeilen = self.model.basin.area.df.set_index("node_id") + # assert df_streefpeilen.index.is_unique + + # try: + # self.model.discrete_control.add( + # Node(new_nodeid, pump_geometry), + # [ + # discrete_control.Variable( + # compound_variable_id=compound_variable_id, + # listen_node_type=["Basin"], + # listen_node_id=listen_node_id, + # variable=["level"], + # ), + # discrete_control.Condition( + # compound_variable_id=compound_variable_id, + # greater_than=[df_streefpeilen.at[listen_node_id, "meta_streefpeil"]], + # ), + # discrete_control.Logic( + # truth_state=["F", "T"], + # control_state=control_states, + # ), + # ], + # ) + # logging.info(f"Added DiscreteControl Node with ID: {new_nodeid}") + # except Exception as e: + # logging.error(f"Error adding DiscreteControl Node: {e}") + + # try: + # self.model.edge.add(self.model.discrete_control[new_nodeid], self.model.pump[pump_node_id]) + # logging.info( + # f"Added control edge from DiscreteControl Node ID: {new_nodeid} to Pump Node ID: {pump_node_id}" + # ) + # except Exception as e: + # logging.error(f"Error adding control edge: {e}") + + # new_node_type_row = pd.DataFrame( + # [ + # { + # "fid": np.nan, + # "name": np.nan, + # "node_type": "discrete_control", + # "subnetwork_id": np.nan, + # } + # ], + # index=[new_nodeid], + # ) + + # self.df_node_types = pd.concat([self.df_node_types, new_node_type_row]) + + # logging.info( + # f"Added DiscreteControl node with Node ID: {new_nodeid} at the same location as Pump with Node ID: {pump_node_id}" + # ) + # logging.info(f"Added control edge from DiscreteControl Node ID: {new_nodeid} to Pump Node ID: {pump_node_id}") def add_node(self, row): try: @@ -587,7 +586,7 @@ def run(self): self.special_preprocessing_for_hollandse_delta() self.process_model() self.save_feedback() - # self.write_ribasim_model() + self.write_ribasim_model() # # Voorbeeld gebruik diff --git a/src/peilbeheerst_model/peilbeheerst_model/ribasim_parametrization.py b/src/peilbeheerst_model/peilbeheerst_model/ribasim_parametrization.py index c4cf884..826a1f4 100644 --- a/src/peilbeheerst_model/peilbeheerst_model/ribasim_parametrization.py +++ b/src/peilbeheerst_model/peilbeheerst_model/ribasim_parametrization.py @@ -1,8 +1,12 @@ # import pathlib +import json +import os +import shutil import subprocess import sys import warnings +import geopandas as gpd import numpy as np import pandas as pd import ribasim @@ -44,7 +48,9 @@ def set_initial_basin_state(ribasim_model): return -def insert_standard_profile(ribasim_model, regular_percentage=10, boezem_percentage=90, depth_profile=2): +def insert_standard_profile( + ribasim_model, unknown_streefpeil, regular_percentage=10, boezem_percentage=90, depth_profile=2 +): profile = ribasim_model.basin.area.df.copy() profile.node_id, profile.meta_streefpeil = ( profile.node_id.astype(int), @@ -80,9 +86,51 @@ def insert_standard_profile(ribasim_model, regular_percentage=10, boezem_percent profile_total = pd.concat([profile_bottom, profile_slightly_above_bottom, profile_top]) profile_total = profile_total.sort_values(by=["node_id", "level", "area"], ascending=True).reset_index(drop=True) + # the profiles of the bergende basins are not the same as the doorgaande basins. Fix this. + profile_total["meta_categorie"] = profile_total.merge(right=ribasim_model.basin.state.df, on="node_id")[ + "meta_categorie" + ] + + # find the node_id of the bergende nodes with the doorgaande nodes + bergende_nodes = profile_total.loc[profile_total.meta_categorie == "bergend"][["node_id"]].reset_index(drop=True) + bergende_nodes["from_MR_node"] = bergende_nodes.merge( + right=ribasim_model.edge.df, left_on="node_id", right_on="from_node_id", how="left" + )["to_node_id"] + + bergende_nodes["doorgaande_node"] = bergende_nodes.merge( + right=ribasim_model.edge.df, left_on="from_MR_node", right_on="from_node_id", how="left" + )["to_node_id"] + + # find the profiles + bergende_nodes = bergende_nodes.drop_duplicates(subset="node_id") + bergende_nodes = bergende_nodes.merge( + right=ribasim_model.basin.profile.df, + left_on="doorgaande_node", + right_on="node_id", + how="inner", + suffixes=("", "doorgaand"), + ) + bergende_nodes["meta_categorie"] = "bergend" + + # add the found profiles in the table + profile_total = profile_total.loc[profile_total.meta_categorie != "bergend"].reset_index( + drop=True + ) # remove bergende profiles, as they will be added here below + profile_total = pd.concat([profile_total, bergende_nodes[["node_id", "level", "area", "meta_categorie"]]]) + profile_total = profile_total.sort_values(by=["node_id", "level"]).reset_index(drop=True) + # insert the new tables in the model ribasim_model.basin.profile.df = profile_total + # due to the bergende basin, the surface area has been doubled. Correct this. + ribasim_model.basin.profile.df.area /= 2 + + # The newly created (storage) basins do not have a correct initial level yet. Fix this as well. + initial_level = ribasim_model.basin.profile.df.copy() + initial_level = initial_level.drop_duplicates(subset="node_id", keep="last") + ribasim_model.basin.state.df["level"] = ribasim_model.basin.state.df.merge(right=initial_level, on="node_id")[ + "level_y" + ] return @@ -299,9 +347,12 @@ def create_linestring(row): def add_outlets(ribasim_model, delta_crest_level=0.10): # select all TRC's which are inlaten # display(ribasim_model.tabulated_rating_curve.static.df) - TRC_naar_OL = ribasim_model.tabulated_rating_curve.static.df.loc[ - ribasim_model.tabulated_rating_curve.static.df.meta_type_verbinding == "Inlaat" - ] + # TRC_naar_OL = ribasim_model.tabulated_rating_curve.static.df.loc[ + # ribasim_model.tabulated_rating_curve.static.df.meta_type_verbinding == "Inlaat" + # ] + + # update: change all TRC's to Outlets + TRC_naar_OL = ribasim_model.tabulated_rating_curve.static.df.copy() TRC_naar_OL = TRC_naar_OL.drop_duplicates(subset="node_id", keep="first") TRC_naar_OL = TRC_naar_OL[["node_id"]] @@ -337,7 +388,7 @@ def add_outlets(ribasim_model, delta_crest_level=0.10): # add the outlets to the model ribasim_model.outlet.node.df = outlet[["node_id", "node_type", "geometry"]] - ribasim_model.outlet.static.df = outlet[["node_id", "flow_rate", "meta_categorie"]] + ribasim_model.outlet.static.df = outlet[["node_id", "flow_rate", "min_crest_level", "meta_categorie"]] # remove the TRC's nodes ribasim_model.tabulated_rating_curve.node = ribasim_model.tabulated_rating_curve.node.df.loc[ @@ -427,29 +478,47 @@ def add_discrete_control_nodes(ribasim_model): return -def set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=4): - df_edge = ribasim_model.edge.df - df_edge_tab = ribasim_model.tabulated_rating_curve.static.df.merge( - df_edge, left_on="node_id", right_on="to_node_id", how="inner" +def set_tabulated_rating_curves(ribasim_model, level_increase=1.0, flow_rate=4, LevelBoundary_level=0): + """Create the Q(h)-relations for each TRC. It starts passing water from target level onwards.""" + # find the originating basin of each TRC + target_level = ribasim_model.edge.df.loc[ + ribasim_model.edge.df.to_node_type == "TabulatedRatingCurve" + ] # select all TRC's. Do this from the edge table, so we can look the basins easily up afterwards + + # find the target level + target_level = pd.merge( + left=target_level, + right=ribasim_model.basin.state.df[["node_id", "level"]], + left_on="from_node_id", + right_on="node_id", + how="left", ) - df_tab = ribasim_model.basin.state.df.merge(df_edge_tab, left_on="node_id", right_on="from_node_id", how="inner") - df_tab = df_tab[["to_node_id", "active", "level_x", "flow_rate", "control_state", "meta_type_verbinding"]] - df_tab = df_tab.rename(columns={"to_node_id": "node_id", "level_x": "level"}) - def adjust_rows(df): - df["row_num"] = df.groupby("node_id").cumcount() + 1 - df.loc[df["row_num"] == 2, "level"] += level_increase - df.loc[df["row_num"] == 2, "flow_rate"] = flow_rate - df.drop(columns="row_num", inplace=True) + target_level.level.fillna(value=LevelBoundary_level, inplace=True) + + # zero flow rate on target level + Qh_table0 = target_level[["to_node_id", "level"]] + Qh_table0 = Qh_table0.rename(columns={"to_node_id": "node_id"}) + Qh_table0["flow_rate"] = 0 - return df + # pre defined flow rate on target level + level increase + Qh_table1 = Qh_table0.copy() + Qh_table1["level"] += level_increase + Qh_table1["flow_rate"] = flow_rate - # Apply the function - df_tab.node_id = df_tab.node_id.astype(int) - df_tab.level = df_tab.level.astype(float) - df_tab.flow_rate = df_tab.flow_rate.astype(float) + # combine tables, sort, reset index + Qh_table = pd.concat([Qh_table0, Qh_table1]) + Qh_table.sort_values(by=["node_id", "level", "flow_rate"], inplace=True) + Qh_table.reset_index(drop=True, inplace=True) - ribasim_model.tabulated_rating_curve.static.df = adjust_rows(df_tab) + ribasim_model.tabulated_rating_curve.static.df = Qh_table + + # remove all redundand TRC nodes + ribasim_model.tabulated_rating_curve.node.df = ribasim_model.tabulated_rating_curve.node.df.loc[ + ribasim_model.tabulated_rating_curve.node.df.node_id.isin(Qh_table.node_id) + ] + ribasim_model.tabulated_rating_curve.node.df.sort_values(by="node_id", inplace=True) + ribasim_model.tabulated_rating_curve.node.df.reset_index(drop=True, inplace=True) return @@ -501,28 +570,92 @@ def set_tabulated_rating_curves_boundaries(ribasim_model, level_increase=0.1, fl return +def create_sufficient_Qh_relation_points(ribasim_model): + """There are more TRC nodes than defined in the static table. Identify the nodes which occur less than twice in the table, and create a (for now) dummy relation. Also delete the TRC in the static table if it doesnt occur in the node table""" + # #get rid of all TRC's static rows which do not occur in the node table (assuming the node table is the groundtruth) + # TRC_nodes = ribasim_model.tabulated_rating_curve.node.df.node_id.values + # ribasim_model.tabulated_rating_curve.static.df = ribasim_model.tabulated_rating_curve.static.df.loc[ribasim_model.tabulated_rating_curve.static.df.node_id.isin(TRC_nodes)] + + # #put all TRC's nodes on one pile. So both the static as well as the node df + # TRC_pile = pd.concat([ribasim_model.tabulated_rating_curve.static.df['node_id'], + # ribasim_model.tabulated_rating_curve.node.df['node_id']]) + + # #each node_id should occur at least three times in the pile (once because of the node, twice because of the Qh relation) + # node_id_counts = ribasim_model.tabulated_rating_curve.static.df['node_id'].value_counts() + + # #select all nodes which occur less than 3 times + # unique_node_ids = node_id_counts[node_id_counts < 3].index + + # #create new Qh relations + # zero_flow = ribasim_model.tabulated_rating_curve.static.df[ribasim_model.tabulated_rating_curve.static.df['node_id'].isin(unique_node_ids)] + # one_flow = zero_flow.copy() + # zero_flow.flow_rate = 0 #set flow rate to 0 if on target level + # one_flow.level += 1 #set level 1 meter higher where it discharges 1 m3/s + + # #remove old Qh points + # ribasim_model.tabulated_rating_curve.static.df = ribasim_model.tabulated_rating_curve.static.df.loc[~ribasim_model.tabulated_rating_curve.static.df['node_id'].isin(unique_node_ids)] + + # #add the new Qh points back in the df + # ribasim_model.tabulated_rating_curve.static.df = pd.concat([ribasim_model.tabulated_rating_curve.static.df, + # zero_flow, + # one_flow]) + # #drop duplicates, sort and reset index + # ribasim_model.tabulated_rating_curve.static.df.drop_duplicates(subset = ['node_id', 'level'], inplace = True) + # ribasim_model.tabulated_rating_curve.static.df.sort_values(by=['node_id', 'level', 'flow_rate'], inplace = True) + # ribasim_model.tabulated_rating_curve.node.df.sort_values(by=['node_id'], inplace = True) + # ribasim_model.tabulated_rating_curve.static.df.reset_index(drop = True, inplace = True) + + # print(len(TRC_nodes)) + # print(len(ribasim_model.tabulated_rating_curve.static.df.node_id.unique())) + + return + + def write_ribasim_model_Zdrive(ribasim_model, path_ribasim_toml): # Write Ribasim model to the Z drive + if not os.path.exists(path_ribasim_toml): + os.makedirs(path_ribasim_toml) + ribasim_model.write(path_ribasim_toml) def write_ribasim_model_GoodCloud( ribasim_model, path_ribasim_toml, waterschap, modeltype="boezemmodel", include_results=True ): - # Write Ribasim model to the Z drive again, as we want to store the results as well - ribasim_model.write(path_ribasim_toml) + # copy the results folder from the "updated" folder to the "Ribasim_networks" folder + results_source = f"../../../../../Ribasim_updated_models/{waterschap}/modellen/{waterschap}_parametrized/results" + parametrized_location = ( + f"../../../../../Ribasim_networks/Waterschappen/{waterschap}/modellen/{waterschap}_parametrized" + ) + + if not os.path.exists(parametrized_location): + os.makedirs(parametrized_location) + + # If the destination folder of the results already exists, remove it + print(os.path.join(parametrized_location, "results")) + if os.path.exists(os.path.join(parametrized_location, "results")): + shutil.rmtree(os.path.join(parametrized_location, "results")) + + # copy the results to the Ribasim_networks folder + shutil.copytree(results_source, os.path.join(parametrized_location, "results")) + + # copy the model to the Ribasim_networks folder + parametrized_location = os.path.join(parametrized_location, "ribasim.toml") + ribasim_model.write( + parametrized_location + ) # write to the "Ribasim_networks" folder (will NOT be overwritten at each upload) - # Write Ribasim model to the GoodCloud path_goodcloud_password = "../../../../../Data_overig/password_goodcloud.txt" with open(path_goodcloud_password) as file: password = file.read() - # Gain access to the goodcloud - cloud_storage = CloudStorage(password=password, data_dir=r"../../../../../Ribasim_networks/Waterschappen/") + cloud_storage = CloudStorage( + password=password, + data_dir=r"../../../../../Ribasim_networks/Waterschappen/", + ) - # Upload the model cloud_storage.upload_model( - authority=waterschap, model=waterschap + "_" + modeltype, include_results=include_results + authority=waterschap, model=waterschap + "_parametrized", include_results=include_results ) print(f"The model of waterboard {waterschap} has been uploaded to the goodcloud in the directory of {modeltype}!") @@ -684,6 +817,7 @@ def validate_basin_area(model): :param model: The ribasim model to validate :return: None """ + too_small_basins = [] error = False for index, row in model.basin.node.df.iterrows(): basin_id = int(row["node_id"]) @@ -693,9 +827,620 @@ def validate_basin_area(model): if basin_area < 100: error = True print(f"Basin with Node ID {basin_id} has an area smaller than 100 m²: {basin_area} m²") + too_small_basins.append(basin_id) if not error: print("All basins are larger than 100 m²") + return + + +def identify_node_meta_categorie(ribasim_model): + """ + Identify the meta_categorie of each Outlet, Pump and LevelBoundary. + + It checks whether they are inlaten en uitlaten from a boezem, buitenwater or just regular peilgebieden. + This will determine the rules of the control nodes. + """ + # create new columsn to store the meta categorie of each node + ribasim_model.outlet.static.df["meta_categorie"] = np.nan + ribasim_model.pump.static.df["meta_categorie"] = np.nan + + # select all basins which are not "bergend" + basin_nodes = ribasim_model.basin.state.df.copy() + # peilgebied_basins = basin_nodes.loc[basin_nodes.meta_categorie == "doorgaand", "node_id"] + boezem_basins = basin_nodes.loc[basin_nodes.meta_categorie == "hoofdwater", "node_id"] + + # select the nodes which originate from a boezem, and the ones which go to a boezem. Use the edge table for this. + nodes_from_boezem = ribasim_model.edge.df.loc[ribasim_model.edge.df.from_node_id.isin(boezem_basins), "to_node_id"] + nodes_to_boezem = ribasim_model.edge.df.loc[ribasim_model.edge.df.to_node_id.isin(boezem_basins), "from_node_id"] + + # select the nodes which originate from, and go to a boundary + nodes_from_boundary = ribasim_model.edge.df.loc[ + ribasim_model.edge.df.from_node_type == "LevelBoundary", "to_node_id" + ] + nodes_to_boundary = ribasim_model.edge.df.loc[ribasim_model.edge.df.to_node_type == "LevelBoundary", "from_node_id"] + + # identify the INlaten from the boezem, both stuwen (outlets) and gemalen (pumps) + ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.node_id.isin(nodes_from_boezem), "meta_categorie" + ] = "Inlaat boezem, stuw" + ribasim_model.pump.static.df.loc[ribasim_model.pump.static.df.node_id.isin(nodes_from_boezem), "meta_categorie"] = ( + "Inlaat boezem, gemaal" + ) + + # identify the UITlaten from the boezem, both stuwen (outlets) and gemalen (pumps) + ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.node_id.isin(nodes_to_boezem), "meta_categorie" + ] = "Uitlaat boezem, stuw" + ribasim_model.pump.static.df.loc[ribasim_model.pump.static.df.node_id.isin(nodes_to_boezem), "meta_categorie"] = ( + "Uitlaat boezem, gemaal" + ) + + # identify the outlets and pumps at the regular peilgebieden + ribasim_model.outlet.static.df.loc[ + ~( + (ribasim_model.outlet.static.df.node_id.isin(nodes_from_boezem)) + | (ribasim_model.outlet.static.df.node_id.isin(nodes_to_boezem)) + ), + "meta_categorie", + ] = "Reguliere stuw" + ribasim_model.pump.static.df.loc[ + ~( + (ribasim_model.pump.static.df.node_id.isin(nodes_from_boezem)) + | (ribasim_model.pump.static.df.node_id.isin(nodes_to_boezem)) + ), + "meta_categorie", + ] = "Regulier gemaal" + + # repeat for the boundary nodes + # identify the buitenwater uitlaten and inlaten. A part will be overwritten later, if its a boundary & boezem. + ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.node_id.isin(nodes_to_boundary), "meta_categorie" + ] = "Uitlaat buitenwater peilgebied, stuw" + ribasim_model.pump.static.df.loc[ribasim_model.pump.static.df.node_id.isin(nodes_to_boundary), "meta_categorie"] = ( + "Uitlaat buitenwater peilgebied, gemaal" + ) + + ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.node_id.isin(nodes_from_boundary), "meta_categorie" + ] = "Inlaat buitenwater peilgebied, stuw" + ribasim_model.pump.static.df.loc[ + ribasim_model.pump.static.df.node_id.isin(nodes_from_boundary), "meta_categorie" + ] = "Inlaat buitenwater peilgebied, gemaal" + + # boundary & boezem. This is the part where a portion of the already defined meta_categorie will be overwritten by the code above. + ribasim_model.outlet.static.df.loc[ + (ribasim_model.outlet.static.df.node_id.isin(nodes_to_boundary)) + & (ribasim_model.outlet.static.df.node_id.isin(nodes_from_boezem)), # to + "meta_categorie", + ] = "Uitlaat buitenwater boezem, stuw" + ribasim_model.pump.static.df.loc[ + (ribasim_model.pump.static.df.node_id.isin(nodes_to_boundary)) + & (ribasim_model.pump.static.df.node_id.isin(nodes_from_boezem)), # to + "meta_categorie", + ] = "Uitlaat buitenwater boezem, gemaal" + + ribasim_model.outlet.static.df.loc[ + (ribasim_model.outlet.static.df.node_id.isin(nodes_from_boundary)) + & (ribasim_model.outlet.static.df.node_id.isin(nodes_to_boezem)), # from + "meta_categorie", + ] = "Inlaat buitenwater boezem, stuw" + ribasim_model.pump.static.df.loc[ + (ribasim_model.pump.static.df.node_id.isin(nodes_from_boundary)) + & (ribasim_model.pump.static.df.node_id.isin(nodes_to_boezem)), # from + "meta_categorie", + ] = "Inlaat buitenwater boezem, gemaal" + + # boezem & boezem. + ribasim_model.outlet.static.df.loc[ + (ribasim_model.outlet.static.df.node_id.isin(nodes_from_boezem)) + & (ribasim_model.outlet.static.df.node_id.isin(nodes_to_boezem)), + "meta_categorie", + ] = "Boezem boezem, stuw" + + ribasim_model.pump.static.df.loc[ + (ribasim_model.pump.static.df.node_id.isin(nodes_from_boezem)) + & (ribasim_model.pump.static.df.node_id.isin(nodes_to_boezem)), + "meta_categorie", + ] = "Boezem boezem, gemaal" + + # some pumps have been added due to the feedback form. Assume all these nodes are afvoer gemalen + ribasim_model.pump.static.df.meta_func_afvoer.fillna(value=1.0, inplace=True) + ribasim_model.pump.static.df.meta_func_aanvoer.fillna(value=0.0, inplace=True) + ribasim_model.pump.static.df.meta_func_circulatie.fillna(value=0.0, inplace=True) + + return + + +def load_model_settings(file_path): + with open(file_path) as file: + settings = json.load(file) + return settings + + +def add_discrete_control(ribasim_model, waterschap, default_level): + """Add discrete control nodes to the network. The rules are based on the meta_categorie of each node.""" + # load in the sturing which is defined in the json files + sturing = load_model_settings(f"sturing_{waterschap}.json") + + # Remove all Discrete Control nodes and edges if its present + ribasim_model.discrete_control.node.df = ribasim_model.discrete_control.node.df.iloc[0:0] + if ribasim_model.discrete_control.condition.df is not None: + ribasim_model.discrete_control.condition.df = ribasim_model.discrete_control.condition.df.iloc[0:0] + ribasim_model.discrete_control.logic.df = ribasim_model.discrete_control.logic.df.iloc[0:0] + ribasim_model.discrete_control.variable.df = ribasim_model.discrete_control.variable.df.iloc[0:0] + ribasim_model.edge.df = ribasim_model.edge.df.loc[ribasim_model.edge.df.edge_type != "control"] + + # start assigning sturing to outlets/weirs + # find the nodes to change + inlaat_boezem_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Inlaat boezem, stuw", "node_id" + ] + uitlaat_boezem_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Uitlaat boezem, stuw", "node_id" + ] + reguliere_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Reguliere stuw", "node_id" + ] + inlaat_buitenwater_peilgebied_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Inlaat buitenwater peilgebied, stuw", "node_id" + ] + uitlaat_buitenwater_peilgebied_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Uitlaat buitenwater peilgebied, stuw", "node_id" + ] + boezem_boezem_stuw = ribasim_model.outlet.static.df.loc[ + ribasim_model.outlet.static.df.meta_categorie == "Boezem boezem, stuw", "node_id" + ] + + # assign the sturing for the weirs/outlets. + nodes_to_control_list_stuw = [ + inlaat_boezem_stuw, + uitlaat_boezem_stuw, + reguliere_stuw, + inlaat_buitenwater_peilgebied_stuw, + uitlaat_buitenwater_peilgebied_stuw, + boezem_boezem_stuw, + ] + + category_list_stuw = [ + "Inlaat boezem, stuw", + "Uitlaat boezem, stuw", + "Reguliere stuw", + "Inlaat buitenwater peilgebied, stuw", + "Uitlaat buitenwater peilgebied, stuw", + "Boezem boezem, stuw", + ] + + # fill the discrete control. Do this table by tables, where the condition table is determined by the meta_categorie + for nodes_to_control, category in zip(nodes_to_control_list_stuw, category_list_stuw): + if len(nodes_to_control) > 0: + print(f"Sturing has been added for the category {category}") + add_discrete_control_partswise( + ribasim_model=ribasim_model, + nodes_to_control=nodes_to_control, + category=category, + sturing=sturing, + default_level=default_level, + ) + else: + print(f"No stuwen are found in the category of {category}") + + # repeat for the pumps + # find the nodes to change + inlaat_boezem_gemaal = ribasim_model.pump.static.df.loc[ + ribasim_model.pump.static.df.meta_categorie == "Inlaat boezem, gemaal", "node_id" + ] + uitlaat_boezem_gemaal = ribasim_model.pump.static.df.loc[ + ribasim_model.pump.static.df.meta_categorie == "Uitlaat boezem, gemaal", "node_id" + ] + + regulier_gemaal_afvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Regulier gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 0) + ), + "node_id", + ] + regulier_gemaal_aanvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Regulier gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 1) + ), + "node_id", + ] + + uitlaat_buitenwater_peilgebied_gemaal_afvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Uitlaat buitenwater peilgebied, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 0) + ), + "node_id", + ] + uitlaat_buitenwater_peilgebied_gemaal_aanvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Uitlaat buitenwater peilgebied, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 1) + ), + "node_id", + ] + + inlaat_buitenwater_peilgebied_gemaal_afvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Inlaat buitenwater peilgebied, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 0) + ), + "node_id", + ] + inlaat_buitenwater_peilgebied_gemaal_aanvoer = ribasim_model.pump.static.df.loc[ + ( + (ribasim_model.pump.static.df.meta_categorie == "Inlaat buitenwater peilgebied, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 1) + ), + "node_id", + ] + + # display(inlaat_buitenwater_peilgebied_gemaal_afvoer) + # display(inlaat_buitenwater_peilgebied_gemaal_aanvoer) + boezem_boezem_gemaal_afvoer = ribasim_model.outlet.static.df.loc[ + ( + (ribasim_model.outlet.static.df.meta_categorie == "Boezem boezem, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 0) + ), + "node_id", + ] + boezem_boezem_gemaal_aanvoer = ribasim_model.outlet.static.df.loc[ + ( + (ribasim_model.outlet.static.df.meta_categorie == "Boezem boezem, gemaal") + & (ribasim_model.pump.static.df.meta_func_afvoer != 1) + ), + "node_id", + ] + + # assign the sturing for the gemalen/pumps. + nodes_to_control_list_gemaal = [ + inlaat_boezem_gemaal, + uitlaat_boezem_gemaal, + regulier_gemaal_afvoer, + regulier_gemaal_aanvoer, + uitlaat_buitenwater_peilgebied_gemaal_afvoer, + uitlaat_buitenwater_peilgebied_gemaal_aanvoer, + inlaat_buitenwater_peilgebied_gemaal_afvoer, # + inlaat_buitenwater_peilgebied_gemaal_aanvoer, # + boezem_boezem_gemaal_afvoer, + boezem_boezem_gemaal_aanvoer, + ] + + category_list_gemaal = [ + "Inlaat boezem, gemaal", + "Uitlaat boezem, gemaal", + "Regulier afvoer gemaal", + "Regulier aanvoer gemaal", + "Uitlaat buitenwater peilgebied, afvoer gemaal", + "Uitlaat buitenwater peilgebied, aanvoer gemaal", + "Inlaat buitenwater peilgebied, afvoer gemaal", # + "Inlaat buitenwater peilgebied, aanvoer gemaal", # + "Boezem boezem, afvoer gemaal", + "Boezem boezem, aanvoer gemaal", + ] + + # fill the discrete control. Do this table by tables, where the condition table is determined by the meta_categorie + for nodes_to_control, category in zip(nodes_to_control_list_gemaal, category_list_gemaal): + if len(nodes_to_control) > 0: + print(f"Sturing has been added for the category {category}") + add_discrete_control_partswise( + ribasim_model=ribasim_model, + nodes_to_control=nodes_to_control, + category=category, + sturing=sturing, + default_level=default_level, + ) + else: + print(f"No gemalen are found in the category of {category}") + + # # fill the discrete control. Do this table by tables, where the condition table is determined by the meta_categorie. Start with the outlets/stuwen + # add_discrete_control_partswise( + # ribasim_model=ribasim_model, + # nodes_to_control=inlaat_boezem_stuw, + # category='Inlaat boezem, stuw', + # sturing = sturing, + # default_level = default_level) + + # many duplicate values have been created. Discard those. + # ribasim_model.outlet.static.df = ribasim_model.outlet.static.df.drop_duplicates().reset_index(drop=True) + # ribasim_model.pump.static.df = ribasim_model.pump.static.df.drop_duplicates().reset_index(drop=True) + + # a DC node occures twice in the table of teh nodes at case of AGV, while this node is not present at all in the DC tables. REmove it + DC_nodes = pd.concat( + [ + ribasim_model.discrete_control.logic.df.node_id, + ribasim_model.discrete_control.variable.df.node_id, + ribasim_model.discrete_control.condition.df.node_id, + ] + ) + + DC_nodes = DC_nodes.drop_duplicates().reset_index(drop=True) + + # add meta_downstream to the DC variable + ribasim_model.discrete_control.variable.df["meta_downstream"] = ribasim_model.discrete_control.variable.df.merge( + right=ribasim_model.discrete_control.condition.df, + left_on=["compound_variable_id", "listen_node_id"], + right_on=["compound_variable_id", "meta_listen_node_id"], + how="left", + )[["meta_downstream"]] + + ribasim_model.discrete_control.node.df = ribasim_model.discrete_control.node.df.loc[ + ribasim_model.discrete_control.node.df.node_id.isin(DC_nodes.values) + ].reset_index(drop=True) + ribasim_model.discrete_control.node.df = ribasim_model.discrete_control.node.df.drop_duplicates( + subset="node_id" + ).reset_index(drop=True) + ribasim_model.discrete_control.condition.df = ( + ribasim_model.discrete_control.condition.df.drop_duplicates() + .sort_values(by=["node_id", "meta_downstream"]) + .reset_index(drop=True) + ) + ribasim_model.discrete_control.variable.df = ( + ribasim_model.discrete_control.variable.df.drop_duplicates() + .sort_values(by=["node_id", "meta_downstream"]) + .reset_index(drop=True) + ) + ribasim_model.discrete_control.logic.df = ( + ribasim_model.discrete_control.logic.df.drop_duplicates() + .sort_values(by=["node_id", "truth_state"]) + .reset_index(drop=True) + ) + + return + + +def add_discrete_control_partswise(ribasim_model, nodes_to_control, category, sturing, default_level): + # define the sturing parameters in variables + upstream_level_offset = sturing[category]["upstream_level_offset"] + truth_state = sturing[category]["truth_state"] + control_state = sturing[category]["control_state"] + flow_rate_block = sturing[category]["flow_rate_block"] + flow_rate_pass = sturing[category]["flow_rate_pass"] + node_type = sturing[category]["node_type"] + + ### node #################################################### + # add the discrete control .node table. The node_ids are the same as the node_id of the outlet/pump, but 80.000 is added + DC_nodes = pd.DataFrame() + DC_nodes["node_id"] = nodes_to_control.astype(int) + 80000 + + # trace back the node_id which the DiscreteControl controls, including the compoun_variable_id which is set the same as the node_id + DC_nodes["meta_control_node_id"] = nodes_to_control + DC_nodes["meta_compound_variable_id"] = DC_nodes["node_id"] + DC_nodes["node_type"] = "DiscreteControl" + DC_nodes = DC_nodes.sort_values(by="node_id").reset_index(drop=True) + + # retrieve the geometries of the DiscreteControl. Put it at the same location, so they may be stored in either the Outlets or the Pumps, so check both if + if node_type == "outlet" or node_type == "Outlet": + DC_nodes["geometry"] = DC_nodes.merge( + right=ribasim_model.outlet.node.df[["node_id", "geometry"]], + left_on="meta_control_node_id", + right_on="node_id", + how="left", + )["geometry"] + elif node_type == "pump" or node_type == "Pump": + DC_nodes["geometry"] = DC_nodes.merge( + right=ribasim_model.pump.node.df[["node_id", "geometry"]], + left_on="meta_control_node_id", + right_on="node_id", + how="left", + )["geometry"] + + DC_nodes = DC_nodes[["node_id", "node_type", "meta_control_node_id", "meta_compound_variable_id", "geometry"]] + + # concat the DC_nodes to the ribasim model + ribasim_model.discrete_control.node.df = ( + pd.concat([ribasim_model.discrete_control.node.df, DC_nodes]).sort_values(by="node_id").reset_index(drop=True) + ) + ribasim_model.discrete_control.node.df = gpd.GeoDataFrame( + ribasim_model.discrete_control.node.df, geometry="geometry" + ) + + ### node OUTLET static ### + if node_type == "outlet" or node_type == "Outlet": + # df when water is blocked + outlet_static_block = ribasim_model.outlet.static.df.copy() + outlet_static_block["control_state"] = "block" + outlet_static_block["flow_rate"] = flow_rate_block + outlet_static_block["min_crest_level"] = ( + np.nan + ) # min crest level is redundant, as control is defined for both upstream as well as downstream levels + + # df when water is passed + outlet_static_pass = ribasim_model.outlet.static.df.copy() + outlet_static_pass["control_state"] = "pass" + outlet_static_pass["flow_rate"] = flow_rate_pass + outlet_static_pass["min_crest_level"] = ( + np.nan + ) # min crest level is redundant, as control is defined for both upstream as well as downstream levels + + outlet_static = ( + pd.concat([outlet_static_block, outlet_static_pass]) + .sort_values(by=["node_id", "control_state"]) + .reset_index(drop=True) + ) + ribasim_model.outlet.static.df = outlet_static + + ### node PUMP static ### + if node_type == "pump" or node_type == "Pump": + # df when water is blocked + pump_static_block = ribasim_model.pump.static.df.copy() + pump_static_block["control_state"] = "block" + pump_static_block["flow_rate"] = flow_rate_block + + # df when water is passed + pump_static_pass = ribasim_model.pump.static.df.copy() + pump_static_pass["control_state"] = "pass" + pump_static_pass["flow_rate"] = flow_rate_pass + + pump_static = ( + pd.concat([pump_static_block, pump_static_pass]) + .sort_values(by=["node_id", "control_state"]) + .reset_index(drop=True) + ) + ribasim_model.pump.static.df = pump_static + # display(pump_static) + + ### condition #################################################### + # create the DiscreteControl condition table + DC_condition_us = pd.DataFrame() + DC_condition_us["node_id"] = ribasim_model.discrete_control.node.df["node_id"] + DC_condition_us["meta_control_node_id"] = ribasim_model.discrete_control.node.df["meta_control_node_id"] + DC_condition_us["compound_variable_id"] = ribasim_model.discrete_control.node.df["meta_compound_variable_id"] + DC_condition_ds = DC_condition_us.copy(deep=True) + + # find the greather_than value by looking the corresponding UPstream basin up in the edge table + basin_to_control_node_us = ribasim_model.edge.df.loc[ + ribasim_model.edge.df.to_node_id.isin(nodes_to_control.values) + ] # ['from_node_id'] + basin_to_control_node_us = basin_to_control_node_us.merge( + right=ribasim_model.basin.state.df, left_on="from_node_id", right_on="node_id", how="left" + )[["to_node_id", "to_node_type", "level", "from_node_id", "from_node_type"]] + basin_to_control_node_us["meta_to_control_node_id"] = basin_to_control_node_us["to_node_id"] + basin_to_control_node_us["meta_to_control_node_type"] = basin_to_control_node_us["to_node_type"] + + DC_condition_us = DC_condition_us.merge( + right=basin_to_control_node_us, left_on="meta_control_node_id", right_on="to_node_id" + ) + DC_condition_us["level"] -= upstream_level_offset + + # formatting + DC_condition_us.rename( + columns={ + "level": "greater_than", + "from_node_id": "meta_listen_node_id", + "from_node_type": "meta_listen_node_type", + }, + inplace=True, + ) + DC_condition_us = DC_condition_us[ + [ + "node_id", + "compound_variable_id", + "greater_than", + "meta_listen_node_id", + "meta_listen_node_type", + "meta_to_control_node_id", + "meta_to_control_node_type", + ] + ] + DC_condition_us["meta_downstream"] = 0 # add a column to sort it later on + + # for each row, there is (incorrectly) another row added where the listen node is the DiscreteControl. This should not be the case. Remove it + DC_condition_us = DC_condition_us.loc[DC_condition_us.meta_listen_node_type != "DiscreteControl"] + + # the upstream node which is listened to is found. Now, find the downstream listen node. + # basically repeat the same lines as above + basin_to_control_node_ds = ribasim_model.edge.df.loc[ + ribasim_model.edge.df.from_node_id.isin(nodes_to_control.values) + ] + basin_to_control_node_ds = basin_to_control_node_ds.merge( + right=ribasim_model.basin.state.df, left_on="to_node_id", right_on="node_id", how="left" + )[["from_node_id", "from_node_type", "level", "to_node_id", "to_node_type"]] + + DC_condition_ds = DC_condition_ds.merge( + right=basin_to_control_node_ds, left_on="meta_control_node_id", right_on="from_node_id" + ) + DC_condition_ds["level"] -= upstream_level_offset + + # formatting + DC_condition_ds.rename( + columns={"level": "greater_than", "to_node_id": "meta_listen_node_id", "to_node_type": "meta_listen_node_type"}, + inplace=True, + ) + DC_condition_ds = DC_condition_ds[ + ["node_id", "compound_variable_id", "greater_than", "meta_listen_node_id", "meta_listen_node_type"] + ] + DC_condition_ds["meta_downstream"] = 1 # add a column to sort it later on + + # add some more columns so the downstream table matches the upstream table. Not sure why this is not created + DC_condition_ds["meta_to_control_node_id"] = DC_condition_ds.merge( + right=DC_condition_us, on="compound_variable_id", how="left" + )["meta_to_control_node_id"] + DC_condition_ds["meta_to_control_node_type"] = DC_condition_ds.merge( + right=DC_condition_us, on="compound_variable_id", how="left" + )["meta_to_control_node_type"] + + # concat the upstream and the downstream condition table + DC_condition = pd.concat([DC_condition_us, DC_condition_ds]) + + # every basin should have a target level by this part of the code. However, LevelBoundaries may not. Implement it + DC_condition.greater_than.fillna(value=default_level, inplace=True) + + # concat the entire DC_condition to the ribasim model + ribasim_model.discrete_control.condition.df = pd.concat([ribasim_model.discrete_control.condition.df, DC_condition]) + ribasim_model.discrete_control.condition.df = ribasim_model.discrete_control.condition.df.sort_values( + by=["node_id", "meta_downstream"] + ).reset_index(drop=True) + + ### logic #################################################### + DC_logic = pd.DataFrame() + for i in range(len(truth_state)): + DC_logic_temp = DC_condition.copy()[["node_id"]].drop_duplicates() + DC_logic_temp["truth_state"] = truth_state[i] + DC_logic_temp["control_state"] = control_state[i] + + DC_logic = pd.concat([DC_logic, DC_logic_temp]) + + # concat the DC_condition to the ribasim model + ribasim_model.discrete_control.logic.df = pd.concat([ribasim_model.discrete_control.logic.df, DC_logic]) + ribasim_model.discrete_control.logic.df = ribasim_model.discrete_control.logic.df.sort_values( + by=["node_id", "truth_state"] + ).reset_index(drop=True) + + ### variable #################################################### + DC_variable = DC_condition.copy()[ + ["node_id", "compound_variable_id", "meta_listen_node_id", "meta_listen_node_type"] + ] + DC_variable.rename( + columns={"meta_listen_node_id": "listen_node_id", "meta_listen_node_type": "listen_node_type"}, inplace=True + ) + DC_variable["variable"] = "level" + + # concat the DC_variable to the ribasim model + ribasim_model.discrete_control.variable.df = pd.concat([ribasim_model.discrete_control.variable.df, DC_variable]) + ribasim_model.discrete_control.variable.df = ribasim_model.discrete_control.variable.df.sort_values( + by=["node_id", "listen_node_id"] + ).reset_index(drop=True) + + ### edge #################################################### + DC_edge = DC_condition.copy()[["node_id", "meta_to_control_node_id", "meta_to_control_node_type"]] + + # as the DC listens to both the upstream as well as the downstream nodes, it contains twice the node_ids. Only select one. + DC_edge = DC_edge.drop_duplicates(subset="node_id") + DC_edge.rename( + columns={ + "node_id": "from_node_id", + "meta_to_control_node_id": "to_node_id", + "meta_to_control_node_type": "to_node_type", + }, + inplace=True, + ) + + # DC_edge['to_node_type'] = ribasim_model.edge.df.loc[ribasim_model.edge.df.to_node_id == DC_edge.node_id, 'to_node_type'] + DC_edge["from_node_type"] = "DiscreteControl" + DC_edge["edge_type"] = "control" + DC_edge["meta_categorie"] = "DC_control" + + # retrieve the FROM geometry from the DC_nodes. The TO is the same, as the DiscreteControl is on the same location + DC_edge["from_coord"] = DC_nodes["geometry"] + DC_edge["to_coord"] = DC_nodes["geometry"] + + def create_linestring(row): + return LineString([row["from_coord"], row["to_coord"]]) + + DC_edge["geometry"] = DC_edge.apply(create_linestring, axis=1) + + DC_edge = DC_edge[ + ["from_node_id", "from_node_type", "to_node_id", "to_node_type", "edge_type", "meta_categorie", "geometry"] + ] + ribasim_model.edge.df = pd.concat([ribasim_model.edge.df, DC_edge]).reset_index(drop=True) + + return + ##################### Recycle bin ########################## # def calculate_update_basin_area(ribasim_model, percentage): diff --git a/src/ribasim_lumping/README.md b/src/ribasim_lumping/README.md index a5ee50c..de8ca0d 100644 --- a/src/ribasim_lumping/README.md +++ b/src/ribasim_lumping/README.md @@ -5,10 +5,10 @@ This python package is used to develop an aggregated Ribasim network (Deltares, This code was integrated into the Ribasim-NL repository after development in https://github.com/harm-nomden-sweco/ribasim_lumping. ### Objective -This python-package provides functions to translate a D-Hydro or a HyDAMO network into a simplified (aggregated/lumped) Ribasim-network: +This python-package provides functions to translate a D-Hydro or a HyDAMO network into a simplified (aggregated/lumped) Ribasim-network: - The user provides a list of locations where the network should be split, resulting into sub-networks which are called 'basins''; - For these basins relations regarding waterlevel-watersurface-watervolume are calculated; -- Exchange of watervolume (flow) between basins takes place via these split locations. +- Exchange of watervolume (flow) between basins takes place via these split locations. - Stage-discharge relations are generated to define flow-rates between basins. ### Dependencies @@ -21,7 +21,7 @@ Most important dependencies: We will make this package accessible via pypi. It is recommended to clone this repository because it is under development and it includes some example notebooks. We are still working on tests and test data, etc. ### Development, contributions and licences -This package is developed by Sweco (contributors: Harm Nomden and Tessa Andringa) when working on a TKI-project (top consortia for knowledge and innovation) within the NHI programme (Dutch Hydrological Instruments programme). This focuses on the development, testing and application of the new Ribasim-model (https://tkideltatechnologie.nl/project/oppervlaktewatermodule-nhi/). +This package is developed by Sweco (contributors: Harm Nomden and Tessa Andringa) when working on a TKI-project (top consortia for knowledge and innovation) within the NHI programme (Dutch Hydrological Instruments programme). This focuses on the development, testing and application of the new Ribasim-model (https://tkideltatechnologie.nl/project/oppervlaktewatermodule-nhi/). It is possible to contribute, create issues, start discussions. We will respond as soon as possible. This package is developed under the MIT license. Reference to this package: Ribasim-Lumping (Sweco, 2023).