Example #1
0
def _math_op_common(cube, operation_function, new_unit, new_dtype=None,
                    in_place=False):
    _assert_is_cube(cube)

    if in_place:
        new_cube = cube
        if cube.has_lazy_data():
            new_cube.data = operation_function(cube.lazy_data())
        else:
            try:
                operation_function(cube.data, out=cube.data)
            except TypeError:
                # Non ufunc function
                operation_function(cube.data)
    else:
        new_cube = cube.copy(data=operation_function(cube.core_data()))

    # If the result of the operation is scalar and masked, we need to fix up
    # the dtype
    if new_dtype is not None \
            and not new_cube.has_lazy_data() \
            and new_cube.data.shape == () \
            and ma.is_masked(new_cube.data):
        new_cube.data = ma.masked_array(0, 1, dtype=new_dtype)

    iris.analysis.clear_phenomenon_identity(new_cube)
    new_cube.units = new_unit
    return new_cube
Example #2
0
def exp(cube, in_place=False):
    """
    Calculate the exponential (exp(x)) of the cube.

    Args:

    * cube:
        An instance of :class:`iris.cube.Cube`.

    .. note::

        Taking an exponential will return a cube with dimensionless units.

    Kwargs:

    * in_place:
        Whether to create a new Cube, or alter the given "cube".

    Returns:
        An instance of :class:`iris.cube.Cube`.

    """
    _assert_is_cube(cube)
    new_dtype = _output_dtype(np.exp, cube.dtype, in_place=in_place)
    op = da.exp if cube.has_lazy_data() else np.exp
    return _math_op_common(cube, op, cf_units.Unit('1'), new_dtype,
                           in_place=in_place)
Example #3
0
 def test_lazy_concatenate_masked_array_mixed_deferred(self):
     c1 = self.build_lazy_cube([1, 2])
     c2 = self.build_lazy_cube([3, 4, 5])
     c2.data = np.ma.masked_greater(c2.data, 3)
     cube, = concatenate([c1, c2])
     self.assertTrue(cube.has_lazy_data())
     self.assertTrue(ma.isMaskedArray(cube.data))
Example #4
0
 def test_lazy_biggus_concatenate_masked_array_mixed_deffered(self):
     c1 = self.build_lazy_cube([1, 2])
     c2 = self.build_lazy_cube([3, 4, 5])
     c2.data = np.ma.masked_greater(c2.data, 3)
     self.assertFalse(c2.has_lazy_data())
     cube, = concatenate([c1, c2])
     self.assertTrue(cube.has_lazy_data())
     self.assertIsInstance(cube.data, np.ma.MaskedArray)
Example #5
0
def abs(cube, in_place=False):
    """
    Calculate the absolute values of the data in the Cube provided.

    Args:

    * cube:
        An instance of :class:`iris.cube.Cube`.

    Kwargs:

    * in_place:
        Whether to create a new Cube, or alter the given "cube".

    Returns:
        An instance of :class:`iris.cube.Cube`.

    """
    _assert_is_cube(cube)
    new_dtype = _output_dtype(np.abs, cube.dtype, in_place=in_place)
    op = da.absolute if cube.has_lazy_data() else np.abs
    return _math_op_common(cube, op, cube.units, new_dtype, in_place=in_place)
Example #6
0
def exponentiate(cube, exponent, in_place=False):
    """
    Returns the result of the given cube to the power of a scalar.

    Args:

    * cube:
        An instance of :class:`iris.cube.Cube`.
    * exponent:
        The integer or floating point exponent.

        .. note:: When applied to the cube's unit, the exponent must
            result in a unit that can be described using only integer
            powers of the basic units.

            e.g. Unit('meter^-2 kilogram second^-1')

    Kwargs:

    * in_place:
        Whether to create a new Cube, or alter the given "cube".

    Returns:
        An instance of :class:`iris.cube.Cube`.

    """
    _assert_is_cube(cube)
    new_dtype = _output_dtype(operator.pow, cube.dtype, _get_dtype(exponent),
                              in_place=in_place)
    if cube.has_lazy_data():
        def power(data):
            return operator.pow(data, exponent)
    else:
        def power(data, out=None):
            return np.power(data, exponent, out)

    return _math_op_common(cube, power, cube.units ** exponent, new_dtype,
                           in_place=in_place)
Example #7
0
def log10(cube, in_place=False):
    """
    Calculate the base-10 logarithm of the cube.

    Args:

    * cube:
        An instance of :class:`iris.cube.Cube`.

    Kwargs:

    * in_place:
        Whether to create a new Cube, or alter the given "cube".

    Returns:
        An instance of :class:`iris.cube.Cube`.

    """
    _assert_is_cube(cube)
    new_dtype = _output_dtype(np.log10, cube.dtype, in_place=in_place)
    op = da.log10 if cube.has_lazy_data() else np.log10
    return _math_op_common(cube, op, cube.units.log(10), new_dtype,
                           in_place=in_place)
Example #8
0
def log2(cube, in_place=False):
    """
    Calculate the base-2 logarithm of the cube.

    Args:

    * cube:
        An instance of :class:`iris.cube.Cube`.

    Kwargs:lib/iris/tests/unit/analysis/maths/test_subtract.py

    * in_place:
        Whether to create a new Cube, or alter the given "cube".

    Returns:
        An instance of :class:`iris.cube.Cube`.

    """
    _assert_is_cube(cube)
    new_dtype = _output_dtype(np.log2, cube.dtype, in_place=in_place)
    op = da.log2 if cube.has_lazy_data() else np.log2
    return _math_op_common(cube, op, cube.units.log(2), new_dtype,
                           in_place=in_place)
Example #9
0
 def assert_is_not_lazy(self, cube):
     self.assertFalse(cube.has_lazy_data())
Example #10
0
 def assert_is_lazy(self, cube):
     self.assertTrue(cube.has_lazy_data())
Example #11
0
 def test_lazy_biggus_concatenate(self):
     c1 = self.build_lazy_cube([1, 2])
     c2 = self.build_lazy_cube([3, 4, 5])
     cube, = concatenate([c1, c2])
     self.assertTrue(cube.has_lazy_data())
     self.assertNotIsInstance(cube.data, np.ma.MaskedArray)
Example #12
0
 def _is_concrete(self, cube):
     self.assertFalse(cube.has_lazy_data())
Example #13
0
 def test_lazy_concatenate(self):
     c1 = self.build_lazy_cube([1, 2])
     c2 = self.build_lazy_cube([3, 4, 5])
     cube, = concatenate([c1, c2])
     self.assertTrue(cube.has_lazy_data())
     self.assertFalse(ma.isMaskedArray(cube.data))