{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "
\n", "\"Unidata\n", "
\n", "\n", "

MetPy Basics

\n", "

Unidata AMS 2021 Student Conference

\n", "\n", "
\n", "
\n", "\n", "---\n", "\n", "This notebook will introduce the use of [MetPy](https://unidata.github.io/MetPy/latest/index.html) for meteorological\n", "calculations. MetPy is an open-source python package designed to provide support for flexible calculations of various\n", "meteorological quantities in support of educational, operational, and research needs. This notebook will introduce some basic\n", "computations and provide an overview of some available computations within the MetPy package.\n", "\n", "\n", "\n", "### Focuses\n", "* Introduce the use of the MetPy package for meteorological calculations\n", "* Demonstrate simple meteorological calculations with MetPy\n", "\n", "\n", "### Objectives\n", "1. [Basic Calculations](#1.-Basic-Calculations)\n", "1. [Thermodynamic Calculations](#2.-Thermodynamic-Calculations)\n", "1. [Other Calculations](#3.-Other-Calculations)\n", "1. [See also](#4.-See-also)\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import metpy.calc\n", "from metpy.units import units\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Basic Calculations\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's first start with some simple calculations. To do this, we will need some data to work with. Let's pretend we have a timeseries of surface observations consisting of wind components, temperature, dewpoint temperature, and pressure." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperature = (np.random.randn(100) + 1 * 68) * units('degF')\n", "dewpoint = (np.random.randn(100) + 1 * 10) * units('degC')\n", "uwind = (np.random.randn(100) + 1 * 20) * units('kt')\n", "vwind = (np.random.randn(100) + 1 * 5) * units('m/s')\n", "pressure = (np.random.randn(100) + 1 * 1000) * units('hPa')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have some data we can start making some calculations. Notice that the data units are all different. Ordinarily this would require units conversions before any calculations. However, MetPy will handle those for us, meaning that as long as the units are defined, we can simply tell MetPy what units we want. Let's start with determining the wind speed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ws = metpy.calc.wind_speed(uwind, vwind).to('m/s')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it! We now have a timeseries of wind speed with units of m/s. Let's see what it looks like." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(ws)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also calculate wind direction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wd = metpy.calc.wind_direction(uwind, vwind)\n", "plt.plot(wd)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we want a plot of relative humidity? This can be easily done as well." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rh = metpy.calc.relative_humidity_from_dewpoint(temperature, dewpoint)\n", "plt.plot(rh*100)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Top\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Thermodynamic Calculations\n", "\n", "Now that we have introduced some basic calculations, let's try some more complicated thermodynamic calculations. First, let's calculate the potential temperature and equivalent potential temperature" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "theta = metpy.calc.potential_temperature(pressure, temperature)\n", "plt.plot(theta)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "theta_e = metpy.calc.equivalent_potential_temperature(pressure, temperature, dewpoint)\n", "plt.plot(theta_e)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also calculate the saturation values for vairous moisture variables, such as saturation vapor pressure." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "es = metpy.calc.saturation_vapor_pressure(temperature)\n", "plt.plot(es)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Top\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Other Calculations\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "MetPy also has support for computation of common indicies and standard atmosphere values. Let's calculate the standard atmosphere height based on the pressure." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "height = metpy.calc.pressure_to_height_std(pressure)\n", "plt.plot(height.to('meter'))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Top\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. See also\n", "\n", "There are many other calculations available within the MetPy package. Be sure to check out the [docs](https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.html#module-metpy.calc) for a complete listing and see the other notebooks within this training for detailed examples.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Top\n", "\n", "---" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:pyaos-ams-2021]", "language": "python", "name": "conda-env-pyaos-ams-2021-py" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 4 }