Changeset 2193 in lmdz_wrf


Ignore:
Timestamp:
Oct 18, 2018, 3:31:10 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `mat_dimreshape': Function to reshape a matrix on a new one according to values along their dimensions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2192 r2193  
    138138# list_norepeatcombos: Function to all possible (Num-1)-combinations of a Num values without repetitions
    139139# 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
     140# mat_dimreshape: Function to reshape a matrix on a new one according to values along their dimensions
    140141# maxNcounts: Function to provide a value according to the maximum number of repeated values along a given axis
    141142# minNcounts: Function to provide a value according to the minimum number of repeated values along a given axis
     
    98319832        if shape1[idn] != shape2[idn]:
    98329833            print errormsg
    9833             print '  ' +fname+ ': length of',idn,'diemsion in mat1 and mat2 differ !!'
     9834            print '  ' +fname+ ': length of',idn,'dimension in mat1 and mat2 differ !!'
    98349835            print '    Length mat 1:', shape1[idn],' Length mat2:', shape2[idn]
    98359836            print '    shape mat 1:', shape1, 'mat 2:', shape2
     
    1456114562    return rightCFtimeu
    1456214563
    14563 def masks_mult(dimns1, mask1, dimns2, mask2, dimsuse, shapeuse, nonusemaskdims):
    14564     """ Function to multiply two sets of masks on coincident diensions
    14565       dimns1= list of dimensions names for mask 1
    14566       mask1= mask 1
    14567       dimns2= list of dimensions names for mask 2
    14568       mask2= mask 2
    14569       dimnuse= list of dimensions to use
    14570       shapeuse: shape of being used
    14571       nonusedmaskdims: dictionary with values of the given dimennsios of the masks
    14572         not to be used
    14573     """
    14574     fname = 'masks_mult'
    14575 
    14576     coincdims = list(set(dimns1) & set(dimns2))
    14577     NOTcoincdims = list(set(dimns1) - set(dimns2))
    14578     print coincdims
    14579     print NOTcoincdims
     14564def mat_dimreshape(dimns, values, dimnsuse, shapeuse, nonusevaluesdims):
     14565    """ Function to reshape a matrix on a new one according to values along their
     14566        dimensions
     14567      dimns= list of dimensions names of the input matrix
     14568      values= values of the matrix
     14569      dimnsuse= list of dimensions to use
     14570      shapeuse: list with shape of being produced
     14571      nonusevaluesdims: dictionary to provide index values to that non-concident
     14572        dimennsios of [values] from [dimnuse]
     14573    >>> mask1 = np.zeros((3,3,3), dtype=bool)
     14574    mask1[1,:,:] = True
     14575    print mat_dimreshape(['dt', 'dy', 'dx'], mask1, ['dz', 'dy', 'dx'], [2, 3, 3], {'dt': 0})
     14576
     14577    [[[False False False]
     14578      [False False False]
     14579      [False False False]]
     14580
     14581     [[False False False]
     14582      [False False False]
     14583      [False False False]]]
     14584    >>> mask2 = np.zeros((2,3), dtype=bool)
     14585    mask2[1,1] = True
     14586    mat_dimreshape(['dt', 'dy'], mask2, ['dz', 'dy', 'dx'], [3, 3, 3], {'dt': 1})
     14587    [[[False False False]
     14588      [ True  True  True]
     14589      [False False False]]
     14590
     14591     [[False False False]
     14592      [ True  True  True]
     14593      [False False False]]
     14594
     14595     [[False False False]
     14596      [ True  True  True]
     14597      [False False False]]]
     14598    """
     14599    fname = 'mat_dimreshape'
     14600
     14601    coincdims = list(set(dimns) & set(dimnsuse))
     14602    NOTcoincdims = list(set(dimnsuse) - set(dimns))
    1458014603   
    14581     NOTsliceuse = []
     14604    newvalues = np.zeros(tuple(shapeuse), dtype=values.dtype)
     14605
     14606    # Slices to fill new matrix
     14607    newslices = provide_slices(dimnsuse, shapeuse, NOTcoincdims)
     14608
     14609    # Getting values to use to fill new matrix from values
     14610    shapevals = values.shape
     14611    slcvalues = []
    1458214612    idim = 0
    14583     for dimn in dimsuse:
    14584         if not searchInlist(dimns1, dimn):
    14585             NOTsliceuse.append(dimn)
    14586             continue
    14587         if searchInlist(dimns2, dimn):
    14588             NOTsliceuse.append(dimn)
    14589             continue
    14590 
    14591     mask = np.zeros(shapeuse, dtype=bool)
    14592     slices = []
    14593     idim = 0
    14594     for dimn in dimsuse:   
    14595         if searchInlist(NOTsliceuse, dimn):
    14596             slices.append(-9)
     14613    for dn in dimns:
     14614        if searchInlist(coincdims,dn):
     14615            slcvalues.append(slice(0,shapevals[idim]))
    1459714616        else:
    14598             slices.append(slice(0,shapeuse[idim]))
    14599         idim = idim+1
    14600 
    14601     finalslc = []
    14602     idim = 0
    14603     for slc in slices:
    14604         if type(slc) == type(slice(0,1)):
    14605             finalslc.append(slc)
    14606         else:
    14607             finalslc.append(range(shapeuse[idim]))
     14617            if not nonusevaluesdims.has_key(dn):
     14618                print errormsg
     14619                print '  ' + fname + ": non-concident dimension '" + dn +            \
     14620                  "' requires a value to proceed in dictionary 'nonusevaluesdims'!!"
     14621                print '    values provided _______'
     14622                printing_dictionary(nonusevaluesdims)
     14623                quit(-1)
     14624            else:
     14625                slcvalues.append(nonusevaluesdims[dn])
    1460814626        idim = idim + 1
    1460914627
    14610     print 'finalslc:', finalslc   
    14611 
    14612        
    14613 
    14614 
    14615     return
    14616 
    14617 mask1 = np.zeros((3,3,3), dtype=bool).reshape(3,3,3)
    14618 mask1[1,:,:] = True
    14619 
    14620 mask2 = np.zeros((3,3,3), dtype=bool).reshape(3,3,3)
    14621 mask2[1,1,1] = True
    14622 
    14623 masks_mult(['dt', 'dx', 'dy'], mask1, ['dt', 'dx', 'dy'], mask2, ['dz', 'dy', 'dx'], (2, 3, 3), {'dt': 0})
     14628    slicevals = values[tuple(slcvalues)]
     14629
     14630    # Checking for consistency
     14631    newvalslc = newvalues[tuple(newslices[0])]
     14632    matsame = same_shape(slicevals, newvalslc, quitval=False)
     14633    if not matsame:
     14634        print errormsg
     14635        print '  ' + fname + ": not coincident shapes of matrices !!"
     14636        print '    shape of example of slice for new values:', newvalslc.shape
     14637        print '      example of slice from the newvalues:', newslices[0]
     14638        print '    shape of values:', values.shape
     14639        print '    shape of slice from values to fill:', slicevals.shape
     14640        print '      slice of values:', slcvalues
     14641        print '    check dimensions and values provided ________'
     14642        print '      * dimension names of values:', dimns
     14643        print '      * dimensions of new values:', dimnsuse
     14644        print '      * shape of new values:', shapeuse
     14645        print '      * selected indices from values for non-coincident dimensions'
     14646        printing_dictionary(nonusevaluesdims)
     14647        quit(-1)
     14648
     14649    Nnewslices = len(newslices)
     14650    for islc in range(Nnewslices):
     14651        slc = newslices[islc]
     14652        newvalues[tuple(slc)] = slicevals
     14653
     14654    return newvalues
    1462414655
    1462514656#quit()
Note: See TracChangeset for help on using the changeset viewer.