コード例 #1
0
def rt2dt(array, units_in, units_out=None, dummy1=None):
    '''Convert reference times  to date-time objects

    The returned array is always independent.

    :Parameters:

        array: numpy array-like

        units_in: `Units`

        units_out: *optional*
            Ignored.

        dummy1:
            Ignored.

    :Returns:

        `numpy.ndarray`
            An array of `cf.Datetime` objects with the same shape as
            *array*.

    '''
    mask = None
    if numpy_ma_isMA(array):
        # num2date has issues if the mask is nomask
        mask = array.mask
        if mask is numpy_ma_nomask or not numpy_ma_is_masked(array):
            array = array.view(numpy_ndarray)

    units = units_in.units
    calendar = getattr(units_in, 'calendar', 'standard')

    array = cftime.num2date(array, units, calendar,
                            only_use_cftime_datetimes=True)
#    array = units_in._utime.num2date(array)

    if mask is not None:
        array = numpy_ma_masked_where(mask, array)

    ndim = numpy_ndim(array)

    if mask is None:
        # There is no missing data
        return numpy_array(array, dtype=object)
        # return numpy_vectorize(
        #     partial(dt2Dt, calendar=units_in._calendar),
        #     otypes=[object])(array)
    else:
        # There is missing data
        if not ndim:
            return numpy_ma_masked_all((), dtype=object)
        else:
            # array = numpy_array(array)
            # array = numpy_vectorize(
            #     partial(dt2Dt, calendar=units_in._calendar),
            #     otypes=[object])(array)
            return numpy_ma_masked_where(mask, array)
コード例 #2
0
def pmax(x, y):
    """The element-wise maximum of two arrays.

    :Parameters:

        x: array-like
           May be updated in place and should not be used again.

        y: array-like
           Will not be updated in place.

    :Returns:

        `numpy.ndarray`

    """
    if numpy_ma_isMA(x):
        if numpy_ma_isMA(y):
            # x and y are both masked
            z = numpy_maximum(x, y)
            z = numpy_ma_where(x.mask & ~y.mask, y, z)
            x = numpy_ma_where(y.mask & ~x.mask, x, z)
            if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
                x = numpy_array(x)
        else:
            # Only x is masked
            z = numpy_maximum(x, y)
            x = numpy_ma_where(x.mask, y, z)
            if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
                x = numpy_array(x)
    elif numpy_ma_isMA(y):
        # Only y is masked
        z = numpy_maximum(x, y)
        x = numpy_ma_where(y.mask, x, z)
        if x.mask is numpy_ma_nomask:  # not numpy_any(x.mask):
            x = numpy_array(x)
    else:
        # x and y are both unmasked
        if not numpy_ndim(x):
            # Make sure that we have a numpy array (as opposed to,
            # e.g. a numpy.float64)
            x = numpy_asanyarray(x)

        numpy_maximum(x, y, out=x)

    return x
コード例 #3
0
def pmin(x, y):
    '''TODO

    :Parameters:

        x: `numpy.ndarray`
           May be updated in place and should not be used again.

        y: `numpy.ndarray`
           Will not be updated in place.

    :Returns:

        out: `numpy.ndarray`

    '''
    if numpy_ma_isMA(x):
        if numpy_ma_isMA(y):
            # x and y are both masked
            z = numpy_minimum(x, y)
            z = numpy_ma_where(x.mask & ~y.mask, y, z)
            x = numpy_ma_where(y.mask & ~x.mask, x, z)
            if x.mask is numpy_ma_nomask:
                x = numpy_array(x)
        else:
            # Only x is masked
            z = numpy_minimum(x, y)
            x = numpy_ma_where(x.mask, y, z)
            if x.mask is numpy_ma_nomask:
                x = numpy_array(x)
    elif numpy_ma_isMA(y):
        # Only y is masked
        z = numpy_minimum(x, y)
        x = numpy_ma_where(y.mask, x, z)
        if x.mask is numpy_ma_nomask:
            x = numpy_array(x)
    else:
        # x and y are both unmasked
        if not numpy_ndim(x):
            # Make sure that we have a numpy array (as opposed to,
            # e.g. a numpy.float64)
            x = numpy_asanyarray(x)

        numpy_minimum(x, y, out=x)

    return x
コード例 #4
0
def psum(x, y):
    """Add two arrays element-wise.

    If either or both of the arrays are masked then the output array is
    masked only where both input arrays are masked.

    :Parameters:

        x: numpy array-like
           *Might be updated in place*.

        y: numpy array-like
           Will not be updated in place.

    :Returns:

        `numpy.ndarray`

    **Examples:**

    >>> c = psum(a, b)

    """
    if numpy_ma_isMA(x):
        if numpy_ma_isMA(y):
            # x and y are both masked
            x_mask = x.mask
            x = x.filled(0)
            x += y.filled(0)
            x = numpy_ma_array(x, mask=x_mask & y.mask, copy=False)
        else:
            # Only x is masked
            x = x.filled(0)
            x += y
    elif numpy_ma_isMA(y):
        # Only y is masked
        x += y.filled(0)
    else:
        # x and y are both unmasked
        x += y

    return x
コード例 #5
0
def double_precision(a):
    """Convert the input array to double precision.

    :Parameters:

        a: `numpy.ndarray`

    :Returns:

        `numpy.ndarray`

    """
    char = a.dtype.char
    if char == "f":
        newtype = float
    elif char == "i":
        newtype = int
    else:
        return a

    if numpy_ma_isMA(a):
        return a.astype(newtype)
    else:
        return a.astype(newtype, copy=False)