Changeset 844 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jun 16, 2016, 6:10:03 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `computevar_model': Function to provide the way to compute a CF-variable providing its name
  • `var_model': Function to provide the way to compute a CF-variable providing its name
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r831 r844  
    114114# var_dim_dimv: Function to bring back which variables with the values should be use for a given variable
    115115# varDimension: Function to find the variable with the values of a dimension. It is assumed, that given variable
     116# var_model: Function to provide the way to compute a CF-variable providing its name
    116117# varout: Function when we want to output variable values
    117118# varoutold: Function when we want to output variable values
     
    1510115102    return
    1510215103
     15104def var_model(varname, onc):
     15105    """ Function to provide the way to compute a CF-variable providing its name
     15106      varname= CF-compilant variable name
     15107      onc= netCDF file object from which the variable should be retrieved
     15108    >>> onc = NetCDFFile('/home/lluis/PY/wrfout_d01_2001-11-11_00:00:00', 'r')
     15109    >>> mod, diag = var_model('tas', onc)
     15110    ['T2'], None
     15111    >>> mod, diag = var_model('hurs', onc)
     15112    None, [['PSFC', 'T2', 'Q2']]
     15113    """
     15114    fname = 'var_model'
     15115
     15116    ncvars = onc.variables
     15117
     15118# Getting non-chceking variables
     15119    folder = os.path.dirname(os.path.realpath(__file__))
     15120    infile = folder + '/diagnostics.inf'
     15121    if not os.path.isfile(infile):
     15122        print errormsg
     15123        print '  ' + fname + ": File '" + infile + "' does not exist !!"
     15124        quit(-1)
     15125
     15126    ncf = open(infile, 'r')
     15127
     15128    for line in ncf:
     15129        if line[0:9] == '# NOcheck':
     15130            NOchk = line.split('=')[1].replace(' ','').replace('\n','').split(':')
     15131            ncf.close()
     15132            break
     15133
     15134    print NOchk
     15135
     15136# Variables from which CF variable might be computed
     15137    modelvars = gen.CFvar_MODvar(varname)
     15138    diagvars = gen.CFvar_DIAGvar(varname)
     15139
     15140    computevarmod = []
     15141    if modelvars is not None:
     15142        for var in modelvars:
     15143            if gen.searchInlist(ncvars,var): computevarmod.append(var)
     15144    if len(computevarmod) < 1: computevarmod = None
     15145
     15146    computevardiag = []
     15147    if diagvars is not None:
     15148        for combovar in diagvars:
     15149            alltrue = False
     15150            if len(combovar) > 1:
     15151                for cvar in combovar:
     15152                    if gen.searchInlist(NOchk,cvar):
     15153                        alltrue = True
     15154                    else:
     15155                        if gen.searchInlist(ncvars,cvar): alltrue = True
     15156            else:
     15157                if gen.searchInlist(ncvars,combovar): alltrue = True
     15158
     15159            if alltrue: computevardiag.append(combovar)
     15160
     15161    if len(computevardiag) < 1: computevardiag = None
     15162
     15163    return computevarmod, computevardiag
     15164
     15165def computevar_model(values, ncfile):
     15166    """ Function to provide the way to compute a CF-variable providing its name
     15167      varname= CF-compilant variable name
     15168      ncfile= netCDF file from which the variable should be retrieved
     15169    """
     15170    fname = 'computevar_model'
     15171
     15172    if values == 'h':
     15173        print fname + '_____________________________________________________________'
     15174        print computevar_model.__doc__
     15175        quit()
     15176
     15177    varname = values
     15178    ncobj = NetCDFFile(ncfile, 'r')
     15179 
     15180    varmod, vardiag =  var_model(varname, ncobj)
     15181    print '  ' + fname + ": CF variable '" + varname + "' can be computed ______"
     15182    print '    model:', varmod
     15183    print '    diagnostics:', vardiag
     15184   
     15185    ncobj.close()
     15186
     15187    return
     15188
    1510315189#quit()
    1510415190
Note: See TracChangeset for help on using the changeset viewer.