Changeset 828 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jun 16, 2016, 12:13:05 AM (8 years ago)
Author:
lfita
Message:

Adding:

  • `radius_angle': Function to generate a matrix with the angle at a given point
  • `subbasin_point': Function to provide sub-basins given a grid point following a matrix of trips
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic.py

    r818 r828  
    126126        print gen.datetimeStr_conversion(vals[0], vals[1], vals[2])
    127127
    128 'days_period'
     128#'days_period'
    129129
    130130elif oper == 'grid_combinations':
  • trunk/tools/generic_tools.py

    r826 r828  
    3030# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
    3131# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
     32# index_mat_way: Function to look for a value within a matrix following a sense
    3233# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    3334# num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
    3435# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
     36# radius_angle: Function to generate a matrix with the angle at a given point
     37# radius_dist: Function to generate a matrix with the distance at a given point
    3538# significant_decomposition: Function to decompose a given number by its signifcant potencies
    3639# unitsdsDate: Function to know how many units of time are from a given pair of dates
     
    46344637      [dx/y]: dimension of the matrix
    46354638      [ptx/y]: grid point coordinates of the point
    4636     >>> radius_dist(3,5,2,2)
    4637     [[ 1.41421356  1.          1.41421356  2.23606798  3.16227766]
    4638      [ 1.          0.          1.          2.          3.        ]
    4639      [ 1.41421356  1.          1.41421356  2.23606798  3.16227766]]
     4639    >>> radius_dist(5,4,2,2)
     4640    [[ 2.82842712  2.23606798  2.          2.23606798  2.82842712]
     4641     [ 2.23606798  1.41421356  1.          1.41421356  2.23606798]
     4642     [ 2.          1.          0.          1.          2.        ]
     4643     [ 2.23606798  1.41421356  1.          1.41421356  2.23606798]]
    46404644    """
    46414645
     
    46484652        quit(-1)
    46494653
    4650     xdist =  np.zeros((dx,dy), dtype=np.float)
    4651     ydist =  np.zeros((dx,dy), dtype=np.float)
    4652     dist =  np.zeros((dx,dy), dtype=np.float)
     4654    xdist =  np.zeros((dy,dx), dtype=np.float)
     4655    ydist =  np.zeros((dy,dx), dtype=np.float)
     4656    dist =  np.zeros((dy,dx), dtype=np.float)
    46534657
    46544658    for ix in range(dx):
    4655         xdist[ix,:] = np.float(ix-ptx)
     4659        xdist[:,ix] = np.float(ix-ptx)
    46564660    for iy in range(dy):
    4657         ydist[:,iy] = np.float(iy-pty)
     4661        ydist[iy,:] = np.float(pty-iy)
    46584662
    46594663    dist = np.sqrt(xdist*xdist + ydist*ydist)
    46604664
    46614665    return dist
     4666
     4667def radius_angle(dx,dy,ptx,pty):
     4668    """ Function to generate a matrix with the angle at a given point
     4669    radius_angle(dx,dy,ptx,pty)
     4670      [dx/y]: dimension of the matrix
     4671      [ptx/y]: grid point coordinates of the point
     4672    >>> radius_angle(5,4,2,2)*180./np.pi
     4673    [[ 315.          333.43494882    0.           26.56505118   45.        ]
     4674     [ 296.56505118  315.            0.           45.           63.43494882]
     4675     [ 270.          270.            0.           90.           90.        ]
     4676     [ 243.43494882  225.          180.          135.          116.56505118]]
     4677    """
     4678    fname = 'radius_angle'
     4679
     4680    if ptx < 0 or ptx > dx-1 or pty < 0 or pty > dy-1:
     4681        print errormsg
     4682        print '  ' + fname + ': wrong point coordinates:',dx,',',dy,'for matrix;',dx \
     4683         ,'x',dy
     4684        quit(-1)
     4685
     4686    xdist =  np.zeros((dy,dx), dtype=np.float)
     4687    ydist =  np.zeros((dy,dx), dtype=np.float)
     4688    angle =  np.zeros((dy,dx), dtype=np.float)
     4689
     4690    for ix in range(dx):
     4691        xdist[:,ix] = np.float(ix-ptx)
     4692    for iy in range(dy):
     4693        ydist[iy,:] = np.float(pty-iy)
     4694
     4695    angle = np.arctan2(xdist,ydist)
     4696    angle = np.where(angle < 0., angle + 2*np.pi, angle)
     4697
     4698    return angle
    46624699
    46634700def lonlat2D(lon,lat):
     
    73207357    hexblue = '{0:X}'.format(int(col[2]*255))
    73217358
    7322     if hexred == '0': hexred = '00'
     7359    if hexred == '0': hexred = '00' 
    73237360    if hexgreen == '0': hexgreen = '00'
    73247361    if hexblue == '0': hexblue = '00'
     
    73287365    return hexcol
    73297366
     7367def index_mat_way(mat,val,way):
     7368    """ Function to look for a value within a matrix following a sense
     7369      mat: matrix
     7370      val: value to search
     7371      way: way of search
     7372        'anticlockwise12': looking in an anti-clockwise sens starting at 12
     7373        'clockwise12': looking in an clockwise sens starting at 12
     7374    >>> mat = np.arange(20).reshape(4,5)
     7375    >>> mat[2,1] = 3
     7376    >>> index_mat_way(mat,3,'clockwise12')
     7377    [array([0, 3]), array([2, 1])]
     7378    >>> index_mat_way(mat,3,'anticlockwise12')
     7379    [array([2, 1]), array([0, 3])]
     7380    """
     7381    fname = 'index_mat_way'
     7382
     7383    ways = ['anticlockwise12', 'anticlockwise6', 'clockwise12', 'clockwise6']
     7384
     7385    Ndims = len(mat.shape)
     7386    dx = mat.shape[Ndims-1]
     7387    dy = mat.shape[Ndims-2]
     7388
     7389    diffmat = np.abs(mat - val)
     7390    mindiff = np.min(diffmat)
     7391    if np.sum(diffmat == mindiff) == 1:
     7392        ijindex = index_mat(diffmat,mindiff)
     7393        return ijindex
     7394
     7395    if Ndims > 2: dz = mat.shape[Ndims-3]
     7396    if Ndims > 3: dt = mat.shape[Ndims-4]
     7397
     7398    if way == 'anticlockwise12':
     7399# Sorting following a double criteria, first angle and then distance taken from the
     7400#   center in the anti-clockwise sens starting at 12
     7401        if Ndims > 2:
     7402            print errormsg
     7403            print '  ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way +       \
     7404              "' is not possible !!"
     7405            quit(-1)
     7406 
     7407        distrad = radius_dist(dx,dy,dx/2,dy/2)
     7408        distangle = radius_angle(dx,dy,dx/2,dy/2)
     7409        distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle)
     7410        distangle = np.where(distangle > 0., 2.* np.pi - distangle, -distangle)
     7411
     7412        sortedij = {}
     7413        minangle = 10000.
     7414        notfound = np.zeros((dy,dx), dtype=bool)
     7415        maskdistrad = ma.array(distrad, mask=notfound)
     7416        maskdistangle = ma.array(distangle, mask=notfound)
     7417        for ip in range(dx*dy):
     7418            minangle = np.min(maskdistangle)
     7419            jiangle = multi_index_mat(maskdistangle, minangle)
     7420            mindist = 10000.
     7421            for ia in range(len(jiangle)):
     7422                iaji = jiangle[ia]
     7423                if maskdistrad[iaji[0], iaji[1]] < mindist:
     7424                    mindist = maskdistrad[iaji[0], iaji[1]]
     7425                    idistangle = iaji
     7426            notfound[idistangle[0],idistangle[1]] = True
     7427            sortedij[ip] = idistangle
     7428
     7429    elif way == 'anticlockwise6':
     7430# Sorting following a double criteria, first angle and then distance taken from the
     7431#   center in the anti-clockwise sens starting at 6
     7432        if Ndims > 2:
     7433            print errormsg
     7434            print '  ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way +       \
     7435              "' is not possible !!"
     7436            quit(-1)
     7437 
     7438        distrad = radius_dist(dx,dy,dx/2,dy/2)
     7439        distangle = radius_angle(dx,dy,dx/2,dy/2)
     7440        distangle = np.where(distangle > np.pi, distangle - 2.*np.pi, distangle)
     7441        distangle = np.where(distangle >= 0., np.pi - distangle, np.pi-distangle)
     7442
     7443        sortedij = {}
     7444        minangle = 10000.
     7445        notfound = np.zeros((dy,dx), dtype=bool)
     7446        maskdistrad = ma.array(distrad, mask=notfound)
     7447        maskdistangle = ma.array(distangle, mask=notfound)
     7448        for ip in range(dx*dy):
     7449            minangle = np.min(maskdistangle)
     7450            jiangle = multi_index_mat(maskdistangle, minangle)
     7451            mindist = 10000.
     7452            for ia in range(len(jiangle)):
     7453                iaji = jiangle[ia]
     7454                if maskdistrad[iaji[0], iaji[1]] < mindist:
     7455                    mindist = maskdistrad[iaji[0], iaji[1]]
     7456                    idistangle = iaji
     7457            notfound[idistangle[0],idistangle[1]] = True
     7458            sortedij[ip] = idistangle
     7459
     7460    elif way == 'clockwise12':
     7461# Sorting following a double criteria, first angle and then distance taken from the
     7462#   center in the cloc-kwise sens starting at 12
     7463        if Ndims > 2:
     7464            print errormsg
     7465            print '  ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way +       \
     7466              "' is not possible !!"
     7467            quit(-1)
     7468 
     7469        distrad = radius_dist(dx,dy,dx/2,dy/2)
     7470        distangle = radius_angle(dx,dy,dx/2,dy/2)
     7471
     7472        sortedij = {}
     7473        minangle = 10000.
     7474        notfound = np.zeros((dy,dx), dtype=bool)
     7475        maskdistrad = ma.array(distrad, mask=notfound)
     7476        maskdistangle = ma.array(distangle, mask=notfound)
     7477        for ip in range(dx*dy):
     7478            minangle = np.min(maskdistangle)
     7479            jiangle = multi_index_mat(maskdistangle, minangle)
     7480            mindist = 10000.
     7481            for ia in range(len(jiangle)):
     7482                iaji = jiangle[ia]
     7483                if maskdistrad[iaji[0], iaji[1]] < mindist:
     7484                    mindist = maskdistrad[iaji[0], iaji[1]]
     7485                    idistangle = iaji
     7486            notfound[idistangle[0],idistangle[1]] = True
     7487            sortedij[ip] = idistangle
     7488
     7489    elif way == 'clockwise6':
     7490# Sorting following a double criteria, first angle and then distance taken from the
     7491#   center in the clockwise sens starting at 6
     7492        if Ndims > 2:
     7493            print errormsg
     7494            print '  ' + fame + ': wiht more than 2-dims:', Ndims, "'" + way +       \
     7495              "' is not possible !!"
     7496            quit(-1)
     7497 
     7498        distrad = radius_dist(dx,dy,dx/2,dy/2)
     7499        distangle = radius_angle(dx,dy,dx/2,dy/2)
     7500        distangle = np.where(distangle >= np.pi, distangle - 2.*np.pi, distangle)
     7501        distangle = np.where(distangle > 0., np.pi + distangle, np.pi+distangle)
     7502
     7503        sortedij = {}
     7504        minangle = 10000.
     7505        notfound = np.zeros((dy,dx), dtype=bool)
     7506        maskdistrad = ma.array(distrad, mask=notfound)
     7507        maskdistangle = ma.array(distangle, mask=notfound)
     7508        for ip in range(dx*dy):
     7509            minangle = np.min(maskdistangle)
     7510            jiangle = multi_index_mat(maskdistangle, minangle)
     7511            mindist = 10000.
     7512            for ia in range(len(jiangle)):
     7513                iaji = jiangle[ia]
     7514                if maskdistrad[iaji[0], iaji[1]] < mindist:
     7515                    mindist = maskdistrad[iaji[0], iaji[1]]
     7516                    idistangle = iaji
     7517            notfound[idistangle[0],idistangle[1]] = True
     7518            sortedij[ip] = idistangle
     7519
     7520    else:
     7521        print errormsg
     7522        print '  ' + fname + ": way of search '" + way + "' not ready !!"
     7523        print '    available ways:', ways
     7524
     7525    indices = []
     7526    for ij in range(dx*dy):
     7527        sij = sortedij[ij]
     7528        sval = mat[sij[0], sij[1]]
     7529        if sval == val:
     7530            indices.append(sij)
     7531
     7532    return indices
     7533
     7534mat = np.arange(20).reshape(4,5)
     7535mat[2,1] = 3
     7536print index_mat_way(mat,3,'anticlockwise6')
     7537
    73307538#quit()
    73317539
Note: See TracChangeset for help on using the changeset viewer.