{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Reading Matroos observations\n", "\n", "This notebook introduces how to use the `hydropandas` package to read, process and visualise data obtained using the Matroos API." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import contextily as ctx\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import hydropandas as hpd\n", "from hydropandas.io.matroos import load_parameter_metadata, select_parameters\n", "\n", "# enabling debug logging so we can see what happens in the background\n", "hpd.util.get_color_logger(\"INFO\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get data from a certain location and grootheid\n", "tmin = pd.Timestamp(\"2020-1-1\")\n", "tmax = pd.Timestamp(\"2020-1-3\")\n", "o1 = hpd.WaterlvlObs.from_matroos(\n", " location=\"schoonhoven\", unit=\"waterlevel\", source=\"observed\", tmin=tmin, tmax=tmax\n", ")\n", "o1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extent = [100_500, 120_000, 430_000, 457_000] # Schoonhoven\n", "oc = hpd.read_matroos(\n", " extent=extent, units=\"waterlevel\", sources=\"observed\", keep_all_obs=False\n", ")\n", "oc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "oc.plots.interactive_map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Find selection criteria\n", "\n", "You may not know which location, unit and source keywords you have to use. The code below will give you some guidance on how to find them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# find all units and sources for schoonhoven\n", "selected = select_parameters(\n", " locations=\"schoonhoven\", astype=\"dataframe\", keep_coords=False\n", ")\n", "print(\"All units and sources available for Schoonhoven:\")\n", "display(selected)\n", "print(\"\\n\")\n", "\n", "# find all sources for multiple locations and unit waterlevel\n", "selected = select_parameters(\n", " locations=[\"schoonhoven\", \"Nieuwpoort\"],\n", " units=\"waterlevel\",\n", " astype=\"dataframe\",\n", " keep_coords=False,\n", ")\n", "print(\"Units and source for Schoonhoven and Nieuwpoort:\")\n", "display(selected)\n", "print(\"\\n\")\n", "\n", "# find all locations for observed waterlevels\n", "selected = select_parameters(\n", " units=\"waterlevel\", sources=\"observed\", astype=\"dataframe\", keep_coords=False\n", ")\n", "print(\"Locations for observed waterlevels:\")\n", "display(selected)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "params_dic = load_parameter_metadata()\n", "\n", "# print all locations\n", "all_locations = list(params_dic.keys())\n", "print(\"all locations:\")\n", "print(all_locations, \"\\n\")\n", "\n", "# print all units\n", "all_units = np.unique(\n", " [unit for loc in params_dic.values() for unit in loc[\"units\"].keys()]\n", ")\n", "print(\"all units:\")\n", "print(all_units, \"\\n\")\n", "\n", "# print all sources\n", "all_sources = np.unique(\n", " [s for loc in params_dic.values() for unit in loc[\"units\"].values() for s in unit]\n", ")\n", "print(\"all sources:\")\n", "print(all_sources, \"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot all locations with waterlevel measurements\n", "units = \"waterlevel\"\n", "sources = \"observed\"\n", "\n", "# get all locations with waterlevel measurements\n", "selected = select_parameters(units=units, sources=sources, astype=\"geodataframe\")\n", "selected2 = select_parameters(units=units, sources=None, astype=\"geodataframe\")\n", "selected_other_sources = selected2.loc[~selected2.index.isin(selected.index)]\n", "\n", "# plot locations\n", "f, ax = plt.subplots(figsize=(10, 10))\n", "selected_other_sources.plot(ax=ax, color=\"orange\", marker=\"x\", label=\"other sources\")\n", "selected.plot(ax=ax, label=f\"source={sources}\")\n", "ax.set_title(f\"locations with {units} measurements\")\n", "ax.legend()\n", "ctx.add_basemap(ax=ax, crs=28992, alpha=0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "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.12.10" } }, "nbformat": 4, "nbformat_minor": 4 }