{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparison of six regridding algorithms\n", "\n", "xESMF exposes five different regridding algorithms from the ESMF library:\n", "\n", "- `bilinear`: `ESMF.RegridMethod.BILINEAR`\n", "- `conservative`: `ESMF.RegridMethod.CONSERVE`\n", "- `conservative_normed`: `ESMF.RegridMethod.CONSERVE`\n", "- `patch`: `ESMF.RegridMethod.PATCH`\n", "- `nearest_s2d`: `ESMF.RegridMethod.NEAREST_STOD`\n", "- `nearest_d2s`: `ESMF.RegridMethod.NEAREST_DTOS`\n", "\n", "where `conservative_normed` is just the `conservative` method with the\n", "normalization set to `ESMF.NormType.FRACAREA` instead of the default\n", "`norm_type=ESMF.NormType.DSTAREA`.\n", "\n", "This notebook demonstrates how these algorithms behave in different situations.\n", "\n", "## Notes\n", "\n", "- `bilinear` and `conservative` should be the most commonly used methods. They\n", " are both monotonic (i.e. will not create new maximum/minimum).\n", "- Nearest neighbour methods, either source to destination (s2d) or destination\n", " to source (d2s), could be useful in special cases. Keep in mind that d2s is\n", " highly non-monotonic.\n", "- Patch is ESMF's unique method, producing highly smooth results but quite slow.\n", "- From the ESMF documentation:\n", "\n", " > The weight $w_{ij}$ for a particular source cell $i$ and destination cell\n", " > $j$ are calculated as $w_{ij}=f_{ij} * A_{si}/A_{dj}$. In this equation\n", " > $f_{ij}$ is the fraction of the source cell $i$ contributing to destination\n", " > cell $j$, and $A_{si}$ and $A_{dj}$ are the areas of the source and\n", " > destination cells.\n", "\n", " For `conservative_normed`,\n", "\n", " > ... then the weights are further divided by the destination fraction. In\n", " > other words, in that case $w_{ij}=f_{ij} * A_{si}/(A_{dj}*D_j)$ where $D_j$\n", " > is fraction of the destination cell that intersects the unmasked source\n", " > grid.\n", "\n", "Detailed explanations are available on\n", "[ESMPy documentation](http://www.earthsystemmodeling.org/esmf_releases/last_built/esmpy_doc/html/api.html#regridding).\n", "\n", "## Preparation\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import cartopy.crs as ccrs\n", "import numpy as np\n", "import xarray as xr\n", "import xesmf as xe\n", "\n", "method_list = [\n", " \"bilinear\",\n", " \"conservative\",\n", " \"conservative_normed\",\n", " \"nearest_s2d\",\n", " \"nearest_d2s\",\n", " \"patch\",\n", "]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ds_in = xe.util.grid_global(20, 15) # input grid\n", "ds_fine = xe.util.grid_global(4, 4) # high-resolution target grid\n", "ds_coarse = xe.util.grid_global(30, 20) # low-resolution target grid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a wave field that is widely used in regridding benchmarks.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n",
"Dimensions: (y: 12, x: 18, y_b: 13, x_b: 19)\n",
"Coordinates:\n",
" lon (y, x) float64 -170.0 -150.0 -130.0 -110.0 ... 130.0 150.0 170.0\n",
" lat (y, x) float64 -82.5 -82.5 -82.5 -82.5 ... 82.5 82.5 82.5 82.5\n",
" lon_b (y_b, x_b) int64 -180 -160 -140 -120 -100 ... 100 120 140 160 180\n",
" lat_b (y_b, x_b) int64 -90 -90 -90 -90 -90 -90 -90 ... 90 90 90 90 90 90\n",
"Dimensions without coordinates: y, x, y_b, x_b\n",
"Data variables:\n",
" data (y, x) float64 2.016 2.009 1.997 1.987 ... 1.987 1.997 2.009 2.016array([[-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.],\n",
" [-170., -150., -130., -110., -90., -70., -50., -30., -10.,\n",
" 10., 30., 50., 70., 90., 110., 130., 150., 170.]])array([[-82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5,\n",
" -82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5, -82.5],\n",
" [-67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5,\n",
" -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5],\n",
" [-52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5,\n",
" -52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5, -52.5],\n",
" [-37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5,\n",
" -37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5, -37.5],\n",
" [-22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5,\n",
" -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5],\n",
" [ -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5,\n",
" -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5],\n",
" [ 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5,\n",
" 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5],\n",
" [ 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5,\n",
" 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5],\n",
" [ 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5,\n",
" 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5],\n",
" [ 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5,\n",
" 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5],\n",
" [ 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5,\n",
" 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5],\n",
" [ 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5,\n",
" 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5, 82.5]])array([[-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180],\n",
" [-180, -160, -140, -120, -100, -80, -60, -40, -20, 0, 20,\n",
" 40, 60, 80, 100, 120, 140, 160, 180]])array([[-90, -90, -90, -90, -90, -90, -90, -90, -90, -90, -90, -90, -90,\n",
" -90, -90, -90, -90, -90, -90],\n",
" [-75, -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, -75,\n",
" -75, -75, -75, -75, -75, -75],\n",
" [-60, -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, -60,\n",
" -60, -60, -60, -60, -60, -60],\n",
" [-45, -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, -45,\n",
" -45, -45, -45, -45, -45, -45],\n",
" [-30, -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, -30,\n",
" -30, -30, -30, -30, -30, -30],\n",
" [-15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15,\n",
" -15, -15, -15, -15, -15, -15],\n",
" [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0],\n",
" [ 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,\n",
" 15, 15, 15, 15, 15, 15],\n",
" [ 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,\n",
" 30, 30, 30, 30, 30, 30],\n",
" [ 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,\n",
" 45, 45, 45, 45, 45, 45],\n",
" [ 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,\n",
" 60, 60, 60, 60, 60, 60],\n",
" [ 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,\n",
" 75, 75, 75, 75, 75, 75],\n",
" [ 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90,\n",
" 90, 90, 90, 90, 90, 90]])array([[2.01600962, 2.00851854, 1.99704154, 1.98694883, 1.98296291,\n",
" 1.98694883, 1.99704154, 2.00851854, 2.01600962, 2.01600962,\n",
" 2.00851854, 1.99704154, 1.98694883, 1.98296291, 1.98694883,\n",
" 1.99704154, 2.00851854, 2.01600962],\n",
" [2.1376148 , 2.0732233 , 1.97456981, 1.88781539, 1.85355339,\n",
" 1.88781539, 1.97456981, 2.0732233 , 2.1376148 , 2.1376148 ,\n",
" 2.0732233 , 1.97456981, 1.88781539, 1.85355339, 1.88781539,\n",
" 1.97456981, 2.0732233 , 2.1376148 ],\n",
" [2.34824114, 2.18529524, 1.93564764, 1.71611122, 1.62940952,\n",
" 1.71611122, 1.93564764, 2.18529524, 2.34824114, 2.34824114,\n",
" 2.18529524, 1.93564764, 1.71611122, 1.62940952, 1.71611122,\n",
" 1.93564764, 2.18529524, 2.34824114],\n",
" [2.59145148, 2.31470476, 1.89070418, 1.51784433, 1.37059048,\n",
" 1.51784433, 1.89070418, 2.31470476, 2.59145148, 2.59145148,\n",
" 2.31470476, 1.89070418, 1.51784433, 1.37059048, 1.51784433,\n",
" 1.89070418, 2.31470476, 2.59145148],\n",
" [2.80207782, 2.4267767 , 1.85178201, 1.34614017, 1.14644661,\n",
" 1.34614017, 1.85178201, 2.4267767 , 2.80207782, 2.80207782,\n",
" 2.4267767 , 1.85178201, 1.34614017, 1.14644661, 1.34614017,\n",
" 1.85178201, 2.4267767 , 2.80207782],\n",
"...\n",
" [2.80207782, 2.4267767 , 1.85178201, 1.34614017, 1.14644661,\n",
" 1.34614017, 1.85178201, 2.4267767 , 2.80207782, 2.80207782,\n",
" 2.4267767 , 1.85178201, 1.34614017, 1.14644661, 1.34614017,\n",
" 1.85178201, 2.4267767 , 2.80207782],\n",
" [2.59145148, 2.31470476, 1.89070418, 1.51784433, 1.37059048,\n",
" 1.51784433, 1.89070418, 2.31470476, 2.59145148, 2.59145148,\n",
" 2.31470476, 1.89070418, 1.51784433, 1.37059048, 1.51784433,\n",
" 1.89070418, 2.31470476, 2.59145148],\n",
" [2.34824114, 2.18529524, 1.93564764, 1.71611122, 1.62940952,\n",
" 1.71611122, 1.93564764, 2.18529524, 2.34824114, 2.34824114,\n",
" 2.18529524, 1.93564764, 1.71611122, 1.62940952, 1.71611122,\n",
" 1.93564764, 2.18529524, 2.34824114],\n",
" [2.1376148 , 2.0732233 , 1.97456981, 1.88781539, 1.85355339,\n",
" 1.88781539, 1.97456981, 2.0732233 , 2.1376148 , 2.1376148 ,\n",
" 2.0732233 , 1.97456981, 1.88781539, 1.85355339, 1.88781539,\n",
" 1.97456981, 2.0732233 , 2.1376148 ],\n",
" [2.01600962, 2.00851854, 1.99704154, 1.98694883, 1.98296291,\n",
" 1.98694883, 1.99704154, 2.00851854, 2.01600962, 2.01600962,\n",
" 2.00851854, 1.99704154, 1.98694883, 1.98296291, 1.98694883,\n",
" 1.99704154, 2.00851854, 2.01600962]])