Skip to content

Applying ESMFRegridWeightsGen weights

Liam Bindle edited this page May 20, 2021 · 1 revision

Here is an example of how to apply an ESMF weights file.

from tqdm import tqdm
import numpy as np
import xarray as xr

order = "C"   # Kind of surprising, but the weights file is row-major

# Load input data
ds_in = xr.open_dataset('sresa1b_ncar_ccsm3-example.nc')
data_in = ds_in['tas'].values.flatten(order)

# Load weights file
weights = xr.open_dataset('sresa1b_to_c24_weights.nc')

# Regrid
data_out= np.zeros((6,24,24))
data_out = data_out.flatten(order)
for i in tqdm(range(weights.dims['n_s'])):
    row = weights.row[i].item()-1
    col = weights.col[i].item()-1
    S = weights.S[i].item()
    data_out[row] += S*data_in[col]

# Write output
ds_out = xr.open_dataset('c24_recv-my_data.nc')
ds_out["my_tas"] = ds_out["tas"].copy()
ds_out["my_tas"].values = np.reshape(data_out, (1, 6,24,24), order=order)
ds_out.to_netcdf('my_output.nc')
Clone this wiki locally