source: lmdz_wrf/trunk/tools/model_graphics.py @ 1347

Last change on this file since 1347 was 1345, checked in by lfita, 8 years ago

Fixing wrong 2nd line-characterisitcs in `2lines'

File size: 209.1 KB
Line 
1# Python script to generate the files and the plots for a sensitivity study with multiple modle configurations and models
2#   L. Fita
3#   LMD, Jussieu/Palaisseau, France
4#  Script configuration get from ASCII file 'model_graphics.dat'
5
6import numpy as np
7import nc_var_tools as ncvar
8import generic_tools as gen
9import drawing_tools as drw
10import time as tim
11#  To avoid errors like: '/bin/sh: 1: Syntax error: Bad fd number'
12#    Make sure that /bin/sh directs to bash and not to dash:
13#  http://stackoverflow.com/questions/15809060/sh-syntax-error-bad-fd-number
14import subprocess as sub
15import os
16
17main = 'model_graphics.py'
18
19errmsg = ncvar.errormsg
20warnmsg = ncvar.warnmsg
21
22def verify_configuration(ExpConf, debug):
23    """ Function to verify mandatory keys from experiment configuration
24      ExpConf= dictionary with the configuration
25    """
26    fname = 'verify_configuration'
27 
28    # List with the mandatory keys
29    mandatorykeys = ['pyHOME', 'cdoHOME', 'scratch', 'filescratch', 'figscratch',    \
30      'diffscratch', 'figdiffscratch', 'figallmodexpscratch', 'addfiles',            \
31      'addfigures', 'adddiffs', 'adddifffigures', 'addallmodexpfigures', 'debug',    \
32      'models', 'modgraphchar', 'expgraphchar', 'ifold', 'ofold', 'warnmsg',         \
33      'errmsg', 'titleoperations', 'kindfig', 'CFreftime', 'CFunitstime', 'mapval',  \
34      'timekind', 'timefmt', 'timelabel' ]
35
36    # List with the optional keys
37    optionalkeys = ['specificvarplot', 'specificdiffopplot', 'specificdiffvarplot',  \
38      'RefProj']
39
40    # Dictionary with the mandatory keys which are required as function of a given
41    #   key from the dictionary
42    #     if [key] must exist [keyB] = [values]
43    ifkeys = {'WRFexps': ['models','WRF'], 'LMDZexps': ['models','LMDZ'],            \
44      'WRF_LMDZexps': ['models','WRF_LMDZ'], 'DYNAMICOexps': ['models','DYNAMICO'],  \
45      'WRFheaders': ['models','WRF'], 'WRF_LMDZheaders': ['models','WRF_LMDZ'],      \
46      'LMDZheaders': ['models','LMDZ'], 'DYNAMICOheaders': ['models','DYNAMICO']}
47
48    # Dictionary with the optional keys (at least one) which are required as function
49    #   of a given key from the dictionary
50    #     if [key] must exist at least one of the [keyA, keyB, keyC, ...] with values
51    ifopkeys = {'RefProj': ['reprojectvar_remapbil', 'reprojectvar_remapbic',        \
52      'reprojectvar_remapdis', 'reprojectvar_remapnn', 'reprojectvar_remapcon',      \
53      'reprojectvar_remapcon2', 'reprojectvar_remaplaf', 'reprojectvar_dis',         \
54      'reprojecvar_dis']}
55
56    for kdict in mandatorykeys:
57        if not ExpConf.has_key(kdict):
58            print errmsg
59            print '  ' + fname + "' configuration without required '" + kdict + "' !!"
60            quit(-1)
61        if debug:
62            print ExpConf[kdict]
63
64    for kdict in ifkeys.keys():
65        vals = ifkeys[kdict]
66        keyn = vals[0]
67        keyv = vals[1]
68        if type(ExpConf[keyn]) == type('A'):
69            if not ExpConf.has_key(kdict) and ExpConf[keyn] == keyv:
70                print errmsg
71                print '  ' + fname + "' configuration without '" + kdict +           \
72                  "' when it is required with configuration '" + keyn + '=' + keyv + \
73                  "' !!"
74                quit(-1)
75        elif type(ExpConf[keyn]) == type(['A', 'B']):
76            keyvals = ExpConf[keyn]
77            if not ExpConf.has_key(kdict) and gen.searchInlist(keyvals, keyv):
78                print errmsg
79                print '  ' + fname + "' configuration without '" + kdict +           \
80                  "' when it is required with configuration '" + keyn + '= [..., ' + \
81                 keyv + ", ...]' !!"
82                quit(-1)
83        else:
84            print errmsg
85            print '  ' +fname+ 'dictionary type ', type(ExpConf[keyn]), ' not ready!!'
86            quit(-1)
87        if debug:
88            print '  Experiments configuration _______'
89            gen.printing_dictionary(ExpConf)
90
91    for kdict in ifopkeys.keys():
92        if ExpConf.has_key(kdict):
93            opkeys = ifopkeys[kdict]
94            hasopkey = False
95            opdictvals = {}
96            for opkv in opkeys:
97                if ExpConf.has_key(opkv):
98                    hasopkey = True
99                    opdictvals[opkv] = ExpConf[opkv]
100
101            if not hasopkey:
102                print errmsg
103                print '  ' + fname + "' configuration wit '" + kdict + "' but " +    \
104                  "without any key value:", opkeys, "' when it is required !!"
105                quit(-1)
106
107            if debug:
108                print "  Optional key '" + kdict + "' with values ________"
109                gen.printing_dictionary(opdictvals)
110
111    print fname + ': configuration seems to be fine'
112
113    return
114
115def scratches(config):
116    """ Function to set-up if it is needed to start from the scratch
117      config: dictionary with the configuration
118    """
119    fname = 'scratches'
120
121    if config['scratch'] == 'true':
122        scr = True
123        print warnmsg
124        print "  " + fname + ": starting from the SCRATCH !!"
125        print "    10 seconds left!!"
126        filescr = True
127        figscr = True
128        difscr = True
129        figdifscr = True
130        moddifscr = True
131        figmoddifscr = True
132        figallmodexpscr = True
133        tim.sleep(10)
134    else:
135        scr = False
136        if config['filescratch'] == 'true':
137            filescr = True
138            print warnmsg
139            print "  " + main + ": files starting from the SCRATCH !!"
140            print "    5 seconds left!!"
141            tim.sleep(5)
142        else:
143            filescr = False
144
145        if config['figscratch'] == 'true':
146            figscr = True
147            print warnmsg
148            print "  " + main + ": figures starting from the SCRATCH !!"
149            print "    5 seconds left!!"
150            tim.sleep(5)
151        else:
152            figscr = False
153
154        if config['diffscratch'] == 'true':
155            difscr = True
156            print warnmsg
157            print "  " + main + ": experiment differences starting from the SCRATCH !!"
158            print "    5 seconds left!!"
159            tim.sleep(5)
160        else:
161            difscr = False
162
163        if config['figdiffscratch'] == 'true':
164            figdifscr = True
165            print warnmsg
166            print "  " + main + ": figures experiment differences starting from the SCRATCH !!"
167            print "    5 seconds left!!"
168            tim.sleep(5)
169        else:
170            figdifscr = False
171
172        if config['moddiffscratch'] == 'true':
173            moddifscr = True
174            print warnmsg
175            print "  " + main + ": model differences starting from the SCRATCH !!"
176            print "    5 seconds left!!"
177            tim.sleep(5)
178        else:
179            moddifscr = False
180
181        if config['figmoddiffscratch'] == 'true':
182            figmoddifscr = True
183            print warnmsg
184            print "  " + main + ": figures model differences starting from the SCRATCH !!"
185            print "    5 seconds left!!"
186            tim.sleep(5)
187        else:
188            figmoddifscr = False
189
190        if config['figallmodexpscratch'] == 'true':
191            figallmodexpscr = True
192            print warnmsg
193            print "  " + main + ": figures all model-experiment starting from the SCRATCH !!"
194            print "    5 seconds left!!"
195            tim.sleep(5)
196        else:
197            figallmodexpscr = False
198
199    if config['addall'] == 'true':
200        print warnmsg
201        print "  " + fname + ": adding new values everywhere !!"
202        addfils = True
203        addfigs = True
204        adddiffs = True
205        adddifffigs = True
206        addmoddiffs = True
207        addmoddifffigs = True
208        addallmodexpfigs = True
209    else:
210        if config['addfiles'] == 'true':
211            addfils = True
212        else:
213            addfils = False
214
215        if config['addfigures'] == 'true':
216            addfigs = True
217        else:
218            addfigs = False
219
220        if config['adddiffs'] == 'true':
221            adddiffs = True
222        else:
223            adddiffs = False
224
225        if config['adddifffigures'] == 'true':
226            adddifffigs = True
227        else:
228            adddifffigs = False
229
230        if config['addmoddiffs'] == 'true':
231            addmoddiffs = True
232        else:
233            addmoddiffs = False
234
235        if config['addmoddifffigures'] == 'true':
236            addmoddifffigs = True
237        else:
238            addmoddifffigs = False
239
240        if config['addallmodexpfigures'] == 'true':
241            addallmodexpfigs = True
242        else:
243            addallmodexpfigs = False
244
245    if config['debug'] == 'true':
246        debug = True
247    else:
248        debug = False
249
250    return scr, filescr, figscr, difscr, figdifscr, moddifscr, figmoddifscr,         \
251      figallmodexpscr, addfils, addfigs, adddiffs, adddifffigs, addmoddiffs,         \
252      addmoddifffigs, addallmodexpfigs, debug
253
254
255class gc(object):
256    """ Class which holds all the graphical characteristics
257      [label]: label as it should appear in the graphics
258      [color]: specific color of the model
259      [linetype]: specific type of line of the model
260      [marker]: specific marker of the model
261      [sizes]: line width and point size for the model
262      [tmod]: time modification to apply to the model files (None for nothing)
263        'setorigin',[YYYYMMDDHHMISS]: re-set origin of times at [YYYYMMDDHHMISS]
264    """
265    def __init__( self, label, color, linetype, marker, sizes, Tmod):
266        self.label = None
267        self.color = None
268        self.linetype = None
269        self.marker = None
270        self.sizes = None
271        self.tmod = None
272        if label is not None:
273            self.label = label
274            self.color = color
275            self.linetype = linetype
276            self.marker = marker
277            self.sizes = sizes
278            if Tmod != 'None':
279                self.tmod = Tmod
280
281def graphical_characteristics(config, debug):
282    """ Function to provide the graphical characteristics of models and experiments
283      config= configuration of the experiment
284    """
285    fname = 'graphical_characteristics'
286
287    modgraphchar = {}
288    modvals = config['modgraphchar'].split(':')
289    for modval in modvals:
290        modv = modval.split('|')
291        modgraphchar[modv[0]] = gc(modv[1], modv[2], modv[3], modv[4], modv[5], modv[6])
292
293    expgraphchar = {}
294    expvals = config['expgraphchar'].split(':')
295    for expval in expvals:
296        expvs = expval.split('|')
297        modexp = expvs[0] + '/' + expvs[1]
298        if not modgraphchar.has_key(expvs[0]):
299            print errmsg
300            print '  ' + fname + ": a model called '" +expvs[0]+ "' does not exist !!"
301            print '    existing models with graphic characteristics:',               \
302              modgraphchar.keys()
303            quit(-1)
304
305        modvs = modgraphchar[expvs[0]]
306        expv = expvs[2:7]
307        if expv[1] == 'asmodel': expv[1] = modvs.color
308        if expv[2] == 'asmodel': expv[2] = modvs.linetype
309        if expv[3] == 'asmodel': expv[3] = modvs.marker
310        if expv[4] == 'asmodel': expv[4] = modvs.sizes
311
312        expgraphchar[modexp] = gc(expv[0], expv[1], expv[2], expv[3], expv[4], 'None')
313
314    # Running some tests
315    mods = config['models'].split(':')
316    for mod in mods:
317        if not modgraphchar.has_key(mod):
318            print errmsg
319            print '  ' + fname + ": model called '" + mod + "' does not have " +     \
320              "graphical characteristics !!"
321            print '    existing models with graphic characteristics:',               \
322              modgraphchar.keys()
323            quit(-1)
324
325        exps = config[mod + 'exps'].split(':')
326        for exp in exps:
327            modexp = mod + '/' + exp
328            if not expgraphchar.has_key(modexp):
329                print errmsg
330                print '  ' + fname + ": model/experiment called '" + modexp +        \
331                  "' does not have graphical characteristics !!"
332                print '    existing model/experiments with graphic ' +               \
333                  'characteristics:', expgraphchar.keys()
334                quit(-1)
335
336    if debug:
337        print '  ' + fname + ': model graphical characteristics _______'
338        for modn in modgraphchar.keys():
339            with gen.Capturing() as output:
340                gen.printing_class(modgraphchar[modn])
341            print "'" + modn + "'; " + ', '.join(output)
342        print '  ' + fname + ': experiment graphical characteristics _______'
343        for modexpn in expgraphchar.keys():
344            with gen.Capturing() as output:
345                gen.printing_class(expgraphchar[modexpn])
346            print "'" + modexpn + "'; " + ', '.join(output)
347
348    return modgraphchar, expgraphchar
349
350def exp_headers(mod,config):
351    """ Function to provide the headers and the experiments of a given model
352      model= model
353      config= configuration of the experiment
354    """
355    fname = 'exp_headers'
356
357    # No CASE in python!
358    #  case ${mod} in ...
359    if mod == 'WRF':
360        expers = config['WRFexps'].split(':')
361        fhs = config['WRFheaders'].split(':')
362    elif mod == 'LMDZ':
363        expers = config['LMDZexps'].split(':')
364        fhs = config['LMDZheaders'].split(':')
365    elif mod == 'WRF_LMDZ':
366        expers = config['WRF_LMDZexps'].split(':')
367        fhs = config['WRF_LMDZheaders'].split(':')
368    elif mod == 'DYNAMICO':
369        expers = config['DYNAMICOexps'].split(':')
370        fhs = config['DYNAMICOheaders'].split(':')
371    else:
372        print errmsg
373        print "  " + fname + ": model '" + mod + "' not ready!!"
374        quit(-1)
375
376    return expers, fhs
377
378class VariableInf(object):
379    """ Class which holds all information of a given variable
380      name= CF name of the variabgle
381      header= header of the file from which it can be computed
382      model= file-header variable from which it can be directly computed
383      diag= file-header diagnostics from which it can be computed
384    """
385    def __init__( self, name, fheader, model, diag):
386       self.name= None
387       self.fheader= None
388       self.model= None
389       self.diag= None
390       if name is not None:
391           self.name = name
392           self.fheader= fheader
393           self.model= model
394           self.diag= diag
395
396class ModelInf(object):
397    """ Class which holds all information from a given model
398      name= Acronym of the model
399      model= Long description of the model
400      dim[x/y/z/t/s]n= name of the dimensions of the model
401      vdim[x/y/z/t/s]n= name of the variable-dimensions of the model
402      dimensions= list of the dimensions
403      vardimensions= list of the variable-dimensions
404      timemodif= time modification to apply to the model files ('None' for nothing)
405        'setorigin',[YYYYMMDDHHMISS]: re-set origin of times at [YYYYMMDDHHMISS]
406    """
407    def __init__( self, name, model, dx, dy, dz, dt, ds, vdx, vdy, vdz, vdt, vds,    \
408      tmod):
409        self.name= None
410        self.model= None
411        self.dimxn= None
412        self.dimyn= None
413        self.dimzn= None
414        self.dimtn= None
415        self.dimsn= None
416        self.vardxn= None
417        self.vardyn= None
418        self.vardzn= None
419        self.vardtn= None
420        self.vardsn= None
421        self.timemodif = None
422        if name is not None:
423            self.name = name
424            self.model= model
425            self.dimxn= dx
426            self.dimyn= dy
427            self.dimzn= dz
428            self.dimtn= dt
429            self.dimsn= ds
430            self.vardxn= vdx
431            self.vardyn= vdy
432            self.vardzn= vdz
433            self.vardtn= vdt
434            self.vardsn= vds
435            if tmod is not None:
436                self.timemodif = tmod
437            self.dimensions = [dx, dy, dz, dt, ds]
438            self.vardimensions = [vdx, vdy, vdz, vdt, vds]
439
440def variable_compute(idir,var,ftests,db):
441    """ Function to retrieve the computation way of a given variable using a series of test files
442      iwdir= directory with the test files
443      var= name of the variable to test
444      filtests= dictionary with the test files for each header
445    """
446    fname='variable_compute'
447
448    cancompute = True
449    for headerf in ftests.keys():
450        filen=ftests[headerf]
451        try:
452            with gen.Capturing() as output:
453                vmod, vdiag = ncvar.computevar_model(var, idir + '/' + filen)
454        except:
455            print errmsg
456            print 'ncvar.computevar_model(' + var + ', ' + idir + '/' + filen + ')'
457            for s1out in output: print s1out
458            quit(-1)
459
460        if vmod is None and vdiag is None:
461            cancompute = False
462        else:
463            cancompute = True
464            # Should be considered that variable can also be computed by both ways?
465            break
466
467    if not cancompute:
468        print warnmsg
469        print '  ' + fname + ": there is no way to compute '" + var +                \
470          "' for model '" + mod
471# Too extrict!
472#        quit(-1)
473
474    # Getting only the first possibility of model and diagnostic
475    if vmod is not None:
476        modV = vmod[0]
477    else:
478        modV = None
479    if vdiag is not None:
480        diagV = vdiag[0]
481    else:
482        diagV = None
483
484    varcomp = VariableInf(var, headerf, modV, diagV)
485
486    # a ';' list 'varcompute' it is created for each variable giving:
487    #   [var]|[vark]|[headerf][varmod]|[vardiag]
488    # This list will be used to compute a new file for each variable
489
490    if db:
491        print 'Variable information _______'
492        gen.printing_class(varcomp)
493
494    return varcomp
495
496def get_operations_var(vc,db):
497    """ Function to provide the operations to make for each variable from 'VAR_' dictionary
498      vc= 'VAR_' dictionary, dictionary with all the parameters started with 'VAR_'
499    """
500    fname = 'get_operations_var'
501
502    LH = len('VAR_')
503    iop = 1
504    # list of operations
505    doopers = {}
506    # operations by variable
507    operationsvar = {}
508    # individual operations by variable (not repeated)
509    indivoperationsvar = {}
510
511    # Variables with a calculation whic requires vertical interpolation to all the
512    #   file (operations stating by 'VAR_pinterp')
513    LVp = len('VAR_pinterp')
514    varglobalp = []
515
516    for oper in vc.keys():
517        Loper = len(oper)
518        opn = oper[LH:Loper+1]
519        vns = vc[oper].split(':')
520        ops1 = opn.split('+')
521        doopers[opn] = ops1
522        for vn in vns:
523            if not operationsvar.has_key(vn):
524                operationsvar[vn] = [opn]
525                indivoperationsvar[vn] = ops1
526            else:
527                opers = operationsvar[vn]
528                opers.append(opn)
529                operationsvar[vn] = opers
530                opers1 = indivoperationsvar[vn]
531                for op1 in ops1:
532                    if not gen.searchInlist(opers1, op1):
533                        opers1.append(op1)
534                indivoperationsvar[vn] = opers1
535
536        if oper[0:LVp] == 'VAR_pinterp':
537            for vn in vns:
538                if not gen.searchInlist(varglobalp,vn): varglobalp.append(vn)
539
540        iop = iop + 1
541
542    if db:
543        print '  operations to make _______'
544        gen.printing_dictionary(doopers)
545        print '  operations by variable _______'
546        gen.printing_dictionary(operationsvar)
547        print '  individual operations by variable _______'
548        gen.printing_dictionary(indivoperationsvar)
549        print '  variables with the vertical interpolation for all the file _______'
550        print '    #', varglobalp
551
552    return doopers, operationsvar, indivoperationsvar, varglobalp
553
554def pinterp_var(oper):
555    """ Function to retrieve characteristics of the vertical interpolation for the operation
556      oper= operation
557        Wether vertical interpolation is:
558          # 'global': for all file
559          # 'local': at a given step of the process
560          # 'none': no vertical interpolation
561    >>> pinterp_var('last+pinterp+xmean')
562    local
563    >>> pinterp_var('pinterp+tmean+xmean')
564    global
565    >>> pinterp_var('xmean')
566    none
567    """
568    fname = 'pinterp_var'
569
570    pinn = 'pinterp'
571
572    Lpinterp = len(pinn)
573    if oper[0:Lpinterp] == pinn: 
574        pkind = 'global'
575        return pkind
576
577    if oper.find(pinn) != -1:
578        pkind = 'local'
579    else:
580        pkind = 'none'
581       
582    return pkind
583
584def compvars_listconstruct(config, minf, Files, TestFiles, idir, odir, debug):
585    """ Function to construct the list of variables to compute
586      config= dictionary with the configuration of the execution
587         variables to compute are get with all values started by `VAR_' in 'module_graphics.dat', and then
588           providing a consecutive number of calculations separated by '+'
589             VAR_[calc1]+[calc2] = tas:wss
590           will compute first [calc1] and then [calc2] for 'tas' and 'wss'
591      minf= class with information about the model
592      Files= dictionary of files for heach header
593      TestFiles= dictionary of files to test how to compute the variable
594      odir= output directory
595    """
596    fname='compvars_listconstruct'
597
598    varcomp = gen.get_specdictionary_HMT(config,H='VAR_')
599
600    if debug:
601        print '  variables to compute ________'
602        gen.printing_dictionary(varcomp)
603
604    # Getting operations by variable
605    opers, varoper, indivaroper, varglobp = get_operations_var(varcomp,debug)   
606
607    strfiles = gen.dictKeysVals_stringList(Files)
608    strtestfiles = gen.dictKeysVals_stringList(TestFiles)
609
610    Svarcompute = ''
611
612    # Main dictionary with all the variables and their calculation
613    allvarcomp = {}
614
615    ivop = 0
616    for vn in varoper:
617        vcomp = variable_compute(idir,vn,TestFiles,False)
618        for op in varoper[vn]:
619            # Creation of a String as: [CFname]|[operation]|[fheader]|[vmodel]|[vdiag]|[globalP]
620            #   [CFname]: CF-name of the variable (must appear in 'variables_values.dat')
621            #   [operation]: operation to compute
622            #   [fheader]: header of file with the required variables
623            #   [vmodel]: Variable from model output
624            #   [vdiag]: Varible as diagnostic from different variables of the model output
625            #   [globalP]: Wether vertical interpolation is:
626            #     'global': for all file
627            #     'local': at a given step of the process
628            #     'none': no vertical interpolation
629            globalP = pinterp_var(op)
630            if vcomp.model is not None:
631                if type(vcomp.model) == type(list([1, 2])):
632                    Smodel = ':'.join(vcomp.model)
633                elif type(vcomp.model) == type('A'):
634                    Smodel = vcomp.model
635            else:
636                Smodel = 'None'
637            if vcomp.diag is not None:
638                if type(vcomp.diag) == type(list([1, 2])):
639                    Sdiag = ':'.join(vcomp.diag)
640                elif type(vcomp.diag) == type('A'):
641                    Sdiag = vcomp.diag
642            else:
643                Sdiag = 'None'
644
645            # To get a combination of operations, all the precedent steps are kept
646            #   thus, they are also available !
647            seqops = op.split('+')
648            for seqop in seqops:
649                if seqop == seqops[0]: seqopS=seqop
650                else: seqopS = seqopS + '+' + seqop
651                allvarcomp[vn+'_'+seqopS] = [vcomp.fheader, vcomp.model, vcomp.diag, \
652                  globalP]
653
654                Svc = vcomp.name + '|' + seqopS + '|' + vcomp.fheader + '|' +        \
655                  Smodel + '|' + Sdiag + '|' + globalP
656                if ivop == 0:
657                    Svarcompute = Svc
658                else:
659                    Svarcompute = Svarcompute + ',' + Svc
660
661                ivop = ivop + 1
662
663    if debug:
664        print '  Variables to compute _______'
665        gen.printing_dictionary(allvarcomp)
666
667    # Outwritting the varcompute to avoid next time (if it is not filescratch!)
668    objf = open(odir + '/varcompute.inf', 'w')
669    objf.write('files: ' + strfiles + '\n')
670    objf.write('testfiles: ' + strtestfiles + '\n')
671    objf.write('varcompute: ' + Svarcompute + '\n')
672    objf.write('itotv: ' + str(ivop) + '\n')
673    objf.close() 
674
675    return allvarcomp, ivop
676
677def read_varcomp_file(filen):
678    """ Function to read 'varcompute.inf' and reconstruct the dictionaries
679    """
680    fname = 'read_varcomp_file'
681
682    allvarcomp = {}
683   
684    objf = open(filen, 'r')
685
686    for line in objf:
687        vals = line.replace('\n','').split(' ')
688        if vals[0] == 'files:':
689            Files = gen.stringList_dictKeysVals(vals[1])
690        elif vals[0] == 'testfiles:':
691            TestFiles = gen.stringList_dictKeysVals(vals[1])
692        elif vals[0] == 'varcompute:':
693            for VvalsS in vals[1].split(','):
694               
695                Vvals = VvalsS.split('|')
696                if Vvals[3] == 'None':
697                    mod = None
698                else:
699                    mod = Vvals[3].split(':')
700                if Vvals[4] == 'None':
701                    diag = None
702                else:
703                    diag = Vvals[4].split(':')
704
705                allvarcomp[Vvals[0]+'_'+Vvals[1]] = [Vvals[2], mod, diag, Vvals[5]]
706
707#                # To get a combination of operations, all the precedent steps are kept
708#                #   thus, they are also available !
709#                seqops = Vvals[1].split('+')
710#                for seqop in seqops:
711#                    if seqop == seqops[0]: seqopS=seqop
712#                    else: seqopS = seqopS + '+' + seqop
713#                    allvarcomp[Vvals[0]+'_'+seqopS] = [Vvals[2], mod, diag, Vvals[5]]
714        elif vals[0] == 'itotv:':
715            ivop = int(vals[1])
716
717    objf.close()
718
719    return Files, TestFiles, allvarcomp, ivop
720
721def pinterpS(gP):
722    """ For that variables which require vertical interpolation 'p' suffix to the file header is added
723      gP= kind of pinterp: 'global', 'local', 'none'
724    """
725    fname = 'pinterpS'
726
727    if gP != 'none':
728        gPS = 'p'
729    else:
730        gPS = ''
731
732    return gPS
733
734def compute_variable(minf, idir, usefiles, odir, cvar, gP, scr, pyH, Tref, Tunits, db):
735    """ Function to compute a variable
736      minf= class with the information of the model
737      idir= directory with the input files
738      usefiles= dictionary of files as dict([headerf]) = [file1], ..., [fileN]
739      odir= directory to write the output files
740      cvar= class with the information of the variable: 'name', 'fheader', 'varmod', 'vardiag'
741      gP= kind of vertical interpolation ('global', 'local', 'none')
742      scr= should it be done from the scratch?
743      pyH= location of the python HOME
744      Tref= CF time reference
745      Tunits= CF time units
746    """
747    fname='compute_variable'
748
749    CFvarn=cvar.name
750    headerf = cvar.fheader
751    modvar = cvar.model
752    diagvar = cvar.diag
753
754    cfiles = usefiles[headerf]
755
756    # dimensions
757    dnx = minf.dimxn
758    dny = minf.dimyn
759    # var-dimensions
760    vdnx = minf.vardxn
761    vdny = minf.vardyn
762
763    # Computing separately and then joinging for all files
764    Ntotfiles = len(cfiles)
765    Nzeros = len(str(Ntotfiles))
766    NStot = str(Ntotfiles).zfill(Nzeros)
767    # For that variables which require vertical interpolation 'p' suffix to the
768    #   file header is added
769    SgP = pinterpS(gP)
770
771    # Getting in working dir
772    os.chdir(odir)
773
774    # File to keep track of all operations
775    otrackf = open( odir + '/all_computevars.inf', 'a')
776
777    # Computing variable
778    ifile=1
779    for cf in cfiles:
780        ifS = str(ifile).zfill(Nzeros)
781        ifilen = odir + '/' + CFvarn + '_' + headerf + SgP + '_' + ifS + '-' +       \
782          NStot + '.nc'
783        fileon = odir + '/' + CFvarn + '_' + headerf + SgP + '.nc'
784
785        if scr:
786            sout = sub.call('rm ' + ifilen + ' >& /dev/null', shell=True)
787            sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
788
789        if not os.path.isfile(ifilen) and not os.path.isfile(fileon):
790# Since model direct values are retrieved from `variables_valules.dat' which was
791#   initially coincived as a way to only give variable attributes, range and color
792#   bars, if a variable has a diagnostic way to be computed, the later one will be
793#   preferred
794            if db:
795                print '  ' + fname + ": creation of variable file '" + CFvarn +      \
796                  "' in file '" + ifilen + "' ..."
797                print '    model variable:', modvar
798                print '    diagostic variable:', diagvar
799
800            if diagvar is None:
801                # model variable
802                if db: print '  '+fname+ ": variable direct from model '" +modvar+ "'"
803                values = modvar + ',0,-1,-1'
804                vs = modvar + ',' + vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt
805
806                try:
807                    with gen.Capturing() as output:
808                        ncvar.DataSetSection_multivars(values, idir + '/' + cf, vs)
809                except:
810                    print errmsg
811                    print 'ncvar.DataSetSection_multivars('+values+', '+idir+'/'+cf+ \
812                      ', '+vs+')'
813                    for s1out in output: print s1out
814                    quit(-1)
815
816                newfile, loc = gen.search_sec_list(output,'succesfull')
817                ofile = newfile[0].split(' ')[7]
818                sub.call('mv ' + ofile + ' ' + ifilen, shell=True)
819
820                # Keeping track of the operations
821                pyins = pyH + "/nc_var.py -f " + idir + '/' + cf +                   \
822                  " -o DataSetSection_multivars -v " + vs + " -S '" + values + "'"
823                otrackf.write('\n')
824                otrackf.write('# ' + CFvarn + " " + modvar + '\n')
825                otrackf.write('python ' + pyins + '\n')
826
827                # CF renaming of variable
828                ncvar.chvarname(CFvarn,ifilen,modvar)
829            else:
830                # diagnostic variable
831                if db: print '  '+fname+ ": variable as diagnostic '" +diagvar[0]+ "'"
832                dims = dnt+'@'+vdnt+','+dnz+'@'+vdnz+','+dny+'@'+vdny+','+dnx+'@'+vdnx
833                diagn = diagvar[0]
834                Ndiagvars = len(diagvar)
835                diagc = '@'.join(diagvar[1:])
836           
837                values = '-f ' + idir + '/' + cf + " -d '" + dims + "' -v '" +       \
838                  diagn + '|' + diagc + "'"
839                try:
840                    with gen.Capturing() as output:
841                        sout = sub.call('python ' +pyH+ '/diagnostics.py ' + values, \
842                          shell=True)
843                except:
844                    print errmsg
845                    print 'python ' + pyH + '/diagnostics.py ' + values
846                    for s1out in output: print s1out
847                    quit(-1)
848                if db:
849                    for s1out in output: print s1out
850
851                sout = sub.call(' mv diagnostics.nc ' + ifilen, shell=True)
852
853                # Keeping track of the operations
854                pyins = 'python ' + pyH + '/diagnostics.py ' + values
855                otrackf.write('\n')
856                otrackf.write('# ' + CFvarn + " " + diagn + '\n')
857                otrackf.write(pyins + '\n')
858
859            # Attaching necessary variables for the pressure interpolation
860            if gP != 'none':
861                if minf.name == 'WRF' or minf.name == 'WRF_LMDZ':
862                    requiredinterpvars = ['P', 'PB', 'PSFC', 'PH', 'PHB', 'HGT', 'T',\
863                      'QVAPOR', 'XLONG', 'XLAT', 'Times']
864                elif minf.name == 'LMDZ':
865                    requiredinterpvars = ['pres', 'psol', 'geop', 'phis', 'temp',    \
866                      'ovap', 'lon', 'lat', 'time_counter']
867                elif minf.name == 'DYNAMICO':
868                    requiredinterpvars = ['pres', 'psol', 'geop', 'phis', 'temp',    \
869                      'ovap', 'lon', 'lat', 'time_counter']
870                else:
871                    print errmsg
872                    print fname + ": for model '" + minf.name + "' required " +      \
873                      " variables for vertical interpolation are not known !!"
874                    quit(-1)
875
876                print "   " + fname + ": adding variables:", requiredinterpvars,     \
877                  ' to allow pressure interpolation'
878                for rqv in requiredinterpvars:
879                    try:
880                        with gen.Capturing() as output:
881                            ncvar.fvaradd(idir+'/'+cf+','+rqv,ifilen)
882                    except:
883                        print errmsg
884                        print 'fvaradd('+idir+'/'+cf+','+rqv+', '+ifilen+')'
885                        for s1out in output: print s1out
886                        quit(-1)
887                    if db:
888                        for s1out in output: print s1out
889
890            # CFification of files
891            if minf.name == 'WRF' or minf.name == 'WRF_LMDZ':
892                values = vdnx + ':' + vdny + ':'+ Tref + ':' + Tunits
893                try:
894                    with gen.Capturing() as output:
895                        ncvar.WRF_toCF(values, ifilen) 
896                except:
897                    print errmsg
898                    print 'WRF_toCF('+ values +', ' + ifilen + ')'
899                    for s1out in output: print s1out
900                    # Removing file in order to make sure that it will be redone
901                    print '  ' + fname + " removing file '" + ifilen + "' ..."
902                    sub.call('rm ' + ifilen + ' >& /dev/null', shell=True)
903                    quit(-1)
904                if db:
905                    for s1out in output: print s1out
906            elif minf.name == 'LMDZ':
907                try:
908                    with gen.Capturing() as output:
909                        ncvar.LMDZ_toCF(Tunits + '!since!' + Tref, ifilen)
910                except:
911                    print errmsg
912                    print 'LMDZ_toCF('+ Tunits +'!since!'+ Tref + ', ' + ifilen + ')'
913                    for s1out in output: print s1out
914                    # Removing file in order to make sure that it will be redone
915                    print '  ' + fname + " removing file '" + ifilen + "' ..."
916                    sub.call('rm ' + ifilen + ' >& /dev/null', shell=True)
917                    quit(-1)
918                if db:
919                    for s1out in output: print s1out
920            # At this stage, let's be simple. It might be necessary to complicate it! Since
921            #   DYNAMICO might have 'time_counter' or 'time_instant'
922            elif minf.name == 'DYNAMICO':
923                try:
924                    with gen.Capturing() as output:
925                        ncvar.DYNAMICO_toCF(Tunits + '!since!' + Tref, ifilen)
926                except:
927                    print errmsg
928                    print 'DYNAMICO_toCF('+Tunits+'!since!'+Tref+ ', ' + ifilen + ')'
929                    for s1out in output: print s1out
930                    # Removing file in order to make sure that it will be redone
931                    print '  ' + fname + " removing file '" + ifilen + "' ..."
932                    sub.call('rm ' + ifilen + ' >& /dev/null', shell=True)
933                    quit(-1)
934                if db:
935                    for s1out in output: print s1out
936            else:
937                print errmsg
938                print '  ' + fname + ": no CFification for model '" +minf.name+ "' !!"
939                print "    available ones: 'WRF', 'WRF_LMDZ', 'LMDZ', 'DYNAMICO'"
940                quit(-1)
941
942        ifile = ifile + 1
943
944    otrackf.close()
945
946    # Joining variable files
947    if not os.path.isfile(fileon):
948        if Ntotfiles > 1:
949            if db:
950                print '  ' + fname + ": concatenating all variable files to " +      \
951                  "create '" + fileon + "..."
952            try:
953                with gen.Capturing() as output:
954                    ncvar.netcdf_fold_concatenation_HMT('./,time', CFvarn + '_' +    \
955                      headerf + SgP +'_,-,.nc', 'all')
956            except:
957                print errmsg
958                print 'netcdf_fold_concatenation_HMT(./,time, ' + CFvarn + '_' +     \
959                   headerf +SgP + '_,-,.nc, all)'
960                for s1out in output: print s1out
961                quit(-1)
962            if db:
963                for s1out in output: print s1out
964
965            sout = sub.call('mv netcdf_fold_concatenated_HMT.nc '+fileon, shell=True)
966            if os.path.isfile(fileon):
967                sout = sub.call('rm '+CFvarn + '_' + headerf + SgP + '_*-*.nc >& ' + \
968                  '/dev/null', shell=True)
969        else:
970            sout = sub.call('mv ' + CFvarn + '_' + headerf + SgP + '_1-1.nc ' +      \
971                  fileon, shell=True)
972
973    # Re-setting time of final concatenated file
974    if minf.timemodif is not None:
975        if db:
976            print '  ' + fname + ": Modifying times of the file by '" +              \
977              minf.timemodif + "' !!"
978        try:
979            with gen.Capturing() as output:
980                ncvar.time_reset(minf.timemodif, fileon,'time')
981        except:
982            print errmsg
983            print 'time_reset(' + minf.timemodif + ', ' + fileon + ', time)'
984            for s1out in output: print s1out
985            # Removing file in order to make sure that it will be redone
986            print '  ' + fname + " removing file '" + fileon + "' ..."
987            sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
988            quit(-1)
989        if db:
990            for s1out in output: print s1out
991
992    return
993
994def compute_statistics(minf, config, idir, usefiles, odir, cvar, gP, Opers, scr, db):
995    """ Function to compute different statistics it will take previous steps if they
996        are availale
997      minf= class with the information of the model
998      config= dictionary with the configuration of the experiment
999      idir= directory with the input files
1000      usefiles= ',' list of files to use [file1],...,[fileN]
1001      odir= directory to write the output files
1002      cvar= class with the information of the variable: 'name', 'fheader', 'varmod', 'vardiag'
1003      gP= kind of vertical interpolation ('global', 'local', 'none')
1004      Opers= kind of operation: (as possible multiple consecutive combination of operations separated by '+'
1005        [calc1]+[calc2] will compute first [calc1] and then [calc2]
1006        acc: temporal accumulated values
1007        diff: differences between models
1008        direct: no statistics
1009        last: last temporal value
1010        Lmean: latitudinal mean values
1011        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat])
1012        lmean: longitudinal mean values
1013        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
1014        pinterp: pressure interpolation (to the given $plevels)
1015        tmean: temporal mean values
1016        tstd: temporal standard deviation values
1017        tturb: Taylor's turbulence decomposition value (x - <x>) for time
1018        tvar: temporal variance values
1019        xmean: x-axis mean values
1020        xvar: x-axis variance values
1021        ymean: y-axis mean values
1022        zsum: vertical aggregated values
1023      scr= should it be done from the scratch?
1024    """
1025    fname='compute_statistics'
1026
1027    # Getting a previous file name to continue with(for combinations)
1028    CFvarn=cvar.name
1029    headerf = cvar.fheader
1030    modvar = cvar.model
1031    diagvar = cvar.diag
1032
1033    cfiles = usefiles[headerf]
1034
1035    # dimensions
1036    dnx = minf.dimxn
1037    dny = minf.dimyn
1038    # var-dimensions
1039    vdnx = minf.vardxn
1040    vdny = minf.vardyn
1041    # Model dimension-variable dictionary
1042    Modeldimvardict = {dnx: vdnx, dny: vdny, dnz: vdnz, dnt: vdnt}
1043
1044    # Some experiment configuration values
1045    #  plevels= ':' separated list of pressures (in Pa) to use for the vertical interpolation
1046    plevels = config['plevels']
1047    #  pyH= location of the python HOME
1048    pyH = config['pyHOME']
1049    #  Tref= CF time reference
1050    Tref = config['CFreftime']
1051    #  Tunits= CF time units
1052    Tunits = config['CFunitstime']
1053    #  opsur = operation surnames
1054    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
1055    Opsurs = {}
1056    for opk in opsur:
1057        opn = opk.split('_')[1]
1058        vals = opsur[opk]
1059        Opsurs[opn] = vals.split(':')
1060#    if db:
1061#        print '  ' + fname + ' operation surnames _______'
1062#        gen.printing_dictionary(Opsurs)
1063
1064    # Getting in working dir
1065    os.chdir(odir)
1066
1067    # File to keep track of all operations
1068    otrackf = open( odir + '/all_statsvars.inf', 'a')
1069
1070    # For that variables which require vertical interpolation 'p' suffix to the file
1071    #   header is added.
1072    # After some operations some CF dimension-variables might not be kept
1073
1074    if gP != 'none':
1075        SgP = 'p'
1076        varnCFs = ['lon', 'lat', 'pres', 'time']
1077    else:
1078        SgP = ''
1079        varnCFs = ['lon', 'lat', 'time']
1080
1081    # CF dimension-variable dictionary
1082    CFdimvardict = {'lon': 'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'}
1083
1084    # Input file
1085    ifilen = odir + '/' + CFvarn + '_' + headerf + SgP + '.nc'
1086
1087    # Computing variable statisitcs
1088    istats=0
1089
1090    # List of not computed statistics
1091    wrongstats = []
1092
1093    opers = Opers.split('+')
1094    Fopers = ''
1095    if db: print "    computing statistics of variable:'", CFvarn, "' operation '" + \
1096      Opers + "' using file '" + ifilen + "...'"
1097    for op in opers:
1098
1099        # File name from previous operations, and name of the variable within the
1100        #   file (some operations change the name of the variable)
1101        if op == opers[0]:
1102            Fopers = op
1103            prevfile = ifilen
1104            vninF = CFvarn
1105        else:
1106            Fopers = Fopers + '_' + op
1107            prevfile = fileon
1108        fileon = odir + '/' + CFvarn + '_' + headerf + SgP + '_' + Fopers + '.nc'
1109
1110        # Adding required variables for the vertical interpolation in all operations
1111        SvarnCFs = ',' + ','.join(varnCFs)
1112
1113        if gP != 'none':
1114            if gP == 'local':
1115                CFvarnp = vninF + ',P,PB,PSFC,PH,PHB,HGT,T,QVAPOR,XLONG,XLAT,Times'+ \
1116                  SvarnCFs
1117            else:
1118                CFvarnp = vninF + SvarnCFs
1119        else:
1120            CFvarnp = vninF + SvarnCFs
1121
1122        if scr:
1123            sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
1124
1125        hprevfile = prevfile[0:len(prevfile)-3]
1126
1127        if db: print '      ', op, "using '" + prevfile + "' "
1128        # Just produce file in case output file does not exist
1129        if not os.path.isfile(fileon):
1130            if db: 
1131                print '  ' + fname + ": creation of file '" + fileon + "' ..."
1132            dofile = True
1133        else:
1134            dofile = False
1135       
1136        # Variables to be kept in the final file
1137        varkeep = []
1138
1139        if op == 'acc':
1140            # temporal accumulated values
1141            print "  " + fname + ": kind '" + op + "' not ready !!"
1142            wrongstats.append(CFvarn + '_' + opers)
1143            break
1144        elif op == 'direct':
1145            # no statistics
1146            if dofile:
1147                sout = sub.call('mv ' + prevfile + ' ' + fileon, shell=True)
1148        elif op == 'last':
1149            # last temporal value
1150            vals='time,-9,0,0'
1151            if dofile:
1152                try:
1153                    with gen.Capturing() as output:
1154                        ncvar.DataSetSection(vals,prevfile)
1155                except:
1156                    print errmsg
1157                    print 'DataSetSection('+vals+',', prevfile, ')'
1158                    for s1out in output: print s1out
1159                    quit(-1)
1160
1161                if db:
1162                    for s1out in output: print s1out
1163               
1164                sout = sub.call('mv ' + hprevfile + '_time_B-9-E0-I0.nc ' + fileon, \
1165                  shell=True)
1166
1167                # Keeping the operations
1168                pyins=pyH + "/nc_var.py -o DataSetSection -S '" + vals + "' -f " +  \
1169                  prevfile
1170                otrackf.write("\n")
1171                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1172                otrackf.write(pyins + "\n")
1173
1174        elif op =='Lmean':
1175            # latitudinal mean values
1176            print "  " + fname + ": kind '" + op + "' not ready !!"
1177            wrongstats.append(CFvarn + '_' + opers)
1178            break
1179        elif op =='Lsec':
1180            # latitudinal section (latitudinal value must be given, [var]@[lat])
1181            print "  " + fname + ": kind '" + op + "' not ready !!"
1182            wrongstats.append(CFvarn + '_' + opers)
1183            break
1184        elif op =='lmean':
1185            # longitudinal mean values
1186            print "  " + fname + ": kind '" + op + "' not ready !!"
1187            wrongstats.append(CFvarn + '_' + opers)
1188            break
1189        elif op =='lsec':
1190            # longitudinal section (longitudinal value must be given, [var]@[lon])
1191            print "  " + fname + ": kind '" + op + "' not ready !!"
1192            wrongstats.append(CFvarn + '_' + opers)
1193            break
1194        elif op == 'pinterp':
1195            # pinterp: pressure interpolation (to the given $plevels)
1196            vals=plevels + ',1,1'
1197            if dofile:
1198                try:
1199                    with gen.Capturing() as output:
1200                        ncvar.pinterp(vals,prevfile,vninF)
1201                except:
1202                    print errmsg
1203                    print 'pinterp('+vals+',', prevfile, ','+vninF+')'
1204                    for s1out in output: print s1out
1205                    ncvar.pinterp(vals,prevfile,vninF)
1206                    quit(-1)
1207
1208                if db:
1209                    for s1out in output: print s1out
1210
1211                sout = sub.call('mv pinterp.nc ' + fileon, shell=True)
1212
1213                # Keeping the operations
1214                pyins=pyH + "/nc_var.py -o pinterp -S '" + vals + "' -f " +          \
1215                  prevfile + "-v " + vninF
1216                otrackf.write("\n")
1217                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1218                otrackf.write(pyins + "\n")
1219
1220                # adding CF lon,lat,time in WRF files
1221                if minf.name == 'WRF' or minf.name == 'WRF_LMDZ':
1222                    values = vdnx + ':' + vdny + ':'+ Tref + ':' + Tunits
1223                    if db:
1224                        print "   CFification of '" + fileon + "' with:", values, "..."
1225                    try:
1226                        with gen.Capturing() as output:
1227                            ncvar.WRF_toCF(values, fileon)
1228                    except:
1229                        print errmsg
1230                        print 'ncvar.WRF_toCF(' + values+ ', ' + fileon + ')'
1231                        for s1out in output: print s1out
1232                        # Removing file in order to make sure that it will be redone
1233                        print '  ' + fname + " removing file '" + fileon + "' ..."
1234                        sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
1235                        quit(-1)
1236
1237            # vertical interpolation variables are no more needed
1238            CFvarnp = vninF
1239            gP = 'none'
1240
1241        elif op == 'tmean':
1242            # temporal mean values
1243            vals='time|-1,time,mean,' + ':'.join(varnCFs) + ':' + vdnz
1244            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1245            if dofile:
1246                try:
1247                    with gen.Capturing() as output:
1248                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1249                except:
1250                    print errmsg
1251                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1252                    for s1out in output: print s1out
1253                    quit(-1)
1254
1255                if db:
1256                    for s1out in output: print s1out
1257
1258                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
1259
1260                # Keeping the operations
1261                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1262                  "' -f " + prevfile + " -v " + CFvarnp
1263                otrackf.write("\n")
1264                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1265                otrackf.write(pyins + "\n")
1266
1267            # removing dimension variable-dimension 'time'
1268            varnCFs.remove('time')
1269
1270            varkeep.append('timestats')
1271
1272        elif op == 'tstd':
1273            # temporal standard deviation values
1274            vals='time|-1,time,std,' + ':'.join(varnCFs) + ':' + vdnz
1275            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1276            if dofile:
1277                try:
1278                    with gen.Capturing() as output:
1279                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1280                except:
1281                    print errmsg
1282                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1283                    for s1out in output: print s1out
1284                    quit(-1)
1285
1286                if db:
1287                    for s1out in output: print s1out
1288
1289                sout = sub.call('mv file_oper_alongdims_std.nc '+fileon, shell=True)
1290
1291                # Keeping the operations
1292                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1293                  "' -f " + prevfile + " -v " + CFvarnp
1294                otrackf.write("\n")
1295                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1296                otrackf.write(pyins + "\n")
1297
1298            # removing dimension variable-dimension 'time'
1299            varnCFs.remove('time')
1300
1301            varkeep.append('timestats')
1302
1303        elif op == 'tturb':
1304            # temporal turbulent values
1305            vals='time|-1,time,turb,' + ':'.join(varnCFs) + ':' + vdnz
1306            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1307            if dofile:
1308                try:
1309                    with gen.Capturing() as output:
1310                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1311                except:
1312                    print errmsg
1313                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1314                    for s1out in output: print s1out
1315                    quit(-1)
1316
1317                if db:
1318                    for s1out in output: print s1out
1319
1320                sout = sub.call('mv file_oper_alongdims_turb.nc '+fileon, shell=True)
1321
1322                # Keeping the operations
1323                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1324                  "' -f " + prevfile + " -v " + CFvarnp
1325                otrackf.write("\n")
1326                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1327                otrackf.write(pyins + "\n")
1328
1329            # removing dimension variable-dimension 'time'
1330            varkeep.append('timestats')
1331
1332        elif op == 'tvar':
1333            # temporal variance values (Taylor's turbulence)
1334            vals='time|-1,time,var,' + ':'.join(varnCFs) + ':' + vdnz
1335            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1336            if dofile:
1337                try:
1338                    with gen.Capturing() as output:
1339                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1340                except:
1341                    print errmsg
1342                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1343                    for s1out in output: print s1out
1344                    quit(-1)
1345
1346                if db:
1347                    for s1out in output: print s1out
1348
1349                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
1350
1351                # Keeping the operations
1352                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1353                  "' -f " + prevfile + " -v " + CFvarnp
1354                otrackf.write("\n")
1355                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1356                otrackf.write(pyins + "\n")
1357
1358            # removing dimension variable-dimension 'time'
1359            varnCFs.remove('time')
1360
1361            varkeep.append('timestats')
1362
1363        elif op == 'xmean':
1364            # x-axis mean values
1365            vals='lon|-1,lon,mean,' + ':'.join(varnCFs) + ':' + vdnz
1366            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1367            if dofile:
1368                try:
1369                    with gen.Capturing() as output:
1370                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1371                except:
1372                    print errmsg
1373                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1374                    for s1out in output: print s1out
1375                    quit(-1)
1376
1377                if db:
1378                    for s1out in output: print s1out
1379
1380                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
1381
1382                # Keeping the operations
1383                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1384                  "' -f " + prevfile + " -v " + CFvarnp
1385                otrackf.write("\n")
1386                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1387                otrackf.write(pyins + "\n")
1388
1389            # removing dimension variable-dimension 'lon'
1390            varnCFs.remove('lon')
1391
1392            varkeep.append('lonstats')
1393
1394        elif op == 'xvar':
1395            # x-axis variance values
1396            vals='lon|-1,lon,var,' + ':'.join(varnCFs) + ':' + vdnz
1397            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1398            if dofile:
1399                try:
1400                    with gen.Capturing() as output:
1401                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1402                except:
1403                    print errmsg
1404                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1405                    for s1out in output: print s1out
1406                    quit(-1)
1407
1408                if db:
1409                    for s1out in output: print s1out
1410
1411                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
1412
1413                # Keeping the operations
1414                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1415                  "' -f " + prevfile + " -v " + CFvarnp
1416                otrackf.write("\n")
1417                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1418                otrackf.write(pyins + "\n")
1419
1420            # removing dimension variable-dimension 'lon'
1421            varnCFs.remove('lon')
1422
1423            varkeep.append('lonstats')
1424
1425        elif op == 'ymean':
1426            # y-axis mean values
1427            vals='lat|-1,lat,mean,' + ':'.join(varnCFs) + ':' + vdnz
1428            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
1429            if dofile:
1430                try:
1431                    with gen.Capturing() as output:
1432                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
1433                except:
1434                    print errmsg
1435                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
1436                    for s1out in output: print s1out
1437                    quit(-1)
1438
1439                if db:
1440                    for s1out in output: print s1out
1441
1442                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
1443
1444                # Keeping the operations
1445                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
1446                  "' -f " + prevfile + " -v " + CFvarnp
1447                otrackf.write("\n")
1448                otrackf.write("# " + CFvarn + " " + Fopers + "\n")
1449                otrackf.write(pyins + "\n")
1450
1451            # removing dimension variable-dimension 'lat'
1452            varnCFs.remove('lat')
1453
1454            varkeep.append('latstats')
1455
1456        elif op == 'zsum':
1457            # vertical aggregated values
1458            print "  " + fname + ": kind '" + op + "' not ready !!"
1459            wrongstats.append(CFvarn + '_' + opers)
1460            break
1461        else:
1462            print errmsg
1463            print '  ' + fname + ": operation '" + op + "' not ready !!"
1464            quit(-1)
1465
1466        # End of kind of operation
1467
1468        # Variable name in file (vninF) changed due to operation
1469        #   but only if previous operation does not the same 'statistic'
1470        chvn = gen.dictionary_key_list(Opsurs, op)
1471        if chvn is not None:
1472            oldvninF = vninF
1473            vninF = vninF + chvn
1474            CFvarnp = CFvarnp.replace(oldvninF,vninF)
1475               
1476        if dofile:
1477            if len(varkeep) > 0:
1478                varkeepS = ',' + ','.join(varkeep)
1479            else:
1480                varkeepS = ''
1481
1482            # Adding such CF dimension variables which might not be in the
1483            #   post-operation file
1484            try:
1485                with gen.Capturing() as output: 
1486                    varinfile = ncvar.ivars(fileon)
1487            except:
1488                print errmsg
1489                print 'ivars('+fileon+')'
1490                for s1out in output: print s1out
1491                # Removing file in order to make sure that it will be redone
1492                print '  ' + fname + " removing file '" + fileon + "' ..."
1493                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
1494                quit(-1)
1495
1496            for CFvn in varnCFs:
1497                if not gen.searchInlist(varinfile, CFvn):
1498                    if db:
1499                        print '  ' + fname + ": recupering CF variable '" + CFvn + "'"
1500                    try:
1501                        with gen.Capturing() as output:
1502                            ncvar.fvaradd(prevfile+','+CFvn, fileon)
1503                    except:
1504                        print errmsg
1505                        print 'fvaradd(' + prevfile + ',' + CFvn +', '+fileon+')'
1506                        ncvar.fvaradd(prevfile+','+CFvn, fileon)
1507                        for s1out in output: print s1out
1508                        # Removing file in order to make sure that it will be redone
1509                        print '  ' + fname + " removing file '" + fileon + "' ..."
1510                        #sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
1511                        quit(-1)
1512
1513                    if db:
1514                        for s1out in output: print s1out
1515
1516            totalvarkeeps = CFvarnp + ',' + ','.join(varnCFs) + varkeepS
1517            try:
1518                with gen.Capturing() as output:
1519                    oclean = ncvar.cleaning_varsfile(totalvarkeeps,fileon)
1520            except:
1521                print errmsg
1522                print 'cleaning_varsfile('+totalvarkeeps+','+fileon+')'
1523                ncvar.cleaning_varsfile(totalvarkeeps,fileon)
1524                for s1out in output: print s1out
1525                # Removing file in order to make sure that it will be redone
1526                print '  ' + fname + " removing file '" + fileon + "' ..."
1527                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
1528                quit(-1)
1529
1530            if db:
1531                for s1out in output: print s1out
1532
1533    # End of operations
1534    if db:
1535        print fname + ": successful calculation of '" + vninF + "' from '" +        \
1536          CFvarn + "' '" + Opers + "' !!"
1537
1538    if len(wrongstats) > 1:
1539        print warnmsg
1540        print '  ' + fname + ": statisitcs not possible to compute:", wrongstats
1541
1542    return 
1543
1544def compute_vars(config, modinf, idir, odir, Files, allvarcomp, fscr, debug):
1545    """ Function to compute the variables
1546      config= Configuration of the experiment
1547      modinf= class with information about the model
1548      idir= input experiment folder
1549      odir= output experiment folder
1550      Files= dictionary with the header and the files' correspondence
1551      allvarcomp= dictionary with all the variables to compute and their information
1552      fscr= whether files should be done from the scratch or not
1553    """
1554    fname = 'compute_vars'
1555
1556    for vopn in allvarcomp.keys():
1557        # variable & operation
1558        vn = vopn.split('_')[0]
1559        op = vopn.split('_')[1]
1560        vopnV = allvarcomp[vopn]
1561        fheader = vopnV[0]
1562        if type(vopnV[1]) == type([1, 2]) and len(vopnV[1]) == 1:
1563            vals = vopnV[1]
1564            model = vals[0]
1565        else:
1566            model = vopnV[1]
1567        diag = vopnV[2]
1568        globalP = vopnV[3]
1569
1570        if debug:
1571            print '      ' + vn + ': ' + op
1572
1573        # Computing CF variable
1574        if model is not None or diag is not None:
1575            vinf = VariableInf(vn,fheader,model,diag)
1576            if op.find('pinterp') != -1: 
1577                SP = 'p'
1578            else:
1579                SP = ''
1580
1581            # Comppute variable
1582            finalfilen = odir + '/' + vn + '_' + fheader + SP + '.nc'
1583            if fscr:
1584                sout = sub.call('rm ' + finalfilen + ' >& /dev/null', shell=True)
1585
1586            if not os.path.isfile(finalfilen):
1587                compute_variable(modinf, idir, Files, odir, vinf, globalP, fscr,     \
1588                  config['pyHOME'], config['CFreftime'], config['CFunitstime'], debug)
1589
1590            # Compute variable statistics
1591            finalfilen = odir + '/' + vn + '_' + fheader + SP + '_' +                \
1592              op.replace('+','_') + '.nc'
1593            if fscr:
1594                sout = sub.call('rm ' + finalfilen + ' >& /dev/null', shell=True)
1595
1596            if not os.path.isfile(finalfilen):
1597                compute_statistics(modinf, cnf, iwdir, Files, owdir, vinf, globalP,  \
1598                  op, fscr, debug)
1599
1600        else:
1601            print errmsg
1602            print '  ' + fname + ": neither 'model' or 'diag' variables for '" + vn  \
1603              + "' !!"
1604            quit(-1)
1605
1606    return
1607
1608def get_plots_var(vc,pltk,db):
1609    """ Function to provide the plots to make for each variable from pltk ('DIRPLT_', or 'DIFFPLT_') dictionary
1610      vc= pltk dictionary, dictionary with all the parameters started with pltk
1611      pltk= kind of plots. Values started by 'DIRPLT_', or 'DIFFPLT_' in model configuration
1612    """
1613    fname = 'get_plots_var'
1614
1615    LH = len(pltk)
1616    # list of plots
1617    doplots = {}
1618    # plots by variable
1619    plotsvar = {}
1620    # individual plots by variable (not repeated)
1621    indivplotsvar = {}
1622
1623    # Variables with a plot which requires vertical interpolation
1624    varplotp = []
1625
1626    ipl = 0
1627    for plot in vc.keys():
1628        Lplot = len(plot)
1629        pln = plot[LH:Lplot+1]
1630        varops = vc[plot].split(':')
1631        for vn in varops:
1632            VOn = vn.split('#')
1633            # Variables and operations in plot
1634            vplt = []
1635            oplt = []
1636            voplt = []
1637            for vop in VOn:
1638                vv = vop.split('|')[0]
1639                oo = vop.split('|')[1]
1640                vplt.append(vv)
1641                oplt.append(oo)
1642                if not gen.searchInlist(voplt,vop): voplt.append(vop)
1643            vpltS = '#'.join(vplt)
1644            opltS = '#'.join(oplt)
1645           
1646            if not doplots.has_key(pln):
1647                doplots[pln] = [vn]
1648            else:
1649                plots = doplots[pln]
1650                plots.append(vn)
1651                doplots[pln] = plots
1652
1653            if not plotsvar.has_key(vn):
1654                plotsvar[vn] = [pln]
1655            else:
1656                plots = plotsvar[vn]
1657                plots.append(pln)
1658                plotsvar[vn] = plots
1659
1660            for VOv in voplt:
1661                if not indivplotsvar.has_key(VOv):
1662                    indivplotsvar[VOv] = [pln]
1663                else:
1664                    plots = indivplotsvar[VOv]
1665                    plots.append(pln)
1666                    indivplotsvar[VOv] = plots
1667
1668        if vc[plot].find('pinterp') != -1:
1669            if not gen.searchInlist(varplotp,vn): varplotp.append(pln)
1670
1671        ipl = ipl + 1
1672
1673    if db:
1674        print '  plots to make _______'
1675        gen.printing_dictionary(doplots)
1676        print '  plots by variable|operation _______'
1677        gen.printing_dictionary(plotsvar)
1678        print '  individual plots by variable|operation _______'
1679        gen.printing_dictionary(indivplotsvar)
1680        print '  plots with the vertical interpolation _______'
1681        print '    #', varplotp
1682
1683    return doplots, plotsvar, indivplotsvar, varplotp
1684
1685def plots_listconstruct(config, pkind, finf, odir, debug):
1686    """ Function to create the list of plots to draw
1687      config= Configuration of the experiment
1688      pkind= kind of plots ('DIRPLT', 'DIFFPLT')
1689      finf= file with the instructions of the plots
1690      odir= output experiment folder
1691    """
1692    fname='plots_listconstruct'
1693 
1694    dirplot = gen.get_specdictionary_HMT(config, H=pkind+'_')
1695
1696    # No plots because there are not plots of this kind
1697    if dirplot is None: 
1698        print warnmsg
1699        print '  ' + fname + ": no plots for type '" + pkind + "' !!"
1700        return {}, 0
1701
1702    if debug:
1703        print "  '" + pkind + "' plots to draw ________"
1704        gen.printing_dictionary(dirplot)
1705
1706    # Getting plots by variable
1707    plots, varplot, indivarplot, plotp = get_plots_var(dirplot, pkind+'_', debug)
1708
1709    Svarplot = gen.dictKeysVals_stringList(plots,cV=':')
1710
1711    Nplots = 0
1712    for pl in Svarplot.split(','):
1713        Nplots = Nplots + len(pl.split(':'))
1714
1715    # Outwritting the varcompute to avoid next time (if it is not filescratch!)
1716    objf = open(finf, 'w')
1717    objf.write('plots: ' + Svarplot + '\n')
1718    objf.write('itotp: ' + str(Nplots) + '\n')
1719    objf.close() 
1720
1721    return plots, Nplots
1722
1723def read_plot_file(figfile):
1724    """ Function to read the file with the information about the  plots to draw
1725      figfile= file with the information
1726    """
1727    fname = 'read_plot_file'
1728
1729    if not os.path.isfile(figfile):
1730        print errormsg
1731        print '  ' + fname + ": figures file '" + figfile + "' does not exist !!"
1732        quit(-1)
1733
1734    objf = open(figfile, 'r')
1735    for line in objf:
1736        if line[0:1] != '#' and len(line) > 1:
1737            values = line.replace('\\n','').split(' ')
1738            if values[0] == 'plots:':
1739                if values[1] != 'none':
1740                    plots = gen.stringList_dictKeysVals(values[1],cV=':')
1741                else:
1742                    pots = {}
1743            elif values[0] == 'itotp:':
1744                Nplots = int(values[1])
1745
1746    objf.close()
1747
1748    return plots, Nplots
1749
1750def varnoper(vn, oper, opsurdict):
1751    """ Function to provide name of the variable after operation as result of addition of the 'surnames'
1752      vn= variable name
1753      oper= '+' separated list of operations
1754      opsurdict= dictionary with the surname of operations
1755    >>> OpSurDict = {'mean': ['tmean', 'xmean', 'ymean']}
1756    >>> varnoper('tas', 'pinterp+tmean+xmean', OpSurDict)
1757    tasmeanmean
1758    """
1759    fname = 'varnoper'
1760
1761    newvn = vn
1762
1763    for op in oper.split('+'):
1764        surname = gen.dictionary_key_list(opsurdict,op)
1765        if surname is not None:
1766            newvn = newvn + surname
1767
1768    return newvn
1769
1770def draw_plot(kplot, CFvplot, fplot, vplot, dplot, pplot, finame, tfig, kfig, mapval,\
1771  tvals, expgraphc, od, pyH, fscr, db):
1772    """ Function to draw a plot
1773      kplot= kind of plot
1774      CFvplot= list of the CFnames of the variables
1775      fplot= list with the files in the plot
1776      vplot= list with the name of the variables in each file
1777      dplot= list of the dimensions in each file
1778      pplot= list of pictoric characteristics for each variable
1779      finame= name of the figure
1780      tfig= title of the figure
1781      kfig= kind of the figure
1782      mapval= value of the map
1783      tvals= list with time values
1784        tunits: units of time ('!' for spaces)
1785        timekind: kind of time ticks in the plot
1786        timefmt: format of time ticks in the plot
1787        timelabel: label of time-axis in the plot
1788      expgraphc= dictionary with expriment graphical characteristics
1789      od= output directory
1790      pyH= python HOME
1791      fscr= whether figure should be done from scratch
1792    """
1793    fname = 'draw_plot'
1794
1795    if fscr:
1796        sout = sub.call('rm ' + finame + ' >& /dev/null', shell=True)
1797
1798    os.chdir(od)
1799
1800    # CF variable-dimensions
1801    CFvardims = {'lon':'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'}
1802
1803    # Tracking file with all the figures
1804    trkobjf = open('all_figures.inf', 'a')
1805
1806    # Time-characteristics
1807    tunits = tvals[0]
1808    timekind = tvals[1]
1809    timefmt = tvals[2]
1810    timelabel = tvals[3]
1811
1812    # Graphical labels and configuration
1813   
1814
1815    if not os.path.isfile(finame):
1816        if db:
1817            print "    drawing '" + finame + "' ..."
1818            print '      plot kind:', kplot
1819            print '      CFvars:', CFvplot
1820            print '      files:', fplot
1821            print '      in file vars:', vplot
1822            print '      dims:', dplot
1823            print '      pictoric values:', pplot
1824            print '      figure name:', finame
1825            print '      title:', tfig.replace('!',' ')
1826            print '      kind:', kfig
1827            print '      map:', mapval
1828            print '      time units:', tunits
1829
1830        if kplot == 'diffmap2Dsfc':
1831            print "  " + fname + ": kind of plot '" + kplot + "' not ready !!"
1832            quit(-1)
1833        elif kplot == 'diffmap2Dz':
1834            print "  " + fname + ": kind of plot '" + kplot + "' not ready !!"
1835            quit(-1)
1836        elif kplot == 'map2Dsfc':
1837            print "  " + fname + ": kind of plot '" + kplot + "' not ready !!"
1838            quit(-1)
1839        elif kplot == 'map3D':
1840            print "  " + fname + ": kind of plot '" + kplot + "' not ready !!"
1841            quit(-1)
1842        elif kplot == '2lines':
1843            figtit = tfig.replace('!','|')
1844
1845            lineAstdn = CFvplot[0]
1846            lineBstdn = CFvplot[1]
1847            figfs = ','.join(fplot)
1848
1849            lineA = pplot[0]
1850            Arange = str(lineA[0]) + ',' + str(lineA[1])
1851            # Values are changed from specific values in `model_graphics.dat'
1852            if lineA[3].find('%') == -1:
1853                Aline = lineA[2]
1854                Akind = lineA[3]
1855                Amark = lineA[4]
1856                Asize = lineA[5]
1857            else:
1858                Aline = 'red'
1859                Akind = '-'
1860                Amark = ','
1861                Asize = 2.
1862
1863            lineB = pplot[1]
1864            Brange = str(lineB[0]) + ',' + str(lineB[1])
1865            # Values are changed from specific values in `model_graphics.dat'
1866            if lineB[3].find('%') == -1:
1867                Bline = lineB[2]
1868                Bkind = lineB[3]
1869                Bmark = lineB[4]
1870                Bsize = lineB[5]
1871            else:
1872                Bline = 'blue'
1873                Bkind = '-'
1874                Bmark = ','
1875                Bsize = '2.'
1876
1877            # It is assumed that if the space variable is 'lon': is desired a
1878            #   (lon, vals) plot if it is 'lat': then (vals, lat) plot
1879            if gen.searchInlist(dplot[0],'lon'):
1880                spacedim ='lon'
1881                axisvals = 'y'
1882                figmid = 'longitudinal'
1883            elif gen.searchInlist(dplot[0],'lat'):
1884                spacedim = 'lat'
1885                axisvals = 'x'
1886                figmid = 'meridional'
1887            elif gen.searchInlist(dplot[0],'pres'):
1888                spacedim = 'pres'
1889                axisvals = 'x'
1890                figmid = 'vertical'
1891            else:
1892                print errmsg
1893                print '  ' + fname + ": in '2lines' only ready for: 'lon', 'lat'," + \
1894                 " 'pres' as common dimension !!"
1895                print '  dimensions in plot:', dplot[0]
1896                quit(-1)
1897
1898            figtit = '!'.join(tfig.split('!')[0:2]) + '!' + figmid + '!' +           \
1899              '!'.join(tfig.split('!')[2:])
1900
1901            graphvals = spacedim + ':' + Arange + ':' + Brange + ':Extrs:' +         \
1902              axisvals + ':' + ','.join(CFvplot) + ':' + Aline + ',' + Bline +       \
1903              ':2.,2.:' + Akind + ',' + Bkind + ':2.,2.:,:' + figtit + ':' +         \
1904              spacedim + ':0|auto:' +  finame.replace('.'+kfig, '') + ':' + kfig +   \
1905              ':True'
1906
1907            fvarS = ','.join(vplot)
1908
1909            drwins = 'draw_2lines'
1910            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
1911              " -S '" + graphvals + "' -v " + fvarS
1912
1913            try:
1914                with gen.Capturing() as output:
1915                    sout = sub.call(plotins, shell=True)
1916            except:
1917                print errmsg
1918                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
1919                print sout
1920                for s1out in output: print s1out
1921                quit(-1)
1922
1923            # keeping all figures
1924            trkobjf.write('\n')
1925            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
1926            trkobjf.write(plotins + '\n')
1927
1928        elif kplot == '2linesTime':
1929            figtit = tfig.replace('!','|')
1930
1931            lineAstdn = CFvplot[0]
1932            lineBstdn = CFvplot[1]
1933            figfs = ','.join(fplot)
1934
1935            lineA = pplot[0]
1936            Arange = str(lineA[0]) + ',' + str(lineA[1])
1937            # Values are changed from specific values in `model_graphics.dat'
1938            if lineA[3].find('%') == -1:
1939                Aline = lineA[2]
1940                Akind = lineA[3]
1941                Amark = lineA[4]
1942                Asize = lineA[5]
1943            else:
1944                Aline = 'red'
1945                Akind = '-'
1946                Amark = ','
1947                Asize = '2.'
1948
1949            lineB = pplot[1]
1950            Brange = str(lineB[0]) + ',' + str(lineB[1])
1951            # Values are changed from specific values in `model_graphics.dat'
1952            if lineB[3].find('%') == -1:
1953                Bline = lineB[2]
1954                Bkind = lineB[3]
1955                Bmark = lineB[4]
1956                Bsize = lineB[5]
1957            else:
1958                Bline = 'blue'
1959                Bkind = '-'
1960                Bmark = ','
1961                Bsize = '2.'
1962
1963            timeaxis = 'x'
1964            figmid = 'evolution'
1965
1966            figtit = '!'.join(tfig.split('!')[0:2]) + '!' + figmid + '!' +           \
1967              '!'.join(tfig.split('!')[2:])
1968
1969            graphvals = 'time:' + Arange + ':' + Brange + ':' + timekind + ';' +     \
1970              timefmt + ':' + timeaxis + ':' + ','.join(CFvplot) + ':' + Aline + ',' \
1971              + Bline + ':2.,2.:' + Akind + ',' + Bkind + ':2.,2.:,:' + figtit + ':' \
1972              + timelabel + ':0|auto:' +  finame.replace('.'+kfig, '') + ':' +       \
1973              kfig + ':True' 
1974
1975            fvarS = ','.join(vplot)
1976
1977            drwins = 'draw_2lines_time'
1978            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
1979              " -S '" + graphvals + "' -v " + fvarS
1980
1981            try:
1982                with gen.Capturing() as output:
1983                    sout = sub.call(plotins, shell=True)
1984            except:
1985                print errmsg
1986                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
1987                print sout
1988                for s1out in output: print s1out
1989                quit(-1)
1990
1991            # keeping all figures
1992            trkobjf.write('\n')
1993            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
1994            trkobjf.write(plotins + '\n')
1995
1996        elif kplot == 'Nlines':
1997            figtit = tfig.replace('!','|')
1998
1999            linestdn = CFvplot[0]
2000            figfs = ','.join(fplot)
2001
2002            Nlines = len(fplot)
2003
2004            lablines = []
2005            for il in range(Nlines):
2006                linev = pplot[il]
2007                # Values are changed from specific values in `model_graphics.dat'
2008                if linev[3].find('%') == -1:
2009                    line = linev[2]
2010                    kind = linev[3]
2011                    mark = linev[4]
2012                    size = linev[5]
2013                    if il == 0:
2014                        vrange = str(linev[0]) + ',' + str(linev[1])
2015                        kinds = kind
2016                        lines = line
2017                        marks = mark
2018                        sizes = str(size)
2019                    else:
2020                        kinds = kinds + ',' + kind
2021                        lines = lines + ',' + line
2022                        marks = marks + '@' + mark
2023                        sizes = sizes + ',' + str(size)
2024                else:
2025                    lines = 'None'
2026                    kinds = '-'
2027                    marks = ','
2028                    sizes = '2.'
2029
2030                secsf = fplot[il].split('/')
2031                Nsecsf = len(secsf)
2032                expvs = expgraphc[secsf[Nsecsf-3] + '/' + secsf[Nsecsf-2]]
2033                lablines.append(expvs.label)
2034
2035            # It is assumed that if the space variable is 'lon': is desired a
2036            #   (lon, vals) plot if it is 'lat': then (vals, lat) plot
2037            if gen.searchInlist(dplot[0],'lon'):
2038                spacedim ='lon'
2039                sdlab ='lon ($degrees\ East$)'
2040                axisvals = 'y'
2041                figmid = 'longitudinal'
2042            elif gen.searchInlist(dplot[0],'lat'):
2043                spacedim = 'lat'
2044                sdlab = 'lat ($degrees\ North$)'
2045                axisvals = 'x'
2046                figmid = 'meridional'
2047            elif gen.searchInlist(dplot[0],'pres'):
2048                spacedim = 'pres'
2049                sdlab = 'pres ($Pa$)'
2050                axisvals = 'x'
2051                figmid = 'vertical'
2052            else:
2053                print errmsg
2054                print '  ' + fname + ": in 'Nlines' only ready for: 'lon', 'lat'," + \
2055                 " 'pres' as common dimension !!"
2056                print '  dimensions in plot:', dplot[0]
2057                quit(-1)
2058
2059            figtit = '!'.join(tfig.split('!')[0:2]) + '!' + figmid + '!' +           \
2060              '!'.join(tfig.split('!')[2:])
2061            figtit = figtit.replace('!',' ')
2062            leglabels = ','.join(lablines)
2063
2064            graphvals = spacedim + ':' + axisvals + ':' + sdlab + ':auto:' +         \
2065              leglabels + ':' + CFvplot[0] +':'+ figtit + ':0|auto:' + lines + ':' + \
2066              kinds + ':' + marks +':'+ sizes +':'+ sizes +':all:'+                  \
2067              finame.replace('.'+kfig, '') + ':' + kfig + ':True'
2068
2069            fvarS = vplot[0]
2070
2071            drwins = 'draw_lines'
2072            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
2073              " -S '" + graphvals + "' -v " + fvarS
2074
2075            try:
2076                with gen.Capturing() as output:
2077                    sout = sub.call(plotins, shell=True)
2078            except:
2079                print errmsg
2080                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
2081                print sout
2082                for s1out in output: print s1out
2083                quit(-1)
2084
2085            # keeping all figures
2086            trkobjf.write('\n')
2087            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
2088            trkobjf.write(plotins + '\n')
2089
2090        elif kplot == 'Nlines_time':
2091            figtit = tfig.replace('!','|')
2092
2093            linestdn = CFvplot[0]
2094            figfs = ','.join(fplot)
2095
2096            Nlines = len(fplot)
2097
2098            lablines = []
2099            for il in range(Nlines):
2100                linev = pplot[il]
2101                # Values are changed from specific values in `model_graphics.dat'
2102                if linev[3].find('%') == -1:
2103                    line = linev[2]
2104                    kind = linev[3]
2105                    mark = linev[4]
2106                    size = linev[5]
2107                    if il == 0:
2108                        vrange = str(linev[0]) + ',' + str(linev[1])
2109                        kinds = kind
2110                        lines = line
2111                        marks = mark
2112                        sizes = str(size)
2113                    else:
2114                        kinds = kinds + ',' + kind
2115                        lines = lines + ',' + line
2116                        marks = marks + '@' + mark
2117                        sizes = sizes + ',' + str(size)
2118                else:
2119                    lines = 'None'
2120                    kinds = '-'
2121                    marks = ','
2122                    sizes = '2.'
2123
2124                secsf = fplot[il].split('/')
2125                Nsecsf = len(secsf)
2126                expvs = expgraphc[secsf[Nsecsf-3] + '/' + secsf[Nsecsf-2]]
2127                lablines.append(expvs.label)
2128
2129            valsaxis = 'y'
2130            figmid = 'evolution'
2131
2132            figtit = '!'.join(tfig.split('!')[0:2]) + '!' + figmid + '!' +           \
2133              '!'.join(tfig.split('!')[2:])
2134            figtit = figtit.replace('!',' ')
2135            leglabels = ','.join(lablines)
2136
2137            graphvals = 'time;' + valsaxis + ';time;' + leglabels + ';' +            \
2138              CFvplot[0] + ';' + figtit + ';None;time|' + '|'.join(tvals) +          \
2139              ';0|auto;' + kfig + ';' + kinds + ';' + lines + ';' + marks + ';' +    \
2140              sizes + ';' + sizes + ';all;-1;True' 
2141
2142            fvarS = vplot[0]
2143
2144            drwins = 'draw_lines_time'
2145            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
2146              " -S '" + graphvals + "' -v " + fvarS
2147
2148            try:
2149                with gen.Capturing() as output:
2150                    sout = sub.call(plotins, shell=True)
2151            except:
2152                print errmsg
2153                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
2154                print sout
2155                for s1out in output: print s1out
2156                quit(-1)
2157
2158            sout = sub.call('mv lines_time.' + kfig + ' ' + finame, shell=True)
2159
2160            # keeping all figures
2161            trkobjf.write('\n')
2162            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
2163            trkobjf.write(plotins + '\n')
2164
2165        elif kplot == 'shadcont2Dsfc':
2166            figtit = tfig.replace('!','|')
2167            shdstdn = CFvplot[0]
2168            cntstdn = CFvplot[1]
2169            figfs = ','.join(fplot)
2170
2171            shad = pplot[0]
2172            srange = str(shad[0]) + ',' + str(shad[1])
2173            cbar = shad[2]
2174            cnt = pplot[1]
2175            crange = str(cnt[0]) + ',' + str(cnt[1])
2176            cfmt = cnt[3]
2177            ckind = cnt[4]
2178            cline = cnt[5]
2179
2180            graphvals = ','.join(CFvplot)
2181            graphvals = graphvals + ':lon|-1,lat|-1:lon|-1,lat|-1:lon:lat:auto:' +   \
2182              cbar + ',auto,auto:' + ckind + ',' + cline + ':' + cfmt + ':' +        \
2183              srange + ':' + crange + ',9:' + figtit + ':' + kfig + ':None:' +       \
2184              mapval + ':True'
2185
2186            fvarS = ','.join(vplot)
2187
2188            drwins = 'draw_2D_shad_cont'
2189            plotins = "python " + pyH + "/drawing.py -f " + figfs + " -o " + drwins +\
2190              " -S '" + graphvals + "' -v " + fvarS
2191            try:
2192                with gen.Capturing() as output:
2193                    sout = sub.call(plotins, shell=True)
2194            except:
2195                print errmsg
2196                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
2197                print sout
2198                for s1out in output: print s1out
2199                quit(-1)
2200
2201            sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True)
2202
2203            # keeping all figures
2204            trkobjf.write('\n')
2205            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
2206            trkobjf.write(plotins + '\n')
2207
2208        elif kplot == 'shadconthovmsfc':
2209            figtit = tfig.replace('!','|')
2210            shdstdn = CFvplot[0]
2211            cntstdn = CFvplot[1]
2212            figfs = ','.join(fplot)
2213
2214            shad = pplot[0]
2215            srange = str(shad[0]) + ',' + str(shad[1])
2216            cbar = shad[2]
2217            cnt = pplot[1]
2218            crange = str(cnt[0]) + ',' + str(cnt[1])
2219            cfmt = cnt[3]
2220            ckind = cnt[4]
2221            cline = cnt[5]
2222            # It is assumed that if the space variable is 'lon': is desired a
2223            #   (lon, time) plot if it is 'lat': then (time, lat) plot
2224            if gen.searchInlist(dplot,'lon'):
2225                spacedim ='lon'
2226                figmid = 'longitudinal|evolution|of'
2227                figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' +       \
2228                  '|'.join(tfig.split('!')[2:])
2229                reverse= 'None'
2230                dims= spacedim+'|-1,time|-1;'+spacedim+'|-1,time|-1;'+spacedim+';time'
2231            else:
2232                spacedim='lat'
2233                figmid = 'meridional|evolution|of'
2234                figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' +       \
2235                  '|'.join(tfig.split('!')[2:])
2236                reverse='None'
2237                dims= spacedim+'|-1,time|-1;'+spacedim+'|-1,time|-1;time;'+spacedim
2238
2239            graphvals = ','.join(CFvplot)
2240            graphvals = graphvals + ';' + dims +';auto;' + cbar + ',auto,auto' + ';' \
2241              + ckind + ',' + cline + ';' + cfmt + ';' + srange + ';' + crange +     \
2242              ',9;' + figtit + ';' + kfig + ';' + reverse + ';' + 'time|' + tunits + \
2243              '|'+ timekind + '|' + timefmt + '|' + timelabel + ';True'
2244
2245            fvarS = ','.join(vplot)
2246
2247            drwins = 'draw_2D_shad_cont_time'
2248            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \
2249              " -S '" + graphvals + "' -v " + fvarS
2250
2251            try:
2252                with gen.Capturing() as output:
2253                    sout = sub.call(plotins, shell=True)
2254            except:
2255                print errmsg
2256                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
2257                print sout
2258                for s1out in output: print s1out
2259                quit(-1)
2260
2261            sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True)
2262
2263            # keeping all figures
2264            trkobjf.write('\n')
2265            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
2266            trkobjf.write(plotins + '\n')
2267
2268        elif kplot == 'shadcont2Dzsec':
2269            figtit = tfig.replace('!','|')
2270            shdstdn = CFvplot[0]
2271            cntstdn = CFvplot[1]
2272            figfs = ','.join(fplot)
2273
2274            shad = pplot[0]
2275            srange = str(shad[0]) + ',' + str(shad[1])
2276            cbar = shad[2]
2277            cnt = pplot[1]
2278            crange = str(cnt[0]) + ',' + str(cnt[1])
2279            cfmt = cnt[3]
2280            ckind = cnt[4]
2281            cline = cnt[5]
2282
2283            # It is assumed that if the space variable is 'lon': is desired a
2284            #   (lon, pres) plot it it is 'lat': then (pres, lat) plot
2285            if gen.searchInlist(dplot, 'lon'):
2286                spacedim='lon'
2287                figmid = 'long.|vert.|cross|sec.|of'
2288                figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' +       \
2289                  '|'.join(tfig.split('!')[2:])         
2290                dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+spacedim+':pres'
2291            else:
2292                spacedim='lat'
2293                figmid = 'mer.|vert.|cross|sec.|of'
2294                figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' +       \
2295                  '|'.join(tfig.split('!')[2:])
2296# NOT WORKING?   dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+'pres:'+spacedim
2297                dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+spacedim+':pres'
2298
2299            reverse = 'None'
2300
2301            graphvals = ','.join(CFvplot)
2302            graphvals = graphvals + ':' + dims + ':auto:' + cbar + ',auto,auto:' +   \
2303              ckind + ',' + cline + ':' + cfmt + ':' + srange + ':' + crange + ',9:' \
2304              + figtit + ':' + kfig + ':' + reverse + ':None:True'
2305 
2306            fvarS = ','.join(vplot)
2307
2308            drwins = 'draw_2D_shad_cont'
2309            plotins = 'python ' + pyH + '/drawing.py -f ' + figfs + ' -o ' + drwins + \
2310              " -S '" + graphvals + "' -v " + fvarS
2311            try:
2312                with gen.Capturing() as output:
2313                    sout0 = sub.call(plotins, shell=True)
2314            except:
2315                print errmsg
2316                print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')'
2317                for s1out in output: print s1out
2318                quit(-1)
2319
2320            sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True)
2321
2322            # keeping all figures
2323            trkobjf.write('\n')
2324            trkobjf.write("# '" + kplot + "'; " + tfig.replace('!',' ') + '\n')
2325            trkobjf.write(plotins + '\n')
2326
2327        else:
2328            print errmsg
2329            print "  " + fname + ": plot kind '" + kplot + "' not ready !!"
2330            quit(-1)
2331
2332    trkobjf.close()
2333
2334    return
2335
2336def optitle(op,opexp):
2337    """ Function to creat the operation section in the title of a figure ('!' for spaces)
2338      op= '+' separated list of operations
2339      opexp= dictionary with the `explanation'(text in title) to appear for each operation
2340    >>> optitle('pinterp+xmean+tmean',{'pinterp':'pinterp', 'xmean':'xmean', 'tmean':'tmean'})
2341    $_{[pinterp\!xmean\!tmean]}$
2342    """
2343    fname = 'optitle'
2344
2345    opvals = op.split('+')
2346    for op1 in opvals:
2347        if not opexp.has_key(op1):
2348            print errmsg
2349            print '  '+fname+": no explanation for operation '"+op1+"' !!"
2350            print '    provided:', opexp.keys()
2351            quit(-1)
2352        if op1 == opvals[0]:
2353            titop = '$_{[' + opexp[op1].replace(' ','!')
2354            if len(opvals) == 1: titop = titop + ']}$'
2355        elif op1 == opvals[len(opvals)-1]:
2356            titop = titop + '\!' + opexp[op1].replace(' ','!') + ']}$'
2357        else:
2358            titop = titop + '\!' + opexp[op1].replace(' ','!')
2359
2360    return titop
2361
2362def create_figure_title(mod,exp,varops,explop):
2363    """ Function to create the title of a figure ('!' for spaces)
2364      mod: model name
2365      exp: experiment name
2366      varops: list with [var]|[op] values from wich create the title
2367      explop: dictionary with the `explanation'(text in title) to appear for each operation
2368    >>> expops = {'pinterp':'pinterp', 'xmean':'xmean', 'tmean':'tmean', 'last':'last'}
2369    >>> create_figure_title('WRF','current',['ua|pinterp+xmean+tmean', 'va|pinterp+xmean+tmean'], expops)
2370    WRF!current!ua!&!va$_{[pinterp\!xmean\!tmean]}$
2371    >>> create_figure_title('WRF','current',['ua|pinterp+xmean+tmean', 'ua|pinterp+xmean+last'], expops)
2372    WRF!current!ua$_{[pinterp\!xmean\!tmean]}$!&!!$_{[pinterp\!xmean\!last]}$
2373    >>> create_figure_title('WRF','current',['ua|pinterp+xmean+tmean', 'va|pinterp+xmean+last'], expops)
2374    WRF!current!ua$_{[pinterp\!xmean\!tmean]}$!&!va$_{[pinterp\!xmean\!last]}$
2375    """
2376    fname = 'create_figure_title'
2377
2378    titfig = mod + '!' + exp + '!'
2379
2380    varns = []
2381    opns = []
2382    for varop in varops:
2383        vn = varop.split('|')[0]
2384        op = varop.split('|')[1]
2385        varns.append(vn)
2386        opns.append(op)
2387        if varop == varops[0]:
2388            allvarequal = True
2389            allopequal = True
2390            varinfig = vn
2391            opinfig = op
2392        else:
2393            if vn != varinfig: allvarequal = False
2394            if op != opinfig: allopequal = False
2395
2396    Nvars = len(varns)
2397    Nops = len(opns)
2398
2399    if allvarequal:
2400        titfig = titfig + '!' + varns[0]
2401        if allopequal:
2402            opS = optitle(op,explop)
2403            titfig = titfig + opS
2404        else:
2405            for opn in opns:
2406                opS = optitle(opn,explop)
2407                if opn == opns[0]:
2408                    titfig = titfig + opS
2409                elif opn == opns[Nops-1]:
2410                    titfig = titfig + '!&!' + opS
2411                else:
2412                    titfig = titfig + ',!' + opS
2413    else: 
2414        if allopequal:
2415            opS = optitle(op,explop)
2416            for vn in varns:
2417                if vn == varns[0]:
2418                    titfig = titfig + '!' + vn
2419                elif vn == varns[Nvars-1]:
2420                    titfig = titfig + '!&!' + vn
2421                else:
2422                    titfig = titfig + ',!' + vn
2423            titfig = titfig + opS
2424        else:
2425            for ivop in range(Nvars):
2426                vn = varns[ivop]
2427                op = opns[ivop]
2428                vnop = vn + optitle(op,explop)
2429               
2430                if vn == varns[0]:
2431                    titfig = titfig + '!' + vnop
2432                elif vn == varns[Nvars-1]:
2433                    titfig = titfig + '!&!' + vnop
2434                else:
2435                    titfig = titfig + ',!' + vnop
2436
2437    return titfig
2438
2439def draw_plots(config, plots, mod, exp, odir, allvarcomp, figscr, debug):
2440    """ Function to draw all plots
2441      config= Configuration of the experiment
2442      plots= dictionary with the plots
2443      mod= model of the plots
2444      exp= experiment of the plots
2445      odir= output experiment folder
2446      allvarcomp= dictionary with all the variables to compute and their information
2447      figscr= whether figures should be done from the scratch or not
2448
2449    * Plot as
2450      {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ...
2451        [kplot] ___
2452          diffmap2Dsfc: 2D map of surface differences values of 1 variable
2453          diffmap2Dz: 2D map of 3D differences values of 1 variable
2454          map2Dsfc: 2D map of surface values of 1 variable
2455          map3D: 2D map of 3D values of 1 variable
2456          shadconthovmsfc: Hovmoeller diagrams of 2 variable at the surface in shadow and the other in contourn
2457          shadcont2Dsfc: 2D map of shadow (1st variable) and countour (2nd variable) [stvar1]#[stvar2]
2458          shadcont2Dzsec: 2D map of vertical section of 2 variables one in shadow and the other in contourn
2459        [varn]
2460          variable
2461        [op]
2462          '+' separated list of operations
2463        in figures with more than 1 variable, use '#' to separate the [varn]|[op]
2464    """ 
2465    fname = 'draw_plots'
2466
2467    os.chdir(odir)
2468
2469    # Dictionary with the operations with surnames for the operated variable
2470    opersurnames = {}
2471    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
2472    for opsr in opsur.keys():
2473        opn = opsr.split('_')[1]
2474        vls = opsur[opsr].split(':')
2475        opersurnames[opn] = vls
2476
2477    # time values
2478    # Units time for the plots
2479    rd = config['CFreftime']
2480    tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' +  \
2481      rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14]
2482    # time ticks kind
2483    tkind = config['timekind']
2484    # time ticks format
2485    tfmt = config['timefmt']
2486    # time axis label
2487    tlab = config['timelabel']
2488    timevals = [tunits, tkind, tfmt, tlab]
2489
2490    # Dictionary of plot specificities
2491    specplotkeyn = 'specificvarplot'
2492    plotspecifics = {}
2493    if config.has_key(specplotkeyn):
2494        #   [minval]: minimum value
2495        #   [maxval]: minimum value
2496        #   [colorbar]: name of the colorbar (from matplotlib) to use
2497        #   [cntformat]: format of the contour labels
2498        #   [colorcnt]: color for the countor lines
2499        plotspecs = config[specplotkeyn].split(':')
2500        for pltspc in plotspecs:
2501            pltvls = pltspc.split('|')
2502            vn = pltvls[0]
2503            op = pltvls[1]
2504            fn = pltvls[2]
2505            plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:]
2506        if debug:
2507            print 'Specific values for plots _______'
2508            gen.printing_dictionary(plotspecifics)
2509
2510    # Kind of figures
2511    kindfigure = config['kindfig']
2512
2513    # Graphical labels and configuration
2514    modgc, expgc = graphical_characteristics(config, debug)
2515    modv = modgc[mod]
2516    modlab = modv.label
2517    expv = expgc[mod+'/'+exp]
2518    explab = expv.label
2519
2520    # Map value
2521    mapvalue = config['mapval']
2522
2523    # pythone scripts HOME
2524    pyHOME = config['pyHOME']
2525
2526    # Title-text of operations
2527    opexplained = {}
2528    optits = config['titleoperations'].split(':')
2529    for optit in optits:
2530        opn = optit.split('|')[0]
2531        opt = optit.split('|')[1]
2532        opexplained[opn] = opt
2533    if debug:
2534        print 'Titles for operations  _______'
2535        gen.printing_dictionary(opexplained)
2536
2537    for kplot in plots.keys():
2538        varsplt = plots[kplot]
2539        for varplt in varsplt:
2540            if debug:
2541                print "  printing '" + kplot + "' var ':" + varplt + "'..."
2542            varops = varplt.split('#')
2543
2544            # CF variables in plot
2545            CFvarsplot = []
2546            # Files in plot
2547            filesplot = []
2548            # Variables in plot within the files
2549            varsplot = []
2550            # Dims in figure
2551            dimsplot = []
2552            # pictoric values in figure
2553            pictplot = []
2554            # Name of the figure
2555            figname = ''
2556            # Title of the figure
2557            titfigure = ''
2558
2559            ivp = 0
2560            for varop in varops:
2561                vn = varop.split('|')[0]
2562                op = varop.split('|')[1]
2563
2564                # CF variables in plot
2565                CFvarsplot.append(vn)
2566 
2567                vnopS = vn + '_' + op
2568                if not allvarcomp.has_key(vnopS):
2569                    print errmsg
2570                    print '  ' + fname + ": no file for variable-operation '" +     \
2571                      vnopS + "' !!"
2572                vopvals = allvarcomp[vnopS]
2573                headf = vopvals[0]
2574                globalP = vopvals[3]
2575                gP = pinterpS(globalP)
2576
2577                filen = vn + '_' + headf + gP + '_' + op.replace('+','_') + '.nc'
2578                filesplot.append(filen)
2579                # Do we have processed the given variable?
2580                if not os.path.isfile(filen):
2581                    print warnmsg
2582                    print "  " + fname + ": there is no file for variable '" + varop \
2583                      + "' skiping it !!"
2584                    break
2585
2586                # Name of the variable inside the file
2587                vnsur = varnoper(vn, op, opersurnames)
2588                varsplot.append(vnsur)
2589
2590                # Dimensions in file
2591                try:
2592                    with gen.Capturing() as output:
2593                        dims = ncvar.idims(filen)
2594                except:
2595                    print errmsg
2596                    print 'ncvar.idims('+filen+')'
2597                    for s1out in output: print s1out
2598                    quit(-1)
2599
2600                dimsplot.append(dims)
2601
2602                # pictoric values for the figure
2603                Sfivaop = kplot + '_' + vn + '_' + op
2604                if plotspecifics.has_key(Sfivaop):
2605                    pictvals = plotspecifics[Sfivaop]
2606                else:
2607                    Vvals = gen.variables_values(vn)
2608                    pictvals = [Vvals[2], Vvals[3], Vvals[6], '%g', 'fixc', 'black']
2609
2610                pictplot.append(pictvals)
2611
2612                # Header of the name of the figure
2613                if ivp == 0:
2614                    figname = kplot +'_'+ vn + '_' + headf + '_' + op.replace('+','-')
2615                else:
2616                    figname = figname + '-'+vn+'_'+headf+'_'+op.replace('+','-')
2617
2618                ivp = ivp + 1
2619            # End of variable-operation
2620            figname = figname + '.' + kindfigure
2621
2622            # Title of figure
2623            titfigure = create_figure_title(modlab, explab, varops, opexplained)
2624
2625            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
2626              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
2627              pyHOME, figscr, debug)
2628
2629        # End of variables-operations
2630
2631    # End of kind of plots
2632
2633    return
2634
2635def get_differences_var(vc,kdiff,db):
2636    """ Function to provide the operations to make for each variable from kdiff ('DIFFOP_' or 'DIFFVAR_') dictionary
2637      vc= kdiff dictionary, dictionary with all the parameters started with kdiff
2638      kdiff= kind of differences 'op', 'var'
2639    """
2640    fname = 'get_differences_var'
2641
2642    if kdiff == 'op':
2643        diffn = 'DIFFOP'
2644    elif kdiff == 'var':
2645        diffn = 'DIFFVAR'
2646    else:
2647         print errmsg
2648         print '  ' + fname + ": differences '" + kdiff + "' not ready !!"
2649         quit(-1)
2650    LD = len(diffn + '_')
2651
2652    iop = 1
2653    # list of operations by difference
2654    doopers = {}
2655    # operations by variable
2656    operationsvar = {}
2657    # individual operations by variable (not repeated)
2658    indivoperationsvar = {}
2659
2660    # Variables with a calculation whic requires vertical interpolation to all the
2661    #   file (operations stating by [kdiff]'_pinterp')
2662    LVp = len(diffn + '_pinterp')
2663    varglobalp = []
2664
2665    for oper in vc.keys():
2666        Loper = len(oper)
2667        opn = oper[LD:Loper+1]
2668        vns = vc[oper].split(':')
2669        ops1 = opn.split('+')
2670        doopers[opn] = ops1
2671        for vn in vns:
2672            if not operationsvar.has_key(vn):
2673                operationsvar[vn] = [opn]
2674                indivoperationsvar[vn] = ops1
2675            else:
2676                opers = operationsvar[vn]
2677                opers.append(opn)
2678                operationsvar[vn] = opers
2679                opers1 = indivoperationsvar[vn]
2680                for op1 in ops1:
2681                    if not gen.searchInlist(opers1, op1):
2682                        opers1.append(op1)
2683                indivoperationsvar[vn] = opers1
2684
2685        if oper[0:LVp] == diffn + '_pinterp':
2686            for vn in vns:
2687                if not gen.searchInlist(varglobalp,vn): varglobalp.append(vn)
2688
2689        iop = iop + 1
2690
2691    if db:
2692        print '  operations to make _______'
2693        gen.printing_dictionary(doopers)
2694        print '  operations by variable _______'
2695        gen.printing_dictionary(operationsvar)
2696        print '  individual operations by variable _______'
2697        gen.printing_dictionary(indivoperationsvar)
2698        print '  variables with the vertical interpolation for all the file _______'
2699        print '    #', varglobalp
2700
2701    return doopers, operationsvar, indivoperationsvar, varglobalp
2702
2703def diffvarop_listconstruct(config, mods, exps, reprj, odir, debug):
2704    """ Function to create the list of differences to compute and draw
2705      config= Configuration of the experiment
2706      mods= list with the models to use
2707      exps= list with the experiments to use
2708      reprj= list with whether re-rpojection needs to be done
2709      odir= output differences folder
2710    """
2711    fname='diffvarop_listconstruct'
2712 
2713    diffop = gen.get_specdictionary_HMT(config, H='DIFFOP_')
2714    diffvar = gen.get_specdictionary_HMT(config, H='DIFFVAR_')
2715
2716    print '  ' + fname + '; Lluis HERE !!!'
2717
2718    if debug:
2719        if diffop is not None:
2720            print "  'op' differences ________"
2721            gen.printing_dictionary(diffop)
2722        if diffvar is not None:
2723            print "  'var' differences ________"
2724            gen.printing_dictionary(diffvar)
2725
2726    # Getting variable characteristics from each model/experiment
2727    idir1 = config['ofold'] + '/' + mods[0] + '/' + exps[0]
2728    idir2 = config['ofold'] + '/' + mods[1] + '/' + exps[1]
2729
2730    fvarcomp = 'varcompute.inf'
2731    if reprj[0]: fvc1 = 'reproj_' + fvarcomp
2732    else: fvc1 = fvarcomp
2733
2734    if reprj[1]: fvc2 = 'reproj_' + fvarcomp
2735    else: fvc2 = fvarcomp
2736
2737    files1, testfiles1, allcompvar1, Nvar1= read_varcomp_file(idir1 + '/' + fvc1)
2738    files2, testfiles2, allcompvar2, Nvar2= read_varcomp_file(idir2 + '/' + fvc2)
2739
2740    diffops = {}
2741    if diffop is not None:
2742        # Getting 'op' differences characteristics
2743        doops, opvar, indivopvar, varglobalp = get_differences_var(diffop,'op',debug)
2744
2745        Ndiffop = 0
2746        for vn in opvar.keys():
2747            vnops = opvar[vn]
2748            for op in vnops:
2749                vnop = vn + '_' + op
2750                if not allcompvar1.has_key(vnop):
2751                    print warnmsg
2752                    print '  ' + fname + ": DIFFOP variable+operation '" + vnop +    \
2753                      "' in '" + mods[0] + '/' + exps[0] + "' was not computed !!"
2754                    print '    skipping it!'
2755                    print '    variables computed:', allcompvar1
2756                    break
2757                if not allcompvar2.has_key(vnop):
2758                    print warnmsg
2759                    print '  ' + fname + ": DIFFOP variable+operation '" + vnop +    \
2760                      "' in '" + mods[1] + '/' + exps[1] + "' was not computed !!"
2761                    print '    skipping it!'
2762                    print '    variables computed:', allcompvar2
2763                    break
2764                vals1 = allcompvar1[vnop]
2765                vals2 = allcompvar2[vnop]
2766
2767                headerf1 = vals1[0]
2768                headerf2 = vals2[0]
2769
2770                diffops[vnop] = [mods[0],mods[1],exps[0],exps[1],headerf1,headerf2]
2771
2772                Ndiffop = Ndiffop + 1       
2773    else:
2774        Ndiffop = 0
2775
2776    diffvars = {}
2777    if diffvar is not None:
2778        # Getting 'var' differences characteristics
2779        doops,opvar,indivopvar,varglobalp = get_differences_var(diffvar,'var',debug)
2780
2781        Ndiffvar = 0
2782        for vn in opvar.keys():
2783            vnops = opvar[vn]
2784            for op in vnops:
2785                vnop = vn + '_' + op
2786                if not allcompvar1.has_key(vnop):
2787                    print warnmsg
2788                    print '  ' + fname + ": DIFFVAR variable+operation '" + vnop +   \
2789                      "' in '" + mods[0] + '/' + exps[0] + "' was not computed !!"
2790                    print '    skipping it!'
2791                    print '    variables computed:', allcompvar1
2792                    break
2793                if not allcompvar2.has_key(vnop):
2794                    print warnmsg
2795                    print '  ' + fname + ": DIFFVAR variable+operation '" + vnop +   \
2796                      "' in '" + mods[1] + '/' + exps[1] + "' was not computed !!"
2797                    print '    skipping it!'
2798                    print '    variables computed:', allcompvar2
2799                    break
2800                vals1 = allcompvar1[vnop]
2801                vals2 = allcompvar2[vnop]
2802   
2803                headerf1 = vals1[0]
2804                headerf2 = vals2[0]
2805
2806                diffvars[vnop] = [mods[0],mods[1],exps[0],exps[1],headerf1,headerf2]
2807
2808                Ndiffvar = Ndiffvar + 1
2809    else:
2810        Ndiffvar = 0
2811
2812    if diffop is not None:
2813        Sopdiffs = gen.dictKeysVals_stringList(diffops)
2814    else:
2815        Sopdiffs = 'none'
2816    # Outwritting the op diffferences file to avoid next time
2817    objf = open(odir + '/diffop.inf', 'w')
2818    objf.write('differences: ' + Sopdiffs + '\n')
2819    objf.write('Ndiff: ' + str(Ndiffop) + '\n')
2820    objf.close() 
2821
2822    if diffvar is not None:
2823        Svardiffs = gen.dictKeysVals_stringList(diffvars)
2824    else:
2825        Svardiffs = 'none'
2826    # Outwritting the var diffferences file to avoid next time
2827    objf = open(odir + '/diffvar.inf', 'w')
2828    objf.write('differences: ' + Svardiffs + '\n')
2829    objf.write('Ndiff: ' + str(Ndiffvar) + '\n')
2830    objf.close() 
2831
2832    return diffops, diffvars, Ndiffop, Ndiffvar
2833
2834def read_diff_file(difffile):
2835    """ Function to read the file with the information about the differences
2836      difffile= file with the information
2837    """
2838    fname = 'read_diff_file'
2839
2840    if not os.path.isfile(difffile):
2841        print errormsg
2842        print '  ' + fname + ": differences file '" + difffile + "' does not exist !!"
2843        quit(-1)
2844
2845    objf = open(difffile, 'r')
2846    for line in objf:
2847        if line[0:1] != '#' and len(line) > 1:
2848            values = line.replace('\n','').split(' ')
2849            if values[0] == 'differences:':
2850                if values[1] != 'none':
2851                    diffs = gen.stringList_dictKeysVals(values[1])
2852                else:
2853                    diffs = {} 
2854            elif values[0] == 'Ndiff:':
2855                Ndiffs = int(values[1])
2856
2857    objf.close()
2858
2859    return diffs, Ndiffs
2860
2861def compute_op_diffs(config, diffallop, odir, diffscr, debug):
2862    """ Function to compute operation differences
2863      config= experiment configuration
2864      diffallop= dictonary with the differences to compute
2865      odir= output folder
2866      diffscr= wether it should be done from the scratch
2867    """
2868    fname = 'compute_op_diffs'
2869
2870    # output folder
2871    ofold = config['ofold']
2872
2873    # Home of the python scripts
2874    pyH = config['pyHOME']
2875
2876    #  opsur = operation surnames
2877    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
2878    Opsurs = {}
2879    for opk in opsur:
2880        opn = opk.split('_')[1]
2881        vals = opsur[opk]
2882        Opsurs[opn] = vals.split(':')
2883
2884    # Getting in working dir
2885    os.chdir(odir)
2886
2887    # CF dimensions
2888    CFdims = ['lon', 'lat', 'pres', 'time']
2889
2890    # Trakcing file
2891    trkobjf = open(odir + '/all_diffop.inf', 'a')
2892
2893    for vnop in diffallop.keys():
2894        vn = vnop.split('_')[0]
2895        op = vnop.split('_')[1]
2896        opS = op.replace('+','_')
2897
2898        # `p' header add related to 'pinterp'
2899        if opS.find('pinterp') != -1: SgP = 'p'
2900        else: SgP = ''
2901
2902        # Variable name within the file
2903        vninF = varnoper(vn, op, Opsurs)
2904
2905        vals = diffallop[vnop]
2906        mod1 = vals[0]
2907        mod2 = vals[1]
2908        exp1 = vals[2]
2909        exp2 = vals[3]
2910        headerf1 = vals[4]
2911        headerf2 = vals[5]
2912        modexpdiff = mod2 + '/' + exp2 + ' - ' + mod1 + '/' + exp1
2913        modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
2914
2915        ifile1 = ofold + '/' + mod1 + '/' + exp1 + '/' + vn + '_' + headerf1 + SgP + \
2916          '_' + opS + '.nc'
2917        ifile2 = ofold + '/' + mod2 + '/' + exp2 + '/' + vn + '_' + headerf2 + SgP + \
2918          '_' + opS + '.nc'
2919        difffilen = odir + '/' + vn + '_diffop_' + modexpdiffS + '_' + opS + '.nc'
2920
2921        if diffscr:
2922            sout = sub.call('rm ' + difffilen + ' >& /dev/null', shell=True)
2923
2924        if not os.path.isfile(difffilen):
2925            if debug:
2926                print "    Computing operation difference '" + op + "' of '" + vn +  \
2927                  "' for: '" + modexpdiff
2928                print "    var in file: '" + vninF + "'"
2929
2930            # Range of the dimensions
2931            for CFd in CFdims:
2932                if CFd == CFdims[0]:
2933                    CFdimS = CFd + '|' + CFd + '|-1'
2934                else:
2935                    CFdimS = CFdimS + ';' + CFd + '|' + CFd + '|-1'
2936
2937            # Attributes of the variable
2938            with gen.Capturing() as output:
2939                varattrs = ncvar.ivattrs(ifile2,vninF)
2940            if debug:
2941                for s1out in output: print s1out
2942            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
2943              varattrs['units'][0]
2944
2945            values = CFdimS + '@' + 'add|' + ifile2 + '|' + vninF + '%' + CFdimS +   \
2946              '@' + 'sub|' + ifile1 + '|' + vninF
2947            pyins = 'python ' + pyH + "/nc_var.py -o compute_opersvarsfiles -S '" +  \
2948              values + "' -v '" + varvals + "'"
2949
2950            try:
2951                with gen.Capturing() as output:
2952                    ncvar.compute_opersvarsfiles(values,varvals)
2953            except:
2954                print errmsg
2955                print 'ncvar.compute_opersvarsfiles(' + values + ',' + varvals + ')'
2956                for s1out in output: print s1out
2957                quit(-1)
2958
2959            sout = sub.call('mv opersvarsfiles_'+vninF+'.nc ' + difffilen, shell=True)
2960
2961            # keeping all differences
2962            trkobjf.write('\n')
2963            trkobjf.write("#" + vn + ' ' + op + '\n')
2964            trkobjf.write( pyins + '\n')
2965
2966    trkobjf.close()
2967
2968    return
2969
2970def compute_diff_statistics(Ecnf, filen, ofold, varn, Opers, scr, db):
2971    """ Function to compute statistics from var diff files
2972      Ecnf= dictionary with the configuration of the experiment
2973      filen= variable difference file to use
2974      ofold= directory to write the output files
2975      varn= name of the variable
2976      Opers= kind of operation: (as possible multiple consecutive combination of operations separated by '+'
2977        [calc1]+[calc2] will compute first [calc1] and then [calc2]
2978        acc: temporal accumulated values
2979        diff: differences between models
2980        direct: no statistics
2981        last: last temporal value
2982        Lmean: latitudinal mean values
2983        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat])
2984        lmean: longitudinal mean values
2985        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
2986        pinterp: pressure interpolation (to the given $plevels)
2987        tmean: temporal mean values
2988        tstd: temporal standard deviation values
2989        tturb: Taylor's turbulence decomposition value (x - <x>) for time
2990        tvar: temporal variance values
2991        xmean: x-axis mean values
2992        xvar: x-axis variance values
2993        ymean: y-axis mean values
2994        zsum: vertical aggregated values
2995      scr= should it be done from the scratch?
2996    """
2997    fname='compute_diff_statistics'
2998
2999    # Full header of file
3000    Lfilen = len(filen)
3001    Hfile = filen[0:Lfilen-3]
3002
3003    # CF dimension-variables name
3004    OpersS = '+'.join(Opers)
3005    if OpersS.find('pinterp') != -1 or filen.find('pinterp') != -1:
3006        varnCFs = ['lon', 'lat', 'pres', 'time']
3007    else:
3008        varnCFs = ['lon', 'lat', 'time']
3009
3010    # Variable-dimension CF dictionary
3011    CFdimvardict = {'lon': 'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'}
3012
3013    # Python scripts HOME
3014    pyH = Ecnf['pyHOME']
3015
3016    #  opsur = operation surnames
3017    opsur = gen.get_specdictionary_HMT(Ecnf, H='opsur_',M='',T='')
3018    Opsurs = {}
3019    for opk in opsur:
3020        opn = opk.split('_')[1]
3021        vals = opsur[opk]
3022        Opsurs[opn] = vals.split(':')
3023
3024    # Getting in working dir
3025    os.chdir(ofold)
3026
3027    # File to keep track of all the statistics of var differences
3028    otrackf = open(ofold + '/all_vardiffstatistics.inf', 'a')
3029
3030    if db: 
3031        print "    computing statistics of diff variable: '", varn ,"' operation '"+ \
3032      OpersS + "' using file '" + filen + " ...'"
3033
3034    for op in Opers:
3035        # File name from previous operations, and name of the variable within the
3036        #   file (some operations change the name of the variable)
3037        if op == Opers[0]:
3038            Fopers = op
3039            prevfile = filen
3040            vninF = varn
3041        else:
3042            Fopers = Fopers + '_' + op
3043            prevfile = fileon
3044        fileon = Hfile + '_' + Fopers + '.nc'
3045
3046        SvarnCFs = ',' + ','.join(varnCFs) 
3047        CFvarnp = vninF + SvarnCFs
3048
3049        if scr:
3050            sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3051
3052        # Just produce file in case output file does not exist
3053        if not os.path.isfile(fileon):
3054            if db: 
3055                print '  ' + fname + ": creation of file '" + fileon + "' ..."
3056            dofile = True
3057        else:
3058            dofile = False
3059       
3060        # Variables to be kept in the final file
3061        varkeep = []
3062
3063        if op == 'acc':
3064            # temporal accumulated values
3065            print "  " + fname + ": kind '" + op + "' not ready !!"
3066            wrongstats.append(varn + '_' + opers)
3067            break
3068        elif op == 'direct':
3069            # no statistics
3070            if dofile:
3071                sout = sub.call('mv ' + prevfile + ' ' + fileon, shell=True)
3072        elif op == 'last':
3073            # last temporal value
3074            vals='time,-9,0,0'
3075            if dofile:
3076                try:
3077                    with gen.Capturing() as output:
3078                        ncvar.DataSetSection(vals,prevfile)
3079                except:
3080                    print errmsg
3081                    print 'DataSetSection('+vals+',', prevfile, ')'
3082                    for s1out in output: print s1out
3083                    quit(-1)
3084
3085                if db:
3086                    for s1out in output: print s1out
3087
3088                hprevfile = prevfile[0:len(prevfile)-3]
3089
3090                sout = sub.call('mv ' + hprevfile + '_time_B-9-E0-I0.nc ' + fileon, \
3091                  shell=True)
3092
3093                # Keeping the operations
3094                pyins=pyH + "/nc_var.py -o DataSetSection -S '" + vals + "' -f " +  \
3095                  prevfile
3096                otrackf.write("\n")
3097                otrackf.write("# " + varn + " " + Fopers + "\n")
3098                otrackf.write(pyins + "\n")
3099
3100        elif op =='Lmean':
3101            # latitudinal mean values
3102            print "  " + fname + ": kind '" + op + "' not ready !!"
3103            wrongstats.append(varn + '_' + opers)
3104            break
3105        elif op =='Lsec':
3106            # latitudinal section (latitudinal value must be given, [var]@[lat])
3107            print "  " + fname + ": kind '" + op + "' not ready !!"
3108            wrongstats.append(varn + '_' + opers)
3109            break
3110        elif op =='lmean':
3111            # longitudinal mean values
3112            print "  " + fname + ": kind '" + op + "' not ready !!"
3113            wrongstats.append(varn + '_' + opers)
3114            break
3115        elif op =='lsec':
3116            # longitudinal section (longitudinal value must be given, [var]@[lon])
3117            print "  " + fname + ": kind '" + op + "' not ready !!"
3118            wrongstats.append(varn + '_' + opers)
3119            break
3120        elif op == 'tmean':
3121            # temporal mean values
3122            vals='time|-1,time,mean,' + ':'.join(varnCFs)
3123            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3124            if dofile:
3125                try:
3126                    with gen.Capturing() as output:
3127                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3128                except:
3129                    print errmsg
3130                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3131                    for s1out in output: print s1out
3132                    quit(-1)
3133
3134                if db:
3135                    for s1out in output: print s1out
3136
3137                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3138
3139                # Keeping the operations
3140                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3141                  "' -f " + prevfile + " -v " + CFvarnp
3142                otrackf.write("\n")
3143                otrackf.write("# " + varn + " " + Fopers + "\n")
3144                otrackf.write(pyins + "\n")
3145
3146            # removing dimension variable-dimension 'time'
3147            varnCFs.remove('time')
3148
3149            varkeep.append('timestats')
3150
3151        elif op == 'tstd':
3152            # temporal standard deviation values
3153            vals='time|-1,time,std,' + ':'.join(varnCFs) + ':' + vdnz
3154            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
3155            if dofile:
3156                try:
3157                    with gen.Capturing() as output:
3158                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3159                except:
3160                    print errmsg
3161                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3162                    for s1out in output: print s1out
3163                    quit(-1)
3164
3165                if db:
3166                    for s1out in output: print s1out
3167
3168                sout = sub.call('mv file_oper_alongdims_std.nc '+fileon, shell=True)
3169
3170                # Keeping the operations
3171                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3172                  "' -f " + prevfile + " -v " + CFvarnp
3173                otrackf.write("\n")
3174                otrackf.write("# " + varn + " " + Fopers + "\n")
3175                otrackf.write(pyins + "\n")
3176
3177            # removing dimension variable-dimension 'time'
3178            varnCFs.remove('time')
3179
3180            varkeep.append('timestats')
3181
3182        elif op == 'tturb':
3183            # temporal turbulent values
3184            vals='time|-1,time,turb,' + ':'.join(varnCFs)
3185            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3186            if dofile:
3187                try:
3188                    with gen.Capturing() as output:
3189                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3190                except:
3191                    print errmsg
3192                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3193                    for s1out in output: print s1out
3194                    quit(-1)
3195
3196                if db:
3197                    for s1out in output: print s1out
3198
3199                sout = sub.call('mv file_oper_alongdims_turb.nc '+fileon, shell=True)
3200
3201                # Keeping the operations
3202                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3203                  "' -f " + prevfile + " -v " + CFvarnp
3204                otrackf.write("\n")
3205                otrackf.write("# " + varn + " " + Fopers + "\n")
3206                otrackf.write(pyins + "\n")
3207
3208            varkeep.append('timestats')
3209
3210        elif op == 'tvar':
3211            # temporal variance values (Taylor's turbulence)
3212            vals='time|-1,time,var,' + ':'.join(varnCFs) + ':' + vdnz
3213            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
3214            if dofile:
3215                try:
3216                    with gen.Capturing() as output:
3217                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3218                except:
3219                    print errmsg
3220                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3221                    for s1out in output: print s1out
3222                    quit(-1)
3223
3224                if db:
3225                    for s1out in output: print s1out
3226
3227                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
3228
3229                # Keeping the operations
3230                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3231                  "' -f " + prevfile + " -v " + CFvarnp
3232                otrackf.write("\n")
3233                otrackf.write("# " + varn + " " + Fopers + "\n")
3234                otrackf.write(pyins + "\n")
3235
3236            # removing dimension variable-dimension 'time'
3237            varnCFs.remove('time')
3238
3239            varkeep.append('timestats')
3240
3241        elif op == 'xmean':
3242            # x-axis mean values
3243            vals='lon|-1,lon,mean,' + ':'.join(varnCFs)
3244            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3245            if dofile:
3246                try:
3247                    with gen.Capturing() as output:
3248                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3249                except:
3250                    print errmsg
3251                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3252                    for s1out in output: print s1out
3253                    quit(-1)
3254
3255                if db:
3256                    for s1out in output: print s1out
3257
3258                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3259
3260                # Keeping the operations
3261                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3262                  "' -f " + prevfile + " -v " + CFvarnp
3263                otrackf.write("\n")
3264                otrackf.write("# " + varn + " " + Fopers + "\n")
3265                otrackf.write(pyins + "\n")
3266
3267            # removing dimension variable-dimension 'lon'
3268            varnCFs.remove('lon')
3269
3270            varkeep.append('lonstats')
3271
3272        elif op == 'xvar':
3273            # x-axis variance values
3274            vals='lon|-1,lon,var,' + ':'.join(varnCFs)
3275            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3276            if dofile:
3277                try:
3278                    with gen.Capturing() as output:
3279                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3280                except:
3281                    print errmsg
3282                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3283                    for s1out in output: print s1out
3284                    quit(-1)
3285
3286                if db:
3287                    for s1out in output: print s1out
3288
3289                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
3290
3291                # Keeping the operations
3292                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3293                  "' -f " + prevfile + " -v " + CFvarnp
3294                otrackf.write("\n")
3295                otrackf.write("# " + varn + " " + Fopers + "\n")
3296                otrackf.write(pyins + "\n")
3297
3298            # removing dimension variable-dimension 'lon'
3299            varnCFs.remove('lon')
3300
3301            varkeep.append('lonstats')
3302
3303        elif op == 'ymean':
3304            # y-axis mean values
3305            vals='lat|-1,lat,mean,' + ':'.join(varnCFs)
3306            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3307            if dofile:
3308                try:
3309                    with gen.Capturing() as output:
3310                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3311                except:
3312                    print errmsg
3313                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3314                    for s1out in output: print s1out
3315                    quit(-1)
3316
3317                if db:
3318                    for s1out in output: print s1out
3319
3320                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3321
3322                # Keeping the operations
3323                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3324                  "' -f " + prevfile + " -v " + CFvarnp
3325                otrackf.write("\n")
3326                otrackf.write("# " + varn + " " + Fopers + "\n")
3327                otrackf.write(pyins + "\n")
3328
3329            # removing dimension variable-dimension 'lat'
3330            varnCFs.remove('lat')
3331
3332            varkeep.append('latstats')
3333
3334        elif op == 'zsum':
3335            # vertical aggregated values
3336            print "  " + fname + ": kind '" + op + "' not ready !!"
3337            wrongstats.append(varn + '_' + opers)
3338            break
3339        else:
3340            print errmsg
3341            print '  ' + fname + ": operation '" + op + "' not ready !!"
3342            quit(-1)
3343
3344        # End of kind of operation
3345
3346        # Variable name in file (vninF) changed due to operation
3347        #   but only if previous operation does not the same 'statistic'
3348        chvn = gen.dictionary_key_list(Opsurs, op)
3349        if chvn is not None:
3350            oldvninF = vninF
3351            vninF = vninF + chvn
3352            CFvarnp = CFvarnp.replace(oldvninF,vninF)
3353               
3354        if dofile:
3355            if len(varkeep) > 0:
3356                varkeepS = ',' + ','.join(varkeep)
3357            else:
3358                varkeepS = ''
3359
3360            # Adding such CF dimension variables which might not be in the
3361            #   post-operation file
3362            try:
3363                with gen.Capturing() as output: 
3364                    varinfile = ncvar.ivars(fileon)
3365            except:
3366                print errmsg
3367                print 'ivars('+fileon+')'
3368                for s1out in output: print s1out
3369                # Removing file in order to make sure that it will be redone
3370                print '  ' + fname + " removing file '" + fileon + "' ..."
3371                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3372                quit(-1)
3373
3374            for CFvn in varnCFs:
3375                if not gen.searchInlist(varinfile, CFvn):
3376                    if db:
3377                        print '  ' + fname + ": recupering CF variable '" + CFvn + "'"
3378                    try:
3379                        with gen.Capturing() as output:
3380                            ncvar.fvaradd(prevfile+','+CFvn, fileon)
3381                    except:
3382                        print errmsg
3383                        print 'fvaradd(' + prevfile + ',' + CFvn +', ' + fileon + ')'
3384                        for s1out in output: print s1out
3385                        # Removing file in order to make sure that it will be redone
3386                        print '  ' + fname + " removing file '" + fileon + "' ..."
3387                        sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3388                        quit(-1)
3389
3390                    if db:
3391                        for s1out in output: print s1out
3392
3393            totalvarkeeps = CFvarnp + ',' + ','.join(varnCFs) + varkeepS
3394            try:
3395                with gen.Capturing() as output:
3396                    oclean = ncvar.cleaning_varsfile(totalvarkeeps,fileon)
3397            except:
3398                print errmsg
3399                print 'cleaning_varsfile('+totalvarkeeps+','+fileon+')'
3400                for s1out in output: print s1out
3401                # Removing file in order to make sure that it will be redone
3402                print '  ' + fname + " removing file '" + fileon + "' ..."
3403                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3404                quit(-1)
3405
3406            if db:
3407                for s1out in output: print s1out
3408
3409    # End of operations
3410    otrackf.close()
3411
3412    return
3413
3414def compute_var_diffs(config, diffallvar, odir, diffscr, debug):
3415    """ Function to compute variable differences
3416      config= experiment configuration
3417      diffallvar= dictonary with the differences to compute
3418      odir= output folder
3419      diffscr= wether it should be done from the scratch
3420    """
3421    fname = 'compute_var_diffs'
3422
3423    # output folder
3424    ofold = config['ofold']
3425
3426    # Home of the python scripts
3427    pyH = config['pyHOME']
3428
3429    #  opsur = operation surnames
3430    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3431    Opsurs = {}
3432    for opk in opsur:
3433        opn = opk.split('_')[1]
3434        vals = opsur[opk]
3435        Opsurs[opn] = vals.split(':')
3436
3437    # Getting in working dir
3438    os.chdir(odir)
3439
3440    # CF dimensions
3441    CFdims = ['lon', 'lat', 'pres', 'time']
3442
3443    # Trakcing file
3444    trkobjf = open(odir + '/all_diffvar.inf', 'a')
3445
3446    for vnop in diffallvar.keys():
3447        vn = vnop.split('_')[0]
3448        op = vnop.split('_')[1]
3449        opSfinal = op.replace('+','_')
3450
3451        lop = op.split('+')
3452        # `p' header add related to 'pinterp'
3453        if op.find('pinterp') != -1:
3454            print warnmsg
3455            print '  ' + fname + ': there is the vertical interpolation as operation'
3456            print '    variable differences will be computed from that files where '
3457            print '    vertical interpolation was just done'
3458            ipop = lop.index('pinterp')
3459            opS = '_' + '_'.join(lop[0:ipop+1])
3460            doops = lop[ipop+1:]
3461            SgP = 'p'
3462            vninF = varnoper(vn, '+'.join(lop[0:ipop+1]), Opsurs)
3463            print '    operations to perform:', doops
3464        else: 
3465            SgP = ''
3466            opS = ''
3467            doops = lop
3468            vninF = vn
3469
3470        Sdoops = '+'.join(doops)
3471
3472        # Variable name within the file
3473
3474        vals = diffallvar[vnop]
3475        mod1 = vals[0]
3476        mod2 = vals[1]
3477        exp1 = vals[2]
3478        exp2 = vals[3]
3479        headerf1 = vals[4]
3480        headerf2 = vals[5]
3481        modexpdiff = mod2 + '/' + exp2 + ' - ' + mod1 + '/' + exp1
3482        modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
3483
3484        ifile1 = ofold + '/' + mod1 + '/' + exp1 + '/' + vn + '_' + headerf1 + SgP + \
3485          opS + '.nc'
3486        ifile2 = ofold + '/' + mod2 + '/' + exp2 + '/' + vn + '_' + headerf2 + SgP + \
3487          opS + '.nc'
3488        difffilen = odir + '/' + vn + '_diffvar_' + modexpdiffS + opS + '.nc'
3489        difffinalfilen = odir + '/' + vn + '_diffvar_' + modexpdiffS + opS + '_' +   \
3490          Sdoops + '.nc'
3491
3492        if diffscr:
3493            sout = sub.call('rm ' + difffilen + ' >& /dev/null', shell=True)
3494            sout = sub.call('rm ' + difffinalfilen + ' >& /dev/null', shell=True)
3495
3496        if not os.path.isfile(difffinalfilen):
3497            if debug:
3498                print "    Computing variable difference of '" + vn + "' for: '" +   \
3499                  modexpdiff + "' and performing operation '" + Sdoops + "'"
3500                print "    var in file: '" + vninF + "'"
3501
3502            # Range of the dimensions
3503            for CFd in CFdims:
3504                if CFd == CFdims[0]:
3505                    CFdimS = CFd + '|' + CFd + '|-1'
3506                else:
3507                    CFdimS = CFdimS + ';' + CFd + '|' + CFd + '|-1'
3508
3509            # Attributes of the variable
3510            with gen.Capturing() as output:
3511                varattrs = ncvar.ivattrs(ifile2,vninF)
3512            if debug: 
3513                for s1out in output: print s1out
3514            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
3515              varattrs['units'][0]
3516
3517            values = CFdimS + '@' + 'add|' + ifile2 + '|' + vninF + '%' + CFdimS +   \
3518              '@' + 'sub|' + ifile1 + '|' + vninF
3519            pyins = 'python ' + pyH + "/nc_var.py -o compute_opersvarsfiles -S '" +  \
3520              values + "' -v '" + varvals + "'"
3521
3522            try:
3523                with gen.Capturing() as output:
3524                    ncvar.compute_opersvarsfiles(values,varvals)
3525            except:
3526                print errmsg
3527                print 'ncvar.compute_opersvarsfiles(' + values + ', ' + varvals + ')'
3528                for s1out in output: print s1out
3529                quit(-1)
3530
3531            sout = sub.call('mv opersvarsfiles_'+vninF+'.nc ' + difffilen, shell=True)
3532
3533            # keeping all differences
3534            trkobjf.write('\n')
3535            trkobjf.write("#" + vn + ' ' + op + '\n')
3536            trkobjf.write( pyins + '\n')
3537
3538            # Computing statisitcs
3539            compute_diff_statistics(config, difffilen, odir, vninF, doops, diffscr,  \
3540              debug)
3541
3542    trkobjf.close()
3543
3544    return
3545
3546
3547def draw_diff_plots(config, plots, odir, allvarcomp, kdiff, figscr, debug):
3548    """ Function to draw all plots
3549      config= Configuration of the experiment
3550      plots= dictionary with the plots
3551      odir= output experiment folder
3552      allvarcomp= dictionary with all the variables to compute and their information
3553      kdiff= kind of differences:
3554        'diffop': plots from operation differences
3555        'diffvar': plots from variable differences
3556      figscr= whether figures should be done from the scratch or not
3557
3558    * Plot as
3559      {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ...
3560        [kplot] ___
3561          diffmap2Dsfc: 2D map of surface differences values of 1 variable
3562          diffmap2Dz: 2D map of 3D differences values of 1 variable
3563          map2Dsfc: 2D map of surface values of 1 variable
3564          map3D: 2D map of 3D values of 1 variable
3565          shadconthovmsfc: Hovmoeller diagrams of 2 variable at the surface in shadow and the other in contourn
3566          shadcont2Dsfc: 2D map of shadow (1st variable) and countour (2nd variable) [stvar1]#[stvar2]
3567          shadcont2Dzsec: 2D map of vertical section of 2 variables one in shadow and the other in contourn
3568        [varn]
3569          variable
3570        [op]
3571          '+' separated list of operations
3572        in figures with more than 1 variable, use '#' to separate the [varn]|[op]
3573    """ 
3574    fname = 'draw_diff_plots'
3575
3576    os.chdir(odir)
3577
3578    # Dictionary with the operations with surnames for the operated variable
3579    opersurnames = {}
3580    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3581    for opsr in opsur.keys():
3582        opn = opsr.split('_')[1]
3583        vls = opsur[opsr].split(':')
3584        opersurnames[opn] = vls
3585
3586    # time values
3587    # Units time for the plots
3588    rd = config['CFreftime']
3589    tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' +  \
3590      rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14]
3591    # time ticks kind
3592    tkind = config['timekind']
3593    # time ticks format
3594    tfmt = config['timefmt']
3595    # time axis label
3596    tlab = config['timelabel']
3597    timevals = [tunits, tkind, tfmt, tlab]
3598
3599    if kdiff == 'diffop':
3600         specplotkeyn = 'specificdiffopplot'
3601    elif kdiff == 'diffvar':
3602         specplotkeyn = 'specificdiffvarplot'
3603    else:
3604         print errmsg
3605         print '  ' + fname + ": differences kind '" + kdiff + "' not ready !!"
3606         quit(-1)
3607
3608    # Dictionary of plot specificities
3609    plotspecifics = {}
3610    if config.has_key(specplotkeyn):
3611        #   [minval]: minimum value
3612        #   [maxval]: minimum value
3613        #   [colorbar]: name of the colorbar (from matplotlib) to use
3614        #   [cntformat]: format of the contour labels
3615        #   [colorcnt]: color for the countor lines
3616        plotspecs = config[specplotkeyn].split(':')
3617        for pltspc in plotspecs:
3618            pltvls = pltspc.split('|')
3619            vn = pltvls[0]
3620            op = pltvls[1]
3621            fn = pltvls[2]
3622            plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:]
3623        if debug:
3624            print 'Specific values for plots _______'
3625            gen.printing_dictionary(plotspecifics)
3626
3627    # Kind of figures
3628    kindfigure = config['kindfig']
3629
3630    # Graphical labels and configuration
3631    modgc, expgc = graphical_characteristics(config, debug)
3632
3633    # Map value
3634    mapvalue = config['mapval']
3635
3636    # pythone scripts HOME
3637    pyHOME = config['pyHOME']
3638
3639    # Title-text of operations
3640    opexplained = {}
3641    optits = config['titleoperations'].split(':')
3642    for optit in optits:
3643        opn = optit.split('|')[0]
3644        opt = optit.split('|')[1]
3645        opexplained[opn] = opt
3646    if debug:
3647        print 'Titles for operations  _______'
3648        gen.printing_dictionary(opexplained)
3649
3650    for kplot in plots.keys():
3651        varsplt = plots[kplot]
3652        for varplt in varsplt:
3653            if debug:
3654                print "  printing '" + kplot + "' var ':" + varplt + "'..."
3655            varops = varplt.split('#')
3656
3657            # CF variables in plot
3658            CFvarsplot = []
3659            # Files in plot
3660            filesplot = []
3661            # Variables in plot within the files
3662            varsplot = []
3663            # Dims in figure
3664            dimsplot = []
3665            # pictoric values in figure
3666            pictplot = []
3667            # Name of the figure
3668            figname = ''
3669            # Title of the figure
3670            titfigure = ''
3671
3672            ivp = 0
3673            for varop in varops:
3674                vn = varop.split('|')[0]
3675                op = varop.split('|')[1]
3676
3677                # CF variables in plot
3678                CFvarsplot.append(vn)
3679 
3680                vnopS = vn + '_' + op
3681                if not allvarcomp.has_key(vnopS):
3682                    print errmsg
3683                    print '  ' + fname + ": no file for variable-operation '" +     \
3684                      vnopS + "' !!"
3685                    print '  available ones:', allvarcomp.keys()
3686                    quit(-1)
3687                vopvals = allvarcomp[vnopS]
3688                mod1 = vopvals[0]
3689                mod2 = vopvals[1]
3690                mod3 = vopvals[2]
3691                mod4 = vopvals[3]
3692                headf = vopvals[4]
3693
3694                modv = modgc[mod1]
3695                expv = expgc[mod1+'/'+exp1]
3696                modlab1 = modv.label
3697                explab1 = expv.label
3698                modv = modgc[mod2]
3699                expv = expgc[mod2+'/'+exp2]
3700                modlab2 = modv.label
3701                explab2 = expv.label
3702
3703                modexpdiff = explab2 + '-' + explab1
3704                modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
3705
3706                difffilen = odir + '/' + vn + '_' + kdiff + '_' + modexpdiffS +      \
3707                  '_' + op.replace('+','_') + '.nc'
3708
3709                filesplot.append(difffilen)
3710                # Do we have processed the given difference?
3711                if not os.path.isfile(difffilen):
3712                    print warnmsg
3713                    print "  " + fname + ": there is no file for '" + kdiff +        \
3714                      "' difference '" + varop + "' skiping it !!"
3715                    break
3716
3717                # Name of the variable inside the file
3718                vnsur = varnoper(vn, op, opersurnames)
3719                varsplot.append(vnsur)
3720
3721                # Dimensions in file
3722                try:
3723                    with gen.Capturing() as output:
3724                        dims = ncvar.idims(difffilen)
3725                except:
3726                    print errmsg
3727                    print 'ncvar.idims('+difffilen+')'
3728                    for s1out in output: print s1out
3729                    quit(-1)
3730
3731                dimsplot.append(dims)
3732
3733                # pictoric values for the figure
3734                Sfivaop = kplot + '_' + vn + '_' + op
3735                if plotspecifics.has_key(Sfivaop):
3736                    pictvals = plotspecifics[Sfivaop]
3737                else:
3738                    Vvals = gen.variables_values(vn)
3739                    pictvals = [Vvals[2], Vvals[3], Vvals[6], '%g', 'fixsigc','black']
3740
3741                pictplot.append(pictvals)
3742
3743                # Header of the name of the figure
3744                if ivp == 0:
3745                    figname = kplot + '_' + vn + '_' + kdiff + '_' + modexpdiffS +   \
3746                      '_' + op.replace('+','-')
3747                else:
3748                    figname = figname + '-' + vn + '_' + op.replace('+','-')
3749
3750                # Title of the figure:
3751                if mod1 == mod2:
3752                    modexptit = mod1 + '!' + exp2 + '-' + exp1
3753                else:
3754                    modexptit = modexpdiff
3755
3756                ivp = ivp + 1
3757            # End of variable-operation
3758            figname = figname + '.' + kindfigure
3759
3760            # Title of figure
3761            titfigure = create_figure_title(modexptit, kdiff, varops, opexplained)
3762
3763            if len(titfigure) > 80:
3764                print warnmsg
3765                print ' ' + fname + ": figure title '" + titfigure.replace('!', ' ')+\
3766                  "' larger than 80 characters (actually", len(titfigure), ') !!'
3767                print "  simplifying it to '" + modexptit.replace('!',' ') + '!' +   \
3768                  kdiff + '!' + vnS + "'"
3769                titfigure = modexptit.replace(' ','!') + '!' + kdiff + '!' + vnS
3770
3771            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
3772              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
3773              pyHOME, figscr, debug)
3774
3775        # End of variables-operations
3776
3777    # End of kind of plots
3778
3779    return
3780
3781def reproject_modexp(mod,exp,config,REPRJops,fscr,fadd,debug):
3782    """ Function to re-project and re-compute files from a given model/experiment
3783      mod= model
3784      exp= experiment
3785      config= configuration
3786      REPRJops= dictionary with the reprojection operators to use for reproject and for which variable
3787        #    remapbil   CDO: Bilinear interpolation
3788        #    remapbic   CDO: Bicubic interpolation
3789        #    remapdis   CDO: Distance-weighted average remapping
3790        #    remapnn    CDO: Nearest neighbor remapping
3791        # Operators only available if projections have the corners of the grid points.
3792        #    remapcon   CDO: First order conservative remapping
3793        #    remapcon2  CDO: Second order conservative remapping
3794        #    remaplaf   CDO: Largest area fraction remapping
3795        ####### ####### ####### or additionally ####### ####### #######
3796        # python remapping
3797        #    dis        python: Distance-weighted average remapping
3798        #    pnn        python: Nearest neighbor remapping
3799
3800      fscr= re-projected files to compute from the scratch
3801      fadd= re-projected files to be added
3802    """
3803    fname = 'reproject_modexp'
3804
3805    odir = config['ofold'] + '/' + mod + '/' + exp
3806
3807    # NON re-projectable operations
3808    opNOreproj = config['NOreprojops'].split(':')
3809
3810    # Need to pass to analyze all the data?
3811    inffiles = ['reproj_varcompute.inf', 'all_reproj_computevars.inf']
3812    if fscr:
3813        for inff in inffiles:
3814            if debug: print "    removing information file '" + inff + "' ..."
3815            ins = 'rm ' + odir + '/' + inff + ' >& /dev/null'
3816            sout = sub.call(ins, shell=True)
3817
3818        objf = open(odir+'/all_reproj_computevars.inf','w')
3819        objf.write("## Computation of reprojected variables \n")
3820        objf.close()
3821
3822    varcompf = odir + '/reproj_varcompute.inf'
3823    if fadd:
3824        sub.call('rm ' + varcompf +' >& /dev/null', shell=True)
3825
3826    # CF dims-vardims pairs:
3827    CFvardims = ['long@lon', 'lat@lat', 'pres@pres', 'time@time']
3828
3829    # Dictionary with the operations with surnames for the operated variable
3830    opersurnames = {}
3831    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3832    for opsr in opsur.keys():
3833        opn = opsr.split('_')[1]
3834        vls = opsur[opsr].split(':')
3835        opersurnames[opn] = vls
3836
3837    if not os.path.isfile(varcompf):
3838        # Getting original computing information
3839        origvarcompf = odir + '/varcompute.inf'
3840        origfiles,origtestfiles,origallcompvar, Nvar = read_varcomp_file(origvarcompf)
3841             
3842        Svarcompute = ''
3843        allcompvar = dict(origallcompvar)
3844        ivop = 0
3845        for vnop in origallcompvar.keys():
3846            vn = vnop.split('_')[0]
3847            op = vnop.split('_')[1]
3848            values = allcompvar[vnop]
3849            values[0] = 'reproj-' + values[0]
3850            allcompvar[vnop] = values
3851
3852            # Building strings from list values
3853            model = values[1]
3854            diag = values[2]
3855            if model is not None:
3856                if type(model) == type(list([1, 2])):
3857                    Smodel = ':'.join(model)
3858                elif type(model) == type('A'):
3859                    Smodel = model
3860            else:
3861                Smodel = 'None'
3862            if diag is not None:
3863                if type(diag) == type(list([1, 2])):
3864                    Sdiag = ':'.join(diag)
3865                elif type(diag) == type('A'):
3866                    Sdiag = diag
3867            else:
3868                Sdiag = 'None'
3869
3870            Svc = vn + '|' + op + '|' + values[0] + '|' + Smodel + '|' + Sdiag +     \
3871              '|' + values[3]
3872            if ivop == 0:
3873                Svarcompute = Svc
3874            else:
3875                Svarcompute = Svarcompute + ',' + Svc
3876            ivop = ivop + 1
3877
3878        origstrfiles = gen.dictKeysVals_stringList(origfiles)
3879        origstrtestfiles = gen.dictKeysVals_stringList(origtestfiles)
3880
3881        # Outwritting the varcompute to avoid next time (if it is not filescratch!)
3882        objf = open(odir + '/reproj_varcompute.inf', 'w')
3883        objf.write('files: ' + origstrfiles + '\n')
3884        objf.write('testfiles: ' + origstrtestfiles + '\n')
3885        objf.write('varcompute: ' + Svarcompute + '\n')
3886        objf.write('itotv: ' + str(ivop) + '\n')
3887        objf.close() 
3888    else: 
3889        if dbg:
3890            print warnmsg
3891            print '  ' + main + ": getting variables to compute already from file !!"
3892        origfiles, origtestfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
3893
3894    # End of avoiding to repeat all the experiment search
3895
3896    print "    For experiment '" + exp + "' is required to re-project:", Nvar,       \
3897      "variables"
3898
3899    if dbg:
3900        print 'Variables to re-project _______'
3901        gen.printing_dictionary(allcompvar)
3902
3903    # Re-projection variables
3904    print "    Reprojecting variables ..."
3905    os.chdir(odir)
3906
3907    # Keeping track of reprojection
3908    trckobjf = open(odir + '/all_reproj_computevars.inf','a')
3909
3910    for vnop in allcompvar.keys():
3911        vn = vnop.split('_')[0]
3912        op = vnop.split('_')[1]
3913        values = allcompvar[vnop]
3914        VnOpS = varnoper(vn, op, opersurnames)
3915
3916        # Only are reprojected that files which do contain 'lon' and 'lat',
3917        #   otherways, it is only re-projected the original file
3918        inNO, iopNO, iop = gen.list_coincidences(opNOreproj,op.split('+'))
3919
3920        reprjop = gen.dictionary_key_list(REPRJops,vn).split('_')[1]
3921        if reprjop is None:
3922            print errmsg
3923            print '  ' + fname + ": no reprojecting operator found for " +           \
3924              " variable '" + vn + "' !!"
3925            print '    available values _______'
3926            gen.printing_dictionary(REPRJops)
3927            quit(-1)
3928       
3929        # Re-projecting also the 'original' (at least with 'pinterp') file for varsdif
3930        if op.find('pinterp') != -1:
3931            ifileorig = vn +'_'+ values[0].replace('reproj-','') + 'p_pinterp.nc'
3932            ofileorig = vn +'_'+ values[0] + 'p_pinterp.nc'
3933            ofilen = vn +'_'+ values[0] + 'p_pinterp.nc'
3934            ifileop = vn +'_'+ values[0].replace('reproj-','') + 'p_' +              \
3935              op.replace('+','_') + '.nc'
3936        else:
3937            ifileorig = vn +'_'+ values[0].replace('reproj-','') + '.nc'
3938            ofileorig = vn +'_'+ values[0] + '.nc'
3939            ifileop = vn +'_'+ values[0].replace('reproj-','') + '_' +               \
3940              op.replace('+','_') + '.nc'
3941
3942        ofileop = vn + '_' + values[0] + '_' + op.replace('+','_') + '.nc'
3943
3944        if fscr:
3945            sout = sub.call('rm ' + odir+'/'+ofileorig + ' >& /dev/null', shell=True)
3946            sout = sub.call('rm ' + odir+'/'+ofileop + ' >& /dev/null', shell=True)
3947
3948        # List of files to re-project (first without operation)
3949        ifiles = [odir+'/'+ifileorig, odir+'/'+ifileop]
3950        # List of output files after re-projection (first without operation)
3951        ofiles = [odir+'/'+ofileorig, odir+'/'+ofileop]
3952
3953        for iif in range(2):
3954            ifile = ifiles[iif]
3955            ofile = ofiles[iif]
3956
3957            # Only projecting original file-variable if there are not 'lon' and 'lat'
3958            doreproj = True
3959            if iif == 1 and len(inNO) > 0: 
3960                doreproj = False
3961                if dbg:
3962                    print warnmsg
3963                    print '  ' + fname + ": operation '" + op + "' can not be " +    \
3964                      "reprojected due to the lack of 'lon' or 'lat' skipping it !!"
3965                    print "    NOTE: difference can only be computed as:"
3966                    print "      - 1st: compute variable's differences between models"
3967                    print "      - 2nd: Perform the operations"
3968
3969                # Building up resources to compute re-projected statistics
3970                if dbg:
3971                    print '  '+fname+': computing from the original reprojected file'
3972                ModI = ModelInf(mod, mod, 'lon', 'lat', 'pres', 'time', 'depth',     \
3973                  'lon', 'lat', 'pres', 'time', 'depth', None)
3974                if type(values) == type([1]): 
3975                  fileh = values[0]
3976                else:
3977                  fileh = values
3978
3979                if type(values) == type([1]): 
3980                  fileh = values[0]
3981                else:
3982                  fileh = values
3983
3984                VarI = VariableInf(vn, values[0], values[2], values[3])
3985                usefs = {values[0]: ofileorig}
3986
3987                compute_statistics(ModI, config, config['ifold'], usefs,             \
3988                  config['ofold']+'/'+mod+'/'+exp,VarI,pinterp_var(op), op, fscr, dbg)
3989
3990            if not os.path.isfile(ofile) and doreproj:
3991                if debug and ofile == ofiles[0]:
3992                    print "      reprojecting '" + vn + "' '" + op + "' into '" +    \
3993                      ofile + "'"
3994
3995                # Reprojecting...
3996                if reprjop[0:6] == 'remap':
3997                    ins = config['cdoHOME'] + '/cdo ' + reprjop + ',' +              \
3998                      config['RefProjfile'] + ' ' + ifile + ' ' + ofile
3999                    try:
4000                        with gen.Capturing() as output:
4001                            sout = sub.call(ins, shell=True)
4002                    except:
4003                        print errmsg
4004                        print '  ' + fname + ': $ ' + ins
4005                        for s1out in output: print s1out
4006                        print sout
4007                        quit(-1)
4008                    if sout != 0:
4009                        print errmsg
4010                        print '  ' + fname + ': $ ' + ins
4011                        sub.call(ins, shell=True)
4012                        quit(-1)
4013                else:
4014                    # Name of the variable to reproject
4015                    if iif == 0:
4016                        reprjvn = vn
4017                    else: 
4018                        reprjvn = VnOpS
4019             
4020                    reprojvalues = 'lon,lat,' + config['RefProjfile'] + ',lon,lat,'+ \
4021                      reprjop + ',' + ':'.join(CFvardims)
4022                    ins= 'ncvar.reproject('+reprojvalues + ', ' + ifile + ', '+      \
4023                      reprjvn + ')'
4024                    try:
4025                        with gen.Capturing() as output:
4026                            sout=ncvar.reproject(reprojvalues, ifile, reprjvn)
4027                    except:
4028                        print errmsg
4029                        print '  ' + fname + ': ' + ins
4030                        for s1out in output: print s1out
4031                        quit(-1)
4032
4033                    sub.call('mv reproject.nc ' + ofile, shell=True)
4034
4035                if debug:
4036                    for s1out in output: print s1out
4037                    print sout
4038
4039                # Keeping track of re-projection
4040                trckobjf.write('\n')
4041                if iif == 0:
4042                    trckobjf.write('# ' + vn + '\n')
4043                else:
4044                    trckobjf.write('# ' + vn + ' ' + op + '\n')
4045
4046                trckobjf.write(ins + '\n')
4047
4048                if reprjop[0:6] == 'remap':
4049                    # CFing the reprojected file
4050                    try:
4051                        with gen.Capturing() as output:
4052                            ncvar.CDO_toCF(ofile)
4053                    except:
4054                        print errmsg
4055                        print 'ncvar.CDO_toCF(' + ofile + ')'
4056                        for s1out in output: print s1out
4057                        # Removing file in order to make sure that it will be redone
4058                        print '  ' + fname + " removing file '" + ofile + "' ..."
4059                        sub.call('rm ' + ofile + ' >& /dev/null', shell=True)
4060                        quit(-1)
4061
4062                    if debug:
4063                        for s1out in output: print s1out
4064               
4065    trckobjf.close()
4066
4067    return
4068
4069def allmodexps_listconstruct(config, modexps, odir, debug):
4070    """ Function to create the list of all model-experiments to draw
4071      config= Configuration of the experiment
4072      modexps= list with the models-experiments pairs to use
4073      odir= output differences folder
4074    """
4075    fname='allmodexps_listconstruct'
4076 
4077    allplts = gen.get_specdictionary_HMT(config, H='PLOTALLMODEXP_')
4078
4079    if debug:
4080        if allplts is not None:
4081            print "  'all mod-exps' plots ________"
4082            gen.printing_dictionary(allplts)
4083
4084    # Getting Model-Reference Projection
4085    ModComProj = cnf['RefProj'].split(':')
4086
4087    allmodexp = {}
4088    Nallmodexp = 0
4089    for modexp in modexps:
4090        if debug:
4091            print "    " +  fname + ": '" + modexp + "' ..."
4092        mod = modexp.split('/')[0]
4093        exp = modexp.split('/')[1]
4094
4095        # Getting variable characteristics from each model/experiment
4096        idir = config['ofold'] + '/' + modexp
4097
4098        reproj = not gen.searchInlist(ModComProj,mod)
4099
4100        fvarcomp = 'varcompute.inf'
4101        if reproj: fvc = 'reproj_' + fvarcomp
4102        else: fvc = fvarcomp
4103
4104        files, testfiles, allcompvar, Nvar= read_varcomp_file(idir + '/' + fvc)
4105
4106        if allplts is not None:
4107            # Getting all model-experiments plots characteristics
4108
4109            for allplt in allplts.keys():
4110                pltn = allplt.split('_')[1]
4111                if debug and modexp == modexps[0]:
4112                    print '    ' + fname + ":      plot '" + pltn + "' "
4113                varops = allplts[allplt].split(':')
4114                for varop in varops:
4115                    vn = varop.split('|')[0]
4116                    op = varop.split('|')[1]
4117                    vnop = vn + '_' + op
4118                    if debug and modexp == modexps[0]:
4119                        print '    ' + fname + ":        varop '" + vnop + "' "
4120                    if not allcompvar.has_key(vnop):
4121                        print warnmsg
4122                        print '  ' + fname + ": variable+operation '" + vnop +       \
4123                          "' in '" + modexp + "' was not computed !!"
4124                        print '    skipping it!'
4125                        print '    computed values:', allcompvar.keys()
4126                        break
4127
4128                    vals = allcompvar[vnop]
4129
4130                    headerf = vals[0]
4131                    if modexp == modexps[0]:
4132                        allmodexp[vnop] = [modexp,headerf]
4133                        Nallmodexp = Nallmodexp + 1
4134                    else:
4135                        prevals = allmodexp[vnop]
4136                        prevals = prevals + [modexp,headerf]
4137                        allmodexp[vnop] = prevals
4138
4139        else:
4140            Nallmodexp = 0
4141
4142    if allplts is not None:
4143        Sallmodexps = gen.dictKeysVals_stringList(allmodexp)
4144    else:
4145        Sallmodexps = 'none'
4146
4147    # Outwritting the all model-experiment plots file to avoid next time
4148    objf = open(odir + '/allmodexp.inf', 'w')
4149    objf.write('allmodexps: ' + Sallmodexps + '\n')
4150    objf.write('Nallmodexp: ' + str(Nallmodexp) + '\n')
4151    objf.close()
4152
4153    return allmodexp, Nallmodexp
4154
4155def allmodexps_read(filen):
4156    """ Function to read the file with the information about the all model-experiment plots
4157      filen= file with the information
4158    """
4159    fname = 'allmodexps_read'
4160
4161    if not os.path.isfile(filen):
4162        print errormsg
4163        print '  ' + fname + ": all model-experiment file '" + filen +               \
4164          "' does not exist !!"
4165        quit(-1)
4166
4167    objf = open(filen, 'r')
4168    for line in objf:
4169        if line[0:1] != '#' and len(line) > 1:
4170            values = line.replace('\n','').split(' ')
4171            if values[0] == 'allmodexps:':
4172                allplts = gen.stringList_dictKeysVals(values[1])
4173            elif values[0] == 'Nallmodexp:':
4174                Nallmodexp = int(values[1])
4175
4176    objf.close()
4177
4178    return allplts, Nallmodexp
4179
4180def draw_allmodexp_plots(config, allvarcomp, plots, modexp, odir, figscr, debug):
4181    """ Function to draw all model-experiment plots
4182      config= Configuration of the experiment
4183      allvarcomp = dictionary with the file headers for each variable
4184      plots= dictionary with the plots
4185      modexps= list with the names of the model-experiment pairs
4186      odir= output experiment folder
4187      figscr= whether figures should be done from the scratch or not
4188
4189    * Plot as
4190      {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ...
4191        [kplot] ___
4192          Nlines: Multiple lines with one line by model-experiment pairs
4193        [varn]
4194          variable
4195        [op]
4196          '+' separated list of operations
4197        in figures with more than 1 variable, use '#' to separate the [varn]|[op]
4198    """ 
4199    fname = 'draw_allmodexp_plots'
4200
4201    os.chdir(odir)
4202
4203    # Folder with the processed files
4204    infold = config['ofold']
4205
4206    # Dictionary with the operations with surnames for the operated variable
4207    opersurnames = {}
4208    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
4209    for opsr in opsur.keys():
4210        opn = opsr.split('_')[1]
4211        vls = opsur[opsr].split(':')
4212        opersurnames[opn] = vls
4213
4214    # time values
4215    # Units time for the plots
4216    rd = config['CFreftime']
4217    tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' +  \
4218      rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14]
4219    # time ticks kind
4220    tkind = config['timekind']
4221    # time ticks format
4222    tfmt = config['timefmt']
4223    # time axis label
4224    tlab = config['timelabel']
4225    timevals = [tunits, tkind, tfmt, tlab]
4226
4227    # Dictionary of plot specificities
4228    specplotkeyn = 'specificvarplot'
4229    plotspecifics = {}
4230    if config.has_key(specplotkeyn):
4231        #   [minval]: minimum value
4232        #   [maxval]: minimum value
4233        #   [colorbar]: name of the colorbar (from matplotlib) to use
4234        #   [cntformat]: format of the contour labels
4235        #   [colorcnt]: color for the countor lines
4236        plotspecs = config[specplotkeyn].split(':')
4237        for pltspc in plotspecs:
4238            pltvls = pltspc.split('|')
4239            vn = pltvls[0]
4240            op = pltvls[1]
4241            fn = pltvls[2]
4242            plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:]
4243        if debug:
4244            print 'Specific values for plots _______'
4245            gen.printing_dictionary(plotspecifics)
4246
4247    # Graphical labels and configuration
4248    modgc, expgc = graphical_characteristics(config, debug)
4249
4250    # Kind of figures
4251    kindfigure = config['kindfig']
4252
4253    # Map value
4254    mapvalue = config['mapval']
4255
4256    # pythone scripts HOME
4257    pyHOME = config['pyHOME']
4258
4259    # Title-text of operations
4260    opexplained = {}
4261    optits = config['titleoperations'].split(':')
4262    for optit in optits:
4263        opn = optit.split('|')[0]
4264        opt = optit.split('|')[1]
4265        opexplained[opn] = opt
4266    if debug:
4267        print 'Titles for operations  _______'
4268        gen.printing_dictionary(opexplained)
4269
4270    for kplot in plots.keys():
4271        varsplt = plots[kplot]
4272        for varplt in varsplt:
4273            if debug:
4274                print "  printing '" + kplot + "' var: '" + varplt + "'..."
4275            varops = varplt.split('#')
4276
4277            # CF variables in plot
4278            CFvarsplot = []
4279            # Files in plot
4280            filesplot = []
4281            # Variables in plot within the files
4282            varsplot = []
4283            # Dims in figure
4284            dimsplot = []
4285            # pictoric values in figure
4286            pictplot = []
4287            # Name of the figure
4288            figname = ''
4289            # Title of the figure
4290            titfigure = ''
4291
4292            ivp = 0
4293            for varop in varops:
4294                vn = varop.split('|')[0]
4295                op = varop.split('|')[1]
4296
4297                # CF variables in plot
4298                CFvarsplot.append(vn)
4299 
4300                if op.find('pinterp') != -1: gP = 'p'
4301                else: gP = ''
4302
4303                vnopS = vn + '_' + op                   
4304                if not allvarcomp.has_key(vnopS):
4305                    print errmsg
4306                    print '  ' + fname + ": no file for variable-operation '" +      \
4307                      vnopS + "' !!"
4308                modexpvals = allvarcomp[vnopS]
4309                Nmodexps = len(modexpvals)
4310
4311                for imodexp in range(0,Nmodexps,2):
4312                    modexp = modexpvals[imodexp]
4313                    headf = modexpvals[imodexp+1]
4314                    if debug:
4315                        print '  ' + fname + "    model/experiment: '" +  modexp + "'"
4316
4317                    filen = infold + '/' + modexp + '/' + vn +'_'+ headf+gP + '_' +  \
4318                      op.replace('+','_') + '.nc'
4319
4320                    filesplot.append(filen)
4321                    # Do we have processed the given variable?
4322                    if not os.path.isfile(filen):
4323                        print warnmsg
4324                        print "  " + fname + ": there is no file for variable '" +   \
4325                          varop + "' skiping it !!"
4326                        break
4327
4328                    if imodexp == 0:
4329                        # Name of the variable inside the file
4330                        vnsur = varnoper(vn, op, opersurnames)
4331                        varsplot.append(vnsur)
4332
4333                    # Dimensions in file
4334                    try:
4335                        with gen.Capturing() as output:
4336                            dims = ncvar.idims(filen)
4337                    except:
4338                        print errmsg
4339                        print 'ncvar.idims('+filen+')'
4340                        for s1out in output: print s1out
4341                        quit(-1)
4342
4343                    if imodexp == 0:
4344                        # Dimensions in plot
4345                        dimsplot.append(dims)
4346
4347                        # Header of the name of the figure
4348                        if ivp == 0:
4349                            figname = kplot+'_'+vn+'_'+headf+'_'+ op.replace('+','-')
4350                        else:
4351                            figname = figname+'-'+vn+'_'+headf+'_'+op.replace('+','-')
4352
4353                    # pictoric values for the figure
4354                    Sfivaop = kplot + '_' + vn + '_' + op
4355                    if plotspecifics.has_key(Sfivaop):
4356                        pictvals = plotspecifics[Sfivaop]
4357                    else:
4358                        Vvals = gen.variables_values(vn)
4359                        pictvals = [Vvals[2],Vvals[3],Vvals[6], '%g', 'fixc', 'black']
4360
4361                    pictplot.append(pictvals)
4362
4363                ivp = ivp + 1
4364            # End of variable-operation
4365            figname = figname + '.' + kindfigure
4366
4367            # Title of figure
4368            titfigure = 'all!model-experiments!' + vn + optitle(op,opexplained)
4369
4370            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
4371              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
4372              pyHOME, figscr, debug)
4373
4374        # End of variables-operations
4375
4376    # End of kind of plots
4377
4378    return
4379
4380def figinf_figname(figname,config):
4381    """ Function to retrieve figure information from its name
4382      figname= name of the figure
4383      config= configuration of `model_graphics.py'
4384    >>> figinf_figname('2linesTime_wss_wrfout_xvar-ymean-tas_wrfout_xvar-ymean.pdf', cnf)
4385    (2, '2linesTime', 'wss-tas', 'xvar-ymean@xvar-ymean')
4386    >>> figinf_figname('Nlines_tas_wrfout_tturb-xmean-tmean.pdf', cnf)
4387    (1, 'Nlines', 'tas', 'tturb-xmean-tmean')
4388    >>> figinf_figname('2lines_wss_diffvar_WRF-micro1_WRF-current_tturb-xmean-last-tas_tturb-xmean-last.pdf',cnf)
4389    (2, '2lines', 'wss-tas', 'tturb-xmean-last@tturb-xmean-last')
4390    """
4391    fname = 'figinf_figname'
4392    plotsecs = figname.split('_')
4393    plotk = plotsecs[0]
4394
4395    if gen.searchInlist(config['pairfigures'].split(':'), plotk):
4396        Nvars = 2
4397        varn1 = plotsecs[1]
4398        if plotsecs[2] == 'diffvar' or plotsecs[2] == 'diffop':
4399            statn1varn2 = plotsecs[5].split('-')
4400            statn2 = plotsecs[6].split('.')[0]
4401            Lstatn1varn2 = len(statn1varn2)
4402            statn1 = '-'.join(statn1varn2[0:Lstatn1varn2-1])
4403            varn2 = statn1varn2[Lstatn1varn2-1]
4404        else:
4405            statn1varn2 = plotsecs[3].split('-')
4406            statn2 = plotsecs[5].split('.')[0]
4407            Lstatn1varn2 = len(statn1varn2)
4408            statn1 = '-'.join(statn1varn2[0:Lstatn1varn2-1])
4409            varn2 = statn1varn2[Lstatn1varn2-1]
4410        return Nvars, plotk, varn1+'-'+varn2, statn1+'@'+statn2
4411    elif gen.searchInlist(config['Nsourcesfigures'].split(':'), plotk):
4412        Nvars = 1
4413        varn = plotsecs[1]
4414        statn = plotsecs[3].split('.')[0]
4415        return Nvars, plotk, varn, statn
4416    else:
4417        print errmsg
4418        print '  ' + fname + ": kind of figure '" + plotk + "' not ready !!"
4419        print '    ready ones _______'
4420        print "    'pair_figures': figures of pair of variables: ",                  \
4421          config['pairfigures'].split(':')
4422        print "    'N-sources_figures': figures of pair of variables: ",                  \
4423          config['Nsourcesfigures'].split(':')
4424        quit(-1)
4425   
4426# Files with information about the configuration of the script
4427inffiles = ['varcompute.inf', 'all_computevars.inf', 'all_statsvars.inf']
4428
4429#######    #######
4430## MAIN
4431    #######
4432if not os.path.isfile('model_graphics.dat'):
4433    print errmsg
4434    print main + ": configuration file 'model_graphics.dat' does not exist!!"
4435    quit(-1)
4436
4437# Getting configuration from external ASCII file 'model_graphics.dat'
4438cnf = gen.get_configuration('model_graphics.dat', False)
4439
4440verify_configuration(cnf, gen.Str_Bool(cnf['debug']))
4441
4442# scratches
4443scratch, filescratch, figscratch, diffscratch, figdiffscratch, moddiffscratch,       \
4444  figmoddiffscratch, figallmodexpscratch, addfiles, addfigures, adddiffs,            \
4445  adddifffigures, addmoddiffs, addmoddifffigures, addallmodexpfigures, dbg =         \
4446  scratches(cnf)
4447
4448if dbg:
4449    print 'Experiment configuration scratches _______'
4450    print '  scratch:', scratch, 'filescratch:', filescratch,'figscratch:', figscratch
4451    print '  diffscratch:', diffscratch, 'figdiffscratch:', figdiffscratch
4452    print '  moddiffscratch:', moddiffscratch, 'figmoddiffscratch:', figmoddiffscratch
4453    print 'Experiment configuration adding _______'
4454    print '  addfiles:', addfiles, 'addfigures:', addfigures
4455    print '  adddiffs:', adddiffs, 'adddifffigures:', adddifffigures
4456    print '  addmoddiffs:', addmoddiffs, 'addmoddifffigures:', addmoddifffigures
4457
4458# Getting models
4459mods = cnf['models'].split(':')
4460
4461# Getting graphical characeristics
4462modGC, expGC = graphical_characteristics(cnf, dbg)
4463
4464# Models loop
4465##
4466
4467# dictionary with the experiments of each model
4468modexps = {}
4469
4470for mod in mods:
4471    print mod
4472    if scratch:
4473        if cnf['ofold'] != cnf['ifold']:
4474            sout = sub.call('rm -rf '+cnf['ofold']+'/'+mod+' > /dev/null', shell=True)
4475        else:
4476            print warnmsg
4477            print '  ' + main + ": input '" + cnf['ifold'] + "' and output '" +      \
4478              cnf['ofold'] + "' are the same folder !!"
4479            print "    Keeping output folder although it has 'scratch' start"
4480
4481    # Get experiments and headers of model
4482    exps, fheaders = exp_headers(mod,cnf)
4483
4484    modexps[mod] = exps
4485    # Characteristics of the model
4486    Modinf = ncvar.model_characteristics(mod,'None','False')
4487    dnx = Modinf.dimxn
4488    dny = Modinf.dimyn
4489    dnz = Modinf.dimzn
4490    dnt = Modinf.dimtn
4491    dns = Modinf.dimsn
4492    vdnx = Modinf.vardxn
4493    vdny = Modinf.vardyn
4494    vdnz = Modinf.vardzn
4495    vdnt = Modinf.vardtn
4496    vdns = Modinf.vardsn
4497
4498    modgraphv = modGC[mod]
4499    Modinf = ModelInf(mod, mod, dnx, dny, dnz, dnt, dns, vdnx, vdny, vdnz, vdnt,     \
4500      vdns, modgraphv.tmod)
4501
4502    if dbg:
4503        print '  model characteristics _______'
4504        gen.printing_class(Modinf)
4505
4506    moddims = dnx + ',' + dny + ',' + dnz + ',' + dnt
4507    modvdims = vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt
4508
4509# Experiments loop
4510##
4511    for exp in exps:
4512        print '  ' + exp + '...'
4513
4514        # input folder
4515        iwdir = cnf['ifold'] + '/' + mod + '/' + exp
4516
4517        # Does input folder exist?
4518        if not os.path.isdir(iwdir):
4519            if filescratch*addfiles:
4520                print errmsg
4521                print "  " + main + ": folder '" + iwdir + "' does not exist !!"
4522                quit(-1)
4523            else:
4524                print warnmsg
4525                print "  " + main + ": folder '" + iwdir + "' does not exist !!"
4526                print "    but start of files from scratch 'filescratch':",          \
4527                  filescratch, "and there is not the add of any file 'addfiles':",   \
4528                  addfiles
4529                print '    so, keep moving'
4530
4531        owdir = cnf['ofold'] + '/' + mod + '/' + exp
4532        sout = sub.call('mkdir -p ' + owdir, shell=True)
4533
4534        # Need to pass to analyze all the data?
4535        if filescratch:
4536            for inff in inffiles:
4537                if dbg: print "    removing information file '" + inff + "' ..."
4538                ins = 'rm ' + owdir + '/' + inff + ' >& /dev/null'
4539                sout = sub.call(ins, shell=True)
4540
4541            objf = open(owdir+'/all_computevars.inf','w')
4542            objf.write("## Computation of variables \n")
4543            objf.close()
4544            objf = open(owdir+'/all_statsvars.inf','w')
4545            objf.write("## Computation of statistics \n")
4546            objf.close()
4547
4548        varcompf = owdir + '/varcompute.inf'
4549        if addfiles:
4550            if dbg:
4551                print '  ' + main + ": adding variables to compute removing " +      \
4552                  "file '" + varcompf + "' ..."
4553            sub.call('rm ' + varcompf +' >& /dev/null', shell=True)
4554
4555        if not os.path.isfile(varcompf):
4556            # Does input folder has header files?
4557            ih=1
4558            # Dictionary with the list of files for each headers
4559            files = {}
4560            # Dictionary with a file fromor each headers
4561            testfiles = {}
4562
4563            for fh in fheaders:
4564                if filescratch:
4565                    ins = 'rm '+ owdir+'/*_' + fh + '*.nc >& /dev/null'
4566                    sout = sub.call(ins, shell=True)
4567                files1h = gen.files_folder(iwdir,fh)
4568                if len(files1h) < 1:
4569                    print errmsg
4570                    print '  ' + main + ": folder '" + iwdir + "' does not " +   \
4571                      "contain files '" + fh + "*' !!"
4572                    quit(-1)
4573                files[fh] = files1h
4574                testfiles[fh] = files1h[0]
4575
4576            if dbg:
4577                print '  Dictionary of files _______'
4578                gen.printing_dictionary(files)
4579
4580                print '  Dictionary of test-files _______'
4581                gen.printing_dictionary(testfiles)
4582         
4583            allcompvar, Nvar = compvars_listconstruct(cnf, Modinf, files, testfiles, \
4584              iwdir, owdir, dbg)
4585        else:
4586            if dbg:
4587                print warnmsg
4588                print '  '+main+": getting variables to compute already from file !!"
4589            files, testfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
4590
4591        # End of avoiding to repeat all the experiment search
4592
4593        print "    For experiment '" + exp + "' is required to compute:", Nvar,      \
4594          "variables"
4595
4596        if dbg:
4597            print 'Variables to compute _______'
4598            gen.printing_dictionary(allcompvar)
4599
4600        # Computing variables
4601        print "    Computing variables ..."
4602        compute_vars(cnf, Modinf, iwdir, owdir, files, allcompvar, filescratch, dbg)
4603
4604        # Figures of variables direct from files
4605        print "    Plotting direct figures ..."
4606        dirfigf = owdir + '/directplotsdraw.inf'
4607        if figscratch:
4608            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4609
4610            objf = open(owdir+'/all_figures.inf','w')
4611            objf.write("## Drawing of all figures \n")
4612            objf.close()
4613
4614        if addfigures:
4615            if dbg:
4616                print '  ' + main + ": adding direct figures removing file '" +      \
4617                  dirfigf + "' ..."
4618            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4619
4620        if not os.path.isfile(dirfigf):
4621            listplots, Nplt = plots_listconstruct(cnf, 'DIRPLT', dirfigf, owdir, dbg)
4622        else:
4623            if dbg:
4624                print warnmsg
4625                print '  ' + main + ": getting plots to draw already from file !!"
4626            listplots, Nplt = read_plot_file(dirfigf)
4627
4628        # End of avoiding to repeat all the plots search
4629
4630        print "    For experiment '" + exp + "' is required to plot:", Nplt, "plots"
4631
4632        if dbg:
4633            print 'Plots to draw _______'
4634            gen.printing_dictionary(listplots)
4635
4636        draw_plots(cnf, listplots, mod, exp, owdir, allcompvar, figscratch, dbg)
4637
4638    # end of experiments loop
4639
4640### ## #
4641# Experiment differences
4642## # ## #
4643
4644# Experiments loop
4645##
4646    print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4647    print "  ** '" + mod + "': Inter experiments differences  "
4648    print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4649    print '    experiments:', exps
4650    # There are two kind of differences:
4651    #   DIFFop: differences between operations of each given variable.
4652    #     Differences are computed directly from the last stage of the operation
4653    #   DIFFvar: operations of differences between variables
4654    #     First are computed the differences from the initial variable file
4655    #     and then operations are made
4656    # NOTE: remember that: meanvar2 - meanvar1 = mean(var2 - var1)
4657    difffiles = ['diffop.inf', 'diffvar.inf']
4658
4659    Nexps = len(exps)
4660    for iexp1 in range(0,Nexps-1):
4661        exp1 = exps[iexp1]
4662        for iexp2 in range(iexp1+1,Nexps):
4663            exp2 = exps[iexp2]
4664            Sexps = exp2 + '-' + exp1
4665            print '  ' + Sexps + '...'
4666            owdir = cnf['ofold'] + '/' + mod + '/' + exp2 + '-' + exp1
4667            sout = sub.call('mkdir -p ' + owdir, shell=True)
4668
4669            # Removing files with list of differences if should be started from scratch or add differences
4670            for fdiff in difffiles:
4671                difff = owdir + '/' + fdiff
4672                if diffscratch:
4673                    sub.call('rm -rf' + owdir +' >& /dev/null', shell=True)
4674                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4675                    objf = open(owdir+'/all_' + fdiff,'w')
4676                    objf.write("## Computation and drawing of differences " +        \
4677                      "between '" + exp2 + "'-'" + exp1 + "'\n")
4678                    objf.close()
4679                    difff = owdir + '/all_vardiffstatistics.inf'
4680                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4681                    objf = open(owdir+'/all_' + fdiff,'w')
4682                    objf.write("## Computation of all differences statistics " +     \
4683                      "between '" + exp2 + "'-'" + exp1 + "'\n")
4684                    objf.close()
4685
4686                if adddiffs:
4687                    if dbg:
4688                        print '  ' + main + ": adding differences removing file '" + \
4689                          difff + "' ..."
4690                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4691
4692            # op and var differences
4693            diffvarcompf = owdir + '/' + difffiles[0]
4694            print 'Lluis HERE !2!1!: diffvarcompf:', diffvarcompf
4695            if not os.path.isfile(diffvarcompf):
4696                alldiffop, alldiffvar,Nopdiffs,Nvardiffs=diffvarop_listconstruct(cnf,\
4697                  [mod, mod], [exp1, exp2], [False, False], owdir, dbg)
4698            else:
4699                if dbg:
4700                    print warnmsg
4701                    print '  ' + main + ": getting 'op' and 'var' differences to " + \
4702                      "compute already from file !!"
4703                alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
4704                alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
4705                print 'Nopdiffops:', Nopdiffops,'Nopdiffvars:', Nopdiffvars
4706
4707            # End of avoiding to repeat all the experiment search
4708            print "    For experiments '"+exp2+"'-'"+exp1+"' is required to " +      \
4709              "compute:", Nvar, "differences"
4710
4711            if dbg:
4712                print 'Differences to compute _______'
4713                gen.printing_dictionary(alldiffop)
4714                gen.printing_dictionary(alldiffvar)
4715
4716            # Computing differences
4717            ##
4718            print "    Computing operation differences ..."
4719            compute_op_diffs(cnf, alldiffop, owdir, diffscratch, dbg)
4720            print "    Computing variable differences ..."
4721            compute_var_diffs(cnf, alldiffvar, owdir, diffscratch, dbg)
4722
4723            # Plotting operation differences
4724            ##
4725            print "  " + main + ": Plotting operation differences' figures ..."
4726            dirfigf = owdir + '/diffopplotsdraw.inf'
4727            if figscratch:
4728                sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4729
4730                objf = open(owdir+'/all_diffopfigures.inf','w')
4731                objf.write("## Drawing of all operation difference figures \n")
4732                objf.close()
4733
4734            if adddifffigures:
4735                if dbg:
4736                    print '  ' + main + ": adding differences' figures removing " +  \
4737                      "file '" + dirfigf + "' ..."
4738
4739                sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4740
4741            if not os.path.isfile(dirfigf):
4742                listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFOP', dirfigf,    \
4743                  owdir, dbg)
4744            else:
4745                if dbg:
4746                    print warnmsg
4747                    print '  ' + main + ": getting plots to draw already from file !!"
4748                listplots, Nplt = read_plot_file(dirfigf)
4749
4750            # End of avoiding to repeat all the plots search
4751
4752            print "  For experiment 'operation' differences '" + Sexps + "' is " +   \
4753              "required to plot:" , Nplt, "plots"
4754
4755            if dbg:
4756                print '    Plots to draw _______'
4757                gen.printing_dictionary(listplots)
4758
4759            draw_diff_plots(cnf, listplots, owdir, alldiffop, 'diffop',              \
4760              figdiffscratch, dbg)
4761
4762            # Plotting variable differences
4763            ##
4764            print "  " + main + ": Plotting variable differences' figures ..."
4765            dirfigf = owdir + '/diffvarplotsdraw.inf'
4766            if figscratch:
4767                sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4768
4769                objf = open(owdir+'/all_diffvarfigures.inf','w')
4770                objf.write("## Drawing of all variables difference figures \n")
4771                objf.close()
4772
4773            if adddifffigures:
4774                if dbg:
4775                    print '  '+main+": adding differences' figures removing file '" +\
4776                      dirfigf + "' ..."
4777                sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4778
4779            if not os.path.isfile(dirfigf):
4780                listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFVAR', dirfigf,   \
4781                  owdir, dbg)
4782            else:
4783                if dbg:
4784                    print warnmsg
4785                    print '  ' + main + ": getting plots to draw already from file !!"
4786                listplots, Nplt = read_plot_file(dirfigf)
4787
4788            # End of avoiding to repeat all the plots search
4789
4790            print "  For experiment 'variables' differences '" + Sexps + "' is " +   \
4791              "required to plot:" , Nplt, "plots"
4792 
4793            if dbg:
4794                print '    Plots to draw _______'
4795                gen.printing_dictionary(listplots)
4796
4797            draw_diff_plots(cnf, listplots, owdir, alldiffvar, 'diffvar',            \
4798              figdiffscratch, dbg)
4799
4800# end of mods loop
4801
4802### ## #
4803# Model differences
4804### ## #
4805
4806# Models already at the reference projection
4807ModComProj = cnf['RefProj'].split(':')
4808if dbg:
4809    print 'Models to use as reference for the projection:', ModComProj
4810
4811# Operators to use for the reprojection of each variable
4812REPRJvar0 = gen.get_specdictionary_HMT(cnf, H='reprojectvar_')
4813REPRJvar = {}
4814for Skey in REPRJvar0.keys():
4815    REPRJvar[Skey] = gen.str_list(REPRJvar0[Skey],':')
4816
4817if dbg:
4818    print 'Remaping operators to use _______'
4819    gen.printing_dictionary(REPRJvar)
4820
4821# Getting common base weights for grid point remapping with CDO
4822frefgridn = 'RefProj.nc'
4823for mod in mods:
4824    frefgriddes = cnf['ofold'] + '/' + mod + '/' + frefgridn
4825    if moddiffscratch: 
4826        sout = sub.call('rm ' + frefgriddes + ' >& /dev/null', shell=True)
4827    if gen.searchInlist(ModComProj,mod) and not os.path.isfile(frefgriddes):
4828        # Looking for a file to use (all files are CF compilant, but we need to
4829        #   be sure that at least they have 'lon', 'lat' variables)
4830        exps = modexps[mod]
4831        exp = exps[0]
4832        reprjfdir = cnf['ofold'] + '/' + mod + '/' + exp
4833        reprjfile = None
4834        for reprjop in REPRJvar.keys():
4835            reprjvars = REPRJvar[reprjop]
4836            for reprjvar in reprjvars:
4837                reprjfs= gen.files_folder_HMT(folder=reprjfdir,head=reprjvar+'_',tail='.nc')
4838                for reprjf in reprjfs:
4839                    # Getting direct files as [var]_[fhead].nc
4840                    if len(reprjf.split('_')) == 2:
4841                        reprjfile = reprjfdir + '/' + reprjf
4842                        reprjvn = reprjvar
4843                        break
4844        if reprjfile is None:
4845            print errmsg
4846            print '  ' + main + ": no proper file to get projection information " +  \
4847              "from '" + reprjfdir + "' has been found !!"
4848            quit(-1)
4849
4850        print "  Creation of reference projection information file from '" + mod+ "'"
4851        if dbg:
4852            print "    using file: '" + reprjfile
4853
4854        # Getting only 1 time-step for disk space issues
4855        try:
4856             with gen.Capturing() as output:
4857                 ncvar.DataSetSection('time,0,1,1', reprjfile)
4858        except:
4859            print errmsg
4860            print 'ncvar.DataSetSection(time,0,1,1, ' + reprjfile + ')'
4861            for s1out in output: print s1out
4862            quit(-1)
4863
4864        of = reprjfile.split('.')[0] + '_time_B0-E1-I1.nc'
4865
4866        # Cleaning variables
4867        try:
4868             with gen.Capturing() as output:
4869                 ncvar.selvar('lon@lon,lat@lat,time@time', of, reprjvn)
4870        except:
4871            print errmsg
4872            print 'ncvar.selvar(lon@lon,lat@lat, ' + of + ', ' + reprjvn + ')'
4873            for s1out in output: print s1out
4874            quit(-1)
4875        sout = sub.call('mv selvar_new.nc ' + frefgriddes, shell=True)
4876        sout = sub.call('rm ' + of + ' >& /dev/null', shell=True)
4877
4878        cnf['RefProjfile'] = frefgriddes
4879
4880        if dbg:
4881            for s1out in output: print s1out
4882        break
4883
4884    elif gen.searchInlist(ModComProj,mod) and os.path.isfile(frefgriddes):
4885        # Including reprojecting reference file in configuration dictionary
4886        cnf['RefProjfile'] = frefgriddes
4887
4888Nmods = len(mods)
4889for imod1 in range(0,Nmods-1):
4890    mod1 = mods[imod1]
4891    exps1 = modexps[mod1]
4892
4893    for imod2 in range(imod1+1,Nmods):
4894        mod2 = mods[imod2]
4895        exps2 = modexps[mod2]
4896
4897        Smods = mod2 + '-' + mod1
4898        print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4899        print "  ** '" + Smods + "': Inter models differences  "
4900        print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4901        print '    experiments mod1:', exps1
4902        print '    experiments mod2:', exps2
4903
4904# Experiments loop
4905##
4906        difffiles = ['diffop.inf', 'diffvar.inf']
4907
4908        Nexps1 = len(exps1)
4909        Nexps2 = len(exps2)
4910        for iexp1 in range(0,Nexps1):
4911            exp1 = exps1[iexp1]
4912            for iexp2 in range(0,Nexps2):
4913                exp2 = exps2[iexp2]
4914                Sexps = exp2 + '-' + exp1
4915                print '    ' + Sexps + '...'
4916                owdir = cnf['ofold'] + '/' + Smods + '/' + Sexps
4917                sout = sub.call('mkdir -p ' + owdir, shell=True)
4918
4919                # Getting the right projection in order to perform differences
4920                difmods = [mod1, mod2]
4921                difexps = [exp1, exp2]
4922                difreproj = [False, False]
4923               
4924                for imod in range(2):
4925                    mod = difmods[imod]
4926                    exp = difexps[imod]
4927                    if not gen.searchInlist(ModComProj,mod):
4928                        print  "  Projecting files of model '" + mod + "' exp: '" +  \
4929                          exp + "' with file '" + cnf['RefProjfile'] + "'"
4930                        reproject_modexp(mod, exp, cnf, REPRJvar, moddiffscratch,    \
4931                          addmoddiffs, dbg)
4932                        difreproj[imod] = True
4933
4934                # Removing files with list of differences if should be started from
4935                #   scratch or add differences
4936                for fdiff in difffiles:
4937                    difff = owdir + '/' + fdiff
4938                    if moddiffscratch:
4939                        sub.call('rm -rf' + owdir +' >& /dev/null', shell=True)
4940                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4941                        objf = open(owdir+'/all_' + fdiff,'w')
4942                        objf.write("## Computation and drawing of differences " +    \
4943                          "between '" + mod2+'@'+exp2 + "'-'" + mod1+'@'+exp1 + "'\n")
4944                        objf.close()
4945                        difff = owdir + '/all_vardiffstatistics.inf'
4946                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4947                        objf = open(owdir+'/all_' + fdiff,'w')
4948                        objf.write("## Computation of all differences statistics " + \
4949                          "between '" + mod2+'@'+exp2 + "'-'" + mod1+'@'+exp1 + "'\n")
4950                        objf.close()
4951
4952                    if addmoddiffs:
4953                        if dbg:
4954                            print '  ' + main + ": adding model differences " +      \
4955                              "removing file '" + difff + "' ..."
4956                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4957
4958                # op and var differences
4959                diffvarcompf = owdir + '/' + difffiles[0]
4960                if not os.path.isfile(owdir+'/'+difffiles[0]) or                     \
4961                  not os.path.isfile(owdir+'/'+difffiles[1]):
4962                    alldiffop, alldiffvar, Nopdiffs, Nvardiffs =                     \
4963                      diffvarop_listconstruct(cnf, difmods, difexps, difreproj,owdir,\
4964                      dbg)
4965                else: 
4966                    if dbg:
4967                        print warnmsg
4968                        print '  '+main+": getting 'op' and 'var' differences to " + \
4969                          "compute already from file !!"
4970                    alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
4971                    alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
4972
4973                # End of avoiding to repeat all the experiment search
4974
4975                print "    For experiments '" + mod2+'@'+exp2 + "'-'" + mod+'@'+exp1+\
4976                  "' is required to compute:", Nvar, "differences"
4977
4978                if dbg:
4979                    print '    op differences to compute _______'
4980                    gen.printing_dictionary(alldiffop)
4981                    print '    var differences to compute _______'
4982                    gen.printing_dictionary(alldiffvar)
4983
4984# Computing differences
4985##
4986                print "    Computing operation differences ..."
4987                compute_op_diffs(cnf, alldiffop, owdir, moddiffscratch, dbg)
4988                print "    Computing variable differences ..."
4989                compute_var_diffs(cnf, alldiffvar, owdir, moddiffscratch, dbg)
4990
4991# Plotting operation differences
4992##
4993                print "  " + main + ": Plotting operation differences' figures ..."
4994                dirfigf = owdir + '/diffopplotsdraw.inf'
4995                if figmoddiffscratch:
4996                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4997
4998                objf = open(owdir+'/all_diffopfigures.inf','w')
4999                objf.write("## Drawing of all operation difference figures \n")
5000                objf.close()
5001
5002                if addmoddifffigures:
5003                    if dbg:
5004                        print '  ' + main + ": adding model differences' figures " + \
5005                          "removing file '" + dirfigf + "' ..."
5006                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5007
5008                if not os.path.isfile(dirfigf):
5009                    listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFOP', dirfigf,\
5010                      owdir, dbg)
5011                else:
5012                    if dbg:
5013                        print warnmsg
5014                        print '  '+main+": getting plots to draw already from file !!"
5015                    listplots, Nplt = read_plot_file(dirfigf)
5016
5017                # End of avoiding to repeat all the plots search
5018
5019                print "  For experiment 'operation' differences '" + Sexps + "' is "+\
5020                  "required to plot:" , Nplt, "plots"
5021
5022                if dbg:
5023                    print 'Plots to draw _______'
5024                    gen.printing_dictionary(listplots)
5025
5026                draw_diff_plots(cnf, listplots, owdir, alldiffop, 'diffop',          \
5027                  figmoddiffscratch, dbg)
5028
5029                # Plotting variable differences
5030                ##
5031                print "  " + main + ": Plotting variable differences' figures ..."
5032                dirfigf = owdir + '/diffvarplotsdraw.inf'
5033                if figscratch:
5034                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5035
5036                    objf = open(owdir+'/all_diffvarfigures.inf','w')
5037                    objf.write("## Drawing of all variables difference figures \n")
5038                    objf.close()
5039       
5040                if adddifffigures:
5041                    if dbg:
5042                        print '  ' + main + ": adding differences's figures " +      \
5043                          "removing file '" + dirfigf + "' ..."
5044                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5045
5046                if not os.path.isfile(dirfigf):
5047                    listplots, Nplt = plots_listconstruct(cnf,'PLOTDIFFVAR',dirfigf, \
5048                      owdir, dbg)
5049                else:
5050                    if dbg:
5051                        print warnmsg
5052                        print '  '+main+": getting plots to draw already from file !!"
5053                    listplots, Nplt = read_plot_file(dirfigf)
5054
5055                # End of avoiding to repeat all the plots search
5056
5057                print "  For experiment 'variables' differences '" + Sexps+ "' is "+ \
5058                  "required to plot:" , Nplt, "plots"
5059
5060                if dbg:
5061                    print '    Plots to draw _______'
5062                    gen.printing_dictionary(listplots)
5063
5064                draw_diff_plots(cnf, listplots, owdir, alldiffvar, 'diffvar',        \
5065                  figdiffscratch, dbg)
5066
5067            # end of exp2 loop
5068        # end of exp1 loop
5069    # end of mod2 loop
5070# end of mod1 loop
5071
5072### ## #
5073# All model/exps linear plots
5074### ## #
5075
5076allmodexps = []
5077for mod in mods:
5078    for exp in modexps[mod]:
5079        allmodexps.append(mod + '/' + exp)
5080
5081print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5082print '  ** Multi models, multi experiments plots  '
5083print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5084print '    Model/experiments:', allmodexps
5085
5086owdir = cnf['ofold'] + '/allmodexps'
5087sout = sub.call('mkdir -p ' + owdir, shell=True)
5088
5089# Plotting all model-experiments lines
5090##
5091print "  " + main + ": Plotting all model-experiments variable figures ..."
5092allf = owdir + '/allmodexp.inf'
5093dirfigf = owdir + '/allmodexpfigures.inf'
5094allfigf = owdir+'/all_figures.inf'
5095if figallmodexpscratch:
5096    sout = sub.call('rm ' + allf + ' >& /dev/null', shell=True)
5097    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5098    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
5099
5100    objf = open(allfigf,'w')
5101    objf.write("## Drawing of all variables figures for all model-experiments\n")
5102    objf.close()
5103       
5104if addallmodexpfigures:
5105    if dbg:
5106        print '  ' + main + ": adding model-experiment differences removing " +      \
5107          " file '" + allfigf + "' ..."
5108        print '  ' + main + ": adding model-experiment differences' figures " +      \
5109          "removing file '" + dirfigf + "' ..."
5110    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
5111    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5112
5113if not os.path.isfile(dirfigf):
5114    listplots, Nplt = plots_listconstruct(cnf, 'PLOTALLMODEXP', dirfigf, owdir, dbg)
5115else:
5116    if dbg:
5117        print warnmsg
5118        print '  ' + main + ": getting plots to draw already from file !!"
5119    listplots, Nplt = read_plot_file(dirfigf)
5120
5121# End of avoiding to repeat all the plots search
5122
5123print "  Is required to plot:", Nplt, " all model-experiment plots"
5124
5125if dbg:
5126    print '    Plots to draw _______'
5127    gen.printing_dictionary(listplots)
5128
5129if not os.path.isfile(allf):
5130    # Constructing the information of modexp / variable
5131    allmodexpvar, Nallmodexpvar = allmodexps_listconstruct(cnf, allmodexps, owdir, dbg)
5132else:
5133    allmodexpvar, Nallmodexpvar = allmodexps_read(allf)
5134
5135draw_allmodexp_plots(cnf, allmodexpvar, listplots, modexps, owdir, figallmodexpscratch, dbg)
5136
5137print main + ': all files and figures have been properly done !!'
5138
5139# Creation of a pdf with all figures
5140##
5141###
5142owdir = cnf['ofold']
5143os.chdir(owdir)
5144
5145texout ='model_graphics-images'
5146print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5147print '  ** Creation of a pdf with all figures  '
5148print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5149print '  ' + owdir + '/' + texout + '.pdf'
5150
5151if dbg:
5152    print main + ": generation of a pdf file '" + owdir + '/' + texout +             \
5153      "' with all the figures..."
5154
5155otexf = open(owdir + '/' + texout + '.tex', 'w')
5156otexf.write('\\documentclass{article}\n')
5157otexf.write('\\usepackage{graphicx}\n')
5158otexf.write('\\usepackage[colorlinks=true,urlcolor=blue]{hyperref}\n')
5159otexf.write('\\textheight=23cm\n')
5160otexf.write('\\textwidth=18cm\n')
5161otexf.write('\\oddsidemargin=-1cm\n')
5162otexf.write('\\evensidemargin=-1cm\n')
5163otexf.write('\\topmargin=-1cm\n')
5164
5165otexf.write('\n')
5166otexf.write('\\begin{document}\n')
5167otexf.write('\n')
5168otexf.write('\\def\\fontBloc{\\fontfamily{\\sfdefault}\\large\\bfseries}\n')
5169otexf.write('\\def\\linia{\\setlength{\\unitlength}{1mm}\n')
5170otexf.write('\\line(1,0){80}}\n')
5171otexf.write('\\newcommand{\\modexp}[1]{\\clearpage\n')
5172otexf.write('\\noindent\\linia\n')
5173otexf.write('\\section{#1}\n')
5174otexf.write('\\linia}\n')
5175otexf.write('\n')
5176
5177otexf.write('\\title{model\_graphics: ' + gen.latex_text(owdir) + '}\n')
5178otexf.write('\\maketitle\n')
5179otexf.write('\n')
5180otexf.write('\\tableofcontents\n')
5181otexf.write('\\listoffigures\n')
5182otexf.write('\\clearpage\n')
5183
5184Nmods = len(mods)
5185lmodexps = []
5186ldiffmodexps = []
5187# Figures from mod/exp pairs
5188for mod in mods:
5189    exps = modexps[mod]
5190    for exp in exps:
5191        print '  direct plots:', mod, exp
5192        lmodexps.append(mod+'/'+exp)
5193        owdirme = owdir + '/' + mod + '/' + exp
5194        otexf.write('% ' + mod + ' ' + exp + '\n')
5195        otexf.write('\\modexp{' + gen.latex_text(mod+' '+exp) + '}\n')
5196        listplots, Nplt = read_plot_file(owdirme + '/directplotsdraw.inf')
5197        for plot in listplots.keys():
5198            plots = listplots[plot]
5199            ops = []
5200            for plt in plots:
5201                opn = plt.split('#')[0].split('|')[1]
5202                if not gen.searchInlist(ops,opn): ops.append(opn)
5203            for op in ops:
5204                opS = op.replace('+','_')
5205                modexpimg = gen.files_folder_HMT(owdirme, head=plot, middle=opS,     \
5206                  tail=cnf['kindfig'])
5207                Nimages = len(modexpimg)
5208                if Nimages > 0:
5209                    for img in range(Nimages): modexpimg[img] = owdirme + '/' +      \
5210                      modexpimg[img]
5211                    caption = mod + ' ' + exp + ' ' + plot + ' ' + op
5212                    flab = 'fig:' + caption.replace(' ', '_')
5213                    try:
5214                        with gen.Capturing() as output:
5215                            gen.latex_fig_array(modexpimg, otexf,                    \
5216                              gen.latex_text(caption), flab, refsize=0.5, dist='sqr',\
5217                              dorest='center')
5218                    except:
5219                        print errmsg
5220                        print 'gen.latex_fig_array(',modexpimg,', '+otexf+', '+      \
5221                              'gen.latex_text(' + caption + '), ' + flab +           \
5222                              ", refsize=0.5, dist='sqr', dorest='center')"
5223                        for s1out in output: print s1out
5224                        quit(-1)
5225                    otexf.write('%\\clearpage\n')
5226
5227    # differences between experiments
5228    Nexps = len(exps)
5229    for iexp1 in range(0,Nexps-1):
5230        exp1 = exps[iexp1]
5231        for iexp2 in range(iexp1+1,Nexps):
5232            exp2 = exps[iexp2]
5233            Sexps = exp2 + '-' + exp1
5234            owdirme = owdir + '/' + mod + '/' + Sexps
5235            print '    diffs:',  mod + '/' + Sexps + ' ...'
5236            ldiffmodexps.append(mod + '/' + Sexps)
5237            for diffn in ['op', 'var']:
5238                fileplotinf = owdirme + '/diff' + diffn + 'plotsdraw.inf'
5239                if os.path.isfile(fileplotinf):
5240                    otexf.write('% ' + mod + ' ' + Sexps + ' diff' + diffn + '\n')
5241                    otexf.write('\\modexp{' + gen.latex_text(mod+' '+Sexps+' diff'+  \
5242                      diffn)+ '}\n')
5243                    listplots, Nplt = read_plot_file(fileplotinf)
5244                    for plot in listplots.keys():
5245                        plots = listplots[plot]
5246                        ops = []
5247                        for plt in plots:
5248                            opn = plt.split('#')[0].split('|')[1]
5249                            if not gen.searchInlist(ops,opn): ops.append(opn)
5250                        for op in ops:
5251                            opS = op.replace('+','-')
5252                            modexpimg = gen.files_folder_HMT(owdirme, head=plot,     \
5253                              middle=opS, tail=cnf['kindfig'])
5254                            Nimages = len(modexpimg)
5255                            if Nimages > 0:
5256                                for img in range(Nimages): modexpimg[img] = owdirme +\
5257                                  '/' + modexpimg[img]
5258                                caption = mod + ' ' + Sexps + ' ' + plot + ' ' + op +\
5259                                  ' diff' + diffn
5260                                flab = 'fig:' + caption.replace(' ', '_')
5261                                try:
5262                                    with gen.Capturing() as output:
5263                                        gen.latex_fig_array(modexpimg, otexf,        \
5264                                          gen.latex_text(caption), flab, refsize=0.5,\
5265                                          dist='sqr', dorest='center')
5266                                except:
5267                                    print errmsg
5268                                    print 'gen.latex_fig_array(',modexpimg,', '+     \
5269                                      otexf+', gen.latex_text(' + caption + '), ' +  \
5270                                      flab + ", refsize=0.5, dist='sqr', " +         \
5271                                      "dorest='center')"
5272                                    for s1out in output: print s1out
5273                                    quit(-1)
5274
5275                                otexf.write('%\\clearpage\n')
5276print '    mods-exps diffs ...'
5277for imod1 in range(0,Nmods-1):
5278    mod1 = mods[imod1]
5279    exps1 = modexps[mod1]
5280    for imod2 in range(imod1+1,Nmods):
5281        mod2 = mods[imod2]
5282        exps2 = modexps[mod2]
5283
5284        Smods = mod2 + '-' + mod1
5285        Nexps1 = len(exps1)
5286        Nexps2 = len(exps2)
5287        for iexp1 in range(0,Nexps1):
5288            exp1 = exps1[iexp1]
5289            for iexp2 in range(0,Nexps2):
5290                exp2 = exps2[iexp2]
5291                Sexps = exp2 + '-' + exp1
5292                print '      ' + Smods + ' ' + Sexps + ' ...'
5293                ldiffmodexps.append(Smods + '/' + Sexps)
5294                owdirme = owdir + '/' + Smods + '/' + Sexps
5295                for diffn in ['op', 'var']:
5296                    fileplotinf = owdirme + '/diff' + diffn + 'plotsdraw.inf'
5297                    if os.path.isfile(fileplotinf):
5298                            otexf.write('% '+Smods + ' ' + Sexps + ' diff'+diffn+'\n')
5299                            otexf.write('\\modexp{' + gen.latex_text(Smods+' '+Sexps+\
5300                              ' diff'+diffn) + '}\n')
5301                            listplots, Nplt = read_plot_file(fileplotinf)
5302                            for plot in listplots.keys():
5303                                plots = listplots[plot]
5304                                ops = []
5305                                for plt in plots:
5306                                    opn = plt.split('#')[0].split('|')[1]
5307                                    if not gen.searchInlist(ops,opn): ops.append(opn)
5308                                for op in ops:
5309                                    opS = op.replace('+','-')
5310                                    modexpimg = gen.files_folder_HMT(owdirme,        \
5311                                      head=plot, middle=opS, tail=cnf['kindfig'])
5312                                    Nimages = len(modexpimg)
5313                                    if Nimages > 0:
5314                                        for img in range(Nimages): modexpimg[img] =  \
5315                                          owdirme + '/' + modexpimg[img]
5316                                        caption = Smods + ' ' + Sexps + ' ' + plot + \
5317                                          ' ' + op + ' diff' + diffn
5318                                        flab = 'fig:' + caption.replace(' ', '_')
5319                                        try:
5320                                            with gen.Capturing() as output:
5321                                                gen.latex_fig_array(modexpimg, otexf,\
5322                                                  gen.latex_text(caption), flab,     \
5323                                                  refsize=0.5, dist='sqr',           \
5324                                                  dorest='center')
5325                                        except:
5326                                            print errmsg
5327                                            print 'gen.latex_fig_array(',modexpimg,  \
5328                                              ', '+otexf+', gen.latex_text(' +       \
5329                                              caption + '), '+flab+ ", refsize=0.5,"+\
5330                                              " dist='sqr', dorest='center')"
5331                                            for s1out in output: print s1out
5332                                            quit(-1)
5333                                        otexf.write('%\\clearpage\n')
5334
5335# allmodexp
5336owdirme = owdir + '/allmodexps'
5337print '    allmodexps...'
5338fileplotinf = owdirme + '/allmodexpfigures.inf'
5339if os.path.isfile(fileplotinf):
5340    otexf.write('% allmodexps\n')
5341    otexf.write('\\modexp{allmodexps}\n')
5342    listplots, Nplt = read_plot_file(fileplotinf)
5343    for plot in listplots.keys():
5344        plots = listplots[plot]
5345        ops = []
5346        for plt in plots:
5347            opn = plt.split('|')[1]
5348            if not gen.searchInlist(ops,opn): ops.append(opn)
5349        for op in ops:
5350            opS = op.replace('+','-')
5351            modexpimg = gen.files_folder_HMT(owdirme, head=plot, middle=opS,         \
5352              tail=cnf['kindfig'])
5353            Nimages = len(modexpimg)
5354            if Nimages > 0:
5355                for img in range(Nimages): modexpimg[img] = owdirme + '/' +          \
5356                    modexpimg[img]
5357                caption = 'allmodexps ' + plot + ' ' + op + ' diff' + diffn
5358                flab = 'fig:' + caption.replace(' ', '_')
5359                try:
5360                    with gen.Capturing() as output:
5361                        gen.latex_fig_array(modexpimg,otexf, gen.latex_text(caption),\
5362                          flab, refsize=0.9, dist='sqr', dorest='center')
5363                except:
5364                    print errmsg
5365                    print 'gen.latex_fig_array(',modexpimg,', '+otexf+               \
5366                      ', gen.latex_text(' + caption + '), ' +flab+ ", refsize=0.9," +\
5367                      " dist='sqr', dorest='center')"
5368                    for s1out in output: print s1out
5369                    quit(-1)
5370
5371                otexf.write('%\\clearpage\n')
5372#
5373# Grouping by type of figure
5374#
5375Nmods = len(mods)
5376# Figures from mod/exp pairs
5377owsearch = owdir + '/' + mods[0] + '/' + modexps[mods[0]][0]
5378print "  Looking in '" + owsearch + "' for figures by kind and variable..."
5379otexf.write('% figures by kind and variable from:' + owsearch + '\n')
5380modexpimg = gen.files_folder_HMT(owsearch, tail=cnf['kindfig'])
5381# dictionary with kind of plots
5382dplots = {}
5383# list with kind of plots
5384lplots = []
5385# dictionary with variable-statistics of each kind of plot
5386dvarstats = {} 
5387# list with variables of each kind of plot
5388lvars = []
5389for plot in modexpimg:
5390    Nvars, plotk, varn, statn = figinf_figname(plot,cnf)
5391    if not gen.searchInlist(lplots,plotk):
5392        lplots.append(plotk)
5393        if not dplots.has_key(plotk+'_'+varn):
5394            if not gen.searchInlist(lvars,varn): lvars.append(varn)
5395            dplots[plotk+'_'+varn] = [statn]
5396        else:
5397            vals = dplots[plotk+'_'+varn]
5398            dplots[plotk+'_'+varn] = vals + [statn]
5399    else:
5400        if not dplots.has_key(plotk+'_'+varn):
5401            if not gen.searchInlist(lvars,varn): lvars.append(varn)
5402            dplots[plotk+'_'+varn] = [statn]
5403        else:
5404            vals = dplots[plotk+'_'+varn]
5405            dplots[plotk+'_'+varn] = vals + [statn]
5406       
5407# Figures found
5408print '    direct figures to group:', modexpimg
5409print '      plots:', lplots
5410print '      variables:', lvars
5411
5412for dplot in dplots.keys():
5413    plot = dplot.split('_')[0]
5414    var = dplot.split('_')[1]
5415    otexf.write('% ' + var + '\n')
5416    otexf.write('\\modexp{' + gen.latex_text(var) + '}\n')
5417    varn1 = var.split('-')[0]
5418    varn2 = var.split('-')[1]
5419    varn = varn1+'-'+varn2
5420    for stn12 in dplots[dplot]:
5421        stn1 = stn12.split('@')[0]
5422        stn2 = stn12.split('@')[1]
5423        stn = gen.lstring_values(stn12, '@',' & ')
5424        plotvarfigs = []
5425        caption = varn.replace('-',' & ') + ' ' + plot + ' ' + stn
5426        flab = 'fig:' + varn + '_' + plot + '_' + stn12 + '_allmodexp'
5427        for me in lmodexps:
5428            modexpn = expGC[me].label
5429            modn = me.split('/')[0]
5430            imgns = gen.files_folder_HMT(owdir+'/'+me, head=plot+'_'+varn1,          \
5431              middle=stn1+'-'+varn2, tail=stn2+'.'+cnf['kindfig'])
5432            caption = caption + ', ' + modexpn
5433            imgn = owdir+'/'+me+'/'+imgns[0]
5434            if os.path.isfile(imgn):
5435                plotvarfigs.append(imgn)
5436        if dbg:
5437            print '      Grouping figures:', plotvarfigs
5438        try:
5439            with gen.Capturing() as output:
5440                gen.latex_fig_array(plotvarfigs, otexf, gen.latex_text(caption),     \
5441                  flab, refsize=0.9, dist='sqr', dorest='center')
5442        except:
5443            print errmsg
5444            print 'gen.latex_fig_array(',plotvarfigs,', '+otexf+', gen.latex_text(' +\
5445               caption + '), ' + flab + ", refsize=0.9, dist='sqr', dorest='center')"
5446            for s1out in output: print s1out
5447            quit(-1)
5448
5449Nmods = len(mods)
5450# Figures from mod/exp differences
5451owsearch = owdir + '/' + ldiffmodexps[0]
5452print "  Looking in '" + owsearch + "' for difference figures by kind and variable..."
5453otexf.write('% difference figures by kind and variable from:' + owsearch + '\n')
5454modexpimg = gen.files_folder_HMT(owsearch, tail=cnf['kindfig'])
5455# dictionary with kind of plots
5456dplots = {}
5457# list with kind of plots
5458lplots = []
5459# dictionary with variable-statistics of each kind of plot
5460dvarstats = {} 
5461# list with variables of each kind of plot
5462lvars = []
5463for plot in modexpimg:
5464    Nvars, plotk, varn, statn = figinf_figname(plot,cnf)
5465    if not gen.searchInlist(lplots,plotk):
5466        lplots.append(plotk)
5467        if not dplots.has_key(plotk+'_'+varn):
5468            if not gen.searchInlist(lvars,varn): lvars.append(varn)
5469            dplots[plotk+'_'+varn] = [statn]
5470        else:
5471            vals = dplots[plotk+'_'+varn]
5472            dplots[plotk+'_'+varn] = vals + [statn]
5473    else:
5474        if not dplots.has_key(plotk+'_'+varn):
5475            if not gen.searchInlist(lvars,varn): lvars.append(varn)
5476            dplots[plotk+'_'+varn] = [statn]
5477        else:
5478            vals = dplots[plotk+'_'+varn]
5479            dplots[plotk+'_'+varn] = vals + [statn]
5480       
5481# Figures found
5482print '    difference figures to group:', modexpimg
5483print '      plots:', lplots
5484print '      variables:', lvars
5485
5486for dplot in dplots.keys():
5487    plot = dplot.split('_')[0]
5488    var = dplot.split('_')[1]
5489    otexf.write('% ' + var + '\n')
5490    otexf.write('\\modexp{diff ' + gen.latex_text(var) + '}\n')
5491    varn1 = var.split('-')[0]
5492    varn2 = var.split('-')[1]
5493    varn = varn1+'-'+varn2
5494    for stn12 in dplots[dplot]:
5495        stn1 = stn12.split('@')[0]
5496        stn2 = stn12.split('@')[1]
5497        stn = gen.lstring_values(stn12, '@',' & ')
5498        plotvarfigs = []
5499        caption = varn.replace('-',' & ') + ' ' + plot + ' ' + stn
5500        flab = 'fig:' + varn + '_' + plot + '_' + stn12 + '_alldiffmodexp'
5501        for me in ldiffmodexps:
5502            mods = me.split('/')[0]
5503            exps = me.split('/')[1]
5504            if exps.find('-') != -1:
5505                expn1 = exps.split('-')[0]
5506                expn2 = exps.split('-')[1]
5507            else:
5508                expn1 = exps
5509                expn2 = exps
5510
5511            if mods.find('-') != -1:
5512                modn1 = mods.split('-')[0]
5513                modn2 = mods.split('-')[1]
5514            else:
5515                modn1 = mods
5516                modn2 = mods
5517            modexpn1 = expGC[modn1 + '/' + expn1].label
5518            modexpn2 = expGC[modn2 + '/' + expn2].label
5519            modexpn = modexpn1 + '-' + modexpn2
5520
5521            modn = me.split('/')[0]
5522            imgns = gen.files_folder_HMT(owdir+'/'+me, head=plot+'_'+varn1,          \
5523              middle=stn1+'-'+varn2, tail=stn2+'.'+cnf['kindfig'])
5524            caption = caption + ', ' + modexpn
5525            imgn = owdir+'/'+me+'/'+imgns[0]
5526            if os.path.isfile(imgn):
5527                plotvarfigs.append(imgn)
5528        if dbg:
5529            print '      Grouping figures:', plotvarfigs
5530        try:
5531            with gen.Capturing() as output:
5532                gen.latex_fig_array(plotvarfigs, otexf, gen.latex_text(caption),     \
5533                  flab, refsize=0.9, dist='sqr', dorest='center')
5534        except:
5535            print errmsg
5536            print 'gen.latex_fig_array(',plotvarfigs,', '+otexf+', gen.latex_text('+ \
5537              caption + '), ' + flab + ", refsize=0.9, dist='sqr', dorest='center')"
5538            for s1out in output: print s1out
5539            quit(-1)
5540
5541otexf.write('\\end{document}\n')
5542otexf.close()
5543
5544try:
5545    with gen.Capturing() as output:
5546        ins = 'pdflatex ' + owdir + '/' + texout
5547        sout = sub.call(ins, shell=True)
5548except:
5549    print errormsg
5550    print '  ' + ins
5551    print sout
5552    for s1out in output: print s1out
5553    quit(-1)
5554
5555with gen.Capturing() as output:
5556    sout = sub.call(ins + '>& /dev/null', shell=True)
5557with gen.Capturing() as output:
5558    sout = sub.call(ins + '>& /dev/null', shell=True)
5559with gen.Capturing() as output:
5560    sout = sub.call(ins + '>& /dev/null', shell=True)
5561with gen.Capturing() as output:
5562    sout = sub.call(ins, shell=True)
5563if dbg:
5564    print sout
5565    for s1out in output: print s1out
5566
5567sub.call('evince ' + owdir + '/' + texout + '.pdf &', shell=True)
5568
Note: See TracBrowser for help on using the repository browser.