Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xESMF regridder #2433

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,25 @@

# Configuration for intersphinx
intersphinx_mapping = {
'cf_units': ('https://cf-units.readthedocs.io/en/latest/', None),
'cf_units': ('https://cf-units.readthedocs.io/en/stable/', None),
'cftime': ('https://unidata.github.io/cftime/', None),
'esmvalcore':
(f'https://docs.esmvaltool.org/projects/ESMValCore/en/{rtd_version}/',
None),
'esmvaltool': (f'https://docs.esmvaltool.org/en/{rtd_version}/', None),
'dask': ('https://docs.dask.org/en/stable/', None),
'distributed': ('https://distributed.dask.org/en/stable/', None),
'iris': ('https://scitools-iris.readthedocs.io/en/latest/', None),
'iris-esmf-regrid': ('https://iris-esmf-regrid.readthedocs.io/en/latest',
'iris': ('https://scitools-iris.readthedocs.io/en/stable/', None),
'iris-esmf-regrid': ('https://iris-esmf-regrid.readthedocs.io/en/stable',
None),
'matplotlib': ('https://matplotlib.org/stable/', None),
'ncdata': ('https://ncdata.readthedocs.io/en/stable/', None),
'numpy': ('https://numpy.org/doc/stable/', None),
'pyesgf': ('https://esgf-pyclient.readthedocs.io/en/latest/', None),
'pyesgf': ('https://esgf-pyclient.readthedocs.io/en/stable/', None),
'python': ('https://docs.python.org/3/', None),
'scipy': ('https://docs.scipy.org/doc/scipy/', None),
'xarray': ('https://docs.xarray.dev/en/stable/', None),
'xesmf': ('https://xesmf.readthedocs.io/en/stable/', None),
}

# -- Extlinks extension -------------------------------------------------------
Expand Down
165 changes: 165 additions & 0 deletions esmvalcore/preprocessor/_regrid_xesmf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
"""xESMF regridding.

To use this, install xesmf and ncdata, e.g. ``mamba install xesmf ncdata``.
"""

import inspect

import dask.array as da
import iris.cube
import numpy as np


class xESMFRegridder: # noqa
"""xESMF regridding function.

This is a wrapper around :class:`xesmf.frontend.Regridder` so it can be
used in :meth:`iris.cube.Cube.regrid`.

Supports lazy regridding.

Parameters
----------
src_cube:
Cube describing the source grid.
tgt_cube:
Cube describing the target grid.
**kwargs:
Any keyword argument to :class:`xesmf.frontend.Regridder` or
:meth:`xesmf.frontend.Regridder.__call__` can be provided.

Attributes
----------
kwargs:
Keyword arguments to :class:`xesmf.frontend.Regridder`.
default_call_kwargs:
Default keyword arguments to :meth:`xesmf.frontend.Regridder.__call__`.
"""

def __init__(
self,
src_cube: iris.cube.Cube,
tgt_cube: iris.cube.Cube,
**kwargs,
) -> None:
import ncdata.iris_xarray

Check notice on line 45 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_regrid_xesmf.py#L45

Import outside toplevel (ncdata.iris_xarray) (import-outside-toplevel)
import xesmf

Check notice on line 46 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_regrid_xesmf.py#L46

Import outside toplevel (xesmf) (import-outside-toplevel)

Check warning on line 46 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L45-L46

Added lines #L45 - L46 were not covered by tests

call_arg_names = list(

Check warning on line 48 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L48

Added line #L48 was not covered by tests
inspect.signature(xesmf.Regridder.__call__).parameters)[2:]
self.kwargs = {

Check warning on line 50 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L50

Added line #L50 was not covered by tests
k: v
for k, v in kwargs.items() if k not in call_arg_names
}
self.default_call_kwargs = {

Check warning on line 54 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L54

Added line #L54 was not covered by tests
k: v
for k, v in kwargs.items() if k in call_arg_names
}

src_cube = src_cube.copy(da.ma.filled(src_cube.core_data(), np.nan))
tgt_cube = tgt_cube.copy(da.ma.filled(tgt_cube.core_data(), np.nan))
src_ds = ncdata.iris_xarray.cubes_to_xarray([src_cube])
tgt_ds = ncdata.iris_xarray.cubes_to_xarray([tgt_cube])

Check warning on line 62 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L59-L62

Added lines #L59 - L62 were not covered by tests

self._regridder = xesmf.Regridder(src_ds, tgt_ds, **self.kwargs)

Check warning on line 64 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L64

Added line #L64 was not covered by tests

def __repr__(self) -> str:
"""Return a string representation of the class."""
kwargs = self.kwargs | self.default_call_kwargs
return f"{self.__class__.__name__}(**{kwargs})"

Check warning on line 69 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L68-L69

Added lines #L68 - L69 were not covered by tests

def __call__(self, src_cube: iris.cube.Cube, **kwargs) -> iris.cube.Cube:
"""Run the regridder.

Parameters
----------
src_cube:
The cube to regrid.
**kwargs:
Keyword arguments to :meth:`xesmf.frontend.Regridder.__call__`.

Returns
-------
iris.cube.Cube
The regridded cube.
"""
import ncdata.iris_xarray

Check warning on line 86 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L86

Added line #L86 was not covered by tests

call_args = dict(self.default_call_kwargs)
call_args.update(kwargs)

Check warning on line 89 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L88-L89

Added lines #L88 - L89 were not covered by tests

src_cube = src_cube.copy(da.ma.filled(src_cube.core_data(), np.nan))
src_ds = ncdata.iris_xarray.cubes_to_xarray([src_cube])

Check warning on line 92 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L91-L92

Added lines #L91 - L92 were not covered by tests

tgt_ds = self._regridder(src_ds, **call_args)

Check warning on line 94 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L94

Added line #L94 was not covered by tests

cube = ncdata.iris_xarray.cubes_from_xarray(

Check warning on line 96 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L96

Added line #L96 was not covered by tests
tgt_ds,
iris_load_kwargs={'constraints': src_cube.standard_name},
)[0]
cube.data = da.ma.masked_where(da.isnan(cube.core_data()),

Check warning on line 100 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L100

Added line #L100 was not covered by tests
cube.core_data())
return cube

Check warning on line 102 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L102

Added line #L102 was not covered by tests


class xESMF: # noqa
"""xESMF regridding scheme.

This is a wrapper around :class:`xesmf.frontend.Regridder` so it can be
used in :meth:`iris.cube.Cube.regrid`. It uses the :mod:`ncdata` package to
convert the :class:`iris.cube.Cube` to an :class:`xarray.Dataset` before
regridding and back after regridding.

Supports lazy regridding.

Masks are converted to :obj:`numpy.nan` before regridding and converted
back to masks after regridding.

Parameters
----------
**kwargs:
Any keyword argument to :class:`xesmf.frontend.Regridder` or
:meth:`xesmf.frontend.Regridder.__call__` can be provided. By default,
the arguments ``ignore_degenerate=True``, ``keep_attrs=True``,
``skipna=True``, and ``unmapped_to_nan=True`` will be used.

Attributes
----------
kwargs:
Keyword arguments that will be provided to the regridder.
"""

def __init__(self, **kwargs) -> None:
args = {

Check warning on line 133 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L133

Added line #L133 was not covered by tests
'ignore_degenerate': True,
'skipna': True,
'keep_attrs': True,
'unmapped_to_nan': True,
}
args.update(kwargs)
self.kwargs = args

Check warning on line 140 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L139-L140

Added lines #L139 - L140 were not covered by tests

def __repr__(self) -> str:
"""Return string representation of class."""
return f'{self.__class__.__name__}(**{self.kwargs})'

Check warning on line 144 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L144

Added line #L144 was not covered by tests

def regridder(
self,
src_cube: iris.cube.Cube,
tgt_cube: iris.cube.Cube,
) -> xESMFRegridder:
"""Create xESMF regridding function.

Parameters
----------
src_cube:
Cube defining the source grid.
tgt_cube:
Cube defining the target grid.

Returns
-------
xESMFRegridder
xESMF regridding function.
"""
return xESMFRegridder(src_cube, tgt_cube, **self.kwargs)

Check warning on line 165 in esmvalcore/preprocessor/_regrid_xesmf.py

View check run for this annotation

Codecov / codecov/patch

esmvalcore/preprocessor/_regrid_xesmf.py#L165

Added line #L165 was not covered by tests
8 changes: 3 additions & 5 deletions esmvalcore/preprocessor/regrid_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
UnstructuredLinearRegridder,
UnstructuredNearest,
)
from esmvalcore.preprocessor._regrid_xesmf import xESMF, xESMFRegridder

