Changeset 307 in lmdz_wrf for trunk


Ignore:
Timestamp:
Feb 26, 2015, 9:43:15 AM (10 years ago)
Author:
lfita
Message:

Moving some functions to their place

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r306 r307  
    704704    return varvals
    705705
     706def numVector_String(vec,char):
     707    """ Function to transform a vector of numbers to a single string [char] separated
     708    numVector_String(vec,char)
     709      vec= vector with the numerical values
     710      char= single character to split the values
     711    >>> print numVector_String(np.arange(10),' ')
     712    0 1 2 3 4 5 6 7 8 9
     713    """
     714    fname = 'numVector_String'
     715
     716    if vec == 'h':
     717        print fname + '_____________________________________________________________'
     718        print numVector_String.__doc__
     719        quit()
     720
     721    Nvals = len(vec)
     722
     723    string=''
     724    for i in range(Nvals):
     725        if i == 0:
     726            string = str(vec[i])
     727        else:
     728            string = string + char + str(vec[i])
     729
     730    return string
     731
     732def index_mat(mat,val):
     733    """ Function to provide the coordinates of a given value inside a matrix
     734    index_mat(mat,val)
     735      mat= matrix with values
     736      val= value to search
     737    >>> index_mat(np.arange(27).reshape(3,3,3),22)
     738    [2 1 1]
     739    """
     740
     741    fname = 'index_mat'
     742
     743    matshape = mat.shape
     744
     745    matlist = list(mat.flatten())
     746    ifound = matlist.index(val)
     747
     748    Ndims = len(matshape)
     749    valpos = np.zeros((Ndims), dtype=int)
     750    baseprevdims = np.zeros((Ndims), dtype=int)
     751
     752    for dimid in range(Ndims):
     753        baseprevdims[dimid] = np.product(matshape[dimid+1:Ndims])
     754        if dimid == 0:
     755            alreadyplaced = 0
     756        else:
     757            alreadyplaced = np.sum(baseprevdims[0:dimid]*valpos[0:dimid])
     758        valpos[dimid] = int((ifound - alreadyplaced )/ baseprevdims[dimid])
     759
     760    return valpos
    706761
    707762####### ###### ##### #### ### ## #
     
    1125211307#  '001/full_concatenated.nc', 'PSFC')
    1125311308
    11254 def numVector_String(vec,char):
    11255     """ Function to transform a vector of numbers to a single string [char] separated
    11256     numVector_String(vec,char)
    11257       vec= vector with the numerical values
    11258       char= single character to split the values
    11259     >>> print numVector_String(np.arange(10),' ')
    11260     0 1 2 3 4 5 6 7 8 9
    11261     """
    11262     fname = 'numVector_String'
    11263 
    11264     if vec == 'h':
    11265         print fname + '_____________________________________________________________'
    11266         print numVector_String.__doc__
    11267         quit()
    11268 
    11269     Nvals = len(vec)
    11270 
    11271     string=''
    11272     for i in range(Nvals):
    11273         if i == 0:
    11274             string = str(vec[i])
    11275         else:
    11276             string = string + char + str(vec[i])
    11277 
    11278     return string
    11279 
    1128011309def interpolate_locs(locs,coords,kinterp):
    1128111310    """ Function to provide interpolate locations on a given axis
     
    1258812617
    1258912618#netcdf_fold_concatenation('/media/data2/etudes/WRF_LMDZ/WL_HyMeX_HighRes/medic950116,Time', 'wrfout_d03', 'Times,XLONG,XLAT,T2,TH2,PSFC,U10,V10,MAPFAC_M')
    12590 
    12591 def index_mat(mat,val):
    12592     """ Function to provide the coordinates of a given value inside a matrix
    12593     index_mat(mat,val)
    12594       mat= matrix with values
    12595       val= value to search
    12596     >>> index_mat(np.arange(27).reshape(3,3,3),22)
    12597     [2 1 1]
    12598     """
    12599 
    12600     fname = 'index_mat'
    12601 
    12602     matshape = mat.shape
    12603 
    12604     matlist = list(mat.flatten())
    12605     ifound = matlist.index(val)
    12606 
    12607     Ndims = len(matshape)
    12608     valpos = np.zeros((Ndims), dtype=int)
    12609     baseprevdims = np.zeros((Ndims), dtype=int)
    12610 
    12611     for dimid in range(Ndims):
    12612         baseprevdims[dimid] = np.product(matshape[dimid+1:Ndims])
    12613         if dimid == 0:
    12614             alreadyplaced = 0
    12615         else:
    12616             alreadyplaced = np.sum(baseprevdims[0:dimid]*valpos[0:dimid])
    12617         valpos[dimid] = int((ifound - alreadyplaced )/ baseprevdims[dimid])
    12618 
    12619     return valpos
    1262012619
    1262112620def netcdf_concatenation(ncfile):
Note: See TracChangeset for help on using the changeset viewer.