{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NYSM_TimeSeries.ipynb\n", "### This notebook generates time series of recent meteorological data for various [New York State Mesonet (NYSM)](https://nysmesonet.org) sites." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
TRY THIS: Edit the next Markdown cell with your first and last name.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Author: firstName lastName" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a Pandas DataFrame to read in the file containing the past hour's worth of NYSM data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First define the format and then define the lambda function\n", "timeFormat = \"%Y-%m-%d %H:%M:%S UTC\"\n", "# This function will iterate over each string in a 1-d array\n", "# and use Pandas' implementation of strptime to convert the string into a datetime object.\n", "parseTime = lambda x: datetime.strptime(x, timeFormat)\n", "df = pd.read_csv('/data1/nysm/latest.csv',parse_dates=['time'], date_parser=parseTime).set_index('time') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inspect the `Dataframe`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specify a particular NYSM site ID, and then create a `Dataframe` specific to that site." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
TRY THIS: Edit the next cell with your own choice of NYSM site.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Change the station ID to any one of the 126 NYSM stations.\n", "stnID = 'VOOR'\n", "dfStn = df.query(' station == @stnID ')" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Inspect this `Dataframe`, now consisting only of the station that was specified in the above." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "dfStn" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Output the names of all the variables (i.e., *columns*) in the `Dataframe`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "dfStn.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a nested set of `for` loops that plots time series traces for several variables at several sites." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
TRY THIS: Edit the next two cells with your own choice of NYSM sites and variables.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Set your own list of variarbles here\n", "varList = ['temp_2m [degC]', 'temp_9m [degC]', 'relative_humidity [percent]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Set your own list of sites here\n", "sites = ['SCHO','QUEE','CHAZ']" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Invoke the `seaborn` library to make our time series plots more readable." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "sns.set()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
NOTE: To reset the plots to their default look, uncomment and run the following cell. Note how the seaborn-enabled plots look nicer.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "#sns.reset_orig()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a nested `for` loop over the sites and variables, and make time series plots." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "for site in sites:\n", " for var in varList:\n", " dfStn = df.query(' station == @site ')\n", " titleStr = \"Past hour time series of \" + var + \" for site \" + site\n", " dfStn[var].plot(title=titleStr)\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Before you leave ... :\n", "
    \n", "
  1. Save the notebook (File-->Save Notebook)
  2. \n", "
  3. Shutdown the notebook (File-->Close and Shutdown Notebook)
  4. \n", "
\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 August 2022 Environment", "language": "python", "name": "aug22" }, "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }