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 time as tim |
---|
10 | # To avoid errors like: '/bin/sh: 1: Syntax error: Bad fd number' |
---|
11 | # Make sure that /bin/sh directs to bash and not to dash: |
---|
12 | # http://stackoverflow.com/questions/15809060/sh-syntax-error-bad-fd-number |
---|
13 | import subprocess as sub |
---|
14 | import os |
---|
15 | |
---|
16 | main = 'model_graphics.py' |
---|
17 | |
---|
18 | errmsg = ncvar.errormsg |
---|
19 | warnmsg = ncvar.warnmsg |
---|
20 | |
---|
21 | def scratches(config): |
---|
22 | """ Function to set-up if it is needed to start from the scratch |
---|
23 | config: dictionary with the configuration |
---|
24 | """ |
---|
25 | fname = 'scratches' |
---|
26 | |
---|
27 | if config['scratch'] == 'true': |
---|
28 | scr = True |
---|
29 | print warnmsg |
---|
30 | print " " + main + ": starting from the SCRATCH !!" |
---|
31 | print " 10 seconds left!!" |
---|
32 | filescr = True |
---|
33 | figscr = True |
---|
34 | # tim.sleep(10) |
---|
35 | else: |
---|
36 | scr = False |
---|
37 | if config['filescratch'] == 'true': |
---|
38 | filescr = True |
---|
39 | print warnmsg |
---|
40 | print " " + main + ": files starting from the SCRATCH !!" |
---|
41 | print " 5 seconds left!!" |
---|
42 | tim.sleep(5) |
---|
43 | else: |
---|
44 | filescr = False |
---|
45 | |
---|
46 | if config['figscratch'] == 'true': |
---|
47 | figscr = True |
---|
48 | print warnmsg |
---|
49 | print " " + main + ": figures starting from the SCRATCH !!" |
---|
50 | print " 5 seconds left!!" |
---|
51 | tim.sleep(5) |
---|
52 | else: |
---|
53 | figscr = False |
---|
54 | |
---|
55 | if config['addfiles'] == 'true': |
---|
56 | addfils = True |
---|
57 | else: |
---|
58 | addfils = False |
---|
59 | |
---|
60 | if config['addfigures'] == 'true': |
---|
61 | addfigs = True |
---|
62 | else: |
---|
63 | addfigs = False |
---|
64 | |
---|
65 | if config['debug'] == 'true': |
---|
66 | debug = True |
---|
67 | else: |
---|
68 | debug = False |
---|
69 | |
---|
70 | return scr, filescr, figscr, addfils, addfigs, debug |
---|
71 | |
---|
72 | def exp_headers(mod,config): |
---|
73 | """ Function to provide the headers and the experiments of a given model |
---|
74 | model= model |
---|
75 | config= configuration of the experiment |
---|
76 | """ |
---|
77 | fname = 'exp_headers' |
---|
78 | |
---|
79 | # No CASE in python! |
---|
80 | # case ${mod} in ... |
---|
81 | if mod == 'WRF': |
---|
82 | expers = config['WRFexps'].split(':') |
---|
83 | fhs = config['WRFheaders'].split(':') |
---|
84 | elif mod == 'LMDZ': |
---|
85 | expers = config['LMDZexps'].split(':') |
---|
86 | fhs = config['LMDZheaders'].split(':') |
---|
87 | elif mod == 'WRF_LMDZ': |
---|
88 | expers = config['WRF_LMDZexps'].split(':') |
---|
89 | fhs = config['WRF_LMDZheaders'].split(':') |
---|
90 | else: |
---|
91 | print errmsg |
---|
92 | print " " + fname + ": model '" + mod + "' not ready!!" |
---|
93 | quit(-1) |
---|
94 | |
---|
95 | return expers, fhs |
---|
96 | |
---|
97 | # Files with information about the configuration of the script |
---|
98 | inffiles = ['varcompute.inf', 'all_computevars.inf', 'all_statsvars.inf'] |
---|
99 | |
---|
100 | ####### ####### |
---|
101 | ## MAIN |
---|
102 | ####### |
---|
103 | |
---|
104 | # Getting configuration from external ASCII file 'model_graphics.dat' |
---|
105 | cnf = gen.get_configuration('model_graphics.dat', False) |
---|
106 | |
---|
107 | # scratches |
---|
108 | scratch, filescratch, figscratch, addfiles, addfigures, dbg = scratches(cnf) |
---|
109 | |
---|
110 | # Getting models |
---|
111 | mods = cnf['models'].split(':') |
---|
112 | |
---|
113 | # Getting kinds of variables |
---|
114 | varks = cnf['varkinds'].split(':') |
---|
115 | |
---|
116 | # Models loop |
---|
117 | ## |
---|
118 | for mod in mods: |
---|
119 | print mod |
---|
120 | # Get experiments and headers of model |
---|
121 | exps, fheaders = exp_headers(mod,cnf) |
---|
122 | |
---|
123 | # Characteristics of the model |
---|
124 | modinf = ncvar.model_characteristics(mod,'None','False') |
---|
125 | dnx = modinf.dimxn |
---|
126 | dny = modinf.dimyn |
---|
127 | dnz = modinf.dimzn |
---|
128 | dnt = modinf.dimtn |
---|
129 | vdnx = modinf.vardxn |
---|
130 | vdny = modinf.vardyn |
---|
131 | vdnz = modinf.vardzn |
---|
132 | vdnt = modinf.vardtn |
---|
133 | |
---|
134 | if dbg: |
---|
135 | print ' model characteristics _______' |
---|
136 | print " dims:", dnx, dny, dnz, dnt |
---|
137 | print " var dims:", vdnx, vdny, vdnz, vdnt |
---|
138 | |
---|
139 | moddims = dnx + ',' + dny + ',' + dnz + ',' + dnt |
---|
140 | modvdims = vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt |
---|
141 | |
---|
142 | for exp in exps: |
---|
143 | print ' ' + exp + '...' |
---|
144 | |
---|
145 | # input folder |
---|
146 | iwdir = cnf['ifold'] + '/' + mod + '/' + exp |
---|
147 | |
---|
148 | # Does input folder exist? |
---|
149 | if not os.path.isdir(iwdir): |
---|
150 | print errmsg |
---|
151 | print " " + main + ": folder '" + iwdir + "' does not exist !!" |
---|
152 | quit(-1) |
---|
153 | |
---|
154 | owdir = cnf['ofold'] + '/' + mod + '/' + exp |
---|
155 | sout = sub.call('mkdir -p ' + owdir, shell=True) |
---|
156 | |
---|
157 | # Need to pass to analyze all the data? |
---|
158 | if filescratch: |
---|
159 | for inff in inffiles: |
---|
160 | if dbg: print " removing information file '" + inff + "' ..." |
---|
161 | ins = 'rm ' + owdir + '/' + inff + ' >& /dev/null' |
---|
162 | sout = sub.call(ins, shell=True) |
---|
163 | |
---|
164 | objf = open(owdir+'/all_computevars.inf','w') |
---|
165 | objf.write("## Computation of variables \n") |
---|
166 | objf.close() |
---|
167 | objf = open(owdir+'/all_statsvars.inf','w') |
---|
168 | objf.write("## Computation of statistics \n") |
---|
169 | objf.close() |
---|
170 | |
---|
171 | if addfiles: |
---|
172 | sub.call('rm ' + owdir +'/varcompute.inf >& /dev/null', shell=True) |
---|
173 | |
---|
174 | varcompf = owdir + '/varcompute.inf' |
---|
175 | if not os.path.isfile(varcompf): |
---|
176 | # Does input folder has header files? |
---|
177 | ih=1 |
---|
178 | # Dictionary with the list of files for each headers |
---|
179 | files = {} |
---|
180 | # Dictionary with a file fromor each headers |
---|
181 | testfiles = {} |
---|
182 | |
---|
183 | for fh in fheaders: |
---|
184 | if filescratch: |
---|
185 | ins = 'rm '+ owdir+'/*_' + fh + '*.nc >& /dev/null' |
---|
186 | sout = sub.call(ins, shell=True) |
---|
187 | files1h = gen.files_folder(iwdir,fh) |
---|
188 | if len(files1h) < 1: |
---|
189 | print errmsg |
---|
190 | print ' ' + main + ": folder '" + iwdir + "' does not " + \ |
---|
191 | "contain files '" + fh + "*' !!" |
---|
192 | quit(-1) |
---|
193 | files[fh] = files1h |
---|
194 | testfiles[fh] = files1h[0] |
---|
195 | |
---|
196 | if dbg: |
---|
197 | print ' Dictionary of files _______' |
---|
198 | gen.printing_dictionary(files) |
---|
199 | |
---|
200 | print ' Dictionary of test-files _______' |
---|
201 | gen.printing_dictionary(testfiles) |
---|
202 | |
---|
203 | |
---|
204 | # end of experiments loop |
---|
205 | # end of mods loop |
---|
206 | |
---|