Save time by reusing regridder

There is an important reason why the regridding is broken into two steps (making the regridder and perform regridding). For high-resolution grids, making the regridder (i.e. “computing regridding weights”, explained later) is quite computationally expensive, but performing regridding on data (“applying regridding weights”) is still pretty fast.

[1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import xesmf as xe

Prepare data

The grids in previous examples were all quite small and the regridding was almost instantaneous. Let’s try a large-ish grid here.

[2]:
ds_in = xe.util.grid_2d(
    -120, 120, 0.4, -60, 60, 0.3  # longitude range and resolution
)  # latitude range and resolution
ds_in
[2]:
Show/Hide data repr Show/Hide attributes
xarray.Dataset
    • x: 600
    • x_b: 601
    • y: 400
    • y_b: 401
    • lon
      (y, x)
      float64
      -119.8 -119.4 ... 119.4 119.8
      array([[-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
             [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
             [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
             ...,
             [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
             [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
             [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8]])
    • lat
      (y, x)
      float64
      -59.85 -59.85 ... 59.85 59.85
      array([[-59.85, -59.85, -59.85, ..., -59.85, -59.85, -59.85],
             [-59.55, -59.55, -59.55, ..., -59.55, -59.55, -59.55],
             [-59.25, -59.25, -59.25, ..., -59.25, -59.25, -59.25],
             ...,
             [ 59.25,  59.25,  59.25, ...,  59.25,  59.25,  59.25],
             [ 59.55,  59.55,  59.55, ...,  59.55,  59.55,  59.55],
             [ 59.85,  59.85,  59.85, ...,  59.85,  59.85,  59.85]])
    • lon_b
      (y_b, x_b)
      float64
      -120.0 -119.6 ... 119.6 120.0
      array([[-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
             [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
             [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
             ...,
             [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
             [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
             [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ]])
    • lat_b
      (y_b, x_b)
      float64
      -60.0 -60.0 -60.0 ... 60.0 60.0
      array([[-60. , -60. , -60. , ..., -60. , -60. , -60. ],
             [-59.7, -59.7, -59.7, ..., -59.7, -59.7, -59.7],
             [-59.4, -59.4, -59.4, ..., -59.4, -59.4, -59.4],
             ...,
             [ 59.4,  59.4,  59.4, ...,  59.4,  59.4,  59.4],
             [ 59.7,  59.7,  59.7, ...,  59.7,  59.7,  59.7],
             [ 60. ,  60. ,  60. , ...,  60. ,  60. ,  60. ]])
    [3]:
    
    ds_out = xe.util.grid_2d(-120, 120, 0.6, -60, 60, 0.4)
    ds_out
    
    [3]:
    
    Show/Hide data repr Show/Hide attributes
    xarray.Dataset
      • x: 400
      • x_b: 401
      • y: 300
      • y_b: 301
      • lon
        (y, x)
        float64
        -119.7 -119.1 ... 119.1 119.7
        array([[-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7],
               [-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7],
               [-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7],
               ...,
               [-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7],
               [-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7],
               [-119.7, -119.1, -118.5, ...,  118.5,  119.1,  119.7]])
      • lat
        (y, x)
        float64
        -59.8 -59.8 -59.8 ... 59.8 59.8
        array([[-59.8, -59.8, -59.8, ..., -59.8, -59.8, -59.8],
               [-59.4, -59.4, -59.4, ..., -59.4, -59.4, -59.4],
               [-59. , -59. , -59. , ..., -59. , -59. , -59. ],
               ...,
               [ 59. ,  59. ,  59. , ...,  59. ,  59. ,  59. ],
               [ 59.4,  59.4,  59.4, ...,  59.4,  59.4,  59.4],
               [ 59.8,  59.8,  59.8, ...,  59.8,  59.8,  59.8]])
      • lon_b
        (y_b, x_b)
        float64
        -120.0 -119.4 ... 119.4 120.0
        array([[-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ],
               [-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ],
               [-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ],
               ...,
               [-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ],
               [-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ],
               [-120. , -119.4, -118.8, ...,  118.8,  119.4,  120. ]])
      • lat_b
        (y_b, x_b)
        float64
        -60.0 -60.0 -60.0 ... 60.0 60.0
        array([[-60. , -60. , -60. , ..., -60. , -60. , -60. ],
               [-59.6, -59.6, -59.6, ..., -59.6, -59.6, -59.6],
               [-59.2, -59.2, -59.2, ..., -59.2, -59.2, -59.2],
               ...,
               [ 59.2,  59.2,  59.2, ...,  59.2,  59.2,  59.2],
               [ 59.6,  59.6,  59.6, ...,  59.6,  59.6,  59.6],
               [ 60. ,  60. ,  60. , ...,  60. ,  60. ,  60. ]])

      Also make a large-ish 4D data, with multiple time frames and vertical levels.

      [4]:
      
      ds_in.coords["time"] = np.arange(1, 11)
      ds_in.coords["lev"] = np.arange(1, 51)
      ds_in["data2D"] = xe.data.wave_smooth(ds_in["lon"], ds_in["lat"])
      ds_in["data4D"] = ds_in["time"] * ds_in["lev"] * ds_in["data2D"]
      ds_in
      
      [4]:
      
      Show/Hide data repr Show/Hide attributes
      xarray.Dataset
        • lev: 50
        • time: 10
        • x: 600
        • x_b: 601
        • y: 400
        • y_b: 401
        • lon
          (y, x)
          float64
          -119.8 -119.4 ... 119.4 119.8
          array([[-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
                 [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
                 [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
                 ...,
                 [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
                 [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8],
                 [-119.8, -119.4, -119. , ...,  119. ,  119.4,  119.8]])
        • lat
          (y, x)
          float64
          -59.85 -59.85 ... 59.85 59.85
          array([[-59.85, -59.85, -59.85, ..., -59.85, -59.85, -59.85],
                 [-59.55, -59.55, -59.55, ..., -59.55, -59.55, -59.55],
                 [-59.25, -59.25, -59.25, ..., -59.25, -59.25, -59.25],
                 ...,
                 [ 59.25,  59.25,  59.25, ...,  59.25,  59.25,  59.25],
                 [ 59.55,  59.55,  59.55, ...,  59.55,  59.55,  59.55],
                 [ 59.85,  59.85,  59.85, ...,  59.85,  59.85,  59.85]])
        • lon_b
          (y_b, x_b)
          float64
          -120.0 -119.6 ... 119.6 120.0
          array([[-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
                 [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
                 [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
                 ...,
                 [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
                 [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ],
                 [-120. , -119.6, -119.2, ...,  119.2,  119.6,  120. ]])
        • lat_b
          (y_b, x_b)
          float64
          -60.0 -60.0 -60.0 ... 60.0 60.0
          array([[-60. , -60. , -60. , ..., -60. , -60. , -60. ],
                 [-59.7, -59.7, -59.7, ..., -59.7, -59.7, -59.7],
                 [-59.4, -59.4, -59.4, ..., -59.4, -59.4, -59.4],
                 ...,
                 [ 59.4,  59.4,  59.4, ...,  59.4,  59.4,  59.4],
                 [ 59.7,  59.7,  59.7, ...,  59.7,  59.7,  59.7],
                 [ 60. ,  60. ,  60. , ...,  60. ,  60. ,  60. ]])
        • time
          (time)
          int64
          1 2 3 4 5 6 7 8 9 10
          array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
        • lev
          (lev)
          int64
          1 2 3 4 5 6 7 ... 45 46 47 48 49 50
          array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
                 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
                 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50])
        • data2D
          (y, x)
          float64
          1.872 1.869 1.866 ... 1.869 1.872
          array([[1.87234253, 1.86931698, 1.86631691, ..., 1.86631691, 1.86931698,
                  1.87234253],
                 [1.87003418, 1.86695393, 1.86389961, ..., 1.86389961, 1.86695393,
                  1.87003418],
                 [1.86771234, 1.86457706, 1.86146818, ..., 1.86146818, 1.86457706,
                  1.86771234],
                 ...,
                 [1.86771234, 1.86457706, 1.86146818, ..., 1.86146818, 1.86457706,
                  1.86771234],
                 [1.87003418, 1.86695393, 1.86389961, ..., 1.86389961, 1.86695393,
                  1.87003418],
                 [1.87234253, 1.86931698, 1.86631691, ..., 1.86631691, 1.86931698,
                  1.87234253]])
        • data4D
          (time, lev, y, x)
          float64
          1.872 1.869 1.866 ... 934.7 936.2
          array([[[[  1.87234253,   1.86931698,   1.86631691, ...,   1.86631691,
                      1.86931698,   1.87234253],
                   [  1.87003418,   1.86695393,   1.86389961, ...,   1.86389961,
                      1.86695393,   1.87003418],
                   [  1.86771234,   1.86457706,   1.86146818, ...,   1.86146818,
                      1.86457706,   1.86771234],
                   ...,
                   [  1.86771234,   1.86457706,   1.86146818, ...,   1.86146818,
                      1.86457706,   1.86771234],
                   [  1.87003418,   1.86695393,   1.86389961, ...,   1.86389961,
                      1.86695393,   1.87003418],
                   [  1.87234253,   1.86931698,   1.86631691, ...,   1.86631691,
                      1.86931698,   1.87234253]],
          
                  [[  3.74468505,   3.73863396,   3.73263383, ...,   3.73263383,
                      3.73863396,   3.74468505],
                   [  3.74006836,   3.73390785,   3.72779922, ...,   3.72779922,
                      3.73390785,   3.74006836],
                   [  3.73542468,   3.72915412,   3.72293635, ...,   3.72293635,
                      3.72915412,   3.73542468],
                   ...,
                   [  3.73542468,   3.72915412,   3.72293635, ...,   3.72293635,
                      3.72915412,   3.73542468],
                   [  3.74006836,   3.73390785,   3.72779922, ...,   3.72779922,
                      3.73390785,   3.74006836],
                   [  3.74468505,   3.73863396,   3.73263383, ...,   3.73263383,
                      3.73863396,   3.74468505]],
          
                  [[  5.61702758,   5.60795094,   5.59895074, ...,   5.59895074,
                      5.60795094,   5.61702758],
                   [  5.61010254,   5.60086178,   5.59169883, ...,   5.59169883,
                      5.60086178,   5.61010254],
                   [  5.60313702,   5.59373117,   5.58440453, ...,   5.58440453,
                      5.59373117,   5.60313702],
                   ...,
                   [  5.60313702,   5.59373117,   5.58440453, ...,   5.58440453,
                      5.59373117,   5.60313702],
                   [  5.61010254,   5.60086178,   5.59169883, ...,   5.59169883,
                      5.60086178,   5.61010254],
                   [  5.61702758,   5.60795094,   5.59895074, ...,   5.59895074,
                      5.60795094,   5.61702758]],
          
                  ...,
          
                  [[ 89.87244122,  89.72721511,  89.58321189, ...,  89.58321189,
                     89.72721511,  89.87244122],
                   [ 89.76164062,  89.61378848,  89.46718135, ...,  89.46718135,
                     89.61378848,  89.76164062],
                   [ 89.65019231,  89.49969879,  89.35047252, ...,  89.35047252,
                     89.49969879,  89.65019231],
                   ...,
                   [ 89.65019231,  89.49969879,  89.35047252, ...,  89.35047252,
                     89.49969879,  89.65019231],
                   [ 89.76164062,  89.61378848,  89.46718135, ...,  89.46718135,
                     89.61378848,  89.76164062],
                   [ 89.87244122,  89.72721511,  89.58321189, ...,  89.58321189,
                     89.72721511,  89.87244122]],
          
                  [[ 91.74478375,  91.59653209,  91.44952881, ...,  91.44952881,
                     91.59653209,  91.74478375],
                   [ 91.6316748 ,  91.48074241,  91.33108096, ...,  91.33108096,
                     91.48074241,  91.6316748 ],
                   [ 91.51790465,  91.36427585,  91.2119407 , ...,  91.2119407 ,
                     91.36427585,  91.51790465],
                   ...,
                   [ 91.51790465,  91.36427585,  91.2119407 , ...,  91.2119407 ,
                     91.36427585,  91.51790465],
                   [ 91.6316748 ,  91.48074241,  91.33108096, ...,  91.33108096,
                     91.48074241,  91.6316748 ],
                   [ 91.74478375,  91.59653209,  91.44952881, ...,  91.44952881,
                     91.59653209,  91.74478375]],
          
                  [[ 93.61712627,  93.46584907,  93.31584572, ...,  93.31584572,
                     93.46584907,  93.61712627],
                   [ 93.50170898,  93.34769633,  93.19498057, ...,  93.19498057,
                     93.34769633,  93.50170898],
                   [ 93.38561699,  93.22885291,  93.07340887, ...,  93.07340887,
                     93.22885291,  93.38561699],
                   ...,
                   [ 93.38561699,  93.22885291,  93.07340887, ...,  93.07340887,
                     93.22885291,  93.38561699],
                   [ 93.50170898,  93.34769633,  93.19498057, ...,  93.19498057,
                     93.34769633,  93.50170898],
                   [ 93.61712627,  93.46584907,  93.31584572, ...,  93.31584572,
                     93.46584907,  93.61712627]]],
          
          
                 [[[  3.74468505,   3.73863396,   3.73263383, ...,   3.73263383,
                      3.73863396,   3.74468505],
                   [  3.74006836,   3.73390785,   3.72779922, ...,   3.72779922,
                      3.73390785,   3.74006836],
                   [  3.73542468,   3.72915412,   3.72293635, ...,   3.72293635,
                      3.72915412,   3.73542468],
                   ...,
                   [  3.73542468,   3.72915412,   3.72293635, ...,   3.72293635,
                      3.72915412,   3.73542468],
                   [  3.74006836,   3.73390785,   3.72779922, ...,   3.72779922,
                      3.73390785,   3.74006836],
                   [  3.74468505,   3.73863396,   3.73263383, ...,   3.73263383,
                      3.73863396,   3.74468505]],
          
                  [[  7.4893701 ,   7.47726793,   7.46526766, ...,   7.46526766,
                      7.47726793,   7.4893701 ],
                   [  7.48013672,   7.46781571,   7.45559845, ...,   7.45559845,
                      7.46781571,   7.48013672],
                   [  7.47084936,   7.45830823,   7.44587271, ...,   7.44587271,
                      7.45830823,   7.47084936],
                   ...,
                   [  7.47084936,   7.45830823,   7.44587271, ...,   7.44587271,
                      7.45830823,   7.47084936],
                   [  7.48013672,   7.46781571,   7.45559845, ...,   7.45559845,
                      7.46781571,   7.48013672],
                   [  7.4893701 ,   7.47726793,   7.46526766, ...,   7.46526766,
                      7.47726793,   7.4893701 ]],
          
                  [[ 11.23405515,  11.21590189,  11.19790149, ...,  11.19790149,
                     11.21590189,  11.23405515],
                   [ 11.22020508,  11.20172356,  11.18339767, ...,  11.18339767,
                     11.20172356,  11.22020508],
                   [ 11.20627404,  11.18746235,  11.16880906, ...,  11.16880906,
                     11.18746235,  11.20627404],
                   ...,
                   [ 11.20627404,  11.18746235,  11.16880906, ...,  11.16880906,
                     11.18746235,  11.20627404],
                   [ 11.22020508,  11.20172356,  11.18339767, ...,  11.18339767,
                     11.20172356,  11.22020508],
                   [ 11.23405515,  11.21590189,  11.19790149, ...,  11.19790149,
                     11.21590189,  11.23405515]],
          
                  ...,
          
                  [[179.74488244, 179.45443022, 179.16642378, ..., 179.16642378,
                    179.45443022, 179.74488244],
                   [179.52328123, 179.22757696, 178.93436269, ..., 178.93436269,
                    179.22757696, 179.52328123],
                   [179.30038461, 178.99939758, 178.70094504, ..., 178.70094504,
                    178.99939758, 179.30038461],
                   ...,
                   [179.30038461, 178.99939758, 178.70094504, ..., 178.70094504,
                    178.99939758, 179.30038461],
                   [179.52328123, 179.22757696, 178.93436269, ..., 178.93436269,
                    179.22757696, 179.52328123],
                   [179.74488244, 179.45443022, 179.16642378, ..., 179.16642378,
                    179.45443022, 179.74488244]],
          
                  [[183.48956749, 183.19306418, 182.89905761, ..., 182.89905761,
                    183.19306418, 183.48956749],
                   [183.26334959, 182.96148481, 182.66216191, ..., 182.66216191,
                    182.96148481, 183.26334959],
                   [183.03580929, 182.72855169, 182.42388139, ..., 182.42388139,
                    182.72855169, 183.03580929],
                   ...,
                   [183.03580929, 182.72855169, 182.42388139, ..., 182.42388139,
                    182.72855169, 183.03580929],
                   [183.26334959, 182.96148481, 182.66216191, ..., 182.66216191,
                    182.96148481, 183.26334959],
                   [183.48956749, 183.19306418, 182.89905761, ..., 182.89905761,
                    183.19306418, 183.48956749]],
          
                  [[187.23425254, 186.93169815, 186.63169144, ..., 186.63169144,
                    186.93169815, 187.23425254],
                   [187.00341795, 186.69539266, 186.38996114, ..., 186.38996114,
                    186.69539267, 187.00341795],
                   [186.77123397, 186.45770581, 186.14681775, ..., 186.14681775,
                    186.45770581, 186.77123397],
                   ...,
                   [186.77123397, 186.45770581, 186.14681775, ..., 186.14681775,
                    186.45770581, 186.77123397],
                   [187.00341795, 186.69539266, 186.38996114, ..., 186.38996114,
                    186.69539267, 187.00341795],
                   [187.23425254, 186.93169815, 186.63169144, ..., 186.63169144,
                    186.93169815, 187.23425254]]],
          
          
                 [[[  5.61702758,   5.60795094,   5.59895074, ...,   5.59895074,
                      5.60795094,   5.61702758],
                   [  5.61010254,   5.60086178,   5.59169883, ...,   5.59169883,
                      5.60086178,   5.61010254],
                   [  5.60313702,   5.59373117,   5.58440453, ...,   5.58440453,
                      5.59373117,   5.60313702],
                   ...,
                   [  5.60313702,   5.59373117,   5.58440453, ...,   5.58440453,
                      5.59373117,   5.60313702],
                   [  5.61010254,   5.60086178,   5.59169883, ...,   5.59169883,
                      5.60086178,   5.61010254],
                   [  5.61702758,   5.60795094,   5.59895074, ...,   5.59895074,
                      5.60795094,   5.61702758]],
          
                  [[ 11.23405515,  11.21590189,  11.19790149, ...,  11.19790149,
                     11.21590189,  11.23405515],
                   [ 11.22020508,  11.20172356,  11.18339767, ...,  11.18339767,
                     11.20172356,  11.22020508],
                   [ 11.20627404,  11.18746235,  11.16880906, ...,  11.16880906,
                     11.18746235,  11.20627404],
                   ...,
                   [ 11.20627404,  11.18746235,  11.16880906, ...,  11.16880906,
                     11.18746235,  11.20627404],
                   [ 11.22020508,  11.20172356,  11.18339767, ...,  11.18339767,
                     11.20172356,  11.22020508],
                   [ 11.23405515,  11.21590189,  11.19790149, ...,  11.19790149,
                     11.21590189,  11.23405515]],
          
                  [[ 16.85108273,  16.82385283,  16.79685223, ...,  16.79685223,
                     16.82385283,  16.85108273],
                   [ 16.83030762,  16.80258534,  16.7750965 , ...,  16.7750965 ,
                     16.80258534,  16.83030762],
                   [ 16.80941106,  16.78119352,  16.7532136 , ...,  16.7532136 ,
                     16.78119352,  16.80941106],
                   ...,
                   [ 16.80941106,  16.78119352,  16.7532136 , ...,  16.7532136 ,
                     16.78119352,  16.80941106],
                   [ 16.83030762,  16.80258534,  16.7750965 , ...,  16.7750965 ,
                     16.80258534,  16.83030762],
                   [ 16.85108273,  16.82385283,  16.79685223, ...,  16.79685223,
                     16.82385283,  16.85108273]],
          
                  ...,
          
                  [[269.61732366, 269.18164533, 268.74963567, ..., 268.74963567,
                    269.18164533, 269.61732366],
                   [269.28492185, 268.84136544, 268.40154404, ..., 268.40154404,
                    268.84136544, 269.28492185],
                   [268.95057692, 268.49909637, 268.05141755, ..., 268.05141755,
                    268.49909637, 268.95057692],
                   ...,
                   [268.95057692, 268.49909637, 268.05141755, ..., 268.05141755,
                    268.49909637, 268.95057692],
                   [269.28492185, 268.84136544, 268.40154404, ..., 268.40154404,
                    268.84136544, 269.28492185],
                   [269.61732366, 269.18164533, 268.74963567, ..., 268.74963567,
                    269.18164533, 269.61732366]],
          
                  [[275.23435124, 274.78959627, 274.34858642, ..., 274.34858642,
                    274.78959627, 275.23435124],
                   [274.89502439, 274.44222722, 273.99324287, ..., 273.99324287,
                    274.44222722, 274.89502439],
                   [274.55371394, 274.09282754, 273.63582209, ..., 273.63582209,
                    274.09282754, 274.55371394],
                   ...,
                   [274.55371394, 274.09282754, 273.63582209, ..., 273.63582209,
                    274.09282754, 274.55371394],
                   [274.89502439, 274.44222722, 273.99324287, ..., 273.99324287,
                    274.44222722, 274.89502439],
                   [275.23435124, 274.78959627, 274.34858642, ..., 274.34858642,
                    274.78959627, 275.23435124]],
          
                  [[280.85137881, 280.39754722, 279.94753716, ..., 279.94753716,
                    280.39754722, 280.85137881],
                   [280.50512693, 280.043089  , 279.5849417 , ..., 279.5849417 ,
                    280.043089  , 280.50512693],
                   [280.15685096, 279.68655872, 279.22022662, ..., 279.22022662,
                    279.68655872, 280.15685096],
                   ...,
                   [280.15685096, 279.68655872, 279.22022662, ..., 279.22022662,
                    279.68655872, 280.15685096],
                   [280.50512693, 280.043089  , 279.5849417 , ..., 279.5849417 ,
                    280.043089  , 280.50512693],
                   [280.85137881, 280.39754722, 279.94753716, ..., 279.94753716,
                    280.39754722, 280.85137881]]],
          
          
                 ...,
          
          
                 [[[ 14.9787402 ,  14.95453585,  14.93053532, ...,  14.93053532,
                     14.95453585,  14.9787402 ],
                   [ 14.96027344,  14.93563141,  14.91119689, ...,  14.91119689,
                     14.93563141,  14.96027344],
                   [ 14.94169872,  14.91661646,  14.89174542, ...,  14.89174542,
                     14.91661646,  14.94169872],
                   ...,
                   [ 14.94169872,  14.91661646,  14.89174542, ...,  14.89174542,
                     14.91661646,  14.94169872],
                   [ 14.96027344,  14.93563141,  14.91119689, ...,  14.91119689,
                     14.93563141,  14.96027344],
                   [ 14.9787402 ,  14.95453585,  14.93053532, ...,  14.93053532,
                     14.95453585,  14.9787402 ]],
          
                  [[ 29.95748041,  29.9090717 ,  29.86107063, ...,  29.86107063,
                     29.9090717 ,  29.95748041],
                   [ 29.92054687,  29.87126283,  29.82239378, ...,  29.82239378,
                     29.87126283,  29.92054687],
                   [ 29.88339744,  29.83323293,  29.78349084, ...,  29.78349084,
                     29.83323293,  29.88339744],
                   ...,
                   [ 29.88339744,  29.83323293,  29.78349084, ...,  29.78349084,
                     29.83323293,  29.88339744],
                   [ 29.92054687,  29.87126283,  29.82239378, ...,  29.82239378,
                     29.87126283,  29.92054687],
                   [ 29.95748041,  29.9090717 ,  29.86107063, ...,  29.86107063,
                     29.9090717 ,  29.95748041]],
          
                  [[ 44.93622061,  44.86360755,  44.79160595, ...,  44.79160595,
                     44.86360755,  44.93622061],
                   [ 44.88082031,  44.80689424,  44.73359067, ...,  44.73359067,
                     44.80689424,  44.88082031],
                   [ 44.82509615,  44.74984939,  44.67523626, ...,  44.67523626,
                     44.74984939,  44.82509615],
                   ...,
                   [ 44.82509615,  44.74984939,  44.67523626, ...,  44.67523626,
                     44.74984939,  44.82509615],
                   [ 44.88082031,  44.80689424,  44.73359067, ...,  44.73359067,
                     44.80689424,  44.88082031],
                   [ 44.93622061,  44.86360755,  44.79160595, ...,  44.79160595,
                     44.86360755,  44.93622061]],
          
                  ...,
          
                  [[718.97952976, 717.81772088, 716.66569513, ..., 716.66569513,
                    717.81772088, 718.97952976],
                   [718.09312494, 716.91030783, 715.73745076, ..., 715.73745076,
                    716.91030783, 718.09312494],
                   [717.20153845, 715.99759031, 714.80378015, ..., 714.80378015,
                    715.99759031, 717.20153845],
                   ...,
                   [717.20153845, 715.99759031, 714.80378015, ..., 714.80378015,
                    715.99759031, 717.20153845],
                   [718.09312494, 716.91030783, 715.73745076, ..., 715.73745076,
                    716.91030783, 718.09312494],
                   [718.97952976, 717.81772088, 716.66569513, ..., 716.66569513,
                    717.81772088, 718.97952976]],
          
                  [[733.95826996, 732.77225673, 731.59623044, ..., 731.59623044,
                    732.77225673, 733.95826996],
                   [733.05339838, 731.84593925, 730.64864766, ..., 730.64864766,
                    731.84593925, 733.05339838],
                   [732.14323717, 730.91420678, 729.69552557, ..., 729.69552557,
                    730.91420678, 732.14323717],
                   ...,
                   [732.14323717, 730.91420678, 729.69552557, ..., 729.69552557,
                    730.91420678, 732.14323717],
                   [733.05339838, 731.84593925, 730.64864766, ..., 730.64864766,
                    731.84593925, 733.05339838],
                   [733.95826996, 732.77225673, 731.59623044, ..., 731.59623044,
                    732.77225673, 733.95826996]],
          
                  [[748.93701017, 747.72679258, 746.52676576, ..., 746.52676576,
                    747.72679258, 748.93701017],
                   [748.01367181, 746.78157066, 745.55984455, ..., 745.55984455,
                    746.78157066, 748.01367181],
                   [747.08493588, 745.83082324, 744.58727099, ..., 744.58727099,
                    745.83082324, 747.08493588],
                   ...,
                   [747.08493588, 745.83082324, 744.58727099, ..., 744.58727099,
                    745.83082324, 747.08493588],
                   [748.01367181, 746.78157066, 745.55984455, ..., 745.55984455,
                    746.78157066, 748.01367181],
                   [748.93701017, 747.72679258, 746.52676576, ..., 746.52676576,
                    747.72679258, 748.93701017]]],
          
          
                 [[[ 16.85108273,  16.82385283,  16.79685223, ...,  16.79685223,
                     16.82385283,  16.85108273],
                   [ 16.83030762,  16.80258534,  16.7750965 , ...,  16.7750965 ,
                     16.80258534,  16.83030762],
                   [ 16.80941106,  16.78119352,  16.7532136 , ...,  16.7532136 ,
                     16.78119352,  16.80941106],
                   ...,
                   [ 16.80941106,  16.78119352,  16.7532136 , ...,  16.7532136 ,
                     16.78119352,  16.80941106],
                   [ 16.83030762,  16.80258534,  16.7750965 , ...,  16.7750965 ,
                     16.80258534,  16.83030762],
                   [ 16.85108273,  16.82385283,  16.79685223, ...,  16.79685223,
                     16.82385283,  16.85108273]],
          
                  [[ 33.70216546,  33.64770567,  33.59370446, ...,  33.59370446,
                     33.64770567,  33.70216546],
                   [ 33.66061523,  33.60517068,  33.550193  , ...,  33.550193  ,
                     33.60517068,  33.66061523],
                   [ 33.61882211,  33.56238705,  33.50642719, ...,  33.50642719,
                     33.56238705,  33.61882211],
                   ...,
                   [ 33.61882211,  33.56238705,  33.50642719, ...,  33.50642719,
                     33.56238705,  33.61882211],
                   [ 33.66061523,  33.60517068,  33.550193  , ...,  33.550193  ,
                     33.60517068,  33.66061523],
                   [ 33.70216546,  33.64770567,  33.59370446, ...,  33.59370446,
                     33.64770567,  33.70216546]],
          
                  [[ 50.55324819,  50.4715585 ,  50.39055669, ...,  50.39055669,
                     50.4715585 ,  50.55324819],
                   [ 50.49092285,  50.40775602,  50.32528951, ...,  50.32528951,
                     50.40775602,  50.49092285],
                   [ 50.42823317,  50.34358057,  50.25964079, ...,  50.25964079,
                     50.34358057,  50.42823317],
                   ...,
                   [ 50.42823317,  50.34358057,  50.25964079, ...,  50.25964079,
                     50.34358057,  50.42823317],
                   [ 50.49092285,  50.40775602,  50.32528951, ...,  50.32528951,
                     50.40775602,  50.49092285],
                   [ 50.55324819,  50.4715585 ,  50.39055669, ...,  50.39055669,
                     50.4715585 ,  50.55324819]],
          
                  ...,
          
                  [[808.85197098, 807.54493599, 806.24890702, ..., 806.24890702,
                    807.54493599, 808.85197098],
                   [807.85476556, 806.52409631, 805.20463211, ..., 805.20463211,
                    806.52409631, 807.85476556],
                   [806.85173075, 805.4972891 , 804.15425266, ..., 804.15425266,
                    805.4972891 , 806.85173075],
                   ...,
                   [806.85173075, 805.4972891 , 804.15425266, ..., 804.15425266,
                    805.4972891 , 806.85173075],
                   [807.85476556, 806.52409631, 805.20463211, ..., 805.20463211,
                    806.52409631, 807.85476556],
                   [808.85197098, 807.54493599, 806.24890702, ..., 806.24890702,
                    807.54493599, 808.85197098]],
          
                  [[825.70305371, 824.36878882, 823.04575925, ..., 823.04575925,
                    824.36878882, 825.70305371],
                   [824.68507317, 823.32668165, 821.97972861, ..., 821.97972861,
                    823.32668165, 824.68507317],
                   [823.66114181, 822.27848262, 820.90746626, ..., 820.90746626,
                    822.27848262, 823.66114181],
                   ...,
                   [823.66114181, 822.27848262, 820.90746626, ..., 820.90746626,
                    822.27848262, 823.66114181],
                   [824.68507317, 823.32668165, 821.97972861, ..., 821.97972861,
                    823.32668165, 824.68507317],
                   [825.70305371, 824.36878882, 823.04575925, ..., 823.04575925,
                    824.36878882, 825.70305371]],
          
                  [[842.55413644, 841.19264165, 839.84261148, ..., 839.84261148,
                    841.19264165, 842.55413644],
                   [841.51538079, 840.12926699, 838.75482511, ..., 838.75482511,
                    840.12926699, 841.51538079],
                   [840.47055287, 839.05967615, 837.66067986, ..., 837.66067986,
                    839.05967615, 840.47055287],
                   ...,
                   [840.47055287, 839.05967615, 837.66067986, ..., 837.66067986,
                    839.05967615, 840.47055287],
                   [841.51538079, 840.12926699, 838.75482511, ..., 838.75482511,
                    840.12926699, 841.51538079],
                   [842.55413644, 841.19264165, 839.84261148, ..., 839.84261148,
                    841.19264165, 842.55413644]]],
          
          
                 [[[ 18.72342525,  18.69316981,  18.66316914, ...,  18.66316914,
                     18.69316981,  18.72342525],
                   [ 18.7003418 ,  18.66953927,  18.63899611, ...,  18.63899611,
                     18.66953927,  18.7003418 ],
                   [ 18.6771234 ,  18.64577058,  18.61468177, ...,  18.61468177,
                     18.64577058,  18.6771234 ],
                   ...,
                   [ 18.6771234 ,  18.64577058,  18.61468177, ...,  18.61468177,
                     18.64577058,  18.6771234 ],
                   [ 18.7003418 ,  18.66953927,  18.63899611, ...,  18.63899611,
                     18.66953927,  18.7003418 ],
                   [ 18.72342525,  18.69316981,  18.66316914, ...,  18.66316914,
                     18.69316981,  18.72342525]],
          
                  [[ 37.44685051,  37.38633963,  37.32633829, ...,  37.32633829,
                     37.38633963,  37.44685051],
                   [ 37.40068359,  37.33907853,  37.27799223, ...,  37.27799223,
                     37.33907853,  37.40068359],
                   [ 37.35424679,  37.29154116,  37.22936355, ...,  37.22936355,
                     37.29154116,  37.35424679],
                   ...,
                   [ 37.35424679,  37.29154116,  37.22936355, ...,  37.22936355,
                     37.29154116,  37.35424679],
                   [ 37.40068359,  37.33907853,  37.27799223, ...,  37.27799223,
                     37.33907853,  37.40068359],
                   [ 37.44685051,  37.38633963,  37.32633829, ...,  37.32633829,
                     37.38633963,  37.44685051]],
          
                  [[ 56.17027576,  56.07950944,  55.98950743, ...,  55.98950743,
                     56.07950944,  56.17027576],
                   [ 56.10102539,  56.0086178 ,  55.91698834, ...,  55.91698834,
                     56.0086178 ,  56.10102539],
                   [ 56.03137019,  55.93731174,  55.84404532, ...,  55.84404532,
                     55.93731174,  56.03137019],
                   ...,
                   [ 56.03137019,  55.93731174,  55.84404532, ...,  55.84404532,
                     55.93731174,  56.03137019],
                   [ 56.10102539,  56.0086178 ,  55.91698834, ...,  55.91698834,
                     56.0086178 ,  56.10102539],
                   [ 56.17027576,  56.07950944,  55.98950743, ...,  55.98950743,
                     56.07950944,  56.17027576]],
          
                  ...,
          
                  [[898.7244122 , 897.2721511 , 895.83211891, ..., 895.83211891,
                    897.2721511 , 898.7244122 ],
                   [897.61640617, 896.13788479, 894.67181346, ..., 894.67181346,
                    896.13788479, 897.61640617],
                   [896.50192306, 894.99698789, 893.50472518, ..., 893.50472518,
                    894.99698789, 896.50192306],
                   ...,
                   [896.50192306, 894.99698789, 893.50472518, ..., 893.50472518,
                    894.99698789, 896.50192306],
                   [897.61640617, 896.13788479, 894.67181346, ..., 894.67181346,
                    896.13788479, 897.61640617],
                   [898.7244122 , 897.2721511 , 895.83211891, ..., 895.83211891,
                    897.2721511 , 898.7244122 ]],
          
                  [[917.44783745, 915.96532091, 914.49528806, ..., 914.49528806,
                    915.96532091, 917.44783745],
                   [916.31674797, 914.80742406, 913.31080957, ..., 913.31080957,
                    914.80742406, 916.31674797],
                   [915.17904646, 913.64275847, 912.11940696, ..., 912.11940696,
                    913.64275847, 915.17904646],
                   ...,
                   [915.17904646, 913.64275847, 912.11940696, ..., 912.11940696,
                    913.64275847, 915.17904646],
                   [916.31674797, 914.80742406, 913.31080957, ..., 913.31080957,
                    914.80742406, 916.31674797],
                   [917.44783745, 915.96532091, 914.49528806, ..., 914.49528806,
                    915.96532091, 917.44783745]],
          
                  [[936.17126271, 934.65849073, 933.1584572 , ..., 933.1584572 ,
                    934.65849073, 936.17126271],
                   [935.01708976, 933.47696332, 931.94980568, ..., 931.94980568,
                    933.47696333, 935.01708976],
                   [933.85616985, 932.28852905, 930.73408873, ..., 930.73408873,
                    932.28852905, 933.85616985],
                   ...,
                   [933.85616985, 932.28852905, 930.73408873, ..., 930.73408873,
                    932.28852905, 933.85616985],
                   [935.01708976, 933.47696332, 931.94980568, ..., 931.94980568,
                    933.47696333, 935.01708976],
                   [936.17126271, 934.65849073, 933.1584572 , ..., 933.1584572 ,
                    934.65849073, 936.17126271]]]])

      It is almost 1GB!

      [5]:
      
      ds_in["data4D"].nbytes / 1e9  # Byte -> GB
      
      [5]:
      
      0.96
      

      The data itself is not too interesting… We only focus on performance here.

      [6]:
      
      plt.figure(figsize=[12, 3])
      
      plt.subplot(121)
      ds_in["data4D"].isel(time=0, lev=0).plot()
      plt.title("2D field")
      
      plt.subplot(122)
      ds_in["data4D"].mean(dim=["x", "y"]).plot()
      plt.title("extra dimensions to test broadcasting")
      
      [6]:
      
      Text(0.5, 1.0, 'extra dimensions to test broadcasting')
      
      ../_images/notebooks_Reuse_regridder_12_1.png

      Build Regridder

      Making a bilinear regridder takes ~7s on my Mac! ('conservative' would take even longer. Try it yourself.)

      [7]:
      
      %%time
      regridder = xe.Regridder(ds_in, ds_out, 'bilinear')
      
      CPU times: user 4.19 s, sys: 228 ms, total: 4.41 s
      Wall time: 4.43 s
      
      [8]:
      
      regridder
      
      [8]:
      
      xESMF Regridder
      Regridding algorithm:       bilinear
      Input grid shape:           (400, 600)
      Output grid shape:          (300, 400)
      Output grid dimension name: ('y', 'x')
      Periodic in longitude?      False
      

      Apply regridding

      However, applying the regridder to 1GB of data only takes ~0.5s

      [9]:
      
      %%time
      dr_out = regridder(ds_in['data4D'])
      
      CPU times: user 408 ms, sys: 205 ms, total: 613 ms
      Wall time: 612 ms
      

      Why applying regridding is so fast?

      Most regridding algorithms (including all 5 algorithms in ESMF) are linear, i.e. the output data field is linearly dependent on the input data field. Any linear transform can be viewed as a matrix-vector multiplication \(y = Ax\), where \(A\) is a matrix containing regridding weights, and \(x\), \(y\) are input and output data fields flatten to 1D.

      Computing the weight matrix \(A\) is expensive, but \(A\) only depends on input and output grids, not on input data. That means we can use the same \(A\) on different input fields \(x\), as long as the grid structure is not changed.

      An xESMF regridder has an attribute weights, i.e. the weight matrix.

      [10]:
      
      regridder.weights
      
      [10]:
      
      <120000x240000 sparse matrix of type '<class 'numpy.float64'>'
              with 480000 stored elements in COOrdinate format>
      

      It is typically very sparse, because a single destination point will only receive contribution from a small number of source points.

      [11]:
      
      plt.spy(regridder.weights)
      plt.xlabel("input grid indices")
      plt.ylabel("output grid indices")
      
      [11]:
      
      Text(0, 0.5, 'output grid indices')
      
      ../_images/notebooks_Reuse_regridder_26_1.png

      Retrieve regridder

      To avoid recomputing the same weights later, you can write the weights to disk and reload them later. First write the weights using the to_netcdf method. A default file name will be chosen if none is given.

      [12]:
      
      fn = regridder.to_netcdf()
      print(fn)
      
      bilinear_400x600_300x400.nc
      
      [13]:
      
      %%bash
      du -sh bilinear_400x600_300x400.nc
      
      7.4M    bilinear_400x600_300x400.nc
      

      When you open the notebook next time, simply set the weights argument to the path to the netCDF file. The weight file is typically pretty small (due to sparsity), so reading it is almost instantaneous.

      [14]:
      
      %%time
      regridder2 = xe.Regridder(ds_in, ds_out, 'bilinear', weights=fn)
      
      CPU times: user 33.7 ms, sys: 2.3 ms, total: 36 ms
      Wall time: 33.1 ms
      

      The second-step, applying those weights to data, is just a matrix multiplication \(y=Ax\). With highly-optimized sparse matrix multiplication library, it is blazingly fast.

      [15]:
      
      %%time
      dr_out2 = regridder2(ds_in['data4D'])
      
      CPU times: user 739 ms, sys: 191 ms, total: 929 ms
      Wall time: 926 ms
      

      The retrieved regridder gives the same result as the first regridder.

      [16]:
      
      xr.testing.assert_identical(dr_out, dr_out2)  # they are equal
      

      For even larger grids, you might spend several minutes computing the weights. But once they are computed, you don’t have to do it again.