Changeset 1399 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jan 9, 2017, 2:39:54 PM (8 years ago)
Author:
lfita
Message:

Adding `exctcurvloc', exact location on the i,j grid space of the curve in 'curvelocalize_2D'
Fixing right weights in 'linearint_3x3weights'
num_ordinal: Function to provide the ordinal of a given number, language, format and gender
removing error message on `reduce_last_spaces' when no space is found
adding some new signs in `ASCII_to'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1397 r1399  
    8989# multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string
    9090# 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
    9192# num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
    9293# oper_submatrix: Function to perform an operation of a given matrix along a sub-set of values along its dimensions
     
    67146715
    67156716    Lstring = len(string)
     6717    if Lstring < 1:
     6718        print errormsg
     6719        print '  ' + fname + ": String '" + string + "' too short !!"
     6720        quit(-1)
    67166721
    67176722    firstspace = -1
     
    67236728
    67246729    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
    67296731
    67306732    newstr = string[0:firstspace]
     
    95419543    langs = ['tex']
    95429544
     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
    95439547    texchars = {'\\': '\\textbackslash', \
    95449548      'á': "\\'a", 'é': "\\'e", 'í': "\\'i", 'ó': "\\'o", 'ú': "\\'u",               \
     9549      'ć': "\\'c", 'ǵ': "\\'g", 'áž±': "\\'k", 'ĺ': "\\'l", 'áž¿': "\\'m", 'ń': "\\'n",  \
     9550      'ṕ': "\\'p", 'ŕ': "\\'r", 'ś': "\\'s", 'ẃ': "\\'w", 'Ü': "\\'y", 'ź': "\\'z",  \
    95459551      'à': "\\`a", 'Ú': "\\`e", 'ì': "\\`i", 'ò': "\\`o", 'ù': "\\`u",               \
     9552      'ǹ': "\\`n", 'ẁ': "\\`w", 'ỳ': "\\`y",                                         \
    95469553      'â': "\\^a", 'ê': "\\^e", 'î': "\\^i", 'ÃŽ': "\\^o", 'û': "\\^u",               \
     9554      'ĉ': "\\^c", 'ĝ': "\\^g", 'Ä¥': "\\^h", 'ĵ': "\\^j", 'ŝ': "\\^s", 'ŵ': "\\^w",  \
     9555      'Å·': "\\^y", 'ẑ': "\\^z",                                                      \
    95479556      'À': '\\"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{}',                                     \
    95499572      'Á': "\\'A", 'É': "\\'E", 'Í': "\\'I", 'Ó': "\\'O", 'Ú': "\\'U",               \
     9573      'Ć': "\\'C", 'ÇŽ': "\\'G", 'áž°': "\\'K", 'Ĺ': "\\'L", 'ស': "\\'M", 'Ń': "\\'N",  \
     9574      'Ṕ': "\\'P", 'Ŕ': "\\'R", 'Ś': "\\'S", 'Ẃ': "\\'W", 'Ý': "\\'Y", 'Ź': "\\'Z",  \
    95509575      'À': "\\`A", 'È': "\\`E", 'Ì': "\\`I", 'Ò': "\\`O", 'Ù': "\\`U",               \
     9576      'Çž': "\\`N", 'Ẁ': "\\`W", 'Ỳ': "\\`Y",                                         \
    95519577      'Â': "\\^A", 'Ê': "\\^E", 'Î': "\\^I", 'Ô': "\\^O", 'Û': "\\^U",               \
     9578      'Ĉ': "\\^C", 'Ĝ': "\\^G", 'Ä€': "\\^H", 'ÄŽ': "\\^J", 'Ŝ': "\\^S", 'ÅŽ': "\\^W",  \
     9579      'Ŷ': "\\^Y", 'Ẑ': "\\^Z",                                                      \
    95529580      'Ä': '\\"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',                                        \
    95549597      '¡': '!`', '¿': '¿`', '%': '\\%', '#': '\\#', '&': '\\&', '$': '\\$',          \
    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$'}
    95589603
    95599604    if to == 'tex':
     
    1092710972    # Normalizing values (only the first 4)
    1092810973    if iijj >= 3:
    10929         inc = np.sum(sortdist[0:4])
     10974        inc = np.sum(1./np.array(sortdist[0:4]))
    1093010975        maxdist = sortdist[3]
    1093110976        Nvals = 4
    1093210977    else:
    10933         inc = np.sum(sortdist[0:iijj])
     10978        inc = np.sum(1./np.array(sortdist[0:iijj]))
    1093410979        maxdist = sortdist[iijj]
    1093510980        Nvals = iijj
    1093610981
    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)
    1093810984    if np.sum(wgt) > 1.000001:
     10985        print 'maxdist:', maxdist
    1093910986        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.)
    1094210992        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)
    1094311004
    1094411005#    print 'Lluis weights _______', sortdist[0:4],'max:',maxdist,'sum:',np.sum(wgt)
     
    1095411015             x,y_j,i       x,y_j,i+1
    1095511016
    10956                 curve(t)
    10957 
    10958                            curve(t+1)
     11017                curve(k)
     11018
     11019                           curve(k+1)
    1095911020
    1096011021             x,y_j-1,i     x,y_j-1,i+1
     
    1097211033        Nwgt= Number of weights
    1097311034        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
    1097411036    """
    1097511037    fname = 'curvelocalize_2D'
     
    1097911041    curvloc = np.ones((curve.shape), dtype=int)*(-1)
    1098011042    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)
    1098211044    ijaround3x3 = np.zeros((Npts,2,9), dtype=int)
     11045    exctcurvloc = np.ones((curve.shape), dtype=np.float)*(-1)
    1098311046
    1098411047    for icv in range(Npts):
     
    1099911062        yaroundvls = vals_around(yvals,ijloc)
    1100011063
    11001         curveweights[icv,:,:], Nwgts[icv], ijaround3x3[icv,:,:,] =                   \
     11064        curveweights[icv,:,:], Nwgts[icv], ijaround3x3[icv,:,:] =                    \
    1100211065          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#
    1100611081#lon1d = np.arange(10)
    1100711082#lat1d = np.arange(-5,5)
    1100811083#dx = lon1d.shape[0]
    1100911084#dy = lat1d.shape[0]
    11010 
     11085#
    1101111086#lons = np.zeros((dy,dx), dtype=np.float)
    1101211087#lats = np.zeros((dy,dx), dtype=np.float)
    11013 
     11088#
    1101411089#for iy in range(dy):
    1101511090#    lons[iy,:] = lon1d
    1101611091#for ix in range(dx):
    1101711092#    lats[:,ix] = lat1d
    11018 
     11093#
    1101911094#Ncurve = 12
    1102011095#iicurve = 0
     
    1102811103#Dxcurve = (eloncurve-iloncurve)/(Ncurve-1.)
    1102911104#Dycurve = (elatcurve-ilatcurve)/(Ncurve-1.)
    11030 
     11105#
    1103111106#curve = np.zeros((Ncurve,2), dtype=np.float)
    1103211107#for icv in range(Ncurve):
    1103311108#    curve[icv,0] = ilatcurve + Dycurve*icv
    1103411109#    curve[icv,1] = iloncurve + Dxcurve*icv
    11035 
     11110#
    1103611111#print 'Curve origin:', ilatcurve, ',', iloncurve
    1103711112#print 'Curve ending:', elatcurve, ',', eloncurve
    11038 
     11113#
    1103911114#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)
    1104211117#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
     11124def 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
    1104411249
    1104511250#quit()
Note: See TracChangeset for help on using the changeset viewer.