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


Ignore:
Timestamp:
Sep 30, 2016, 6:36:03 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `ASCII_LaTeX': Function to transform from an ASCII character to LaTeX codification
  • `latex_text': Function to transform a text to LaTeX following style rules
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1106 r1125  
     1# -*- coding: iso-8859-15 -*-
    12import numpy as np
    23from netCDF4 import Dataset as NetCDFFile
     
    2627
    2728####### Content
     29# ASCII_LaTeX: Function to transform from an ASCII character to LaTeX codification
    2830# Capturing: Class to capture the standard output from a function
    2931# CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
     
    5658# index_vec: Function to provide the coordinates of a given value inside a vector
    5759# julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
     60# latex_text: Function to transform a text to LaTeX following style rules
    5861# list_combos: Function to construct a new list with all possible N-combinations of the list-values
    5962# list_coincidences: Function to provide the coincidences between two lists
     
    92949297    return newmat
    92959298
     9299def ASCII_LaTeX(ln):
     9300    """ Function to transform from an ASCII character to LaTeX codification
     9301    >>> ASCII_LaTeX('Laboratoire de Météorologie Dynamique però Hovmöller \¿')
     9302    Laboratoire de M\'et\'eorologie Dynamique per\`o Hovm\"oller \textbackslash¿`
     9303    """
     9304
     9305    newln = ln.replace('\\', '\\textbackslash')
     9306
     9307    newln = newln.replace('á', "\\'a")
     9308    newln = newln.replace('é', "\\'e")
     9309    newln = newln.replace('í', "\\'i")
     9310    newln = newln.replace('ó', "\\'o")
     9311    newln = newln.replace('ú', "\\'u")
     9312
     9313    newln = newln.replace('à', "\\`a")
     9314    newln = newln.replace('Ú', "\\`e")
     9315    newln = newln.replace('ì', "\\`i")
     9316    newln = newln.replace('ò', "\\`o")
     9317    newln = newln.replace('ù', "\\`u")
     9318
     9319    newln = newln.replace('â', "\\^a")
     9320    newln = newln.replace('ê', "\\^e")
     9321    newln = newln.replace('î', "\\^i")
     9322    newln = newln.replace('ÃŽ', "\\^o")
     9323    newln = newln.replace('û', "\\^u")
     9324
     9325    newln = newln.replace('À', '\\"a')
     9326    newln = newln.replace('ë', '\\"e')
     9327    newln = newln.replace('ï', '\\"i')
     9328    newln = newln.replace('ö', '\\"o')
     9329    newln = newln.replace('ÃŒ', '\\"u')
     9330
     9331    newln = newln.replace('ç', '\c{c}')
     9332    newln = newln.replace('ñ', '\~{n}')
     9333
     9334    newln = newln.replace('Á', "\\'A")
     9335    newln = newln.replace('É', "\\'E")
     9336    newln = newln.replace('Í', "\\'I")
     9337    newln = newln.replace('Ó', "\\'O")
     9338    newln = newln.replace('Ú', "\\'U")
     9339
     9340    newln = newln.replace('À', "\\`A")
     9341    newln = newln.replace('È', "\\`E")
     9342    newln = newln.replace('Ì', "\\`I")
     9343    newln = newln.replace('Ò', "\\`O")
     9344    newln = newln.replace('Ù', "\\`U")
     9345
     9346    newln = newln.replace('Â', "\\^A")
     9347    newln = newln.replace('Ê', "\\^E")
     9348    newln = newln.replace('Î', "\\^I")
     9349    newln = newln.replace('Ô', "\\^O")
     9350    newln = newln.replace('Û', "\\^U")
     9351
     9352    newln = newln.replace('Ä', '\\"A')
     9353    newln = newln.replace('Ë', '\\"E')
     9354    newln = newln.replace('Ï', '\\"I')
     9355    newln = newln.replace('Ö', '\\"O')
     9356    newln = newln.replace('Ü', '\\"U')
     9357
     9358    newln = newln.replace('Ç', '\\c{C}')
     9359    newln = newln.replace('Ñ', '\\~{N}')
     9360
     9361    newln = newln.replace('¡', '!`')
     9362    newln = newln.replace('¿', '¿`')
     9363    newln = newln.replace('%', '\\%')
     9364    newln = newln.replace('#', '\\#')
     9365    newln = newln.replace('&', '\\&')
     9366    newln = newln.replace('$', '\\$')
     9367    newln = newln.replace('_', '\\_')
     9368    newln = newln.replace('·', '\\textperiodcentered')
     9369    newln = newln.replace('<', '$<$')
     9370    newln = newln.replace('>', '$>$')
     9371    newln = newln.replace('', '*')
     9372#    newln = newln.replace('º', '$^{\\circ}$')
     9373    newln = newln.replace('ª', '$^{a}$')
     9374    newln = newln.replace('º', '$^{o}$')
     9375    newln = newln.replace('°', '$^{\\circ}$')
     9376    newln = newln.replace('\n', '\\\\\n')
     9377    newln = newln.replace('\t', '\\medskip')
     9378    newln = newln.replace('“', '``')
     9379    newln = newln.replace('”', '\'\'')
     9380
     9381    return newln
     9382
     9383def latex_text(itext):
     9384    """ Function to transform a text to LaTeX following style rules
     9385      itext: original text
     9386      >>> latex_text('US use the dolar $ as in AUS')
     9387      US use the dolar \$ as in AUS
     9388      >>> latex_text('definition of sinus: $\sin=\\frac{a}{\\sqrt{a^2+b^2}}$')
     9389      definition of sinus: $\sin=\frac{a}{\sqrt{a^2+b^2}}$
     9390      >>> latex_text('WRF_LMDZ$_{current}^{AR40}$')
     9391      WRF\_LMDZ$_{current}^{AR40}$
     9392    """
     9393    function = 'latex_text'
     9394
     9395    # Sesitive characters which might interfer with the mathematical environment
     9396    symbs = {'sub': '_', 'sup':'^', 'slash':'\\'}
     9397
     9398    # Do not change that sensitive_math which are between $ (from mathematical notation)
     9399    latexsymb = {}
     9400
     9401    Litext = len(itext)
     9402    for ic in range(Litext):
     9403        if itext[ic] == '$':
     9404            if latexsymb.has_key('math'):
     9405                vals = latexsymb['math']
     9406                vals.append(ic)
     9407            else:
     9408                vals = [ic]
     9409            latexsymb['math'] = vals
     9410        elif itext[ic] == '_':
     9411            if latexsymb.has_key('sub'):
     9412                vals = latexsymb['sub']
     9413                vals.append(ic)
     9414            else:
     9415                vals = [ic]
     9416            latexsymb['sub'] = vals
     9417        elif itext[ic] == '^':
     9418            if latexsymb.has_key('sup'):
     9419                vals = latexsymb['sup']
     9420                vals.append(ic)
     9421            else:
     9422                vals = [ic]
     9423            latexsymb['sup'] = vals
     9424        elif itext[ic] == '\\':
     9425            if latexsymb.has_key('slash'):
     9426                vals = latexsymb['slash']
     9427                vals.append(ic)
     9428            else:
     9429                vals = [ic]
     9430            latexsymb['slash'] = vals
     9431
     9432    latextext = itext + ''
     9433    for car in symbs.keys():
     9434        changecar = ASCII_LaTeX(symbs[car])
     9435        if latexsymb.has_key(car):
     9436            if not latexsymb.has_key('math') or len(latexsymb['math']) == 1:
     9437                latextext = latextext.replace(car,changecar)
     9438            else:
     9439                # Changing that characters which are outside an even 'math' pair
     9440                mathpos = latexsymb['math']
     9441                Nmath = len(mathpos)
     9442                if np.mod(Nmath,2) != 0 and Nmath > 1:
     9443                    print errormsg
     9444                    print '  '+fname + ': even number', Nmath,' of math symbols $ !!'
     9445                    print '    impossible to determine where starts math environment '
     9446                    print "    change text: '" + itext + "'"
     9447                    quit(-1)
     9448
     9449                for isymb in latexsymb[car]:
     9450                    Ltxt = len(latextext)
     9451                    if Nmath == 2:
     9452                        # Case with only 2 $
     9453                        mathbeg = mathpos[0]
     9454                        mathend = mathpos[1]
     9455                        if isymb < mathbeg or isymb > mathend:
     9456                            headtxt = latextext[0:isymb]
     9457                            tailtxt = latextext[isymb+1:Ltxt+1]
     9458                            latextext = headtxt + changecar + tailtxt
     9459                    else:
     9460                        # Case with more than 2 $
     9461                        for imath in range(0,Nmath,2):
     9462                            mathbeg = mathpos[imath]
     9463                            mathend = mathpos[imath+1]
     9464                            mathnext = mathpos[imath+2]   
     9465                            if isymb < mathbeg and (isymb > mathend and isymb < mathnext):
     9466                                headtxt = latextext[0:isymb]
     9467                                tailtxt = latextext[isymb+1:Ltxt+1]
     9468                                latextext = headtxt + changecar + tailtxt
     9469                                break
     9470    # Only 1 mathematical sign
     9471    Ltxt = len(latextext)
     9472    if len(latexsymb['math']) == 1:
     9473        isymb = latexsymb['math'][0]
     9474        headtxt = latextext[0:isymb]
     9475        tailtxt = latextext[isymb+1:Ltxt+1]
     9476        latextext = headtxt + '\\$' + tailtxt
     9477
     9478    return latextext
     9479
    92969480#quit()
    92979481
Note: See TracChangeset for help on using the changeset viewer.