Regridding

Regrid zonal wind data from one regular latitude-longitude grid to another.

import easyclimate as ecl
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np

Reading example raw grid data

u_data = ecl.tutorial.open_tutorial_dataset("uwnd_202201_mon_mean").sortby("lat").uwnd
u_data
<xarray.DataArray 'uwnd' (time: 2, level: 17, lat: 73, lon: 144)> Size: 1MB
[357408 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 16B 2022-01-01 2022-02-01
  * level    (level) float32 68B 1e+03 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0
  * lat      (lat) float32 292B -90.0 -87.5 -85.0 -82.5 ... 82.5 85.0 87.5 90.0
  * lon      (lon) float32 576B 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
Attributes:
    long_name:     Monthly mean u wind
    units:         m/s
    precision:     2
    var_desc:      u-wind
    level_desc:    Pressure Levels
    statistic:     Mean
    parent_stat:   Other
    dataset:       NCEP Reanalysis Derived Products
    actual_range:  [-68.194824 124.399994]


Define the target grid (only for latitude/longitude and regular grids)

target_grid = xr.DataArray(
    dims=("lat", "lon"),
    coords={
        "lat": np.arange(-89, 89, 6) + 1 / 1.0,
        "lon": np.arange(-180, 180, 6) + 1 / 1.0,
    },
)

easyclimate.interp.interp_mesh2mesh performs a regridding operation.

/home/docs/checkouts/easyclimate/checkouts/latest/src/easyclimate/interp/mesh2mesh.py:101: UserWarning: It seems that the input data longitude range is from -180° to 180°. Currently automatically converted to it from 0° to 360°.
  warnings.warn(
<xarray.DataArray 'uwnd' (time: 2, level: 17, lat: 30, lon: 60)> Size: 490kB
array([[[[ 5.60226874e-01,  1.02667854e+00,  1.49387228e+00, ...,
          -7.23611728e-01, -3.29127963e-01,  1.04678595e-01],
         [-7.13537854e-02,  4.89775393e-01,  1.05180766e+00, ...,
          -1.44522469e+00, -1.06899892e+00, -6.04192472e-01],
         [-3.99954723e+00, -3.65577349e+00, -3.40841787e+00, ...,
          -3.56099903e+00, -4.03412802e+00, -4.18725691e+00],
         ...,
         [ 4.90549424e-01,  6.82065852e-01,  8.77517420e-01, ...,
           3.63861390e+00,  1.77867857e+00,  7.16775250e-01],
         [-1.08903117e+00, -2.07193439e+00, -2.58209562e+00, ...,
           4.92451715e+00,  2.62419448e+00,  4.87097889e-01],
         [-3.48438617e+00, -4.12667664e+00, -4.65919235e+00, ...,
          -1.30906340e+00, -2.02451519e+00, -2.77096648e+00]],

        [[ 5.60516653e-01,  1.02732305e+00,  1.49006492e+00, ...,
          -7.03160787e-01, -3.10322073e-01,  1.14032799e-01],
         [-2.41967230e-01,  3.33032787e-01,  9.23710175e-01, ...,
          -1.57606402e+00, -1.21519297e+00, -7.65322161e-01],
         [-3.85738644e+00, -3.43561255e+00, -3.15858052e+00, ...,
          -3.72483816e+00, -4.09529016e+00, -4.14435425e+00],
...
           3.27103224e+01,  3.58181056e+01,  3.79991066e+01],
         [ 1.64507137e+01,  1.70246422e+01,  1.75105354e+01, ...,
           1.25291067e+01,  1.42987497e+01,  1.56053562e+01],
         [-1.19253563e+00, -6.11321535e-01, -3.92853737e-03, ...,
          -2.87689318e+00, -2.31967869e+00, -1.76107121e+00]],

        [[-2.16849996e+00, -2.24924998e+00, -2.28517835e+00, ...,
          -1.70846427e+00, -1.90010710e+00, -2.05642877e+00],
         [ 9.25357084e-01,  8.11392884e-01,  6.53821398e-01, ...,
           1.17457151e+00,  1.10617848e+00,  1.01874988e+00],
         [-2.32710742e+00, -2.31753611e+00, -2.39192836e+00, ...,
          -2.25328592e+00, -2.32342876e+00, -2.35374999e+00],
         ...,
         [ 4.42924988e+01,  4.69607846e+01,  4.87700726e+01, ...,
           3.43513564e+01,  3.74099994e+01,  4.09591072e+01],
         [ 1.97691078e+01,  2.20551815e+01,  2.40682140e+01, ...,
           1.48973213e+01,  1.59275005e+01,  1.75919628e+01],
         [-1.64439263e+00, -5.79463692e-01,  4.93000393e-01, ...,
          -4.43860670e+00, -3.60410727e+00, -2.66571401e+00]]]],
      shape=(2, 17, 30, 60))
Coordinates:
  * time     (time) datetime64[ns] 16B 2022-01-01 2022-02-01
  * level    (level) float32 68B 1e+03 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0
  * lat      (lat) float64 240B -88.0 -82.0 -76.0 -70.0 ... 68.0 74.0 80.0 86.0
  * lon      (lon) float64 480B 1.0 7.0 13.0 19.0 ... 337.0 343.0 349.0 355.0
Attributes:
    long_name:     Monthly mean u wind
    units:         m/s
    precision:     2
    var_desc:      u-wind
    level_desc:    Pressure Levels
    statistic:     Mean
    parent_stat:   Other
    dataset:       NCEP Reanalysis Derived Products
    actual_range:  [-68.194824 124.399994]


Plotting differences before and after interpolation

fig, ax = plt.subplots(1, 2, figsize=(12, 5))

u_data.sel(level=500).isel(time=0).plot(ax=ax[0])
ax[0].set_title("Before", size=20)

regriding_data.sel(level=500).isel(time=0).plot(ax=ax[1])
ax[1].set_title("After", size=20)
Before, After
Text(0.5, 1.0, 'After')

Total running time of the script: (0 minutes 1.143 seconds)