Skip to content

Commit

Permalink
build: make xesmf optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanker committed Oct 9, 2024
1 parent df094d6 commit 12b7da2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ dependencies = [
"deprecation",
"pooch",
"scikit-learn",
"xesmf",
###
# optimization
###
Expand All @@ -79,6 +78,9 @@ dependencies = [
]

[project.optional-dependencies]
synth = [
"xesmf",
]
test = [
"pytest >=6",
"pytest-cov >=3",
Expand All @@ -97,7 +99,7 @@ docs = [
"sphinxcontrib-bibtex",
]
dev = [
"invert4geom[test,docs]",
"invert4geom[test,docs, synth]",
"ruff",
"nox",
"pre-commit",
Expand All @@ -108,7 +110,7 @@ dev = [
"mypy",
]
all = [
"invert4geom[test,dev,docs]",
"invert4geom[test,dev,docs,synth]",
]

[project.urls]
Expand Down
13 changes: 12 additions & 1 deletion src/invert4geom/synthetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import pooch
import verde as vd
import xarray as xr
import xesmf
from numpy.typing import NDArray
from polartoolkit import fetch, maps
from polartoolkit import utils as polar_utils

from invert4geom import cross_validation, log, utils

try:
import xesmf
except ImportError:
xesmf = None


def load_synthetic_model(
spacing: float = 1e3,
Expand Down Expand Up @@ -283,6 +287,12 @@ def contaminate_with_long_wavelength_noise(
xarray.DataArray
Contaminated grid
"""
if xesmf is None:
msg = (
"To use the `contaminate_with_long_wavelength_noise` function, you must "
"have the `xesmf` package installed."
)
raise ImportError(msg)

grid = copy.deepcopy(grid)

Expand Down Expand Up @@ -310,6 +320,7 @@ def contaminate_with_long_wavelength_noise(
data=None,
data_names=None,
).rename({"northing": "lat", "easting": "lon"})

low_res_grid = xesmf.Regridder(
grid.rename({"northing": "lat", "easting": "lon"}),
low_res_grid,
Expand Down

0 comments on commit 12b7da2

Please sign in to comment.