Changeset 1399 in lmdz_wrf for trunk/tools
- Timestamp:
- Jan 9, 2017, 2:39:54 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1397 r1399 89 89 # multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string 90 90 # num_chainSnum: Function to pass a value to a `ChainStrNum' number 91 # num_ordinal: Function to provide the ordinal of a given number, language, format and gender 91 92 # num_split: Function to split a string at each numeric value keeping the number as the last character of each cut 92 93 # oper_submatrix: Function to perform an operation of a given matrix along a sub-set of values along its dimensions … … 6714 6715 6715 6716 Lstring = len(string) 6717 if Lstring < 1: 6718 print errormsg 6719 print ' ' + fname + ": String '" + string + "' too short !!" 6720 quit(-1) 6716 6721 6717 6722 firstspace = -1 … … 6723 6728 6724 6729 if firstspace == -1: 6725 print errormsg 6726 print ' ' + fname + ": string '" + string + "' is not ended by spaces!!" 6727 print ' char. number of last character:',ord(string[Lstring:Lstring+1]) 6728 quit(-1) 6730 firstspace = Lstring + 1 6729 6731 6730 6732 newstr = string[0:firstspace] … … 9541 9543 langs = ['tex'] 9542 9544 9545 #From: http://tex.stackexchange.com/questions/14872/croatian-serbian-letters-in-cv-problem 9546 # From: https://www.dpg-tagung.de/dd14/infochars.html?language=en 9543 9547 texchars = {'\\': '\\textbackslash', \ 9544 9548 'á': "\\'a", 'é': "\\'e", 'Ã': "\\'i", 'ó': "\\'o", 'ú': "\\'u", \ 9549 'Ä': "\\'c", 'ǵ': "\\'g", 'áž±': "\\'k", 'ĺ': "\\'l", 'áž¿': "\\'m", 'Å': "\\'n", \ 9550 'á¹': "\\'p", 'Å': "\\'r", 'Å': "\\'s", 'áº': "\\'w", 'Ü': "\\'y", 'ź': "\\'z", \ 9545 9551 'à ': "\\`a", 'Ú': "\\`e", 'ì': "\\`i", 'ò': "\\`o", 'ù': "\\`u", \ 9552 'ǹ': "\\`n", 'áº': "\\`w", 'ỳ': "\\`y", \ 9546 9553 'â': "\\^a", 'ê': "\\^e", 'î': "\\^i", 'ÃŽ': "\\^o", 'û': "\\^u", \ 9554 'Ä': "\\^c", 'Ä': "\\^g", 'Ä¥': "\\^h", 'ĵ': "\\^j", 'Å': "\\^s", 'ŵ': "\\^w", \ 9555 'Å·': "\\^y", 'áº': "\\^z", \ 9547 9556 'À': '\\"a', 'ë': '\\"e', 'ï': '\\"i', 'ö': '\\"o', 'ÃŒ': '\\"u', \ 9548 'ç': '\c{c}', 'ñ': '\~{n}', \ 9557 'áž§': '\\"h', 'áº': '\\"t', 'Ạ9558 ': '\\"w', 'áº': '\\"x', 'ÿ': '\\"y', \ 9559 'Ä': '\\u{a}', 'Ä': '\\u{e}', 'Ä': '\\u{i}', 'Å': '\\u{o}', 'Å': '\\u{u}', \ 9560 'Ä': '\\u{g}', \ 9561 'Ç': '\\v{a}', 'Ä': '\\v{e}', 'Ç': '\\v{i}', 'Ç': '\\v{o}', 'Ç': '\\v{u}', \ 9562 'Ä': '\\v{c}', 'Ä': '\\v{d}', 'ǧ': '\\v{g}', 'È': '\\v{h}', 'ǰ': '\\v{j}', \ 9563 'Ç©': '\\v{k}', 'ÄŸ': '\\v{l}', 'Å': '\\v{n}', 'Å': '\\v{r}', 'Å¡': '\\v{s}', \ 9564 'Å¥': '\\v{t}', 'ÅŸ': '\\v{z}', \ 9565 'Ã¥': '\\a{}', 'ů': '\\r{u}', 'áº': '\\r{w}', 'áº': '\\r{y}', \ 9566 'ã': '\\~{a}', 'ẜ': '\\~{e}', 'Ä©': '\\~{i}', 'õ': '\\~{o}', 'Å©': '\\~{u}', \ 9567 'ñ': '\\~{n}', 'Ṝ': '\\~{v}', 'ỹ': '\\~{y}', \ 9568 'ç': '\\c{c}', 'áž': '\\c{d}', 'È©': '\\c{e}', 'Ä£': '\\c{g}', 'áž©': '\\c{h}', \ 9569 'Ä·': '\\c{k}', 'ÄŒ': '\\c{l}', 'Å': '\\c{n}', 'Å': '\\c{r}', 'Å': '\\c{s}', \ 9570 'Å£': '\\c{t}', \ 9571 'Å': '\\l{}', 'Þ': '\\o{}', 'Ä': '\\dj', 'Ã': '\\ss{}', \ 9549 9572 'Ã': "\\'A", 'Ã': "\\'E", 'Ã': "\\'I", 'Ã': "\\'O", 'Ã': "\\'U", \ 9573 'Ä': "\\'C", 'ÇŽ': "\\'G", 'áž°': "\\'K", 'Ĺ': "\\'L", 'ស': "\\'M", 'Å': "\\'N", \ 9574 'á¹': "\\'P", 'Å': "\\'R", 'Å': "\\'S", 'áº': "\\'W", 'Ã': "\\'Y", 'Ź': "\\'Z", \ 9550 9575 'Ã': "\\`A", 'Ã': "\\`E", 'Ã': "\\`I", 'Ã': "\\`O", 'Ã': "\\`U", \ 9576 'Çž': "\\`N", 'áº': "\\`W", 'Ỳ': "\\`Y", \ 9551 9577 'Ã': "\\^A", 'Ã': "\\^E", 'Ã': "\\^I", 'Ã': "\\^O", 'Ã': "\\^U", \ 9578 'Ä': "\\^C", 'Ä': "\\^G", 'Ä€': "\\^H", 'ÄŽ': "\\^J", 'Å': "\\^S", 'ÅŽ': "\\^W", \ 9579 'Ŷ': "\\^Y", 'áº': "\\^Z", \ 9552 9580 'Ã': '\\"A', 'Ã': '\\"E', 'Ã': '\\"I', 'Ã': '\\"O', 'Ã': '\\"U', \ 9553 'Ã': '\\c{C}', 'Ã': '\\~{N}', \ 9581 'ដ': '\\"H', 'áº': '\\"W', 'áº': '\\"X', 'Åž': '\\"Y', \ 9582 'Ä': '\\u{A}', 'Ä': '\\u{E}', 'Ĭ': '\\u{I}', 'Å': '\\u{O}', 'Ŭ': '\\u{U}', \ 9583 'Ä': '\\u{G}', 'Åœ': '\\u{Z}', \ 9584 'Ç': '\\v{A}', 'Ä': '\\v{E}', 'Ç': '\\v{I}', 'Ç': '\\v{O}', 'Ç': '\\v{U}', \ 9585 'Ä': '\\v{C}', 'Ä': '\\v{D}', 'ÇŠ': '\\v{G}', 'È': '\\v{H}', 'Çš': '\\v{K}', \ 9586 'Äœ': '\\v{L}', 'Å': '\\v{N}', 'Å': '\\v{R}', 'Å ': '\\v{S}', 'Å€': '\\v{T}', \ 9587 'Åœ': '\\v{Z}', \ 9588 'à 9589 ': '\\AA{}', 'Å®': '\\r{U}', \ 9590 'Ã': '\\~{A}', 'Ẍ': '\\~{E}', 'Äš': '\\~{I}', 'Ã': '\\~{O}', 'Åš': '\\~{U}', \ 9591 'Ã': '\\~{N}', 'Ṍ': '\\~{V}', 'Ở': '\\~{Y}', \ 9592 'Ã': '\\c{C}', 'áž': '\\c{D}', 'Èš': '\\c{E}', 'Ä¢': '\\c{G}', 'ážš': '\\c{H}', \ 9593 'Ķ': '\\c{K}', 'Ä»': '\\c{L}', 'Å 9594 ': '\\c{N}', 'Å': '\\c{R}', 'Å': '\\c{S}', \ 9595 'Å¢': '\\c{T}', \ 9596 'Å': '\\L{}', 'Ã': '\O{}', 'Ä': '\\DJ', \ 9554 9597 '¡': '!`', '¿': '¿`', '%': '\\%', '#': '\\#', '&': '\\&', '$': '\\$', \ 9555 '_': '\\_', '·': '\\textperiodcentered', '<': '$<$', '>': '$>$', \ 9556 'ï': '*', 'ª': '$^{a}$', 'º': '$^{o}$', '°': '$^{\\circ}$', '\n': '\\\\\n', \ 9557 '\t': '\\medskip', 'â': '``', 'â': '\'\'', '^': '\^'} 9598 '_': '\\_', 'â': '--', '·': '\\textperiodcentered', '<': '$<$', '>': '$>$', \ 9599 'ï': '*', 'ª': '$^{a}$', 'º': '$^{o}$', '°': '$^{\\circ}$', '²': '$^{2}$', \ 9600 '\n': '\\\\\n', '\t': '\\medskip', \ 9601 'â': '``', 'â': '\'\'', '^': '\^', 'â': "'", \ 9602 'â¥': '$\geq$', '~': '$\sim$'} 9558 9603 9559 9604 if to == 'tex': … … 10927 10972 # Normalizing values (only the first 4) 10928 10973 if iijj >= 3: 10929 inc = np.sum( sortdist[0:4])10974 inc = np.sum(1./np.array(sortdist[0:4])) 10930 10975 maxdist = sortdist[3] 10931 10976 Nvals = 4 10932 10977 else: 10933 inc = np.sum( sortdist[0:iijj])10978 inc = np.sum(1./np.array(sortdist[0:iijj])) 10934 10979 maxdist = sortdist[iijj] 10935 10980 Nvals = iijj 10936 10981 10937 wgt = np.where(dist <= maxdist, dist / inc, 0.) 10982 wgt = np.where(dist <= maxdist, 1./dist/inc, 0.) 10983 # Case with more than 4 points with less distance than `maxdist' (point at the diagonal) 10938 10984 if np.sum(wgt) > 1.000001: 10985 print 'maxdist:', maxdist 10939 10986 wgt = np.where(dist <= maxdist, dist, 0.) 10940 inc = np.sum(wgt) 10941 wgt = np.where(wgt > 0., wgt/inc, 0.) 10987 inc = 0. 10988 wgtflat = wgt.flatten() 10989 for i in range(9): 10990 if wgtflat[i] > 0.: inc = inc + 1./wgtflat[i] 10991 wgt = np.where(wgt > 0., 1./wgt/inc, 0.) 10942 10992 Nvals = np.sum(wgt > 0.) 10993 10994 if np.all(wgt == 0.): 10995 print errormsg 10996 print ' ' + fname + ': no point found !!' 10997 print ' point to interpolate:', intval0, ' around x & y & dist _____' 10998 for i in range(3): 10999 print xvals[:,i], '&', yvals[:,i], '&', dist[:,i] 11000 print ' sortdist:', sortdist, 'weights _____' 11001 for i in range(3): 11002 print wgt[:,i] 11003 quit(-1) 10943 11004 10944 11005 # print 'Lluis weights _______', sortdist[0:4],'max:',maxdist,'sum:',np.sum(wgt) … … 10954 11015 x,y_j,i x,y_j,i+1 10955 11016 10956 curve( t)10957 10958 curve( t+1)11017 curve(k) 11018 11019 curve(k+1) 10959 11020 10960 11021 x,y_j-1,i x,y_j-1,i+1 … … 10972 11033 Nwgt= Number of weights 10973 11034 ijaorund3x3= [Npts,2,9] distance (in xval, yval units) to the curve position 11035 exctcurvloc= [Npts,2] matrix with the exact i,j grid point in the 2D space of the field positions 10974 11036 """ 10975 11037 fname = 'curvelocalize_2D' … … 10979 11041 curvloc = np.ones((curve.shape), dtype=int)*(-1) 10980 11042 Nwgts = np.zeros((Npts), dtype=int) 10981 curveweights = np.zeros((Npts,3,3), dtype= float)11043 curveweights = np.zeros((Npts,3,3), dtype=np.float) 10982 11044 ijaround3x3 = np.zeros((Npts,2,9), dtype=int) 11045 exctcurvloc = np.ones((curve.shape), dtype=np.float)*(-1) 10983 11046 10984 11047 for icv in range(Npts): … … 10999 11062 yaroundvls = vals_around(yvals,ijloc) 11000 11063 11001 curveweights[icv,:,:], Nwgts[icv], ijaround3x3[icv,:,: ,] =\11064 curveweights[icv,:,:], Nwgts[icv], ijaround3x3[icv,:,:] = \ 11002 11065 linearint_3x3weights(xaroundvls, yaroundvls, curve[icv,:]) 11003 11004 return curvloc, curveweights, Nwgts, ijaround3x3 11005 11066 exctcurvloc[icv,:] = curvloc[icv,:] 11067 11068 for ip in range(Nwgts[icv]): 11069 ipy = ijaround3x3[icv,0,ip] 11070 ipx = ijaround3x3[icv,1,ip] 11071 exctcurvloc[icv,0] = np.float(exctcurvloc[icv,0]) + curveweights[icv,ipy,ipx]*(ipy-1.) 11072 exctcurvloc[icv,1] = np.float(exctcurvloc[icv,1]) + curveweights[icv,ipy,ipx]*(ipx-1.) 11073 print ' ' + fname + 'ip:', ip, 'ipy:', ipy-1., 'wgt:', curveweights[icv,ipy,ipx], \ 11074 'exctcurv:', exctcurvloc[icv,0] 11075 print ' ' + fname + ' icv:', icv, 'curve:', curve[icv,:], 'ijloc:', ijloc, \ 11076 'cwgt:', curveweights[icv,:,:], 'exctloc:', exctcurvloc[icv,:] 11077 # quit() 11078 11079 return curvloc, curveweights, Nwgts, ijaround3x3, exctcurvloc 11080 # 11006 11081 #lon1d = np.arange(10) 11007 11082 #lat1d = np.arange(-5,5) 11008 11083 #dx = lon1d.shape[0] 11009 11084 #dy = lat1d.shape[0] 11010 11085 # 11011 11086 #lons = np.zeros((dy,dx), dtype=np.float) 11012 11087 #lats = np.zeros((dy,dx), dtype=np.float) 11013 11088 # 11014 11089 #for iy in range(dy): 11015 11090 # lons[iy,:] = lon1d 11016 11091 #for ix in range(dx): 11017 11092 # lats[:,ix] = lat1d 11018 11093 # 11019 11094 #Ncurve = 12 11020 11095 #iicurve = 0 … … 11028 11103 #Dxcurve = (eloncurve-iloncurve)/(Ncurve-1.) 11029 11104 #Dycurve = (elatcurve-ilatcurve)/(Ncurve-1.) 11030 11105 # 11031 11106 #curve = np.zeros((Ncurve,2), dtype=np.float) 11032 11107 #for icv in range(Ncurve): 11033 11108 # curve[icv,0] = ilatcurve + Dycurve*icv 11034 11109 # curve[icv,1] = iloncurve + Dxcurve*icv 11035 11110 # 11036 11111 #print 'Curve origin:', ilatcurve, ',', iloncurve 11037 11112 #print 'Curve ending:', elatcurve, ',', eloncurve 11038 11113 # 11039 11114 #print curve 11040 11041 #loccurve, wgts, Nwgts, loc3x3 = curvelocalize_2D(curve,lons,lats)11115 # 11116 #loccurve, wgts, Nwgts, loc3x3, exctloccurve = curvelocalize_2D(curve,lons,lats) 11042 11117 #for ipt in range(Ncurve): 11043 # print 'pos:', ipt, 'curve:', curve[ipt,:],'loccurve:', loccurve[ipt,:], 'wgts:', wgts[ipt,:,:], 'Nwgts:', Nwgts, 'loc3x3:', loc3x3[ipt,:] 11118 # print 'pos:', ipt, 'curve:', curve[ipt,:],'loccurve:', loccurve[ipt,:], 'wgts:', wgts[ipt,:,:], 'Nwgts:', Nwgts[ipt], 'loc3x3:', loc3x3[ipt,:], 'exctloccurve:', exctloccurve[ipt,:] 11119 # for iw in range(Nwgts[ipt]): 11120 # iwy = loc3x3[ipt,0,iw] 11121 # iwx = loc3x3[ipt,1,iw] 11122 # print ' sort wgts:', iw,' iwy,iwx:', iwy,',',iwx, 'wgt:', wgts[ipt,iwy,iwx] 11123 11124 def num_ordinal(num, lang='eng', kind='numOr', fmt='ascii', gender='masc'): 11125 """ Function to provide the ordinal of a given number, language, format and gender 11126 num= number to provide the ordinal 11127 lang= language of the ordinal 11128 'eng': English 11129 kind= kind of the ordinal 11130 'numOr': as num[Ord] 11131 'Ordinal': long ordinal (not fully ready) 11132 fmt= format of the ordinal 11133 'ascii': standard ASCII 11134 'utf': utf-8 (with accents) 11135 gender= gender of the ordinal 11136 'fem': femenin 11137 'masc': masculin 11138 >>> num_ordinal(2, 'eng', 'numOr', 'ascii') 11139 '2nd' 11140 >>> num_ordinal(2, 'eng', 'Ordinal', 'ascii') 11141 'second' 11142 >>> num_ordinal(7, 'cat', 'Ordinal', 'utf') 11143 'setÚ' 11144 >>> num_ordinal(1, 'cat', 'Ordinal', 'utf', fem) 11145 'primera' 11146 """ 11147 fname = 'num_ordinal' 11148 11149 languages = ['cat', 'eng'] 11150 kinds = ['numOr', 'Ordinal'] 11151 formats = ['ascii', 'utf'] 11152 genders = ['fem', 'masc'] 11153 11154 if not searchInlist(languages, lang): 11155 print errormsg 11156 print ' ' + fname + ": language '" + lang + "' not ready !!" 11157 print ' available ones:', languages 11158 quit(-1) 11159 11160 if not searchInlist(kinds, kind): 11161 print errormsg 11162 print ' ' + fname + ": kind '" + kind + "' not ready !!" 11163 print ' available ones:', kinds 11164 quit(-1) 11165 11166 if not searchInlist(formats, fmt): 11167 print errormsg 11168 print ' ' + fname + ": format '" + fmt + "' not ready !!" 11169 print ' available ones:', fomrats 11170 quit(-1) 11171 11172 if not searchInlist(genders, gender): 11173 print errormsg 11174 print ' ' + fname + ": gender '" + gender + "' not ready !!" 11175 print ' available ones:', genders 11176 quit(-1) 11177 11178 # Catalan 11179 # short ordinals ([fem, masc]) 11180 catord = {1: ['era', 'er'], 2: ['ona', 'on'], 3: ['era', 'er'], 4: ['rta', 'rt']} 11181 # Last special ordinal 11182 catlastord = 4 11183 # generic ordinal ([fem, masc]) 11184 catgenord = ['ena', 'Ú'] 11185 # long ordinals 11186 catLord = {1: ['primera', 'primer'], 2: ['segona', 'segon'], 3: ['tercera', \ 11187 'tercer'], 4: ['quarta', 'quart'], 5: ['cinquena', 'cinquÚ'], 6: ['sisena', \ 11188 'sisÚ'], 7: ['setena', 'setÚ'], 8: ['vuitena', 'vuitÚ'], 9: ['novena', 'novÚ']} 11189 11190 # English 11191 # short ordinals ([fem, masc]) 11192 engord = {1: ['st', 'st'], 2: ['nd', 'nd'], 3: ['rd', 'rd']} 11193 # Last special ordinal 11194 englastord = 3 11195 # generic ordinal ([fem, masc]) 11196 enggenord = ['th','th'] 11197 # long ordinals 11198 engLord = {1: ['first', 'first'], 2: ['second', 'second'], 3: ['third', 'third'],\ 11199 4: ['fourth', 'fourth'], 5: ['fifth', 'fifth'], 6: ['sixth','sixth'], \ 11200 7: ['seventh', 'seventh'], 8: ['eight', 'eight'], 9: ['nineth', 'nineth']} 11201 11202 if lang == 'cat': 11203 ords = catord 11204 lastord = catlastord 11205 genord = catgenord 11206 Lords = catLord 11207 elif lang == 'eng': 11208 ords = engord 11209 lastord = englastord 11210 genord = enggenord 11211 Lords = engLord 11212 11213 Snum = str(num) 11214 if num > 10: 11215 LSnum = len(Snum) 11216 Senu = Snum[LSnum:LSnum+1] 11217 else: 11218 Senu = Snum 11219 11220 # Ordinal 11221 if num < lastord: 11222 Ord = ords[num] 11223 else: 11224 Ord = genord 11225 if gender == 'fem': Sord = Ord[0] 11226 else: Sord = Ord[1] 11227 11228 # Long ordinal 11229 if num < 10: 11230 LOrd = Lords[num] 11231 else: 11232 print warnmsg 11233 print ' ' + fname + ' long ordinal not ready for:', num, '!!' 11234 LOrd = 'None' 11235 if gender == 'fem': SLord = LOrd[0] 11236 else: SLord = LOrd[1] 11237 11238 if kind == 'numOr': 11239 stringv = Sord 11240 elif kind == 'Ordinal': 11241 stringv = SLord 11242 else: 11243 print errormsg 11244 print ' ' + fname + ": kind '" + kind + "' not ready !!" 11245 print ' available ones:', kinds 11246 quit(-1) 11247 11248 return stringv 11044 11249 11045 11250 #quit()
Note: See TracChangeset
for help on using the changeset viewer.