Changeset 1365 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Dec 4, 2016, 4:36:57 PM (8 years ago)
Author:
lfita
Message:

Adding picky character chains checks within mathematical environments in `ASCII_to'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1359 r1365  
    7878#   are repeated they are not included
    7979# 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
    8081# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    8182# num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
     
    23172318    return ivalpos
    23182319
     2320def 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
    23192352def addfileInfile(origfile,destfile,addfile,addsign):
    23202353    """ Function to add the content of a file [addfile] to another one [origfile] at
     
    94899522    >>> ASCII_to('à$353\pol', 'tex', ['$'])
    94909523    \`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.}\ (\%)$
    94919527    """
    94929528    fname = 'ASCII_to'
    94939529    langs = ['tex']
    9494 
    94959530
    94969531    texchars = {'\\': '\\textbackslash', \
     
    95399574        newtext = newtext.replace(char,chars[char])
    95409575
     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
    95419610    return newtext
     9611
     9612#NOchange = ['_', '^', '\\', '&', '$']
    95429613
    95439614def ASCII_LaTeX(ln):
     
    96139684    newln = newln.replace('_', '\\_')
    96149685    newln = newln.replace('·', '\\textperiodcentered')
    9615     newln = newln.replace('<', '$<$')
    9616     newln = newln.replace('>', '$>$')
     9686    newln = newln.replace('<', '\\tetxless')
     9687    newln = newln.replace('>', '\\textgreater')
    96179688    newln = newln.replace('', '*')
    96189689#    newln = newln.replace('º', '$^{\\circ}$')
Note: See TracChangeset for help on using the changeset viewer.