Changeset 709 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 20, 2016, 6:00:26 PM (9 years ago)
Author:
lfita
Message:

Adding `multi_index_mat': Function to provide the multiple coordinates of a given value inside a matrix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r708 r709  
    12101210    return valpos
    12111211
     1212def multi_index_mat(mat,val):
     1213    """ Function to provide the multiple coordinates of a given value inside a matrix
     1214    index_mat(mat,val)
     1215      mat= matrix with values
     1216      val= value to search
     1217    >>> vals = np.ones((24), dtype=np.float).reshape(2,3,4)
     1218    vals[:,:,2] = 0.
     1219    vals[1,:,:] = np.pi
     1220    vals[:,2,:] = -1.
     1221    multi_index_mat(vals,1.)
     1222    [array([0, 0, 0]), array([0, 0, 1]), array([0, 0, 3]), array([0, 1, 0]), array([0, 1, 1]), array([0, 1, 3])]
     1223    """
     1224    fname = 'multi_index_mat'
     1225
     1226    matshape = mat.shape
     1227
     1228    ivalpos = []
     1229    matlist = list(mat.flatten())
     1230    Lmatlist = len(matlist)
     1231   
     1232    val0 = val - val
     1233    if val != val0:
     1234        valdiff = val0
     1235    else:
     1236        valdiff = np.ones((1), dtype = type(val))
     1237   
     1238    ifound = 0
     1239    while ifound < Lmatlist:
     1240        if matlist.count(val) == 0:
     1241            ifound = Lmatlist + 1
     1242        else:
     1243            ifound = matlist.index(val)
     1244
     1245            Ndims = len(matshape)
     1246            valpos = np.zeros((Ndims), dtype=int)
     1247            baseprevdims = np.zeros((Ndims), dtype=int)
     1248
     1249            for dimid in range(Ndims):
     1250                baseprevdims[dimid] = np.product(matshape[dimid+1:Ndims])
     1251                if dimid == 0:
     1252                    alreadyplaced = 0
     1253                else:
     1254                    alreadyplaced = np.sum(baseprevdims[0:dimid]*valpos[0:dimid])
     1255                valpos[dimid] = int((ifound - alreadyplaced )/ baseprevdims[dimid])
     1256            matlist[ifound] = valdiff
     1257            ivalpos.append(valpos)
     1258
     1259    return ivalpos
     1260
    12121261def addfileInfile(origfile,destfile,addfile,addsign):
    12131262    """ Function to add the content of a file [addfile] to another one [origfile] at
Note: See TracChangeset for help on using the changeset viewer.