Changeset 1699 in lmdz_wrf


Ignore:
Timestamp:
Dec 8, 2017, 5:42:47 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `fill_Narray': Function to fill a n-dimensional array with an arrary of lesser rank
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1671 r1699  
    7979# dtsec360dyr: Class to operate a number of seconds to a date in a 360 days/yr (or 12 30-days months) calendar
    8080# files_folder_HMT: Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]
     81# fill_Narray: Function to fill a n-dimensional array with an arrary of lesser rank
    8182# get_configuration: Function to get the configuration from an ASCII external file
    8283# get_specdictionary_HMT: Function to get specific values from a dictionary by selcting that keys with H*M*T
     
    1248512486    return True
    1248612487
     12488def fill_Narray(idata, narray, filldim=None):
     12489    """ Function to fill a n-dimensional array with an arrary of lesser rank
     12490      idata: initial array
     12491      narray: enpty narray yo fill
     12492      filldim: which number of dimensions have to be filled (None, for automatic)
     12493        provide a list of number of dimensions in case some lengths of dimensions are repeated
     12494      >>> ivec = np.arange(3, dtype=int)
     12495      >>> nmat = np.zeros((3,2), dtype=int)
     12496      >>> fill_Narray(ivec, nmat)
     12497      [[0 0]
     12498       [1 1]
     12499       [2 2]]
     12500      >>> nmat = np.zeros((3,2,3), dtype=int)
     12501      >>> fill_Narray(ivec, nmat, filldim=[2])
     12502      [[[0 0 0]
     12503        [0 0 0]]
     12504
     12505       [[1 1 1]
     12506        [1 1 1]]
     12507
     12508       [[2 2 2]
     12509        [2 2 2]]]
     12510    """
     12511    fname = 'fill_Narray'
     12512
     12513    ishape = idata.shape
     12514    nshape = narray.shape
     12515
     12516    Ndimsi = len(ishape)
     12517    Ndimsn = len(nshape)
     12518
     12519    # Dictionary with the equivalencies among dimensions
     12520    #    (-1, fill; 0, same dim as input)
     12521    dimequiv = {}
     12522    dimns = []
     12523    rundims = []
     12524    for idim in range(Ndimsn):
     12525        dimns.append(str(idim))
     12526        if searchInlist(ishape, nshape[idim]):
     12527            if filldim is not None:
     12528                if searchInlist(filldim, idim):
     12529                    dimequiv[idim] = -1
     12530                    rundims.append(str(idim))
     12531                else: dimequiv[idim] = 0
     12532            else:
     12533                dimequiv[idim] = 0
     12534        else:
     12535            dimequiv[idim] = -1
     12536            rundims.append(str(idim))
     12537
     12538    # Getting all the slices to fill
     12539    narrayslices = provide_slices(dimns, list(nshape), rundims)
     12540
     12541    # Filling narray
     12542    Nslices = len(narrayslices)
     12543    for islc in range(Nslices):
     12544        narray[tuple(narrayslices[islc])] = idata
     12545
     12546    return narray
     12547
    1248712548#quit()
    1248812549
Note: See TracChangeset for help on using the changeset viewer.