question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrieve regridding weights as numpy arrays

See original GitHub issue

Most regridding schemes are linear, i.e. the output data field is linearly dependent on the input data field. Any linear transform can be viewed as a matrix-vector multiplication y = A*x, where A is a matrix (typically sparse) containing regridding weights, and x, y are input and output data fields flatten to 1D.

In practice, linear regridding schemes are broken into two steps:

  1. Calculate regridding weights (i.e. get the matrix A), from the knowledge of source and destination grids. In ESMPy, this is done by regrid = ESMF.Regrid(...).
  2. Apply regridding weights to the data field (i.e. perform A*x). In ESMPy, this is done by destfield = regrid(sourcefield, destfield), where regrid was created in the previous step.

However, ESMPy’s regrid object is like a black box. It knows how to perform regridding but there’s no way to explicitly view the regridding weights (the matrix A).

In the Fortran version of ESMF, the function ESMF_RegridWeightGen dumps regridding weights to NetCDF files. The content of the file is shown in “12.8 Regrid Interpolation Weight File Format” section in ESMF documention (link). The matrix A is stored in a sparse matrix form by variables row,col and S in that NetCDF file. But in ESMPy there’s no function equivalent to ESMF_RegridWeightGen.

Being able to view the regridding weights in the Python-level will solve many troubles:

  • Dimension matching. To broadcast the regridding operation across additional dimensions (call them N1, N2…), ESMPy requires the input data to have the shape [Nlat, Nlon, N1, N2,…]. Instead of using xarray’s transpose() function to match ESMPy’s expection, we can write the sparse matrix multiplication directly in numpy, taking care of dimension broadcasting.
  • Dask intergration. By using numpy instead of the underlying Fortran routine to apply regridding weights, we can use dask.array easily and natively. Otherwise, we need to let each dask worker call the underlying Fortran routine separately to regrid each hyberslab – sounds like a very ugly solution.
  • Allow other programs to use ESMF regridding. One use case is NASA-GEOS5’s MAPL software. Calculating regridding weights is very hard and we don’t want to rebuild the wheel, but applying the weights to the data field can be done by ~5 lines of code in most languages.

@bekozi seems to have some Python tools for ESMF_RegridWeightGen. Maybe we could start with that.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
JiaweiZhuangcommented, Oct 13, 2017

@bekozi Thanks for looking into this! I am OK with Python2.7 for now.

1reaction
bekozicommented, Oct 12, 2017

My sincere apologies for the delay on getting you the weight file write code. We have a snapshot tag that includes writing weights to file:

git clone -b ESMF_7_1_0_beta_snapshot_35 https://git.code.sf.net/p/esmf/esmf esmf

The only change to your code should be the addition of a filename argument to the regrid call:

rh = ESMF.Regrid(srcfield, dstfield, filename='/tmp/weights.nc', ...)

Please let me know if you have any questions or find any issues! Though tested, note this is considered “development code”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regrid — ESMPy 8.5.0b0 documentation
src_frac_field (ndarray) – return a numpy array of values containing weights corresponding to the amount of each Field value which contributes to the...
Read more >
regrid2 package - CDMS Documentation
datafull numpy array, this method will take care of setting ... Get the indices and weights for a single target location. Parameters. dst_indices....
Read more >
ESMF_regrid
Generates the weights and writes them to a NetCDF file. Regrids the data by applying the weights. Copies metadata (attributes and coordinate arrays)...
Read more >
Analysis — Documentation - GitHub Pages
This version of cf is for Python 3 only and there are incompatible differences ... b = a.collapse('area: mean T: maximum', weights=True) >>>...
Read more >
Using xesmf to efficiently regrid data to another resolution
import xarray as xr import xesmf as xe import numpy as np. Open the data. In this example, we will open a monthly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found