From 3f85659c27be92bd7ee13ef96c44a2704250be9d Mon Sep 17 00:00:00 2001 From: Zach Bruick Date: Mon, 23 Sep 2019 16:31:00 -0600 Subject: [PATCH] Add an example of ERA5 and GRIB data & visualization to the gallery (#3199) * Adds an example of ERA5 and GRIB data to the gallery * Add markdown narrative cells to GRIB example * Update load method to use xr.tutorial * Fix load method --- ci/requirements/doc.yml | 1 + doc/examples/ERA5-GRIB-example.ipynb | 121 +++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 doc/examples/ERA5-GRIB-example.ipynb diff --git a/ci/requirements/doc.yml b/ci/requirements/doc.yml index e521ee4a4b8..39eee89a4ab 100644 --- a/ci/requirements/doc.yml +++ b/ci/requirements/doc.yml @@ -6,6 +6,7 @@ dependencies: - python=3.7 - bottleneck - cartopy + - cfgrib - h5netcdf - ipython - iris diff --git a/doc/examples/ERA5-GRIB-example.ipynb b/doc/examples/ERA5-GRIB-example.ipynb new file mode 100644 index 00000000000..b82a07a64e6 --- /dev/null +++ b/doc/examples/ERA5-GRIB-example.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# GRIB Data Example " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "GRIB format is commonly used to disemminate atmospheric model data. With Xarray and the cfgrib engine, GRIB data can easily be analyzed and visualized." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import xarray as xr\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To read GRIB data, you can use `xarray.load_dataset`. The only extra code you need is to specify the engine as `cfgrib`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds = xr.tutorial.load_dataset('era5-2mt-2019-03-uk.grib', engine='cfgrib')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's create a simple plot of 2-m air temperature in degrees Celsius:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds = ds - 273.15\n", + "ds.t2m[0].plot(cmap=plt.cm.coolwarm)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With CartoPy, we can create a more detailed plot, using built-in shapefiles to help provide geographic context:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import cartopy.crs as ccrs\n", + "import cartopy\n", + "fig = plt.figure(figsize=(10,10))\n", + "ax = plt.axes(projection=ccrs.Robinson())\n", + "ax.coastlines(resolution='10m')\n", + "plot = ds.t2m[0].plot(cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree(), cbar_kwargs={'shrink':0.6})\n", + "plt.title('ERA5 - 2m temperature British Isles March 2019')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, we can also pull out a time series for a given location easily:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds.t2m.sel(longitude=0,latitude=51.5).plot()\n", + "plt.title('ERA5 - London 2m temperature March 2019')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}