logger = logging.getLogger(__name__)


__all__ = [
'ESMPyAreaWeighted',
'ESMPyLinear',
Expand All @@ -31,6 +31,8 @@
'UnstructuredLinear',
'UnstructuredLinearRegridder',
'UnstructuredNearest',
'xESMF',
'xESMFRegridder',
]


Expand All @@ -51,7 +53,6 @@ class GenericRegridder:
Cube, \*\*kwargs) -> Cube.
**kwargs:
Keyword arguments for the generic regridding function.

"""

def __init__(
Expand Down Expand Up @@ -79,7 +80,6 @@ def __call__(self, cube: Cube) -> Cube:
-------
Cube
Regridded cube.

"""
return self.func(cube, self.tgt_cube, **self.kwargs)

Expand All @@ -98,7 +98,6 @@ class GenericFuncScheme:
Cube, \*\*kwargs) -> Cube.
**kwargs:
Keyword arguments for the generic regridding function.

"""

def __init__(self, func: Callable, **kwargs):
Expand All @@ -125,6 +124,5 @@ def regridder(self, src_cube: Cube, tgt_cube: Cube) -> GenericRegridder:
-------
GenericRegridder
Regridder instance.

"""
return GenericRegridder(src_cube, tgt_cube, self.func, **self.kwargs)