{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 03_Xarray: Overlays of variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "1. Work with multiple Xarray `Dataset`s\n", "2. Subset the Dataset along its dimensions\n", "3. Perform unit conversions\n", "4. Create a well-labeled multi-parameter contour plot of gridded ERA5 data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import pandas as pd\n", "import numpy as np\n", "from datetime import datetime as dt\n", "from metpy.units import units\n", "import metpy.calc as mpcalc\n", "import cartopy.crs as ccrs\n", "import cartopy.feature as cfeature\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Work with an Xarray `Dataset`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset('/spare11/atm350/data/199303_era5.nc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Examine the dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Subset the Datasets along their dimensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We noticed in the previous notebook that our contour labels were not appearing with every contour line. This is because we passed the entire horizontal extent (all latitudes and longitudes) to the `ax.contour` method. Since our intent is to plot only over a regional subset, we will use the `sel` method on the latitude and longitude dimensions as well as time and isobaric surface.\n", "\n", "We'll retrieve two data variables, geopotential and sea-level pressure, from the Dataset.\n", "\n", "We'll also use Datetime and string methods to more dynamically assign various dimensional specifications, as well as aid in figure-labeling later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Areal extent\n", "\n", "lonW = -100\n", "lonE = -60\n", "latS = 25\n", "latN = 55\n", "cLat, cLon = (latS + latN)/2, (lonW + lonE)/2\n", "\n", "# Recall that in ERA5, longitudes run between 0 and 360, not -180 and 180\n", "if (lonW < 0 ):\n", " lonW = lonW + 360\n", "if (lonE < 0 ):\n", " lonE = lonE + 360\n", " \n", "expand = 1\n", "latRange = np.arange(latS - expand,latN + expand,.5) # expand the data range a bit beyond the plot range\n", "lonRange = np.arange((lonW - expand),(lonE + expand),.5) # Need to match longitude values to those of the coordinate variable\n", "\n", "# Vertical level specificaton\n", "plevel = 500\n", "levelStr = str(plevel)\n", "\n", "# Date/Time specification\n", "Year = 1993\n", "Month = 3\n", "Day = 14\n", "Hour = 12\n", "Minute = 0\n", "dateTime = dt(Year,Month,Day, Hour, Minute)\n", "timeStr = dateTime.strftime(\"%Y-%m-%d %H%M UTC\")\n", "\n", "# Data variable selection\n", "\n", "z = ds['geopotential'].sel(time=dateTime,level=plevel,latitude=latRange,longitude=lonRange)\n", "slp = ds['mean_sea_level_pressure'].sel(time=dateTime,latitude=latRange,longitude=lonRange) # of course, no isobaric surface for SLP" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "levelStr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "timeStr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at some of the attributes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z.dims" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a result of selecting just a single time and isobaric surface, both of those dimensions have been abstracted out of the DataArray." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z.units" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slp.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slp.dims" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slp.units" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Define our subsetted coordinate arrays of lat and lon. Pull them from either of the two DataArrays. We'll need to pass these into the contouring functions later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lats = z.latitude\n", "lons = z.longitude" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Perform unit conversions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert geopotential to geopotential height in decameters, and Pascals to hectoPascals." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We take the DataArrays and apply [MetPy's unit conversion method](https://unidata.github.io/MetPy/latest/tutorials/xarray_tutorial.html#units)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slp = slp.metpy.convert_units('hPa')\n", "z = mpcalc.geopotential_to_height(z).metpy.convert_units('dam')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "