Changeset 1391 in lmdz_wrf for trunk/tools
- Timestamp:
- Dec 19, 2016, 4:09:08 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1390 r1391 55 55 # color_deg: Function to generate a degradation of colors in rgb base 56 56 # 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 57 60 # dictionary_key: Function to provide the first key in a dictionay with a given value 58 61 # dictionary_key_list: Function to provide the first key in a dictionary of lists with a given value … … 7592 7595 [None None None]] 7593 7596 """ 7597 import numpy.ma as ma 7594 7598 fname = 'vals_around' 7595 7599 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.) 7597 7603 7598 7604 dx = vals.shape[1] … … 10803 10809 [ 5.00000000e-04 9.99500000e-01] 10804 10810 """ 10805 fname = 'linear_we giths'10811 fname = 'linear_weights' 10806 10812 10807 10813 if vals[0] > vals[1]: … … 10839 10845 * Number of closest grid points (<=4) 10840 10846 * 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.) 10842 10848 xvals[:,0] = 1. 10843 10849 xvals[:,1] = 2. 10844 10850 xvals[:,2] = 3. 10845 xvals [2,2] = None10851 xvals.mask[2,2] = True 10846 10852 yvals = xvals.transpose() 10847 10853 >>> linearint_3x3weights(xvals, yvals, [1.5, 2.5]) … … 10858 10864 [ 1, 2, 1, 2, 0, 0, 1, 0, -1]])) 10859 10865 """ 10860 fname = 'linear_ wegiths'10866 fname = 'linear_3x3wegiths' 10861 10867 10862 10868 intval = np.array(intval0, dtype=np.float) 10863 print 'type xvals:', xvals.dtype10864 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)10868 10869 10869 10870 # 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 10874 10882 10875 10883 if not intval[1] >= nx and intval[1] <= xx: 10876 10884 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:' 10878 10887 for i in range(3): 10879 10888 print ' ', xvals[:,i] … … 10881 10890 if not intval[0] >= ny and intval[0] <= xy: 10882 10891 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:' 10884 10894 for i in range(3): 10885 10895 print ' ', yvals[:,i] 10886 10896 quit(-1) 10887 10897 10888 print 'Lluis intval:', intval10889 print ' ' + fname + ' Lluis xvalsma ____'10890 print xvalsma10891 10892 print ' ' + fname + ' Lluis yvalsma ____'10893 print yvalsma10894 10895 10898 dist = np.sqrt( (xvals-intval[1])**2 + (yvals-intval[0])**2) 10896 10899 inc = np.sum(dist) 10897 print 'inc:', inc, 'intval', intval10898 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] 10900 10903 10901 10904 ijvals = dist.copy() … … 10903 10906 10904 10907 # shortest minium distance 10905 sortdist = list(dist. flatten())10908 sortdist = list(dist.compressed()) 10906 10909 sortdist.sort() 10907 10910 iijj = 0 … … 10933 10936 10934 10937 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] 10935 10941 10936 10942 return wgt, Nvals, distpos 10937 10943 10938 10944 def curvelocalize_2D(curve,xvals,yvals): 10939 10945 """ Function to provide the localization a curve in a 2D field of positions via the equivalent i,j-quads … … 10956 10962 yvals= 2D matrix of y-coordinate values 10957 10963 RETURNS: 10958 curvloc= [ 2,Npts] matrix with the closest i,j grid point in the 2D space of the field positions10964 curvloc= [Npts,2] matrix with the closest i,j grid point in the 2D space of the field positions 10959 10965 curvweights= [Npts,3,3] 3x3 matrix weights around the curve position 10960 10966 Nwgt= Number of weights 10961 ijaorund3x3= [ 2,9] distance (in xval, yval units) to the curve position10967 ijaorund3x3= [Npts,2,9] distance (in xval, yval units) to the curve position 10962 10968 """ 10963 10969 fname = 'curvelocalize_2D' 10964 10970 10965 Npts = curve.shape[ 1]10971 Npts = curve.shape[0] 10966 10972 10967 10973 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) 10970 10976 10971 10977 for icv in range(Npts): 10972 10978 # 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) 10974 10980 mindist = np.min(dist) 10975 10981 ijloc = index_mat(dist,mindist) 10976 10982 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] 10979 10989 10980 10990 # Values around the nearest 2D-point to the curve point 10981 10991 xaroundvls = vals_around(xvals,ijloc) 10982 10992 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,:]) 10987 10996 10988 10997 return curvloc, curveweights, Nwgt, ijaround3x3 … … 11013 11022 #Dycurve = (elatcurve-ilatcurve)/(Ncurve-1.) 11014 11023 11015 #curve = np.zeros(( 2,Ncurve), dtype=np.float)11024 #curve = np.zeros((Ncurve,2), dtype=np.float) 11016 11025 #for icv in range(Ncurve): 11017 # curve[ 0,icv] = ilatcurve + Dycurve*icv11018 # curve[ 1,icv] = iloncurve + Dxcurve*icv11026 # curve[icv,0] = ilatcurve + Dycurve*icv 11027 # curve[icv,1] = iloncurve + Dxcurve*icv 11019 11028 11020 11029 #print 'Curve origin:', ilatcurve, ',', iloncurve … … 11024 11033 11025 11034 #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,:] 11027 11037 #quit()
Note: See TracChangeset
for help on using the changeset viewer.