コード例 #1
0
ファイル: classes.py プロジェクト: PBrockmann/climaf
def fds(filename, simulation=None, variable=None, period=None, model=None) :
    """
    fds stands for FileDataSet; it allows to create a dataset simply
    by providing a filename and optionally a simulation name , a
    variable name, a period and a model name.

    For dataset attributes which are not provided, these defaults apply :

    - simulation : the filename basename (without suffix '.nc')
    - variable : the set of variables in the data file
    - period : the period actually covered by the data file (if it has time_bnds)
    - model : the 'model_id' attribute if it exists, otherwise : 'no_model'
    - project  : 'file' (with separator = '|')

    The following restriction apply to such datasets :

    - functions :py:func:`~climaf.classes.calias` and 
      :py:func:`~climaf.operators.derive` cannot be used for project 
      'file'
    
    Results are unforeseen if all variables do not have the same time axis
    
    Examples : See :download:`data_file.py <../examples/data_file.py>`
    
    """
    filename=os.path.expanduser(filename)
    if not os.path.exists(filename): 
        raise Climaf_Classes_Error("File %s does no exist"%filename)
    #
    if model is None : model=model_id(filename)
    if simulation is None : simulation=os.path.basename(filename)[0:-3]
    #
    if variable is None :
        lvars=varsOfFile(filename)
        if len(lvars)==0 : 
            raise Climaf_Classes_Error("No variable in file %s"%filename)
        variable=lvars.pop()
        for v in lvars : variable+=","+v
    else :
        lvars=variable.split(',')
        for v in lvars :
            if not fileHasVar(filename,v) :
                raise Climaf_Classes_Error("No variable %s in file %s"%(v,filename))
    #
    fperiod=timeLimits(filename)
    if period is None :
        if fperiod is None :
            raise Climaf_Classes_Error("Must provide a period for file %s "\
                                           %(filename))
        else :
            period=`fperiod`
    else :
        if fperiod and not fperiod.includes(init_period(period)) :
            raise Climaf_Classes_Error("Max period from file %s is %s"\
                                           %(filename,`fperiod`))
    #
    d=ds(project='file', model=model, simulation=simulation, 
         variable=variable, period=period, path=filename)
    d.files=filename
    return d
コード例 #2
0
ファイル: classes.py プロジェクト: jservonnat/climaf
def processDatasetArgs(**kwargs) :
    """
    Perfom basic checks on kwargs for functions cdataset and eds
    regarding the project where the dataset is defined
    Also complement with default values as handled by the
    project's definition and by cdef()
    """
    if 'project' in kwargs : project=kwargs['project']
    else : project= cdef("project")
    if project is None :
        raise Climaf_Classes_Error("Must provide a project (Can use cdef)")
    elif project not in cprojects :
        raise Climaf_Classes_Error(
            "Dataset's project '%s' has not "
            "been described by a call to cproject()"%project)
    attval=dict()
    attval["project"]=project
    sep=cprojects[project].separator
    #
    # Register facets values
    for facet in cprojects[project].facets :
        if facet in kwargs and kwargs[facet] : val=kwargs[facet]
        else: val=cdef(facet,project=project)
        attval[facet]=val
        if val :
            if isinstance(val,list) : listval=val
            else : listval=[val]
            for lval in listval :
                if isinstance(lval,str) and lval.find(sep) >= 0 :
                    Climaf_Classes_Error(
                        "You cannot use character '%s' when setting '%s=%s' because "
                        "it is the declared separator for project '%s'. "
                        "See help(cproject) for changing it, if needed"%(sep,facet,val,project))
        #print "initalizing facet %s with value"%(facet,val)
    #
    # Special processing for CMIP5 fixed fields : handling redundancy in facets
    if (attval['project'] == 'CMIP5'):
        if ( attval['table']=='fx' or attval['period']=='fx' or 
             attval['simulation']=='r0i0p0' or attval['frequency']=='fx') :
            attval['table']='fx' ; attval['period']='fx' 
            attval['simulation']='r0i0p0' ; attval['frequency']='fx'
    #
    errmsg=""
    for facet in cprojects[project].facets :
        if attval[facet] is None :
            e="Project '%s' needs facet '%s'. You may use cdef() for setting a default value"\
               %(project,facet)
            errmsg+=" "+e
    if errmsg != "" : raise Climaf_Classes_Error(errmsg)
    #
    #print "kw="+`kwargs`
    for facet in attval :
        # Facet specific processing
        if facet=='period' :
            if not isinstance(attval['period'],cperiod) :
                try :
                    attval['period']=init_period(attval['period'])
                except :
                    raise Climaf_Classes_Error("Cannot interpret period for %s"%`attval['period']`)
            #else :
            #    print "%s is a cperiod"%`attval['period']`
        #elif facet=='domain' and not type(attval['domain']) is str :
        #    # May be a list
        #    attval['domain']=eval(attval['domain'])
        # Check for typing or user's logic errors
        if not facet in cprojects[project].facets :
            e="Project %s doesn't have facet %s"%(project,facet)
            errmsg+=" "+e
    if errmsg != "" : raise Climaf_Classes_Error(errmsg)
    if 'period' in attval and not isinstance(attval['period'],cperiod) :
        Climaf_Classes_Error("at end of  process.. : period is not a cperiod")
    return attval