|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Import temperature data from the DWD and process it\n", |
| 8 | + "\n", |
| 9 | + "This notebook pulls historical temperature data from the DWD server and formats it for future use in other projects. The data is delivered in a hourly frequencs in a .zip file for each of the available weather stations. To use the data, we need everythin in a single .csv-file, all stations side-by-side. Also, we need the daily average.\n", |
| 10 | + "\n", |
| 11 | + "To reduce computing time, we also crop all data earlier than 2007. \n", |
| 12 | + "\n", |
| 13 | + "Files should be executed in the following pipeline:\n", |
| 14 | + "* 1-dwd_konverter_download\n", |
| 15 | + "* 2-dwd_konverter_extract\n", |
| 16 | + "* 3-dwd_konverter_build_df\n", |
| 17 | + "* 4-dwd_konverter_final_processing" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## 1.) Download files from the DWD-API\n", |
| 25 | + "Here we download all relevant files from the DWS Server. The DWD Server is http-based, so we scrape the download page for all links that match 'stundenwerte_TU_.\\*_hist.zip' and download them to the folder 'download'. \n", |
| 26 | + "\n", |
| 27 | + "Link to the relevant DWD-page: https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/hourly/air_temperature/historical/" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 1, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "name": "stdout", |
| 37 | + "output_type": "stream", |
| 38 | + "text": [ |
| 39 | + "Done\n" |
| 40 | + ] |
| 41 | + } |
| 42 | + ], |
| 43 | + "source": [ |
| 44 | + "import requests\n", |
| 45 | + "import re\n", |
| 46 | + "from bs4 import BeautifulSoup\n", |
| 47 | + "from pathlib import Path\n", |
| 48 | + "\n", |
| 49 | + "# Set base values\n", |
| 50 | + "download_folder = Path.cwd() / 'download'\n", |
| 51 | + "base_url = 'https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/hourly/air_temperature/historical/'\n", |
| 52 | + "\n", |
| 53 | + "\n", |
| 54 | + "# Initiate Session and get the Index-Page\n", |
| 55 | + "with requests.Session() as s:\n", |
| 56 | + " resp = s.get(base_url)\n", |
| 57 | + "\n", |
| 58 | + "# Parse the Index-Page for all relevant <a href> \n", |
| 59 | + "soup = BeautifulSoup(resp.content)\n", |
| 60 | + "links = soup.findAll(\"a\", href=re.compile(\"stundenwerte_TU_.*_hist.zip\"))\n", |
| 61 | + "\n", |
| 62 | + "# For testing, only download 10 files\n", |
| 63 | + "file_max = 10\n", |
| 64 | + "dl_count = 0\n", |
| 65 | + "\n", |
| 66 | + "#Download the .zip files to the download_folder\n", |
| 67 | + "for link in links:\n", |
| 68 | + " zip_response = requests.get(base_url + link['href'], stream=True)\n", |
| 69 | + " # Limit the downloads while testing\n", |
| 70 | + " dl_count += 1\n", |
| 71 | + " if dl_count > file_max:\n", |
| 72 | + " break\n", |
| 73 | + " with open(Path(download_folder) / link['href'], 'wb') as file:\n", |
| 74 | + " for chunk in zip_response.iter_content(chunk_size=128):\n", |
| 75 | + " file.write(chunk) \n", |
| 76 | + " \n", |
| 77 | + "print('Done')" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [] |
| 86 | + } |
| 87 | + ], |
| 88 | + "metadata": { |
| 89 | + "kernelspec": { |
| 90 | + "display_name": "Python 3", |
| 91 | + "language": "python", |
| 92 | + "name": "python3" |
| 93 | + }, |
| 94 | + "language_info": { |
| 95 | + "codemirror_mode": { |
| 96 | + "name": "ipython", |
| 97 | + "version": 3 |
| 98 | + }, |
| 99 | + "file_extension": ".py", |
| 100 | + "mimetype": "text/x-python", |
| 101 | + "name": "python", |
| 102 | + "nbconvert_exporter": "python", |
| 103 | + "pygments_lexer": "ipython3", |
| 104 | + "version": "3.8.5" |
| 105 | + } |
| 106 | + }, |
| 107 | + "nbformat": 4, |
| 108 | + "nbformat_minor": 4 |
| 109 | +} |
0 commit comments