{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } }, "source": [ "# wetterdienst - A basic notebook example\n", "\n", "pip install wetterdienst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import modules necessary for general functioning" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "from wetterdienst.provider.dwd.observation import DwdObservationRequest, \\\n", " DwdObservationPeriod, DwdObservationResolution, DwdObservationParameter, DwdObservationDataset\n", "\n", "%matplotlib inline\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "from matplotlib import cm" ] }, { "cell_type": "markdown", "source": [ "Which parameters are available?" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "# all\n", "print(\"All available parameters\")\n", "print(\n", " DwdObservationRequest.discover()\n", ")\n", "# selection\n", "print(\"Selection of daily data\")\n", "print(\n", " DwdObservationRequest.discover(\n", " filter_=DwdObservationResolution.DAILY\n", " )\n", ")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. First check the metadata to inform yourself of available stations\n", "(here we pick historical daily precipitation - hdp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "request = DwdObservationRequest(\n", " parameter=DwdObservationDataset.PRECIPITATION_MORE,\n", " resolution=DwdObservationResolution.DAILY,\n", " period=DwdObservationPeriod.HISTORICAL\n", ")\n", "print(\"Number of stations with available data: \", request.all().df.sum())\n", "print(\"Some of the stations:\")\n", "request.all().df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The metadata includes an id, the range of the measurements, the position\n", "(including height) as well as place and state of it and if it has a file. With the\n", "following plot we want to show a map of those stations:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "cmap = cm.get_cmap('viridis')\n", "bounds = request.all().df.height.quantile([0, 0.25, 0.5, 0.75, 1]).values\n", "norm = mpl.colors.BoundaryNorm(bounds, cmap.N)\n", "\n", "fig, ax = plt.subplots(figsize=(10, 10))\n", "plot = request.all().df.plot.scatter(\n", " x=\"longitude\", y=\"latitude\", c=\"height\", cmap=cmap, norm=norm, ax=ax)\n", "plot.set_title(\"Map of daily precipitation stations in Germany\\n\"\n", " \"Color refers to height of station\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. The usual way of retrieving data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get some data for the above request" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "print(\"Receiving historical daily climate data for Dresden-Klotzsche (1048)\")\n", "station_data = request.filter_by_station_id(station_id=[1048]).values.all().df\n", "\n", "station_data.dropna(axis=0).head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See that DATE is already parsed, so we can easily get some nice graphs with matplotlib,\n", "which we will do in the next part. We can also request direct parameters from the given\n", "parameter sets. We know that CLIMATE_SUMMARY contains TMK, TNK, TXK and RSK, so let's\n", "request those. This option will automatically tidy the data." ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "print(\"Receiving historical daily temperature and precipitation for Dresden-Klotzsche \"\n", " \"(1048).\")\n", "\n", "request = DwdObservationRequest(\n", " parameter=[\n", " DwdObservationParameter.DAILY.TEMPERATURE_AIR_200,\n", " DwdObservationParameter.DAILY.TEMPERATURE_AIR_MAX_200,\n", " DwdObservationParameter.DAILY.TEMPERATURE_AIR_MIN_200,\n", " DwdObservationParameter.DAILY.PRECIPITATION_HEIGHT\n", " ],\n", " resolution=DwdObservationResolution.DAILY,\n", " period=DwdObservationPeriod.HISTORICAL\n", ").filter_by_station_id(station_id=(1048, ))\n", "\n", "station_data = request.values.all().df\n", "\n", "station_data.dropna(axis=0).head()" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Let's create some plots\n", "\n", "Now that we have data, let's create some plots! We can create a time series/histogram of\n", "the temperatures and precipitation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "cmap = plt.get_cmap('viridis', 4)\n", "colors = cmap.colors\n", "\n", "PARAMETERS = [\"tnk\", \"tmk\", \"txk\", \"rsk\"]\n", "\n", "station_data_grouped = station_data.groupby(station_data[\"parameter\"], observed=True)\n", "\n", "fig, axes = plt.subplots(nrows=len(PARAMETERS), tight_layout=True, figsize=(10, 40))\n", "\n", "for (parameter, group), ax, color in zip(station_data_grouped, axes, colors):\n", " group.plot(x=\"date\", y=\"value\", label=parameter, alpha=.75, ax=ax, c=color)\n", "\n", "plt.tight_layout()\n", "plt.subplots_adjust(top=0.9)\n", "plt.suptitle(\"Temperature and precipitation time series of Dresden, Germany\")\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Create yearly values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "station_data_yearly = []\n", "\n", "for (year, parameter), group in station_data.groupby(\n", " [station_data[\"date\"].dt.year, \"parameter\"], as_index=False, observed=True):\n", " if parameter == \"rsk\":\n", " station_data_yearly.append(group.dropna().agg({\"value\": np.sum}))\n", " else:\n", " station_data_yearly.append(group.dropna().agg({\"value\": np.mean}))\n", "\n", "station_data_yearly = pd.concat(station_data_yearly)\n", "\n", "station_data_yearly" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } }, "source": [ "## 5. Find a station\n", "\n", "To find a station near to a certain area, call ``filter_by_rank()``." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "DwdObservationRequest(\n", " parameter=DwdObservationDataset.CLIMATE_SUMMARY,\n", " resolution=DwdObservationResolution.DAILY,\n", " period=DwdObservationPeriod.HISTORICAL,\n", " start_date=\"2000-01-01\",\n", " end_date=\"2010-01-01\"\n", ").filter_by_rank(\n", " 51.05089,\n", " 13.73832,\n", " 5\n", ").df" ] } ], "metadata": { "kernel_info": { "name": "python3" }, "kernelspec": { "display_name": "Python 3", "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.8.5" }, "nteract": { "version": "0.15.0" } }, "nbformat": 4, "nbformat_minor": 4 }