Changeset 837 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jun 16, 2016, 4:47:47 PM (8 years ago)
Author:
lfita
Message:

Adding `CFvar_MODvar': Function to provide which model values can provide a CF-variable from ASCII file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r832 r837  
    1919
    2020####### Content
     21# CFvar_MODvar: Function to provide which model values can provide a CF-variable from ASCII file
    2122# chainSnumHierarchy: Class to provide the structure of a `ChainStrNum' hierarchy
    2223# chainSnum_levnext: Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers
     
    4142# vals_around: Function to provide the 3x3 values around a given j,i point
    4243
     44def searchInlist(listname, nameFind):
     45    """ Function to search a value within a list
     46    listname = list
     47    nameFind = value to find
     48    >>> searInlist(['1', '2', '3', '5'], '5')
     49    True
     50    """
     51    for x in listname:
     52      if x == nameFind:
     53        return True
     54    return False
     55
    4356def values_line(line, splitv, chars):
    4457    """ Function to retrieve values from a line of ASCII text removing a series of characters
     
    251264    if idate == 'h':
    252265        print fname + '_____________________________________________________________'
    253         print variables_values.__doc__
     266        print period_information.__doc__
    254267        quit()
    255268
     
    552565
    553566    return
     567
     568def CFvar_MODvar(varn):
     569    """ Function to provide which model values can provide a CF-variable from ASCII file
     570      'variables_values.dat'
     571    CFvar_MODvar(varName)
     572      [varName]= name of the variable
     573    >>> CFvar_MODvar('hfss')
     574    ['hfss', 'LSENS', 'sens', 'HFX', 'hfx']
     575    >>> CFvar_MODvar('clm')
     576    ['clm', 'cldm', 'LCLDM', 'Mid-level cloudiness']
     577    >>> CFvar_MODvar('pr')
     578    ['pr', 'RAINTOT', 'precip', 'LPRECIP', 'Precip Totale liq+sol']
     579    """
     580    import subprocess as sub
     581
     582    fname='CFvar_MODvar'
     583
     584    if varn == 'h':
     585        print fname + '_____________________________________________________________'
     586        print CFvar_MODvar.__doc__
     587        quit()
     588
     589    folder = os.path.dirname(os.path.realpath(__file__))
     590
     591    infile = folder + '/variables_values.dat'
     592
     593    if not os.path.isfile(infile):
     594        print errormsg
     595        print '  ' + fname + ": File '" + infile + "' does not exist !!"
     596        quit(-1)
     597
     598    ncf = open(infile, 'r')
     599
     600    MODvars = []
     601    for line in ncf:
     602        if line[0:1] != '#':
     603            values = line.replace('\n','').split(',')
     604            varvals = [values[1].replace(' ',''), values[2].replace(' ',''),         \
     605              np.float(values[3]), np.float(values[4]),values[5].replace(' ',''),    \
     606              values[6].replace(' ',''), values[7].replace(' ','')]
     607            if varvals[0] == varn: MODvars.append(values[0])
     608
     609    if len(MODvars) == 0:
     610        print errormsg
     611        print '  ' + fname + ": variable '" + varn + "' not defined !!!"
     612        ncf.close()
     613        return None
     614    else:
     615        return MODvars
     616
     617print CFvar_MODvar('hfss')
     618print CFvar_MODvar('pr')
     619
     620quit()
    554621
    555622def variables_values_old(varName):
     
    14191486      return end
    14201487
    1421 def searchInlist(listname, nameFind):
    1422     """ Function to search a value within a list
    1423     listname = list
    1424     nameFind = value to find
    1425     >>> searInlist(['1', '2', '3', '5'], '5')
    1426     True
    1427     """
    1428     for x in listname:
    1429       if x == nameFind:
    1430         return True
    1431     return False
    1432 
    14331488def datetimeStr_datetime(StringDT):
    14341489    """ Function to transform a string date ([YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format) to a date object
Note: See TracChangeset for help on using the changeset viewer.