Skip to content

Commit

Permalink
adapt to changes in iris structure
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenworsley committed Jul 30, 2024
1 parent 5354829 commit b795c7a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
6 changes: 3 additions & 3 deletions benchmarks/benchmarks/esmf_regridder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import dask.array as da
import iris
from iris.cube import Cube
from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD
import numpy as np

from esmf_regrid import _load_context
from esmf_regrid.esmf_regridder import GridInfo
from esmf_regrid.experimental.unstructured_scheme import (
GridToMeshESMFRegridder,
Expand Down Expand Up @@ -252,7 +252,7 @@ def setup_cache(self):

iris.save(src, file, chunksizes=chunk_size)
# Construct regridder with a loaded version of the grid for consistency.
with PARSE_UGRID_ON_LOAD.context():
with _load_context():
loaded_src = iris.load_cube(file)
regridder = MeshToGridESMFRegridder(loaded_src, tgt)

Expand All @@ -261,7 +261,7 @@ def setup_cache(self):
def setup(self, cache):
"""ASV setup method."""
regridder, file = cache
with PARSE_UGRID_ON_LOAD.context():
with _load_context():
self.src = iris.load_cube(file)
cube = iris.load_cube(file)
self.result = regridder(cube)
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/benchmarks/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from warnings import warn

from iris import load_cube
from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD

from esmf_regrid import _load_context

#: Python executable used by :func:`run_function_elsewhere`, set via env
#: variable of same name. Must be path of Python within an environment that
Expand Down Expand Up @@ -253,6 +254,6 @@ def external(*args, **kwargs):
save_path=str(save_path),
)

with PARSE_UGRID_ON_LOAD.context():
with _load_context():
return_cube = load_cube(str(save_path))
return return_cube
13 changes: 13 additions & 0 deletions esmf_regrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
except ImportError:
raise exc

try:
import iris.mesh as _imesh
except ImportError as exc:
try:
import iris.experimental.ugrid as _imesh
except ImportError:
raise exc
if hasattr(_imesh, "PARSE_UGRID_ON_LOAD"):
_load_context = _imesh.PARSE_UGRID_ON_LOAD
else:
from contextlib import nullcontext
_load_context = nullcontext

# constants needs to be above schemes, as it is used within
from .constants import Constants, check_method, check_norm
from .schemes import *
Expand Down
4 changes: 2 additions & 2 deletions esmf_regrid/experimental/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import iris
from iris.coords import AuxCoord
from iris.cube import Cube, CubeList
from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD
import numpy as np
import scipy.sparse

import esmf_regrid
from esmf_regrid import _load_context
from esmf_regrid import check_method, Constants
from esmf_regrid.experimental.unstructured_scheme import (
GridToMeshESMFRegridder,
Expand Down Expand Up @@ -288,7 +288,7 @@ def load_regridder(filename):
-------
:class:`~esmf_regrid.experimental.unstructured_scheme.GridToMeshESMFRegridder` or :class:`~esmf_regrid.experimental.unstructured_scheme.MeshToGridESMFRegridder`
"""
with PARSE_UGRID_ON_LOAD.context():
with _load_context():
cubes = iris.load(filename)

# Extract the source, target and metadata information.
Expand Down
3 changes: 1 addition & 2 deletions esmf_regrid/experimental/unstructured_scheme.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""Provides an iris interface for unstructured regridding."""

try:
from iris.experimental.ugrid import MeshXY
from iris.mesh import MeshXY
except ImportError as exc:
# Prior to v3.10.0, `MeshXY` could was named `Mesh`.
try:
from iris.experimental.ugrid import Mesh as MeshXY
except ImportError:
raise exc

from esmf_regrid import check_method, Constants
from esmf_regrid.schemes import (
_ESMFRegridder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import os

import iris
from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD
import numpy as np
try:
from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD
_load_context = PARSE_UGRID_ON_LOAD.context
except ImportError:
from contextlib import nullcontext
_load_context = nullcontext

from esmf_regrid.experimental.unstructured_scheme import (
regrid_unstructured_to_rectilinear,
Expand All @@ -22,7 +27,7 @@ def test_real_data():
src_fn = os.path.join(
test_data_dir, "NetCDF", "unstructured_grid", "lfric_surface_mean.nc"
)
with PARSE_UGRID_ON_LOAD.context():
with _load_context():
src = iris.load_cube(src_fn, "rainfall_flux")

# Load target grid cube.
Expand Down
2 changes: 1 addition & 1 deletion esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import scipy.sparse

try:
from iris.experimental.ugrid import MeshXY
from iris.mesh import MeshXY
except ImportError as exc:
# Prior to v3.10.0, `MeshXY` could was named `Mesh`.
try:
Expand Down

0 comments on commit b795c7a

Please sign in to comment.