Changeset 1897 in lmdz_wrf


Ignore:
Timestamp:
Apr 7, 2018, 2:20:33 PM (7 years ago)
Author:
lfita
Message:

Converting 'WRFsetup' to a multiple namelist table output function

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic.py

    r1896 r1897  
    3939# variables_values: Function to provide values to plot the different variables values from ASCII file
    4040# wdismean: Function to compute the mean value weighted to its 4 distances
    41 # WRFsetup: Function to check the set-up of a WRF model namelist or from any other model
     41# WRFsetup: Function to check the set-up of a series of model namelist
    4242
    4343# Character to split passed values
     
    8282## e.g. # generic.py -o variables_values -S 'hus'
    8383## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3.
    84 ## e.g. # python /home/lluis/PyNCplot/generic.py -o WRFsetup -S ../UBA/namelist.input,basic,textabrow
     84## e.g. # generic.py -o WRFsetup -S '/home/lluis/estudios/RELAMPAGO/SimCoor/UBA/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/SMN/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/NOA-IERSD/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/UBAmili/namelist.input,basic,textabrow'
    8585
    8686operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
     
    417417            print gen.WRFsetup.__doc__
    418418            quit(-1)
    419         vals0 = vals[0]
     419        vals0 = vals[0].replace('@', ',')
    420420        vals1 = vals[1]
    421421        vals2 = vals[2]
  • trunk/tools/generic_tools.py

    r1896 r1897  
    114114# multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string
    115115# Nomasked: Function to bring back a given array wthout the masked values reducing a given dimension
     116# Ntchar: Function to repeat a number of times a given tring
    116117# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    117118# num_ordinal: Function to provide the ordinal of a given number, language, format and gender
     
    166167        return True
    167168    return False
     169
     170def Ntchar(char,N):
     171    """ Function to repeat a number of times a given tring
     172      char= string to repeat
     173      N= quantity of repetitions
     174    >>> Ntchar('c', 4)
     175    cccc
     176    """
     177    fname = 'Ntchar'
     178
     179    newstr = ''
     180    for it in range(N): newstr = newstr + char
     181
     182    return newstr
    168183
    169184def values_line(line, splitv, chars):
     
    1281812833#print CFtime_freq(521438., 521439., 'hours since 1950-01-01_00:00:00')
    1281912834
    12820 def WRFsetup(namelistn, params='basic', output='twopoints@'):
    12821     """ Function to check the set-up of a WRF model namelist or from any other model
    12822       namelistn= file name of the 'namelist.input'
     12835def WRFsetup(namelistns, params='WRFbasic', output='twopoints@'):
     12836    """ Function to check the set-up of a series of model namelist
     12837      namelistns= ',' separated list of namelist files
    1282312838      params= parameters to check
    12824         'basic': a pre-determined list of basic parameters (default)
     12839        'WRFbasic': a pre-determined WRF list of basic parameters (default)
    1282512840        'specific',[listparams]: a given ':' separated list of parameters to check
    1282612841      output= kind of format of the output
    12827         'textabcol': a LaTeX-table column formated
    12828         'textabrow': a LaTeX-table row formated
    12829         'twopoints@': as ':' separated list of values [attrn]@[attrv]:... (default)
     12842        'textabcol': a LaTeX-table column formated for each model
     12843        'textabrow': a LaTeX-table row formated for each model
     12844        'twopoints@': as ':' separated list of values [attrn]@[attrv]:... (default) for each
     12845          model with ';' for the following model
    1283012846      WRFsetup('namelist.input')
    1283112847    """
     
    1286812884        quit(-1)
    1286912885
    12870     onf = open(namelistn, 'r')
    12871     fparams = {}
    12872     pvals = ''
    12873     for line in onf:
    12874         if line[0:1] != '#' and len(line) > 1 and line.find('&') == -1 and           \
    12875           line.find('/') == -1:
    12876             if line.find('=') != -1:
    12877                 if len(pvals) != 0:
    12878                     fparams[paramn] = pvals
    12879                 ilvals = line.replace('\n', '').replace('\t','').split('=')
    12880                 paramn = ilvals[0].replace(' ','')
    12881                 pvals = ilvals[1].replace(' ','')
     12886    # namelist files
     12887    nmlfs = str_list(namelistns, ',')
     12888    Nfiles = len(nmlfs)
     12889
     12890    # Number of total parameters to look for
     12891    Npars = 0
     12892    for kn in lookparams.keys():
     12893        Npars = Npars + len(lookparams[kn])
     12894
     12895    allfparams = {}
     12896    for namelistn in nmlfs:
     12897        onf = open(namelistn, 'r')
     12898        fparams = {}
     12899        pvals = ''
     12900        for line in onf:
     12901            if line[0:1] != '#' and len(line) > 1 and line.find('&') == -1 and       \
     12902              line.find('/') == -1:
     12903                if line.find('=') != -1:
     12904                    if len(pvals) != 0:
     12905                        fparams[paramn] = pvals
     12906                    ilvals = line.replace('\n', '').replace('\r', '').replace('\t','').split('=')
     12907                    paramn = ilvals[0].replace(' ','')
     12908                    pvals = ilvals[1].replace(' ','')
     12909                else:
     12910                    ilvals = line.replace('\n', '').replace('\r', '').replace('\t','').replace(' ', '')
     12911                    pvals = pvals + ilvals
     12912        allfparams[namelistn] = fparams
     12913
     12914    # Output
     12915    ##
     12916    outl = ''
     12917    # LaTeX table with a column per file
     12918    if output == 'textabcol':
     12919        ipar = 0
     12920        outl = '\\begin{tabular}{' + Ntchar('c',Nfiles+1) + '}\n' +                  \
     12921         '{\\bfseries{filename}}'
     12922        for fn in nmlfs:
     12923            outl = outl + ' & {\\bfseries{' + fn.replace('_', '\\_') + '}}'
     12924        outl = outl + ' \\\\ \hline\n'
     12925        for secn in secnames:
     12926            psec = lookparams[secn]
     12927            ipar = 0
     12928            for ps in psec:
     12929                ifile = 0
     12930                outl = outl + '{\\bfseries{' + ps + '}}'
     12931                for fn in nmlfs:
     12932                    fparams = allfparams[fn]
     12933                    if not fparams.has_key(ps):
     12934                        #print infmsg
     12935                        #print '  ' + fname + ": provided namelist file '" + fn +     \
     12936                        #  "' has not parameter '" + ps + "' !!"
     12937                        val = '-'
     12938                    else:
     12939                        val = fparams[ps].replace('_', '\\_')
     12940                    outl = outl + ' & ' + val
     12941                    ifile = ifile + 1
     12942                outl = outl + ' \\\\ \n'
     12943                ipar = ipar + 1
     12944        outl = outl + '\\end{tabular}\n'
     12945
     12946    # LaTeX table with a row per file
     12947    elif output == 'textabrow':
     12948        ifile = 0
     12949        outl = '\\begin{tabular}{' + Ntchar('c',Npars+1) + '}\n' +                   \
     12950           '{\\bfseries{filename}}'
     12951        for secn in secnames:
     12952            psec = lookparams[secn]
     12953            for ps in psec:
     12954                outl = outl + ' & {\\bfseries{' + ps.replace('_', '\\_') + '}}'
     12955        outl = outl + ' \\\\ \\hline\n'
     12956
     12957        for fn in nmlfs:
     12958            fparams = allfparams[fn]
     12959            ipar = 0
     12960            outl = outl + fn.replace('_', '\\_')
     12961            for secn in secnames:
     12962                print secn + "&"
     12963                psec = lookparams[secn]
     12964                for ps in psec:
     12965                    if not fparams.has_key(ps):
     12966                        #print infmsg
     12967                        #print '  ' + fname + ": provided namelist file '" + fn +     \
     12968                        #  "' has not parameter '" + ps + "' !!"
     12969                        val = '-'
     12970                    else:
     12971                        val = fparams[ps].replace('_', '\\_')
     12972                    outl = outl + ' & ' + val
     12973            outl = outl + '\\\\ \n'
     12974        outl = outl + '\\end{tabular}\n'
     12975
     12976    # ASCII line
     12977    elif output == 'twopoints@':
     12978        ifile = 0
     12979        for fn in nmlfs:
     12980            fparams = allfparams[fn]
     12981            ipar = 0
     12982            if ifile == 0:
     12983                outl = fn
    1288212984            else:
    12883                 ilvals = line.replace('\n', '').replace('\t','').replace(' ', '')
    12884                 pvals = pvals + ilvals
    12885 
    12886     outl = ''
    12887     ipar = 0
    12888     for secn in secnames:
    12889         print secn + "&"
    12890         psec = lookparams[secn]
    12891         for ps in psec:
    12892             if not fparams.has_key(ps):
    12893                 print errormsg
    12894                 print '  ' + fname + ": provided namelist file '" + namelistn +      \
    12895                   "' has not parameter '" + ps + "' !!"
    12896                 quit(-1)
    12897 
    12898             # output
    12899             if output == 'textabcol':
    12900                 if ipar == 0: outl = ps.replace('_', '\\_') + ' & ' +                \
    12901                   fparams[ps].replace('_', '\\_') + ' \\\\'
    12902                 else: outl = outl + '\n' + ps.replace('_', '\\_') +' & '+            \
    12903                   fparams[ps].replace('_', '\\_') + ' \\\\'
    12904             elif output == 'textabrow':
    12905                 if ipar == 0:
    12906                     outl1 = '{\\bfseries{' + ps.replace('_', '\\_') + '}}'
    12907                     outl2 = fparams[ps].replace('_', '\\_')
    12908                 else:
    12909                     outl1 = outl1 + ' & {\\bfseries{' + ps.replace('_', '\\_') + '}}'
    12910                     outl2 = outl2 + ' & ' + fparams[ps].replace('_', '\\_')
    12911             elif output == 'twopoints@':
    12912                 if ipar == 0: outl = ps + '@' + fparams[ps]
    12913                 else: outl = outl + ':' + ps + '@' + fparams[ps]
    12914             else:
    12915                 print errormsg
    12916                 print '  ' + fname + ": kind of output '" + output + "' not ready !!"
    12917                 print '    available ones:', availoutput
    12918                 quit(-1)
    12919             ipar=ipar+1
    12920 
    12921     # Additional parameters (if they exist)
    12922     if params == 'basic':
    12923         psec = lookparams['IFexist']
    12924         for ps in psec:
    12925             if fparams.has_key(ps):
    12926                 if output == 'textabcol':
    12927                     outl = outl + '\n' + ps.replace('_', '\\_') +' & '+              \
    12928                       fparams[ps].replace('_', '\\_') + ' \\\\'
    12929                 elif output == 'textabrow':
    12930                     outl1 = outl1 + ' & {\\bfseries{' + ps.replace('_', '\\_') + '}}'
    12931                     outl2 = outl2 + ' & ' + fparams[ps].replace('_', '\\_')
    12932                 elif output == 'twopoints@':
    12933                     outl = outl + ':' + ps + '@' + fparams[ps]
    12934 
    12935     if output == 'textabrow':
    12936         outl = outl1 + ' \\\\\n' + outl2
     12985                outl = outl + ';' + fn
     12986
     12987            for secn in secnames:
     12988                print secn + "&"
     12989                psec = lookparams[secn]
     12990                for ps in psec:
     12991                    if not fparams.has_key(ps):
     12992                        print infmsg
     12993                        print '  ' + fname + ": provided namelist file '" + fn +     \
     12994                          "' has not parameter '" + ps + "' !!"
     12995                        val = '-'
     12996                    else:
     12997                        val = fparams[ps]
     12998
     12999                    if ipar == 0: outl = ps + '@' + val
     13000                    else: outl = outl + ':' + ps + '@' + val
     13001
     13002                    ipar = ipar + 1
     13003            ifile = ifile + 1
     13004    else:
     13005        print errormsg
     13006        print '  ' + fname + ": kind of output '" + output + "' not ready !!"
     13007        print '    available ones:', availoutput
     13008        quit(-1)
    1293713009
    1293813010    return outl
    1293913011
    12940 #print WRFsetup('/home/lluis/estudios/RELAMPAGO/SimCoor/UBA/namelist.input', 'specific,start_year:end_year', 'textabcol')
     13012#print WRFsetup('/home/lluis/estudios/RELAMPAGO/SimCoor/UBA/namelist.input,/home/lluis/estudios/RELAMPAGO/SimCoor/SMN/namelist.input,/home/lluis/estudios/RELAMPAGO/SimCoor/NOA-IERSD/namelist.input,/home/lluis/estudios/RELAMPAGO/SimCoor/UBAmili/namelist.input', 'basic', 'textabcol')
    1294113013
    1294213014#quit()
Note: See TracChangeset for help on using the changeset viewer.