Changeset 1325 in lmdz_wrf


Ignore:
Timestamp:
Nov 11, 2016, 9:38:47 PM (9 years ago)
Author:
lfita
Message:

Adding generation of pdf file with all the graphics sorted by mod/exp

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/model_graphics.py

    r1317 r1325  
    29342934
    29352935            # Attributes of the variable
    2936             varattrs = ncvar.ivattrs(ifile2,vninF)
     2936            with gen.Capturing() as output:
     2937                varattrs = ncvar.ivattrs(ifile2,vninF)
     2938            if debug:
     2939                for s1out in output: print s1out
    29372940            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
    29382941              varattrs['units'][0]
     
    35033506
    35043507            # Attributes of the variable
    3505             varattrs = ncvar.ivattrs(ifile2,vninF)
     3508            with gen.Capturing() as output:
     3509                varattrs = ncvar.ivattrs(ifile2,vninF)
     3510            if debug:
     3511                for s1out in output: print s1out
    35063512            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
    35073513              varattrs['units'][0]
     
    38773883        objf.close()
    38783884    else:
    3879         print warnmsg
    3880         print '  ' + main + ": getting variables to compute already from file !!"
     3885        if dbg:
     3886            print warnmsg
     3887            print '  ' + main + ": getting variables to compute already from file !!"
    38813888        origfiles, origtestfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
    38823889
     
    39483955            if iif == 1 and len(inNO) > 0:
    39493956                doreproj = False
    3950                 print warnmsg
    3951                 print '  ' + fname + ": operation '" + op + "' can not be " +        \
    3952                   "reprojected due to the lack of 'lon' or 'lat' skipping it !!"
    3953                 print "    NOTE: difference can only be computed as:"
    3954                 print "      - 1st: compute variable's differences between models"
    3955                 print "      - 2nd: Perform the operations"
     3957                if dbg:
     3958                    print warnmsg
     3959                    print '  ' + fname + ": operation '" + op + "' can not be " +    \
     3960                      "reprojected due to the lack of 'lon' or 'lat' skipping it !!"
     3961                    print "    NOTE: difference can only be computed as:"
     3962                    print "      - 1st: compute variable's differences between models"
     3963                    print "      - 2nd: Perform the operations"
    39563964
    39573965                # Building up resources to compute re-projected statistics
    3958                 print '  ' + fname + ': computing from the original reprojected file'
     3966                if dbg:
     3967                    print '  '+fname+': computing from the original reprojected file'
    39593968                ModI = ModelInf(mod, mod, 'lon', 'lat', 'pres', 'time', 'depth',     \
    39603969                  'lon', 'lat', 'pres', 'time', 'depth', None)
     
    42934302                    print '  ' + fname + ": no file for variable-operation '" +      \
    42944303                      vnopS + "' !!"
    4295                 modexpvals = allvarcomp[vnopS]
    4296 #                modexpvalsS = allvarcomp[vnopS]
    4297 #                modexpvals = modexpvalsS[0].split('|')
     4304#                modexpvals = allvarcomp[vnopS]
     4305                modexpvalsS = allvarcomp[vnopS]
     4306                modexpvals = modexpvalsS[0].split('|')
    42984307                Nmodexps = len(modexpvals)
    42994308
     
    45264535            allcompvar, Nvar = compvars_listconstruct(cnf, Modinf, files, testfiles, \
    45274536              iwdir, owdir, dbg)
    4528         else:
    4529             print warnmsg
    4530             print '  ' + main + ": getting variables to compute already from file !!"
     4537        else:
     4538            if dbg:
     4539                print warnmsg
     4540                print '  '+main+": getting variables to compute already from file !!"
    45314541            files, testfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
    45324542
     
    45634573            listplots, Nplt = plots_listconstruct(cnf, 'DIRPLT', dirfigf, owdir, dbg)
    45644574        else:
    4565             print warnmsg
    4566             print '  ' + main + ": getting plots to draw already from file !!"
     4575            if dbg:
     4576                print warnmsg
     4577                print '  ' + main + ": getting plots to draw already from file !!"
    45674578            listplots, Nplt = read_plot_file(dirfigf)
    45684579
     
    46364647                alldiffop, alldiffvar,Nopdiffs,Nvardiffs=diffvarop_listconstruct(cnf,\
    46374648                  [mod, mod], [exp1, exp2], [False, False], owdir, dbg)
    4638             else:
    4639                 print warnmsg
    4640                 print '  ' + main + ": getting 'op' and 'var'differences to " +      \
    4641                   "compute already from file !!"
     4649            else:
     4650                if dbg:
     4651                    print warnmsg
     4652                    print '  ' + main + ": getting 'op' and 'var'differences to " +  \
     4653                      "compute already from file !!"
    46424654                alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
    46434655                alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
     
    46824694              dbg)
    46834695        else:
    4684             print warnmsg
    4685             print '  ' + main + ": getting plots to draw already from file !!"
     4696            if dbg:
     4697                print warnmsg
     4698                print '  ' + main + ": getting plots to draw already from file !!"
    46864699            listplots, Nplt = read_plot_file(dirfigf)
    46874700
     
    47194732              dbg)
    47204733        else:
    4721             print warnmsg
    4722             print '  ' + main + ": getting plots to draw already from file !!"
     4734            if dbg:
     4735                print warnmsg
     4736                print '  ' + main + ": getting plots to draw already from file !!"
    47234737            listplots, Nplt = read_plot_file(dirfigf)
    47244738
     
    49014915                      dbg)
    49024916                else:
    4903                     print warnmsg
    4904                     print '  ' + main + ": getting 'op' and 'var' differences to " + \
    4905                       "compute already from file !!"
     4917                    if dbg:
     4918                        print warnmsg
     4919                        print '  '+main+": getting 'op' and 'var' differences to " + \
     4920                          "compute already from file !!"
    49064921                    alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
    49074922                    alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
     
    49464961                      owdir, dbg)
    49474962                else:
    4948                     print warnmsg
    4949                     print '  ' + main + ": getting plots to draw already from file !!"
     4963                    if dbg:
     4964                        print warnmsg
     4965                        print '  '+main+": getting plots to draw already from file !!"
    49504966                    listplots, Nplt = read_plot_file(dirfigf)
    49514967
     
    49834999                      owdir, dbg)
    49845000                else:
    4985                     print warnmsg
    4986                     print '  ' + main + ": getting plots to draw already from file !!"
     5001                    if dbg:
     5002                        print warnmsg
     5003                        print '  '+main+": getting plots to draw already from file !!"
    49875004                    listplots, Nplt = read_plot_file(dirfigf)
    49885005
     
    50485065    listplots, Nplt = plots_listconstruct(cnf, 'PLOTALLMODEXP', dirfigf, owdir, dbg)
    50495066else:
    5050     print warnmsg
    5051     print '  ' + main + ": getting plots to draw already from file !!"
     5067    if dbg:
     5068        print warnmsg
     5069        print '  ' + main + ": getting plots to draw already from file !!"
    50525070    listplots, Nplt = read_plot_file(dirfigf)
    50535071
     
    50695087
    50705088print main + ': all files and figures have been properly done !!'
     5089
     5090# Creation of a pdf with all figures
     5091##
     5092###
     5093owdir = cnf['ofold']
     5094os.chdir(owdir)
     5095
     5096texout ='model_graphics-images'
     5097print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
     5098print '  ** Creation of a pdf with all figures  '
     5099print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
     5100print '  ' + owdir + '/' + texout + '.pdf'
     5101
     5102if dbg:
     5103    print main + ": generation of a pdf file '" + owdir + '/' + texout +             \
     5104      "' with all the figures..."
     5105
     5106otexf = open(owdir + '/' + texout + '.tex', 'w')
     5107otexf.write('\\documentclass{article}\n')
     5108otexf.write('\\usepackage{graphicx}\n')
     5109otexf.write('\\usepackage[colorlinks=true,urlcolor=blue]{hyperref}\n')
     5110otexf.write('\\textheight=23cm\n')
     5111otexf.write('\\textwidth=18cm\n')
     5112otexf.write('\\oddsidemargin=-1cm\n')
     5113otexf.write('\\evensidemargin=-1cm\n')
     5114otexf.write('\\topmargin=-1cm\n')
     5115
     5116otexf.write('\n')
     5117otexf.write('\\begin{document}\n')
     5118otexf.write('\n')
     5119otexf.write('\\def\\fontBloc{\\fontfamily{\\sfdefault}\\large\\bfseries}\n')
     5120otexf.write('\\def\\linia{\\setlength{\\unitlength}{1mm}\n')
     5121otexf.write('\\line(1,0){80}}\n')
     5122otexf.write('\\newcommand{\\modexp}[1]{\\clearpage\n')
     5123otexf.write('\\noindent\\linia\n')
     5124otexf.write('\\section{#1}\n')
     5125otexf.write('\\linia}\n')
     5126otexf.write('\n')
     5127
     5128otexf.write('\\title{model\_graphics: ' + gen.latex_text(owdir) + '}\n')
     5129otexf.write('\\maketitle\n')
     5130otexf.write('\n')
     5131otexf.write('\\tableofcontents\n')
     5132otexf.write('\\listoffigures\n')
     5133otexf.write('\\clearpage\n')
     5134
     5135Nmods = len(mods)
     5136# Figures from mod/exp pairs
     5137for mod in mods:
     5138    exps = modexps[mod]
     5139    for exp in exps:
     5140        print '  direct plots:', mod, exp
     5141        owdirme = owdir + '/' + mod + '/' + exp
     5142        otexf.write('% ' + mod + ' ' + exp + '\n')
     5143        otexf.write('\\modexp{' + gen.latex_text(mod+' '+exp) + '}\n')
     5144        listplots, Nplt = read_plot_file(owdirme + '/directplotsdraw.inf')
     5145        for plot in listplots.keys():
     5146            plots = listplots[plot]
     5147            ops = []
     5148            for plt in plots:
     5149                opn = plt.split('#')[0].split('|')[1]
     5150                if not gen.searchInlist(ops,opn): ops.append(opn)
     5151            for op in ops:
     5152                opS = op.replace('+','_')
     5153                modexpimg = gen.files_folder_HMT(owdirme, head=plot, middle=opS,     \
     5154                  tail=cnf['kindfig'])
     5155                Nimages = len(modexpimg)
     5156                if Nimages > 0:
     5157                    for img in range(Nimages): modexpimg[img] = owdirme + '/' +      \
     5158                      modexpimg[img]
     5159                    caption = mod + ' ' + exp + ' ' + plot + ' ' + op
     5160                    flab = 'fig:' + caption.replace(' ', '_')
     5161                    try:
     5162                        with gen.Capturing() as output:
     5163                            gen.latex_fig_array(modexpimg, otexf,                    \
     5164                              gen.latex_text(caption), flab, refsize=0.5, dist='sqr',\
     5165                              dorest='center')
     5166                    except:
     5167                        print errmsg
     5168                        print 'gen.latex_fig_array('+modexpimg+', '+otexf+', '+      \
     5169                              'gen.latex_text(' + caption + '), ' + flab +           \
     5170                              ", refsize=0.5, dist='sqr', dorest='center')"
     5171                        for s1out in output: print s1out
     5172                        quit(-1)
     5173                    otexf.write('%\\clearpage\n')
     5174
     5175    # differences between experiments
     5176    Nexps = len(exps)
     5177    for iexp1 in range(0,Nexps-1):
     5178        exp1 = exps[iexp1]
     5179        for iexp2 in range(iexp1+1,Nexps):
     5180            exp2 = exps[iexp2]
     5181            Sexps = exp2 + '-' + exp1
     5182            owdirme = owdir + '/' + mod + '/' + Sexps
     5183            print '    diffs:',  mod + '/' + Sexps + ' ...'
     5184            for diffn in ['op', 'var']:
     5185                fileplotinf = owdirme + '/diff' + diffn + 'plotsdraw.inf'
     5186                if os.path.isfile(fileplotinf):
     5187                    otexf.write('% ' + mod + ' ' + Sexps + ' diff' + diffn + '\n')
     5188                    otexf.write('\\modexp{' + gen.latex_text(mod+' '+Sexps+' diff'+  \
     5189                      diffn)+ '}\n')
     5190                    listplots, Nplt = read_plot_file(fileplotinf)
     5191                    for plot in listplots.keys():
     5192                        plots = listplots[plot]
     5193                        ops = []
     5194                        for plt in plots:
     5195                            opn = plt.split('#')[0].split('|')[1]
     5196                            if not gen.searchInlist(ops,opn): ops.append(opn)
     5197                        for op in ops:
     5198                            opS = op.replace('+','-')
     5199                            modexpimg = gen.files_folder_HMT(owdirme, head=plot,     \
     5200                              middle=opS, tail=cnf['kindfig'])
     5201                            Nimages = len(modexpimg)
     5202                            if Nimages > 0:
     5203                                for img in range(Nimages): modexpimg[img] = owdirme +\
     5204                                  '/' + modexpimg[img]
     5205                                caption = mod + ' ' + Sexps + ' ' + plot + ' ' + op +\
     5206                                  ' diff' + diffn
     5207                                flab = 'fig:' + caption.replace(' ', '_')
     5208                                try:
     5209                                    with gen.Capturing() as output:
     5210                                        gen.latex_fig_array(modexpimg, otexf,        \
     5211                                          gen.latex_text(caption), flab, refsize=0.5,\
     5212                                          dist='sqr', dorest='center')
     5213                                except:
     5214                                    print errmsg
     5215                                    print 'gen.latex_fig_array('+modexpimg+', '+     \
     5216                                      otexf+', gen.latex_text(' + caption + '), ' +  \
     5217                                      flab + ", refsize=0.5, dist='sqr', " +         \
     5218                                      "dorest='center')"
     5219                                    for s1out in output: print s1out
     5220                                    quit(-1)
     5221
     5222                                otexf.write('%\\clearpage\n')
     5223print '    mods-exps diffs ...'
     5224for imod1 in range(0,Nmods-1):
     5225    mod1 = mods[imod1]
     5226    exps1 = modexps[mod1]
     5227    for imod2 in range(imod1+1,Nmods):
     5228        mod2 = mods[imod2]
     5229        exps2 = modexps[mod2]
     5230
     5231        Smods = mod2 + '-' + mod1
     5232        Nexps1 = len(exps1)
     5233        Nexps2 = len(exps2)
     5234        for iexp1 in range(0,Nexps1):
     5235            exp1 = exps1[iexp1]
     5236            for iexp2 in range(0,Nexps2):
     5237                exp2 = exps2[iexp2]
     5238                Sexps = exp2 + '-' + exp1
     5239                print '      ' + Smods + ' ' + Sexps + ' ...'
     5240                owdirme = owdir + '/' + Smods + '/' + Sexps
     5241                for diffn in ['op', 'var']:
     5242                    fileplotinf = owdirme + '/diff' + diffn + 'plotsdraw.inf'
     5243                    if os.path.isfile(fileplotinf):
     5244                            otexf.write('% '+Smods + ' ' + Sexps + ' diff'+diffn+'\n')
     5245                            otexf.write('\\modexp{' + gen.latex_text(Smods+' '+Sexps+\
     5246                              ' diff'+diffn) + '}\n')
     5247                            listplots, Nplt = read_plot_file(fileplotinf)
     5248                            for plot in listplots.keys():
     5249                                plots = listplots[plot]
     5250                                ops = []
     5251                                for plt in plots:
     5252                                    opn = plt.split('#')[0].split('|')[1]
     5253                                    if not gen.searchInlist(ops,opn): ops.append(opn)
     5254                                for op in ops:
     5255                                    opS = op.replace('+','-')
     5256                                    modexpimg = gen.files_folder_HMT(owdirme,        \
     5257                                      head=plot, middle=opS, tail=cnf['kindfig'])
     5258                                    Nimages = len(modexpimg)
     5259                                    if Nimages > 0:
     5260                                        for img in range(Nimages): modexpimg[img] =  \
     5261                                          owdirme + '/' + modexpimg[img]
     5262                                        caption = Smods + ' ' + Sexps + ' ' + plot + \
     5263                                          ' ' + op + ' diff' + diffn
     5264                                        flab = 'fig:' + caption.replace(' ', '_')
     5265                                        try:
     5266                                            with gen.Capturing() as output:
     5267                                                gen.latex_fig_array(modexpimg, otexf,\
     5268                                                  gen.latex_text(caption), flab,     \
     5269                                                  refsize=0.5, dist='sqr',           \
     5270                                                  dorest='center')
     5271                                        except:
     5272                                            print errmsg
     5273                                            print 'gen.latex_fig_array('+modexpimg+  \
     5274                                              ', '+otexf+', gen.latex_text(' +       \
     5275                                              caption + '), '+flab+ ", refsize=0.5,"+\
     5276                                              " dist='sqr', dorest='center')"
     5277                                            for s1out in output: print s1out
     5278                                            quit(-1)
     5279                                        otexf.write('%\\clearpage\n')
     5280
     5281# allmodexp
     5282owdirme = owdir + '/allmodexps'
     5283print '    allmodexps...'
     5284fileplotinf = owdirme + '/allmodexpfigures.inf'
     5285if os.path.isfile(fileplotinf):
     5286    otexf.write('% allmodexps\n')
     5287    otexf.write('\\modexp{allmodexps}\n')
     5288    listplots, Nplt = read_plot_file(fileplotinf)
     5289    for plot in listplots.keys():
     5290        plots = listplots[plot]
     5291        ops = []
     5292        for plt in plots:
     5293            opn = plt.split('|')[1]
     5294            if not gen.searchInlist(ops,opn): ops.append(opn)
     5295        for op in ops:
     5296            opS = op.replace('+','-')
     5297            modexpimg = gen.files_folder_HMT(owdirme, head=plot, middle=opS,         \
     5298              tail=cnf['kindfig'])
     5299            Nimages = len(modexpimg)
     5300            if Nimages > 0:
     5301                for img in range(Nimages): modexpimg[img] = owdirme + '/' +          \
     5302                    modexpimg[img]
     5303                caption = 'allmodexps ' + plot + ' ' + op + ' diff' + diffn
     5304                flab = 'fig:' + caption.replace(' ', '_')
     5305                try:
     5306                    with gen.Capturing() as output:
     5307                        gen.latex_fig_array(modexpimg,otexf, gen.latex_text(caption),\
     5308                          flab, refsize=0.5, dist='sqr', dorest='center')
     5309                except:
     5310                    print errmsg
     5311                    print 'gen.latex_fig_array('+modexpimg+', '+otexf+               \
     5312                      ', gen.latex_text(' + caption + '), ' +flab+ ", refsize=0.5," +\
     5313                      " dist='sqr', dorest='center')"
     5314                    for s1out in output: print s1out
     5315                    quit(-1)
     5316
     5317                otexf.write('%\\clearpage\n')
     5318
     5319otexf.write('\\end{document}\n')
     5320otexf.close()
     5321
     5322try:
     5323    with gen.Capturing() as output:
     5324        ins = 'pdflatex ' + owdir + '/' + texout
     5325        sout = sub.call(ins, shell=True)
     5326except:
     5327    print errormsg
     5328    print '  ' + ins
     5329    print sout
     5330    for s1out in output: print s1out
     5331    quit(-1)
     5332
     5333with gen.Capturing() as output:
     5334    sout = sub.call(ins + '>& /dev/null', shell=True)
     5335with gen.Capturing() as output:
     5336    sout = sub.call(ins + '>& /dev/null', shell=True)
     5337with gen.Capturing() as output:
     5338    sout = sub.call(ins + '>& /dev/null', shell=True)
     5339with gen.Capturing() as output:
     5340    sout = sub.call(ins, shell=True)
     5341if dbg:
     5342    print sout
     5343    for s1out in output: print s1out
     5344
     5345sub.call('evince ' + owdir + '/' + texout + '.pdf &', shell=True)
     5346
Note: See TracChangeset for help on using the changeset viewer.