Changeset 1095 in lmdz_wrf


Ignore:
Timestamp:
Sep 9, 2016, 7:05:24 PM (8 years ago)
Author:
lfita
Message:

First final version!!!

  • Adding re-set of times for a given model
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/model_graphics.py

    r1085 r1095  
    3030      'diffscratch', 'figdiffscratch', 'figallmodexpscratch', 'addfiles',            \
    3131      'addfigures', 'adddiffs', 'adddifffigures', 'addallmodexpfigures', 'debug',    \
    32       'models', 'ifold', 'ofold', 'warnmsg', 'errmsg', 'titleoperations', 'kindfig', \
    33       'CFreftime', 'CFunitstime', 'mapval', 'timekind', 'timefmt', 'timelabel' ]
     32      'models', 'modgraphchar', 'expgraphchar', 'ifold', 'ofold', 'warnmsg',         \
     33      'errmsg', 'titleoperations', 'kindfig', 'CFreftime', 'CFunitstime', 'mapval',  \
     34      'timekind', 'timefmt', 'timelabel' ]
    3435
    3536    # List with the optional keys
     
    248249      addmoddifffigs, addallmodexpfigs, debug
    249250
     251
     252class gc(object):
     253    """ Class which holds all the graphical characteristics
     254      [label]: label as it should appear in the graphics
     255      [color]: specific color of the model
     256      [linetype]: specific type of line of the model
     257      [marker]: specific marker of the model
     258      [sizes]: line width and point size for the model
     259      [tmod]: time modification to apply to the model files (None for nothing)
     260        'setorigin',[YYYYMMDDHHMISS]: re-set origin of times at [YYYYMMDDHHMISS]
     261    """
     262    def __init__( self, label, color, linetype, marker, sizes, Tmod):
     263        self.label = None
     264        self.color = None
     265        self.linetype = None
     266        self.marker = None
     267        self.sizes = None
     268        self.tmod = None
     269        if label is not None:
     270            self.label = label
     271            self.color = color
     272            self.linetype = linetype
     273            self.marker = marker
     274            self.sizes = sizes
     275            if Tmod != 'None':
     276                self.tmod = Tmod
     277
     278def graphical_characteristics(config, debug):
     279    """ Function to provide the graphical characteristics of models and experiments
     280      config= configuration of the experiment
     281    """
     282    fname = 'graphical_characteristics'
     283
     284    modgraphchar = {}
     285    modvals = config['modgraphchar'].split(':')
     286    for modval in modvals:
     287        modv = modval.split('|')
     288        modgraphchar[modv[0]] = gc(modv[1], modv[2], modv[3], modv[4], modv[5], modv[6])
     289
     290    expgraphchar = {}
     291    expvals = config['expgraphchar'].split(':')
     292    for expval in expvals:
     293        expvs = expval.split('|')
     294        modexp = expvs[0] + '/' + expvs[1]
     295        if not modgraphchar.has_key(expvs[0]):
     296            print errmsg
     297            print '  ' + fname + ": a model called '" +expvs[0]+ "' does not exist !!"
     298            print '    existing models with graphic characteristics:',               \
     299              modgraphchar.keys()
     300            quit(-1)
     301
     302        modvs = modgraphchar[expvs[0]]
     303        expv = expvs[2:7]
     304        if expv[1] == 'asmodel': expv[1] = modvs.color
     305        if expv[2] == 'asmodel': expv[2] = modvs.linetype
     306        if expv[3] == 'asmodel': expv[3] = modvs.marker
     307        if expv[4] == 'asmodel': expv[4] = modvs.sizes
     308
     309        expgraphchar[modexp] = gc(expv[0], expv[1], expv[2], expv[3], expv[4], 'None')
     310
     311    # Running some tests
     312    mods = config['models'].split(':')
     313    for mod in mods:
     314        if not modgraphchar.has_key(mod):
     315            print errmsg
     316            print '  ' + fname + ": model called '" + mod + "' does not have " +     \
     317              "graphical characteristics !!"
     318            print '    existing models with graphic characteristics:',               \
     319              modgraphchar.keys()
     320            quit(-1)
     321
     322        exps = config[mod + 'exps'].split(':')
     323        for exp in exps:
     324            modexp = mod + '/' + exp
     325            if not expgraphchar.has_key(modexp):
     326                print errmsg
     327                print '  ' + fname + ": model/experiment called '" + modexp +        \
     328                  "' does not have graphical characteristics !!"
     329                print '    existing model/experiments with graphic ' +               \
     330                  'characteristics:', expgraphchar.keys()
     331                quit(-1)
     332
     333    if debug:
     334        print '  ' + fname + ': model graphical characteristics _______'
     335        for modn in modgraphchar.keys():
     336            with gen.Capturing() as output:
     337                gen.printing_class(modgraphchar[modn])
     338            print "'" + modn + "'; " + ', '.join(output)
     339        print '  ' + fname + ': experiment graphical characteristics _______'
     340        for modexpn in expgraphchar.keys():
     341            with gen.Capturing() as output:
     342                gen.printing_class(expgraphchar[modexpn])
     343            print "'" + modexpn + "'; " + ', '.join(output)
     344
     345    return modgraphchar, expgraphchar
     346
    250347def exp_headers(mod,config):
    251348    """ Function to provide the headers and the experiments of a given model
     
    299396      dimensions= list of the dimensions
    300397      vardimensions= list of the variable-dimensions
     398      timemodif= time modification to apply to the model files ('None' for nothing)
     399        'setorigin',[YYYYMMDDHHMISS]: re-set origin of times at [YYYYMMDDHHMISS]
    301400    """
    302     def __init__( self, name, model, dx, dy, dz, dt, ds, vdx, vdy, vdz, vdt, vds):
    303        self.name= None
    304        self.model= None
    305        self.dimxn= None
    306        self.dimyn= None
    307        self.dimzn= None
    308        self.dimtn= None
    309        self.dimsn= None
    310        self.vardxn= None
    311        self.vardyn= None
    312        self.vardzn= None
    313        self.vardtn= None
    314        self.vardsn= None
    315        if name is not None:
    316            self.name = name
    317            self.model= model
    318            self.dimxn= dx
    319            self.dimyn= dy
    320            self.dimzn= dz
    321            self.dimtn= dt
    322            self.dimsn= ds
    323            self.vardxn= vdx
    324            self.vardyn= vdy
    325            self.vardzn= vdz
    326            self.vardtn= vdt
    327            self.vardsn= vds
    328            self.dimensions = [dx, dy, dz, dt, ds]
    329            self.vardimensions = [vdx, vdy, vdz, vdt, vds]
     401    def __init__( self, name, model, dx, dy, dz, dt, ds, vdx, vdy, vdz, vdt, vds,    \
     402      tmod):
     403        self.name= None
     404        self.model= None
     405        self.dimxn= None
     406        self.dimyn= None
     407        self.dimzn= None
     408        self.dimtn= None
     409        self.dimsn= None
     410        self.vardxn= None
     411        self.vardyn= None
     412        self.vardzn= None
     413        self.vardtn= None
     414        self.vardsn= None
     415        self.timemodif = None
     416        if name is not None:
     417            self.name = name
     418            self.model= model
     419            self.dimxn= dx
     420            self.dimyn= dy
     421            self.dimzn= dz
     422            self.dimtn= dt
     423            self.dimsn= ds
     424            self.vardxn= vdx
     425            self.vardyn= vdy
     426            self.vardzn= vdz
     427            self.vardtn= vdt
     428            self.vardsn= vds
     429            if tmod is not None:
     430                self.timemodif = tmod
     431            self.dimensions = [dx, dy, dz, dt, ds]
     432            self.vardimensions = [vdx, vdy, vdz, vdt, vds]
    330433
    331434def variable_compute(idir,var,ftests,db):
     
    671774                print '  ' + fname + ": creation of variable file '" + CFvarn +      \
    672775                  "' in file '" + ifilen + "' ..."
    673                 print 'model variable:', modvar
    674                 print 'diagostic variable:', diagvar
     776                print '    model variable:', modvar
     777                print '    diagostic variable:', diagvar
    675778
    676779            if diagvar is None:
     
    777880                try:
    778881                    with gen.Capturing() as output:
    779                         ncvar.LMDZ_toCF(ifilen)
     882                        ncvar.LMDZ_toCF(Tunits + '!since!' + Tref, ifilen)
    780883                except:
    781884                    print errmsg
    782                     print 'LMDZ_toCF('+ ifilen + ')'
     885                    print 'LMDZ_toCF('+ Tunits +'!since!'+ Tref + ', ' + ifilen + ')'
    783886                    for s1out in output: print s1out
    784887                    quit(-1)
     
    815918        sout = sub.call('mv netcdf_fold_concatenated_HMT.nc ' + fileon, shell=True)
    816919        if os.path.isfile(fileon):
    817             sout = sub.call('rm ' + CFvarn +'_'+ headerf+SgP + '_*-*.nc >& /dev/null',
     920            sout = sub.call('rm '+CFvarn +'_'+ headerf+SgP + '_*-*.nc >& /dev/null',\
    818921              shell=True)
     922
     923    # Re-setting time of final concatenated file
     924    if minf.timemodif is not None:
     925        if db:
     926            print '  ' + fname + ": Modifying times of the file by '" +              \
     927              minf.timemodif + "' !!"
     928        try:
     929            with gen.Capturing() as output:
     930                ncvar.time_reset(minf.timemodif, fileon,'time')
     931        except:
     932            print errmsg
     933            print 'time_reset(' + minf.timemodif + ', ' + fileon + ', time)'
     934            for s1out in output: print s1out
     935            quit(-1)
     936        if db:
     937            for s1out in output: print s1out
    819938
    820939    return
     
    13651484        vopnV = allvarcomp[vopn]
    13661485        fheader = vopnV[0]
    1367         model = vopnV[1]
     1486        if type(vopnV[1]) == type([1, 2]) and len(vopnV[1]) == 1:
     1487            vals = vopnV[1]
     1488            model = vals[0]
     1489        else:
     1490            model = vopnV[1]
    13681491        diag = vopnV[2]
    13691492        globalP = vopnV[3]
     
    15701693
    15711694def draw_plot(kplot, CFvplot, fplot, vplot, dplot, pplot, finame, tfig, kfig, mapval,\
    1572   tvals, od, pyH, fscr, db):
     1695  tvals, expgraphc, od, pyH, fscr, db):
    15731696    """ Function to draw a plot
    15741697      kplot= kind of plot
     
    15871710        timefmt: format of time ticks in the plot
    15881711        timelabel: label of time-axis in the plot
     1712      expgraphc= dictionary with expriment graphical characteristics
    15891713      od= output directory
    15901714      pyH= python HOME
     
    16091733    timefmt = tvals[2]
    16101734    timelabel = tvals[3]
     1735
     1736    # Graphical labels and configuration
     1737   
    16111738
    16121739    if not os.path.isfile(finame):
     
    18151942                        kinds = kinds + ',' + kind
    18161943                        lines = lines + ',' + line
    1817                         marks = marks + ',' + mark
     1944                        marks = marks + '@' + mark
    18181945                        sizes = sizes + ',' + str(size)
    18191946                else:
     
    18231950                    sizes = '2.'
    18241951
    1825 
    18261952                secsf = fplot[il].split('/')
    18271953                Nsecsf = len(secsf)
    1828                 lablines.append(secsf[Nsecsf-3] + '/' + secsf[Nsecsf-2])
     1954                expvs = expgraphc[secsf[Nsecsf-3] + '/' + secsf[Nsecsf-2]]
     1955                lablines.append(expvs.label)
    18291956
    18301957            # It is assumed that if the space variable is 'lon': is desired a
     
    18732000                for s1out in output: print s1out
    18742001                quit(-1)
     2002
     2003            # keeping all figures
     2004            trkobjf.write('\n')
     2005            trkobjf.write("#" + tfig.replace('!',' ') + '\n')
     2006            trkobjf.write(plotins + '\n')
     2007
     2008        elif kplot == 'Nlines_time':
     2009            figtit = tfig.replace('!','|')
     2010
     2011            linestdn = CFvplot[0]
     2012            figfs = ','.join(fplot)
     2013
     2014            Nlines = len(fplot)
     2015
     2016            lablines = []
     2017            for il in range(Nlines):
     2018                linev = pplot[il]
     2019                # Values are changed from specific values in `model_graphics.dat'
     2020                if linev[3].find('%') == -1:
     2021                    line = linev[2]
     2022                    kind = linev[3]
     2023                    mark = linev[4]
     2024                    size = linev[5]
     2025                    if il == 0:
     2026                        vrange = str(linev[0]) + ',' + str(linev[1])
     2027                        kinds = kind
     2028                        lines = line
     2029                        marks = mark
     2030                        sizes = str(size)
     2031                    else:
     2032                        kinds = kinds + ',' + kind
     2033                        lines = lines + ',' + line
     2034                        marks = marks + '@' + mark
     2035                        sizes = sizes + ',' + str(size)
     2036                else:
     2037                    lines = 'None'
     2038                    kinds = '-'
     2039                    marks = ','
     2040                    sizes = '2.'
     2041
     2042                secsf = fplot[il].split('/')
     2043                Nsecsf = len(secsf)
     2044                expvs = expgraphc[secsf[Nsecsf-3] + '/' + secsf[Nsecsf-2]]
     2045                lablines.append(expvs.label)
     2046
     2047            valsaxis = 'y'
     2048            figmid = 'evolution'
     2049
     2050            figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' +           \
     2051              '|'.join(tfig.split('!')[2:])
     2052            leglabels = ','.join(lablines)
     2053
     2054            graphvals = 'time;' + valsaxis + ';time;' + leglabels + ';' +            \
     2055              CFvplot[0] + ';' + figtit + ';None;time|' + '|'.join(tvals) +          \
     2056              ';0|auto;' + kfig + ';' + kinds + ';' + lines + ';' + marks + ';' +    \
     2057              sizes + ';' + sizes + ';all;-1'
     2058
     2059            fvarS = vplot[0]
     2060
     2061            drwins = 'draw_lines_time'
     2062            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
     2063              " -S '" + graphvals + "' -v " + fvarS
     2064
     2065            try:
     2066                with gen.Capturing() as output:
     2067                    sout = sub.call(plotins, shell=True)
     2068            except:
     2069                print errmsg
     2070                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
     2071                print sout
     2072                for s1out in output: print s1out
     2073                quit(-1)
     2074
     2075            sout = sub.call('mv lines_time.' + kfig + ' ' + finame, shell=True)
    18752076
    18762077            # keeping all figures
     
    22262427    kindfigure = config['kindfig']
    22272428
     2429    # Graphical labels and configuration
     2430    modgc, expgc = graphical_characteristics(config, debug)
     2431    modv = modgc[mod]
     2432    modlab = modv.label
     2433    expv = expgc[mod+'/'+exp]
     2434    explab = expv.label
     2435
    22282436    # Map value
    22292437    mapvalue = config['mapval']
     
    23292537
    23302538            # Title of figure
    2331             titfigure = create_figure_title(mod, exp, varops, opexplained)
     2539            titfigure = create_figure_title(modlab, explab, varops, opexplained)
    23322540
    23332541            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
    2334               figname, titfigure, kindfigure, mapvalue, timevals, odir, pyHOME,      \
    2335               figscr, debug)
     2542              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
     2543              pyHOME, figscr, debug)
    23362544
    23372545        # End of variables-operations
     
    32783486    kindfigure = config['kindfig']
    32793487
     3488    # Graphical labels and configuration
     3489    modgc, expgc = graphical_characteristics(config, debug)
     3490
    32803491    # Map value
    32813492    mapvalue = config['mapval']
     
    33373548                headf = vopvals[4]
    33383549
    3339                 modexpdiff = mod2 + '/' + exp2 + '-' + mod1 + '/' + exp1
     3550                modv = modgc[mod1]
     3551                expv = expgc[mod1+'/'+exp1]
     3552                modlab1 = modv.label
     3553                explab1 = expv.label
     3554                modv = modgc[mod2]
     3555                expv = expgc[mod2+'/'+exp2]
     3556                modlab2 = modv.label
     3557                explab2 = expv.label
     3558
     3559                modexpdiff = explab2 + '-' + explab1
    33403560                modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
    33413561
     
    34063626
    34073627            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
    3408               figname, titfigure, kindfigure, mapvalue, timevals, odir, pyHOME,      \
    3409               figscr, debug)
     3628              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
     3629              pyHOME, figscr, debug)
    34103630
    34113631        # End of variables-operations
     
    35753795                print '  ' + fname + ': computing from the original reprojected file'
    35763796                ModI = ModelInf(mod, mod, 'lon', 'lat', 'pres', 'time', 'depth',     \
    3577                   'lon', 'lat', 'pres', 'time', 'depth')
     3797                  'lon', 'lat', 'pres', 'time', 'depth', None)
    35783798                VarI = VariableInf(vn, values[0], values[2], values[3])
    35793799                usefs = {values[0]: ofileorig}
     
    37183938    objf.close()
    37193939
    3720     return allplts, Nallmodexp
     3940    return allmodexp, Nallmodexp
    37213941
    37223942def allmodexps_read(filen):
     
    37373957            values = line.replace('\n','').split(' ')
    37383958            if values[0] == 'allmodexps:':
    3739                 allplts = gen.stringList_dictKeysVals(values[1])
     3959                allplts = gen.stringList_dictKeysVals(values[1],cV=':')
    37403960            elif values[0] == 'Nallmodexp:':
    37413961                Nallmodexp = int(values[1])
     
    38124032            gen.printing_dictionary(plotspecifics)
    38134033
     4034    # Graphical labels and configuration
     4035    modgc, expgc = graphical_characteristics(config, debug)
     4036
    38144037    # Kind of figures
    38154038    kindfigure = config['kindfig']
     
    39334156
    39344157            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
    3935               figname, titfigure, kindfigure, mapvalue, timevals, odir, pyHOME,      \
    3936               figscr, debug)
     4158              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
     4159              pyHOME, figscr, debug)
    39374160
    39384161        # End of variables-operations
     
    39724195# Getting models
    39734196mods = cnf['models'].split(':')
     4197
     4198# Getting graphical characeristics
     4199modGC, expGC = graphical_characteristics(cnf, dbg)
    39744200
    39754201# Models loop
     
    40004226    dnz = Modinf.dimzn
    40014227    dnt = Modinf.dimtn
     4228    dns = Modinf.dimsn
    40024229    vdnx = Modinf.vardxn
    40034230    vdny = Modinf.vardyn
    40044231    vdnz = Modinf.vardzn
    40054232    vdnt = Modinf.vardtn
     4233    vdns = Modinf.vardsn
     4234
     4235    modgraphv = modGC[mod]
     4236    Modinf = ModelInf(mod, mod, dnx, dny, dnz, dnt, dns, vdnx, vdny, vdnz, vdnt,     \
     4237      vdns, modgraphv.tmod)
    40064238
    40074239    if dbg:
    40084240        print '  model characteristics _______'
    4009         print "  dims:", dnx, dny, dnz, dnt
    4010         print "  var dims:", vdnx, vdny, vdnz, vdnt
     4241        gen.printing_class(Modinf)
    40114242
    40124243    moddims = dnx + ',' + dny + ',' + dnz + ',' + dnt
     
    45684799    sout = sub.call('rm ' + allf + ' >& /dev/null', shell=True)
    45694800    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
    4570 
    4571     objf = open(allfig,'w')
     4801    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
     4802
     4803    objf = open(allfigf,'w')
    45724804    objf.write("## Drawing of all variables figures for all model-experiments\n")
    45734805    objf.close()
    45744806       
    45754807if addallmodexpfigures:
    4576     sout = sub.call('rm ' + allf + ' >& /dev/null', shell=True)
     4808    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
    45774809    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
    45784810
Note: See TracChangeset for help on using the changeset viewer.