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