Using xesmf to efficiently regrid data to another resolution#
Annette Hirsch, CLEX CMS
Often we want to compare to datasets but find that they are on different grids and first require to be interpolated to a common grid.
There exist a number of different tools for doing this however in this post we’ll talk about the regridding tools from xesmf.
First load the relevant packages
import xarray as xr
import xesmf as xe
import numpy as np
Open the data. In this example, we will open a monthly surface temperature dataset from the ACCESS-1.3 CMIP5 model.
url = 'http://dapds00.nci.org.au/thredds/dodsC/rr3/CMIP5/output1/CSIRO-BOM/ACCESS1-3/historical/mon/atmos/Amon/r1i1p1/latest/tas/tas_Amon_ACCESS1-3_historical_r1i1p1_185001-200512.nc'
ds = xr.open_dataset(url)
ds
<xarray.Dataset> Dimensions: (bnds: 2, lat: 145, lon: 192, time: 1872) Coordinates: * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2005-12-16T12:00:00 * lat (lat) float64 -90.0 -88.75 -87.5 -86.25 ... 86.25 87.5 88.75 90.0 * lon (lon) float64 0.0 1.875 3.75 5.625 ... 352.5 354.4 356.2 358.1 height float64 1.5 Dimensions without coordinates: bnds Data variables: time_bnds (time, bnds) datetime64[ns] 1850-01-01 1850-02-01 ... 2006-01-01 lat_bnds (lat, bnds) float64 -90.0 -89.38 -89.38 ... 89.38 89.38 90.0 lon_bnds (lon, bnds) float64 -0.9375 0.9375 0.9375 ... 357.2 357.2 359.1 tas (time, lat, lon) float32 ... Attributes: (12/29) institution: CSIRO (Commonwealth Scientific and Indus... institute_id: CSIRO-BOM experiment_id: historical source: ACCESS1-3 2011. Atmosphere: AGCM v1.0 (N... model_id: ACCESS1.3 forcing: GHG, Oz, SA, Sl, Vl, BC, OC, (GHG = CO2,... ... ... title: ACCESS1-3 model output prepared for CMIP... parent_experiment: pre-industrial control modeling_realm: atmos realization: 1 cmor_version: 2.8.0 DODS_EXTRA.Unlimited_Dimension: time
- bnds: 2
- lat: 145
- lon: 192
- time: 1872
- time(time)datetime64[ns]1850-01-16T12:00:00 ... 2005-12-...
- bounds :
- time_bnds
- axis :
- T
- long_name :
- time
- standard_name :
- time
array(['1850-01-16T12:00:00.000000000', '1850-02-15T00:00:00.000000000', '1850-03-16T12:00:00.000000000', ..., '2005-10-16T12:00:00.000000000', '2005-11-16T00:00:00.000000000', '2005-12-16T12:00:00.000000000'], dtype='datetime64[ns]')
- lat(lat)float64-90.0 -88.75 -87.5 ... 88.75 90.0
- bounds :
- lat_bnds
- units :
- degrees_north
- axis :
- Y
- long_name :
- latitude
- standard_name :
- latitude
array([-90. , -88.75, -87.5 , -86.25, -85. , -83.75, -82.5 , -81.25, -80. , -78.75, -77.5 , -76.25, -75. , -73.75, -72.5 , -71.25, -70. , -68.75, -67.5 , -66.25, -65. , -63.75, -62.5 , -61.25, -60. , -58.75, -57.5 , -56.25, -55. , -53.75, -52.5 , -51.25, -50. , -48.75, -47.5 , -46.25, -45. , -43.75, -42.5 , -41.25, -40. , -38.75, -37.5 , -36.25, -35. , -33.75, -32.5 , -31.25, -30. , -28.75, -27.5 , -26.25, -25. , -23.75, -22.5 , -21.25, -20. , -18.75, -17.5 , -16.25, -15. , -13.75, -12.5 , -11.25, -10. , -8.75, -7.5 , -6.25, -5. , -3.75, -2.5 , -1.25, 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. , 11.25, 12.5 , 13.75, 15. , 16.25, 17.5 , 18.75, 20. , 21.25, 22.5 , 23.75, 25. , 26.25, 27.5 , 28.75, 30. , 31.25, 32.5 , 33.75, 35. , 36.25, 37.5 , 38.75, 40. , 41.25, 42.5 , 43.75, 45. , 46.25, 47.5 , 48.75, 50. , 51.25, 52.5 , 53.75, 55. , 56.25, 57.5 , 58.75, 60. , 61.25, 62.5 , 63.75, 65. , 66.25, 67.5 , 68.75, 70. , 71.25, 72.5 , 73.75, 75. , 76.25, 77.5 , 78.75, 80. , 81.25, 82.5 , 83.75, 85. , 86.25, 87.5 , 88.75, 90. ])
- lon(lon)float640.0 1.875 3.75 ... 356.2 358.1
- bounds :
- lon_bnds
- units :
- degrees_east
- axis :
- X
- long_name :
- longitude
- standard_name :
- longitude
array([ 0. , 1.875, 3.75 , 5.625, 7.5 , 9.375, 11.25 , 13.125, 15. , 16.875, 18.75 , 20.625, 22.5 , 24.375, 26.25 , 28.125, 30. , 31.875, 33.75 , 35.625, 37.5 , 39.375, 41.25 , 43.125, 45. , 46.875, 48.75 , 50.625, 52.5 , 54.375, 56.25 , 58.125, 60. , 61.875, 63.75 , 65.625, 67.5 , 69.375, 71.25 , 73.125, 75. , 76.875, 78.75 , 80.625, 82.5 , 84.375, 86.25 , 88.125, 90. , 91.875, 93.75 , 95.625, 97.5 , 99.375, 101.25 , 103.125, 105. , 106.875, 108.75 , 110.625, 112.5 , 114.375, 116.25 , 118.125, 120. , 121.875, 123.75 , 125.625, 127.5 , 129.375, 131.25 , 133.125, 135. , 136.875, 138.75 , 140.625, 142.5 , 144.375, 146.25 , 148.125, 150. , 151.875, 153.75 , 155.625, 157.5 , 159.375, 161.25 , 163.125, 165. , 166.875, 168.75 , 170.625, 172.5 , 174.375, 176.25 , 178.125, 180. , 181.875, 183.75 , 185.625, 187.5 , 189.375, 191.25 , 193.125, 195. , 196.875, 198.75 , 200.625, 202.5 , 204.375, 206.25 , 208.125, 210. , 211.875, 213.75 , 215.625, 217.5 , 219.375, 221.25 , 223.125, 225. , 226.875, 228.75 , 230.625, 232.5 , 234.375, 236.25 , 238.125, 240. , 241.875, 243.75 , 245.625, 247.5 , 249.375, 251.25 , 253.125, 255. , 256.875, 258.75 , 260.625, 262.5 , 264.375, 266.25 , 268.125, 270. , 271.875, 273.75 , 275.625, 277.5 , 279.375, 281.25 , 283.125, 285. , 286.875, 288.75 , 290.625, 292.5 , 294.375, 296.25 , 298.125, 300. , 301.875, 303.75 , 305.625, 307.5 , 309.375, 311.25 , 313.125, 315. , 316.875, 318.75 , 320.625, 322.5 , 324.375, 326.25 , 328.125, 330. , 331.875, 333.75 , 335.625, 337.5 , 339.375, 341.25 , 343.125, 345. , 346.875, 348.75 , 350.625, 352.5 , 354.375, 356.25 , 358.125])
- height()float64...
- units :
- m
- axis :
- Z
- positive :
- up
- long_name :
- height
- standard_name :
- height
array(1.5)
- time_bnds(time, bnds)datetime64[ns]...
array([['1850-01-01T00:00:00.000000000', '1850-02-01T00:00:00.000000000'], ['1850-02-01T00:00:00.000000000', '1850-03-01T00:00:00.000000000'], ['1850-03-01T00:00:00.000000000', '1850-04-01T00:00:00.000000000'], ..., ['2005-10-01T00:00:00.000000000', '2005-11-01T00:00:00.000000000'], ['2005-11-01T00:00:00.000000000', '2005-12-01T00:00:00.000000000'], ['2005-12-01T00:00:00.000000000', '2006-01-01T00:00:00.000000000']], dtype='datetime64[ns]')
- lat_bnds(lat, bnds)float64...
array([[-90. , -89.375], [-89.375, -88.125], [-88.125, -86.875], ..., [ 86.875, 88.125], [ 88.125, 89.375], [ 89.375, 90. ]])
- lon_bnds(lon, bnds)float64...
array([[ -0.9375, 0.9375], [ 0.9375, 2.8125], [ 2.8125, 4.6875], ..., [353.4375, 355.3125], [355.3125, 357.1875], [357.1875, 359.0625]])
- tas(time, lat, lon)float32...
- standard_name :
- air_temperature
- long_name :
- Near-Surface Air Temperature
- units :
- K
- cell_methods :
- time: mean
- cell_measures :
- area: areacella
- history :
- 2012-02-05T23:49:51Z altered by CMOR: Treated scalar dimension: 'height'. 2012-02-05T23:49:51Z altered by CMOR: replaced missing value flag (-1.07374e+09) with standard missing value (1e+20).
- associated_files :
- baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_ACCESS1-3_historical_r0i0p0.nc areacella: areacella_fx_ACCESS1-3_historical_r0i0p0.nc
[52116480 values with dtype=float32]
- institution :
- CSIRO (Commonwealth Scientific and Industrial Research Organisation, Australia), and BOM (Bureau of Meteorology, Australia)
- institute_id :
- CSIRO-BOM
- experiment_id :
- historical
- source :
- ACCESS1-3 2011. Atmosphere: AGCM v1.0 (N96 grid-point, 1.875 degrees EW x approx 1.25 degree NS, 38 levels); ocean: NOAA/GFDL MOM4p1 (nominal 1.0 degree EW x 1.0 degrees NS, tripolar north of 65N, equatorial refinement to 1/3 degree from 10S to 10 N, cosine dependent NS south of 25S, 50 levels); sea ice: CICE4.1 (nominal 1.0 degree EW x 1.0 degrees NS, tripolar north of 65N, equatorial refinement to 1/3 degree from 10S to 10 N, cosine dependent NS south of 25S); land: CABLE1.0 (1.875 degree EW x 1.25 degree NS, 6 levels
- model_id :
- ACCESS1.3
- forcing :
- GHG, Oz, SA, Sl, Vl, BC, OC, (GHG = CO2, N2O, CH4, CFC11, CFC12, CFC113, HCFC22, HFC125, HFC134a)
- parent_experiment_id :
- piControl
- parent_experiment_rip :
- r1i1p1
- branch_time :
- 90945.0
- contact :
- The ACCESS wiki: http://wiki.csiro.au/confluence/display/ACCESS/Home. Contact Tony.Hirst@csiro.au regarding the ACCESS coupled climate model. Contact Peter.Uhe@csiro.au regarding ACCESS coupled climate model CMIP5 datasets.
- history :
- Fri Apr 13 09:38:12 2012: ncatted -a forcing,global,m,c,GHG, Oz, SA, Sl, Vl, BC, OC, (GHG = CO2, N2O, CH4, CFC11, CFC12, CFC113, HCFC22, HFC125, HFC134a) /projects/p66/pfu599/CMIP5/output/CSIRO-BOM/ACCESS1-3/historical/mon/atmos/tas/r1i1p1/tas_Amon_ACCESS1-3_historical_r1i1p1_185001-200512.nc CMIP5 compliant file produced from raw ACCESS model output using the ACCESS Post-Processor and CMOR2. 2012-02-05T23:50:03Z CMOR rewrote data to comply with CF standards and CMIP5 requirements. Fri Apr 13 12:13:13 2012: updated version number to v20120413. Fri Apr 13 12:29:32 2012: corrected model_id from ACCESS1-3 to ACCESS1.3
- references :
- See http://wiki.csiro.au/confluence/display/ACCESS/ACCESS+Publications
- initialization_method :
- 1
- physics_version :
- 1
- tracking_id :
- 7f51888d-7daa-45b3-b568-9ce3288b333d
- version_number :
- v20120413
- product :
- output
- experiment :
- historical
- frequency :
- mon
- creation_date :
- 2012-02-05T23:50:03Z
- Conventions :
- CF-1.4
- project_id :
- CMIP5
- table_id :
- Table Amon (27 April 2011) 9c851218e3842df9a62ef38b1e2575bb
- title :
- ACCESS1-3 model output prepared for CMIP5 historical
- parent_experiment :
- pre-industrial control
- modeling_realm :
- atmos
- realization :
- 1
- cmor_version :
- 2.8.0
- DODS_EXTRA.Unlimited_Dimension :
- time
For CMIP data the resolution may already be declared in the attributes of the file. If not, you can calculate this too:
(ds['lat'].max() - ds['lat'].min())/(ds['lat'].count()-1.)
<xarray.DataArray 'lat' ()> array(1.25) Coordinates: height float64 1.5
- 1.25
array(1.25)
- height()float641.5
- units :
- m
- axis :
- Z
- positive :
- up
- long_name :
- height
- standard_name :
- height
array(1.5)
(ds['lon'].max() - ds['lon'].min())/(ds['lon'].count()-1.)
<xarray.DataArray 'lon' ()> array(1.875) Coordinates: height float64 1.5
- 1.875
array(1.875)
- height()float641.5
- units :
- m
- axis :
- Z
- positive :
- up
- long_name :
- height
- standard_name :
- height
array(1.5)
So the original resolution is 1.25 degrees latitude by 1.875 degrees longitude
Say we want to regrid this to a 2.5 degree rectilinear grid, we can use the utils functions to define the new grid. Alternatively, if you want to regrid to the resolution of another dataset you can use a field directly from that dataset.
ds_out = xe.util.grid_2d(-180.0, 180.0, 2.5, -90.0, 90.0, 2.5)
ds_out
<xarray.Dataset> Dimensions: (x: 144, x_b: 145, y: 72, y_b: 73) Coordinates: lon (y, x) float64 -178.8 -176.2 -173.8 -171.2 ... 173.8 176.2 178.8 lat (y, x) float64 -88.75 -88.75 -88.75 -88.75 ... 88.75 88.75 88.75 lon_b (y_b, x_b) float64 -180.0 -177.5 -175.0 ... 175.0 177.5 180.0 lat_b (y_b, x_b) float64 -90.0 -90.0 -90.0 -90.0 ... 90.0 90.0 90.0 90.0 Dimensions without coordinates: x, x_b, y, y_b Data variables: *empty*
- x: 144
- x_b: 145
- y: 72
- y_b: 73
- lon(y, x)float64-178.8 -176.2 ... 176.2 178.8
- standard_name :
- longitude
array([[-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], ..., [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75]])
- lat(y, x)float64-88.75 -88.75 ... 88.75 88.75
- standard_name :
- latitude
array([[-88.75, -88.75, -88.75, ..., -88.75, -88.75, -88.75], [-86.25, -86.25, -86.25, ..., -86.25, -86.25, -86.25], [-83.75, -83.75, -83.75, ..., -83.75, -83.75, -83.75], ..., [ 83.75, 83.75, 83.75, ..., 83.75, 83.75, 83.75], [ 86.25, 86.25, 86.25, ..., 86.25, 86.25, 86.25], [ 88.75, 88.75, 88.75, ..., 88.75, 88.75, 88.75]])
- lon_b(y_b, x_b)float64-180.0 -177.5 ... 177.5 180.0
array([[-180. , -177.5, -175. , ..., 175. , 177.5, 180. ], [-180. , -177.5, -175. , ..., 175. , 177.5, 180. ], [-180. , -177.5, -175. , ..., 175. , 177.5, 180. ], ..., [-180. , -177.5, -175. , ..., 175. , 177.5, 180. ], [-180. , -177.5, -175. , ..., 175. , 177.5, 180. ], [-180. , -177.5, -175. , ..., 175. , 177.5, 180. ]])
- lat_b(y_b, x_b)float64-90.0 -90.0 -90.0 ... 90.0 90.0
array([[-90. , -90. , -90. , ..., -90. , -90. , -90. ], [-87.5, -87.5, -87.5, ..., -87.5, -87.5, -87.5], [-85. , -85. , -85. , ..., -85. , -85. , -85. ], ..., [ 85. , 85. , 85. , ..., 85. , 85. , 85. ], [ 87.5, 87.5, 87.5, ..., 87.5, 87.5, 87.5], [ 90. , 90. , 90. , ..., 90. , 90. , 90. ]])
Then we build the regridder. In this first instance, the weights are not saved to netcdf. Note that there are a few options for the regridding method. As we are working with a global dataset, we use periodic=True
so that we do not get gaps along the central longitude
regridder = xe.Regridder(ds, ds_out, 'bilinear', periodic=True)
regridder
xESMF Regridder
Regridding algorithm: bilinear
Weight filename: bilinear_145x192_72x144_peri.nc
Reuse pre-computed weights? False
Input grid shape: (145, 192)
Output grid shape: (72, 144)
Periodic in longitude? True
As this file contains near-surface air temperature we’ll define this separately.
ds['tas']
<xarray.DataArray 'tas' (time: 1872, lat: 145, lon: 192)> [52116480 values with dtype=float32] Coordinates: * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2005-12-16T12:00:00 * lat (lat) float64 -90.0 -88.75 -87.5 -86.25 ... 86.25 87.5 88.75 90.0 * lon (lon) float64 0.0 1.875 3.75 5.625 7.5 ... 352.5 354.4 356.2 358.1 height float64 1.5 Attributes: standard_name: air_temperature long_name: Near-Surface Air Temperature units: K cell_methods: time: mean cell_measures: area: areacella history: 2012-02-05T23:49:51Z altered by CMOR: Treated scalar d... associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation...
- time: 1872
- lat: 145
- lon: 192
- ...
[52116480 values with dtype=float32]
- time(time)datetime64[ns]1850-01-16T12:00:00 ... 2005-12-...
- bounds :
- time_bnds
- axis :
- T
- long_name :
- time
- standard_name :
- time
array(['1850-01-16T12:00:00.000000000', '1850-02-15T00:00:00.000000000', '1850-03-16T12:00:00.000000000', ..., '2005-10-16T12:00:00.000000000', '2005-11-16T00:00:00.000000000', '2005-12-16T12:00:00.000000000'], dtype='datetime64[ns]')
- lat(lat)float64-90.0 -88.75 -87.5 ... 88.75 90.0
- bounds :
- lat_bnds
- units :
- degrees_north
- axis :
- Y
- long_name :
- latitude
- standard_name :
- latitude
array([-90. , -88.75, -87.5 , -86.25, -85. , -83.75, -82.5 , -81.25, -80. , -78.75, -77.5 , -76.25, -75. , -73.75, -72.5 , -71.25, -70. , -68.75, -67.5 , -66.25, -65. , -63.75, -62.5 , -61.25, -60. , -58.75, -57.5 , -56.25, -55. , -53.75, -52.5 , -51.25, -50. , -48.75, -47.5 , -46.25, -45. , -43.75, -42.5 , -41.25, -40. , -38.75, -37.5 , -36.25, -35. , -33.75, -32.5 , -31.25, -30. , -28.75, -27.5 , -26.25, -25. , -23.75, -22.5 , -21.25, -20. , -18.75, -17.5 , -16.25, -15. , -13.75, -12.5 , -11.25, -10. , -8.75, -7.5 , -6.25, -5. , -3.75, -2.5 , -1.25, 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. , 11.25, 12.5 , 13.75, 15. , 16.25, 17.5 , 18.75, 20. , 21.25, 22.5 , 23.75, 25. , 26.25, 27.5 , 28.75, 30. , 31.25, 32.5 , 33.75, 35. , 36.25, 37.5 , 38.75, 40. , 41.25, 42.5 , 43.75, 45. , 46.25, 47.5 , 48.75, 50. , 51.25, 52.5 , 53.75, 55. , 56.25, 57.5 , 58.75, 60. , 61.25, 62.5 , 63.75, 65. , 66.25, 67.5 , 68.75, 70. , 71.25, 72.5 , 73.75, 75. , 76.25, 77.5 , 78.75, 80. , 81.25, 82.5 , 83.75, 85. , 86.25, 87.5 , 88.75, 90. ])
- lon(lon)float640.0 1.875 3.75 ... 356.2 358.1
- bounds :
- lon_bnds
- units :
- degrees_east
- axis :
- X
- long_name :
- longitude
- standard_name :
- longitude
array([ 0. , 1.875, 3.75 , 5.625, 7.5 , 9.375, 11.25 , 13.125, 15. , 16.875, 18.75 , 20.625, 22.5 , 24.375, 26.25 , 28.125, 30. , 31.875, 33.75 , 35.625, 37.5 , 39.375, 41.25 , 43.125, 45. , 46.875, 48.75 , 50.625, 52.5 , 54.375, 56.25 , 58.125, 60. , 61.875, 63.75 , 65.625, 67.5 , 69.375, 71.25 , 73.125, 75. , 76.875, 78.75 , 80.625, 82.5 , 84.375, 86.25 , 88.125, 90. , 91.875, 93.75 , 95.625, 97.5 , 99.375, 101.25 , 103.125, 105. , 106.875, 108.75 , 110.625, 112.5 , 114.375, 116.25 , 118.125, 120. , 121.875, 123.75 , 125.625, 127.5 , 129.375, 131.25 , 133.125, 135. , 136.875, 138.75 , 140.625, 142.5 , 144.375, 146.25 , 148.125, 150. , 151.875, 153.75 , 155.625, 157.5 , 159.375, 161.25 , 163.125, 165. , 166.875, 168.75 , 170.625, 172.5 , 174.375, 176.25 , 178.125, 180. , 181.875, 183.75 , 185.625, 187.5 , 189.375, 191.25 , 193.125, 195. , 196.875, 198.75 , 200.625, 202.5 , 204.375, 206.25 , 208.125, 210. , 211.875, 213.75 , 215.625, 217.5 , 219.375, 221.25 , 223.125, 225. , 226.875, 228.75 , 230.625, 232.5 , 234.375, 236.25 , 238.125, 240. , 241.875, 243.75 , 245.625, 247.5 , 249.375, 251.25 , 253.125, 255. , 256.875, 258.75 , 260.625, 262.5 , 264.375, 266.25 , 268.125, 270. , 271.875, 273.75 , 275.625, 277.5 , 279.375, 281.25 , 283.125, 285. , 286.875, 288.75 , 290.625, 292.5 , 294.375, 296.25 , 298.125, 300. , 301.875, 303.75 , 305.625, 307.5 , 309.375, 311.25 , 313.125, 315. , 316.875, 318.75 , 320.625, 322.5 , 324.375, 326.25 , 328.125, 330. , 331.875, 333.75 , 335.625, 337.5 , 339.375, 341.25 , 343.125, 345. , 346.875, 348.75 , 350.625, 352.5 , 354.375, 356.25 , 358.125])
- height()float641.5
- units :
- m
- axis :
- Z
- positive :
- up
- long_name :
- height
- standard_name :
- height
array(1.5)
- standard_name :
- air_temperature
- long_name :
- Near-Surface Air Temperature
- units :
- K
- cell_methods :
- time: mean
- cell_measures :
- area: areacella
- history :
- 2012-02-05T23:49:51Z altered by CMOR: Treated scalar dimension: 'height'. 2012-02-05T23:49:51Z altered by CMOR: replaced missing value flag (-1.07374e+09) with standard missing value (1e+20).
- associated_files :
- baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_ACCESS1-3_historical_r0i0p0.nc areacella: areacella_fx_ACCESS1-3_historical_r0i0p0.nc
Apply the regridder to the data
tas_25deg = regridder(ds['tas'])
/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xesmf/frontend.py:464: FutureWarning: ``output_sizes`` should be given in the ``dask_gufunc_kwargs`` parameter. It will be removed as direct parameter in a future version.
dr_out = xr.apply_ufunc(
tas_25deg
<xarray.DataArray 'tas' (time: 1872, y: 72, x: 144)> array([[[241.42548074, 241.46530151, 241.51575264, ..., 241.29264361, 241.3331604 , 241.37937428], [249.29878775, 249.49865723, 249.70174442, ..., 248.53185656, 248.81828308, 249.07307976], [258.90998768, 259.34362793, 259.62011489, ..., 257.00554583, 257.77041626, 258.39012844], ..., [240.0256449 , 240.15310669, 240.28757747, ..., 239.80319719, 239.92158508, 239.9827772 ], [239.95589676, 239.94976807, 239.9454768 , ..., 239.91784213, 239.93392944, 239.96497476], [239.91920767, 239.95617676, 239.98541397, ..., 239.76462219, 239.8212738 , 239.87550099]], [[235.35084386, 235.43763733, 235.53132127, ..., 235.12648029, 235.19128418, 235.26917861], [247.80141914, 248.36372375, 248.88822159, ..., 246.27575141, 246.73565674, 247.26041022], [257.63035803, 257.96582031, 258.17488678, ..., 256.06124283, 256.6065979 , 257.11943182], ... [250.55788953, 250.56117249, 250.62096828, ..., 251.29435661, 250.8780365 , 250.63096473], [252.6061043 , 252.39373779, 252.23463555, ..., 253.47047825, 253.12278748, 252.8264588 ], [254.88175595, 254.8631897 , 254.86619789, ..., 255.01445768, 254.97032166, 254.92613684]], [[246.75074031, 246.7674408 , 246.81818953, ..., 246.66889692, 246.68983459, 246.7283168 ], [253.50811955, 253.73731995, 253.98962857, ..., 252.68888363, 252.96813965, 253.25622345], [262.74184175, 263.19360352, 263.53787576, ..., 261.00175357, 261.69522095, 262.25131706], ..., [245.47192229, 245.0090332 , 244.5782707 , ..., 246.80896796, 246.32383728, 245.88781399], [246.79688785, 246.40936279, 246.01888102, ..., 247.81054793, 247.51338196, 247.15659795], [245.98595642, 245.89811707, 245.81729825, ..., 246.27256695, 246.18641663, 246.08195717]]]) Coordinates: * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2005-12-16T12:00:00 height float64 1.5 lon (y, x) float64 -178.8 -176.2 -173.8 -171.2 ... 173.8 176.2 178.8 lat (y, x) float64 -88.75 -88.75 -88.75 -88.75 ... 88.75 88.75 88.75 Dimensions without coordinates: y, x Attributes: regrid_method: bilinear
- time: 1872
- y: 72
- x: 144
- 241.4 241.5 241.5 241.6 241.6 241.7 ... 246.5 246.4 246.3 246.2 246.1
array([[[241.42548074, 241.46530151, 241.51575264, ..., 241.29264361, 241.3331604 , 241.37937428], [249.29878775, 249.49865723, 249.70174442, ..., 248.53185656, 248.81828308, 249.07307976], [258.90998768, 259.34362793, 259.62011489, ..., 257.00554583, 257.77041626, 258.39012844], ..., [240.0256449 , 240.15310669, 240.28757747, ..., 239.80319719, 239.92158508, 239.9827772 ], [239.95589676, 239.94976807, 239.9454768 , ..., 239.91784213, 239.93392944, 239.96497476], [239.91920767, 239.95617676, 239.98541397, ..., 239.76462219, 239.8212738 , 239.87550099]], [[235.35084386, 235.43763733, 235.53132127, ..., 235.12648029, 235.19128418, 235.26917861], [247.80141914, 248.36372375, 248.88822159, ..., 246.27575141, 246.73565674, 247.26041022], [257.63035803, 257.96582031, 258.17488678, ..., 256.06124283, 256.6065979 , 257.11943182], ... [250.55788953, 250.56117249, 250.62096828, ..., 251.29435661, 250.8780365 , 250.63096473], [252.6061043 , 252.39373779, 252.23463555, ..., 253.47047825, 253.12278748, 252.8264588 ], [254.88175595, 254.8631897 , 254.86619789, ..., 255.01445768, 254.97032166, 254.92613684]], [[246.75074031, 246.7674408 , 246.81818953, ..., 246.66889692, 246.68983459, 246.7283168 ], [253.50811955, 253.73731995, 253.98962857, ..., 252.68888363, 252.96813965, 253.25622345], [262.74184175, 263.19360352, 263.53787576, ..., 261.00175357, 261.69522095, 262.25131706], ..., [245.47192229, 245.0090332 , 244.5782707 , ..., 246.80896796, 246.32383728, 245.88781399], [246.79688785, 246.40936279, 246.01888102, ..., 247.81054793, 247.51338196, 247.15659795], [245.98595642, 245.89811707, 245.81729825, ..., 246.27256695, 246.18641663, 246.08195717]]])
- time(time)datetime64[ns]1850-01-16T12:00:00 ... 2005-12-...
- bounds :
- time_bnds
- axis :
- T
- long_name :
- time
- standard_name :
- time
array(['1850-01-16T12:00:00.000000000', '1850-02-15T00:00:00.000000000', '1850-03-16T12:00:00.000000000', ..., '2005-10-16T12:00:00.000000000', '2005-11-16T00:00:00.000000000', '2005-12-16T12:00:00.000000000'], dtype='datetime64[ns]')
- height()float641.5
- units :
- m
- axis :
- Z
- positive :
- up
- long_name :
- height
- standard_name :
- height
array(1.5)
- lon(y, x)float64-178.8 -176.2 ... 176.2 178.8
- standard_name :
- longitude
array([[-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], ..., [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75], [-178.75, -176.25, -173.75, ..., 173.75, 176.25, 178.75]])
- lat(y, x)float64-88.75 -88.75 ... 88.75 88.75
- standard_name :
- latitude
array([[-88.75, -88.75, -88.75, ..., -88.75, -88.75, -88.75], [-86.25, -86.25, -86.25, ..., -86.25, -86.25, -86.25], [-83.75, -83.75, -83.75, ..., -83.75, -83.75, -83.75], ..., [ 83.75, 83.75, 83.75, ..., 83.75, 83.75, 83.75], [ 86.25, 86.25, 86.25, ..., 86.25, 86.25, 86.25], [ 88.75, 88.75, 88.75, ..., 88.75, 88.75, 88.75]])
- regrid_method :
- bilinear
Comparing the dimensions we have:
time |
lat |
lon |
|
---|---|---|---|
ds[‘tas’] |
1872 |
145 |
192 |
tas_25deg |
1872 |
72 |
144 |
ds['tas'].isel(time=0).plot()
<matplotlib.collections.QuadMesh at 0x7fdaa7c73f40>
tas_25deg.isel(time=0).plot()
<matplotlib.collections.QuadMesh at 0x7fda5f5c3a90>
To save the weights for later use, you’ll need to specify the filename to give to the weights file when you build the regridder for the first time:
regridder_save_weights = xe.Regridder(ds,ds_out,'bilinear',periodic=True,filename='bilinear_145x192_180x360_peri.nc')
regridder_save_weights
xESMF Regridder
Regridding algorithm: bilinear
Weight filename: bilinear_145x192_180x360_peri.nc
Reuse pre-computed weights? False
Input grid shape: (145, 192)
Output grid shape: (72, 144)
Periodic in longitude? True
This will save a file called bilinear_145x192_180x360_peri.nc into the directory you are running this notebook from.
To build the regridder using existing weights:
regridder_reuse_weights = xe.Regridder(ds,ds_out,'bilinear',periodic=True,reuse_weights=True,filename='bilinear_145x192_180x360_peri.nc')
regridder_reuse_weights
xESMF Regridder
Regridding algorithm: bilinear
Weight filename: bilinear_145x192_180x360_peri.nc
Reuse pre-computed weights? True
Input grid shape: (145, 192)
Output grid shape: (72, 144)
Periodic in longitude? True
Regridder will also work for 2D lat/lon that is common to curvilinear grids. In this case you’ll need to create a new xr.Dataset containing the 2D lat/lon values from the grid you want to interpolate to. Often this can be done by copying the lat/lon from an existing file.
If you want to learn more about the different options available within xesmf please check out: https://xesmf.readthedocs.io/en/latest/