Changeset 1391 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Dec 19, 2016, 4:09:08 PM (8 years ago)
Author:
lfita
Message:

Changing no-value by masked array on vals_around', pass changes to linearint_3x3weights'
Adding:
curvelocalize_2D: Function to provide the localization a curve in a 2D field of positions via the equivalent

i,j-quads within which the curve lays (-1, no value for the given quad [2x2 points around]) and provide
the weights of the quad to perform a distance-weighted mean at the given curve point

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1390 r1391  
    5555# color_deg: Function to generate a degradation of colors in rgb base
    5656# contflow: Function to bring back the increment in j,i grid points according to a trip: (inflow directions)
     57# curvelocalize_2D: Function to provide the localization a curve in a 2D field of positions via the equivalent
     58#   i,j-quads within which the curve lays (-1, no value for the given quad [2x2 points around]) and provide
     59#   the weights of the quad to perform a distance-weighted mean at the given curve point
    5760# dictionary_key: Function to provide the first key in a dictionay with a given value
    5861# dictionary_key_list: Function to provide the first key in a dictionary of lists with a given value
     
    75927595     [None None None]]
    75937596    """
     7597    import numpy.ma as ma
    75947598    fname = 'vals_around'
    75957599
    7596     vals3x3 = np.array([None]*9).reshape(3,3)
     7600    # Issues with 'None'
     7601    #vals3x3 = np.array([None]*9).reshape(3,3)
     7602    vals3x3 = ma.masked_equal(np.ones((3,3), dtype=np.float), 1.)
    75977603
    75987604    dx = vals.shape[1]
     
    1080310809    [  5.00000000e-04   9.99500000e-01]
    1080410810    """
    10805     fname = 'linear_wegiths'
     10811    fname = 'linear_weights'
    1080610812
    1080710813    if vals[0] > vals[1]:
     
    1083910845        * Number of closest grid points (<=4)
    1084010846        * 2x9 matrix with distance-sorted positions within the 3x3 matrix
    10841     xvals = np.zeros(9).reshape(3,3)
     10847    xvals = ma.masked_equal(np.zeros(9).reshape(3,3),0.)
    1084210848    xvals[:,0] = 1.
    1084310849    xvals[:,1] = 2.
    1084410850    xvals[:,2] = 3.
    10845     xvals[2,2] = None
     10851    xvals.mask[2,2] = True
    1084610852    yvals = xvals.transpose()
    1084710853    >>> linearint_3x3weights(xvals, yvals, [1.5, 2.5])
     
    1085810864           [ 1,  2,  1,  2,  0,  0,  1,  0, -1]]))
    1085910865    """
    10860     fname = 'linear_wegiths'
     10866    fname = 'linear_3x3wegiths'
    1086110867
    1086210868    intval = np.array(intval0, dtype=np.float)
    10863     print 'type xvals:',  xvals.dtype
    10864 
    10865     # Assuming that values come from `vals_around' function (None, when outside matrix size)
    10866     xvalsma = ma.masked_equal(xvals,None)
    10867     yvalsma = ma.masked_equal(yvals,None)
    1086810869
    1086910870    # Check data
    10870     nx = np.min(xvals)
    10871     xx = np.max(xvals)
    10872     ny = np.min(yvals)
    10873     xy = np.max(yvals)
     10871    nx = xvals.min()
     10872    xx = xvals.max()
     10873    ny = yvals.min()
     10874    xy = yvals.max()
     10875
     10876    #print 'Lluis intval:', intval
     10877    #print '  ' + fname + ' Lluis xvals ____'
     10878    #print xvals
     10879
     10880    #print '  ' + fname + ' Lluis yvals ____'
     10881    #print yvals
    1087410882
    1087510883    if not intval[1] >= nx and intval[1] <= xx:
    1087610884        print errormsg
    10877         print '  ' + fname + ': wrong value:', intval[1] , ' to interpolate for range:'
     10885        print '  ' + fname + ': wrong value:', intval[1] , ' to interpolate for ' +  \
     10886          'x-range:'
    1087810887        for i in range(3):
    1087910888            print '    ', xvals[:,i]
     
    1088110890    if not intval[0] >= ny and intval[0] <= xy:
    1088210891        print errormsg
    10883         print '  ' + fname + ': wrong value:', intval[0] , ' to interpolate for range:'
     10892        print '  ' + fname + ': wrong value:', intval[0] , ' to interpolate for ' +  \
     10893          'y-range:'
    1088410894        for i in range(3):
    1088510895            print '    ', yvals[:,i]
    1088610896        quit(-1)
    1088710897
    10888     print 'Lluis intval:', intval
    10889     print '  ' + fname + ' Lluis xvalsma ____'
    10890     print xvalsma
    10891 
    10892     print '  ' + fname + ' Lluis yvalsma ____'
    10893     print yvalsma
    10894 
    1089510898    dist = np.sqrt( (xvals-intval[1])**2 + (yvals-intval[0])**2)
    1089610899    inc = np.sum(dist)
    10897     print 'inc:', inc, 'intval', intval
    10898     for i in range(3):
    10899         print dist[:,i]
     10900    #print 'inc:', inc, 'intval', intval, 'dist ___'
     10901    #for i in range(3):
     10902    #    print dist[:,i]
    1090010903
    1090110904    ijvals = dist.copy()
     
    1090310906
    1090410907    # shortest minium distance
    10905     sortdist = list(dist.flatten())
     10908    sortdist = list(dist.compressed())
    1090610909    sortdist.sort()
    1090710910    iijj = 0   
     
    1093310936
    1093410937    wgt = np.where(dist <= maxdist, dist / inc, 0.)
     10938    #print 'Lluis weights _______', sortdist[0:4]
     10939    #for i in range(3):
     10940    #    print wgt[:,i]
    1093510941
    1093610942    return wgt, Nvals, distpos
    10937 
     10943 
    1093810944def curvelocalize_2D(curve,xvals,yvals):
    1093910945    """ Function to provide the localization a curve in a 2D field of positions via the equivalent i,j-quads
     
    1095610962      yvals= 2D matrix of y-coordinate values
    1095710963      RETURNS:
    10958         curvloc= [2,Npts] matrix with the closest i,j grid point in the 2D space of the field positions
     10964        curvloc= [Npts,2] matrix with the closest i,j grid point in the 2D space of the field positions
    1095910965        curvweights= [Npts,3,3] 3x3 matrix weights around the curve position
    1096010966        Nwgt= Number of weights
    10961         ijaorund3x3= [2,9] distance (in xval, yval units) to the curve position
     10967        ijaorund3x3= [Npts,2,9] distance (in xval, yval units) to the curve position
    1096210968    """
    1096310969    fname = 'curvelocalize_2D'
    1096410970
    10965     Npts = curve.shape[1]
     10971    Npts = curve.shape[0]
    1096610972
    1096710973    curvloc = np.ones((curve.shape), dtype=int)*(-1)
    10968     Nquad = np.ones((Npts), dtype=int)*(-1)
    10969     curvweights = np.zeros((Npts,3,3), dtype=float)
     10974    curveweights = np.zeros((Npts,3,3), dtype=float)
     10975    ijaround3x3 = np.zeros((Npts,2,9), dtype=int)
    1097010976
    1097110977    for icv in range(Npts):
    1097210978        # Closest grid-point
    10973         dist = np.sqrt((xvals - curve[1,icv])**2 + (yvals - curve[0,icv])**2)
     10979        dist = np.sqrt((xvals - curve[icv,1])**2 + (yvals - curve[icv,0])**2)
    1097410980        mindist = np.min(dist)
    1097510981        ijloc = index_mat(dist,mindist)
    1097610982
    10977         curvloc[0,icv] = ijloc[0]
    10978         curvloc[1,icv] = ijloc[1]
     10983        if np.any(ijloc == -1):
     10984            print errormsg
     10985            print '  ', fname, ': curve position:', curve[icv,:], 'not found !!'
     10986            quit(-1)
     10987        curvloc[icv,0] = ijloc[0]
     10988        curvloc[icv,1] = ijloc[1]
    1097910989
    1098010990        # Values around the nearest 2D-point to the curve point
    1098110991        xaroundvls = vals_around(xvals,ijloc)
    1098210992        yaroundvls = vals_around(yvals,ijloc)
    10983         print 'Lluis type xaroundvls:', type(xaroundvls)
    10984 
    10985         curveweights[icv,:,:], Nwgt, ijaorund3x3= linearint_3x3weights(xaroundvls,   \
    10986           yaroundvls, curve[:,icv])
     10993
     10994        curveweights[icv,:,:], Nwgt, ijaround3x3[icv,:,:,] = linearint_3x3weights(    \
     10995          xaroundvls, yaroundvls, curve[icv,:])
    1098710996       
    1098810997    return curvloc, curveweights, Nwgt, ijaround3x3
     
    1101311022#Dycurve = (elatcurve-ilatcurve)/(Ncurve-1.)
    1101411023
    11015 #curve = np.zeros((2,Ncurve), dtype=np.float)
     11024#curve = np.zeros((Ncurve,2), dtype=np.float)
    1101611025#for icv in range(Ncurve):
    11017 #    curve[0,icv] = ilatcurve + Dycurve*icv
    11018 #    curve[1,icv] = iloncurve + Dxcurve*icv
     11026#    curve[icv,0] = ilatcurve + Dycurve*icv
     11027#    curve[icv,1] = iloncurve + Dxcurve*icv
    1101911028
    1102011029#print 'Curve origin:', ilatcurve, ',', iloncurve
     
    1102411033
    1102511034#loccurve, wgts, Nwgts, loc3x3 = curvelocalize_2D(curve,lons,lats)
    11026 #print loccurve
     11035#for ipt in range(Ncurve):
     11036#    print 'pos:', ipt, 'curve:', curve[ipt,:],'loccurve:', loccurve[ipt,:], 'wgts:', wgts[ipt,:,:], 'Nwgts:', Nwgts, 'loc3x3:', loc3x3[ipt,:]
    1102711037#quit()
Note: See TracChangeset for help on using the changeset viewer.