xESMF: Universal Regridder for Geospatial Data

repository·master·Indexed 42 days ago

https://github.com/pangeo-data/xesmf

A high-performance Python library for regridding geospatial data, providing a high-level API over the ESMF/ESMPy backend. It supports xarray and numpy arrays, enables parallel computation via Dask, and provides algorithms such as bilinear, conservative, and nearest neighbour for general curvilinear grids.

Tokens
18.4K
Snippets
57
Records
78
Agent score
54%

What's inside xESMF

  1. Overview of xESMF

    master

    xESMF is a Python package designed for regridding geospatial data. It serves as a high-level abstraction over the ESMF/ESMPy backend, making complex regridding tasks easier to perform.

    Key features include:

    • Support for General Curvilinear Grids: Can regrid between complex grid types using various ESMF algorithms like bilinear, conservative, and nearest neighbour.
    • High-level API: Compatible with xarray objects as well as standard numpy arrays.
    • Performance: Faster than the original ESMPy Fortran engine in serial execution and supports dask for parallel, out-of-core computation.
  2. Overview of xESMF capabilities

    master

    xESMF is a Python package designed for regridding geospatial data. It serves as a high-level abstraction over the ESMF/ESMPy backend, making complex regridding tasks accessible through a simple API.

    Key features include:

    • Support for General Curvilinear Grids: Can regrid between various grid types using algorithms such as bilinear, conservative, and nearest neighbour.
    • Compatibility: Works seamlessly with xarray datasets as well as standard numpy arrays.
    • Performance: Optimized for speed (faster than the original ESMPy Fortran engine in serial cases) and supports dask for out-of-core, parallel computation.
  3. Why use xESMF for geospatial regridding

    master

    xESMF is designed to address the limitations of traditional interpolation tools (like SciPy's interp2d or MATLAB's interp2) when applied to geospatial data.

    Key advantages include:

    1. Scientific Correctness: Unlike standard image processing interpolation, xESMF accounts for the spherical geometry of the Earth to prevent distortion. It also supports algorithms that prioritize physical properties like conservation (e.g., total mass conservation) and monotonicity (e.g., preventing negative air density), which piecewise polynomial splines often fail to maintain.
    2. Support for Complex Grids: xESMF supports regridding between general curvilinear grids (quadrilateral or "logically rectilinear" grids), provided that geographic latitude and longitude can be represented as 2D or 1D arrays. Supported examples include:
      • Cubed-Sphere grids (e.g., in GFDL-FV3)
      • Latitude-Longitude-Cap grids (e.g., in MITgcm)
      • Lambert Conformal grids (e.g., in WRF) Note: xESMF does not currently support non-quadrilateral grids like the hexagonal grid in MPAS.
    3. Usability and Integration: xESMF integrates directly with the existing Python scientific stack. It works with numpy.ndarray and tracks metadata using xarray.DataArray and xarray.Dataset, minimizing the learning curve for users already familiar with these libraries.
  4. Compare xESMF with other geospatial regridding tools

    master

    If xESMF does not suit your specific needs, several other geospatial regridding tools exist for data on the sphere. xESMF is designed specifically to provide a friendly interface for the xarray community, whereas other tools may be more focused on Earth System Model (ESM) infrastructure or require more complex API interactions.

    Key Alternatives:

    • ESMPy: A Python interface to the Fortran-based ESMF. It provides access to ESMF's powerful regridding engine (which handles general irregular 2D or 3D meshes) but has a complex, non-intuitive API that often requires significant boilerplate code.
    • TempestRemap: A modern C++ package supporting arbitrary-order conservative remapping and on-the-fly cubed-sphere grid generation.
    • xCDAT: A Python package that offers regridding algorithms from both xESMF and regrid2 (originally from the CDAT cdutil package), including support for vertical regridding.
    • Iris: A Python package with built-in interpolation and regridding capabilities.
    • NCO: A command-line tool providing regridding functionality.
    • NCL (NCAR Command Language): Provides bilinear and conservative algorithms for rectilinear grids, with an ESMF wrapper for more complex grid types.
    • ESMF: A massive Fortran package used as infrastructure for Earth System Models. It is intended for building models rather than direct data analysis.
  5. Understand grid type support and limitations

    master

    xESMF is primarily designed for quadrilateral grids, including multi-tile quadrilateral grids like the cubed-sphere, because it relies on the xarray data model.

    While the underlying ESMPy engine can handle general irregular meshes, xESMF's current front-end support is limited to:

    • Quadrilateral grids: Fully supported.
    • Triangular or hexagonal meshes: Limited support.
    • Complex polygonal meshes: Supported only within the context of regional averaging.

    For irregular meshes that require connectivity information (as suggested by UGRID Conventions), xESMF currently lacks a native interface due to the constraints of the xarray data model.

  6. Important notes about ESMpy compatibility

    master

    ESMPy is a critical dependency for xESMF with the following constraints:

    • Version Compatibility: ESMpy 8.4 is only compatible with xESMF >= 0.7.
    • Installation Method: ESMpy must be installed through Conda or compiled manually; it is not available through PyPI. If installing xESMF with pip, you must ensure esmpy is already installed via Conda.
  7. Generate regridding weights on HPC using ESMF_RegridWeightGen

    master

    When source and target grids are too large for local memory, use the ESMF_RegridWeightGen command-line tool on an HPC system with MPI. This tool generates weight files that can be reused in xESMF.

    NetCDF Attribute Requirements

    To avoid file type not recognized errors, ensure your NetCDF files have correct units attributes for geographical coordinates:

    • Longitude: Must use one of degrees_east, degree_east, degrees_E, degree_E, degreesE, or degreeE.
    • Latitude: Must use one of degrees_north, degree_north, degrees_N, degree_N, degreesN, or degreeN.
    • Conservative Regridding: For cell corners, use grid_corner_lon and grid_corner_lat.

    Command Usage

    You can run the generator in parallel using mpirun.

    Key Flags:

    • --netcdf4: Use this if the resulting weight file is very large (NetCDF3 has size limitations).
    • --src_regional / --dst_regional: Use if one of the grids is not periodic in longitude.
    $ mpirun -np 252 ESMF_RegridWeightGen -s source.nc -d destination.nc -w weights.nc -m bilinear
  8. Install ESMPy via Conda

    master

    If you need to use the ESMF regridding functionality via Python, you can use ESMPy. To avoid the complex manual compilation process of the underlying Fortran ESMF library, it is recommended to install the pre-compiled conda package.

    Note: ESMPy has a complex API compared to native Python packages like xESMF.

    conda install -c NESII esmpy
  9. Install development versions of xESMF

    master

    To install the latest development version directly from the GitHub repository (which may not yet be on PyPI):

    pip install --upgrade git+https://github.com/pangeo-data/xESMF.git

    To set up a local development environment for contributing to xESMF:

    git clone https://github.com/pangeo-data/xESMF.git
    cd xESMF
    pip install -e .
    $ pip install --upgrade git+https://github.com/pangeo-data/xESMF.git
    
    # For developers
    $ git clone https://github.com/pangeo-data/xESMF.git
    $ cd xESMF
    $ pip install -e .
  10. Configure mpi4py for HPC environments

    master

    If you are installing ESMF via conda on an HPC, you may encounter MPI conflicts where the conda-provided mpirun does not work with the system's MPI libraries. To resolve this, you must recompile mpi4py from source, linking it specifically to your HPC's MPI implementation using a custom mpi.cfg file.

    1. Create/edit an mpi.cfg file with your system's MPI paths (e.g., mpi_dir, include_dirs, libraries, mpicc, etc.).
    2. Download the mpi4py source tarball.
    3. Build and install using python setup.py build --mpi=<your_config_name>.
    # Example mpi.cfg content
    [gaea-gnu]
      mpi_dir              = /opt/cray/pe/mpt/7.7.11/gni/mpich-gnu/8.2
      include_dirs         = %(mpi_dir)s/include
      libraries            = mpich
      library_dirs         = %(mpi_dir)s/lib
      runtime_library_dirs = %(mpi_dir)s/lib
      mpicc                = /opt/gcc/8.2.0/bin/gcc
      mpicxx               = /opt/gcc/8.2.0/bin/g++
    
    # Recompilation steps
    wget https://bitbucket.org/mpi4py/mpi4py/downloads/mpi4py-3.0.3.tar.gz
    tar -zxf mpi4py-3.0.3.tar.gz
    conda activate myenv
    cat gaea_mpi.cfg >> mpi4py-3.0.3/mpi.cfg
    pushd mpi4py-3.0.3
    python setup.py build --mpi=gaea-gnu
    python setup.py install
  11. Install xESMF using Conda

    master

    The recommended way to install xESMF is via Conda, as it manages the major dependencies xarray and ESMPy.

    1. Create and activate a new environment:
      conda create -n xesmf_env
      conda activate xesmf_env
    2. Install xESMF from the conda-forge channel:
      conda install -c conda-forge xesmf

    To ensure full functionality and support all features, it is highly recommended to install dask and netCDF4 as well:

    conda install -c conda-forge dask netCDF4

    For running notebook examples, you may also want to install matplotlib, cartopy, and jupyterlab:

    conda install -c conda-forge matplotlib cartopy jupyterlab
    $ conda create -n xesmf_env
    $ conda activate xesmf_env
    $ conda install -c conda-forge xesmf
    $ conda install -c conda-forge dask netCDF4
  12. Install xESMF via pip

    master

    If you prefer using pip, you must first manually install the core dependencies using Conda, because ESMPy is not available on PyPI and must be installed via Conda or compiled manually.

    1. Install dependencies via Conda:
      conda install -c conda-forge esmpy xarray numpy shapely cf_xarray sparse numba
    2. Install xESMF via pip:
      pip install git+https://github.com/pangeo-data/xesmf.git

    To install a specific release version via pip, append the version tag to the URL (e.g., @v0.5.0).

    $ conda install -c conda-forge esmpy xarray numpy shapely cf_xarray sparse numba
    $ pip install git+https://github.com/pangeo-data/xesmf.git