1313
1414import contextlib
1515import threading
16- from typing import Mapping
16+ from typing import List , Mapping
17+
18+ import iris
1719
1820
1921class CombineOptions (threading .local ):
2022 """A container for cube combination options.
2123
22- Controls for generalised merge/concatenate options.
24+ Controls for generalised merge/concatenate options : see :data:`iris.LOAD_POLICY`
25+ and :func:`iris.util.combine_cubes`.
2326
2427 Also controls the detection and handling of cases where a hybrid coordinate
2528 uses multiple reference fields during loading : for example, a UM file which
@@ -246,64 +249,57 @@ def context(self, settings=None, **kwargs):
246249 self .set (saved_settings )
247250
248251
249- def _combine_cubes (cubes , options ):
250- """Combine cubes as for load, according to "loading policy" options.
252+ def _combine_cubes_inner (
253+ cubes : List [iris .cube .Cube ], options : dict
254+ ) -> iris .cube .CubeList :
255+ """Combine cubes, according to "combine options".
251256
252- Applies :meth:`~iris.cube.CubeList.merge`/:meth:`~iris.cube.CubeList.concatenate`
253- steps to the given cubes, as determined by the 'settings'.
257+ As described for the main "iris.utils.combine_cubes".
254258
255259 Parameters
256260 ----------
257- cubes : list of :class:`~iris.cube.Cube`
258- A list of cubes to combine.
261+ cubes : list of Cube
262+ Cubes to combine.
263+
259264 options : dict
260- Settings , as described for :class:`iris. CombineOptions` .
265+ A list of options , as described in CombineOptions.
261266
262267 Returns
263268 -------
264- :class:`~iris.cube.CubeList`
265-
266- .. Note::
267- The ``support_multiple_references`` keyword/property has no effect on the
268- :func:`_combine_cubes` operation : it only takes effect during a load operation.
269-
270- Notes
271- -----
272- TODO: make this public API in future.
273- At that point, change the API to support (options=None, **kwargs) + add testing of
274- those modes (notably arg type = None / str / dict).
275-
269+ CubeList
276270 """
277271 from iris .cube import CubeList
278272
279- if not isinstance (cubes , CubeList ):
280- cubes = CubeList (cubes )
273+ if isinstance (cubes , CubeList ):
274+ cubelist = cubes
275+ else :
276+ cubelist = CubeList (cubes )
281277
278+ sequence = options ["merge_concat_sequence" ]
282279 while True :
283- n_original_cubes = len (cubes )
284- sequence = options ["merge_concat_sequence" ]
280+ n_original_cubes = len (cubelist )
285281
286282 if sequence [0 ] == "c" :
287283 # concat if it comes first
288- cubes = cubes .concatenate ()
284+ cubelist = cubelist .concatenate ()
289285 if "m" in sequence :
290286 # merge if requested
291287 # NOTE: this needs "unique=False" to make "iris.load()" work correctly.
292288 # TODO: make configurable via options.
293- cubes = cubes .merge (unique = False )
289+ cubelist = cubelist .merge (unique = False )
294290 if sequence [- 1 ] == "c" :
295291 # concat if it comes last
296- cubes = cubes .concatenate ()
292+ cubelist = cubelist .concatenate ()
297293
298294 # Repeat if requested, *and* this step reduced the number of cubes
299- if not options ["repeat_until_unchanged" ] or len (cubes ) >= n_original_cubes :
295+ if not options ["repeat_until_unchanged" ] or len (cubelist ) >= n_original_cubes :
300296 break
301297
302- return cubes
298+ return cubelist
303299
304300
305301def _combine_load_cubes (cubes ):
306- # A special version to call _combine_cubes while also implementing the
302+ # A special version to call _combine_cubes_inner while also implementing the
307303 # _MULTIREF_DETECTION behaviour
308304 from iris import LOAD_POLICY
309305
@@ -318,4 +314,4 @@ def _combine_load_cubes(cubes):
318314 if _MULTIREF_DETECTION .found_multiple_refs :
319315 options ["merge_concat_sequence" ] += "c"
320316
321- return _combine_cubes (cubes , options )
317+ return _combine_cubes_inner (cubes , options )
0 commit comments