Note
Go to the end to download the full example code.
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
<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]xarray.DataArray
'uwnd'
- time: 2
- level: 17
- lat: 73
- lon: 144
- ...
[357408 values with dtype=float32]
- time(time)datetime64[ns]2022-01-01 2022-02-01
- standard_name :
- time
- long_name :
- Time
- axis :
- T
array(['2022-01-01T00:00:00.000000000', '2022-02-01T00:00:00.000000000'], dtype='datetime64[ns]') - level(level)float321e+03 925.0 850.0 ... 20.0 10.0
- standard_name :
- air_pressure
- long_name :
- Level
- units :
- millibar
- positive :
- down
- axis :
- Z
- GRIB_id :
- 100
- GRIB_name :
- hPa
- actual_range :
- [1000. 10.]
array([1000., 925., 850., 700., 600., 500., 400., 300., 250., 200., 150., 100., 70., 50., 30., 20., 10.], dtype=float32) - lat(lat)float32-90.0 -87.5 -85.0 ... 87.5 90.0
- standard_name :
- latitude
- long_name :
- Latitude
- units :
- degrees_north
- axis :
- Y
array([-90. , -87.5, -85. , -82.5, -80. , -77.5, -75. , -72.5, -70. , -67.5, -65. , -62.5, -60. , -57.5, -55. , -52.5, -50. , -47.5, -45. , -42.5, -40. , -37.5, -35. , -32.5, -30. , -27.5, -25. , -22.5, -20. , -17.5, -15. , -12.5, -10. , -7.5, -5. , -2.5, 0. , 2.5, 5. , 7.5, 10. , 12.5, 15. , 17.5, 20. , 22.5, 25. , 27.5, 30. , 32.5, 35. , 37.5, 40. , 42.5, 45. , 47.5, 50. , 52.5, 55. , 57.5, 60. , 62.5, 65. , 67.5, 70. , 72.5, 75. , 77.5, 80. , 82.5, 85. , 87.5, 90. ], dtype=float32) - lon(lon)float320.0 2.5 5.0 ... 352.5 355.0 357.5
- standard_name :
- longitude
- long_name :
- Longitude
- units :
- degrees_east
- axis :
- X
array([ 0. , 2.5, 5. , 7.5, 10. , 12.5, 15. , 17.5, 20. , 22.5, 25. , 27.5, 30. , 32.5, 35. , 37.5, 40. , 42.5, 45. , 47.5, 50. , 52.5, 55. , 57.5, 60. , 62.5, 65. , 67.5, 70. , 72.5, 75. , 77.5, 80. , 82.5, 85. , 87.5, 90. , 92.5, 95. , 97.5, 100. , 102.5, 105. , 107.5, 110. , 112.5, 115. , 117.5, 120. , 122.5, 125. , 127.5, 130. , 132.5, 135. , 137.5, 140. , 142.5, 145. , 147.5, 150. , 152.5, 155. , 157.5, 160. , 162.5, 165. , 167.5, 170. , 172.5, 175. , 177.5, 180. , 182.5, 185. , 187.5, 190. , 192.5, 195. , 197.5, 200. , 202.5, 205. , 207.5, 210. , 212.5, 215. , 217.5, 220. , 222.5, 225. , 227.5, 230. , 232.5, 235. , 237.5, 240. , 242.5, 245. , 247.5, 250. , 252.5, 255. , 257.5, 260. , 262.5, 265. , 267.5, 270. , 272.5, 275. , 277.5, 280. , 282.5, 285. , 287.5, 290. , 292.5, 295. , 297.5, 300. , 302.5, 305. , 307.5, 310. , 312.5, 315. , 317.5, 320. , 322.5, 325. , 327.5, 330. , 332.5, 335. , 337.5, 340. , 342.5, 345. , 347.5, 350. , 352.5, 355. , 357.5], dtype=float32)
- 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]xarray.DataArray
'uwnd'
- time: 2
- level: 17
- lat: 30
- lon: 60
- 0.5602 1.027 1.494 1.939 2.359 ... -5.719 -5.148 -4.439 -3.604 -2.666
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)) - time(time)datetime64[ns]2022-01-01 2022-02-01
- standard_name :
- time
- long_name :
- Time
- axis :
- T
array(['2022-01-01T00:00:00.000000000', '2022-02-01T00:00:00.000000000'], dtype='datetime64[ns]') - level(level)float321e+03 925.0 850.0 ... 20.0 10.0
- standard_name :
- air_pressure
- long_name :
- Level
- units :
- millibar
- positive :
- down
- axis :
- Z
- GRIB_id :
- 100
- GRIB_name :
- hPa
- actual_range :
- [1000. 10.]
array([1000., 925., 850., 700., 600., 500., 400., 300., 250., 200., 150., 100., 70., 50., 30., 20., 10.], dtype=float32) - lat(lat)float64-88.0 -82.0 -76.0 ... 80.0 86.0
- standard_name :
- latitude
- long_name :
- Latitude
- units :
- degrees_north
- axis :
- Y
array([-88., -82., -76., -70., -64., -58., -52., -46., -40., -34., -28., -22., -16., -10., -4., 2., 8., 14., 20., 26., 32., 38., 44., 50., 56., 62., 68., 74., 80., 86.]) - lon(lon)float641.0 7.0 13.0 ... 343.0 349.0 355.0
- standard_name :
- longitude
- long_name :
- Longitude
- units :
- degrees_east
- axis :
- X
array([ 1., 7., 13., 19., 25., 31., 37., 43., 49., 55., 61., 67., 73., 79., 85., 91., 97., 103., 109., 115., 121., 127., 133., 139., 145., 151., 157., 163., 169., 175., 181., 187., 193., 199., 205., 211., 217., 223., 229., 235., 241., 247., 253., 259., 265., 271., 277., 283., 289., 295., 301., 307., 313., 319., 325., 331., 337., 343., 349., 355.])
- 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)

Text(0.5, 1.0, 'After')
Total running time of the script: (0 minutes 1.143 seconds)