{ "cells": [ { "cell_type": "markdown", "id": "5bbd0865", "metadata": {}, "source": [ "# Access the Timeseries Data\n", "## GIVE ME DATA\n", "\n", "You might be saying \"Okay, that's a lot of code and links Micah, but I just wanted the data\"\n", "\n", "Well have I got a notebook for you!\n", "\n", "The whole point of wading through this layer of code, is that now you don't have to worry about how the\n", "underlying APIs work, you can just ask for the data" ] }, { "cell_type": "code", "execution_count": null, "id": "f8319b6d", "metadata": {}, "outputs": [], "source": [ "# we need imports\n", "from datetime import datetime\n", "import pandas as pd\n", "\n", "from metloom.pointdata import SnotelPointData, CDECPointData" ] }, { "cell_type": "code", "execution_count": null, "id": "a9911803", "metadata": {}, "outputs": [], "source": [ "# Let's look at our old friend banner summit\n", "pt = SnotelPointData(\"312:ID:SNTL\", \"Banner Summit\")\n", "\n", "# start data and end date\n", "start_date = datetime(2017, 3, 1)\n", "end_date = datetime(2017, 4, 1)\n", "# Notice this is a list\n", "variables = [pt.ALLOWED_VARIABLES.SWE]\n", "\n", "# request the data\n", "df = pt.get_daily_data(start_date, end_date, variables)\n", "df.head(10)" ] }, { "cell_type": "code", "execution_count": null, "id": "68ccbe7e", "metadata": { "tags": [ "nbsphinx-thumbnail", "nbsphinx-gallery" ] }, "outputs": [], "source": [ "## Let's look at the data\n", "df.reset_index().set_index(\"datetime\")[\"SWE\"].plot()" ] }, { "cell_type": "markdown", "id": "b2d2d80d", "metadata": {}, "source": [ "**Wait what just happened with that index thing?**\n", "\n", "The `get_<>_data` methods return a pandas dataframe with a multi-level index on **datetime** and **site**\n", "This makes it easy to merge data from multiple stations together, but it is important to remember when plotting and exporting" ] }, { "cell_type": "code", "execution_count": null, "id": "f9c91bbe", "metadata": {}, "outputs": [], "source": [ "print(df.index.levels[0])\n", "print(df.index.levels[1])" ] }, { "cell_type": "markdown", "id": "95c651e1", "metadata": {}, "source": [ "## Comprehension check\n", "What if I wanted to get accumulated precip and SWE from the same station in one request?" ] }, { "cell_type": "code", "execution_count": null, "id": "5bc88b9c", "metadata": {}, "outputs": [], "source": [ "# Your code here\n", "df_multiple = None" ] }, { "cell_type": "code", "execution_count": null, "id": "bbd1a338", "metadata": {}, "outputs": [], "source": [ "# Merge two dataframes\n", "\n", "# Get data for mammoth pass using CDEC\n", "mhp = CDECPointData(\"MHP\", \"Mammoth Pass\")\n", "variables = [mhp.ALLOWED_VARIABLES.SWE]\n", "df2 = mhp.get_daily_data(start_date, end_date, variables)\n", "\n", "# merge the data with the banner summit response\n", "df_merged = pd.concat([df, df2])\n", "\n", "# Notice 2 entries for each date, one for each station.\n", "# Also notice times are slightly different because we converted\n", "# From 2 timezones to UTC\n", "df_merged.sort_index(level=0).head(10)" ] }, { "cell_type": "code", "execution_count": null, "id": "7b63f63f", "metadata": {}, "outputs": [], "source": [ "# plot both\n", "ax = df.reset_index().set_index(\"datetime\")[\"SWE\"].plot()\n", "df2.reset_index().set_index(\"datetime\")[\"SWE\"].plot(ax=ax)" ] }, { "cell_type": "markdown", "id": "32216667", "metadata": {}, "source": [ "In the next section, we will explore how you can find the points you're interested in" ] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }