Changeset 1914 in lmdz_wrf for trunk


Ignore:
Timestamp:
May 14, 2018, 11:57:54 AM (7 years ago)
Author:
lfita
Message:

Adding:

  • maxNcounts: Function to provide a value according to the maximum number of repeated values along a given axis
  • minNcounts: Function to provide a value according to the minimum number of repeated values along a given axis
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1905 r1914  
    111111# list_norepeatcombos: Function to all possible (Num-1)-combinations of a Num values without repetitions
    112112# lstring_values: Function to provide a new list-string from a string which is a list of word separated by a character if some values are repeated they are not included
     113# maxNcounts: Function to provide a value according to the maximum number of repeated values along a given axis
     114# minNcounts: Function to provide a value according to the minimum number of repeated values along a given axis
    113115# minmax_range: Function to provide a range for a given pair of minimum and maximum
    114116# multi_index_mat: Function to provide the multiple coordinates of a given value inside a matrix
     
    1307113073    return newStr
    1307213074
     13075def maxNcounts(values, axis):
     13076    """ Function to provide a value according to the maximum number of repeated values along a given axis
     13077      values= values to count maximum values
     13078      axis= which axis to use to count values
     13079    >>> values3D = np.arange(27).reshape(3,3,3)
     13080    >>> values3D[2,:,:] = 3
     13081    >>> values3D[1,0:2,0:2] = 3
     13082    >>> maxNcounts(values3D, 0)
     13083    [[3 3 2]
     13084     [3 3 5]
     13085     [6 7 8]]
     13086    """
     13087    import module_ForSci as fsci
     13088
     13089    fname = 'maxNcounts'
     13090
     13091    shapev = list(values.shape)
     13092    Ndims = len(shapev)
     13093    if axis > len(shapev):
     13094        print errormsg
     13095        print '  ' + fname + ': axis to use:', axis, 'not in shape:', values.shapev
     13096        quit(-1)
     13097    newshape = shapev.pop(axis)
     13098    maxNCvalues = np.zeros(tuple(shapev), dtype=values.dtype)
     13099
     13100    origdimns = []
     13101    newdimns = []
     13102    for iid in range(Ndims):
     13103        origdimns.append('d'+str(iid))
     13104        if iid != axis: newdimns.append(origdimns[iid])
     13105
     13106    listslcs = provide_slices(origdimns, values.shape, newdimns)
     13107    Nslcs = len(listslcs)
     13108
     13109    for islc in range(Nslcs):
     13110        slicev = listslcs[islc] + []
     13111        slicev.pop(axis)
     13112        vals = values[tuple(listslcs[islc])].flatten()
     13113        Ndiffv, vcounts = fsci.module_scientific.ncountr(values=vals, d1=len(vals))
     13114        countmax = np.max(vcounts[0:Ndiffv,1])
     13115        idmax = index_vec(vcounts[0:Ndiffv,1],countmax)           
     13116
     13117        maxNCvalues[tuple(slicev)] = vcounts[idmax,0]
     13118
     13119    return maxNCvalues
     13120
     13121
     13122def minNcounts(values, axis):
     13123    """ Function to provide a value according to the minimum number of repeated values along a given axis
     13124      values= values to count maximum values
     13125      axis= which axis to use to count values
     13126    >>> values3D = np.arange(27).reshape(3,3,3)
     13127    >>> values3D[2,:,:] = 3
     13128    >>> values3D[1,0:2,0:2] = 3
     13129    >>> minNcounts(values3D, 0)
     13130    [[0 1 2]
     13131     [3 4 5]
     13132     [6 7 8]]
     13133    """
     13134    import module_ForSci as fsci
     13135
     13136    fname = 'minNcounts'
     13137
     13138    shapev = list(values.shape)
     13139    Ndims = len(shapev)
     13140    if axis > len(shapev):
     13141        print errormsg
     13142        print '  ' + fname + ': axis to use:', axis, 'not in shape:', values.shapev
     13143        quit(-1)
     13144    newshape = shapev.pop(axis)
     13145    minNCvalues = np.zeros(tuple(shapev), dtype=values.dtype)
     13146
     13147    origdimns = []
     13148    newdimns = []
     13149    for iid in range(Ndims):
     13150        origdimns.append('d'+str(iid))
     13151        if iid != axis: newdimns.append(origdimns[iid])
     13152
     13153    listslcs = provide_slices(origdimns, values.shape, newdimns)
     13154    Nslcs = len(listslcs)
     13155
     13156    for islc in range(Nslcs):
     13157        slicev = listslcs[islc] + []
     13158        slicev.pop(axis)
     13159        vals = values[tuple(listslcs[islc])].flatten()
     13160        Ndiffv, vcounts = fsci.module_scientific.ncountr(values=vals, d1=len(vals))
     13161        countmax = np.min(vcounts[0:Ndiffv,1])
     13162        idmax = index_vec(vcounts[0:Ndiffv,1],countmax)           
     13163
     13164        minNCvalues[tuple(slicev)] = vcounts[idmax,0]
     13165
     13166    return minNCvalues
     13167
     13168values3D = np.arange(27).reshape(3,3,3)
     13169values3D[2,:,:] = 3
     13170values3D[1,0:2,0:2] = 3
     13171print values3D
     13172print maxNcounts(values3D, 0)
     13173print minNcounts(values3D, 0)
     13174
    1307313175#quit()
    1307413176
Note: See TracChangeset for help on using the changeset viewer.