コード例 #1
0
ファイル: pywrapper.py プロジェクト: wachsylon/cmor
def axis(table_entry,
         units=None,
         length=None,
         coord_vals=None,
         cell_bounds=None,
         interval=None):
    """ Creates an cmor_axis
    Usage:
    axis_id = axis(
    table_entry,
    units=None,
    length=None,
    coord_vals=None,
    cell_bounds=None,
    interval=None)
    Where:
    table_entry: table_entry in the cmor table
    units: the axis units
    length: the number of coord_vals to actuall y use, or simply the number of coord_vals in case in index_only axes
    coord_vals: cmds2 axis or numpy/MV2 array (1D)
    cell_bounds: numpy or MV2 array, if coord_vals is a cdms2 axis then will try to obtain bounds from it
    interval: a string used for time axes only (???)
    """
    if not isinstance(table_entry, six.string_types):
        raise Exception(
            "You need to pass a table_entry to match in the cmor table")

    if (table_entry in ['time2', 'time3']):
        set_climatology(True)

    if coord_vals is None:
        if cell_bounds is not None:
            raise Exception("you passed cell_bounds but no coords")
    else:
        if has_cdms2 and isinstance(coord_vals, cdms2.axis.TransientAxis):
            if units is None:
                if hasattr(coord_vals, "units"):
                    units = coord_vals.units
            if cell_bounds is None:
                cell_bounds = coord_vals.getBounds()

            if interval is None and hasattr(coord_vals, "interval"):
                interval = coord_vals.interval
            coord_vals = numpy.ascontiguousarray(coord_vals[:])
        elif isinstance(coord_vals, (list, tuple)):
            coord_vals = numpy.ascontiguousarray(coord_vals)
        elif has_cdms2 and cdms2.isVariable(coord_vals):
            if units is None:
                if hasattr(coord_vals, "units"):
                    units = coord_vals.units
            if interval is None and hasattr(coord_vals, "interval"):
                interval = coord_vals.interval
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())
        elif has_oldma and numpy.oldnumeric.ma.isMA(coord_vals):
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())
        elif numpy.ma.isMA(coord_vals):
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())

        if not isinstance(coord_vals, numpy.ndarray):
            raise Exception(
                "Error coord_vals must be an array or cdms2 axis or list/tuple"
            )

        if numpy.ndim(coord_vals) > 1:
            raise Exception("Error, you must pass a 1D array!")

    if numpy.ma.isMA(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_cdms2 and cdms2.isVariable(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_cdms2 and cdms2.isVariable(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif isinstance(cell_bounds, (list, tuple)):
        cell_bounds = numpy.ascontiguousarray(cell_bounds)

    if cell_bounds is not None:
        if numpy.ndim(cell_bounds) > 2:
            raise Exception("Error cell_bounds rank must be at most 2")
        if numpy.ndim(cell_bounds) == 2:
            if cell_bounds.shape[0] != coord_vals.shape[0]:
                raise Exception(
                    "Error, coord_vals and cell_bounds do not have the same length"
                )
            if cell_bounds.shape[1] != 2:
                raise Exception(
                    "Error, cell_bounds' second dimension must be of length 2")
            cbnds = 2
            cell_bounds = numpy.ascontiguousarray(numpy.ravel(cell_bounds))
        else:
            cbnds = 1
            if len(cell_bounds) != len(coord_vals) + 1:
                raise Exception(
                    "error cell_bounds are %i long and axes coord_vals are %i long this is not consistent"
                    % (len(cell_bounds), len(coord_vals)))
    else:
        cbnds = 0

    if coord_vals is not None:
        l = len(coord_vals)
        type = coord_vals.dtype.char[0]

        if not type in ['i', 'l', 'f', 'd', 'S', 'U']:
            raise Exception("error allowed data type are: i,l,f,d or S")

        if type == 'S' or type == 'U':
            type = 'c'
            cbnds = 0
            for s in coord_vals:
                # print 'testing:',s,len(s)
                if len(s) > cbnds:
                    cbnds = len(s)
            # cbnds+=1
    else:
        l = 0
        type = 'd'

    if cell_bounds is not None:
        if type != cell_bounds.dtype.char:
            cell_bounds = cell_bounds.astype(type)

    if units is None:
        if coord_vals is not None:
            raise Exception(
                "Error you need to provide the units your coord_vals are in")
        else:
            units = "1"

    if interval is None:
        interval = ""

    if length is not None:
        l = int(length)

    return _cmor.axis(table_entry, units, l, coord_vals, str.encode(type),
                      cell_bounds, cbnds, interval)
コード例 #2
0
ファイル: pywrapper.py プロジェクト: PCMDI/cmor
def axis(table_entry, units=None, length=None,
         coord_vals=None, cell_bounds=None, interval=None):
    """ Creates an cmor_axis
    Usage:
    axis_id = axis(
    table_entry,
    units=None,
    length=None,
    coord_vals=None,
    cell_bounds=None,
    interval=None)
    Where:
    table_entry: table_entry in the cmor table
    units: the axis units
    length: the number of coord_vals to actuall y use, or simply the number of coord_vals in case in index_only axes
    coord_vals: cmds2 axis or numpy/MV2 array (1D)
    cell_bounds: numpy or MV2 array, if coord_vals is a cdms2 axis then will try to obtain bounds from it
    interval: a string used for time axes only (???)
    """
    if not isinstance(table_entry, six.string_types):
        raise Exception(
            "You need to pass a table_entry to match in the cmor table")

    if(table_entry in ['time2', 'time3']):
        set_climatology(True)

    if coord_vals is None:
        if cell_bounds is not None:
            raise Exception("you passed cell_bounds but no coords")
    else:
        if has_cdms2 and isinstance(coord_vals, cdms2.axis.TransientAxis):
            if units is None:
                if hasattr(coord_vals, "units"):
                    units = coord_vals.units
            if cell_bounds is None:
                cell_bounds = coord_vals.getBounds()

            if interval is None and hasattr(coord_vals, "interval"):
                interval = coord_vals.interval
            coord_vals = numpy.ascontiguousarray(coord_vals[:])
        elif isinstance(coord_vals, (list, tuple)):
            coord_vals = numpy.ascontiguousarray(coord_vals)
        elif has_cdms2 and cdms2.isVariable(coord_vals):
            if units is None:
                if hasattr(coord_vals, "units"):
                    units = coord_vals.units
            if interval is None and hasattr(coord_vals, "interval"):
                interval = coord_vals.interval
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())
        elif has_oldma and numpy.oldnumeric.ma.isMA(coord_vals):
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())
        elif numpy.ma.isMA(coord_vals):
            coord_vals = numpy.ascontiguousarray(coord_vals.filled())

        if not isinstance(coord_vals, numpy.ndarray):
            raise Exception(
                "Error coord_vals must be an array or cdms2 axis or list/tuple")

        if numpy.ndim(coord_vals) > 1:
            raise Exception("Error, you must pass a 1D array!")

    if numpy.ma.isMA(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_oldma and numpy.oldnumeric.ma.isMA(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_cdms2 and cdms2.isVariable(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif has_cdms2 and cdms2.isVariable(cell_bounds):
        cell_bounds = numpy.ascontiguousarray(cell_bounds.filled())
    elif isinstance(cell_bounds, (list, tuple)):
        cell_bounds = numpy.ascontiguousarray(cell_bounds)

    if cell_bounds is not None:
        if numpy.ndim(cell_bounds) > 2:
            raise Exception("Error cell_bounds rank must be at most 2")
        if numpy.ndim(cell_bounds) == 2:
            if cell_bounds.shape[0] != coord_vals.shape[0]:
                raise Exception(
                    "Error, coord_vals and cell_bounds do not have the same length")
            if cell_bounds.shape[1] != 2:
                raise Exception(
                    "Error, cell_bounds' second dimension must be of length 2")
            cbnds = 2
            cell_bounds = numpy.ascontiguousarray(numpy.ravel(cell_bounds))
        else:
            cbnds = 1
            if len(cell_bounds) != len(coord_vals) + 1:
                raise Exception(
                    "error cell_bounds are %i long and axes coord_vals are %i long this is not consistent" %
                    (len(cell_bounds), len(coord_vals)))
    else:
        cbnds = 0

    if coord_vals is not None:
        l = len(coord_vals)
        type = coord_vals.dtype.char[0]

        if not type in ['i', 'l', 'f', 'd', 'S']:
            raise Exception("error allowed data type are: i,l,f,d or S")

        if type == 'S':
            type = 'c'
            cbnds = 0
            for s in coord_vals:
                # print 'testing:',s,len(s)
                if len(s) > cbnds:
                    cbnds = len(s)
            # cbnds+=1
    else:
        l = 0
        type = 'd'

    if cell_bounds is not None:
        if type != cell_bounds.dtype.char:
            cell_bounds = cell_bounds.astype(type)

    if units is None:
        if coord_vals is not None:
            raise Exception(
                "Error you need to provide the units your coord_vals are in")
        else:
            units = "1"

    if interval is None:
        interval = ""

    if length is not None:
        l = int(length)

    return _cmor.axis(table_entry, units, l, coord_vals,
                      str.encode(type), cell_bounds, cbnds, interval)