pangeo-data / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
188 stars 34 forks source link

regridding cubed-sphere-type datasets (continued discussion) #96

Open LiamBindle opened 3 years ago

LiamBindle commented 3 years ago

Hi all. I thought I would transfer the cubed-sphere regridding discussion (https://github.com/JiaweiZhuang/xESMF/issues/21) here since this is the now-active project. In summary, xESMF has limited support for cubed-sphere grids (and other non-logically-rectangular grids). The workaround is to sum the results of conservatively regridding all the faces. A general solution for "more complicated" grid types is desirable however.

I'm going to copy-paste my latest comment in https://github.com/JiaweiZhuang/xESMF/issues/21 below.

LiamBindle commented 3 years ago

Hi all. For GCHP we need cubed-sphere and stretched cubed-sphere regridding too. We use the "sum conservatively-regridded faces" approach as well, and it works okay, but it's limited to the "conserve" method, it's brittle, and it's hard to extend because of the extra complexity. Lately I have been working on a few projects that are regridding-heavy, and I have a workflow that works pretty well for what I need. I thought I would share it here, and perhaps we can figure out how to support this in general in the ESM community.

The ESMF_Regrid and ESMF_RegridWeightGen tools support the gridspec file format for cubed-sphere and stretched cubed-sphere grids. I didn't find any tools online for generating these files, so I wrote a small utility for generating gridspec files. This lets you use the ESMF_RegridWeightGen tool natively, so you can generate regridding weights using any method (e.g., bilinear, 2nd order conservative, nearest, etc.), and regrid between any grid-types (assuming you can generate the grid-definition file).

Once you have the regridding weights, the actual regridding is simply a sparse matrix multiplication. I wrote a small library for doing this sparse matrix multiplication the Pythonic way (using numpy.vectorize and xarray.apply_ufunc) which you can find here: sparselt. That said it's simply a sparse matrix multiplication, so it would be easy to implement in any language.

The "nice traits" of this approach are:

This isn't a "new approach"---it's really what's just going on under-the-hood---but breaking regridding up into generating weights and a sparse matrix multiplication is an interface that's flexible and simple.

I'm happy to merge my work into somewhere else, but I'm not entirely sure it fits well in xESMF. Open to any suggestions!