{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Xarray 3: Datasets and Plotting\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"1. The Xarray `Dataset`\n",
"1. Open and select a variable from the ERA-5 dataset\n",
"1. Convert geopotential to height\n",
"1. Create a contour plot\n",
"\n",
"## Prerequisites\n",
"\n",
"| Concepts | Importance | Notes |\n",
"| --- | --- | --- |\n",
"| Xarray Lessons 1-2| Necessary | |\n",
"\n",
"* **Time to learn**: 15 minutes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import numpy as np\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": "markdown",
"metadata": {},
"source": [
"An Xarray [Dataset](http://xarray.pydata.org/en/stable/generated/xarray.Dataset.html) consists of one or more `DataArray`s. Let's open the same NetCDF file as in the first Xarray notebook, but as a `Dataset`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = xr.open_dataset('/spare11/atm533/data/2012103000_z500_era5.nc')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see what this Dataset looks like."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's represented somewhat similarly to a DataArray, but now we have support for multiple data variables. In this case,\n",
"1. There is just one data variable: z (Geopotential)\n",
"2. The coordinate variables are longitude, latitude, and time\n",
"3. The data variable has three dimensions (time x latitude x longitude). \n",
"4. Although time is a dimension, there is only one time value on that coordinate.\n",
"5. The Dataset itself has attributes. The attributes of the coordinate and data variables are separately accessible."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Select a data variable from the Dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Z = ds['z'] # Similar to selecting a Series in Pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can programmatically retrieve various attributes of the data variable that we browsed interactively above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Z"
]
},
{
"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": [
"We see that Z is a 3-dimensional DataArray, with time as the first dimension."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Z.units"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Convert geopotential to height"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Print out the representation of our data variable. We can see it's a `DataArray`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print (Z) # Text-based, as opposed to HTML-prettified output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The units are already part of the data variable. We can use `MetPy`'s `units` and `calc` libraries to [convert geopotential to height](https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.geopotential_to_height.html). We'll assign a new object to the output of the function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"HGHT = mpcalc.geopotential_to_height(Z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Print out its representation. Note that the array values have changed, and there is a unit attached to the `DataArray`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print (HGHT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
TypeError: Input z must be 2D, not 3D
\n",
"