diff --git a/06_metrics.ipynb b/06_metrics.ipynb new file mode 100644 index 0000000..de5d135 --- /dev/null +++ b/06_metrics.ipynb @@ -0,0 +1,2399 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CMIP6 Earth System Model example\n", + "\n", + "Grid-aware operations such as average, integrate and cumulative integration rely on user-provided [grid metrics](https://xgcm.readthedocs.io/en/latest/grid_metrics.html). This notebook demonstrates the methods `interp_like()`, `get_metric()`, and `set_metrics()` which makes working with metrics easier, better, faster, and shorter when processing ocean models such as CMIP6 Earth System Models. The main objective of this notebook is to calculate and plot the time series of area-averaged temperature and zonal velocity from an ESM." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setting up the sample dataset" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First, we import the packages we need for this example. To run a piece of code, click on the cell and hit `Shift` + `Enter`." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import intake\n", + "import matplotlib as plt\n", + "import xarray as xr\n", + "import numpy as np\n", + "import cftime\n", + "import dask" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting git+https://github.com/xgcm/xgcm.git\n", + " Cloning https://github.com/xgcm/xgcm.git to /tmp/pip-req-build-owswiym0\n", + " Running command git clone -q https://github.com/xgcm/xgcm.git /tmp/pip-req-build-owswiym0\n", + " Running command git submodule update --init --recursive -q\n", + "Requirement already satisfied: xarray>=0.14.1 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xgcm==0.5.3.dev24+g6e68f71) (0.16.2)\n", + "Requirement already satisfied: dask in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xgcm==0.5.3.dev24+g6e68f71) (2021.1.1)\n", + "Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xgcm==0.5.3.dev24+g6e68f71) (1.20.0)\n", + "Requirement already satisfied: future in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xgcm==0.5.3.dev24+g6e68f71) (0.18.2)\n", + "Requirement already satisfied: docrep<=0.2.7 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xgcm==0.5.3.dev24+g6e68f71) (0.2.7)\n", + "Requirement already satisfied: six in /srv/conda/envs/notebook/lib/python3.8/site-packages (from docrep<=0.2.7->xgcm==0.5.3.dev24+g6e68f71) (1.15.0)\n", + "Requirement already satisfied: pandas>=0.25 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xarray>=0.14.1->xgcm==0.5.3.dev24+g6e68f71) (1.2.1)\n", + "Requirement already satisfied: setuptools>=38.4 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from xarray>=0.14.1->xgcm==0.5.3.dev24+g6e68f71) (49.6.0.post20210108)\n", + "Requirement already satisfied: python-dateutil>=2.7.3 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from pandas>=0.25->xarray>=0.14.1->xgcm==0.5.3.dev24+g6e68f71) (2.7.5)\n", + "Requirement already satisfied: pytz>=2017.3 in /srv/conda/envs/notebook/lib/python3.8/site-packages (from pandas>=0.25->xarray>=0.14.1->xgcm==0.5.3.dev24+g6e68f71) (2021.1)\n", + "Requirement already satisfied: pyyaml in /srv/conda/envs/notebook/lib/python3.8/site-packages (from dask->xgcm==0.5.3.dev24+g6e68f71) (5.3.1)\n" + ] + }, + { + "data": { + "text/plain": [ + "'0.5.3.dev24+g6e68f71'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "!python -m pip install git+https://github.com/xgcm/xgcm.git\n", + "import xgcm\n", + "xgcm.__version__" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n",
+ "Client\n", + "
| \n",
+ "\n",
+ "Cluster\n", + "
| \n",
+ "
<xarray.DataArray 'thetao' (time: 1980, lev: 75, y: 38, x: 20)>\n", + "dask.array<where, shape=(1980, 75, 38, 20), dtype=float32, chunksize=(4, 75, 38, 20), chunktype=numpy.ndarray>\n", + "Coordinates:\n", + " lat (y, x) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " * lev (lev) float64 0.5058 1.556 2.668 ... 5.698e+03 5.902e+03\n", + " lon (y, x) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " * time (time) object 1850-01-16 12:00:00 ... 2014-12-16 12:00:00\n", + " areacello (y, x) float32 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + "Dimensions without coordinates: y, x\n", + "Attributes:\n", + " cell_measures: area: areacello volume: volcello\n", + " cell_methods: area: mean where sea time: mean\n", + " description: Diagnostic should be contributed even for models usi...\n", + " history: none\n", + " interval_operation: 1800 s\n", + " interval_write: 1 month\n", + " long_name: Sea Water Potential Temperature\n", + " online_operation: average\n", + " standard_name: sea_water_potential_temperature\n", + " units: degC
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
array([5.057600e-01, 1.555855e+00, 2.667682e+00, 3.856280e+00, 5.140361e+00,\n", + " 6.543034e+00, 8.092518e+00, 9.822750e+00, 1.177368e+01, 1.399104e+01,\n", + " 1.652532e+01, 1.942980e+01, 2.275762e+01, 2.655830e+01, 3.087456e+01,\n", + " 3.574020e+01, 4.118002e+01, 4.721189e+01, 5.385064e+01, 6.111284e+01,\n", + " 6.902168e+01, 7.761116e+01, 8.692943e+01, 9.704131e+01, 1.080303e+02,\n", + " 1.200000e+02, 1.330758e+02, 1.474062e+02, 1.631645e+02, 1.805499e+02,\n", + " 1.997900e+02, 2.211412e+02, 2.448906e+02, 2.713564e+02, 3.008875e+02,\n", + " 3.338628e+02, 3.706885e+02, 4.117938e+02, 4.576256e+02, 5.086399e+02,\n", + " 5.652923e+02, 6.280260e+02, 6.972586e+02, 7.733683e+02, 8.566789e+02,\n", + " 9.474479e+02, 1.045854e+03, 1.151991e+03, 1.265861e+03, 1.387377e+03,\n", + " 1.516364e+03, 1.652568e+03, 1.795671e+03, 1.945295e+03, 2.101027e+03,\n", + " 2.262422e+03, 2.429025e+03, 2.600380e+03, 2.776039e+03, 2.955570e+03,\n", + " 3.138565e+03, 3.324641e+03, 3.513446e+03, 3.704657e+03, 3.897982e+03,\n", + " 4.093159e+03, 4.289952e+03, 4.488155e+03, 4.687581e+03, 4.888070e+03,\n", + " 5.089479e+03, 5.291683e+03, 5.494575e+03, 5.698061e+03, 5.902058e+03])
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
array([cftime.DatetimeGregorian(1850, 1, 16, 12, 0, 0, 0),\n", + " cftime.DatetimeGregorian(1850, 2, 15, 0, 0, 0, 0),\n", + " cftime.DatetimeGregorian(1850, 3, 16, 12, 0, 0, 0), ...,\n", + " cftime.DatetimeGregorian(2014, 10, 16, 12, 0, 0, 0),\n", + " cftime.DatetimeGregorian(2014, 11, 16, 0, 0, 0, 0),\n", + " cftime.DatetimeGregorian(2014, 12, 16, 12, 0, 0, 0)], dtype=object)
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
<xarray.Dataset>\n", + "Dimensions: (lev: 75, time: 1980, x: 20, x_c: 20, y: 38)\n", + "Coordinates:\n", + " lat (y, x) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " * lev (lev) float64 0.5058 1.556 2.668 ... 5.698e+03 5.902e+03\n", + " lon (y, x) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " * time (time) object 1850-01-16 12:00:00 ... 2014-12-16 12:00:00\n", + " areacello (y, x) float32 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " lat_u (y, x_c) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + " lon_u (y, x_c) float64 dask.array<chunksize=(38, 20), meta=np.ndarray>\n", + "Dimensions without coordinates: x, x_c, y\n", + "Data variables:\n", + " thetao (time, lev, y, x) float32 dask.array<chunksize=(4, 75, 38, 20), meta=np.ndarray>\n", + " uo (time, lev, y, x_c) float32 dask.array<chunksize=(3, 75, 38, 20), meta=np.ndarray>
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
array([5.057600e-01, 1.555855e+00, 2.667682e+00, 3.856280e+00, 5.140361e+00,\n", + " 6.543034e+00, 8.092518e+00, 9.822750e+00, 1.177368e+01, 1.399104e+01,\n", + " 1.652532e+01, 1.942980e+01, 2.275762e+01, 2.655830e+01, 3.087456e+01,\n", + " 3.574020e+01, 4.118002e+01, 4.721189e+01, 5.385064e+01, 6.111284e+01,\n", + " 6.902168e+01, 7.761116e+01, 8.692943e+01, 9.704131e+01, 1.080303e+02,\n", + " 1.200000e+02, 1.330758e+02, 1.474062e+02, 1.631645e+02, 1.805499e+02,\n", + " 1.997900e+02, 2.211412e+02, 2.448906e+02, 2.713564e+02, 3.008875e+02,\n", + " 3.338628e+02, 3.706885e+02, 4.117938e+02, 4.576256e+02, 5.086399e+02,\n", + " 5.652923e+02, 6.280260e+02, 6.972586e+02, 7.733683e+02, 8.566789e+02,\n", + " 9.474479e+02, 1.045854e+03, 1.151991e+03, 1.265861e+03, 1.387377e+03,\n", + " 1.516364e+03, 1.652568e+03, 1.795671e+03, 1.945295e+03, 2.101027e+03,\n", + " 2.262422e+03, 2.429025e+03, 2.600380e+03, 2.776039e+03, 2.955570e+03,\n", + " 3.138565e+03, 3.324641e+03, 3.513446e+03, 3.704657e+03, 3.897982e+03,\n", + " 4.093159e+03, 4.289952e+03, 4.488155e+03, 4.687581e+03, 4.888070e+03,\n", + " 5.089479e+03, 5.291683e+03, 5.494575e+03, 5.698061e+03, 5.902058e+03])
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
array([cftime.DatetimeGregorian(1850, 1, 16, 12, 0, 0, 0),\n", + " cftime.DatetimeGregorian(1850, 2, 15, 0, 0, 0, 0),\n", + " cftime.DatetimeGregorian(1850, 3, 16, 12, 0, 0, 0), ...,\n", + " cftime.DatetimeGregorian(2014, 10, 16, 12, 0, 0, 0),\n", + " cftime.DatetimeGregorian(2014, 11, 16, 0, 0, 0, 0),\n", + " cftime.DatetimeGregorian(2014, 12, 16, 12, 0, 0, 0)], dtype=object)
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "
\n",
+ "
| \n",
+ "\n", + "\n", + " | \n", + "