{ "cells": [ { "cell_type": "markdown", "id": "1568be9e-b918-4a16-aa44-abb133fc6a79", "metadata": {}, "source": [ "# Downloading CDEC Snow Depth Data \n", "#### **Created by Hannah Besso**, July 2023 \n", "\n", "### This notebook does the following: \n", "* Load a region of interest\n", "* Make a geodataframe of the coordinates of all CDEC stations within the roi\n", "* Make a dataframe of snow depth data from a single CDEC station\n", "* Make a dataframe containing snow depth data from all CDEC stations within the roi\n", "* Convert from inches to meters and plot the timeseries of one station" ] }, { "cell_type": "markdown", "id": "ef80761e-20d7-4bf0-8096-265732ae6eda", "metadata": {}, "source": [ "### Import necessary packages" ] }, { "cell_type": "code", "execution_count": null, "id": "2df717cf-76db-4d7b-a3e2-ffd48f3e53d0", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import geopandas as gpd\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from datetime import datetime\n", "from shapely.geometry import Point\n", "import metloom\n", "from metloom.pointdata import CDECPointData\n", "from metloom.variables import CdecStationVariables" ] }, { "cell_type": "markdown", "id": "0351bb3b-3571-4dbc-87e1-96cf47519510", "metadata": {}, "source": [ "### Load USA states:" ] }, { "cell_type": "code", "execution_count": null, "id": "b6c699c8-2cfe-4f81-a9e0-7746495119bc", "metadata": {}, "outputs": [], "source": [ "states_url = 'http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_5m.json'\n", "states_gdf = gpd.read_file(states_url)\n", "california = states_gdf.loc[states_gdf.NAME == 'California']" ] }, { "cell_type": "code", "execution_count": null, "id": "0ef435ac-7b55-4452-b0db-6a98fd57fd35", "metadata": {}, "outputs": [], "source": [ "from shapely.geometry import Polygon\n", "\n", "coords = [(-120.03380085114448,38.2593960348446),\n", " (-120.03380085114448,36.934834847980355),\n", " (-118.1903872978364,36.934834847980355),\n", " (-118.1903872978364,38.2593960348446),\n", " (-120.03380085114448,38.2593960348446)]\n", "\n", "polygon = Polygon(coords)\n", "\n", "central_sierra = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon])" ] }, { "cell_type": "markdown", "id": "0326076d-c4c7-45ea-9617-d1b46e002fcb", "metadata": {}, "source": [ "### Access a geodataframe of CDEC station locations: \n", "Using the California polygon to get a list of all stations, excluding snow courses" ] }, { "cell_type": "code", "execution_count": null, "id": "3111911f-8a84-40fd-8666-4bb08b25eb59", "metadata": {}, "outputs": [], "source": [ "vrs = [\n", "# CdecStationVariables.SWE # I want snow depth but SWE is also available\n", " CdecStationVariables.SNOWDEPTH\n", "]\n", "points = CDECPointData.points_from_geometry(central_sierra, vrs, snow_courses=False)\n", "cdec_locations = points.to_dataframe()\n", "cdec_locations.set_crs('epsg:4326')\n", "cdec_locations.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "a4ddf07a-3ca5-44d7-8068-e91dc0756017", "metadata": { "tags": [ "nbsphinx-thumbnail", "nbsphinx-gallery" ] }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "california.plot(facecolor='none', edgecolor='k', ax=ax)\n", "central_sierra.plot(facecolor='none', edgecolor='orange', ax=ax)\n", "cdec_locations.plot(ax=ax);" ] }, { "cell_type": "markdown", "id": "cb5047e0-b8bd-464d-8b9f-6e0ff702b0f9", "metadata": {}, "source": [ "### Download snow depth data for a single CDEC station:" ] }, { "cell_type": "code", "execution_count": null, "id": "826d1e37-3637-42a9-b687-c63be11461e5", "metadata": {}, "outputs": [], "source": [ "start_date = datetime(2020,10,1)\n", "end_date = datetime(2023,7,31)" ] }, { "cell_type": "code", "execution_count": null, "id": "9e6a664b-7418-4bee-a902-2dd28571368a", "metadata": {}, "outputs": [], "source": [ "# Download data for TUM to get the datetime index:\n", "cdec_point = CDECPointData(\"TUM\", \"TUM\")\n", "tum_depth = cdec_point.get_daily_data(\n", " start_date, end_date,\n", " [cdec_point.ALLOWED_VARIABLES.SNOWDEPTH]\n", ")\n", "tum_depth.head()" ] }, { "cell_type": "markdown", "id": "9a77304c-72d1-48a1-a74f-61dc249b9f53", "metadata": {}, "source": [ "### Create a dataframe of snow depth data from every CDEC station within the ROI:" ] }, { "cell_type": "code", "execution_count": null, "id": "561092b6-f3e8-49be-a0f1-3f76e25f33c0", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Reindex from a multiindex so I can easily access the datetime index:\n", "tum_depth = tum_depth.reset_index()\n", "tum_depth = tum_depth.set_index('datetime')\n", "\n", "# Create a new empty dataframe with the TUM datetime index:\n", "snwd_df = pd.DataFrame()\n", "snwd_df.index = pd.DatetimeIndex(data=tum_depth.index)\n", "\n", "# Iteratively download all the data and add the SNOWDEPTH column to the snwd_df:\n", "# Note that the snow depth unit is inches\n", "for i in range(len(cdec_locations)):\n", " cdec_point = CDECPointData(cdec_locations.id.values[i], cdec_locations.id.values[i])\n", " cdec_depth = cdec_point.get_daily_data(\n", " start_date, end_date,\n", " [cdec_point.ALLOWED_VARIABLES.SNOWDEPTH]\n", " )\n", " if cdec_depth is not None:\n", " cdec_depth = cdec_depth.reset_index()\n", " cdec_depth = cdec_depth.set_index('datetime')\n", " snwd_df[f'{cdec_locations.id.values[i]}'] = cdec_depth['SNOWDEPTH']\n", " else:\n", " continue" ] }, { "cell_type": "code", "execution_count": null, "id": "530c20d0-5061-4269-9ef8-73fdd3a9324c", "metadata": {}, "outputs": [], "source": [ "snwd_df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "572aea9a-2938-4070-8ffe-af3f3b04479b", "metadata": {}, "outputs": [], "source": [ "# convert from inches to meters\n", "snwd_df_m = snwd_df * 0.0254" ] }, { "cell_type": "code", "execution_count": null, "id": "d58092bb-0852-4e94-ae42-1e9f5abec3a2", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "snwd_df_m['DAN'].plot(ax=ax)\n", "ax.set_ylabel('Snow Depth (m)')" ] }, { "cell_type": "code", "execution_count": null, "id": "caa66833-1a86-440a-b679-c88ad759856b", "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }