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 | import numpy as np |
---|
7 | import nc_var_tools as ncvar |
---|
8 | import generic_tools as gen |
---|
9 | import drawing_tools as drw |
---|
10 | import time as tim |
---|
11 | # To avoid errors like: '/bin/sh: 1: Syntax error: Bad fd number' |
---|
12 | # Make sure that /bin/sh directs to bash and not to dash: |
---|
13 | # http://stackoverflow.com/questions/15809060/sh-syntax-error-bad-fd-number |
---|
14 | import subprocess as sub |
---|
15 | import os |
---|
16 | |
---|
17 | main = 'model_graphics.py' |
---|
18 | |
---|
19 | errmsg = ncvar.errormsg |
---|
20 | warnmsg = ncvar.warnmsg |
---|
21 | |
---|
22 | def scratches(config): |
---|
23 | """ Function to set-up if it is needed to start from the scratch |
---|
24 | config: dictionary with the configuration |
---|
25 | """ |
---|
26 | fname = 'scratches' |
---|
27 | |
---|
28 | if config['scratch'] == 'true': |
---|
29 | scr = True |
---|
30 | print warnmsg |
---|
31 | print " " + main + ": starting from the SCRATCH !!" |
---|
32 | print " 10 seconds left!!" |
---|
33 | filescr = True |
---|
34 | figscr = True |
---|
35 | tim.sleep(10) |
---|
36 | else: |
---|
37 | scr = False |
---|
38 | if config['filescratch'] == 'true': |
---|
39 | filescr = True |
---|
40 | print warnmsg |
---|
41 | print " " + main + ": files starting from the SCRATCH !!" |
---|
42 | print " 5 seconds left!!" |
---|
43 | tim.sleep(5) |
---|
44 | else: |
---|
45 | filescr = False |
---|
46 | |
---|
47 | if config['figscratch'] == 'true': |
---|
48 | figscr = True |
---|
49 | print warnmsg |
---|
50 | print " " + main + ": figures starting from the SCRATCH !!" |
---|
51 | print " 5 seconds left!!" |
---|
52 | tim.sleep(5) |
---|
53 | else: |
---|
54 | figscr = False |
---|
55 | |
---|
56 | if config['addfiles'] == 'true': |
---|
57 | addfils = True |
---|
58 | else: |
---|
59 | addfils = False |
---|
60 | |
---|
61 | if config['addfigures'] == 'true': |
---|
62 | addfigs = True |
---|
63 | else: |
---|
64 | addfigs = False |
---|
65 | |
---|
66 | if config['debug'] == 'true': |
---|
67 | debug = True |
---|
68 | else: |
---|
69 | debug = False |
---|
70 | |
---|
71 | return scr, filescr, figscr, addfils, addfigs, debug |
---|
72 | |
---|
73 | def exp_headers(mod,config): |
---|
74 | """ Function to provide the headers and the experiments of a given model |
---|
75 | model= model |
---|
76 | config= configuration of the experiment |
---|
77 | """ |
---|
78 | fname = 'exp_headers' |
---|
79 | |
---|
80 | # No CASE in python! |
---|
81 | # case ${mod} in ... |
---|
82 | if mod == 'WRF': |
---|
83 | expers = config['WRFexps'].split(':') |
---|
84 | fhs = config['WRFheaders'].split(':') |
---|
85 | elif mod == 'LMDZ': |
---|
86 | expers = config['LMDZexps'].split(':') |
---|
87 | fhs = config['LMDZheaders'].split(':') |
---|
88 | elif mod == 'WRF_LMDZ': |
---|
89 | expers = config['WRF_LMDZexps'].split(':') |
---|
90 | fhs = config['WRF_LMDZheaders'].split(':') |
---|
91 | else: |
---|
92 | print errmsg |
---|
93 | print " " + fname + ": model '" + mod + "' not ready!!" |
---|
94 | quit(-1) |
---|
95 | |
---|
96 | return expers, fhs |
---|
97 | |
---|
98 | class VariableInf(object): |
---|
99 | """ Class which holds all information of a given variable |
---|
100 | name= CF name of the variable |
---|
101 | header= header of the file from which it can be computed |
---|
102 | model= file-header variable from which it can be directly computed |
---|
103 | diag= file-header diagnostics from which it can be computed |
---|
104 | """ |
---|
105 | def __init__( self, name, fheader, model, diag): |
---|
106 | self.name= None |
---|
107 | self.fheader= None |
---|
108 | self.model= None |
---|
109 | self.diag= None |
---|
110 | if name is not None: |
---|
111 | self.name = name |
---|
112 | self.fheader= fheader |
---|
113 | self.model= model |
---|
114 | self.diag= diag |
---|
115 | |
---|
116 | def variable_compute(idir,var,ftests,db): |
---|
117 | """ Function to retrieve the computation way of a given variable using a series of test files |
---|
118 | iwdir= directory with the test files |
---|
119 | var= name of the variable to test |
---|
120 | filtests= dictionary with the test files for each header |
---|
121 | """ |
---|
122 | fname='variable_compute' |
---|
123 | |
---|
124 | cancompute = True |
---|
125 | for headerf in ftests.keys(): |
---|
126 | filen=ftests[headerf] |
---|
127 | try: |
---|
128 | with gen.Capturing() as output: |
---|
129 | vmod, vdiag = ncvar.computevar_model(var, idir + '/' + filen) |
---|
130 | except: |
---|
131 | print 'ncvar.computevar_model(' + var + ', ' + idir + '/' + filen + ')' |
---|
132 | for sout in output: print sout |
---|
133 | quit(-1) |
---|
134 | |
---|
135 | if vmod is None and vdiag is None: |
---|
136 | cancompute = False |
---|
137 | else: |
---|
138 | cancompute = True |
---|
139 | # Should be considered that variable can also be computed by both ways? |
---|
140 | break |
---|
141 | |
---|
142 | if not cancompute: |
---|
143 | print warnmsg |
---|
144 | print ' ' + fname + ": there is no way to compute '" + var + \ |
---|
145 | "' for model '" + mod |
---|
146 | # Too extrict! |
---|
147 | # quit(-1) |
---|
148 | |
---|
149 | # Getting only the first possibility of model and diagnostic |
---|
150 | if vmod is not None: |
---|
151 | modV = vmod[0] |
---|
152 | else: |
---|
153 | modV = None |
---|
154 | if vdiag is not None: |
---|
155 | diagV = vdiag[0] |
---|
156 | else: |
---|
157 | diagV = None |
---|
158 | |
---|
159 | varcomp = VariableInf(var, headerf, modV, diagV) |
---|
160 | |
---|
161 | # a ';' list 'varcompute' it is created for each variable giving: |
---|
162 | # [var]|[vark]|[headerf][varmod]|[vardiag] |
---|
163 | # This list will be used to compute a new file for each variable |
---|
164 | |
---|
165 | if db: |
---|
166 | print 'Variable information _______' |
---|
167 | gen.printing_class(varcomp) |
---|
168 | |
---|
169 | return varcomp |
---|
170 | |
---|
171 | def get_operations_var(vc,db): |
---|
172 | """ Function to provide the operations to make for each variable from 'VAR_' dictionary |
---|
173 | vc= 'VAR_' dictionary, dictionary with all the parameters started with 'VAR_' |
---|
174 | """ |
---|
175 | fname = 'get_operations_var' |
---|
176 | |
---|
177 | LH = len('VAR_') |
---|
178 | iop = 1 |
---|
179 | # list of operations |
---|
180 | doopers = {} |
---|
181 | # operations by variable |
---|
182 | operationsvar = {} |
---|
183 | # individual operations by variable (not repeated) |
---|
184 | indivoperationsvar = {} |
---|
185 | |
---|
186 | # Variables with a calculation whic requires vertical interpolation to all the |
---|
187 | # file (operations stating by 'VAR_pinterp') |
---|
188 | LVp = len('VAR_pinterp') |
---|
189 | varglobalp = [] |
---|
190 | |
---|
191 | for oper in vc.keys(): |
---|
192 | Loper = len(oper) |
---|
193 | opn = oper[LH:Loper+1] |
---|
194 | vns = vc[oper].split(':') |
---|
195 | ops1 = opn.split('+') |
---|
196 | doopers[opn] = ops1 |
---|
197 | for vn in vns: |
---|
198 | if not operationsvar.has_key(vn): |
---|
199 | operationsvar[vn] = [opn] |
---|
200 | indivoperationsvar[vn] = ops1 |
---|
201 | else: |
---|
202 | opers = operationsvar[vn] |
---|
203 | opers.append(opn) |
---|
204 | operationsvar[vn] = opers |
---|
205 | opers1 = indivoperationsvar[vn] |
---|
206 | for op1 in ops1: |
---|
207 | if not gen.searchInlist(opers1, op1): |
---|
208 | opers1.append(op1) |
---|
209 | indivoperationsvar[vn] = opers1 |
---|
210 | |
---|
211 | if oper[0:LVp] == 'VAR_pinterp': |
---|
212 | for vn in vns: |
---|
213 | if not gen.searchInlist(varglobalp,vn): varglobalp.append(vn) |
---|
214 | |
---|
215 | iop = iop + 1 |
---|
216 | |
---|
217 | if db: |
---|
218 | print ' operations to make _______' |
---|
219 | gen.printing_dictionary(doopers) |
---|
220 | print ' operations by variable _______' |
---|
221 | gen.printing_dictionary(operationsvar) |
---|
222 | print ' individual operations by variable _______' |
---|
223 | gen.printing_dictionary(indivoperationsvar) |
---|
224 | print ' variables with the vertical interpolation for all the file _______' |
---|
225 | print ' #', varglobalp |
---|
226 | |
---|
227 | return doopers, operationsvar, indivoperationsvar, varglobalp |
---|
228 | |
---|
229 | def pinterp_var(oper): |
---|
230 | """ Function to retrieve characteristics of the vertical interpolation for the operation |
---|
231 | oper= operation |
---|
232 | Wether vertical interpolation is: |
---|
233 | # 'global': for all file |
---|
234 | # 'local': at a given step of the process |
---|
235 | # 'none': no vertical interpolation |
---|
236 | >>> pinterp_var('last+pinterp+xmean') |
---|
237 | local |
---|
238 | >>> pinterp_var('pinterp+tmean+xmean') |
---|
239 | global |
---|
240 | >>> pinterp_var('xmean') |
---|
241 | none |
---|
242 | """ |
---|
243 | fname = 'pinterp_var' |
---|
244 | |
---|
245 | pinn = 'pinterp' |
---|
246 | |
---|
247 | Lpinterp = len(pinn) |
---|
248 | if oper[0:Lpinterp] == pinn: |
---|
249 | pkind = 'global' |
---|
250 | return pkind |
---|
251 | |
---|
252 | if oper.find(pinn) != -1: |
---|
253 | pkind = 'local' |
---|
254 | else: |
---|
255 | pkind = 'none' |
---|
256 | |
---|
257 | return pkind |
---|
258 | |
---|
259 | def compvars_listconstruct(config, minf, Files, TestFiles, idir, odir, debug): |
---|
260 | """ Function to construct the list of variables to compute |
---|
261 | config= dictionary with the configuration of the execution |
---|
262 | variables to compute are get with all values started by `VAR_' in 'module_graphics.dat', and then |
---|
263 | providing a consecutive number of calculations separated by '+' |
---|
264 | VAR_[calc1]+[calc2] = tas:wss |
---|
265 | will compute first [calc1] and then [calc2] for 'tas' and 'wss' |
---|
266 | minf= class with information about the model |
---|
267 | Files= dictionary of files for heach header |
---|
268 | TestFiles= dictionary of files to test how to compute the variable |
---|
269 | odir= output directory |
---|
270 | """ |
---|
271 | fname='compvars_listconstruct' |
---|
272 | |
---|
273 | varcomp = gen.get_specdictionary_HMT(config,H='VAR_') |
---|
274 | |
---|
275 | if debug: |
---|
276 | print ' variables to compute ________' |
---|
277 | gen.printing_dictionary(varcomp) |
---|
278 | |
---|
279 | # Getting operations by variable |
---|
280 | opers, varoper, indivaroper, varglobp = get_operations_var(varcomp,debug) |
---|
281 | |
---|
282 | strfiles = gen.dictKeysVals_stringList(Files) |
---|
283 | strtestfiles = gen.dictKeysVals_stringList(TestFiles) |
---|
284 | |
---|
285 | Svarcompute = '' |
---|
286 | |
---|
287 | # Main dictionary with all the variables and their calculation |
---|
288 | allvarcomp = {} |
---|
289 | |
---|
290 | ivop = 0 |
---|
291 | for vn in varoper: |
---|
292 | vcomp = variable_compute(idir,vn,TestFiles,False) |
---|
293 | for op in varoper[vn]: |
---|
294 | # Creation of a String as: [CFname]|[operation]|[fheader]|[vmodel]|[vdiag]|[globalP] |
---|
295 | # [CFname]: CF-name of the variable (must appear in 'variables_values.dat') |
---|
296 | # [operation]: operation to compute |
---|
297 | # [fheader]: header of file with the required variables |
---|
298 | # [vmodel]: Variable from model output |
---|
299 | # [vdiag]: Varible as diagnostic from different variables of the model output |
---|
300 | # [globalP]: Wether vertical interpolation is: |
---|
301 | # 'global': for all file |
---|
302 | # 'local': at a given step of the process |
---|
303 | # 'none': no vertical interpolation |
---|
304 | globalP = pinterp_var(op) |
---|
305 | if vcomp.model is not None: |
---|
306 | if type(vcomp.model) == type(list([1, 2])): |
---|
307 | Smodel = ':'.join(vcomp.model) |
---|
308 | elif type(vcomp.model) == type('A'): |
---|
309 | Smodel = vcomp.model |
---|
310 | else: |
---|
311 | Smodel = 'None' |
---|
312 | if vcomp.diag is not None: |
---|
313 | if type(vcomp.diag) == type(list([1, 2])): |
---|
314 | Sdiag = ':'.join(vcomp.diag) |
---|
315 | elif type(vcomp.diag) == type('A'): |
---|
316 | Sdiag = vcomp.diag |
---|
317 | else: |
---|
318 | Sdiag = 'None' |
---|
319 | |
---|
320 | Svc = vcomp.name + '|' + op + '|' + vcomp.fheader + '|' + Smodel + '|' + \ |
---|
321 | Sdiag + '|' + globalP |
---|
322 | if ivop == 0: |
---|
323 | Svarcompute = Svc |
---|
324 | else: |
---|
325 | Svarcompute = Svarcompute + ',' + Svc |
---|
326 | |
---|
327 | allvarcomp[vn + '_' + op] = [vcomp.fheader, vcomp.model, vcomp.diag, globalP] |
---|
328 | ivop = ivop + 1 |
---|
329 | |
---|
330 | if debug: |
---|
331 | print ' Variables to compute _______' |
---|
332 | gen.printing_dictionary(allvarcomp) |
---|
333 | |
---|
334 | # Outwritting the varcompute to avoid next time (if it is not filescratch!) |
---|
335 | objf = open(odir + '/varcompute.inf', 'w') |
---|
336 | objf.write('files: ' + strfiles + '\n') |
---|
337 | objf.write('testfiles: ' + strtestfiles + '\n') |
---|
338 | objf.write('varcompute: ' + Svarcompute + '\n') |
---|
339 | objf.write('itotv: ' + str(ivop) + '\n') |
---|
340 | objf.close() |
---|
341 | |
---|
342 | return allvarcomp, ivop |
---|
343 | |
---|
344 | def read_varcomp_file(filen): |
---|
345 | """ Function to read 'varcompute.inf' and reconstruct the dictionaries |
---|
346 | """ |
---|
347 | fname = 'read_varcomp_file' |
---|
348 | |
---|
349 | allvarcomp = {} |
---|
350 | |
---|
351 | objf = open(filen, 'r') |
---|
352 | |
---|
353 | for line in objf: |
---|
354 | vals = line.split(' ') |
---|
355 | if vals[0] == 'files:': |
---|
356 | Files = gen.stringList_dictKeysVals(vals[1]) |
---|
357 | elif vals[0] == 'testfiles:': |
---|
358 | TestFiles = gen.stringList_dictKeysVals(vals[1]) |
---|
359 | elif vals[0] == 'varcompute:': |
---|
360 | for VvalsS in vals[1].split(','): |
---|
361 | |
---|
362 | Vvals = VvalsS.split('|') |
---|
363 | if Vvals[3] == 'None': |
---|
364 | mod = None |
---|
365 | else: |
---|
366 | mod = Vvals[3].split(':') |
---|
367 | if Vvals[4] == 'None': |
---|
368 | diag = None |
---|
369 | else: |
---|
370 | diag = Vvals[4].split(':') |
---|
371 | |
---|
372 | allvarcomp[Vvals[0]+'_'+Vvals[1]] = [Vvals[2], mod, diag, Vvals[5]] |
---|
373 | elif vals[0] == 'itotv:': |
---|
374 | ivop = int(vals[1]) |
---|
375 | |
---|
376 | objf.close() |
---|
377 | |
---|
378 | return Files, TestFiles, allvarcomp, ivop |
---|
379 | |
---|
380 | def pinterpS(gP): |
---|
381 | """ For that variables which require vertical interpolation 'p' suffix to the file header is added |
---|
382 | gP= kind of pinterp: 'global', 'local', 'none' |
---|
383 | """ |
---|
384 | fname = 'pinterpS' |
---|
385 | |
---|
386 | if gP != 'none': |
---|
387 | gPS = 'p' |
---|
388 | else: |
---|
389 | gPS = '' |
---|
390 | |
---|
391 | return gPS |
---|
392 | |
---|
393 | def compute_variable(minf, idir, usefiles, odir, cvar, gP, scr, pyH, Tref, Tunits, db): |
---|
394 | """ Function to compute a variable |
---|
395 | minf= class with the information of the model |
---|
396 | idir= directory with the input files |
---|
397 | usefiles= dictionary of files as dict([headerf]) = [file1], ..., [fileN] |
---|
398 | odir= directory to write the output files |
---|
399 | cvar= class with the information of the variable: 'name', 'fheader', 'varmod', 'vardiag' |
---|
400 | gP= kind of vertical interpolation ('global', 'local', 'none') |
---|
401 | scr= should it be done from the scratch? |
---|
402 | pyH= location of the python HOME |
---|
403 | Tref= CF time reference |
---|
404 | Tunits= CF time units |
---|
405 | """ |
---|
406 | fname='compute_variable' |
---|
407 | |
---|
408 | CFvarn=cvar.name |
---|
409 | headerf = cvar.fheader |
---|
410 | modvar = cvar.model |
---|
411 | diagvar = cvar.diag |
---|
412 | |
---|
413 | cfiles = usefiles[headerf] |
---|
414 | |
---|
415 | # dimensions |
---|
416 | dnx = minf.dimxn |
---|
417 | dny = minf.dimyn |
---|
418 | # var-dimensions |
---|
419 | vdnx = minf.vardxn |
---|
420 | vdny = minf.vardyn |
---|
421 | |
---|
422 | # Computing separately and then joinging for all files |
---|
423 | Ntotfiles = len(cfiles) |
---|
424 | Nzeros = len(str(Ntotfiles)) |
---|
425 | NStot = str(Ntotfiles).zfill(Nzeros) |
---|
426 | |
---|
427 | # For that variables which require vertical interpolation 'p' suffix to the |
---|
428 | # file header is added |
---|
429 | SgP = pinterpS(gP) |
---|
430 | |
---|
431 | # Getting in working dir |
---|
432 | os.chdir(odir) |
---|
433 | |
---|
434 | # File to keep track of all operations |
---|
435 | otrackf = open( odir + '/all_computevars.inf', 'a') |
---|
436 | |
---|
437 | # Computing variable |
---|
438 | ifile=1 |
---|
439 | for cf in cfiles: |
---|
440 | ifS = str(ifile).zfill(Nzeros) |
---|
441 | ifilen = odir + '/' + CFvarn + '_' + headerf + SgP + '_' + ifS + '-' + \ |
---|
442 | NStot + '.nc' |
---|
443 | fileon = odir + '/' + CFvarn + '_' + headerf + SgP + '.nc' |
---|
444 | |
---|
445 | if scr: |
---|
446 | sout = sub.call('rm ' + ifilen + ' >& /dev/null', shell=True) |
---|
447 | sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True) |
---|
448 | |
---|
449 | if not os.path.isfile(ifilen) and not os.path.isfile(fileon): |
---|
450 | # Since model direct values are retrieved from `variables_valules.dat' which was |
---|
451 | # initially coincived as a way to only give variable attributes, range and color |
---|
452 | # bars, if a variable has a diagnostic way to be computed, the later one will be |
---|
453 | # preferred |
---|
454 | if db: |
---|
455 | print ' ' + fname + ": creation of variable file '" + CFvarn + \ |
---|
456 | "' in file '" + ifilen + "' ..." |
---|
457 | |
---|
458 | if modvar is not None and diagvar is None: |
---|
459 | # model variable |
---|
460 | values = modvar[0] + ',0,-1,-1' |
---|
461 | vs = modvar[0] + ',' + vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt |
---|
462 | with gen.Capturing() as output: |
---|
463 | ncvar.DataSetSection_multivars(values, cf, vs) |
---|
464 | |
---|
465 | newfile, loc = gen.search_sec_list(output,'succesfull') |
---|
466 | ofile = newfile[0].split(' ')[7] |
---|
467 | sub.call('mv ' + ofile + ' ' + ifilen, shell=True) |
---|
468 | |
---|
469 | # Keeping track of the operations |
---|
470 | pyins = pyH + "/nc_var.py -f "+cf+" -o DataSetSection_multivars -v "+\ |
---|
471 | vs + " -S '" + values + "'" |
---|
472 | otrackf.write('\n') |
---|
473 | otrackf.write('# ' + CFvarn + " " + modvar[0] + '\n') |
---|
474 | otrackf.write(pyins + '\n') |
---|
475 | |
---|
476 | # CF renaming of variable |
---|
477 | ncvar.chvarname(CFvarn,ifilen,modvar[0]) |
---|
478 | else: |
---|
479 | # diagnostic variable |
---|
480 | dims = dnt+'@'+vdnt+','+dnz+'@'+vdnz+','+dny+'@'+vdny+','+dnx+'@'+vdnx |
---|
481 | diagn = diagvar[0] |
---|
482 | Ndiagvars = len(diagvar) |
---|
483 | diagc = '@'.join(diagvar[1:]) |
---|
484 | |
---|
485 | values = '-f ' + cf + " -d '" + dims + "' -v '" + diagn + '|' + \ |
---|
486 | diagc + "'" |
---|
487 | try: |
---|
488 | with gen.Capturing() as output: |
---|
489 | sout = sub.call('python ' +pyH+ '/diagnostics.py ' + values, \ |
---|
490 | shell=True) |
---|
491 | except: |
---|
492 | print 'python ' + pyH + '/diagnostics.py ' + values |
---|
493 | for sout in output: print sout |
---|
494 | quit(-1) |
---|
495 | if db: |
---|
496 | for sout in output: print sout |
---|
497 | |
---|
498 | sout = sub.call(' mv diagnostics.nc ' + ifilen, shell=True) |
---|
499 | |
---|
500 | # Keeping track of the operations |
---|
501 | pyins = 'python ' + pyH + '/diagnostics.py ' + values |
---|
502 | otrackf.write('\n') |
---|
503 | otrackf.write('# ' + CFvarn + " " + diagn + '\n') |
---|
504 | otrackf.write(pyins + '\n') |
---|
505 | |
---|
506 | # Attaching necessary variables for the pressure interpolation |
---|
507 | if gP != 'none': |
---|
508 | requiredinterpvars = ['P', 'PB', 'PSFC', 'PH', 'PHB', 'HGT', 'T', \ |
---|
509 | 'QVAPOR', 'XLONG', 'XLAT', 'Times'] |
---|
510 | print " " + fname + ": adding variables:", requiredinterpvars, \ |
---|
511 | ' to allow pressure interpolation' |
---|
512 | for rqv in requiredinterpvars: |
---|
513 | try: |
---|
514 | with gen.Capturing() as output: |
---|
515 | ncvar.fvaradd(idir+'/'+cf+','+rqv,ifilen) |
---|
516 | except: |
---|
517 | print 'fvaradd('+idir+'/'+cf+','+rqv+','+ifilen+')' |
---|
518 | for sout in output: print sout |
---|
519 | quit(-1) |
---|
520 | if db: |
---|
521 | for sout in output: print sout |
---|
522 | |
---|
523 | # adding CF lon,lat,time in WRF files |
---|
524 | if minf.name == 'WRF': |
---|
525 | values = vdnx + ':' + vdny + ':'+ Tref + ':' + Tunits |
---|
526 | ncvar.WRF_toCF(values, ifilen) |
---|
527 | |
---|
528 | ifile = ifile + 1 |
---|
529 | |
---|
530 | otrackf.close() |
---|
531 | |
---|
532 | # Joining variable files |
---|
533 | if not os.path.isfile(fileon): |
---|
534 | if db: |
---|
535 | print ' ' + fname + ": concatenating all variable files to create '" + \ |
---|
536 | fileon + "..." |
---|
537 | try: |
---|
538 | with gen.Capturing() as output: |
---|
539 | ncvar.netcdf_fold_concatenation_HMT('./,time', CFvarn+'_'+headerf+ \ |
---|
540 | SgP+'_,-,.nc', 'all') |
---|
541 | except: |
---|
542 | print 'netcdf_fold_concatenation_HMT(./time, '+CFvarn+'_'+headerf+SgP+ \ |
---|
543 | '_,-,.nc, all)' |
---|
544 | for sout in output: print sout |
---|
545 | quit(-1) |
---|
546 | if db: |
---|
547 | for sout in output: print sout |
---|
548 | |
---|
549 | sout = sub.call('mv netcdf_fold_concatenated_HMT.nc ' + fileon, shell=True) |
---|
550 | if os.path.isfile(fileon): |
---|
551 | sout = sub.call('rm ' + CFvarn + '_' + headerf + '_*-*.nc', shell=True) |
---|
552 | |
---|
553 | return |
---|
554 | |
---|
555 | def compute_statistics(minf, config, idir, usefiles, odir, cvar, gP, Opers, scr, db): |
---|
556 | """ Function to compute different statistics it will take previous steps if they |
---|
557 | are availale |
---|
558 | minf= class with the information of the model |
---|
559 | config= dictionary with the configuration of the experiment |
---|
560 | idir= directory with the input files |
---|
561 | usefiles= ',' list of files to use [file1],...,[fileN] |
---|
562 | odir= directory to write the output files |
---|
563 | cvar= class with the information of the variable: 'name', 'fheader', 'varmod', 'vardiag' |
---|
564 | gP= kind of vertical interpolation ('global', 'local', 'none') |
---|
565 | Opers= kind of operation: (as possible multiple consecutive combination of operations separated by '+' |
---|
566 | [calc1]+[calc2] will compute first [calc1] and then [calc2] |
---|
567 | acc: temporal accumulated values |
---|
568 | diff: differences between models |
---|
569 | direct: no statistics |
---|
570 | last: last temporal value |
---|
571 | Lmean: latitudinal mean values |
---|
572 | Lsec: latitudinal section (latitudinal value must be given, [var]@[lat]) |
---|
573 | lmean: longitudinal mean values |
---|
574 | lsec: longitudinal section (longitudinal value must be given, [var]@[lat]) |
---|
575 | pinterp: pressure interpolation (to the given $plevels) |
---|
576 | tmean: temporal mean values |
---|
577 | turb: Taylor's turbulence decomposition value |
---|
578 | xmean: x-axis mean values |
---|
579 | ymean: y-axis mean values |
---|
580 | zsum: vertical aggregated values |
---|
581 | scr= should it be done from the scratch? |
---|
582 | """ |
---|
583 | fname='compute_statistics' |
---|
584 | |
---|
585 | # Getting a previous file name to continue with(for combinations) |
---|
586 | CFvarn=cvar.name |
---|
587 | headerf = cvar.fheader |
---|
588 | modvar = cvar.model |
---|
589 | diagvar = cvar.diag |
---|
590 | |
---|
591 | cfiles = usefiles[headerf] |
---|
592 | |
---|
593 | # dimensions |
---|
594 | dnx = minf.dimxn |
---|
595 | dny = minf.dimyn |
---|
596 | # var-dimensions |
---|
597 | vdnx = minf.vardxn |
---|
598 | vdny = minf.vardyn |
---|
599 | # Model dimension-variable dictionary |
---|
600 | Modeldimvardict = {dnx: vdnx, dny: vdny, dnz: vdnz, dnt: vdnt} |
---|
601 | |
---|
602 | # Some experiment configuration values |
---|
603 | # plevels= ':' separated list of pressures (in Pa) to use for the vertical interpolation |
---|
604 | plevels = config['plevels'] |
---|
605 | # pyH= location of the python HOME |
---|
606 | pyH = config['pyHOME'] |
---|
607 | # Tref= CF time reference |
---|
608 | Tref = config['CFreftime'] |
---|
609 | # Tunits= CF time units |
---|
610 | Tunits = config['CFunitstime'] |
---|
611 | # opsur = operation surnames |
---|
612 | |
---|
613 | opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='') |
---|
614 | Opsurs = {} |
---|
615 | for opk in opsur: |
---|
616 | opn = opk.split('_')[1] |
---|
617 | vals = opsur[opk] |
---|
618 | Opsurs[opn] = vals.split(':') |
---|
619 | # if db: |
---|
620 | # print ' ' + fname + ' operation surnames _______' |
---|
621 | # gen.printing_dictionary(Opsurs) |
---|
622 | |
---|
623 | # Getting in working dir |
---|
624 | os.chdir(odir) |
---|
625 | |
---|
626 | # File to keep track of all operations |
---|
627 | otrackf = open( odir + '/all_statsvars.inf', 'a') |
---|
628 | |
---|
629 | # For that variables which require vertical interpolation 'p' suffix to the file |
---|
630 | # header is added. |
---|
631 | # After some operations some CF dimension-variables might not be kept |
---|
632 | |
---|
633 | if gP != 'none': |
---|
634 | SgP = 'p' |
---|
635 | varnCFs = ['lon', 'lat', 'pres', 'time'] |
---|
636 | else: |
---|
637 | SgP = '' |
---|
638 | varnCFs = ['lon', 'lat', 'time'] |
---|
639 | |
---|
640 | # CF dimension-variable dictionary |
---|
641 | CFdimvardict = {'lon': 'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'} |
---|
642 | |
---|
643 | # Input file |
---|
644 | ifilen = odir + '/' + CFvarn + '_' + headerf + SgP + '.nc' |
---|
645 | |
---|
646 | # Computing variable statisitcs |
---|
647 | istats=0 |
---|
648 | |
---|
649 | # List of not computed statistics |
---|
650 | wrongstats = [] |
---|
651 | |
---|
652 | opers = Opers.split('+') |
---|
653 | Fopers = '' |
---|
654 | if db: print " computing statistics of variable:'", CFvarn, "' operation '" + \ |
---|
655 | Opers + "'...'" |
---|
656 | for op in opers: |
---|
657 | |
---|
658 | # File name from previous operations, and name of the variable within the |
---|
659 | # file (some operations change the name of the variable) |
---|
660 | if op == opers[0]: |
---|
661 | Fopers = op |
---|
662 | prevfile = ifilen |
---|
663 | vninF = CFvarn |
---|
664 | else: |
---|
665 | Fopers = Fopers + '_' + op |
---|
666 | prevfile = fileon |
---|
667 | fileon = odir + '/' + CFvarn + '_' + headerf + SgP + '_' + Fopers + '.nc' |
---|
668 | |
---|
669 | # Adding required variables for the vertical interpolation in all operations |
---|
670 | SvarnCFs = ',' + ','.join(varnCFs) |
---|
671 | |
---|
672 | if gP != 'none': |
---|
673 | if gP == 'local': |
---|
674 | CFvarnp = vninF + ',P,PB,PSFC,PH,PHB,HGT,T,QVAPOR,XLONG,XLAT,Times'+ \ |
---|
675 | SvarnCFs |
---|
676 | else: |
---|
677 | CFvarnp = vninF + SvarnCFs |
---|
678 | else: |
---|
679 | CFvarnp = vninF + SvarnCFs |
---|
680 | |
---|
681 | if scr: |
---|
682 | sout = sub.call('rm ' + fileon + ' >& /dev/null', shell=True) |
---|
683 | |
---|
684 | hprevfile = prevfile[0:len(prevfile)-3] |
---|
685 | |
---|
686 | if db: print ' ', op, "using '" + prevfile + "' " |
---|
687 | # Just produce file in case output file does not exist |
---|
688 | if not os.path.isfile(fileon): |
---|
689 | if db: |
---|
690 | print ' ' + fname + ": creation of file '" + fileon + "' ..." |
---|
691 | dofile = True |
---|
692 | else: |
---|
693 | dofile = False |
---|
694 | |
---|
695 | # Variables to be kept in the final file |
---|
696 | varkeep = [] |
---|
697 | |
---|
698 | if op == 'acc': |
---|
699 | # temporal accumulated values |
---|
700 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
701 | wrongstats.appen(CFvarn + '_' + opers) |
---|
702 | break |
---|
703 | elif op == 'direct': |
---|
704 | # no statistics |
---|
705 | if dofile: |
---|
706 | sout = sub.call('mv ' + prevfile + ' ' + fileon, shell=True) |
---|
707 | elif op == 'last': |
---|
708 | # last temporal value |
---|
709 | vals='time,-9,0,0' |
---|
710 | if dofile: |
---|
711 | try: |
---|
712 | with gen.Capturing() as output: |
---|
713 | ncvar.DataSetSection(vals,prevfile) |
---|
714 | except: |
---|
715 | print 'DataSetSection('+vals+',', prevfile, ')' |
---|
716 | for sout in output: print sout |
---|
717 | quit(-1) |
---|
718 | |
---|
719 | if db: |
---|
720 | for sout in output: print sout |
---|
721 | |
---|
722 | sout = sub.call('mv ' + hprevfile + '_time_B-9-E0-I0.nc ' + fileon, \ |
---|
723 | shell=True) |
---|
724 | |
---|
725 | # Keeping the operations |
---|
726 | pyins=pyH + "/nc_var.py -o DataSetSection -S '" + vals + "' -f " + \ |
---|
727 | prevfile |
---|
728 | otrackf.write("\n") |
---|
729 | otrackf.write("# " + CFvarn + " " + Fopers + "\n") |
---|
730 | otrackf.write(pyins + "\n") |
---|
731 | |
---|
732 | elif op =='Lmean': |
---|
733 | # latitudinal mean values |
---|
734 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
735 | wrongstats.appen(CFvarn + '_' + opers) |
---|
736 | break |
---|
737 | elif op =='Lsec': |
---|
738 | # latitudinal section (latitudinal value must be given, [var]@[lat]) |
---|
739 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
740 | wrongstats.appen(CFvarn + '_' + opers) |
---|
741 | break |
---|
742 | elif op =='lmean': |
---|
743 | # longitudinal mean values |
---|
744 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
745 | wrongstats.appen(CFvarn + '_' + opers) |
---|
746 | break |
---|
747 | elif op =='lsec': |
---|
748 | # longitudinal section (longitudinal value must be given, [var]@[lon]) |
---|
749 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
750 | wrongstats.appen(CFvarn + '_' + opers) |
---|
751 | break |
---|
752 | elif op == 'pinterp': |
---|
753 | # pinterp: pressure interpolation (to the given $plevels) |
---|
754 | vals=plevels + ',1,1' |
---|
755 | if dofile: |
---|
756 | try: |
---|
757 | with gen.Capturing() as output: |
---|
758 | ncvar.pinterp(vals,prevfile,vninF) |
---|
759 | except: |
---|
760 | print 'pinterp('+vals+',', prevfile, ','+vninF+')' |
---|
761 | for sout in output: print sout |
---|
762 | quit(-1) |
---|
763 | |
---|
764 | if db: |
---|
765 | for sout in output: print sout |
---|
766 | |
---|
767 | sout = sub.call('mv pinterp.nc ' + fileon, shell=True) |
---|
768 | |
---|
769 | # Keeping the operations |
---|
770 | pyins=pyH + "/nc_var.py -o pinterp -S '" + vals + "' -f " + \ |
---|
771 | prevfile + "-v " + vninF |
---|
772 | otrackf.write("\n") |
---|
773 | otrackf.write("# " + CFvarn + " " + Fopers + "\n") |
---|
774 | otrackf.write(pyins + "\n") |
---|
775 | |
---|
776 | # adding CF lon,lat,time in WRF files |
---|
777 | if minf.name == 'WRF': |
---|
778 | values = vdnx + ':' + vdny + ':'+ Tref + ':' + Tunits |
---|
779 | ncvar.WRF_toCF(values, fileon) |
---|
780 | |
---|
781 | # vertical interpolation variables are no more needed |
---|
782 | CFvarnp = vninF |
---|
783 | gP = 'none' |
---|
784 | |
---|
785 | elif op == 'tmean': |
---|
786 | # temporal mean values |
---|
787 | vals='time|-1,time,mean,' + ':'.join(varnCFs) + ':' + vdnz |
---|
788 | dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz |
---|
789 | if dofile: |
---|
790 | try: |
---|
791 | with gen.Capturing() as output: |
---|
792 | ncvar.file_oper_alongdims(vals,prevfile,CFvarnp) |
---|
793 | except: |
---|
794 | print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')' |
---|
795 | for sout in output: print sout |
---|
796 | quit(-1) |
---|
797 | |
---|
798 | if db: |
---|
799 | for sout in output: print sout |
---|
800 | |
---|
801 | sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True) |
---|
802 | |
---|
803 | # Keeping the operations |
---|
804 | pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals + \ |
---|
805 | "' -f " + prevfile + " -v " + CFvarnp |
---|
806 | otrackf.write("\n") |
---|
807 | otrackf.write("# " + CFvarn + " " + Fopers + "\n") |
---|
808 | otrackf.write(pyins + "\n") |
---|
809 | |
---|
810 | # removing dimension variable-dimension 'time' |
---|
811 | varnCFs.remove('time') |
---|
812 | |
---|
813 | varkeep.append('timestats') |
---|
814 | |
---|
815 | # elif op == 'turb': |
---|
816 | # # turbulence values |
---|
817 | # vals='turbulence|'${CFvarn} |
---|
818 | # dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon,pres@pres' |
---|
819 | # pyout=`python ${pyHOME}/diagnostics.py -d ${dims} -v ${vals} -f ${cfiles}` |
---|
820 | |
---|
821 | # # Keeping the operations |
---|
822 | # pyins="python "${pyHOME}"/diagnostics.py -d "${dims}" -v '"${vals} |
---|
823 | # pyins=${pyins}"' -f ${cfiles}" |
---|
824 | # echo " " >> ${odir}/all_statsvars.inf |
---|
825 | # echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf |
---|
826 | # echo ${pyins} >> ${odir}/all_statsvars.inf |
---|
827 | |
---|
828 | # varkeep=':'${CFvarn}'turb' |
---|
829 | |
---|
830 | elif op == 'xmean': |
---|
831 | # x-axis mean values |
---|
832 | vals='lon|-1,lon,mean,' + ':'.join(varnCFs) + ':' + vdnz |
---|
833 | dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz |
---|
834 | if dofile: |
---|
835 | try: |
---|
836 | with gen.Capturing() as output: |
---|
837 | ncvar.file_oper_alongdims(vals,prevfile,CFvarnp) |
---|
838 | except: |
---|
839 | print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')' |
---|
840 | for sout in output: print sout |
---|
841 | quit(-1) |
---|
842 | |
---|
843 | if db: |
---|
844 | for sout in output: print sout |
---|
845 | |
---|
846 | sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True) |
---|
847 | |
---|
848 | # Keeping the operations |
---|
849 | pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals + \ |
---|
850 | "' -f " + prevfile + " -v " + CFvarnp |
---|
851 | otrackf.write("\n") |
---|
852 | otrackf.write("# " + CFvarn + " " + Fopers + "\n") |
---|
853 | otrackf.write(pyins + "\n") |
---|
854 | |
---|
855 | varkeep.append('lonstats') |
---|
856 | |
---|
857 | elif op == 'ymean': |
---|
858 | # y-axis mean values |
---|
859 | vals='lat|-1,lat,mean,' + ':'.join(varnCFs) + ':' + vdnz |
---|
860 | dims = gen.dictvar_listS(varnCFs,CFdimvardict,',','@') + dnz+'@'+vdnz |
---|
861 | if dofile: |
---|
862 | try: |
---|
863 | with gen.Capturing() as output: |
---|
864 | ncvar.file_oper_alongdims(vals,prevfile,CFvarnp) |
---|
865 | except: |
---|
866 | print 'file_oper_alongdims('+vals+',', prevfile, ','+CFvarnp+')' |
---|
867 | for sout in output: print sout |
---|
868 | quit(-1) |
---|
869 | |
---|
870 | if db: |
---|
871 | for sout in output: print sout |
---|
872 | |
---|
873 | sout = sub.call('mv file_oper_alongdims_mean.nc '+fileon, shell=True) |
---|
874 | |
---|
875 | # Keeping the operations |
---|
876 | pyins=pyH + "/nc_var.py -o file_oper_alongdims -S '" + vals + \ |
---|
877 | "' -f " + prevfile + " -v " + CFvarnp |
---|
878 | otrackf.write("\n") |
---|
879 | otrackf.write("# " + CFvarn + " " + Fopers + "\n") |
---|
880 | otrackf.write(pyins + "\n") |
---|
881 | |
---|
882 | varkeep.append('latstats') |
---|
883 | |
---|
884 | elif op == 'zsum': |
---|
885 | # vertical aggregated values |
---|
886 | print " " + fname + ": kind '" + op + "' not ready !!" |
---|
887 | wrongstats.append(CFvarn + '_' + opers) |
---|
888 | break |
---|
889 | else: |
---|
890 | print errmsg |
---|
891 | print ' ' + fname + ": operation '" + op + "' not ready !!" |
---|
892 | quit(-1) |
---|
893 | |
---|
894 | # End of kind of operation |
---|
895 | |
---|
896 | # Variable name in file (vninF) changed due to operation |
---|
897 | # but only if previous operation does not the same 'statistic' |
---|
898 | chvn = gen.dictionary_key_list(Opsurs, op) |
---|
899 | if chvn is not None: |
---|
900 | oldvninF = vninF |
---|
901 | vninF = vninF + chvn |
---|
902 | CFvarnp = CFvarnp.replace(oldvninF,vninF) |
---|
903 | |
---|
904 | if dofile: |
---|
905 | if len(varkeep) > 0: |
---|
906 | varkeepS = ',' + ','.join(varkeep) |
---|
907 | else: |
---|
908 | varkeepS = '' |
---|
909 | |
---|
910 | # Adding such CF dimension variables which might not be in the |
---|
911 | # post-operation file |
---|
912 | try: |
---|
913 | with gen.Capturing() as output: |
---|
914 | varinfile = ncvar.ivars(fileon) |
---|
915 | except: |
---|
916 | print 'ivars('+fileon+')' |
---|
917 | for sout in output: print sout |
---|
918 | quit(-1) |
---|
919 | |
---|
920 | for CFvn in varnCFs: |
---|
921 | if not gen.searchInlist(varinfile, CFvn): |
---|
922 | if db: |
---|
923 | print ' ' + fname + ": recupering CF variable '" + CFvn + "'" |
---|
924 | try: |
---|
925 | with gen.Capturing() as output: |
---|
926 | ncvar.fvaradd(prevfile+','+CFvn, fileon) |
---|
927 | except: |
---|
928 | print 'fvaradd(', prevfile, ','+CFvn+','+fileon+')' |
---|
929 | for sout in output: print sout |
---|
930 | quit(-1) |
---|
931 | |
---|
932 | if db: |
---|
933 | for sout in output: print sout |
---|
934 | |
---|
935 | totalvarkeeps = CFvarnp + ',' + ','.join(varnCFs) + varkeepS |
---|
936 | try: |
---|
937 | with gen.Capturing() as output: |
---|
938 | oclean = ncvar.cleaning_varsfile(totalvarkeeps,fileon) |
---|
939 | except: |
---|
940 | print 'cleaning_varsfile('+totalvarkeeps+','+fileon+')' |
---|
941 | for sout in output: print sout |
---|
942 | quit(-1) |
---|
943 | |
---|
944 | if db: |
---|
945 | for sout in output: print sout |
---|
946 | |
---|
947 | # End of operations |
---|
948 | if db: |
---|
949 | print fname + ": successful calculation of '" + vninF + "' from '" + \ |
---|
950 | CFvarn + "' '" + Opers + "' !!" |
---|
951 | |
---|
952 | if len(wrongstats) > 1: |
---|
953 | print warnmsg |
---|
954 | print ' ' + fname + ": statisitcs not possible to compute:", wrongstats |
---|
955 | |
---|
956 | return |
---|
957 | |
---|
958 | def compute_vars(config, modinf, idir, odir, Files, allvarcomp, fscr, debug): |
---|
959 | """ Function to compute the variables |
---|
960 | config= Configuration of the experiment |
---|
961 | modinf= class with information about the model |
---|
962 | idir= input experiment folder |
---|
963 | odir= output experiment folder |
---|
964 | Files= dictionary with the header and the files' correspondence |
---|
965 | allvarcomp= dictionary with all the variables to compute and their information |
---|
966 | fscr= whether files should be done from the scratch or not |
---|
967 | """ |
---|
968 | fname = 'compute_vars' |
---|
969 | |
---|
970 | for vopn in allvarcomp: |
---|
971 | # variable & operation |
---|
972 | vn = vopn.split('_')[0] |
---|
973 | op = vopn.split('_')[1] |
---|
974 | vopnV = allvarcomp[vopn] |
---|
975 | fheader = vopnV[0] |
---|
976 | model = vopnV[1] |
---|
977 | diag = vopnV[2] |
---|
978 | globalP = vopnV[3] |
---|
979 | |
---|
980 | if debug: |
---|
981 | print ' ' + vn + ': ' + op |
---|
982 | |
---|
983 | # Computing CF variable |
---|
984 | if model is not None or diag is not None: |
---|
985 | vinf = VariableInf(vn,fheader,model,diag) |
---|
986 | # Comppute variable |
---|
987 | compute_variable(modinf, idir, Files, odir, vinf, globalP, fscr, \ |
---|
988 | config['pyHOME'], config['CFreftime'], config['CFunitstime'], debug) |
---|
989 | |
---|
990 | # Compute variable statistics |
---|
991 | compute_statistics(modinf, cnf, iwdir, Files, owdir, vinf, globalP, op, \ |
---|
992 | fscr, debug) |
---|
993 | |
---|
994 | else: |
---|
995 | print errmsg |
---|
996 | print ' ' + fname + ": neither 'model' or 'diag' variables for '" + vn \ |
---|
997 | + "' !!" |
---|
998 | quit(-1) |
---|
999 | |
---|
1000 | return |
---|
1001 | |
---|
1002 | def get_plots_var(vc,pltk,db): |
---|
1003 | """ Function to provide the plots to make for each variable from pltk ('DIRPLT_', or 'DIFFPLT_') dictionary |
---|
1004 | vc= pltk dictionary, dictionary with all the parameters started with pltk |
---|
1005 | pltk= kind of plots. Values started by 'DIRPLT_', or 'DIFFPLT_' in model configuration |
---|
1006 | """ |
---|
1007 | fname = 'get_plots_var' |
---|
1008 | |
---|
1009 | LH = len(pltk) |
---|
1010 | # list of plots |
---|
1011 | doplots = {} |
---|
1012 | # plots by variable |
---|
1013 | plotsvar = {} |
---|
1014 | # individual plots by variable (not repeated) |
---|
1015 | indivplotsvar = {} |
---|
1016 | |
---|
1017 | # Variables with a plot which requires vertical interpolation |
---|
1018 | varplotp = [] |
---|
1019 | |
---|
1020 | ipl = 0 |
---|
1021 | for plot in vc.keys(): |
---|
1022 | Lplot = len(plot) |
---|
1023 | pln = plot[LH:Lplot+1] |
---|
1024 | varops = vc[plot].split(':') |
---|
1025 | for vn in varops: |
---|
1026 | VOn = vn.split('#') |
---|
1027 | # Variables and operations in plot |
---|
1028 | vplt = [] |
---|
1029 | oplt = [] |
---|
1030 | voplt = [] |
---|
1031 | for vop in VOn: |
---|
1032 | vv = vop.split('|')[0] |
---|
1033 | oo = vop.split('|')[1] |
---|
1034 | vplt.append(vv) |
---|
1035 | oplt.append(oo) |
---|
1036 | if not gen.searchInlist(voplt,vop): voplt.append(vop) |
---|
1037 | vpltS = '#'.join(vplt) |
---|
1038 | opltS = '#'.join(oplt) |
---|
1039 | |
---|
1040 | if not doplots.has_key(pln): |
---|
1041 | doplots[pln] = [vn] |
---|
1042 | else: |
---|
1043 | plots = doplots[pln] |
---|
1044 | plots.append(vn) |
---|
1045 | doplots[pln] = plots |
---|
1046 | |
---|
1047 | if not plotsvar.has_key(vn): |
---|
1048 | plotsvar[vn] = [pln] |
---|
1049 | else: |
---|
1050 | plots = plotsvar[vn] |
---|
1051 | plots.append(pln) |
---|
1052 | plotsvar[vn] = plots |
---|
1053 | |
---|
1054 | for VOv in voplt: |
---|
1055 | if not indivplotsvar.has_key(VOv): |
---|
1056 | indivplotsvar[VOv] = [pln] |
---|
1057 | else: |
---|
1058 | plots = indivplotsvar[VOv] |
---|
1059 | indivplots.append(pln) |
---|
1060 | indivplotsvar[VOv] = plots |
---|
1061 | |
---|
1062 | if vc[plot].find('pinterp') != -1: |
---|
1063 | if not gen.searchInlist(varplotp,vn): varplotp.append(pln) |
---|
1064 | |
---|
1065 | ipl = ipl + 1 |
---|
1066 | |
---|
1067 | if db: |
---|
1068 | print ' plots to make _______' |
---|
1069 | gen.printing_dictionary(doplots) |
---|
1070 | print ' plots by variable|operation _______' |
---|
1071 | gen.printing_dictionary(plotsvar) |
---|
1072 | print ' individual plots by variable|operation _______' |
---|
1073 | gen.printing_dictionary(indivplotsvar) |
---|
1074 | print ' plots with the vertical interpolation _______' |
---|
1075 | print ' #', varplotp |
---|
1076 | |
---|
1077 | return doplots, plotsvar, indivplotsvar, varplotp |
---|
1078 | |
---|
1079 | def plots_listconstruct(config, odir, debug): |
---|
1080 | """ Function to create the list of plots to draw |
---|
1081 | config= Configuration of the experiment |
---|
1082 | odir= output experiment folder |
---|
1083 | """ |
---|
1084 | fname='plots_listconstruct' |
---|
1085 | |
---|
1086 | dirplot = gen.get_specdictionary_HMT(config, H='DIRPLT_') |
---|
1087 | |
---|
1088 | if debug: |
---|
1089 | print ' direct plots to draw ________' |
---|
1090 | gen.printing_dictionary(dirplot) |
---|
1091 | |
---|
1092 | # Getting plots by variable |
---|
1093 | plots, varplot, indivarplot, plotp = get_plots_var(dirplot,'DIRPLT_',debug) |
---|
1094 | |
---|
1095 | Svarplot = gen.dictKeysVals_stringList(plots,cV=':') |
---|
1096 | |
---|
1097 | Nplots = 0 |
---|
1098 | for pl in Svarplot.split(','): |
---|
1099 | Nplots = Nplots + len(pl.split(':')) |
---|
1100 | |
---|
1101 | # Outwritting the varcompute to avoid next time (if it is not filescratch!) |
---|
1102 | objf = open(odir + '/directplotsdraw.inf', 'w') |
---|
1103 | objf.write('plots: ' + Svarplot + '\n') |
---|
1104 | objf.write('itotp: ' + str(Nplots) + '\n') |
---|
1105 | objf.close() |
---|
1106 | |
---|
1107 | return plots, Nplots |
---|
1108 | |
---|
1109 | def read_plot_file(figfile): |
---|
1110 | """ Function to read the file with the information about the plots to draw |
---|
1111 | figfile= file with the information |
---|
1112 | """ |
---|
1113 | fname = 'read_plot_file' |
---|
1114 | |
---|
1115 | if not os.path.isfile(figfile): |
---|
1116 | print errormsg |
---|
1117 | print ' ' + fname + ": figures file '" + figfile + "' does not exist !!" |
---|
1118 | quit(-1) |
---|
1119 | |
---|
1120 | objf = open(figfile, 'r') |
---|
1121 | for line in objf: |
---|
1122 | if line[0:1] != '#' and len(line) > 1: |
---|
1123 | values = line.split(' ') |
---|
1124 | if values[0] == 'plots:': |
---|
1125 | plots = gen.stringList_dictKeysVals(values[1],cV=':') |
---|
1126 | elif values[0] == 'itotp:': |
---|
1127 | Nplots = int(values[1]) |
---|
1128 | |
---|
1129 | objf.close() |
---|
1130 | |
---|
1131 | return plots, Nplots |
---|
1132 | |
---|
1133 | def varnoper(vn, oper, opsurdict): |
---|
1134 | """ Function to provide name of the variable after operation as result of addition of the 'surnames' |
---|
1135 | vn= variable name |
---|
1136 | oper= '+' separated list of operations |
---|
1137 | opsurdict= dictionary with the surname of operations |
---|
1138 | >>> OpSurDict = {'mean': ['tmean', 'xmean', 'ymean']} |
---|
1139 | >>> varnoper('tas', 'pinterp+tmean+xmean', OpSurDict) |
---|
1140 | tasmeanmean |
---|
1141 | """ |
---|
1142 | fname = 'varnoper' |
---|
1143 | |
---|
1144 | newvn = vn |
---|
1145 | |
---|
1146 | for op in oper.split('+'): |
---|
1147 | surname = gen.dictionary_key_list(opsurdict,op) |
---|
1148 | if surname is not None: |
---|
1149 | newvn = newvn + surname |
---|
1150 | |
---|
1151 | return newvn |
---|
1152 | |
---|
1153 | def draw_plot(kplot, CFvplot, fplot, vplot, dplot, pplot, finame, tfig, kfig, mapval,\ |
---|
1154 | tvals, od, pyH, fscr, db): |
---|
1155 | """ Function to draw a plot |
---|
1156 | kplot= kind of plot |
---|
1157 | CFvplot= list of the CFnames of the variables |
---|
1158 | fplot= list with the files in the plot |
---|
1159 | vplot= list with the name of the variables in each file |
---|
1160 | dplot= list of the dimensions in each file |
---|
1161 | pplot= list of pictoric characteristics for each variable |
---|
1162 | finame= name of the figure |
---|
1163 | tfig= title of the figure |
---|
1164 | kfig= kind of the figure |
---|
1165 | mapval= value of the map |
---|
1166 | tvals= list with time values |
---|
1167 | tunits: units of time ('!' for spaces) |
---|
1168 | timekind: kind of time ticks in the plot |
---|
1169 | timefmt: format of time ticks in the plot |
---|
1170 | timelabel: label of time-axis in the plot |
---|
1171 | od= output directory |
---|
1172 | pyH= python HOME |
---|
1173 | fscr= whether figure should be done from scratch |
---|
1174 | """ |
---|
1175 | fname = 'draw_plot' |
---|
1176 | |
---|
1177 | if fscr: |
---|
1178 | sout = sub.call('rm ' + finame + ' >& /dev/null', shell=True) |
---|
1179 | |
---|
1180 | os.chdir(od) |
---|
1181 | |
---|
1182 | # CF variable-dimensions |
---|
1183 | CFvardims = {'lon':'lon', 'lat': 'lat', 'pres': 'pres', 'time': 'time'} |
---|
1184 | |
---|
1185 | # Tracking file with all the figures |
---|
1186 | trkobjf = open('all_figures.inf', 'a') |
---|
1187 | |
---|
1188 | # Time-characteristics |
---|
1189 | tunits = tvals[0] |
---|
1190 | timekind = tvals[1] |
---|
1191 | timefmt = tvals[2] |
---|
1192 | timelabel = tvals[3] |
---|
1193 | |
---|
1194 | if not os.path.isfile(finame): |
---|
1195 | if db: |
---|
1196 | print " drawing '" + finame + "' ..." |
---|
1197 | print ' CFvars:', CFvplot |
---|
1198 | print ' files:', fplot |
---|
1199 | print ' in file vars:', vplot |
---|
1200 | print ' dims:', dplot |
---|
1201 | print ' pictoric values:', pplot |
---|
1202 | print ' figure name:', finame |
---|
1203 | print ' title:', tfig.replace('!',' ') |
---|
1204 | print ' kind:', kfig |
---|
1205 | print ' map:', mapval |
---|
1206 | print ' time units:', tunits |
---|
1207 | |
---|
1208 | if kplot == 'diffmap2Dsfc': |
---|
1209 | print " " + fname + ": kind of plot '" + kplot + "' not ready !!" |
---|
1210 | quit(-1) |
---|
1211 | elif kplot == 'diffmap2Dz': |
---|
1212 | print " " + fname + ": kind of plot '" + kplot + "' not ready !!" |
---|
1213 | quit(-1) |
---|
1214 | elif kplot == 'map2Dsfc': |
---|
1215 | print " " + fname + ": kind of plot '" + kplot + "' not ready !!" |
---|
1216 | quit(-1) |
---|
1217 | elif kplot == 'map3D': |
---|
1218 | print " " + fname + ": kind of plot '" + kplot + "' not ready !!" |
---|
1219 | quit(-1) |
---|
1220 | elif kplot == 'shadcont2Dsfc': |
---|
1221 | figtit = tfig.replace('!','|') |
---|
1222 | shdstdn = CFvplot[0] |
---|
1223 | cntstdn = CFvplot[1] |
---|
1224 | figfs = ','.join(fplot) |
---|
1225 | |
---|
1226 | shad = pplot[0] |
---|
1227 | srange = str(shad[0]) + ',' + str(shad[1]) |
---|
1228 | cbar = shad[2] |
---|
1229 | cnt = pplot[1] |
---|
1230 | crange = str(cnt[0]) + ',' + str(cnt[1]) |
---|
1231 | cfmt = cnt[3] |
---|
1232 | cline = cnt[4] |
---|
1233 | |
---|
1234 | graphvals = ','.join(CFvplot) |
---|
1235 | graphvals = graphvals + ':lon|-1,lat|-1:lon|-1,lat|-1:lon:lat:' + cbar + \ |
---|
1236 | ':fixc,' + cline + ':' + cfmt + ':' + srange + ':' + crange + ',9:' + \ |
---|
1237 | figtit + ':' + kfig + ':None:' + mapval |
---|
1238 | |
---|
1239 | fvarS = ','.join(vplot) |
---|
1240 | |
---|
1241 | drwins = 'draw_2D_shad_cont' |
---|
1242 | plotins = "python " + pyH + "/drawing.py -f " + figfs + " -o " + drwins +\ |
---|
1243 | " -S '" + graphvals + "' -v " + fvarS |
---|
1244 | try: |
---|
1245 | with gen.Capturing() as output: |
---|
1246 | sout0 = sub.call(plotins, shell=True) |
---|
1247 | except: |
---|
1248 | print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')' |
---|
1249 | print sout0 |
---|
1250 | for sout in output: print sout |
---|
1251 | quit(-1) |
---|
1252 | |
---|
1253 | sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True) |
---|
1254 | |
---|
1255 | # keeping all figures |
---|
1256 | trkobjf.write('\n') |
---|
1257 | trkobjf.write("#" + tfig.replace('!',' ') + '\n') |
---|
1258 | trkobjf.write(plotins + '\n') |
---|
1259 | |
---|
1260 | elif kplot == 'shadconthovmsfc': |
---|
1261 | figtit = tfig.replace('!','|') |
---|
1262 | shdstdn = CFvplot[0] |
---|
1263 | cntstdn = CFvplot[1] |
---|
1264 | figfs = ','.join(fplot) |
---|
1265 | |
---|
1266 | shad = pplot[0] |
---|
1267 | srange = str(shad[0]) + ',' + str(shad[1]) |
---|
1268 | cbar = shad[2] |
---|
1269 | cnt = pplot[1] |
---|
1270 | crange = str(cnt[0]) + ',' + str(cnt[1]) |
---|
1271 | cfmt = cnt[3] |
---|
1272 | cline = cnt[4] |
---|
1273 | # It is assumed that if the space variable is 'lon': is desired a |
---|
1274 | # (lon, time) plot it it is 'lat': then (time, lat) plot |
---|
1275 | if gen.searchInlist(dplot,'lon'): |
---|
1276 | spacedim ='lon' |
---|
1277 | figmid = 'longitudinal|evolution|of' |
---|
1278 | figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' + \ |
---|
1279 | '|'.join(tfig.split('!')[2:]) |
---|
1280 | reverse= 'None' |
---|
1281 | dims= spacedim+'|-1,time|-1;'+spacedim+'|-1,time|-1;'+spacedim+';time' |
---|
1282 | else: |
---|
1283 | spacedim='lat' |
---|
1284 | figmid = 'meridional|evolution|of' |
---|
1285 | figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' + \ |
---|
1286 | '|'.join(tfig.split('!')[2:]) |
---|
1287 | reverse='None' |
---|
1288 | dims= spacedim+'|-1,time|-1;'+spacedim+'|-1,time|-1;time;'+spacedim |
---|
1289 | |
---|
1290 | graphvals = ','.join(CFvplot) |
---|
1291 | graphvals = graphvals + ';'+ dims +';'+ cbar + \ |
---|
1292 | ';fixsigc,' + cline + ';' + cfmt + ';' + srange + ';' + crange +',9;'+ \ |
---|
1293 | figtit + ';' + kfig + ';' + reverse + ';' + 'time|' + tunits + '|'+ \ |
---|
1294 | timekind + '|' + timefmt + '|' + timelabel |
---|
1295 | |
---|
1296 | fvarS = ','.join(vplot) |
---|
1297 | |
---|
1298 | drwins = 'draw_2D_shad_cont_time' |
---|
1299 | plotins = 'python ' + pyH + '/drawing.py -f ' + figfs +' -o ' + drwins + \ |
---|
1300 | " -S '" + graphvals + "' -v " + fvarS |
---|
1301 | |
---|
1302 | try: |
---|
1303 | with gen.Capturing() as output: |
---|
1304 | sout0 = sub.call(plotins, shell=True) |
---|
1305 | except: |
---|
1306 | print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')' |
---|
1307 | print sout0 |
---|
1308 | for sout in output: print sout |
---|
1309 | quit(-1) |
---|
1310 | |
---|
1311 | sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True) |
---|
1312 | |
---|
1313 | # keeping all figures |
---|
1314 | trkobjf.write('\n') |
---|
1315 | trkobjf.write("#" + tfig.replace('!',' ') + '\n') |
---|
1316 | trkobjf.write(plotins + '\n') |
---|
1317 | |
---|
1318 | elif 'shadcont2Dzsec': |
---|
1319 | figtit = tfig.replace('!','|') |
---|
1320 | shdstdn = CFvplot[0] |
---|
1321 | cntstdn = CFvplot[1] |
---|
1322 | figfs = ','.join(fplot) |
---|
1323 | |
---|
1324 | shad = pplot[0] |
---|
1325 | srange = str(shad[0]) + ',' + str(shad[1]) |
---|
1326 | cbar = shad[2] |
---|
1327 | cnt = pplot[1] |
---|
1328 | crange = str(cnt[0]) + ',' + str(cnt[1]) |
---|
1329 | cfmt = cnt[3] |
---|
1330 | cline = cnt[4] |
---|
1331 | |
---|
1332 | # It is assumed that if the space variable is 'lon': is desired a |
---|
1333 | # (lon, pres) plot it it is 'lat': then (pres, lat) plot |
---|
1334 | if gen.searchInlist(dplot, 'lon'): |
---|
1335 | spacedim='lon' |
---|
1336 | figmid = 'long.|vert.|cross|sec.|of' |
---|
1337 | figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' + \ |
---|
1338 | '|'.join(tfig.split('!')[2:]) |
---|
1339 | dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+spacedim+':pres' |
---|
1340 | else: |
---|
1341 | spacedim='lat' |
---|
1342 | figmid = 'mer.|vert.|cross|sec.|of' |
---|
1343 | figtit = '|'.join(tfig.split('!')[0:2]) + '|' + figmid + '|' + \ |
---|
1344 | '|'.join(tfig.split('!')[2:]) |
---|
1345 | # NOT WORKING? dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+'pres:'+spacedim |
---|
1346 | dims= spacedim+'|-1,pres|-1:'+spacedim+'|-1,pres|-1:'+spacedim+':pres' |
---|
1347 | |
---|
1348 | reverse = 'flip@y' |
---|
1349 | |
---|
1350 | graphvals = ','.join(CFvplot) |
---|
1351 | graphvals = graphvals + ':' + dims + ':' + cbar + ':fixc,' + cline + \ |
---|
1352 | ':' + cfmt + ':' + srange + ':' + crange + ',9:' + figtit + ':' + \ |
---|
1353 | kfig + ':' + reverse + ':None' |
---|
1354 | |
---|
1355 | fvarS = ','.join(vplot) |
---|
1356 | |
---|
1357 | drwins = 'draw_2D_shad_cont' |
---|
1358 | plotins = 'python ' + pyH + '/drawing.py -f ' + figfs + ' -o ' + drwins + \ |
---|
1359 | " -S '" + graphvals + "' -v " + fvarS |
---|
1360 | try: |
---|
1361 | with gen.Capturing() as output: |
---|
1362 | sout0 = sub.call(plotins, shell=True) |
---|
1363 | except: |
---|
1364 | print drwins + '(' + graphvals + ',' + figfs + ',' + fvarS + ')' |
---|
1365 | print sout0 |
---|
1366 | for sout in output: print sout |
---|
1367 | quit(-1) |
---|
1368 | |
---|
1369 | sout = sub.call('mv 2Dfields_shadow-contour.'+kfig+' '+finame, shell=True) |
---|
1370 | |
---|
1371 | # keeping all figures |
---|
1372 | trkobjf.write('\n') |
---|
1373 | trkobjf.write("#" + tfig.replace('!',' ') + '\n') |
---|
1374 | trkobjf.write(plotins + '\n') |
---|
1375 | |
---|
1376 | else: |
---|
1377 | print errmsg |
---|
1378 | print " " + fname + ": plot kind '" + kplot + "' not ready !!" |
---|
1379 | quit(-1) |
---|
1380 | |
---|
1381 | trkobjf.close() |
---|
1382 | |
---|
1383 | return |
---|
1384 | |
---|
1385 | def draw_plots(config, plots, mod, exp, odir, allvarcomp, figscr, debug): |
---|
1386 | """ Function to draw all plots |
---|
1387 | config= Configuration of the experiment |
---|
1388 | plots= dictionary with the plots |
---|
1389 | mod= model of the plots |
---|
1390 | exp= experiment of the plots |
---|
1391 | odir= output experiment folder |
---|
1392 | allvarcomp= dictionary with all the variables to compute and their information |
---|
1393 | figscr= whether figures should be done from the scratch or not |
---|
1394 | |
---|
1395 | * Plot as |
---|
1396 | {[kplot]} = [varn1]|[op1]#[varn2]|[op2]#[...[varnN]|[opN]], ... |
---|
1397 | [kplot] ___ |
---|
1398 | diffmap2Dsfc: 2D map of surface differences values of 1 variable |
---|
1399 | diffmap2Dz: 2D map of 3D differences values of 1 variable |
---|
1400 | map2Dsfc: 2D map of surface values of 1 variable |
---|
1401 | map3D: 2D map of 3D values of 1 variable |
---|
1402 | shadconthovmsfc: Hovmoeller diagrams of 2 variable at the surface in shadow and the other in contourn |
---|
1403 | shadcont2Dsfc: 2D map of shadow (1st variable) and countour (2nd variable) [stvar1]#[stvar2] |
---|
1404 | shadcont2Dzsec: 2D map of vertical section of 2 variables one in shadow and the other in contourn |
---|
1405 | [varn] |
---|
1406 | variable |
---|
1407 | [op] |
---|
1408 | '+' separated list of operations |
---|
1409 | in figures with more than 1 variable, use '#' to separate the [varn]|[op] |
---|
1410 | """ |
---|
1411 | fname = 'draw_plots' |
---|
1412 | |
---|
1413 | os.chdir(odir) |
---|
1414 | |
---|
1415 | # Dictionary with the operations with surnames for the operated variable |
---|
1416 | opersurnames = {} |
---|
1417 | opsur = gen.get_specdictionary_HMT(config, H='opsur_',M='',T='') |
---|
1418 | for opsr in opsur.keys(): |
---|
1419 | opn = opsr.split('_')[1] |
---|
1420 | vls = opsur[opsr].split(':') |
---|
1421 | opersurnames[opn] = vls |
---|
1422 | |
---|
1423 | # time values |
---|
1424 | # Units time for the plots |
---|
1425 | rd = config['CFreftime'] |
---|
1426 | tunits = config['CFunitstime'] + '!since!' + rd[0:4] + '-' + rd[4:6] + '-' + \ |
---|
1427 | rd[6:8] + '!' + rd[8:10] + ':' + rd[10:12] + ':' + rd[12:14] |
---|
1428 | # time ticks kind |
---|
1429 | tkind = config['timekind'] |
---|
1430 | # time ticks format |
---|
1431 | tfmt = config['timefmt'] |
---|
1432 | # time axis label |
---|
1433 | tlab = config['timelabel'] |
---|
1434 | timevals = [tunits, tkind, tfmt, tlab] |
---|
1435 | |
---|
1436 | # Dictionary of plot specificities |
---|
1437 | # [minval]: minimum value |
---|
1438 | # [maxval]: minimum value |
---|
1439 | # [colorbar]: name of the colorbar (from matplotlib) to use |
---|
1440 | # [cntformat]: format of the contour labels |
---|
1441 | # [colorcnt]: color for the countor lines |
---|
1442 | plotspecifics = {} |
---|
1443 | plotspecs = config['specificvarplot'].split(':') |
---|
1444 | for pltspc in plotspecs: |
---|
1445 | pltvls = pltspc.split('|') |
---|
1446 | vn = pltvls[0] |
---|
1447 | op = pltvls[1] |
---|
1448 | fn = pltvls[2] |
---|
1449 | plotspecifics[fn + '_' + vn + '_' + op] = pltvls[3:] |
---|
1450 | if debug: |
---|
1451 | print 'Specific values for plots _______' |
---|
1452 | gen.printing_dictionary(plotspecifics) |
---|
1453 | |
---|
1454 | # Kind of figures |
---|
1455 | kindfigure = config['kindfig'] |
---|
1456 | |
---|
1457 | # Map value |
---|
1458 | mapvalue = config['mapval'] |
---|
1459 | |
---|
1460 | # pythone scripts HOME |
---|
1461 | pyHOME = config['pyHOME'] |
---|
1462 | |
---|
1463 | # Title-text of operations |
---|
1464 | opexplained = {} |
---|
1465 | optits = config['titleoperations'].split(':') |
---|
1466 | for optit in optits: |
---|
1467 | opn = optit.split('|')[0] |
---|
1468 | opt = optit.split('|')[1] |
---|
1469 | opexplained[opn] = opt |
---|
1470 | if debug: |
---|
1471 | print 'Titles for operations _______' |
---|
1472 | gen.printing_dictionary(opexplained) |
---|
1473 | |
---|
1474 | for kplot in plots.keys(): |
---|
1475 | varsplt = plots[kplot] |
---|
1476 | for varplt in varsplt: |
---|
1477 | if debug: |
---|
1478 | print " printing '" + kplot + "' var ':" + varplt + "'..." |
---|
1479 | varops = varplt.split('#') |
---|
1480 | |
---|
1481 | # CF variables in plot |
---|
1482 | CFvarsplot = [] |
---|
1483 | # Files in plot |
---|
1484 | filesplot = [] |
---|
1485 | # Variables in plot within the files |
---|
1486 | varsplot = [] |
---|
1487 | # Dims in figure |
---|
1488 | dimsplot = [] |
---|
1489 | # pictoric values in figure |
---|
1490 | pictplot = [] |
---|
1491 | # Name of the figure |
---|
1492 | figname = '' |
---|
1493 | # Title of the figure |
---|
1494 | titfigure = '' |
---|
1495 | |
---|
1496 | ivp = 0 |
---|
1497 | for varop in varops: |
---|
1498 | vn = varop.split('|')[0] |
---|
1499 | op = varop.split('|')[1] |
---|
1500 | |
---|
1501 | # CF variables in plot |
---|
1502 | CFvarsplot.append(vn) |
---|
1503 | |
---|
1504 | vnopS = vn + '_' + op |
---|
1505 | if not allvarcomp.has_key(vnopS): |
---|
1506 | print errormsg |
---|
1507 | print ' ' + fname + ": no entry for variable-operation '" + \ |
---|
1508 | vnopS + "' !!" |
---|
1509 | vopvals = allvarcomp[vnopS] |
---|
1510 | headf = vopvals[0] |
---|
1511 | globalP = vopvals[3] |
---|
1512 | gP = pinterpS(globalP) |
---|
1513 | |
---|
1514 | filen = vn + '_' + headf + gP + '_' + op.replace('+','_') + '.nc' |
---|
1515 | filesplot.append(filen) |
---|
1516 | # Do we have processed the given variable? |
---|
1517 | if not os.path.isfile(filen): |
---|
1518 | print warnmsg |
---|
1519 | print " " + fname + ": there is no file for variable '" + varop \ |
---|
1520 | + "' skiping it !!" |
---|
1521 | break |
---|
1522 | |
---|
1523 | # Name of the variable inside the file |
---|
1524 | vnsur = varnoper(vn, op, opersurnames) |
---|
1525 | varsplot.append(vnsur) |
---|
1526 | |
---|
1527 | # Dimensions in file |
---|
1528 | try: |
---|
1529 | with gen.Capturing() as output: |
---|
1530 | dims = ncvar.idims(filen) |
---|
1531 | except: |
---|
1532 | print 'ncvar.idims('+filen+')' |
---|
1533 | for sout in output: print sout |
---|
1534 | quit(-1) |
---|
1535 | |
---|
1536 | dimsplot.append(dims) |
---|
1537 | |
---|
1538 | # pictoric values for the figure |
---|
1539 | Sfivaop = kplot + '_' + vn + '_' + op |
---|
1540 | if plotspecifics.has_key(Sfivaop): |
---|
1541 | pictvals = plotspecifics[Sfivaop] |
---|
1542 | else: |
---|
1543 | Vvals = gen.variables_values(vn) |
---|
1544 | pictvals = [Vvals[2], Vvals[3], Vvals[6], '%g', 'black'] |
---|
1545 | |
---|
1546 | pictplot.append(pictvals) |
---|
1547 | |
---|
1548 | # Header of the name of the figure |
---|
1549 | if ivp == 0: |
---|
1550 | figname = kplot +'_'+ vn + '_' + headf + '_' + op.replace('+','-') |
---|
1551 | else: |
---|
1552 | figname = figname + '-'+vn+'_'+headf+'_'+op.replace('+','-') |
---|
1553 | |
---|
1554 | # Title of the figure: |
---|
1555 | if ivp == 0: |
---|
1556 | titfigure = mod + '!' + exp + "!'" + vn + "'" |
---|
1557 | else: |
---|
1558 | titfigure = titfigure + "!&!'" + vn + "'" |
---|
1559 | opvals = op.split('+') |
---|
1560 | for op1 in opvals: |
---|
1561 | if not opexplained.has_key(op1): |
---|
1562 | print errormsg |
---|
1563 | print ' '+fname+": no explanation for operation '"+op1+"' !!" |
---|
1564 | print ' provided:', opexplained.keys() |
---|
1565 | quit(-1) |
---|
1566 | if op1 == opvals[0]: |
---|
1567 | titfigure = titfigure + '$_{[' + opexplained[op1] |
---|
1568 | if len(opvals) == 1: titfigure = titfigure + ']}$' |
---|
1569 | elif op1 == opvals[len(opvals)-1]: |
---|
1570 | titfigure = titfigure + '\!' + opexplained[op1] + ']}$' |
---|
1571 | else: |
---|
1572 | titfigure = titfigure + '\!' + opexplained[op1] |
---|
1573 | |
---|
1574 | ivp = ivp + 1 |
---|
1575 | # End of variable-operation |
---|
1576 | figname = figname + '.' + kindfigure |
---|
1577 | |
---|
1578 | draw_plot(kplot, CFvarsplot, filesplot, varsplot, dimsplot, pictplot, \ |
---|
1579 | figname, titfigure, kindfigure, mapvalue, timevals, odir, pyHOME, \ |
---|
1580 | figscr, debug) |
---|
1581 | |
---|
1582 | # End of variables-operations |
---|
1583 | |
---|
1584 | # End of kind of plots |
---|
1585 | |
---|
1586 | return |
---|
1587 | |
---|
1588 | # Files with information about the configuration of the script |
---|
1589 | inffiles = ['varcompute.inf', 'all_computevars.inf', 'all_statsvars.inf'] |
---|
1590 | |
---|
1591 | ####### ####### |
---|
1592 | ## MAIN |
---|
1593 | ####### |
---|
1594 | |
---|
1595 | # Getting configuration from external ASCII file 'model_graphics.dat' |
---|
1596 | cnf = gen.get_configuration('model_graphics.dat', False) |
---|
1597 | |
---|
1598 | # scratches |
---|
1599 | scratch, filescratch, figscratch, addfiles, addfigures, dbg = scratches(cnf) |
---|
1600 | |
---|
1601 | # Getting models |
---|
1602 | mods = cnf['models'].split(':') |
---|
1603 | |
---|
1604 | # Models loop |
---|
1605 | ## |
---|
1606 | for mod in mods: |
---|
1607 | print mod |
---|
1608 | if scratch: |
---|
1609 | sout = sub.call('rm -rf '+cnf['ofold']+'/'+mod+' > /dev/null', shell=True) |
---|
1610 | |
---|
1611 | # Get experiments and headers of model |
---|
1612 | exps, fheaders = exp_headers(mod,cnf) |
---|
1613 | |
---|
1614 | # Characteristics of the model |
---|
1615 | Modinf = ncvar.model_characteristics(mod,'None','False') |
---|
1616 | dnx = Modinf.dimxn |
---|
1617 | dny = Modinf.dimyn |
---|
1618 | dnz = Modinf.dimzn |
---|
1619 | dnt = Modinf.dimtn |
---|
1620 | vdnx = Modinf.vardxn |
---|
1621 | vdny = Modinf.vardyn |
---|
1622 | vdnz = Modinf.vardzn |
---|
1623 | vdnt = Modinf.vardtn |
---|
1624 | |
---|
1625 | if dbg: |
---|
1626 | print ' model characteristics _______' |
---|
1627 | print " dims:", dnx, dny, dnz, dnt |
---|
1628 | print " var dims:", vdnx, vdny, vdnz, vdnt |
---|
1629 | |
---|
1630 | moddims = dnx + ',' + dny + ',' + dnz + ',' + dnt |
---|
1631 | modvdims = vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt |
---|
1632 | |
---|
1633 | # Experiments loop |
---|
1634 | ## |
---|
1635 | for exp in exps: |
---|
1636 | print ' ' + exp + '...' |
---|
1637 | |
---|
1638 | # input folder |
---|
1639 | iwdir = cnf['ifold'] + '/' + mod + '/' + exp |
---|
1640 | |
---|
1641 | # Does input folder exist? |
---|
1642 | if not os.path.isdir(iwdir): |
---|
1643 | print errmsg |
---|
1644 | print " " + main + ": folder '" + iwdir + "' does not exist !!" |
---|
1645 | quit(-1) |
---|
1646 | |
---|
1647 | owdir = cnf['ofold'] + '/' + mod + '/' + exp |
---|
1648 | sout = sub.call('mkdir -p ' + owdir, shell=True) |
---|
1649 | |
---|
1650 | # Need to pass to analyze all the data? |
---|
1651 | if filescratch: |
---|
1652 | for inff in inffiles: |
---|
1653 | if dbg: print " removing information file '" + inff + "' ..." |
---|
1654 | ins = 'rm ' + owdir + '/' + inff + ' >& /dev/null' |
---|
1655 | sout = sub.call(ins, shell=True) |
---|
1656 | |
---|
1657 | objf = open(owdir+'/all_computevars.inf','w') |
---|
1658 | objf.write("## Computation of variables \n") |
---|
1659 | objf.close() |
---|
1660 | objf = open(owdir+'/all_statsvars.inf','w') |
---|
1661 | objf.write("## Computation of statistics \n") |
---|
1662 | objf.close() |
---|
1663 | |
---|
1664 | varcompf = owdir + '/varcompute.inf' |
---|
1665 | if addfiles: |
---|
1666 | sub.call('rm ' + varcompf +' >& /dev/null', shell=True) |
---|
1667 | |
---|
1668 | if not os.path.isfile(varcompf): |
---|
1669 | # Does input folder has header files? |
---|
1670 | ih=1 |
---|
1671 | # Dictionary with the list of files for each headers |
---|
1672 | files = {} |
---|
1673 | # Dictionary with a file fromor each headers |
---|
1674 | testfiles = {} |
---|
1675 | |
---|
1676 | for fh in fheaders: |
---|
1677 | if filescratch: |
---|
1678 | ins = 'rm '+ owdir+'/*_' + fh + '*.nc >& /dev/null' |
---|
1679 | sout = sub.call(ins, shell=True) |
---|
1680 | files1h = gen.files_folder(iwdir,fh) |
---|
1681 | if len(files1h) < 1: |
---|
1682 | print errmsg |
---|
1683 | print ' ' + main + ": folder '" + iwdir + "' does not " + \ |
---|
1684 | "contain files '" + fh + "*' !!" |
---|
1685 | quit(-1) |
---|
1686 | files[fh] = files1h |
---|
1687 | testfiles[fh] = files1h[0] |
---|
1688 | |
---|
1689 | if dbg: |
---|
1690 | print ' Dictionary of files _______' |
---|
1691 | gen.printing_dictionary(files) |
---|
1692 | |
---|
1693 | print ' Dictionary of test-files _______' |
---|
1694 | gen.printing_dictionary(testfiles) |
---|
1695 | |
---|
1696 | allcompvar, Nvar = compvars_listconstruct(cnf, Modinf, files, testfiles, \ |
---|
1697 | iwdir, owdir, dbg) |
---|
1698 | else: |
---|
1699 | print warnmsg |
---|
1700 | print ' ' + main + ": getting variables to compute already from file !!" |
---|
1701 | files, testfiles, allcompvar, Nvar = read_varcomp_file(varcompf) |
---|
1702 | |
---|
1703 | # End of avoiding to repeat all the experiment search |
---|
1704 | |
---|
1705 | print " For experiment '"+exp+"' is required to compute:", Nvar, "variables" |
---|
1706 | |
---|
1707 | if dbg: |
---|
1708 | print 'Variables to compute _______' |
---|
1709 | gen.printing_dictionary(allcompvar) |
---|
1710 | |
---|
1711 | ### |
---|
1712 | # Computing variables |
---|
1713 | ### |
---|
1714 | print " Computing variables ..." |
---|
1715 | compute_vars(cnf, Modinf, iwdir, owdir, files, allcompvar, filescratch, dbg) |
---|
1716 | |
---|
1717 | ## |
---|
1718 | # Figures of variables direct from files |
---|
1719 | ## |
---|
1720 | print " " + main + ": Plotting direct figures ..." |
---|
1721 | dirfigf = owdir + '/directplotsdraw.inf' |
---|
1722 | if figscratch: |
---|
1723 | sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True) |
---|
1724 | |
---|
1725 | objf = open(owdir+'/all_figures.inf','w') |
---|
1726 | objf.write("## Drawing of all figures \n") |
---|
1727 | objf.close() |
---|
1728 | |
---|
1729 | if addfigures: |
---|
1730 | sout = sub.call('rm ' + dirfigf + ' >& /dev/null', shell=True) |
---|
1731 | |
---|
1732 | if not os.path.isfile(dirfigf): |
---|
1733 | listplots, Nplt = plots_listconstruct(cnf, owdir, dbg) |
---|
1734 | else: |
---|
1735 | print warnmsg |
---|
1736 | print ' ' + main + ": getting plots to draw already from file !!" |
---|
1737 | listplots, Nplt = read_plot_file(dirfigf) |
---|
1738 | |
---|
1739 | # End of avoiding to repeat all the plots search |
---|
1740 | |
---|
1741 | print " For experiment '"+exp+"' is required to plot:", Nplt, "plots" |
---|
1742 | |
---|
1743 | if dbg: |
---|
1744 | print 'Plots to draw _______' |
---|
1745 | gen.printing_dictionary(listplots) |
---|
1746 | |
---|
1747 | draw_plots(cnf, listplots, mod, exp, owdir, allcompvar, figscratch, dbg) |
---|
1748 | |
---|
1749 | quit() |
---|
1750 | # end of experiments loop |
---|
1751 | |
---|
1752 | # end of mods loop |
---|
1753 | |
---|