Changeset 825 in lmdz_wrf


Ignore:
Timestamp:
Jun 13, 2016, 5:35:42 PM (9 years ago)
Author:
lfita
Message:

Adding:

  • `chainSnum_levnext': Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers
  • chainSnum_num': Function to pass a ChainStrNum?' string to a number
  • num_chainSnum': Function to pass a value to a ChainStrNum?' number
  • `num_split': Function to split a string at each numeric value keeping the number as the last character of each cut

Generalising: `significant_decomposition'
Fixing sub-flows names in `subbasin_point'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/tools/generic_tools.py

    r818 r825  
    1919
    2020####### Content
     21# chainSnum_levnext: Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers
     22# chainSnum_num: Function to pass a `ChainStrNum' string to a number
    2123# contflow: Function to bring back the increment in j,i grid points according to a trip: (inflow directions)
    2224# dictionary_key: Function to provide the first key in a dictionay with a given value
     
    2426# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
    2527# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
     28# num_chainSnum: Function to pass a value to a `ChainStrNum' number
     29# num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
    2630# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
    2731# significant_decomposition: Function to decompose a given number by its signifcant potencies
     
    63936397    diff = anum*1.
    63946398    iipot = 1
    6395     while diff > 0.:
    6396         pot = np.floor(np.log10(diff))
    6397         if pot < minpot: break
    6398         potv = np.int(diff/(10.**pot))
    6399 #        print 'Lluis:',np.log10(diff), pot,':',10.**pot,':',diff/(10.**pot),'.',potv,'.',potv*10.**pot,diff
     6399    pot = np.floor(np.log10(anum))
     6400    while pot > minpot:
     6401        if diff >= 10.**(pot-1):
     6402            pot = np.floor(np.log10(diff))
     6403            potv = np.int(diff/(10.**pot))
     6404#            print 'Lluis:',np.log10(diff), pot,':',10.**pot,':',diff/(10.**pot),'.',potv,'.',potv*10.**pot,diff
     6405        else:
     6406            potv = 0
     6407            pot = pot - 1
     6408#            print 'Lluis:','*****', pot,':',10.**pot,':',diff/(10.**pot),'.',potv,'.',potv*10.**pot,diff
     6409
    64006410        diff = diff - potv*10.**pot
     6411#        print '  real diff', diff
    64016412        potencies.append(pot)
    64026413        potval.append(potv)
     
    66056616    return keyval
    66066617
     6618def num_chainSnum(val):
     6619    """ Function to pass a value to a `ChainStrNum' number
     6620        `ChainStrNum:' hirerarchy classification where each levels is a significant number within a string:
     6621            1 lev.: [n]
     6622            2 lev.: [n][m]
     6623            3 lev.: [n][m][l]
     6624            ...
     6625          NOTE: when a given level has more than 9 values in a given potency of 10, then 'a' is attached in the front: 'a'[i]
     6626          NOTE: when a given level has more than 19 values in a given potency of 10, then 'b' is attached in the front: 'b'[i]
     6627          NOTE: when a given level has more than 29 values in a given potency of 10, then 'c' is attached in the front: 'c'[i]
     6628          (...)
     6629          NOTE: when a given level has more than 89 values in a given potency of 10, then 'i' is attached in the front: 'i'[i]
     6630          NOTE: 'o' is for that potencies of 10 without value
     6631      val = value to transform
     6632    >>> num_chainSnum(3)
     6633    3
     6634    >>> num_chainSnum(1976)
     6635    aig6
     6636    >>> num_chainSnum(12010)
     6637    aboa0
     6638    """
     6639    fname = 'num_chainSnum'
     6640
     6641    npot = np.int(np.log10(val))
     6642
     6643    chainSnum=''
     6644    if npot > 0:
     6645        Nvals, pots, vals = significant_decomposition(val,0)
     6646        rest = 0
     6647        for ipot in range(npot):
     6648            rest = rest + int(vals[ipot]*10**pots[ipot])
     6649            if vals[ipot] > 0:
     6650                chainSnum = chainSnum + chr(96+vals[ipot])
     6651            else:
     6652                chainSnum = chainSnum + 'o'
     6653
     6654        num = val - rest
     6655    else:
     6656        num = val
     6657    chainSnum = chainSnum + str(num)
     6658
     6659    return chainSnum
     6660
     6661def chainSnum_num(cSnum):
     6662    """ Function to pass a `ChainStrNum' string to number
     6663        `ChainStrNum:' hirerarchy classification where each levels is a significant number within a string:
     6664            1 lev.: [n]
     6665            2 lev.: [n][m]
     6666            3 lev.: [n][m][l]
     6667            ...
     6668          NOTE: when a given level has more than 9 values in a given potency of 10, then 'a' is attached in the front: 'a'[i]
     6669          NOTE: when a given level has more than 19 values in a given potency of 10, then 'b' is attached in the front: 'b'[i]
     6670          NOTE: when a given level has more than 29 values in a given potency of 10, then 'c' is attached in the front: 'c'[i]
     6671          (...)
     6672          NOTE: when a given level has more than 89 values in a given potency of 10, then 'i' is attached in the front: 'i'[i]
     6673          NOTE: 'o' is for that potencies of 10 without value
     6674      cSnum = value to transform
     6675    >>> num_chainSnum('3')
     6676    3
     6677    >>> num_chainSnum('aig6')
     6678    1976
     6679    >>> num_chainSnum('aboa0')
     6680    12010
     6681    """
     6682    fname = 'chainSnum_num'
     6683
     6684    num = 0
     6685    LcSnum = len(cSnum)
     6686    if LcSnum > 1:
     6687        for ic in range(LcSnum-1):
     6688            ipot = ord(cSnum[ic:ic+1]) - 96
     6689            if ipot < 0 or ipot > 9 and ipot != ord('o') - 96:
     6690                print errormsg
     6691                print '  ' + fname + ": wrong character '" + cSnum[ic:ic+1] + "' !!", ipot
     6692                print "    'ChainStrNum' codification only allows 'a' to 'i' and 'o'"
     6693                quit(-1)
     6694            else:
     6695                if ipot == ord('o') - 96: ipot = 0
     6696
     6697            num = num + int(ipot*10**(LcSnum-ic-1))
     6698
     6699    num = num + int(cSnum[LcSnum-1:LcSnum])
     6700
     6701    return num
     6702
     6703def num_split(string):
     6704    """ Function to split a string at each numeric value keeping the number as the last character of each cut
     6705      string= string to cut
     6706    >>> num_split('asd123asdsa1')
     6707    ['asd1', '2', '3', 'asdsa1']
     6708    """
     6709    fname = 'num_split'
     6710
     6711    Lstring = len(string)
     6712    cuts = []
     6713    newcut = ''
     6714    for ic in range(Lstring):
     6715        sc = string[ic:ic+1]
     6716        newcut = newcut + sc
     6717        if ord(sc) >= 48 and ord(sc) <= 57:
     6718            cuts.append(newcut)
     6719            newcut = ''
     6720
     6721    return cuts
     6722
     6723def chainSnum_levnext(parentlev, ChainStrNums):
     6724    """ Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers
     6725      prevlev: parent level of the desired level
     6726      ChainStrNums: list of strings as `ChainStrNum'
     6727        `ChainStrNum:' hyerarchic classification where each levels is a significant number within a string:
     6728            1 lev.: [n]
     6729            2 lev.: [n][m]
     6730            3 lev.: [n][m][l]
     6731            ...
     6732          NOTE: when a given level has more than 9 values in a given potency of 10, then 'a' is attached in the front: 'a'[i]
     6733          NOTE: when a given level has more than 19 values in a given potency of 10, then 'b' is attached in the front: 'b'[i]
     6734          NOTE: when a given level has more than 29 values in a given potency of 10, then 'c' is attached in the front: 'c'[i]
     6735          (...)
     6736          NOTE: when a given level has more than 89 values in a given potency of 10, then 'i' is attached in the front: 'i'[i]
     6737          NOTE: 'o' is for that potencies of 10 without value
     6738    >>> ChainStrNums = ['1', '2', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a0', '111', '112', '113', '121', '122' ]
     6739    >>> chainSnum_levnext('',ChainStrNums)
     6740    3
     6741    >>> chainSnum_levnext('1',ChainStrNums)
     6742    1a1
     6743    >>> chainSnum_levnext('12',ChainStrNums)
     6744    123
     6745    >>> chainSnum_levnextt('111',ChainStrNums)
     6746    1111
     6747    """
     6748    fname = 'chainSnum_levnext'
     6749
     6750# Number of levels of the parent level
     6751    if len(parentlev) > 0:
     6752        parentlevs = num_split(parentlev)
     6753        Nparentlevs = len(parentlevs)
     6754    else:
     6755        parentlevs = ['']
     6756        Nparentlevs = 0
     6757
     6758#    print '  ' + fname + ' parentlev:', parentlev,'Nlevs:', Nparentlevs,'parentlevs:',parentlevs
     6759
     6760# Level to increment
     6761    lev = Nparentlevs + 1
     6762
     6763    rightvals = []
     6764    for istr in ChainStrNums:
     6765        levs = num_split(istr)
     6766        if len(levs) == lev:
     6767            parent = ''
     6768            for ilev in range(len(levs)-1):
     6769                parent = parent + levs[ilev]
     6770
     6771            if parent == parentlev:
     6772                rightvals.append(chainSnum_num(levs[lev-1]))
     6773
     6774    if len(rightvals) > 0:
     6775        newnum = np.max(rightvals)
     6776        newchainSnum = num_chainSnum(newnum + 1)
     6777    else:
     6778        newchainSnum = '1'
     6779
     6780    return parentlev + newchainSnum
     6781
    66076782def subbasin_point(trips, pt):
    66086783    """ Function to provide sub-basins given a grid point following a matrix of trips
     
    66566831            ijfound[Nij] = True
    66576832            jipt = ijsubbasin[Nij]
    6658             Njipt = dictionary_key_list(ijsubflow, Nij)
    6659             if subfinished.has_key(Njipt + '1'):
    6660                 if subfinished.has_key(Njipt + '01'):
    6661                     if subfinished.has_key(Njipt + '001'):
    6662                         Njipt = Njipt + '0001'
    6663                     else:
    6664                         Njipt = Njipt + '001'
    6665                 else:
    6666                     Njipt = Njipt + '01'
    6667             else:
    6668                 Njipt = Njipt + '1'
     6833            parentNjipt = dictionary_key_list(ijsubflow, Nij)
     6834            Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys())
    66696835#            print '  ' + fname + "new sub-flow '" + Njipt + "' !!"
    66706836            subfinished[Njipt] = False
     
    66926858            followvals = np.zeros((3,3), dtype=bool)
    66936859            followvals = arrive
     6860            parentNjipt = Njipt
    66946861
    66956862            for ifollow in range(Narrive):
     
    67076874                        if ifollow != 0:
    67086875# Avoiding repetition of sub-flow name
    6709                             if subfinished.has_key(Njipt + str(ifollow)):
    6710                                 Njipt = Njipt + '0' + str(ifollow)
     6876                            if len(ijsubflow.keys()) > 1:
     6877                                Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys())
    67116878                            else:
    6712                                 Njipt = Njipt + str(ifollow)
    6713 #                            print '  ' + fname + "new sub-flow '" + Njipt + "' !!"
     6879                                Njipt = '11'
     6880                        else:
     6881                            Njipt = parentNjipt
     6882#                        print '  ' + fname + "new sub-flow '" + Njipt + "' !!"
    67146883                        subfinished[Njipt] = False
    67156884                        subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] = True
     
    67806949#  for isub in subflows[subflow]:
    67816950#      print '  ' , isub , ':', subflowspt[isub]
    6782 
     6951#
    67836952#print 'Total sub-basin:', np.sum(masksubbasin)
    6784 
    6785 #quit()
Note: See TracChangeset for help on using the changeset viewer.