Changeset 2371 in lmdz_wrf


Ignore:
Timestamp:
Feb 26, 2019, 8:51:00 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `range_slicing': Function to provide a list of slices for a series of dimensions giving intervals for a sub-set of the dimensions in order to avoid memory problems
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2370 r2371  
    175175# radius_angle: Function to generate a matrix with the angle at a given point
    176176# radius_dist: Function to generate a matrix with the distance at a given point
     177# range_slicing: Function to provide a list of slices for a series of dimensions giving
     178#   intervals for a sub-set of the dimensions in order to avoid memory problems
    177179# rectangular_spiral: Function to provide a rectangular spiral (along x,y-axis values) of values
    178180# remove_monotones: Function to remove the monotones (len(dim) = 1) from an array
     
    1522715229
    1522815230def range_slicing(dimns, dimvs, intdims):
    15229     """ Function to provide a list of slices for a series of dimensions giving       
    15230           intervals for a series of dimensions in order to avoid memory problems
     15231    """ Function to provide a list of slices for a series of dimensions giving
     15232          intervals for a sub-set of the dimensions in order to avoid memory problems
     15233          * returns: list of slices, array with the start and end for each dimension
     15234           and slice
    1523115235        dimns: list with the names of dimensions
    1523215236        dimvs: list with the lengths of dimensions (same order)
    1523315237        intdims: dictionary with the name of dimensions with the length of intervals
    1523415238    >>> range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7})
     15239    [[slice(0, 2, None), slice(0, 3, None), slice(0, 7, None), slice(0, 5, None)],
     15240     [slice(0, 2, None), slice(0, 3, None), slice(0, 7, None), slice(5, 10, None)],
     15241     [slice(0, 2, None), slice(0, 3, None), slice(0, 7, None), slice(10, 15, None)],
     15242     [slice(0, 2, None), slice(0, 3, None), slice(7, 10, None), slice(0, 5, None)],
     15243     [slice(0, 2, None), slice(0, 3, None), slice(7, 10, None), slice(5, 10, None)],
     15244     [slice(0, 2, None), slice(0, 3, None), slice(7, 10, None), slice(10, 15, None)]],
     15245    array([[[ 0,  2],
     15246            [ 0,  3],
     15247            [ 0,  7],
     15248            [ 0,  5]],
     15249
     15250           [[ 0,  2],
     15251            [ 0,  3],
     15252            [ 0,  7],
     15253            [ 5, 10]],
     15254   
     15255           [[ 0,  2],
     15256            [ 0,  3],
     15257            [ 0,  7],
     15258            [10, 15]],
     15259
     15260           [[ 0,  2],
     15261            [ 0,  3],
     15262            [ 7, 10],
     15263            [ 0,  5]],
     15264
     15265           [[ 0,  2],
     15266            [ 0,  3],
     15267            [ 7, 10],
     15268            [ 5, 10]],
     15269
     15270           [[ 0,  2],
     15271            [ 0,  3],
     15272            [ 7, 10],
     15273            [10, 15]]])
    1523515274    """
    1523615275    fname = 'range_slicing'
     
    1525615295        islcdim[dimn] = 0
    1525715296        doneallslices[dimn] = False
    15258         print 'intervals ', dimn, ':', intslices[dimn]
    15259 
    15260     origslicesize = {}
    15261     Tslicesize = 1
     15297        #print 'intervals ', dimn, ':', intslices[dimn]
     15298
     15299    # Constructing the list for the length for each interval dimension
     15300    intloopvals = []
     15301    # indices of the interval dimensions
     15302    iintd = {}
     15303    iid = 0
    1526215304    for dimn in dimns:
    15263         idim = index_vec(dimns, dimn)
    15264         origslicesize[dimn] = dims[dimn]
    15265         if searchInlist(intdims.keys(), dimn):
    15266             Tslicesize = Tslicesize*Lslcdim[dimn]
    15267             lastintdimn = dimn
    15268 
    15269     print '  ' + fname + ': Total number of slices to provide:', Tslicesize
     15305        if intdims.has_key(dimn):
     15306            intloopvals.append(Lslcdim[dimn])
     15307            iintd[dimn] = iid
     15308            iid = iid + 1
     15309
     15310    # Getting all the combinations of the interval slices
     15311    intcombs = Nloops_1D(intloopvals)
     15312    Tslicesize = intcombs.shape[0]
     15313
     15314    #print '  ' + fname + ': Total number of slices to provide:', Tslicesize
    1527015315
    1527115316    slices = []
     15317    inddims = np.zeros((Tslicesize,Ndims,2), dtype=int)
    1527215318    for il in range(Tslicesize):
    15273         advanced = False
    15274         print 'il:', il
     15319        # indices of the looping dimensions
     15320        loopv = intcombs[il]
    1527515321        islice = []
    1527615322        for idim in range(Ndims):
    1527715323            dimn = dimns[idim]
    1527815324            if intdims.has_key(dimn):
    15279                 # getting dimension slice status
    15280                 Lslc = Lslcdim[dimn]
    15281                 slcsd = intslices[dimn]
    15282                 dimdone = doneallslices[dimn]
    15283                 islc = islcdim[dimn]           
    15284                
    15285                 # Which is the status of later dimensions
    15286                 if idim < Ndims-1:
    15287                     allintdns = True
    15288                     for iid in range(idim+1,Ndims):
    15289                         if intdims.has_key(dimns[iid]):
    15290                             # Up to the dimension without all the intervals done
    15291                             if not doneallslices[dimns[iid]]:
    15292                                 allintdns = False
    15293                                 break
    15294                 else:
    15295                     allintdns = False
    15296 
    15297                 if allintdns:
    15298                     if not advanced:
    15299                         if islc+1 <= Lslc-1:
    15300                             print 'Advancing dim !!', dimn, islc, islc+1, '.', Lslc, slcsd
    15301                             islc = islc + 1
    15302                             advanced = True
    15303                         else:
    15304                             doneallslices[dimn] = True
    15305                     # Zeroeing later dims
    15306                     for iid in range(idim+1,Ndims):
    15307                         if intdims.has_key(dimns[iid]):
    15308                             islcdim[dimns[iid]] = 0
    15309                             doneallslices[dimns[iid]] = False
    15310                 islice.append(slice(slcsd[islc], slcsd[islc+1]))
    15311                 islcdim[dimn] = islc
     15325                dslcv = intslices[dimn]
     15326                iid = iintd[dimn]
     15327                intdslcv = loopv[iid]
     15328                islice.append(slice(dslcv[intdslcv],dslcv[intdslcv+1]))
     15329                inddims[il,idim,0]=dslcv[intdslcv]
     15330                inddims[il,idim,1]=dslcv[intdslcv+1]
    1531215331            else:
    15313                 islice.append(slice(0,origslicesize[dimn]))
    15314 
    15315         print il, ':', islcdim, doneallslices
     15332                islice.append(slice(0,dims[dimn]))
     15333                inddims[il,idim,0]=0
     15334                inddims[il,idim,1]=dims[dimn]
    1531615335
    1531715336        slices.append(islice)
    15318         if not advanced:
    15319             islc = islcdim[lastintdimn]
    15320             islc = islc + 1
    15321             islcdim[lastintdimn] = islc
    15322             if islc > Lslc-1:
    15323                 doneallslices[lastintdimn] = islc
    15324 
    15325     return slices
    15326 
    15327 slcs = range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7})
    15328 Nslcs = len(slcs)
    15329 for i in range(Nslcs):
    15330     print i, ':', slcs[i]
     15337
     15338    return slices, inddims
     15339
     15340#slcs, inds = range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7})
     15341#Nslcs = len(slcs)
     15342#for i in range(Nslcs):
     15343#    print i, ':', slcs[i]
     15344#    for j in range(4):
     15345#        print '  ' , '(', inds[i,j,0], ',', inds[i,j,1], ')'
     15346#print range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7})
    1533115347
    1533215348#quit()
Note: See TracChangeset for help on using the changeset viewer.