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


Ignore:
Timestamp:
Aug 3, 2017, 8:26:35 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `shrinkarray_dim': Function to shrink a given array with a vector of True/False? along a given dimension
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1599 r1600  
    100100# multi_index_mat: Function to provide the multiple coordinates of a given value inside a matrix
    101101# multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string
    102 # Nomasked: Function to brin back a given array wthout the masked values reducing a given dimension
     102# Nomasked: Function to bring back a given array wthout the masked values reducing a given dimension
    103103# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    104104# num_ordinal: Function to provide the ordinal of a given number, language, format and gender
     
    122122# same_shape: Function to check if two matrices have the same shape
    123123# search_sec_list: Function to provide the values and indices on a list which matches a section of a string
     124# shrinkarray_dim: Function to shrink a given array with a vector of True/False along a given dimension
    124125# significant_decomposition: Function to decompose a given number by its signifcant potencies
    125126# singleline_printing_class: Function to print all the values of a given class in a single line to be parseavel
     
    1232112322
    1232212323def Nomasked(vals, dim):
    12323     """ Function to brin back a given array wthout the masked values reducing a given dimension
    12324       vals= arrtay of values
     12324    """ Function to bring back a given array wthout the masked values reducing a given dimension
     12325      vals= array of values
    1232512326      dim= which dimension along which if all values are maskd reduce
    1232612327    >>> arrayv = np.arange(32).reshape(4,8)
     
    1233712338    fname = 'Nomasked'
    1233812339
     12340    if type(vals) != type(mamat):
     12341        print errormsg
     12342        print '  ' + fname + ': only works with masked arrays !!'
     12343        print '    provided type:', type(vals)
     12344        quit(-1)
     12345    if dim >= len(vals.shape):
     12346        print errormsg
     12347        print '  ' + fname + ': dimension provided too big !!'
     12348        print '    shape of the values:', vals.shape
     12349        quit(-1)
     12350
    1233912351    varshape = vals.shape
    1234012352    dimns = []
     
    1237212384
    1237312385    return newarray, allmasked
     12386
     12387def shrinkarray_dim(vals, vector, dim):
     12388    """ Function to shrink a given array with a vector of True/False along a given dimension
     12389      vals= array of values
     12390      vector= vector of booleans of the same length as dim (True: remove)
     12391      dim= which dimension along which if all values are maskd reduce
     12392    >>> arrayv = np.arange(32).reshape(4,8)
     12393    >>> maskv = np.zeros((4), dtype=bool)
     12394    >>> maskv[1:3] = True
     12395    >>> shrinkarray_dim(arrayv, maskv, 0)
     12396    [[ 0  1  2  3  4  5  6  7]
     12397     [24 25 26 27 28 29 30 31]]
     12398    """
     12399    fname = 'shrinkarray_dim'
     12400
     12401    if dim >= len(vals.shape):
     12402        print errormsg
     12403        print '  ' + fname + ': dimension provided too big !!'
     12404        print '    shape of the values:', vals.shape
     12405        quit(-1)
     12406    if len(vector) != vals.shape[dim]:
     12407        print errormsg
     12408        print '  ' + fname + ': provided vector and length of the dimension differ !!'
     12409        print '    shape of the array:', vals.shape[dim], 'on dimension:', dim
     12410        print '    length of the vector:', len(vector)
     12411        quit(-1)
     12412
     12413    varshape = vals.shape
     12414    dimns = []
     12415    for id in range(len(varshape)):
     12416        dimns.append('d' + str(id))
     12417
     12418    # Looking for the slices along the given dimension
     12419    slices = provide_slices(dimns, varshape, ['d'+str(dim)])
     12420
     12421    Nslices = len(slices)
     12422    # First looking for the slices
     12423    Novals = np.sum(vector)
     12424
     12425    # Definition of the new array
     12426    newvarshape = list(varshape)
     12427    newvarshape[dim] = varshape[dim]-Novals
     12428
     12429    newarray = np.zeros(tuple(newvarshape), dtype=vals.dtype)
     12430
     12431    imask = 0
     12432    for islc in range(Nslices-1,-1,-1):
     12433        if not vector[islc]:
     12434            slicev = list(slices[islc])
     12435            slicev[dim] = imask
     12436            newarray[tuple(slicev)] = vals[tuple(slices[islc])]
     12437            imask = imask + 1
     12438
     12439    return newarray
     12440
    1237412441#quit()
    1237512442
Note: See TracChangeset for help on using the changeset viewer.