Changeset 2026 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Aug 6, 2018, 3:42:15 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `stats_Slist': Function to provide extremes from a string as a list separated by a given character
  • `inf_operSlist': Function to provide information from a string as a list separated by a given character followig a given operation and a set of values
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1994 r2026  
    9797# getting_fixedline: Function to get the values from a line of text with fixed lenght of different values
    9898# grib_CFequiv: Function to provide the CF name of a GRIB variable code number
     99# inf_operSlist: Function to provide information from a string as a list separated by
     100#   a given character followig a given operation and a set of values
    99101# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
    100102# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
     
    150152# singleline_printing_class: Function to print all the values of a given class in a single line to be parseavel
    151153# stagger_unstagger: Function to de-stagger a variable
     154# stats_Slist: Function to provide extremes from a string as a list separated by a given character
    152155# std_stats2Val: two variables standard Statistics class
    153156# Str_Bool: Function to transform from a String value to a boolean one
     
    1357913582#create_LateX_figs('/home/lluis/estudios/FPS_ALPS/additional/IOP/analysis/figs', pltns, sts, 'png')
    1358013583
     13584def stats_Slist(listS, char=':'):
     13585    """ Function to provide extremes from a string as a list separated by a given
     13586        character
     13587      listS= String as list
     13588      char= character to provide different values
     13589    >>> stats_Slist('1.2:3.45:-98.21:34321.1:100.1', ':')
     13590    stats_Slist min max mean standard_dev _______
     13591    stats_Slist_values: -98.21 34321.1 6865.528 13727.9292697
     13592    """
     13593    fname = 'stats_Slist'
     13594
     13595    haschar = listS.find(char)
     13596    if haschar == -1:
     13597        print errormsg
     13598        print '  ' + fname + ": provided String-list '" + + "' does not have " +     \
     13599          "character '" + char + "' !!"
     13600        quit(-1)
     13601
     13602    listv = str_list_k(listS, char, 'F')
     13603
     13604    nl = np.min(listv)
     13605    xl = np.max(listv)
     13606    ml = np.mean(listv)
     13607    sl = np.std(listv)
     13608
     13609    print fname + ' min max mean standard_dev _______'
     13610    print fname + '_values:', nl, xl, ml, sl
     13611
     13612    return
     13613
     13614#print stats_Slist('1.2:3.45:-98.21:34321.1:100.1', ':')
     13615
     13616def inf_operSlist(listS, oper, char=':', values=None):
     13617    """ Function to provide information from a string as a list separated by a given
     13618        character followig a given operation and a set of values
     13619      listS= String as list
     13620      char= character to provide different values
     13621      oper= available operations
     13622        'maxloc': index of the maximum value
     13623        'minloc': index of the minimum value
     13624        'sort': sort list of values (returned as a new String list)
     13625        'threshold': index of the values between which a given threshold is met (only
     13626          useful for sort values)
     13627      values= char separated list of required values for the given operation (if
     13628        applicable)
     13629    >>> inf_operSlist('1.2:3.45:-98.21:34321.1:100.1', 'sort', ':')
     13630    -98.21:1.2:3.45:100.1:34321.1
     13631    >>> inf_operSlist('1.2:3.45:-98.21:34321.1:100.1', 'minloc', ':')
     13632    2
     13633    >>> inf_operSlist('-98.21:1.2:3.45:100.1:34321.1', 'threshold', ':', 3.)
     13634    1
     13635    """
     13636    fname = 'inf_operSlist'
     13637    availoper = ['maxloc', 'minloc', 'sort', 'threshold']
     13638
     13639    haschar = listS.find(char)
     13640    if haschar == -1:
     13641        print errormsg
     13642        print '  ' + fname + ": provided String-list '" + + "' does not have " +     \
     13643          "character '" + char + "' !!"
     13644        quit(-1)
     13645
     13646    listv = str_list_k(listS, char, 'F')
     13647
     13648    if oper == 'maxloc':
     13649        xl = np.max(listv)
     13650        return index_vec(listv, xl)
     13651    elif oper == 'minloc':
     13652        nl = np.min(listv)
     13653        return index_vec(listv, nl)
     13654    elif oper == 'sort':
     13655        sortl = listv + []
     13656        sortl.sort()
     13657        sortlistS = ''
     13658        Nval = len(sortl)
     13659        sortlistS = ''
     13660        for iv in range(Nval-1):
     13661            sortlistS = sortlistS + str(sortl[iv]) + ':'
     13662        sortlistS = sortlistS + str(sortl[Nval-1])
     13663        if char != ':': return sortlistS.replace(':', char)
     13664        else: return sortlistS
     13665    elif oper == 'threshold':
     13666        if values is None:
     13667            print errormsg
     13668            print '  ' + fname + ": operation '" + oper + "' requires a value !!"
     13669            print "    None provided"
     13670            quit(-1)
     13671        val = np.float(values)
     13672        Nval = len(listv)
     13673        dlist = np.array(listv[1:Nval]) - np.array(listv[0:Nval-1])
     13674        if not np.all(dlist > 0.) or np.all(dlist < 0.):
     13675            print errormsg
     13676            print '  ' + fname + ": operation '" + oper + "' requires sorted list !!"
     13677            print "    provided one '" + listS + "' is not!!"
     13678            quit(-1)
     13679        sign = listv[1] - listv[0]
     13680        if sign > 0.:
     13681            for iloc in range(Nval-1):
     13682                if listv[iloc+1] >= val and listv[iloc] < val: return iloc
     13683            return Nval
     13684        elif sign < 0.:
     13685            for iloc in range(Nval-1):
     13686                if listv[iloc] >= val and listv[iloc-1] < val: return iloc
     13687            return Nval
     13688
     13689    else:
     13690        print errormsg
     13691        print '  ' + fname + ": operation '" + oper + "' nor ready !!"
     13692        print "    available ones:", availoper
     13693        quit(-1)
     13694
     13695    return       
     13696
    1358113697#quit()
    1358213698
Note: See TracChangeset for help on using the changeset viewer.