JiaweiZhuang / xESMF

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

Generate SCRIP grid file from scratch #4

Closed JiaweiZhuang closed 7 years ago

JiaweiZhuang commented 7 years ago

To solve #2 (the biggest, or perhaps the only barrier for xESMF), I would like a Python version of ESMF_RegridWeightGen. According to @bekozi and @rokuingh, this function will eventually be available in ESMPy. Before that, we can actually use the Fortran version of ESMF_RegridWeightGen to generate offline regridding weights. This command line utility comes with conda ESMF so users don't need to worry about compiling Fortran source code. In the future we can easily replace it by the Python equivalent, without affecting other parts of xESMF.

However,ESMF_RegridWeightGen requires the grid information to be stored in SCRIP Grid File Format. Here are possible ways to get this format:

I am wondering what's the best&quickest way to convert pure numpy arrays to SCRIP format, which can then be fed to ESMF_RegridWeightGen? xESMF only focuses on quadrilateral (i.e. logically rectilinear) grids so there's no need to worry about unstructured meshes. @bekozi and @rokuingh do you have minimal Python scripts to make this format?

JiaweiZhuang commented 7 years ago

Also, will the Python version of ESMF_RegridWeightGen be able to produce everything from pure numpy arrays or it will still rely on the SCRIP format?

bekozi commented 7 years ago

According to @bekozi and @rokuingh, this function will eventually be available in ESMPy. Before that, we can actually use the Fortran version of ESMF_RegridWeightGen to generate offline regridding weights.

We are very close to an implementation. Dealing with a couple test failures gratingly unrelated to the weight file writing.

However, ESMF_RegridWeightGen requires the grid information to be stored in SCRIP Grid File Format.

CF-Grid is the main structured grid format read by ESMF - the spiritual successor to SCRIP.

I am wondering what's the best&quickest way to convert pure numpy arrays to SCRIP format, which can then be fed to ESMF_RegridWeightGen?

ocgis is one option. This should work with ESMF_RegridWeightGen out-of-the-box:

conda create -n ocgis -c conda-forge ocgis
source activate ocgis
# Get the latest repository version.
conda remove ocgis
git clone https://github.com/NCPP/ocgis.git
cd ocgis
python setup.py install
import ocgis
import numpy as np

time = ocgis.TemporalVariable(name='time', value=[1, 2, 3, 4, 5, 6], dimensions='time')
lat = ocgis.Variable(name='lat', value=[-90, 0, 90.], dimensions='lat')
lon = ocgis.Variable(name='lon', value=[0., 90., 180., 270., 359.], dimensions='lon')
data = ocgis.Variable(name='data', value=np.random.rand(6, 3, 5), dimensions=['time', 'lat', 'lon'])
grid = ocgis.Grid(lon, lat, crs=ocgis.crs.Spherical())
field = ocgis.Field(time=time, grid=grid, is_data=data)
path = '/tmp/cf-data.nc'
field.write(path)
ocgis.RequestDataset(path).inspect()

One can also create an ESMPy field:

source activate ocgis
conda install -c conda-forge esmpy
...
from ocgis.regrid.base import get_esmf_field_from_ocgis_field

efield = get_esmf_field_from_ocgis_field(field)
print(type(efield))

Also, will the Python version of ESMF_RegridWeightGen be able to produce everything from pure numpy arrays or it will still rely on the SCRIP format?

It will work from pure numpy arrays.

Let me know if I missed anything...

JiaweiZhuang commented 7 years ago

We are very close to an implementation. Dealing with a couple test failures gratingly unrelated to the weight file writing.

It will work from pure numpy arrays.

Sounds great! Please do tell me once it is released, so can I make a major update on xESMF 😃

(close this issue as SCRIP doesn't have to be a hard dependence)