예제 #1
0
파일: pywrapper.py 프로젝트: jkettleb/cmor
def set_grid_mapping(grid_id,mapping_name,parameter_names,parameter_values=None,parameter_units=None):
    """Sets the grid mapping for CF convention
    Usage:
       set_grid_mapping(grid_id,mapping_name,parameter_names,parameter_values,parameter_units)
    Where:
       grid_id :: grid_id return by cmor.grid
       mapping_name     :: name of the mapping (see CF conventions)
       parameter_names  :: list of parameter names or dictionary with name as keys and values can be either [value,units] list/tuple or dictionary with "values"/"units" as keys
       parameter_values :: array/list of parameter values in the same order of parameter_names (ignored if parameter_names is ditcionary)
       parameter_units  :: array/list of parameter units  in the same order of parameter_names (ignored if parameter_names is ditcionary)
    """
    if not isinstance(grid_id,(numpy.int32,int,long)):
        raise Exception, "grid_id must be an integer: %s"%type(grid_id)
    if not isinstance(mapping_name,str):
        raise Exception, "mapping name must be a string"

    if isinstance(parameter_names,dict):
        pnams = []
        pvals = []
        punit = []
        for k in parameter_names.keys():
            pnams.append(k)
            val = parameter_names[k]
            if isinstance(val,dict):
                ks = val.keys()
                if not 'value' in ks or not 'units' in ks:
                    raise Exception, "error parameter_names key '%s' dictionary does not contain both 'units' and 'value' keys" % k
                pvals.append(val['value'])
                punit.append(val['units'])
            elif isinstance(val,(list,tuple)):
                if len(val)>2:
                    raise Exception, "parameter_names '%s' as more than 2 values" % k
                for v in val:
                    if isinstance(v,str):
                        punit.append(v)
                    try:
                        pvals.append(float(v))
                    except:
                        pass
                if len(pvals)!=len(punits) or len(pvals)!=len(pnams):
                    raise Exception, "could not figure out values for parameter_name: '%s' " % k
            else:
                raise Exception, "could not figure out values for parameter_name: '%s' " % k
    elif isinstance(parameter_names,(list,tuple)):
        pnms = list(parameter_names)
        # now do code for parameter_units
        if parameter_values is None:
            raise Exception, "you must pass a list or array for parameter_values"
        if parameter_units is None:
            raise Exception, "you must pass a list for parameter_units"
        if not isinstance(parameter_units,(list,tuple)):
            raise Exception, "you must pass a list for parameter_units"
        if len(parameter_units)!=len(pnms):
            raise Exception, "length of parameter_units list does not match length of parameter_names"
        punit = list(parameter_units)
        if isinstance(parameter_values,(list,tuple)):
            pvals = list(parameter_values)
        else:
            try:
                pvals = numpy.ascontiguousarray(parameter_values.filled())
            except:
                raise Exception, "Error could not convert parameter_values to a numpy array"
        if len(pvals) != len(parameter_names):
            raise Exception, "length of parameter_values list does not match length of parameter_names"
    else:
        raise Exception, "parameter_names must be either dictionary or list"

    pvals = numpy.ascontiguousarray(pvals).astype('d')
    return _cmor.set_grid_mapping(grid_id,mapping_name,pnms,pvals,punit)
예제 #2
0
def set_grid_mapping(grid_id,
                     mapping_name,
                     parameter_names,
                     parameter_values=None,
                     parameter_units=None):
    """Sets the grid mapping for CF convention
    Usage:
       set_grid_mapping(grid_id,mapping_name,parameter_names,parameter_values,parameter_units)
    Where:
       grid_id :: grid_id return by cmor.grid
       mapping_name     :: name of the mapping (see CF conventions)
       parameter_names  :: list of parameter names or dictionary with name as keys and values can be either [value,units] list/tuple or dictionary with "values"/"units" as keys
       parameter_values :: array/list of parameter values in the same order of parameter_names (ignored if parameter_names is ditcionary)
       parameter_units  :: array/list of parameter units  in the same order of parameter_names (ignored if parameter_names is ditcionary)
    """
    if not isinstance(grid_id, (numpy.int32, int, long)):
        raise Exception, "grid_id must be an integer: %s" % type(grid_id)
    if not isinstance(mapping_name, str):
        raise Exception, "mapping name must be a string"

    if isinstance(parameter_names, dict):
        pnams = []
        pvals = []
        punit = []
        for k in parameter_names.keys():
            pnams.append(k)
            val = parameter_names[k]
            if isinstance(val, dict):
                ks = val.keys()
                if not 'value' in ks or not 'units' in ks:
                    raise Exception, "error parameter_names key '%s' dictionary does not contain both 'units' and 'value' keys" % k
                pvals.append(val['value'])
                punit.append(val['units'])
            elif isinstance(val, (list, tuple)):
                if len(val) > 2:
                    raise Exception, "parameter_names '%s' as more than 2 values" % k
                for v in val:
                    if isinstance(v, str):
                        punit.append(v)
                    try:
                        pvals.append(float(v))
                    except:
                        pass
                if len(pvals) != len(punits) or len(pvals) != len(pnams):
                    raise Exception, "could not figure out values for parameter_name: '%s' " % k
            else:
                raise Exception, "could not figure out values for parameter_name: '%s' " % k
    elif isinstance(parameter_names, (list, tuple)):
        pnms = list(parameter_names)
        # now do code for parameter_units
        if parameter_values is None:
            raise Exception, "you must pass a list or array for parameter_values"
        if parameter_units is None:
            raise Exception, "you must pass a list for parameter_units"
        if not isinstance(parameter_units, (list, tuple)):
            raise Exception, "you must pass a list for parameter_units"
        if len(parameter_units) != len(pnms):
            raise Exception, "length of parameter_units list does not match length of parameter_names"
        punit = list(parameter_units)
        if isinstance(parameter_values, (list, tuple)):
            pvals = list(parameter_values)
        else:
            try:
                pvals = numpy.ascontiguousarray(parameter_values.filled())
            except:
                raise Exception, "Error could not convert parameter_values to a numpy array"
        if len(pvals) != len(parameter_names):
            raise Exception, "length of parameter_values list does not match length of parameter_names"
    else:
        raise Exception, "parameter_names must be either dictionary or list"

    pvals = numpy.ascontiguousarray(pvals).astype('d')
    return _cmor.set_grid_mapping(grid_id, mapping_name, pnms, pvals, punit)