Skip to content

Commit

Permalink
first pass at disabling xesmf module for osx-arm64 #315
Browse files Browse the repository at this point in the history
  • Loading branch information
durack1 committed Aug 31, 2022
1 parent 148a1cb commit 991d625
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
22 changes: 14 additions & 8 deletions xcdat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
)
from xcdat.bounds import BoundsAccessor # noqa: F401
from xcdat.dataset import decode_non_cf_time, open_dataset, open_mfdataset # noqa: F401
from xcdat.regridder.accessor import RegridderAccessor # noqa: F401
from xcdat.regridder.grid import ( # noqa: F401
create_gaussian_grid,
create_global_mean_grid,
create_grid,
create_uniform_grid,
create_zonal_grid,
)
# esmpy, xesmf dependency not solved for osx-arm64 https://github.com/conda-forge/esmpy-feedstock/issues/55
# _importorskip is another option if these accumulate https://github.com/pydata/xarray/blob/main/xarray/tests/__init__.py#L29-L60
# discussion: https://github.com/xCDAT/xcdat/issues/315
try:
from xcdat.regridder.accessor import RegridderAccessor # noqa: F401
from xcdat.regridder.grid import ( # noqa: F401
create_gaussian_grid,
create_global_mean_grid,
create_grid,
create_uniform_grid,
create_zonal_grid,
)
except ImportError:
pass
from xcdat.spatial import SpatialAccessor # noqa: F401
from xcdat.temporal import TemporalAccessor # noqa: F401
from xcdat.utils import compare_datasets # noqa: F401
Expand Down
9 changes: 8 additions & 1 deletion xcdat/regridder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from xcdat.regridder.accessor import RegridderAccessor
from xcdat.regridder.regrid2 import Regrid2Regridder
from xcdat.regridder.xesmf import XESMFRegridder
# esmpy, xesmf dependency not solved for osx-arm64 https://github.com/conda-forge/esmpy-feedstock/issues/55
# _importorskip is another option if these accumulate https://github.com/pydata/xarray/blob/main/xarray/tests/__init__.py#L29-L60
# discussion: https://github.com/xCDAT/xcdat/issues/315
try:
from xcdat.regridder.xesmf import XESMFRegridder
except ImportError:
print("xesmf module not available")
pass
98 changes: 55 additions & 43 deletions xcdat/regridder/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
import xarray as xr

from xcdat.axis import CFAxisName, get_axis_coord
from xcdat.regridder import regrid2, xesmf

RegridTool = Literal["xesmf", "regrid2"]

REGRID_TOOLS = {
"xesmf": xesmf.XESMFRegridder,
"regrid2": regrid2.Regrid2Regridder,
}
# esmpy, xesmf dependency not solved for osx-arm64 https://github.com/conda-forge/esmpy-feedstock/issues/55
# _importorskip is another option if these accumulate https://github.com/pydata/xarray/blob/main/xarray/tests/__init__.py#L29-L60
# discussion: https://github.com/xCDAT/xcdat/issues/315
try:
from xcdat.regridder import regrid2, xesmf
RegridTool = Literal["xesmf", "regrid2"]
REGRID_TOOLS = {
"xesmf": xesmf.XESMFRegridder,
"regrid2": regrid2.Regrid2Regridder,
}
except ImportError:
from xcdat.regridder import regrid2
RegridTool = Literal["regrid2"]
REGRID_TOOLS = {
"regrid2": regrid2.Regrid2Regridder,
}
print("xesmf module not available")
xesmfAvailable = False


@xr.register_dataset_accessor(name="regridder")
Expand Down Expand Up @@ -86,53 +97,54 @@ def _get_axis_data(self, name: CFAxisName) -> Tuple[xr.DataArray, xr.DataArray]:

return coord_var, bounds_var

def horizontal_xesmf(
self,
data_var: str,
output_grid: xr.Dataset,
**options: Dict[str, Any],
) -> xr.Dataset:
"""
Wraps the xESMF library providing access to regridding between
structured rectilinear and curvilinear grids.
if xesmfAvailable:
def horizontal_xesmf(
self,
data_var: str,
output_grid: xr.Dataset,
**options: Dict[str, Any],
) -> xr.Dataset:
"""
Wraps the xESMF library providing access to regridding between
structured rectilinear and curvilinear grids.
Regrids ``data_var`` in dataset to ``output_grid``.
Regrids ``data_var`` in dataset to ``output_grid``.
Option documentation :py:func:`xcdat.regridder.xesmf.XESMFRegridder`
Option documentation :py:func:`xcdat.regridder.xesmf.XESMFRegridder`
Parameters
----------
data_var: str
Name of the variable in the `xr.Dataset` to regrid.
output_grid : xr.Dataset
Dataset containing output grid.
options : Dict[str, Any]
Dictionary with extra parameters for the regridder.
Parameters
----------
data_var: str
Name of the variable in the `xr.Dataset` to regrid.
output_grid : xr.Dataset
Dataset containing output grid.
options : Dict[str, Any]
Dictionary with extra parameters for the regridder.
Returns
-------
xr.Dataset
With the ``data_var`` variable on the grid defined in ``output_grid``.
Returns
-------
xr.Dataset
With the ``data_var`` variable on the grid defined in ``output_grid``.
Raises
------
ValueError
If tool is not supported.
Raises
------
ValueError
If tool is not supported.
Examples
--------
Examples
--------
Generate output grid:
Generate output grid:
>>> output_grid = xcdat.create_gaussian_grid(32)
>>> output_grid = xcdat.create_gaussian_grid(32)
Regrid data to output grid using regrid2:
Regrid data to output grid using regrid2:
>>> ds.regridder.horizontal_regrid2("ts", output_grid)
"""
regridder = REGRID_TOOLS["xesmf"](self._ds, output_grid, **options)
>>> ds.regridder.horizontal_regrid2("ts", output_grid)
"""
regridder = REGRID_TOOLS["xesmf"](self._ds, output_grid, **options)

return regridder.horizontal(data_var, self._ds)
return regridder.horizontal(data_var, self._ds)

def horizontal_regrid2(
self,
Expand Down
9 changes: 8 additions & 1 deletion xcdat/regridder/xesmf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import xarray as xr
import xesmf as xe
# esmpy, xesmf dependency not solved for osx-arm64 https://github.com/conda-forge/esmpy-feedstock/issues/55
# _importorskip is another option if these accumulate https://github.com/pydata/xarray/blob/main/xarray/tests/__init__.py#L29-L60
# discussion: https://github.com/xCDAT/xcdat/issues/315
try:
import xesmf as xe
except ImportError:
print("xesmf module not available")
pass

from xcdat.regridder.base import BaseRegridder, preserve_bounds

Expand Down

0 comments on commit 991d625

Please sign in to comment.