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

Last change on this file since 1311 was 1300, checked in by lfita, 9 years ago

Adding repetition of `pinterp' on error

File size: 187.6 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[0]
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 = 'flip@y'
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    if debug:
2717        if diffop is not None:
2718            print "  'op' differences ________"
2719            gen.printing_dictionary(diffop)
2720        if diffvar is not None:
2721            print "  'var' differences ________"
2722            gen.printing_dictionary(diffvar)
2723
2724    # Getting variable characteristics from each model/experiment
2725    idir1 = config['ofold'] + '/' + mods[0] + '/' + exps[0]
2726    idir2 = config['ofold'] + '/' + mods[1] + '/' + exps[1]
2727
2728    fvarcomp = 'varcompute.inf'
2729    if reprj[0]: fvc1 = 'reproj_' + fvarcomp
2730    else: fvc1 = fvarcomp
2731
2732    if reprj[1]: fvc2 = 'reproj_' + fvarcomp
2733    else: fvc2 = fvarcomp
2734
2735    files1, testfiles1, allcompvar1, Nvar1= read_varcomp_file(idir1 + '/' + fvc1)
2736    files2, testfiles2, allcompvar2, Nvar2= read_varcomp_file(idir2 + '/' + fvc2)
2737
2738    diffops = {}
2739    if diffop is not None:
2740        # Getting 'op' differences characteristics
2741        doops, opvar, indivopvar, varglobalp = get_differences_var(diffop,'op',debug)
2742
2743        Ndiffop = 0
2744        for vn in opvar.keys():
2745            vnops = opvar[vn]
2746            for op in vnops:
2747                vnop = vn + '_' + op
2748                if not allcompvar1.has_key(vnop):
2749                    print warnmsg
2750                    print '  ' + fname + ": DIFFOP variable+operation '" + vnop +    \
2751                      "' in '" + mods[0] + '/' + exps[0] + "' was not computed !!"
2752                    print '    skipping it!'
2753                    print '    variables computed:', allcompvar1
2754                    break
2755                if not allcompvar2.has_key(vnop):
2756                    print warnmsg
2757                    print '  ' + fname + ": DIFFOP variable+operation '" + vnop +    \
2758                      "' in '" + mods[1] + '/' + exps[1] + "' was not computed !!"
2759                    print '    skipping it!'
2760                    print '    variables computed:', allcompvar2
2761                    break
2762                vals1 = allcompvar1[vnop]
2763                vals2 = allcompvar2[vnop]
2764
2765                headerf1 = vals1[0]
2766                headerf2 = vals2[0]
2767
2768                diffops[vnop] = [mods[0],mods[1],exps[0],exps[1],headerf1,headerf2]
2769
2770                Ndiffop = Ndiffop + 1       
2771    else:
2772        Ndiffop = 0
2773
2774    diffvars = {}
2775    if diffvar is not None:
2776        # Getting 'var' differences characteristics
2777        doops,opvar,indivopvar,varglobalp = get_differences_var(diffvar,'var',debug)
2778
2779        Ndiffvar = 0
2780        for vn in opvar.keys():
2781            vnops = opvar[vn]
2782            for op in vnops:
2783                vnop = vn + '_' + op
2784                if not allcompvar1.has_key(vnop):
2785                    print warnmsg
2786                    print '  ' + fname + ": DIFFVAR variable+operation '" + vnop +   \
2787                      "' in '" + mods[0] + '/' + exps[0] + "' was not computed !!"
2788                    print '    skipping it!'
2789                    print '    variables computed:', allcompvar1
2790                    break
2791                if not allcompvar2.has_key(vnop):
2792                    print warnmsg
2793                    print '  ' + fname + ": DIFFVAR variable+operation '" + vnop +   \
2794                      "' in '" + mods[1] + '/' + exps[1] + "' was not computed !!"
2795                    print '    skipping it!'
2796                    print '    variables computed:', allcompvar2
2797                    break
2798                vals1 = allcompvar1[vnop]
2799                vals2 = allcompvar2[vnop]
2800   
2801                headerf1 = vals1[0]
2802                headerf2 = vals2[0]
2803
2804                diffvars[vnop] = [mods[0],mods[1],exps[0],exps[1],headerf1,headerf2]
2805
2806                Ndiffvar = Ndiffvar + 1
2807    else:
2808        Ndiffvar = 0
2809
2810    if diffop is not None:
2811        Sopdiffs = gen.dictKeysVals_stringList(diffops)
2812    else:
2813        Sopdiffs = 'none'
2814    # Outwritting the op diffferences file to avoid next time
2815    objf = open(odir + '/diffop.inf', 'w')
2816    objf.write('differences: ' + Sopdiffs + '\n')
2817    objf.write('Ndiff: ' + str(Ndiffop) + '\n')
2818    objf.close() 
2819
2820    if diffvar is not None:
2821        Svardiffs = gen.dictKeysVals_stringList(diffvars)
2822    else:
2823        Svardiffs = 'none'
2824    # Outwritting the var diffferences file to avoid next time
2825    objf = open(odir + '/diffvar.inf', 'w')
2826    objf.write('differences: ' + Svardiffs + '\n')
2827    objf.write('Ndiff: ' + str(Ndiffvar) + '\n')
2828    objf.close() 
2829
2830    return diffops, diffvars, Ndiffop, Ndiffvar
2831
2832def read_diff_file(difffile):
2833    """ Function to read the file with the information about the differences
2834      difffile= file with the information
2835    """
2836    fname = 'read_diff_file'
2837
2838    if not os.path.isfile(difffile):
2839        print errormsg
2840        print '  ' + fname + ": differences file '" + difffile + "' does not exist !!"
2841        quit(-1)
2842
2843    objf = open(difffile, 'r')
2844    for line in objf:
2845        if line[0:1] != '#' and len(line) > 1:
2846            values = line.replace('\n','').split(' ')
2847            if values[0] == 'differences:':
2848                if values[1] != 'none':
2849                    diffs = gen.stringList_dictKeysVals(values[1])
2850                else:
2851                    diffs = {} 
2852            elif values[0] == 'Ndiff:':
2853                Ndiffs = int(values[1])
2854
2855    objf.close()
2856
2857    return diffs, Ndiffs
2858
2859def compute_op_diffs(config, diffallop, odir, diffscr, debug):
2860    """ Function to compute operation differences
2861      config= experiment configuration
2862      diffallop= dictonary with the differences to compute
2863      odir= output folder
2864      diffscr= wether it should be done from the scratch
2865    """
2866    fname = 'compute_op_diffs'
2867
2868    # output folder
2869    ofold = config['ofold']
2870
2871    # Home of the python scripts
2872    pyH = config['pyHOME']
2873
2874    #  opsur = operation surnames
2875    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
2876    Opsurs = {}
2877    for opk in opsur:
2878        opn = opk.split('_')[1]
2879        vals = opsur[opk]
2880        Opsurs[opn] = vals.split(':')
2881
2882    # Getting in working dir
2883    os.chdir(odir)
2884
2885    # CF dimensions
2886    CFdims = ['lon', 'lat', 'pres', 'time']
2887
2888    # Trakcing file
2889    trkobjf = open(odir + '/all_diffop.inf', 'a')
2890
2891    for vnop in diffallop.keys():
2892        vn = vnop.split('_')[0]
2893        op = vnop.split('_')[1]
2894        opS = op.replace('+','_')
2895
2896        # `p' header add related to 'pinterp'
2897        if opS.find('pinterp') != -1: SgP = 'p'
2898        else: SgP = ''
2899
2900        # Variable name within the file
2901        vninF = varnoper(vn, op, Opsurs)
2902
2903        vals = diffallop[vnop]
2904        mod1 = vals[0]
2905        mod2 = vals[1]
2906        exp1 = vals[2]
2907        exp2 = vals[3]
2908        headerf1 = vals[4]
2909        headerf2 = vals[5]
2910        modexpdiff = mod2 + '/' + exp2 + ' - ' + mod1 + '/' + exp1
2911        modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
2912
2913        ifile1 = ofold + '/' + mod1 + '/' + exp1 + '/' + vn + '_' + headerf1 + SgP + \
2914          '_' + opS + '.nc'
2915        ifile2 = ofold + '/' + mod2 + '/' + exp2 + '/' + vn + '_' + headerf2 + SgP + \
2916          '_' + opS + '.nc'
2917        difffilen = odir + '/' + vn + '_diffop_' + modexpdiffS + '_' + opS + '.nc'
2918
2919        if diffscr:
2920            sout = sub.call('rm ' + difffilen + ' >& /dev/null', shell=True)
2921
2922        if not os.path.isfile(difffilen):
2923            if debug:
2924                print "    Computing operation difference '" + op + "' of '" + vn +  \
2925                  "' for: '" + modexpdiff
2926                print "    var in file: '" + vninF + "'"
2927
2928            # Range of the dimensions
2929            for CFd in CFdims:
2930                if CFd == CFdims[0]:
2931                    CFdimS = CFd + '|' + CFd + '|-1'
2932                else:
2933                    CFdimS = CFdimS + ';' + CFd + '|' + CFd + '|-1'
2934
2935            # Attributes of the variable
2936            varattrs = ncvar.ivattrs(ifile2,vninF)
2937            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
2938              varattrs['units'][0]
2939
2940            values = CFdimS + '@' + 'add|' + ifile2 + '|' + vninF + '%' + CFdimS +   \
2941              '@' + 'sub|' + ifile1 + '|' + vninF
2942            pyins = 'python ' + pyH + "/nc_var.py -o compute_opersvarsfiles -S '" +  \
2943              values + "' -v " + varvals
2944
2945            try:
2946                with gen.Capturing() as output:
2947                    ncvar.compute_opersvarsfiles(values,varvals)
2948            except:
2949                print errmsg
2950                print 'ncvar.compute_opersvarsfiles(' + values + ',' + varvals + ')'
2951                for s1out in output: print s1out
2952                quit(-1)
2953
2954            sout = sub.call('mv opersvarsfiles_'+vninF+'.nc ' + difffilen, shell=True)
2955
2956            # keeping all differences
2957            trkobjf.write('\n')
2958            trkobjf.write("#" + vn + ' ' + op + '\n')
2959            trkobjf.write( pyins + '\n')
2960
2961    trkobjf.close()
2962
2963    return
2964
2965def compute_diff_statistics(Ecnf, filen, ofold, varn, Opers, scr, db):
2966    """ Function to compute statistics from var diff files
2967      Ecnf= dictionary with the configuration of the experiment
2968      filen= variable difference file to use
2969      ofold= directory to write the output files
2970      varn= name of the variable
2971      Opers= kind of operation: (as possible multiple consecutive combination of operations separated by '+'
2972        [calc1]+[calc2] will compute first [calc1] and then [calc2]
2973        acc: temporal accumulated values
2974        diff: differences between models
2975        direct: no statistics
2976        last: last temporal value
2977        Lmean: latitudinal mean values
2978        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat])
2979        lmean: longitudinal mean values
2980        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
2981        pinterp: pressure interpolation (to the given $plevels)
2982        tmean: temporal mean values
2983        tstd: temporal standard deviation values
2984        tturb: Taylor's turbulence decomposition value (x - <x>) for time
2985        tvar: temporal variance values
2986        xmean: x-axis mean values
2987        xvar: x-axis variance values
2988        ymean: y-axis mean values
2989        zsum: vertical aggregated values
2990      scr= should it be done from the scratch?
2991    """
2992    fname='compute_diff_statistics'
2993
2994    # Full header of file
2995    Lfilen = len(filen)
2996    Hfile = filen[0:Lfilen-3]
2997
2998    # CF dimension-variables name
2999    OpersS = '+'.join(Opers)
3000    if OpersS.find('pinterp') != -1 or filen.find('pinterp') != -1:
3001        varnCFs = ['lon', 'lat', 'pres', 'time']
3002    else:
3003        varnCFs = ['lon', 'lat', 'time']
3004
3005    # Variable-dimension CF dictionary
3006    CFdimvardict = {'lon': 'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'}
3007
3008    # Python scripts HOME
3009    pyH = Ecnf['pyHOME']
3010
3011    #  opsur = operation surnames
3012    opsur = gen.get_specdictionary_HMT(Ecnf, H='opsur_',M='',T='')
3013    Opsurs = {}
3014    for opk in opsur:
3015        opn = opk.split('_')[1]
3016        vals = opsur[opk]
3017        Opsurs[opn] = vals.split(':')
3018
3019    # Getting in working dir
3020    os.chdir(ofold)
3021
3022    # File to keep track of all the statistics of var differences
3023    otrackf = open(ofold + '/all_vardiffstatistics.inf', 'a')
3024
3025    if db: 
3026        print "    computing statistics of diff variable: '", varn ,"' operation '"+ \
3027      OpersS + "' using file '" + filen + " ...'"
3028
3029    for op in Opers:
3030        # File name from previous operations, and name of the variable within the
3031        #   file (some operations change the name of the variable)
3032        if op == Opers[0]:
3033            Fopers = op
3034            prevfile = filen
3035            vninF = varn
3036        else:
3037            Fopers = Fopers + '_' + op
3038            prevfile = fileon
3039        fileon = Hfile + '_' + Fopers + '.nc'
3040
3041        SvarnCFs = ',' + ','.join(varnCFs) 
3042        CFvarnp = vninF + SvarnCFs
3043
3044        if scr:
3045            sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3046
3047        # Just produce file in case output file does not exist
3048        if not os.path.isfile(fileon):
3049            if db: 
3050                print '  ' + fname + ": creation of file '" + fileon + "' ..."
3051            dofile = True
3052        else:
3053            dofile = False
3054       
3055        # Variables to be kept in the final file
3056        varkeep = []
3057
3058        if op == 'acc':
3059            # temporal accumulated values
3060            print "  " + fname + ": kind '" + op + "' not ready !!"
3061            wrongstats.append(varn + '_' + opers)
3062            break
3063        elif op == 'direct':
3064            # no statistics
3065            if dofile:
3066                sout = sub.call('mv ' + prevfile + ' ' + fileon, shell=True)
3067        elif op == 'last':
3068            # last temporal value
3069            vals='time,-9,0,0'
3070            if dofile:
3071                try:
3072                    with gen.Capturing() as output:
3073                        ncvar.DataSetSection(vals,prevfile)
3074                except:
3075                    print errmsg
3076                    print 'DataSetSection('+vals+',', prevfile, ')'
3077                    for s1out in output: print s1out
3078                    quit(-1)
3079
3080                if db:
3081                    for s1out in output: print s1out
3082
3083                hprevfile = prevfile[0:len(prevfile)-3]
3084
3085                sout = sub.call('mv ' + hprevfile + '_time_B-9-E0-I0.nc ' + fileon, \
3086                  shell=True)
3087
3088                # Keeping the operations
3089                pyins=pyH + "/nc_var.py -o DataSetSection -S '" + vals + "' -f " +  \
3090                  prevfile
3091                otrackf.write("\n")
3092                otrackf.write("# " + varn + " " + Fopers + "\n")
3093                otrackf.write(pyins + "\n")
3094
3095        elif op =='Lmean':
3096            # latitudinal mean values
3097            print "  " + fname + ": kind '" + op + "' not ready !!"
3098            wrongstats.append(varn + '_' + opers)
3099            break
3100        elif op =='Lsec':
3101            # latitudinal section (latitudinal value must be given, [var]@[lat])
3102            print "  " + fname + ": kind '" + op + "' not ready !!"
3103            wrongstats.append(varn + '_' + opers)
3104            break
3105        elif op =='lmean':
3106            # longitudinal mean values
3107            print "  " + fname + ": kind '" + op + "' not ready !!"
3108            wrongstats.append(varn + '_' + opers)
3109            break
3110        elif op =='lsec':
3111            # longitudinal section (longitudinal value must be given, [var]@[lon])
3112            print "  " + fname + ": kind '" + op + "' not ready !!"
3113            wrongstats.append(varn + '_' + opers)
3114            break
3115        elif op == 'tmean':
3116            # temporal mean values
3117            vals='time|-1,time,mean,' + ':'.join(varnCFs)
3118            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3119            if dofile:
3120                try:
3121                    with gen.Capturing() as output:
3122                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3123                except:
3124                    print errmsg
3125                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3126                    for s1out in output: print s1out
3127                    quit(-1)
3128
3129                if db:
3130                    for s1out in output: print s1out
3131
3132                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3133
3134                # Keeping the operations
3135                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3136                  "' -f " + prevfile + " -v " + CFvarnp
3137                otrackf.write("\n")
3138                otrackf.write("# " + varn + " " + Fopers + "\n")
3139                otrackf.write(pyins + "\n")
3140
3141            # removing dimension variable-dimension 'time'
3142            varnCFs.remove('time')
3143
3144            varkeep.append('timestats')
3145
3146        elif op == 'tstd':
3147            # temporal standard deviation values
3148            vals='time|-1,time,std,' + ':'.join(varnCFs) + ':' + vdnz
3149            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
3150            if dofile:
3151                try:
3152                    with gen.Capturing() as output:
3153                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3154                except:
3155                    print errmsg
3156                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3157                    for s1out in output: print s1out
3158                    quit(-1)
3159
3160                if db:
3161                    for s1out in output: print s1out
3162
3163                sout = sub.call('mv file_oper_alongdims_std.nc '+fileon, shell=True)
3164
3165                # Keeping the operations
3166                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3167                  "' -f " + prevfile + " -v " + CFvarnp
3168                otrackf.write("\n")
3169                otrackf.write("# " + varn + " " + Fopers + "\n")
3170                otrackf.write(pyins + "\n")
3171
3172            # removing dimension variable-dimension 'time'
3173            varnCFs.remove('time')
3174
3175            varkeep.append('timestats')
3176
3177        elif op == 'tturb':
3178            # temporal turbulent values
3179            vals='time|-1,time,turb,' + ':'.join(varnCFs)
3180            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3181            if dofile:
3182                try:
3183                    with gen.Capturing() as output:
3184                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3185                except:
3186                    print errmsg
3187                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3188                    for s1out in output: print s1out
3189                    quit(-1)
3190
3191                if db:
3192                    for s1out in output: print s1out
3193
3194                sout = sub.call('mv file_oper_alongdims_turb.nc '+fileon, shell=True)
3195
3196                # Keeping the operations
3197                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3198                  "' -f " + prevfile + " -v " + CFvarnp
3199                otrackf.write("\n")
3200                otrackf.write("# " + varn + " " + Fopers + "\n")
3201                otrackf.write(pyins + "\n")
3202
3203            varkeep.append('timestats')
3204
3205        elif op == 'tvar':
3206            # temporal variance values (Taylor's turbulence)
3207            vals='time|-1,time,var,' + ':'.join(varnCFs) + ':' + vdnz
3208            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz
3209            if dofile:
3210                try:
3211                    with gen.Capturing() as output:
3212                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3213                except:
3214                    print errmsg
3215                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3216                    for s1out in output: print s1out
3217                    quit(-1)
3218
3219                if db:
3220                    for s1out in output: print s1out
3221
3222                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
3223
3224                # Keeping the operations
3225                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3226                  "' -f " + prevfile + " -v " + CFvarnp
3227                otrackf.write("\n")
3228                otrackf.write("# " + varn + " " + Fopers + "\n")
3229                otrackf.write(pyins + "\n")
3230
3231            # removing dimension variable-dimension 'time'
3232            varnCFs.remove('time')
3233
3234            varkeep.append('timestats')
3235
3236        elif op == 'xmean':
3237            # x-axis mean values
3238            vals='lon|-1,lon,mean,' + ':'.join(varnCFs)
3239            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3240            if dofile:
3241                try:
3242                    with gen.Capturing() as output:
3243                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3244                except:
3245                    print errmsg
3246                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3247                    for s1out in output: print s1out
3248                    quit(-1)
3249
3250                if db:
3251                    for s1out in output: print s1out
3252
3253                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3254
3255                # Keeping the operations
3256                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3257                  "' -f " + prevfile + " -v " + CFvarnp
3258                otrackf.write("\n")
3259                otrackf.write("# " + varn + " " + Fopers + "\n")
3260                otrackf.write(pyins + "\n")
3261
3262            # removing dimension variable-dimension 'lon'
3263            varnCFs.remove('lon')
3264
3265            varkeep.append('lonstats')
3266
3267        elif op == 'xvar':
3268            # x-axis variance values
3269            vals='lon|-1,lon,var,' + ':'.join(varnCFs)
3270            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3271            if dofile:
3272                try:
3273                    with gen.Capturing() as output:
3274                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3275                except:
3276                    print errmsg
3277                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3278                    for s1out in output: print s1out
3279                    quit(-1)
3280
3281                if db:
3282                    for s1out in output: print s1out
3283
3284                sout = sub.call('mv file_oper_alongdims_var.nc '+fileon, shell=True)
3285
3286                # Keeping the operations
3287                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3288                  "' -f " + prevfile + " -v " + CFvarnp
3289                otrackf.write("\n")
3290                otrackf.write("# " + varn + " " + Fopers + "\n")
3291                otrackf.write(pyins + "\n")
3292
3293            # removing dimension variable-dimension 'lon'
3294            varnCFs.remove('lon')
3295
3296            varkeep.append('lonstats')
3297
3298        elif op == 'ymean':
3299            # y-axis mean values
3300            vals='lat|-1,lat,mean,' + ':'.join(varnCFs)
3301            dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@')
3302            if dofile:
3303                try:
3304                    with gen.Capturing() as output:
3305                        ncvar.file_oper_alongdims(vals,prevfile,CFvarnp)
3306                except:
3307                    print errmsg
3308                    print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')'
3309                    for s1out in output: print s1out
3310                    quit(-1)
3311
3312                if db:
3313                    for s1out in output: print s1out
3314
3315                sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True)
3316
3317                # Keeping the operations
3318                pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals +        \
3319                  "' -f " + prevfile + " -v " + CFvarnp
3320                otrackf.write("\n")
3321                otrackf.write("# " + varn + " " + Fopers + "\n")
3322                otrackf.write(pyins + "\n")
3323
3324            # removing dimension variable-dimension 'lat'
3325            varnCFs.remove('lat')
3326
3327            varkeep.append('latstats')
3328
3329        elif op == 'zsum':
3330            # vertical aggregated values
3331            print "  " + fname + ": kind '" + op + "' not ready !!"
3332            wrongstats.append(varn + '_' + opers)
3333            break
3334        else:
3335            print errmsg
3336            print '  ' + fname + ": operation '" + op + "' not ready !!"
3337            quit(-1)
3338
3339        # End of kind of operation
3340
3341        # Variable name in file (vninF) changed due to operation
3342        #   but only if previous operation does not the same 'statistic'
3343        chvn = gen.dictionary_key_list(Opsurs, op)
3344        if chvn is not None:
3345            oldvninF = vninF
3346            vninF = vninF + chvn
3347            CFvarnp = CFvarnp.replace(oldvninF,vninF)
3348               
3349        if dofile:
3350            if len(varkeep) > 0:
3351                varkeepS = ',' + ','.join(varkeep)
3352            else:
3353                varkeepS = ''
3354
3355            # Adding such CF dimension variables which might not be in the
3356            #   post-operation file
3357            try:
3358                with gen.Capturing() as output: 
3359                    varinfile = ncvar.ivars(fileon)
3360            except:
3361                print errmsg
3362                print 'ivars('+fileon+')'
3363                for s1out in output: print s1out
3364                # Removing file in order to make sure that it will be redone
3365                print '  ' + fname + " removing file '" + fileon + "' ..."
3366                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3367                quit(-1)
3368
3369            for CFvn in varnCFs:
3370                if not gen.searchInlist(varinfile, CFvn):
3371                    if db:
3372                        print '  ' + fname + ": recupering CF variable '" + CFvn + "'"
3373                    try:
3374                        with gen.Capturing() as output:
3375                            ncvar.fvaradd(prevfile+','+CFvn, fileon)
3376                    except:
3377                        print errmsg
3378                        print 'fvaradd(' + prevfile + ',' + CFvn +', ' + fileon + ')'
3379                        for s1out in output: print s1out
3380                        # Removing file in order to make sure that it will be redone
3381                        print '  ' + fname + " removing file '" + fileon + "' ..."
3382                        sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3383                        quit(-1)
3384
3385                    if db:
3386                        for s1out in output: print s1out
3387
3388            totalvarkeeps = CFvarnp + ',' + ','.join(varnCFs) + varkeepS
3389            try:
3390                with gen.Capturing() as output:
3391                    oclean = ncvar.cleaning_varsfile(totalvarkeeps,fileon)
3392            except:
3393                print errmsg
3394                print 'cleaning_varsfile('+totalvarkeeps+','+fileon+')'
3395                for s1out in output: print s1out
3396                # Removing file in order to make sure that it will be redone
3397                print '  ' + fname + " removing file '" + fileon + "' ..."
3398                sub.call('rm ' + fileon + ' >& /dev/null', shell=True)
3399                quit(-1)
3400
3401            if db:
3402                for s1out in output: print s1out
3403
3404    # End of operations
3405    otrackf.close()
3406
3407    return
3408
3409def compute_var_diffs(config, diffallvar, odir, diffscr, debug):
3410    """ Function to compute variable differences
3411      config= experiment configuration
3412      diffallvar= dictonary with the differences to compute
3413      odir= output folder
3414      diffscr= wether it should be done from the scratch
3415    """
3416    fname = 'compute_var_diffs'
3417
3418    # output folder
3419    ofold = config['ofold']
3420
3421    # Home of the python scripts
3422    pyH = config['pyHOME']
3423
3424    #  opsur = operation surnames
3425    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3426    Opsurs = {}
3427    for opk in opsur:
3428        opn = opk.split('_')[1]
3429        vals = opsur[opk]
3430        Opsurs[opn] = vals.split(':')
3431
3432    # Getting in working dir
3433    os.chdir(odir)
3434
3435    # CF dimensions
3436    CFdims = ['lon', 'lat', 'pres', 'time']
3437
3438    # Trakcing file
3439    trkobjf = open(odir + '/all_diffvar.inf', 'a')
3440
3441    for vnop in diffallvar.keys():
3442        vn = vnop.split('_')[0]
3443        op = vnop.split('_')[1]
3444        opSfinal = op.replace('+','_')
3445
3446        lop = op.split('+')
3447        # `p' header add related to 'pinterp'
3448        if op.find('pinterp') != -1:
3449            print warnmsg
3450            print '  ' + fname + ': there is the vertical interpolation as operation'
3451            print '    variable differences will be computed from that files where '
3452            print '    vertical interpolation was just done'
3453            ipop = lop.index('pinterp')
3454            opS = '_' + '_'.join(lop[0:ipop+1])
3455            doops = lop[ipop+1:]
3456            SgP = 'p'
3457            vninF = varnoper(vn, '+'.join(lop[0:ipop+1]), Opsurs)
3458            print '    operations to perform:', doops
3459        else: 
3460            SgP = ''
3461            opS = ''
3462            doops = lop
3463            vninF = vn
3464
3465        Sdoops = '+'.join(doops)
3466
3467        # Variable name within the file
3468
3469        vals = diffallvar[vnop]
3470        mod1 = vals[0]
3471        mod2 = vals[1]
3472        exp1 = vals[2]
3473        exp2 = vals[3]
3474        headerf1 = vals[4]
3475        headerf2 = vals[5]
3476        modexpdiff = mod2 + '/' + exp2 + ' - ' + mod1 + '/' + exp1
3477        modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
3478
3479        ifile1 = ofold + '/' + mod1 + '/' + exp1 + '/' + vn + '_' + headerf1 + SgP + \
3480          opS + '.nc'
3481        ifile2 = ofold + '/' + mod2 + '/' + exp2 + '/' + vn + '_' + headerf2 + SgP + \
3482          opS + '.nc'
3483        difffilen = odir + '/' + vn + '_diffvar_' + modexpdiffS + opS + '.nc'
3484        difffinalfilen = odir + '/' + vn + '_diffvar_' + modexpdiffS + opS + '_' +   \
3485          Sdoops + '.nc'
3486
3487        if diffscr:
3488            sout = sub.call('rm ' + difffilen + ' >& /dev/null', shell=True)
3489            sout = sub.call('rm ' + difffinalfilen + ' >& /dev/null', shell=True)
3490
3491        if not os.path.isfile(difffinalfilen):
3492            if debug:
3493                print "    Computing variable difference of '" + vn + "' for: '" +   \
3494                  modexpdiff + "' and performing operation '" + Sdoops + "'"
3495                print "    var in file: '" + vninF + "'"
3496
3497            # Range of the dimensions
3498            for CFd in CFdims:
3499                if CFd == CFdims[0]:
3500                    CFdimS = CFd + '|' + CFd + '|-1'
3501                else:
3502                    CFdimS = CFdimS + ';' + CFd + '|' + CFd + '|-1'
3503
3504            # Attributes of the variable
3505            varattrs = ncvar.ivattrs(ifile2,vninF)
3506            varvals = vninF + ',' + varattrs['long_name'][0] + ',' +                 \
3507              varattrs['units'][0]
3508
3509            values = CFdimS + '@' + 'add|' + ifile2 + '|' + vninF + '%' + CFdimS +   \
3510              '@' + 'sub|' + ifile1 + '|' + vninF
3511            pyins = 'python ' + pyH + "/nc_var.py -o compute_opersvarsfiles -S '" +  \
3512              values + "' -v " + varvals
3513
3514            try:
3515                with gen.Capturing() as output:
3516                    ncvar.compute_opersvarsfiles(values,varvals)
3517            except:
3518                print errmsg
3519                print 'ncvar.compute_opersvarsfiles(' + values + ', ' + varvals + ')'
3520                for s1out in output: print s1out
3521                quit(-1)
3522
3523            sout = sub.call('mv opersvarsfiles_'+vninF+'.nc ' + difffilen, shell=True)
3524
3525            # keeping all differences
3526            trkobjf.write('\n')
3527            trkobjf.write("#" + vn + ' ' + op + '\n')
3528            trkobjf.write( pyins + '\n')
3529
3530            # Computing statisitcs
3531            compute_diff_statistics(config, difffilen, odir, vninF, doops, diffscr,  \
3532              debug)
3533
3534    trkobjf.close()
3535
3536    return
3537
3538
3539def draw_diff_plots(config, plots, odir, allvarcomp, kdiff, figscr, debug):
3540    """ Function to draw all plots
3541      config= Configuration of the experiment
3542      plots= dictionary with the plots
3543      odir= output experiment folder
3544      allvarcomp= dictionary with all the variables to compute and their information
3545      kdiff= kind of differences:
3546        'diffop': plots from operation differences
3547        'diffvar': plots from variable differences
3548      figscr= whether figures should be done from the scratch or not
3549
3550    * Plot as
3551      {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ...
3552        [kplot] ___
3553          diffmap2Dsfc: 2D map of surface differences values of 1 variable
3554          diffmap2Dz: 2D map of 3D differences values of 1 variable
3555          map2Dsfc: 2D map of surface values of 1 variable
3556          map3D: 2D map of 3D values of 1 variable
3557          shadconthovmsfc: Hovmoeller diagrams of 2 variable at the surface in shadow and the other in contourn
3558          shadcont2Dsfc: 2D map of shadow (1st variable) and countour (2nd variable) [stvar1]#[stvar2]
3559          shadcont2Dzsec: 2D map of vertical section of 2 variables one in shadow and the other in contourn
3560        [varn]
3561          variable
3562        [op]
3563          '+' separated list of operations
3564        in figures with more than 1 variable, use '#' to separate the [varn]|[op]
3565    """ 
3566    fname = 'draw_diff_plots'
3567
3568    os.chdir(odir)
3569
3570    # Dictionary with the operations with surnames for the operated variable
3571    opersurnames = {}
3572    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3573    for opsr in opsur.keys():
3574        opn = opsr.split('_')[1]
3575        vls = opsur[opsr].split(':')
3576        opersurnames[opn] = vls
3577
3578    # time values
3579    # Units time for the plots
3580    rd = config['CFreftime']
3581    tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' +  \
3582      rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14]
3583    # time ticks kind
3584    tkind = config['timekind']
3585    # time ticks format
3586    tfmt = config['timefmt']
3587    # time axis label
3588    tlab = config['timelabel']
3589    timevals = [tunits, tkind, tfmt, tlab]
3590
3591    if kdiff == 'diffop':
3592         specplotkeyn = 'specificdiffopplot'
3593    elif kdiff == 'diffvar':
3594         specplotkeyn = 'specificdiffvarplot'
3595    else:
3596         print errmsg
3597         print '  ' + fname + ": differences kind '" + kdiff + "' not ready !!"
3598         quit(-1)
3599
3600    # Dictionary of plot specificities
3601    plotspecifics = {}
3602    if config.has_key(specplotkeyn):
3603        #   [minval]: minimum value
3604        #   [maxval]: minimum value
3605        #   [colorbar]: name of the colorbar (from matplotlib) to use
3606        #   [cntformat]: format of the contour labels
3607        #   [colorcnt]: color for the countor lines
3608        plotspecs = config[specplotkeyn].split(':')
3609        for pltspc in plotspecs:
3610            pltvls = pltspc.split('|')
3611            vn = pltvls[0]
3612            op = pltvls[1]
3613            fn = pltvls[2]
3614            plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:]
3615        if debug:
3616            print 'Specific values for plots _______'
3617            gen.printing_dictionary(plotspecifics)
3618
3619    # Kind of figures
3620    kindfigure = config['kindfig']
3621
3622    # Graphical labels and configuration
3623    modgc, expgc = graphical_characteristics(config, debug)
3624
3625    # Map value
3626    mapvalue = config['mapval']
3627
3628    # pythone scripts HOME
3629    pyHOME = config['pyHOME']
3630
3631    # Title-text of operations
3632    opexplained = {}
3633    optits = config['titleoperations'].split(':')
3634    for optit in optits:
3635        opn = optit.split('|')[0]
3636        opt = optit.split('|')[1]
3637        opexplained[opn] = opt
3638    if debug:
3639        print 'Titles for operations  _______'
3640        gen.printing_dictionary(opexplained)
3641
3642    for kplot in plots.keys():
3643        varsplt = plots[kplot]
3644        for varplt in varsplt:
3645            if debug:
3646                print "  printing '" + kplot + "' var ':" + varplt + "'..."
3647            varops = varplt.split('#')
3648
3649            # CF variables in plot
3650            CFvarsplot = []
3651            # Files in plot
3652            filesplot = []
3653            # Variables in plot within the files
3654            varsplot = []
3655            # Dims in figure
3656            dimsplot = []
3657            # pictoric values in figure
3658            pictplot = []
3659            # Name of the figure
3660            figname = ''
3661            # Title of the figure
3662            titfigure = ''
3663
3664            ivp = 0
3665            for varop in varops:
3666                vn = varop.split('|')[0]
3667                op = varop.split('|')[1]
3668
3669                # CF variables in plot
3670                CFvarsplot.append(vn)
3671 
3672                vnopS = vn + '_' + op
3673                if not allvarcomp.has_key(vnopS):
3674                    print errmsg
3675                    print '  ' + fname + ": no file for variable-operation '" +     \
3676                      vnopS + "' !!"
3677                vopvals = allvarcomp[vnopS]
3678                mod1 = vopvals[0]
3679                mod2 = vopvals[1]
3680                mod3 = vopvals[2]
3681                mod4 = vopvals[3]
3682                headf = vopvals[4]
3683
3684                modv = modgc[mod1]
3685                expv = expgc[mod1+'/'+exp1]
3686                modlab1 = modv.label
3687                explab1 = expv.label
3688                modv = modgc[mod2]
3689                expv = expgc[mod2+'/'+exp2]
3690                modlab2 = modv.label
3691                explab2 = expv.label
3692
3693                modexpdiff = explab2 + '-' + explab1
3694                modexpdiffS = mod2 + '-' + exp2 + '_' + mod1 + '-' + exp1
3695
3696                difffilen = odir + '/' + vn + '_' + kdiff + '_' + modexpdiffS +      \
3697                  '_' + op.replace('+','_') + '.nc'
3698
3699                filesplot.append(difffilen)
3700                # Do we have processed the given difference?
3701                if not os.path.isfile(difffilen):
3702                    print warnmsg
3703                    print "  " + fname + ": there is no file for '" + kdiff +        \
3704                      "' difference '" + varop + "' skiping it !!"
3705                    break
3706
3707                # Name of the variable inside the file
3708                vnsur = varnoper(vn, op, opersurnames)
3709                varsplot.append(vnsur)
3710
3711                # Dimensions in file
3712                try:
3713                    with gen.Capturing() as output:
3714                        dims = ncvar.idims(difffilen)
3715                except:
3716                    print errmsg
3717                    print 'ncvar.idims('+difffilen+')'
3718                    for s1out in output: print s1out
3719                    quit(-1)
3720
3721                dimsplot.append(dims)
3722
3723                # pictoric values for the figure
3724                Sfivaop = kplot + '_' + vn + '_' + op
3725                if plotspecifics.has_key(Sfivaop):
3726                    pictvals = plotspecifics[Sfivaop]
3727                else:
3728                    Vvals = gen.variables_values(vn)
3729                    pictvals = [Vvals[2], Vvals[3], Vvals[6], '%g', 'fixsigc','black']
3730
3731                pictplot.append(pictvals)
3732
3733                # Header of the name of the figure
3734                if ivp == 0:
3735                    figname = kplot + '_' + vn + '_' + kdiff + '_' + modexpdiffS +   \
3736                      '_' + op.replace('+','-')
3737                else:
3738                    figname = figname + '-' + vn + '_' + op.replace('+','-')
3739
3740                # Title of the figure:
3741                if mod1 == mod2:
3742                    modexptit = mod1 + '!' + exp2 + '-' + exp1
3743                else:
3744                    modexptit = modexpdiff
3745
3746                ivp = ivp + 1
3747            # End of variable-operation
3748            figname = figname + '.' + kindfigure
3749
3750            # Title of figure
3751            titfigure = create_figure_title(modexptit, kdiff, varops, opexplained)
3752
3753            if len(titfigure) > 80:
3754                print warnmsg
3755                print ' ' + fname + ": figure title '" + titfigure.replace('!', ' ')+\
3756                  "' larger than 80 characters (actually", len(titfigure), ') !!'
3757                print "  simplifying it to '" + modexptit.replace('!',' ') + '!' +   \
3758                  kdiff + '!' + vnS + "'"
3759                titfigure = modexptit.replace(' ','!') + '!' + kdiff + '!' + vnS
3760
3761            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
3762              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
3763              pyHOME, figscr, debug)
3764
3765        # End of variables-operations
3766
3767    # End of kind of plots
3768
3769    return
3770
3771def reproject_modexp(mod,exp,config,REPRJops,fscr,fadd,debug):
3772    """ Function to re-project and re-compute files from a given model/experiment
3773      mod= model
3774      exp= experiment
3775      config= configuration
3776      REPRJops= dictionary with the reprojection operators to use for reproject and for which variable
3777        #    remapbil   CDO: Bilinear interpolation
3778        #    remapbic   CDO: Bicubic interpolation
3779        #    remapdis   CDO: Distance-weighted average remapping
3780        #    remapnn    CDO: Nearest neighbor remapping
3781        # Operators only available if projections have the corners of the grid points.
3782        #    remapcon   CDO: First order conservative remapping
3783        #    remapcon2  CDO: Second order conservative remapping
3784        #    remaplaf   CDO: Largest area fraction remapping
3785        ####### ####### ####### or additionally ####### ####### #######
3786        # python remapping
3787        #    dis        python: Distance-weighted average remapping
3788        #    pnn        python: Nearest neighbor remapping
3789
3790      fscr= re-projected files to compute from the scratch
3791      fadd= re-projected files to be added
3792    """
3793    fname = 'reproject_modexp'
3794
3795    odir = config['ofold'] + '/' + mod + '/' + exp
3796
3797    # NON re-projectable operations
3798    opNOreproj = config['NOreprojops'].split(':')
3799
3800    # Need to pass to analyze all the data?
3801    inffiles = ['reproj_varcompute.inf', 'all_reproj_computevars.inf']
3802    if fscr:
3803        for inff in inffiles:
3804            if debug: print "    removing information file '" + inff + "' ..."
3805            ins = 'rm ' + odir + '/' + inff + ' >& /dev/null'
3806            sout = sub.call(ins, shell=True)
3807
3808        objf = open(odir+'/all_reproj_computevars.inf','w')
3809        objf.write("## Computation of reprojected variables \n")
3810        objf.close()
3811
3812    varcompf = odir + '/reproj_varcompute.inf'
3813    if fadd:
3814        sub.call('rm ' + varcompf +' >& /dev/null', shell=True)
3815
3816    # CF dims-vardims pairs:
3817    CFvardims = ['long@lon', 'lat@lat', 'pres@pres', 'time@time']
3818
3819    # Dictionary with the operations with surnames for the operated variable
3820    opersurnames = {}
3821    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
3822    for opsr in opsur.keys():
3823        opn = opsr.split('_')[1]
3824        vls = opsur[opsr].split(':')
3825        opersurnames[opn] = vls
3826
3827    if not os.path.isfile(varcompf):
3828        # Getting original computing information
3829        origvarcompf = odir + '/varcompute.inf'
3830        origfiles,origtestfiles,origallcompvar, Nvar = read_varcomp_file(origvarcompf)
3831             
3832        Svarcompute = ''
3833        allcompvar = dict(origallcompvar)
3834        ivop = 0
3835        for vnop in origallcompvar.keys():
3836            vn = vnop.split('_')[0]
3837            op = vnop.split('_')[1]
3838            values = allcompvar[vnop]
3839            values[0] = 'reproj-' + values[0]
3840            allcompvar[vnop] = values
3841
3842            # Building strings from list values
3843            model = values[1]
3844            diag = values[2]
3845            if model is not None:
3846                if type(model) == type(list([1, 2])):
3847                    Smodel = ':'.join(model)
3848                elif type(model) == type('A'):
3849                    Smodel = model
3850            else:
3851                Smodel = 'None'
3852            if diag is not None:
3853                if type(diag) == type(list([1, 2])):
3854                    Sdiag = ':'.join(diag)
3855                elif type(diag) == type('A'):
3856                    Sdiag = diag
3857            else:
3858                Sdiag = 'None'
3859
3860            Svc = vn + '|' + op + '|' + values[0] + '|' + Smodel + '|' + Sdiag +     \
3861              '|' + values[3]
3862            if ivop == 0:
3863                Svarcompute = Svc
3864            else:
3865                Svarcompute = Svarcompute + ',' + Svc
3866            ivop = ivop + 1
3867
3868        origstrfiles = gen.dictKeysVals_stringList(origfiles)
3869        origstrtestfiles = gen.dictKeysVals_stringList(origtestfiles)
3870
3871        # Outwritting the varcompute to avoid next time (if it is not filescratch!)
3872        objf = open(odir + '/reproj_varcompute.inf', 'w')
3873        objf.write('files: ' + origstrfiles + '\n')
3874        objf.write('testfiles: ' + origstrtestfiles + '\n')
3875        objf.write('varcompute: ' + Svarcompute + '\n')
3876        objf.write('itotv: ' + str(ivop) + '\n')
3877        objf.close() 
3878    else: 
3879        print warnmsg
3880        print '  ' + main + ": getting variables to compute already from file !!"
3881        origfiles, origtestfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
3882
3883    # End of avoiding to repeat all the experiment search
3884
3885    print "    For experiment '" + exp + "' is required to re-project:", Nvar,       \
3886      "variables"
3887
3888    if dbg:
3889        print 'Variables to re-project _______'
3890        gen.printing_dictionary(allcompvar)
3891
3892    # Re-projection variables
3893    print "    Reprojecting variables ..."
3894    os.chdir(odir)
3895
3896    # Keeping track of reprojection
3897    trckobjf = open(odir + '/all_reproj_computevars.inf','a')
3898
3899    for vnop in allcompvar.keys():
3900        vn = vnop.split('_')[0]
3901        op = vnop.split('_')[1]
3902        values = allcompvar[vnop]
3903        VnOpS = varnoper(vn, op, opersurnames)
3904
3905        # Only are reprojected that files which do contain 'lon' and 'lat',
3906        #   otherways, it is only re-projected the original file
3907        inNO, iopNO, iop = gen.list_coincidences(opNOreproj,op.split('+'))
3908
3909        reprjop = gen.dictionary_key_list(REPRJops,vn).split('_')[1]
3910        if reprjop is None:
3911            print errmsg
3912            print '  ' + fname + ": no reprojecting operator found for " +           \
3913              " variable '" + vn + "' !!"
3914            print '    available values _______'
3915            gen.printing_dictionary(REPRJops)
3916            quit(-1)
3917       
3918        # Re-projecting also the 'original' (at least with 'pinterp') file for varsdif
3919        if op.find('pinterp') != -1:
3920            ifileorig = vn +'_'+ values[0].replace('reproj-','') + 'p_pinterp.nc'
3921            ofileorig = vn +'_'+ values[0] + 'p_pinterp.nc'
3922            ofilen = vn +'_'+ values[0] + 'p_pinterp.nc'
3923        else:
3924            ifileorig = vn +'_'+ values[0].replace('reproj-','') + '.nc'
3925            ofileorig = vn +'_'+ values[0] + '.nc'
3926
3927        ifileop = vn+'_'+values[0].replace('reproj-','')+'_'+op.replace('+','_')+'.nc'
3928        ofileop = vn + '_' + values[0] + '_' + op.replace('+','_') + '.nc'
3929
3930        if fscr:
3931            sout = sub.call('rm ' + odir+'/'+ofileorig + ' >& /dev/null', shell=True)
3932            sout = sub.call('rm ' + odir+'/'+ofileop + ' >& /dev/null', shell=True)
3933
3934        # List of files to re-project (first without operation)
3935        ifiles = [odir+'/'+ifileorig, odir+'/'+ifileop]
3936        # List of output files after re-projection (first without operation)
3937        ofiles = [odir+'/'+ofileorig, odir+'/'+ofileop]
3938
3939        for iif in range(2):
3940            ifile = ifiles[iif]
3941            ofile = ofiles[iif]
3942
3943            # Only projecting original file-variable if there are not 'lon' and 'lat'
3944            doreproj = True
3945            if iif == 1 and len(inNO) > 0: 
3946                doreproj = False
3947                print warnmsg
3948                print '  ' + fname + ": operation '" + op + "' can not be " +        \
3949                  "reprojected due to the lack of 'lon' or 'lat' skipping it !!"
3950                print "    NOTE: difference can only be computed as:"
3951                print "      - 1st: compute variable's differences between models"
3952                print "      - 2nd: Perform the operations"
3953
3954                # Building up resources to compute re-projected statistics
3955                print '  ' + fname + ': computing from the original reprojected file'
3956                ModI = ModelInf(mod, mod, 'lon', 'lat', 'pres', 'time', 'depth',     \
3957                  'lon', 'lat', 'pres', 'time', 'depth', None)
3958                if type(values) == type([1]): 
3959                  fileh = values[0]
3960                else:
3961                  fileh = values
3962
3963                if type(values) == type([1]): 
3964                  fileh = values[0]
3965                else:
3966                  fileh = values
3967
3968                VarI = VariableInf(vn, values[0], values[2], values[3])
3969                usefs = {values[0]: ofileorig}
3970
3971                compute_statistics(ModI, config, config['ifold'], usefs,             \
3972                  config['ofold']+'/'+mod+'/'+exp,VarI,pinterp_var(op), op, fscr, dbg)
3973
3974            if not os.path.isfile(ofile) and doreproj:
3975                if debug and ofile == ofiles[0]:
3976                    print "      reprojecting '" + vn + "' '" + op + "' into '" +    \
3977                      ofile + "'"
3978
3979                # Reprojecting...
3980                if reprjop[0:6] == 'remap':
3981                    ins = config['cdoHOME'] + '/cdo ' + reprjop + ',' +              \
3982                      config['RefProjfile'] + ' ' + ifile + ' ' + ofile
3983                    try:
3984                        with gen.Capturing() as output:
3985                            sout = sub.call(ins, shell=True)
3986                    except:
3987                        print errmsg
3988                        print '  ' + fname + ': $ ' + ins
3989                        for s1out in output: print s1out
3990                        print sout
3991                        quit(-1)
3992                    if sout != 0:
3993                        print errmsg
3994                        print '  ' + fname + ': $ ' + ins
3995                        sub.call(ins, shell=True)
3996                        quit(-1)
3997                else:
3998                    # Name of the variable to reproject
3999                    if iif == 0:
4000                        reprjvn = vn
4001                    else: 
4002                        reprjvn = VnOpS
4003             
4004                    reprojvalues = 'lon,lat,' + config['RefProjfile'] + ',lon,lat,'+ \
4005                      reprjop + ',' + ':'.join(CFvardims)
4006                    ins= 'ncvar.reproject('+reprojvalues + ', ' + ifile + ', '+      \
4007                      reprjvn + ')'
4008                    try:
4009                        with gen.Capturing() as output:
4010                            sout=ncvar.reproject(reprojvalues, ifile, reprjvn)
4011                    except:
4012                        print errmsg
4013                        print '  ' + fname + ': ' + ins
4014                        for s1out in output: print s1out
4015                        quit(-1)
4016
4017                    sub.call('mv reproject.nc ' + ofile, shell=True)
4018
4019                if debug:
4020                    for s1out in output: print s1out
4021                    print sout
4022
4023                # Keeping track of re-projection
4024                trckobjf.write('\n')
4025                if iif == 0:
4026                    trckobjf.write('# ' + vn + '\n')
4027                else:
4028                    trckobjf.write('# ' + vn + ' ' + op + '\n')
4029
4030                trckobjf.write(ins + '\n')
4031
4032                if reprjop[0:6] == 'remap':
4033                    # CFing the reprojected file
4034                    try:
4035                        with gen.Capturing() as output:
4036                            ncvar.CDO_toCF(ofile)
4037                    except:
4038                        print errmsg
4039                        print 'ncvar.CDO_toCF(' + ofile + ')'
4040                        for s1out in output: print s1out
4041                        # Removing file in order to make sure that it will be redone
4042                        print '  ' + fname + " removing file '" + ofile + "' ..."
4043                        sub.call('rm ' + ofile + ' >& /dev/null', shell=True)
4044                        quit(-1)
4045
4046                    if debug:
4047                        for s1out in output: print s1out
4048               
4049    trckobjf.close()
4050
4051    return
4052
4053def allmodexps_listconstruct(config, modexps, odir, debug):
4054    """ Function to create the list of all model-experiments to draw
4055      config= Configuration of the experiment
4056      modexps= list with the models-experiments pairs to use
4057      odir= output differences folder
4058    """
4059    fname='allmodexps_listconstruct'
4060 
4061    allplts = gen.get_specdictionary_HMT(config, H='PLOTALLMODEXP_')
4062
4063    if debug:
4064        if allplts is not None:
4065            print "  'all mod-exps' plots ________"
4066            gen.printing_dictionary(allplts)
4067
4068    # Getting Model-Reference Projection
4069    ModComProj = cnf['RefProj'].split(':')
4070
4071    allmodexp = {}
4072    Nallmodexp = 0
4073    for modexp in modexps:
4074        if debug:
4075            print "    " +  fname + ": '" + modexp + "' ..."
4076        mod = modexp.split('/')[0]
4077        exp = modexp.split('/')[1]
4078
4079        # Getting variable characteristics from each model/experiment
4080        idir = config['ofold'] + '/' + modexp
4081
4082        reproj = not gen.searchInlist(ModComProj,mod)
4083
4084        fvarcomp = 'varcompute.inf'
4085        if reproj: fvc = 'reproj_' + fvarcomp
4086        else: fvc = fvarcomp
4087
4088        files, testfiles, allcompvar, Nvar= read_varcomp_file(idir + '/' + fvc)
4089
4090        if allplts is not None:
4091            # Getting all model-experiments plots characteristics
4092
4093            for allplt in allplts.keys():
4094                pltn = allplt.split('_')[1]
4095                if debug and modexp == modexps[0]:
4096                    print '    ' + fname + ":      plot '" + pltn + "' "
4097                varops = allplts[allplt].split(':')
4098                for varop in varops:
4099                    vn = varop.split('|')[0]
4100                    op = varop.split('|')[1]
4101                    vnop = vn + '_' + op
4102                    if debug and modexp == modexps[0]:
4103                        print '    ' + fname + ":        varop '" + vnop + "' "
4104                    if not allcompvar.has_key(vnop):
4105                        print warnmsg
4106                        print '  ' + fname + ": variable+operation '" + vnop +       \
4107                          "' in '" + modexp + "' was not computed !!"
4108                        print '    skipping it!'
4109                        print '    computed values:', allcompvar.keys()
4110                        break
4111
4112                    vals = allcompvar[vnop]
4113
4114                    headerf = vals[0]
4115                    if modexp == modexps[0]:
4116                        allmodexp[vnop] = [modexp,headerf]
4117                        Nallmodexp = Nallmodexp + 1
4118                    else:
4119                        prevals = allmodexp[vnop]
4120                        prevals = prevals + [modexp,headerf]
4121                        allmodexp[vnop] = prevals
4122
4123        else:
4124            Nallmodexp = 0
4125
4126    if allplts is not None:
4127        Sallmodexps = gen.dictKeysVals_stringList(allmodexp)
4128    else:
4129        Sallmodexps = 'none'
4130
4131    # Outwritting the all model-experiment plots file to avoid next time
4132    objf = open(odir + '/allmodexp.inf', 'w')
4133    objf.write('allmodexps: ' + Sallmodexps + '\n')
4134    objf.write('Nallmodexp: ' + str(Nallmodexp) + '\n')
4135    objf.close()
4136
4137    return allmodexp, Nallmodexp
4138
4139def allmodexps_read(filen):
4140    """ Function to read the file with the information about the all model-experiment plots
4141      filen= file with the information
4142    """
4143    fname = 'allmodexps_read'
4144
4145    if not os.path.isfile(filen):
4146        print errormsg
4147        print '  ' + fname + ": all model-experiment file '" + filen +               \
4148          "' does not exist !!"
4149        quit(-1)
4150
4151    objf = open(filen, 'r')
4152    for line in objf:
4153        if line[0:1] != '#' and len(line) > 1:
4154            values = line.replace('\n','').split(' ')
4155            if values[0] == 'allmodexps:':
4156                allplts = gen.stringList_dictKeysVals(values[1],cV=':')
4157            elif values[0] == 'Nallmodexp:':
4158                Nallmodexp = int(values[1])
4159
4160    objf.close()
4161
4162    return allplts, Nallmodexp
4163
4164def draw_allmodexp_plots(config, allvarcomp, plots, modexp, odir, figscr, debug):
4165    """ Function to draw all model-experiment plots
4166      config= Configuration of the experiment
4167      allvarcomp = dictionary with the file headers for each variable
4168      plots= dictionary with the plots
4169      modexps= list with the names of the model-experiment pairs
4170      odir= output experiment folder
4171      figscr= whether figures should be done from the scratch or not
4172
4173    * Plot as
4174      {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ...
4175        [kplot] ___
4176          Nlines: Multiple lines with one line by model-experiment pairs
4177        [varn]
4178          variable
4179        [op]
4180          '+' separated list of operations
4181        in figures with more than 1 variable, use '#' to separate the [varn]|[op]
4182    """ 
4183    fname = 'draw_allmodexp_plots'
4184
4185    os.chdir(odir)
4186
4187    # Folder with the processed files
4188    infold = config['ofold']
4189
4190    # Dictionary with the operations with surnames for the operated variable
4191    opersurnames = {}
4192    opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='')
4193    for opsr in opsur.keys():
4194        opn = opsr.split('_')[1]
4195        vls = opsur[opsr].split(':')
4196        opersurnames[opn] = vls
4197
4198    # time values
4199    # Units time for the plots
4200    rd = config['CFreftime']
4201    tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' +  \
4202      rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14]
4203    # time ticks kind
4204    tkind = config['timekind']
4205    # time ticks format
4206    tfmt = config['timefmt']
4207    # time axis label
4208    tlab = config['timelabel']
4209    timevals = [tunits, tkind, tfmt, tlab]
4210
4211    # Dictionary of plot specificities
4212    specplotkeyn = 'specificvarplot'
4213    plotspecifics = {}
4214    if config.has_key(specplotkeyn):
4215        #   [minval]: minimum value
4216        #   [maxval]: minimum value
4217        #   [colorbar]: name of the colorbar (from matplotlib) to use
4218        #   [cntformat]: format of the contour labels
4219        #   [colorcnt]: color for the countor lines
4220        plotspecs = config[specplotkeyn].split(':')
4221        for pltspc in plotspecs:
4222            pltvls = pltspc.split('|')
4223            vn = pltvls[0]
4224            op = pltvls[1]
4225            fn = pltvls[2]
4226            plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:]
4227        if debug:
4228            print 'Specific values for plots _______'
4229            gen.printing_dictionary(plotspecifics)
4230
4231    # Graphical labels and configuration
4232    modgc, expgc = graphical_characteristics(config, debug)
4233
4234    # Kind of figures
4235    kindfigure = config['kindfig']
4236
4237    # Map value
4238    mapvalue = config['mapval']
4239
4240    # pythone scripts HOME
4241    pyHOME = config['pyHOME']
4242
4243    # Title-text of operations
4244    opexplained = {}
4245    optits = config['titleoperations'].split(':')
4246    for optit in optits:
4247        opn = optit.split('|')[0]
4248        opt = optit.split('|')[1]
4249        opexplained[opn] = opt
4250    if debug:
4251        print 'Titles for operations  _______'
4252        gen.printing_dictionary(opexplained)
4253
4254    for kplot in plots.keys():
4255        varsplt = plots[kplot]
4256        for varplt in varsplt:
4257            if debug:
4258                print "  printing '" + kplot + "' var ':" + varplt + "'..."
4259            varops = varplt.split('#')
4260
4261            # CF variables in plot
4262            CFvarsplot = []
4263            # Files in plot
4264            filesplot = []
4265            # Variables in plot within the files
4266            varsplot = []
4267            # Dims in figure
4268            dimsplot = []
4269            # pictoric values in figure
4270            pictplot = []
4271            # Name of the figure
4272            figname = ''
4273            # Title of the figure
4274            titfigure = ''
4275
4276            ivp = 0
4277            for varop in varops:
4278                vn = varop.split('|')[0]
4279                op = varop.split('|')[1]
4280
4281                # CF variables in plot
4282                CFvarsplot.append(vn)
4283 
4284                if op.find('pinterp') != -1: gP = 'p'
4285                else: gP = ''
4286
4287                vnopS = vn + '_' + op                   
4288                if not allvarcomp.has_key(vnopS):
4289                    print errmsg
4290                    print '  ' + fname + ": no file for variable-operation '" +      \
4291                      vnopS + "' !!"
4292                modexpvals = allvarcomp[vnopS]
4293#                modexpvalsS = allvarcomp[vnopS]
4294#                modexpvals = modexpvalsS[0].split('|')
4295                Nmodexps = len(modexpvals)
4296
4297                for imodexp in range(0,Nmodexps,2):
4298                    modexp = modexpvals[imodexp]
4299                    headf = modexpvals[imodexp+1]
4300                    if debug:
4301                        print '  ' + fname + "    model/experiment: '" +  modexp + "'"
4302
4303                    filen = infold + '/' + modexp + '/' + vn +'_'+ headf+gP + '_' +  \
4304                      op.replace('+','_') + '.nc'
4305
4306                    filesplot.append(filen)
4307                    # Do we have processed the given variable?
4308                    if not os.path.isfile(filen):
4309                        print warnmsg
4310                        print "  " + fname + ": there is no file for variable '" +   \
4311                          varop + "' skiping it !!"
4312                        break
4313
4314                    if imodexp == 0:
4315                        # Name of the variable inside the file
4316                        vnsur = varnoper(vn, op, opersurnames)
4317                        varsplot.append(vnsur)
4318
4319                    # Dimensions in file
4320                    try:
4321                        with gen.Capturing() as output:
4322                            dims = ncvar.idims(filen)
4323                    except:
4324                        print errmsg
4325                        print 'ncvar.idims('+filen+')'
4326                        for s1out in output: print s1out
4327                        quit(-1)
4328
4329                    if imodexp == 0:
4330                        # Dimensions in plot
4331                        dimsplot.append(dims)
4332
4333                        # Header of the name of the figure
4334                        if ivp == 0:
4335                            figname = kplot+'_'+vn+'_'+headf+'_'+ op.replace('+','-')
4336                        else:
4337                            figname = figname+'-'+vn+'_'+headf+'_'+op.replace('+','-')
4338
4339                    # pictoric values for the figure
4340                    Sfivaop = kplot + '_' + vn + '_' + op
4341                    if plotspecifics.has_key(Sfivaop):
4342                        pictvals = plotspecifics[Sfivaop]
4343                    else:
4344                        Vvals = gen.variables_values(vn)
4345                        pictvals = [Vvals[2],Vvals[3],Vvals[6], '%g', 'fixc', 'black']
4346
4347                    pictplot.append(pictvals)
4348
4349                ivp = ivp + 1
4350            # End of variable-operation
4351            figname = figname + '.' + kindfigure
4352
4353            # Title of figure
4354            titfigure = 'all!model-experiments!' + vn + optitle(op,opexplained)
4355
4356            draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot,    \
4357              figname, titfigure, kindfigure, mapvalue, timevals, expgc, odir,       \
4358              pyHOME, figscr, debug)
4359
4360        # End of variables-operations
4361
4362    # End of kind of plots
4363
4364    return
4365
4366# Files with information about the configuration of the script
4367inffiles = ['varcompute.inf', 'all_computevars.inf', 'all_statsvars.inf']
4368
4369#######    #######
4370## MAIN
4371    #######
4372if not os.path.isfile('model_graphics.dat'):
4373    print errmsg
4374    print main + ": configuration file 'model_graphics.dat' does not exist!!"
4375    quit(-1)
4376
4377# Getting configuration from external ASCII file 'model_graphics.dat'
4378cnf = gen.get_configuration('model_graphics.dat', False)
4379
4380verify_configuration(cnf, gen.Str_Bool(cnf['debug']))
4381
4382# scratches
4383scratch, filescratch, figscratch, diffscratch, figdiffscratch, moddiffscratch,       \
4384  figmoddiffscratch, figallmodexpscratch, addfiles, addfigures, adddiffs,            \
4385  adddifffigures, addmoddiffs, addmoddifffigures, addallmodexpfigures, dbg =         \
4386  scratches(cnf)
4387
4388if dbg:
4389    print 'Experiment configuration scratches _______'
4390    print '  scratch:', scratch, 'filescratch:', filescratch,'figscratch:', figscratch
4391    print '  diffscratch:', diffscratch, 'figdiffscratch:', figdiffscratch
4392    print '  moddiffscratch:', moddiffscratch, 'figmoddiffscratch:', figmoddiffscratch
4393    print 'Experiment configuration adding _______'
4394    print '  addfiles:', addfiles, 'addfigures:', addfigures
4395    print '  adddiffs:', adddiffs, 'adddifffigures:', adddifffigures
4396    print '  addmoddiffs:', addmoddiffs, 'addmoddifffigures:', addmoddifffigures
4397
4398# Getting models
4399mods = cnf['models'].split(':')
4400
4401# Getting graphical characeristics
4402modGC, expGC = graphical_characteristics(cnf, dbg)
4403
4404# Models loop
4405##
4406
4407# dictionary with the experiments of each model
4408modexps = {}
4409
4410for mod in mods:
4411    print mod
4412    if scratch:
4413        if cnf['ofold'] != cnf['ifold']:
4414            sout = sub.call('rm -rf '+cnf['ofold']+'/'+mod+' > /dev/null', shell=True)
4415        else:
4416            print warnmsg
4417            print '  ' + main + ": input '" + cnf['ifold'] + "' and output '" +      \
4418              cnf['ofold'] + "' are the same folder !!"
4419            print "    Keeping output folder although it has 'scratch' start"
4420
4421    # Get experiments and headers of model
4422    exps, fheaders = exp_headers(mod,cnf)
4423
4424    modexps[mod] = exps
4425    # Characteristics of the model
4426    Modinf = ncvar.model_characteristics(mod,'None','False')
4427    dnx = Modinf.dimxn
4428    dny = Modinf.dimyn
4429    dnz = Modinf.dimzn
4430    dnt = Modinf.dimtn
4431    dns = Modinf.dimsn
4432    vdnx = Modinf.vardxn
4433    vdny = Modinf.vardyn
4434    vdnz = Modinf.vardzn
4435    vdnt = Modinf.vardtn
4436    vdns = Modinf.vardsn
4437
4438    modgraphv = modGC[mod]
4439    Modinf = ModelInf(mod, mod, dnx, dny, dnz, dnt, dns, vdnx, vdny, vdnz, vdnt,     \
4440      vdns, modgraphv.tmod)
4441
4442    if dbg:
4443        print '  model characteristics _______'
4444        gen.printing_class(Modinf)
4445
4446    moddims = dnx + ',' + dny + ',' + dnz + ',' + dnt
4447    modvdims = vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt
4448
4449# Experiments loop
4450##
4451    for exp in exps:
4452        print '  ' + exp + '...'
4453
4454        # input folder
4455        iwdir = cnf['ifold'] + '/' + mod + '/' + exp
4456
4457        # Does input folder exist?
4458        if not os.path.isdir(iwdir):
4459            if filescratch*addfiles:
4460                print errmsg
4461                print "  " + main + ": folder '" + iwdir + "' does not exist !!"
4462                quit(-1)
4463            else:
4464                print warnmsg
4465                print "  " + main + ": folder '" + iwdir + "' does not exist !!"
4466                print "    but start of files from scratch 'filescratch':",          \
4467                  filescratch, "and there is not the add of any file 'addfiles':",   \
4468                  addfiles
4469                print '    so, keep moving'
4470
4471        owdir = cnf['ofold'] + '/' + mod + '/' + exp
4472        sout = sub.call('mkdir -p ' + owdir, shell=True)
4473
4474        # Need to pass to analyze all the data?
4475        if filescratch:
4476            for inff in inffiles:
4477                if dbg: print "    removing information file '" + inff + "' ..."
4478                ins = 'rm ' + owdir + '/' + inff + ' >& /dev/null'
4479                sout = sub.call(ins, shell=True)
4480
4481            objf = open(owdir+'/all_computevars.inf','w')
4482            objf.write("## Computation of variables \n")
4483            objf.close()
4484            objf = open(owdir+'/all_statsvars.inf','w')
4485            objf.write("## Computation of statistics \n")
4486            objf.close()
4487
4488        varcompf = owdir + '/varcompute.inf'
4489        if addfiles:
4490            if dbg:
4491                print '  ' + main + ": adding variables to compute removing " +      \
4492                  "file '" + varcompf + "' ..."
4493            sub.call('rm ' + varcompf +' >& /dev/null', shell=True)
4494
4495        if not os.path.isfile(varcompf):
4496            # Does input folder has header files?
4497            ih=1
4498            # Dictionary with the list of files for each headers
4499            files = {}
4500            # Dictionary with a file fromor each headers
4501            testfiles = {}
4502
4503            for fh in fheaders:
4504                if filescratch:
4505                    ins = 'rm '+ owdir+'/*_' + fh + '*.nc >& /dev/null'
4506                    sout = sub.call(ins, shell=True)
4507                files1h = gen.files_folder(iwdir,fh)
4508                if len(files1h) < 1:
4509                    print errmsg
4510                    print '  ' + main + ": folder '" + iwdir + "' does not " +   \
4511                      "contain files '" + fh + "*' !!"
4512                    quit(-1)
4513                files[fh] = files1h
4514                testfiles[fh] = files1h[0]
4515
4516            if dbg:
4517                print '  Dictionary of files _______'
4518                gen.printing_dictionary(files)
4519
4520                print '  Dictionary of test-files _______'
4521                gen.printing_dictionary(testfiles)
4522         
4523            allcompvar, Nvar = compvars_listconstruct(cnf, Modinf, files, testfiles, \
4524              iwdir, owdir, dbg)
4525        else: 
4526            print warnmsg
4527            print '  ' + main + ": getting variables to compute already from file !!"
4528            files, testfiles, allcompvar, Nvar = read_varcomp_file(varcompf)
4529
4530        # End of avoiding to repeat all the experiment search
4531
4532        print "    For experiment '" + exp + "' is required to compute:", Nvar,      \
4533          "variables"
4534
4535        if dbg:
4536            print 'Variables to compute _______'
4537            gen.printing_dictionary(allcompvar)
4538
4539        # Computing variables
4540        print "    Computing variables ..."
4541        compute_vars(cnf, Modinf, iwdir, owdir, files, allcompvar, filescratch, dbg)
4542
4543        # Figures of variables direct from files
4544        print "    Plotting direct figures ..."
4545        dirfigf = owdir + '/directplotsdraw.inf'
4546        if figscratch:
4547            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4548
4549            objf = open(owdir+'/all_figures.inf','w')
4550            objf.write("## Drawing of all figures \n")
4551            objf.close()
4552
4553        if addfigures:
4554            if dbg:
4555                print '  ' + main + ": adding direct figures removing file '" +      \
4556                  dirfigf + "' ..."
4557            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4558
4559        if not os.path.isfile(dirfigf):
4560            listplots, Nplt = plots_listconstruct(cnf, 'DIRPLT', dirfigf, owdir, dbg)
4561        else:
4562            print warnmsg
4563            print '  ' + main + ": getting plots to draw already from file !!"
4564            listplots, Nplt = read_plot_file(dirfigf)
4565
4566        # End of avoiding to repeat all the plots search
4567
4568        print "    For experiment '" + exp + "' is required to plot:", Nplt, "plots"
4569
4570        if dbg:
4571            print 'Plots to draw _______'
4572            gen.printing_dictionary(listplots)
4573
4574        draw_plots(cnf, listplots, mod, exp, owdir, allcompvar, figscratch, dbg)
4575
4576    # end of experiments loop
4577
4578### ## #
4579# Experiment differences
4580## # ## #
4581
4582# Experiments loop
4583##
4584    print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4585    print "  ** '" + mod + "': Inter experiments differences  "
4586    print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4587    print '    experiments:', exps
4588    # There are two kind of differences:
4589    #   DIFFop: differences between operations of each given variable.
4590    #     Differences are computed directly from the last stage of the operation
4591    #   DIFFvar: operations of differences between variables
4592    #     First are computed the differences from the initial variable file
4593    #     and then operations are made
4594    # NOTE: remember that: meanvar2 - meanvar1 = mean(var2 - var1)
4595    difffiles = ['diffop.inf', 'diffvar.inf']
4596
4597    Nexps = len(exps)
4598    for iexp1 in range(0,Nexps-1):
4599        exp1 = exps[iexp1]
4600        for iexp2 in range(iexp1+1,Nexps):
4601            exp2 = exps[iexp2]
4602            Sexps = exp2 + '-' + exp1
4603            print '  ' + Sexps + '...'
4604            owdir = cnf['ofold'] + '/' + mod + '/' + exp2 + '-' + exp1
4605            sout = sub.call('mkdir -p ' + owdir, shell=True)
4606
4607            # Removing files with list of differences if should be started from scratch or add differences
4608            for fdiff in difffiles:
4609                difff = owdir + '/' + fdiff
4610                if diffscratch:
4611                    sub.call('rm -rf' + owdir +' >& /dev/null', shell=True)
4612                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4613                    objf = open(owdir+'/all_' + fdiff,'w')
4614                    objf.write("## Computation and drawing of differences " +        \
4615                      "between '" + exp2 + "'-'" + exp1 + "'\n")
4616                    objf.close()
4617                    difff = owdir + '/all_vardiffstatistics.inf'
4618                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4619                    objf = open(owdir+'/all_' + fdiff,'w')
4620                    objf.write("## Computation of all differences statistics " +     \
4621                      "between '" + exp2 + "'-'" + exp1 + "'\n")
4622                    objf.close()
4623
4624                if adddiffs:
4625                    if dbg:
4626                        print '  ' + main + ": adding differences removing file '" + \
4627                          difff + "' ..."
4628                    sub.call('rm ' + difff +' >& /dev/null', shell=True)
4629
4630            # op and var differences
4631            diffvarcompf = owdir + '/' + difffiles[0]
4632            if not os.path.isfile(diffvarcompf):
4633                alldiffop, alldiffvar,Nopdiffs,Nvardiffs=diffvarop_listconstruct(cnf,\
4634                  [mod, mod], [exp1, exp2], [False, False], owdir, dbg)
4635            else: 
4636                print warnmsg
4637                print '  ' + main + ": getting 'op' and 'var'differences to " +      \
4638                  "compute already from file !!"
4639                alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
4640                alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
4641
4642        # End of avoiding to repeat all the experiment search
4643
4644        print "    For experiments '"+exp2+"'-'"+exp1+"' is required to compute:",   \
4645          Nvar, "differences"
4646
4647        if dbg:
4648            print 'Differences to compute _______'
4649            gen.printing_dictionary(alldiffop)
4650            gen.printing_dictionary(alldiffvar)
4651
4652        # Computing differences
4653        ##
4654        print "    Computing operation differences ..."
4655        compute_op_diffs(cnf, alldiffop, owdir, diffscratch, dbg)
4656        print "    Computing variable differences ..."
4657        compute_var_diffs(cnf, alldiffvar, owdir, diffscratch, dbg)
4658
4659        # Plotting operation differences
4660        ##
4661        print "  " + main + ": Plotting operation differences' figures ..."
4662        dirfigf = owdir + '/diffopplotsdraw.inf'
4663        if figscratch:
4664            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4665
4666            objf = open(owdir+'/all_diffopfigures.inf','w')
4667            objf.write("## Drawing of all operation difference figures \n")
4668            objf.close()
4669
4670        if adddifffigures:
4671            if dbg:
4672                print '  ' + main + ": adding differences' figures removing file '" +\
4673                  dirfigf + "' ..."
4674
4675            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4676
4677        if not os.path.isfile(dirfigf):
4678            listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFOP', dirfigf, owdir, \
4679              dbg)
4680        else:
4681            print warnmsg
4682            print '  ' + main + ": getting plots to draw already from file !!"
4683            listplots, Nplt = read_plot_file(dirfigf)
4684
4685        # End of avoiding to repeat all the plots search
4686
4687        print "  For experiment 'operation' differences '" + Sexps + "' is " +       \
4688          "required to plot:" , Nplt, "plots"
4689
4690        if dbg:
4691            print '    Plots to draw _______'
4692            gen.printing_dictionary(listplots)
4693
4694        draw_diff_plots(cnf, listplots, owdir, alldiffop, 'diffop', figdiffscratch,  \
4695          dbg)
4696
4697        # Plotting variable differences
4698        ##
4699        print "  " + main + ": Plotting variable differences' figures ..."
4700        dirfigf = owdir + '/diffvarplotsdraw.inf'
4701        if figscratch:
4702            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4703
4704            objf = open(owdir+'/all_diffvarfigures.inf','w')
4705            objf.write("## Drawing of all variables difference figures \n")
4706            objf.close()
4707
4708        if adddifffigures:
4709            if dbg:
4710                print '  ' + main + ": adding differences' figures removing file '" +\
4711                  dirfigf + "' ..."
4712            sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4713
4714        if not os.path.isfile(dirfigf):
4715            listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFVAR', dirfigf, owdir,\
4716              dbg)
4717        else:
4718            print warnmsg
4719            print '  ' + main + ": getting plots to draw already from file !!"
4720            listplots, Nplt = read_plot_file(dirfigf)
4721
4722        # End of avoiding to repeat all the plots search
4723
4724        print "  For experiment 'variables' differences '" + Sexps + "' is " +       \
4725          "required to plot:" , Nplt, "plots"
4726
4727        if dbg:
4728            print '    Plots to draw _______'
4729            gen.printing_dictionary(listplots)
4730
4731        draw_diff_plots(cnf, listplots, owdir, alldiffvar, 'diffvar', figdiffscratch,\
4732          dbg)
4733
4734# end of mods loop
4735
4736### ## #
4737# Model differences
4738### ## #
4739
4740# Models already at the reference projection
4741ModComProj = cnf['RefProj'].split(':')
4742if dbg:
4743    print 'Models to use as reference for the projection:', ModComProj
4744
4745# Operators to use for the reprojection of each variable
4746REPRJvar0 = gen.get_specdictionary_HMT(cnf, H='reprojectvar_')
4747REPRJvar = {}
4748for Skey in REPRJvar0.keys():
4749    REPRJvar[Skey] = gen.str_list(REPRJvar0[Skey],':')
4750
4751if dbg:
4752    print 'Remaping operators to use _______'
4753    gen.printing_dictionary(REPRJvar)
4754
4755# Getting common base weights for grid point remapping with CDO
4756frefgridn = 'RefProj.nc'
4757for mod in mods:
4758    frefgriddes = cnf['ofold'] + '/' + mod + '/' + frefgridn
4759    if moddiffscratch: 
4760        sout = sub.call('rm ' + frefgriddes + ' >& /dev/null', shell=True)
4761    if gen.searchInlist(ModComProj,mod) and not os.path.isfile(frefgriddes):
4762        # Looking for a file to use (all files are CF compilant, but we need to
4763        #   be sure that at least they have 'lon', 'lat' variables)
4764        exps = modexps[mod]
4765        exp = exps[0]
4766        reprjfdir = cnf['ofold'] + '/' + mod + '/' + exp
4767        reprjfile = None
4768        for reprjop in REPRJvar.keys():
4769            reprjvars = REPRJvar[cdoop]
4770            for reprjvar in reprjvars:
4771                reprjfs= gen.files_folder_HMT(folder=reprjfdir,head=reprjvar+'_',tail='.nc')
4772                for reprjf in reprjfs:
4773                    # Getting direct files as [var]_[fhead].nc
4774                    if len(reprjf.split('_')) == 2:
4775                        reprjfile = reprjfdir + '/' + reprjf
4776                        reprjvn = reprjvar
4777                        break
4778        if reprjfile is None:
4779            print errmsg
4780            print '  ' + main + ": no proper file to get projection information " +  \
4781              "from '" + reprjfdir + "' has been found !!"
4782            quit(-1)
4783
4784        print "  Creation of reference projection information file from '" + mod+ "'"
4785        if dbg:
4786            print "    using file: '" + reprjfile
4787
4788        # Getting only 1 time-step for disk space issues
4789        try:
4790             with gen.Capturing() as output:
4791                 ncvar.DataSetSection('time,0,1,1', reprjfile)
4792        except:
4793            print errmsg
4794            print 'ncvar.DataSetSection(time,0,1,1, ' + reprjfile + ')'
4795            for s1out in output: print s1out
4796            quit(-1)
4797
4798        of = reprjfile.split('.')[0] + '_time_B0-E1-I1.nc'
4799
4800        # Cleaning variables
4801        try:
4802             with gen.Capturing() as output:
4803                 ncvar.selvar('lon@lon,lat@lat,time@time', of, reprjvn)
4804        except:
4805            print errmsg
4806            print 'ncvar.selvar(lon@lon,lat@lat, ' + of + ', ' + reprjvn + ')'
4807            for s1out in output: print s1out
4808            quit(-1)
4809        sout = sub.call('mv selvar_new.nc ' + frefgriddes, shell=True)
4810        sout = sub.call('rm ' + of + ' >& /dev/null', shell=True)
4811
4812        cnf['RefProjfile'] = frefgriddes
4813
4814        if dbg:
4815            for s1out in output: print s1out
4816        break
4817
4818    elif gen.searchInlist(ModComProj,mod) and os.path.isfile(frefgriddes):
4819        # Including reprojecting reference file in configuration dictionary
4820        cnf['RefProjfile'] = frefgriddes
4821
4822Nmods = len(mods)
4823for imod1 in range(0,Nmods-1):
4824    mod1 = mods[imod1]
4825    exps1 = modexps[mod1]
4826
4827    for imod2 in range(imod1+1,Nmods):
4828        mod2 = mods[imod2]
4829        exps2 = modexps[mod2]
4830
4831        Smods = mod2 + '-' + mod1
4832        print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4833        print "  ** '" + Smods + "': Inter models differences  "
4834        print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
4835        print '    experiments mod1:', exps1
4836        print '    experiments mod2:', exps2
4837
4838# Experiments loop
4839##
4840        difffiles = ['diffop.inf', 'diffvar.inf']
4841
4842        Nexps1 = len(exps1)
4843        Nexps2 = len(exps2)
4844        for iexp1 in range(0,Nexps1):
4845            exp1 = exps1[iexp1]
4846            for iexp2 in range(0,Nexps2):
4847                exp2 = exps2[iexp2]
4848                Sexps = exp2 + '-' + exp1
4849                print '    ' + Sexps + '...'
4850                owdir = cnf['ofold'] + '/' + Smods + '/' + Sexps
4851                sout = sub.call('mkdir -p ' + owdir, shell=True)
4852
4853                # Getting the right projection in order to perform differences
4854                difmods = [mod1, mod2]
4855                difexps = [exp1, exp2]
4856                difreproj = [False, False]
4857               
4858                for imod in range(2):
4859                    mod = difmods[imod]
4860                    exp = difexps[imod]
4861                    if not gen.searchInlist(ModComProj,mod):
4862                        print  "  Projecting files of model '" + mod + "' exp: '" +  \
4863                          exp + "' with file '" + cnf['RefProjfile'] + "'"
4864                        reproject_modexp(mod, exp, cnf, REPRJvar, moddiffscratch,    \
4865                          addmoddiffs, dbg)
4866                        difreproj[imod] = True
4867
4868                # Removing files with list of differences if should be started from
4869                #   scratch or add differences
4870                for fdiff in difffiles:
4871                    difff = owdir + '/' + fdiff
4872                    if moddiffscratch:
4873                        sub.call('rm -rf' + owdir +' >& /dev/null', shell=True)
4874                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4875                        objf = open(owdir+'/all_' + fdiff,'w')
4876                        objf.write("## Computation and drawing of differences " +    \
4877                          "between '" + mod2+'@'+exp2 + "'-'" + mod1+'@'+exp1 + "'\n")
4878                        objf.close()
4879                        difff = owdir + '/all_vardiffstatistics.inf'
4880                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4881                        objf = open(owdir+'/all_' + fdiff,'w')
4882                        objf.write("## Computation of all differences statistics " + \
4883                          "between '" + mod2+'@'+exp2 + "'-'" + mod1+'@'+exp1 + "'\n")
4884                        objf.close()
4885
4886                    if addmoddiffs:
4887                        if dbg:
4888                            print '  ' + main + ": adding model differences " +      \
4889                              "removing file '" + difff + "' ..."
4890                        sub.call('rm ' + difff +' >& /dev/null', shell=True)
4891
4892                # op and var differences
4893                diffvarcompf = owdir + '/' + difffiles[0]
4894                if not os.path.isfile(owdir+'/'+difffiles[0]) or                     \
4895                  not os.path.isfile(owdir+'/'+difffiles[1]):
4896                    alldiffop, alldiffvar, Nopdiffs, Nvardiffs =                     \
4897                      diffvarop_listconstruct(cnf, difmods, difexps, difreproj,owdir,\
4898                      dbg)
4899                else: 
4900                    print warnmsg
4901                    print '  ' + main + ": getting 'op' and 'var' differences to " + \
4902                      "compute already from file !!"
4903                    alldiffop, Nopdiffops = read_diff_file(diffvarcompf)
4904                    alldiffvar, Nopdiffvars = read_diff_file(owdir+'/'+difffiles[1])
4905
4906                # End of avoiding to repeat all the experiment search
4907
4908                print "    For experiments '" + mod2+'@'+exp2 + "'-'" + mod+'@'+exp1+\
4909                  "' is required to compute:", Nvar, "differences"
4910
4911                if dbg:
4912                    print '    op differences to compute _______'
4913                    gen.printing_dictionary(alldiffop)
4914                    print '    var differences to compute _______'
4915                    gen.printing_dictionary(alldiffvar)
4916
4917# Computing differences
4918##
4919                print "    Computing operation differences ..."
4920                compute_op_diffs(cnf, alldiffop, owdir, moddiffscratch, dbg)
4921                print "    Computing variable differences ..."
4922                compute_var_diffs(cnf, alldiffvar, owdir, moddiffscratch, dbg)
4923
4924# Plotting operation differences
4925##
4926                print "  " + main + ": Plotting operation differences' figures ..."
4927                dirfigf = owdir + '/diffopplotsdraw.inf'
4928                if figmoddiffscratch:
4929                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4930
4931                objf = open(owdir+'/all_diffopfigures.inf','w')
4932                objf.write("## Drawing of all operation difference figures \n")
4933                objf.close()
4934
4935                if addmoddifffigures:
4936                    if dbg:
4937                        print '  ' + main + ": adding model differences' figures " + \
4938                          "removing file '" + dirfigf + "' ..."
4939                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4940
4941                if not os.path.isfile(dirfigf):
4942                    listplots, Nplt = plots_listconstruct(cnf, 'PLOTDIFFOP', dirfigf,\
4943                      owdir, dbg)
4944                else:
4945                    print warnmsg
4946                    print '  ' + main + ": getting plots to draw already from file !!"
4947                    listplots, Nplt = read_plot_file(dirfigf)
4948
4949                # End of avoiding to repeat all the plots search
4950
4951                print "  For experiment 'operation' differences '" + Sexps + "' is "+\
4952                  "required to plot:" , Nplt, "plots"
4953
4954                if dbg:
4955                    print 'Plots to draw _______'
4956                    gen.printing_dictionary(listplots)
4957
4958                draw_diff_plots(cnf, listplots, owdir, alldiffop, 'diffop',          \
4959                  figmoddiffscratch, dbg)
4960
4961                # Plotting variable differences
4962                ##
4963                print "  " + main + ": Plotting variable differences' figures ..."
4964                dirfigf = owdir + '/diffvarplotsdraw.inf'
4965                if figscratch:
4966                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4967
4968                    objf = open(owdir+'/all_diffvarfigures.inf','w')
4969                    objf.write("## Drawing of all variables difference figures \n")
4970                    objf.close()
4971       
4972                if adddifffigures:
4973                    if dbg:
4974                        print '  ' + main + ": adding differences's figures " +      \
4975                          "removing file '" + dirfigf + "' ..."
4976                    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
4977
4978                if not os.path.isfile(dirfigf):
4979                    listplots, Nplt = plots_listconstruct(cnf,'PLOTDIFFVAR',dirfigf, \
4980                      owdir, dbg)
4981                else:
4982                    print warnmsg
4983                    print '  ' + main + ": getting plots to draw already from file !!"
4984                    listplots, Nplt = read_plot_file(dirfigf)
4985
4986                # End of avoiding to repeat all the plots search
4987
4988                print "  For experiment 'variables' differences '" + Sexps+ "' is "+ \
4989                  "required to plot:" , Nplt, "plots"
4990
4991                if dbg:
4992                    print '    Plots to draw _______'
4993                    gen.printing_dictionary(listplots)
4994
4995                draw_diff_plots(cnf, listplots, owdir, alldiffvar, 'diffvar',        \
4996                  figdiffscratch, dbg)
4997
4998            # end of exp2 loop
4999        # end of exp1 loop
5000    # end of mod2 loop
5001# end of mod1 loop
5002
5003### ## #
5004# All model/exps linear plots
5005### ## #
5006
5007allmodexps = []
5008for mod in mods:
5009    for exp in modexps[mod]:
5010        allmodexps.append(mod + '/' + exp)
5011
5012print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5013print '  ** Multi models, multi experiments plots  '
5014print '  ******* ****** ***** **** *** ** * ** *** **** ***** ****** *******'
5015print '    Model/experiments:', allmodexps
5016
5017owdir = cnf['ofold'] + '/allmodexps'
5018sout = sub.call('mkdir -p ' + owdir, shell=True)
5019
5020# Plotting all model-experiments lines
5021##
5022print "  " + main + ": Plotting all model-experiments variable figures ..."
5023allf = owdir + '/allmodexp.inf'
5024dirfigf = owdir + '/allmodexpfigures.inf'
5025allfigf = owdir+'/all_figures.inf'
5026if figallmodexpscratch:
5027    sout = sub.call('rm ' + allf + ' >& /dev/null', shell=True)
5028    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5029    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
5030
5031    objf = open(allfigf,'w')
5032    objf.write("## Drawing of all variables figures for all model-experiments\n")
5033    objf.close()
5034       
5035if addallmodexpfigures:
5036    if dbg:
5037        print '  ' + main + ": adding model-experiment differences removing " +      \
5038          " file '" + allfigf + "' ..."
5039        print '  ' + main + ": adding model-experiment differences' figures " +      \
5040          "removing file '" + dirfigf + "' ..."
5041    sout = sub.call('rm ' + allfigf + ' >& /dev/null', shell=True)
5042    sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True)
5043
5044if not os.path.isfile(dirfigf):
5045    listplots, Nplt = plots_listconstruct(cnf, 'PLOTALLMODEXP', dirfigf, owdir, dbg)
5046else:
5047    print warnmsg
5048    print '  ' + main + ": getting plots to draw already from file !!"
5049    listplots, Nplt = read_plot_file(dirfigf)
5050
5051# End of avoiding to repeat all the plots search
5052
5053print "  Is required to plot:", Nplt, " all model-experiment plots"
5054
5055if dbg:
5056    print '    Plots to draw _______'
5057    gen.printing_dictionary(listplots)
5058
5059if not os.path.isfile(allf):
5060    # Constructing the information of modexp / variable
5061    allmodexpvar, Nallmodexpvar = allmodexps_listconstruct(cnf, allmodexps, owdir, dbg)
5062else:
5063    allmodexpvar, Nallmodexpvar = allmodexps_read(allf)
5064
5065draw_allmodexp_plots(cnf, allmodexpvar, listplots, modexps, owdir, figallmodexpscratch, dbg)
5066
5067print main + ': all files and figures have been properly done !!'
Note: See TracBrowser for help on using the repository browser.