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

Last change on this file since 1573 was 1431, checked in by lfita, 9 years ago

Adding 'mthDYNAMICO' in different places

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