Source code for esmvalcore.preprocessor._other

"""Preprocessor functions that do not fit into any of the categories."""

import logging
from collections import defaultdict

import dask.array as da
import numpy as np

logger = logging.getLogger(__name__)

[docs] def clip(cube, minimum=None, maximum=None): """Clip values at a specified minimum and/or maximum value. Values lower than minimum are set to minimum and values higher than maximum are set to maximum. Parameters ---------- cube: iris.cube.Cube iris cube to be clipped minimum: float lower threshold to be applied on input cube data. maximum: float upper threshold to be applied on input cube data. Returns ------- iris.cube.Cube clipped cube. """ if minimum is None and maximum is None: raise ValueError("Either minimum, maximum or both have to be\ specified.") elif minimum is not None and maximum is not None: if maximum < minimum: raise ValueError("Maximum should be equal or larger than minimum.") = da.clip(cube.core_data(), minimum, maximum) return cube
def _groupby(iterable, keyfunc): """Group iterable by key function. The items are grouped by the value that is returned by the `keyfunc` Parameters ---------- iterable : list, tuple or iterable List of items to group keyfunc : callable Used to determine the group of each item. These become the keys of the returned dictionary Returns ------- dict Returns a dictionary with the grouped values. """ grouped = defaultdict(set) for item in iterable: key = keyfunc(item) grouped[key].add(item) return grouped def _group_products(products, by_key): """Group products by the given list of attributes.""" def grouper(product): return grouped = _groupby(products, keyfunc=grouper) return grouped.items() def get_array_module(*args): """Return the best matching array module. If at least one of the arguments is a :class:`dask.array.Array` object, the :mod:`dask.array` module is returned. In all other cases the :mod:`numpy` module is returned. """ for arg in args: if isinstance(arg, da.Array): return da return np