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


Ignore:
Timestamp:
Apr 4, 2017, 6:51:11 PM (8 years ago)
Author:
lfita
Message:

Adding:

`provide_slices': Function to provide a list of slices for a matrix giving a sub-section of running dimensions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1483 r1492  
    110110# prime_numbers: Function to find all the prime numbers up to a given value above 17
    111111# printing_dictionary: Function to print the content of a dictionary
     112# provide_slices: Function to provide a list of slices for a matrix giving a sub-section of running dimensions
    112113# radius_angle: Function to generate a matrix with the angle at a given point
    113114# radius_dist: Function to generate a matrix with the distance at a given point
     
    1152711528    return np.array([1, b, c])
    1152811529
     11530def provide_slices(dimns, dimzs, rundims):
     11531    """ Function to provide a list of slices for a matrix giving a sub-section of running dimensions
     11532        dimns: list of names of dimensions
     11533        dimzs: list of sizes of dimensions in the same order as in [dimns]
     11534        rundims: name of dimensions to allow to run
     11535    >>> provide_slices(['z', 't', 'y', 'x'], [2, 3, 10, 20], ['t', 'z', 'l'])
     11536    [[1, 2, slice(0, 10, None), slice(0, 20, None)],
     11537     [0, 1, slice(0, 10, None), slice(0, 20, None)],
     11538     [1, 1, slice(0, 10, None), slice(0, 20, None)],
     11539     [0, 0, slice(0, 10, None), slice(0, 20, None)],
     11540     [1, 0, slice(0, 10, None), slice(0, 20, None)],
     11541     [0, 2, slice(0, 10, None), slice(0, 20, None)]]
     11542    """
     11543    fname = 'provide_slices'
     11544
     11545    Ndims = len(dimns)
     11546
     11547    # Checking presence of dimension in list of dimensions
     11548    Tslicesize = 1
     11549    origslicesize = {}
     11550    runslicesize = {}
     11551    for dimn in rundims:
     11552        if not searchInlist(dimns, dimn):
     11553            #print warnmsg
     11554            #print '  ' + fname + ": dimension '" + dimn + "' not in list !!"
     11555            #print '    removing it'
     11556            rundims.remove(dimn)
     11557
     11558    for dimn in dimns:
     11559        idim = index_vec(dimns, dimn)
     11560        origslicesize[dimn] = dimzs[idim]
     11561        if searchInlist(rundims, dimn):
     11562            runslicesize[dimn] = dimzs[idim] - 1
     11563            Tslicesize = Tslicesize*dimzs[idim]
     11564
     11565    #print '  ' + fname + ': Total number of slices to provide:', Tslicesize,         \
     11566    #  'along:', runslicesize
     11567
     11568    slices = []
     11569    for il in range(Tslicesize):
     11570        islice = []
     11571        alreadychanged = False
     11572        for idim in range(Ndims):
     11573            dimn = dimns[idim]
     11574            if searchInlist(rundims,dimn):
     11575                islice.append(runslicesize[dimn])
     11576                # Only running value for a given dimension for the next
     11577                if not alreadychanged:
     11578                    runslicesize[dimn] = runslicesize[dimn] - 1
     11579                    alreadychanged = True
     11580                    if runslicesize[dimn] < 0:
     11581                        runslicesize[dimn] = origslicesize[dimn] - 1
     11582                        for rdimn in rundims:
     11583                            if rdimn != dimn:
     11584                                runslicesize[rdimn] = runslicesize[rdimn] - 1
     11585                                if runslicesize[rdimn] >= 0:
     11586                                    break
     11587                                else:
     11588                                    runslicesize[rdimn] = origslicesize[rdimn] - 1
     11589            else:
     11590                islice.append(slice(0,origslicesize[dimn]))
     11591
     11592        slices.append(islice)
     11593
     11594    return slices
     11595
    1152911596#quit()
Note: See TracChangeset for help on using the changeset viewer.