Changeset 1365 in lmdz_wrf for trunk/tools/generic_tools.py
- Timestamp:
- Dec 4, 2016, 4:36:57 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1359 r1365 78 78 # are repeated they are not included 79 79 # multi_index_mat: Function to provide the multiple coordinates of a given value inside a matrix 80 # multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string 80 81 # num_chainSnum: Function to pass a value to a `ChainStrNum' number 81 82 # num_split: Function to split a string at each numeric value keeping the number as the last character of each cut … … 2317 2318 return ivalpos 2318 2319 2320 def multi_index_string(String, char): 2321 """ Function to provide the indeces of every repetition of a group of characters within a string 2322 String= string to look in 2323 char= chain of character to look for 2324 >>> multi_index_string('12345654321','2') 2325 [1, 9] 2326 >>> multi_index_string('12345654321','7') 2327 [] 2328 >>> multi_index_string('1234565432123456','23') 2329 [1, 11] 2330 """ 2331 fname = 'multi_index_string' 2332 2333 Lstring = len(String) 2334 Lchar = len(char) 2335 2336 newstring = String + '' 2337 indices = [] 2338 2339 iss = 0 2340 while iss < Lstring: 2341 if newstring.find(char) != -1: 2342 ichar = newstring.find(char) 2343 indices.append(ichar+iss) 2344 print newstring, ':', ichar 2345 newstring = newstring[ichar+iss+Lchar:-1] 2346 iss = iss + ichar + Lchar 2347 else: 2348 iss = Lstring 2349 2350 return indices 2351 2319 2352 def addfileInfile(origfile,destfile,addfile,addsign): 2320 2353 """ Function to add the content of a file [addfile] to another one [origfile] at … … 9489 9522 >>> ASCII_to('à $353\pol', 'tex', ['$']) 9490 9523 \`a$353\textbackslashpol 9524 >>> NOchange = ['_', '^', '\\', '&', '$'] 9525 >>> ASCII_to('$[spatial\ cloud\ cover]^{sub-section}_{cld\ >\ 80000.}\ (%)$', 'tex', NOchange) 9526 $[spatial\ cloud\ cover]^{sub-section}_{cld\ >\ 80000.}\ (\%)$ 9491 9527 """ 9492 9528 fname = 'ASCII_to' 9493 9529 langs = ['tex'] 9494 9495 9530 9496 9531 texchars = {'\\': '\\textbackslash', \ … … 9539 9574 newtext = newtext.replace(char,chars[char]) 9540 9575 9576 # There are some issues one inside a math environment 9577 # $ (...) $<$ (...)$: will not work! need to be $ (...) < (...)$ 9578 CHANGEinsideMATH = ['$<$', '$>$', '$^{a}$', '$^{o}$', '$^{\circ}$'] 9579 Nchgmath = len(CHANGEinsideMATH) 9580 9581 Lnewtext = len(newtext) 9582 mathpos = [] 9583 for ic in range(Lnewtext): 9584 if newtext[ic] == '$': 9585 mathpos.append(ic) 9586 9587 print 'Hola?', mathpos 9588 # Dictionary with locations within line of the dangerous strings... 9589 mathchg = {} 9590 for icS in CHANGEinsideMATH: 9591 indices = multi_index_string(newtext,icS) 9592 if len(indices) > 0: 9593 mathchg[icS] = indices 9594 9595 Lmathpos = len(mathpos) 9596 # All this only has sens if at least there are 4 $! 9597 if Lmathpos >= 4: 9598 for imath in range(Lmathpos,0,-2): 9599 for chars in mathchg.keys(): 9600 positions = mathchg[chars] 9601 Lchars = len(chars) 9602 for ipos in positions: 9603 if mathpos[imath-4] < ipos and mathpos[imath-2] == ipos + Lchars - 1: 9604 print ' ' + fname + ": removing '" + chars + "'" 9605 Lnewstring = len(newtext) 9606 newstring = newtext[0:ipos] + chars.replace('$','') + \ 9607 newtext[ipos+Lchars:Lnewstring+1] 9608 newtext = newstring 9609 9541 9610 return newtext 9611 9612 #NOchange = ['_', '^', '\\', '&', '$'] 9542 9613 9543 9614 def ASCII_LaTeX(ln): … … 9613 9684 newln = newln.replace('_', '\\_') 9614 9685 newln = newln.replace('·', '\\textperiodcentered') 9615 newln = newln.replace('<', ' $<$')9616 newln = newln.replace('>', ' $>$')9686 newln = newln.replace('<', '\\tetxless') 9687 newln = newln.replace('>', '\\textgreater') 9617 9688 newln = newln.replace('ï', '*') 9618 9689 # newln = newln.replace('º', '$^{\\circ}$')
Note: See TracChangeset
for help on using the changeset viewer.