Skip to content

Commit

Permalink
Replaced _CubeFilterCollection.merged() with combined(); replace uses…
Browse files Browse the repository at this point in the history
… in load;load_cube/load_cubes.
  • Loading branch information
pp-mo committed Oct 14, 2024
1 parent e52e20b commit e135573
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/iris/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@ def _current_effective_policy():
return policy


def _apply_loading_policy(cubes, policy=None):
def _combine_with_loading_policy(cubes, policy=None, merge_require_unique=False):
if not policy:
policy = _current_effective_policy()
while True:
n_original_cubes = len(cubes)
if policy.use_concatenate and policy.cat_before_merge:
cubes = cubes.concatenate()
if policy.use_merge:
cubes = cubes.merge()
cubes = cubes.merge(unique=merge_require_unique)
if policy.use_concatenate and not policy.cat_before_merge:
cubes = cubes.concatenate()
n_new_cubes = len(cubes)
Expand Down Expand Up @@ -447,8 +447,7 @@ def load(uris, constraints=None, callback=None, policy=None):
were random.
"""
cubes = _load_collection(uris, constraints, callback).cubes()
cubes = _apply_loading_policy(cubes)
cubes = _load_collection(uris, constraints, callback).combined().cubes()
return cubes


Expand Down Expand Up @@ -477,9 +476,11 @@ def load_cube(uris, constraint=None, callback=None):
if len(constraints) != 1:
raise ValueError("only a single constraint is allowed")

cubes = _load_collection(uris, constraints, callback).cubes()
cubes = _load_collection(uris, constraints, callback).combined(unique=False).cubes()

try:
# NOTE: this call currently retained to preserve the legacy exceptions
# TODO: replace with simple testing to duplicate the relevant error cases
cube = cubes.merge_cube()
except iris.exceptions.MergeError as e:
raise iris.exceptions.ConstraintMismatchError(str(e))
Expand Down Expand Up @@ -514,7 +515,7 @@ def load_cubes(uris, constraints=None, callback=None):
"""
# Merge the incoming cubes
collection = _load_collection(uris, constraints, callback).merged()
collection = _load_collection(uris, constraints, callback).combined()

# Make sure we have exactly one merged cube per constraint
bad_pairs = [pair for pair in collection.pairs if len(pair) != 1]
Expand Down
17 changes: 13 additions & 4 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class _CubeFilter:
"""A constraint, paired with a list of cubes matching that constraint."""

def __init__(self, constraint, cubes=None):
from iris.cube import CubeList

self.constraint = constraint
if cubes is None:
cubes = CubeList()
Expand All @@ -78,7 +80,7 @@ def add(self, cube):
if sub_cube is not None:
self.cubes.append(sub_cube)

def merged(self, unique=False):
def combined(self, unique=False):
"""Return a new :class:`_CubeFilter` by merging the list of cubes.
Parameters
Expand All @@ -88,7 +90,12 @@ def merged(self, unique=False):
duplicate cubes are detected.
"""
return _CubeFilter(self.constraint, self.cubes.merge(unique))
from iris import _combine_with_loading_policy

return _CubeFilter(
self.constraint,
_combine_with_loading_policy(self.cubes, merge_require_unique=unique),
)


class _CubeFilterCollection:
Expand All @@ -114,12 +121,14 @@ def add_cube(self, cube):

def cubes(self):
"""Return all the cubes in this collection in a single :class:`CubeList`."""
from iris.cube import CubeList

result = CubeList()
for pair in self.pairs:
result.extend(pair.cubes)
return result

def merged(self, unique=False):
def combined(self, unique=False):
"""Return a new :class:`_CubeFilterCollection` by merging all the cube lists of this collection.
Parameters
Expand All @@ -129,7 +138,7 @@ def merged(self, unique=False):
duplicate cubes are detected.
"""
return _CubeFilterCollection([pair.merged(unique) for pair in self.pairs])
return _CubeFilterCollection([pair.combined(unique) for pair in self.pairs])


class CubeList(list):
Expand Down

0 comments on commit e135573

Please sign in to comment.