Changeset 1599 in lmdz_wrf


Ignore:
Timestamp:
Aug 3, 2017, 8:12:14 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `Nomasked': Function to brin back a given array wthout the masked values reducing a given dimension
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1596 r1599  
    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
    102103# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    103104# num_ordinal: Function to provide the ordinal of a given number, language, format and gender
     
    1231912320    return newlist
    1232012321
     12322def Nomasked(vals, dim):
     12323    """ Function to brin back a given array wthout the masked values reducing a given dimension
     12324      vals= arrtay of values
     12325      dim= which dimension along which if all values are maskd reduce
     12326    >>> arrayv = np.arange(32).reshape(4,8)
     12327    >>> maskv = np.zeros((4,8), dtype=bool)
     12328    >>> maskv[1:3,:] = True
     12329    >>> maarray = ma.maskedarray(arrayv, mask=maskv)
     12330    >>> Nomasked(maarray, 0)
     12331    masked_array(data =
     12332     [[ 0  1  2  3  4  5  6  7]
     12333     [24 25 26 27 28 29 30 31]],
     12334     mask = False, fill_value = 999999)
     12335    , array([False,  True,  True, False], dtype=bool))
     12336    """
     12337    fname = 'Nomasked'
     12338
     12339    varshape = vals.shape
     12340    dimns = []
     12341    for id in range(len(varshape)):
     12342        dimns.append('d' + str(id))
     12343
     12344    # Looking for the slices along the given dimension
     12345    slices = provide_slices(dimns, varshape, ['d'+str(dim)])
     12346
     12347    Nslices = len(slices)
     12348    allmasked = np.zeros((Nslices), dtype=bool)
     12349
     12350    for islc in range(Nslices):
     12351        if all(vals.mask[tuple(slices[islc])]): allmasked[islc] = True
     12352
     12353    # First looking for the slices
     12354    Novals = np.sum(allmasked)
     12355
     12356    # Definition of the new array
     12357    newvarshape = list(varshape)
     12358    newvarshape[dim] = varshape[dim]-Novals
     12359
     12360    newarray = np.zeros(tuple(newvarshape), dtype=vals.dtype)
     12361    newmaksv = np.zeros(tuple(newvarshape), dtype=bool)
     12362
     12363    imask = 0
     12364    for islc in range(Nslices-1,-1,-1):
     12365        if not allmasked[islc]:
     12366            slicev = list(slices[islc])
     12367            slicev[dim] = imask
     12368            newarray[tuple(slicev)] = vals[tuple(slices[islc])]
     12369            imask = imask + 1
     12370
     12371    newarray = ma.array(newarray)
     12372
     12373    return newarray, allmasked
     12374#quit()
     12375
     12376
Note: See TracChangeset for help on using the changeset viewer.