From 7dfc2ec799c4feda91fca8603a203246d75c2968 Mon Sep 17 00:00:00 2001 From: Micah Sandusky <32111103+micah-prime@users.noreply.github.com> Date: Wed, 29 May 2024 14:42:05 -0600 Subject: [PATCH] Add tutorial for geosphere and supporting kml (#98) * Add tutorial for geosphere and supporting kml * Move tutorial to docs --- docs/gallery/data/geosphere_test.kml | 48 ++++++ docs/gallery/data/geosphere_tutorial.ipynb | 168 +++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 docs/gallery/data/geosphere_test.kml create mode 100644 docs/gallery/data/geosphere_tutorial.ipynb diff --git a/docs/gallery/data/geosphere_test.kml b/docs/gallery/data/geosphere_test.kml new file mode 100644 index 0000000..239a1b0 --- /dev/null +++ b/docs/gallery/data/geosphere_test.kml @@ -0,0 +1,48 @@ + + + + geosphere_test.kml + + + normal + #s_ylw-pushpin + + + highlight + #s_ylw-pushpin_hl + + + + + + geosphere_test + #m_ylw-pushpin + + 1 + + + + 13.30620324349142,47.53902871663209,0 13.01755054381177,46.92732016483155,0 14.11618099527536,46.75743049141452,0 14.36846777946924,47.37742007845974,0 13.30620324349142,47.53902871663209,0 + + + + + + + diff --git a/docs/gallery/data/geosphere_tutorial.ipynb b/docs/gallery/data/geosphere_tutorial.ipynb new file mode 100644 index 0000000..867042f --- /dev/null +++ b/docs/gallery/data/geosphere_tutorial.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "39a8c83c-4d90-498a-b1e0-078df578db42", + "metadata": {}, + "source": [ + "# Geosphere Austria tutorial\n", + "This tutorial walks through the use of a point data class for accessing station data in Austria\n", + "A video tutorial of this notebook can be found [here](https://www.loom.com/share/fcced7c00c47400e9a2b8b7b017949e5?sid=6a4cc255-48d0-48f0-8e14-a8aed0ef4bb3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da7e02c8-6932-4310-8108-7ad7f6bfbade", + "metadata": {}, + "outputs": [], + "source": [ + "# import metloom class https://data.hub.geosphere.at/dataset/\n", + "from metloom.pointdata import GeoSphereHistPointData" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee565b4d-e25d-4f43-8d85-f3b9fef5b31b", + "metadata": {}, + "outputs": [], + "source": [ + "# import packages\n", + "import geopandas as gpd\n", + "import pandas as pd\n", + "import fiona\n", + "import matplotlib.pyplot as plt\n", + "fiona.drvsupport.supported_drivers['kml'] = 'rw'\n", + "fiona.drvsupport.supported_drivers['KML'] = 'rw'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16111f97-d305-4358-9088-eccb5b18cf99", + "metadata": {}, + "outputs": [], + "source": [ + "# Install mapclassify and folium for gdf.explore()\n", + "!pip install mapclassify\n", + "!pip install folium" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31656640-c479-4a2a-aa11-2ada3b5d6194", + "metadata": {}, + "outputs": [], + "source": [ + "# read and explore shape\n", + "gdf = gpd.read_file(\"./data/geosphere_test.kml\")\n", + "gdf.explore()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2fe8a969-ff71-4beb-8259-556c66309f25", + "metadata": {}, + "outputs": [], + "source": [ + "# Define variable of interest https://github.com/M3Works/metloom/blob/main/metloom/variables.py\n", + "variable = GeoSphereHistPointData.ALLOWED_VARIABLES.TEMP\n", + "# Get the points within our region of interest\n", + "points = GeoSphereHistPointData.points_from_geometry(gdf, [variable])\n", + "print(len(points))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "028e0a56-aa66-43a7-b554-16dcfe2c35bc", + "metadata": {}, + "outputs": [], + "source": [ + "# Explore the points we located\n", + "points_df = points.to_dataframe()\n", + "points_df = points_df.set_crs(\"EPSG:4326\")\n", + "\n", + "points_df.explore()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb075ff5-50a5-4de2-8362-511c669e61b5", + "metadata": {}, + "outputs": [], + "source": [ + "# define start and end date of interest\n", + "start = pd.to_datetime(\"2023-01-01\")\n", + "end = pd.to_datetime(\"2023-01-11\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18544661-2f36-4097-b26f-2d6508acdc52", + "metadata": {}, + "outputs": [], + "source": [ + "# dataframe for storing combined data\n", + "final_df = pd.DataFrame()\n", + "\n", + "# loop through first 15 points and store the data\n", + "for p in points.points[:15]:\n", + " result = p.get_daily_data(start, end, [variable])\n", + " if result is None:\n", + " print(f\"{p.name} did not return data\")\n", + " else:\n", + " # reset the index to just be datetime\n", + " result = result.reset_index().set_index(\"datetime\")\n", + " # store off the data in the final dataframe\n", + " final_df[p.name + p.id] = result[variable.name]\n", + "final_df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55be4633-430d-46bb-8c8d-9792bca0c365", + "metadata": {}, + "outputs": [], + "source": [ + "# Plot the timeseries\n", + "final_df.plot(ylabel=variable.name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73cde7e4-2e99-4a3d-a19a-5a2b909737b1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}