Пример #1
0
def write(var_id,
          data,
          ntimes_passed=None,
          file_suffix="",
          time_vals=None,
          time_bnds=None,
          store_with=None):
    """ write data to a cmor variable
    Usage:
    ierr = write(var_id,data,ntimes_passed=None,file_suffix="",time_vals=None,time_bnds=None,store_with=None
    """
    if not isinstance(var_id, (int, numpy.int, numpy.int32, numpy.int64)):
        raise Exception("error var_id must be an integer")
    var_id = int(var_id)

    if not isinstance(file_suffix, six.string_types):
        raise Exception("Error file_suffix must be a string")

    if store_with is not None:
        if not isinstance(store_with,
                          (int, numpy.int, numpy.int32, numpy.int64)):
            raise Exception("error store_with must be an integer")
        store_with = int(store_with)

    if numpy.ma.isMA(data):
        data = numpy.ascontiguousarray(data.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(data):
        data = numpy.ascontiguousarray(data.filled())
    elif has_cdms2 and cdms2.isVariable(data):
        if time_vals is None:
            time_vals = data.getTime()
        data = numpy.ascontiguousarray(data.filled())
    elif isinstance(data, (list, tuple)):
        data = numpy.ascontiguousarray(data)
    elif not isinstance(data, numpy.ndarray):
        raise Exception("Error could not convert data to a numpy array")

    if time_vals is None:
        pass
    elif numpy.ma.isMA(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif has_cdms2 and isinstance(time_vals, cdms2.axis.TransientAxis):
        if time_bnds is None:
            time_bnds = time_vals.getBounds()
        time_vals = numpy.ascontiguousarray(time_vals[:])
    elif has_cdms2 and cdms2.isVariable(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif isinstance(time_vals, (list, tuple)):
        time_vals = numpy.ascontiguousarray(time_vals)
    elif not isinstance(time_vals, numpy.ndarray):
        try:
            time_vals = numpy.ascontiguousarray(time_vals)
        except BaseException:
            raise Exception(
                "Error could not convert time_vals to a numpy array")

    if time_vals is not None:
        type = time_vals.dtype.char
        if not type in ['f', 'd', 'i', 'l']:
            raise Exception(
                "Error time_vals type must one of: 'f','d','i','l', please convert first"
            )
        time_vals = time_vals.astype("d")

    if ntimes_passed is None:
        if time_vals is None:
            ntimes_passed = 0
        else:
            ntimes_passed = len(time_vals)
    if not isinstance(ntimes_passed,
                      (int, numpy.int, numpy.int32, numpy.int64)):
        raise Exception("error ntimes_passed must be an integer")
    ntimes_passed = int(ntimes_passed)

    # At that ponit we check that shapes matches!
    goodshape = _cmor.get_original_shape(var_id, 1)
    osh = data.shape
    ogoodshape = list(goodshape)
    sh = list(osh)
    j = 0
    while sh.count(1) > 0:
        sh.remove(1)
    while goodshape.count(1) > 0:
        goodshape.remove(1)
    while goodshape.count(0) > 0:
        if (len(goodshape) == len(sh)):
            index = goodshape.index(0)
            del sh[index]
            del goodshape[index]
        else:  # assume time==1 was removed
            goodshape.remove(0)

    for i in range(len(goodshape)):
        if sh[j] != goodshape[i]:
            if goodshape[i] != 1:
                msg = "Error: your data shape (%s) does not match the expected variable shape (%s)\nCheck your variable dimensions before caling cmor_write" % (
                    str(osh), str(ogoodshape))
                warnings.warn(msg)
        j += 1

    # Check if there is enough data for the number of times passed
    if ntimes_passed < 0:
        raise Exception("ntimes_passed must be a positive integer")

    expected_size = ntimes_passed
    for d in goodshape:
        expected_size *= d
    passed_size = 1
    for d in osh:
        passed_size *= d
    if expected_size > passed_size:
        raise Exception(
            "not enough data is being passed for the number of times passed")

    data = numpy.ascontiguousarray(numpy.ravel(data))

    if time_bnds is not None:
        if numpy.ma.isMA(time_bnds):
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif has_oldma and numpy.oldnumeric.ma.isMA(time_bnds):
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif has_cdms2 and cdms2.isVariable(time_bnds):
            if time_vals is None:
                time_vals = time_bnds.getTime()
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif isinstance(time_bnds, (list, tuple)):
            time_bnds = numpy.ascontiguousarray(time_bnds)
        elif not isinstance(time_bnds, numpy.ndarray):
            raise Exception(
                "Error could not convert time_bnds to a numpy array")

        if numpy.ndim(time_bnds) > 2:
            raise Exception("bounds rank cannot be greater than 2")
        elif numpy.ndim(time_bnds) == 2:
            if time_bnds.shape[1] != 2:
                raise Exception(
                    "error time_bnds' 2nd dimension must be of length 2")
            bnds = []
            if time_bnds.shape[0] > 1 and get_climatology() is False:
                _check_time_bounds_contiguous(time_bnds)
                bnds = _flatten_time_bounds(time_bnds)
            else:
                bnds = time_bnds.ravel()
            time_bnds = numpy.array(bnds)
        else:  # ok it is a rank 1!
            if numpy.ndim(time_vals) == 0:
                ltv = 1
            else:
                ltv = len(time_vals)
            if len(time_bnds) != ltv + 1:
                raise Exception(
                    "error time_bnds if 1D must be 1 elt greater than time_vals, you have %i vs %i"
                    % (len(time_bnds), ltv))
            bnds = []
            for i in range(ltv):
                bnds.append([time_bnds[i], time_bnds[i + 1]])
            bnds = numpy.array(bnds)
            bnds = _flatten_time_bounds(bnds)
            time_bnds = numpy.array(bnds)

    if time_bnds is not None:
        type = time_bnds.dtype.char
        if not type in ['f', 'd', 'i', 'l']:
            raise Exception(
                "Error time_bnds type must one of: 'f','d','i','l', please convert first"
            )
        time_bnds = time_bnds.astype("d")

    type = data.dtype.char
    if not type in ['f', 'd', 'i', 'l']:
        raise Exception(
            "Error data type must one of: 'f','d','i','l', please convert first"
        )

    return _cmor.write(var_id, data, type, file_suffix, ntimes_passed,
                       time_vals, time_bnds, store_with)
Пример #2
0
def write(var_id, data, ntimes_passed=None, file_suffix="",
          time_vals=None, time_bnds=None, store_with=None):
    """ write data to a cmor variable
    Usage:
    ierr = write(var_id,data,ntimes_passed=None,file_suffix="",time_vals=None,time_bnds=None,store_with=None
    """
    if not isinstance(var_id, (int, numpy.int, numpy.int32)):
        raise Exception("error var_id must be an integer")
    var_id = int(var_id)

    if not isinstance(file_suffix, six.string_types):
        raise Exception("Error file_suffix must be a string")

    if store_with is not None:
        if not isinstance(store_with, (int, numpy.int, numpy.int32)):
            raise Exception("error store_with must be an integer")
        store_with = int(store_with)

    if numpy.ma.isMA(data):
        data = numpy.ascontiguousarray(data.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(data):
        data = numpy.ascontiguousarray(data.filled())
    elif has_cdms2 and cdms2.isVariable(data):
        if time_vals is None:
            time_vals = data.getTime()
        data = numpy.ascontiguousarray(data.filled())
    elif isinstance(data, (list, tuple)):
        data = numpy.ascontiguousarray(data)
    elif not isinstance(data, numpy.ndarray):
        raise Exception("Error could not convert data to a numpy array")

    if time_vals is None:
        pass
    elif numpy.ma.isMA(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif has_cdms2 and isinstance(time_vals, cdms2.axis.TransientAxis):
        if time_bnds is None:
            time_bnds = time_vals.getBounds()
        time_vals = numpy.ascontiguousarray(time_vals[:])
    elif has_cdms2 and cdms2.isVariable(time_vals):
        time_vals = numpy.ascontiguousarray(time_vals.filled())
    elif isinstance(time_vals, (list, tuple)):
        time_vals = numpy.ascontiguousarray(time_vals)
    elif not isinstance(time_vals, numpy.ndarray):
        try:
            time_vals = numpy.ascontiguousarray(time_vals)
        except BaseException:
            raise Exception(
                "Error could not convert time_vals to a numpy array")

    if time_vals is not None:
        type = time_vals.dtype.char
        if not type in ['f', 'd', 'i', 'l']:
            raise Exception(
                "Error time_vals type must one of: 'f','d','i','l', please convert first")
        time_vals = time_vals.astype("d")

    if ntimes_passed is None:
        if time_vals is None:
            ntimes_passed = 0
        else:
            ntimes_passed = len(time_vals)
    if not isinstance(ntimes_passed, (int, numpy.int, numpy.int32)):
        raise Exception("error ntimes_passed must be an integer")
    ntimes_passed = int(ntimes_passed)

    # At that ponit we check that shapes matches!
    goodshape = _cmor.get_original_shape(var_id, 1)
    osh = data.shape
    ogoodshape = list(goodshape)
    sh = list(osh)
    j = 0
    while sh.count(1) > 0:
        sh.remove(1)
    while goodshape.count(1) > 0:
        goodshape.remove(1)
    while goodshape.count(0) > 0:
        if( len(goodshape) == len(sh)):
            index = goodshape.index(0)
            del sh[index]
            del goodshape[index]
        else:  # assume time==1 was removed
            goodshape.remove(0)

    for i in range(len(goodshape)):
        if sh[j] != goodshape[i]:
            if goodshape[i] != 1:
                msg = "Error: your data shape (%s) does not match the expected variable shape (%s)\nCheck your variable dimensions before caling cmor_write" % (str(osh), str(ogoodshape))
                warnings.warn(msg)
        j += 1

    data = numpy.ascontiguousarray(numpy.ravel(data))

    if time_bnds is not None: 
        if numpy.ma.isMA(time_bnds):
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif has_oldma and numpy.oldnumeric.ma.isMA(time_bnds):
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif has_cdms2 and cdms2.isVariable(time_bnds):
            if time_vals is None:
                time_vals = time_bnds.getTime()
            time_bnds = numpy.ascontiguousarray(time_bnds.filled())
        elif isinstance(time_bnds, (list, tuple)):
            time_bnds = numpy.ascontiguousarray(time_bnds)
        elif not isinstance(time_bnds, numpy.ndarray):
            raise Exception(
                "Error could not convert time_bnds to a numpy array")

        if numpy.ndim(time_bnds) > 2:
            raise Exception("bounds rank cannot be greater than 2")
        elif numpy.ndim(time_bnds) == 2:
            if time_bnds.shape[1] != 2:
                raise Exception(
                    "error time_bnds' 2nd dimension must be of length 2")
            bnds = []
            if time_bnds.shape[0] > 1 and get_climatology() is False:
                _check_time_bounds_contiguous(time_bnds)
                bnds = _flatten_time_bounds(time_bnds)
            else:
                bnds = time_bnds.ravel()
            time_bnds = numpy.array(bnds)
        else:  # ok it is a rank 1!
            if numpy.ndim(time_vals) == 0:
                ltv = 1
            else:
                ltv = len(time_vals)
            if len(time_bnds) != ltv + 1:
                raise Exception(
                    "error time_bnds if 1D must be 1 elt greater than time_vals, you have %i vs %i" %
                    (len(time_bnds), ltv))
            bnds = []
            for i in range(ltv):
                bnds.append([time_bnds[i], time_bnds[i + 1]])
            bnds = numpy.array(bnds)
            bnds = _flatten_time_bounds(bnds)
            time_bnds = numpy.array(bnds)

    if time_bnds is not None:
        type = time_bnds.dtype.char
        if not type in ['f', 'd', 'i', 'l']:
            raise Exception(
                "Error time_bnds type must one of: 'f','d','i','l', please convert first")
        time_bnds = time_bnds.astype("d")

    type = data.dtype.char
    if not type in ['f', 'd', 'i', 'l']:
        raise Exception(
            "Error data type must one of: 'f','d','i','l', please convert first")

    return _cmor.write(var_id, data, type, file_suffix, ntimes_passed,
                       time_vals, time_bnds, store_with)