Changeset 995 in lmdz_wrf


Ignore:
Timestamp:
Aug 10, 2016, 11:51:38 AM (8 years ago)
Author:
lfita
Message:

Adding `get_specdictionary_HMT': Function to get specific values from a dictionary by selcting that keys with H*M*T

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r992 r995  
    3838# files_folder_HMT: Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]
    3939# get_configuration: Function to get the configuration from an ASCII external file
     40# get_specdictionary_HMT: Function to get specific values from a dictionary by selcting that keys with H*M*T
    4041# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
    4142# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
     
    81208121
    81218122    return
     8123
     8124def get_specdictionary_HMT(dictv,H='',M='',T=''):
     8125    """ Function to get specific values from a dictionary by selcting that keys with H*M*T
     8126      dictv= dictionary with the values
     8127      H= head of the key (empty for nothing)
     8128      M= middle of the key (empty for nothing)
     8129      T= tail of the key (empty for nothing)
     8130    dictrom = {'i':1, 'ii':2, 'iii':3, 'iv':4, 'v':5, 'vi':6, 'vii':2, 'viii':8, 'ix':9, 'x':10, 'xi':11 }
     8131    >>> get_specdictionary_HTM(dictrom,H='i')
     8132    {'ii': 2, 'ix': 9, 'iii': 3, 'i': 1, 'iv': 4}
     8133    >>> get_specdictionary_HTM(dictrom,M='i')
     8134    {'vii': 2, 'iii': 3, 'viii': 8}
     8135    >>> get_specdictionary_HTM(dictrom,T='i')
     8136    {'xi': 11, 'i': 1, 'vi': 6, 'vii': 2, 'ii': 2, 'viii': 8, 'iii': 3}
     8137    >>> get_specdictionary_HMT(dictrom,H='v',M='i')
     8138    {'vii': 2, 'viii': 8}
     8139    >>> get_specdictionary_HMT(dictrom,H='x',T='i')
     8140    {'xi': 11}
     8141    >>> get_specdictionary_HMT(dictrom,M='i',T='x')
     8142    None
     8143    >>> get_specdictionary_HMT(dictrom,H='v',M='i',T='i')
     8144    {'vii': 2, 'viii': 8}
     8145    """
     8146    fname = 'get_specdictionary_HMT'
     8147   
     8148    LH = len(H)
     8149    LM = len(M)
     8150    LT = len(T)
     8151
     8152    dictkeys = dictv.keys()
     8153
     8154    #print '  ' + fname + ': H=',H,'M=',M,'T=',T,'_______'
     8155    # Looking for heads
     8156    if LH > 0:
     8157        Hkeys = []
     8158        for key in dictkeys:
     8159            if key[0:LH] == H: Hkeys.append(key)
     8160        #print '    Hkeys:', Hkeys
     8161    # Looking for middles
     8162    if LM > 0:
     8163        Mkeys = []
     8164        for key in dictkeys:
     8165            Lkey = len(key)
     8166            if len(key[1:Lkey-1]) < LM:
     8167                print warnmsg
     8168                print '  ' + fname + ": key '" + key + "' too short for M='" + M +   \
     8169                  "' !!"
     8170            if key[1:Lkey-1].find(M) != -1: Mkeys.append(key)
     8171        #print '    Mkeys:', Mkeys
     8172    # Looking for tails
     8173    if LT > 0:
     8174        Tkeys = []
     8175        for key in dictkeys:
     8176            Lkey = len(key)
     8177            if key[Lkey-LT:Lkey+1] == T: Tkeys.append(key)
     8178        #print '    Tkeys:', Tkeys
     8179
     8180    # Coincidences
     8181    if LH > 0:
     8182        if LM > 0:
     8183            coinlistA = list(set(Hkeys).intersection(set(Mkeys)))
     8184            if LT > 0:
     8185                coinlist = list(set(coinlistA).intersection(set(Tkeys)))
     8186            else:
     8187                coinlist = list(coinlistA)
     8188        elif LT > 0:
     8189            coinlist = list(set(Hkeys).intersection(set(Tkeys)))
     8190        else:
     8191            coinlist = list(Hkeys)
     8192    elif LM > 0:
     8193        if LT > 0:
     8194            coinlist = list(set(Mkeys).intersection(set(Tkeys)))
     8195        else:
     8196            coinlist = list(Mkeys)
     8197    elif LT > 0:
     8198        coinlist = list(Tkeys)
     8199
     8200    if len(coinlist) > 0:
     8201        newdictv = {}
     8202        for key in coinlist:
     8203            newdictv[key] = dictv[key]
     8204    else:
     8205        newdictv= None
     8206
     8207    return newdictv
     8208
     8209dictrom = {'i':1, 'ii':2, 'iii':3, 'iv':4, 'v':5, 'vi':6, 'vii':2, 'viii':8, 'ix':9, 'x':10, 'xi':11 }
     8210print get_specdictionary_HMT(dictrom,H='i')
     8211print get_specdictionary_HMT(dictrom,M='i')
     8212print get_specdictionary_HMT(dictrom,T='i')
     8213print get_specdictionary_HMT(dictrom,H='v',M='i')
     8214print get_specdictionary_HMT(dictrom,H='x',T='i')
     8215print get_specdictionary_HMT(dictrom,M='i',T='x')
     8216print get_specdictionary_HMT(dictrom,H='v',M='i',T='i')
    81228217#quit()
    81238218
Note: See TracChangeset for help on using the changeset viewer.