Project

General

Profile

Use CDO remapping weights in Python

Added by Nikolai Stulov almost 2 years ago

Hi all,

I would like to remap (128 x 128) regular grid in Lambert projection to a regular grid in latlon projection. I am perfectly able to do it using `cdo remapbil`.

However, I would like to incorporate it into my Python pipeline, so I would like to use `cdo genbil` and then work with the weights in Python like:
```
dst_add = weights["dst_address"].to_numpy()
src_add = weights["src_address"].to_numpy()
map_wts = weights["remap_matrix"].to_numpy().squeeze()

for n in range(int(num_links / 4)):
noff = n * 4

dst_array[dst_add[noff]] = (
src_array[src_add[noff]] * map_wts[1 * noff] +
src_array[src_add[noff + 1]] * map_wts[1 * (noff + 1)] +
src_array[src_add[noff + 2]] * map_wts[1 * (noff + 2)] +
src_array[src_add[noff + 3]] * map_wts[1 * (noff + 3)]
)
```

This seems quite straightforward, but for some reason I am getting a substantially different result (see attached file).

I suspect that I am missing some operation performed on the input grid, but I could not find it in the CDO code myself.

Please advise.


Replies (4)

RE: Use CDO remapping weights in Python - Added by Karin Meier-Fleischer almost 2 years ago

Hi Nikolai,

can you provide your script and data?

RE: Use CDO remapping weights in Python - Added by Nikolai Stulov almost 2 years ago

Hi, thank you for reaching out. Input data, target grid, python script and cdo-generated outputs are attached.

era_grid (313 Bytes) era_grid target grid
wrf_t2.nc (4.73 MB) wrf_t2.nc input data
interp_script.py (1.58 KB) interp_script.py python script
wrf_to_era_weights.nc (505 KB) wrf_to_era_weights.nc weights
wrf_to_era_remap.nc (226 KB) wrf_to_era_remap.nc cdo result

RE: Use CDO remapping weights in Python - Added by Uwe Schulzweida almost 2 years ago

The indices in dst_address and src_address have the range 1 to N. So they are "fortran" indices as used in the original SCRIP package. I don't know how it is in python but in C all indices must be reduced by one.

RE: Use CDO remapping weights in Python - Added by Nikolai Stulov almost 2 years ago

Many thanks, it did the job!

    (1-4/4)