forked from jservonnat/C-ESM-EP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_C-ESM-EP.py
executable file
·372 lines (302 loc) · 15.1 KB
/
main_C-ESM-EP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# ----------------------------------------------------------------------------------- #
# -- Header of the atlas.py ; import the useful python modules (CliMAF, NEMO_atlas, -- #
# -- and potentially your own stuff stored in my_plot_functions.py in the current -- #
# -- working directory. -- #
# ------------------------------------------------------------------------------------ #
from climaf.api import *
from climaf.html import *
from climaf import cachedir
from CM_atlas import *
from climaf.site_settings import onCiclad, atTGCC, atCNRM
from climaf.classes import Climaf_Error
from getpass import getuser
from climaf import __path__ as cpath
import json
import os
import copy
import subprocess
import shlex
from optparse import OptionParser
from locations import path_to_cesmep_output_rootdir, path_to_cesmep_output_rootdir_on_web_server, \
root_url_to_cesmep_outputs
# -----------------------------------------------------------------------------------
# -- PART 1: Get the instructions from:
# -- - the default values
# -- - the parameter file (priority 2)
# -- - the command line (priority 1)
# -----------------------------------------------------------------------------------
# -- Description of the atlas
# -----------------------------------------------------------------------------------
desc = "\n\nCliMAF Earth System Model Evaluation Platform: comparing multiple simulations against references " \
"(https://github.com/jservonnat/C-ESM-EP/wiki)"
# -- Get the parameters that the atlas takes as arguments
# -----------------------------------------------------------------------------------
parser = OptionParser(desc)
parser.set_usage("%%prog [-h]\n%s" % desc)
parser.add_option("--index_name",
help="Name of the html file (atlas)",
action="store", default=None)
parser.add_option("--comparison",
help="Name of the comparison",
action="store", default=None)
parser.add_option("--component",
help="Name of the component - set of diagnostics",
action="store", default=None)
parser.add_option("--cesmep_frontpage",
help="URL to C-ESM-EP frontpage",
action="store", default=None)
(opts, args) = parser.parse_args()
# -- Define the path to the main C-ESM-EP directory:
# -----------------------------------------------------------------------------------
#rootmainpath = str.split(os.getcwd(), 'C-ESM-EP')[0] + 'C-ESM-EP/'
#if os.path.isfile(rootmainpath+'main_C-ESM-EP.py'):
# main_cesmep_path = rootmainpath
#if os.path.isfile(rootmainpath+str.split(str.split(os.getcwd(), 'C-ESM-EP')[1], '/')[1] + '/main_C-ESM-EP.py'):
# main_cesmep_path = rootmainpath+str.split(str.split(os.getcwd(), 'C-ESM-EP')[1], '/')[1] + '/'
rootmainpath = os.getcwd()
print 'rootmainpath = ',rootmainpath
if os.path.isfile(rootmainpath+'main_C-ESM-EP.py'):
main_cesmep_path = rootmainpath
if os.path.isfile(rootmainpath+'/../main_C-ESM-EP.py'):
main_cesmep_path = rootmainpath+'/../'
if os.path.isfile(rootmainpath+'/../../main_C-ESM-EP.py'):
main_cesmep_path = rootmainpath+'/../../'
# -- Get the default parameters from default_atlas_settings.py -> Priority = default
# -----------------------------------------------------------------------------------
default_file = '/share/default/default_atlas_settings.py'
execfile(main_cesmep_path+default_file)
# -- Get the values for comparison and component
# -----------------------------------------------------------------------------------
component = opts.component
comparison = opts.comparison
index_name = opts.index_name
cesmep_frontpage = opts.cesmep_frontpage
# -- Build the paths to:
# -----------------------------------------------------------------------------------
# - the datasets_setup.py
# - the parameter file
# - the diagnostics file
datasets_setup = main_cesmep_path + comparison + '/datasets_setup.py'
param_file = main_cesmep_path + comparison + '/' + component + '/params_' + component + '.py'
diagnostics_file = str.replace(param_file, 'params_', 'diagnostics_')
if not os.path.isfile(diagnostics_file):
diagnostics_file = main_cesmep_path + 'share/cesmep_diagnostics/diagnostics_' + component + '.py'
print '-- Use diagnostics_file =', diagnostics_file
# -- If we specify a datasets_setup from the command line, we use 'models' from this file
# -----------------------------------------------------------------------------------
datasets_setup_available_period_set_file = str.replace(datasets_setup, '.py', '_available_period_set.py')
if os.path.isfile(datasets_setup_available_period_set_file):
use_available_period_set = True
execfile(datasets_setup_available_period_set_file)
# Create Wmodels_ts and Wmodels_clim from Wmodels
Wmodels_clim = copy.deepcopy(Wmodels)
for item in Wmodels_clim:
clim_period_args = copy.deepcopy(item['clim_period'])
item.pop('clim_period')
item.pop('ts_period')
item.update(clim_period_args)
#
Wmodels_ts = copy.deepcopy(Wmodels)
for item in Wmodels_ts:
ts_period_args = copy.deepcopy(item['ts_period'])
item.pop('ts_period')
item.pop('clim_period')
item.update(ts_period_args)
else:
execfile(datasets_setup)
use_available_period_set = False
# -- Get the username ; if we work on fabric (ciclad), we get the manually attributed username
# -----------------------------------------------------------------------------------
username = getuser()
user_login = (str.split(os.getcwd(), '/')[4] if username == 'fabric' else username)
# -- Get the site specifications:
# -----------------------------------------------------------------------------------
# -> path_to_cesmep_output_rootdir = where (directory) we physically store the results of the C-ESM-EP
# (root directory of the whole atlas tree)
# -> path_to_cesmep_output_rootdir_on_web_server = path to the results on the web server
# -> root_url_to_cesmep_outputs = URL of the root directory of the C-ESM-EP atlas (need to add 'C-ESM-EP',
# comparison and component to reach the atlas)
# -- Location of the directory where we will store the results of the atlas
atlas_dir = path_to_cesmep_output_rootdir + '/C-ESM-EP/' + comparison + '_' + user_login + '/' + component
# -- Url of the atlas (without the html file)
atlas_url = str.replace(atlas_dir, path_to_cesmep_output_rootdir, root_url_to_cesmep_outputs)
# -- We create the atlas directory if it doesn't exist, or remove the figures
# -----------------------------------------------------------------------------------
if atCNRM or atTGCC or onCiclad:
if not os.path.isdir(atlas_dir):
os.makedirs(atlas_dir)
else:
os.system('rm -f '+atlas_dir+'/*.png')
# -> Specif TGCC and CNRM:
# - Copy the empty.png image in the cache
# -----------------------------------------------------------------------------------
# if atTGCC or atCNRM :
# if not os.path.isdir(cachedir):
# os.makedirs(cachedir)
# if not os.path.isfile(cachedir+'/Empty.png'):
# cmd = 'cp '+cpath+'/plot/Empty.png '+cachedir
# print cmd
# os.system(cmd)
# -- Specify the directory where we will output the atlas html file and the figures
# -----------------------------------------------------------------------------------
alternative_dir = {'dirname': atlas_dir}
# -- Set the verbosity of CliMAF (minimum is 'critical', maximum is 'debug',
# -- intermediate -> 'warning')
# -----------------------------------------------------------------------------------
clog(verbose)
# -- Print the models
# -----------------------------------------------------------------------------------
print '==> ----------------------------------- #'
print '==> Working on models:'
print '==> ----------------------------------- #'
print ' '
for model in models:
for key in model:
print ' '+key+' = ', model[key]
print ' --'
print ' --'
print '==> ----------------------------------- #'
print '==> Against reference:'
print '==> ----------------------------------- #'
print ' '
if reference=='default':
print ' reference = default'
print ' --> you are using the catalog of pre-defined references (in share/cesmep_modules/reference/reference.py)'
print ' --> you can setup you own references in custom_obs_dict.py for each variable independently'
else:
for key in reference:
print ' '+key+' = ', reference[key]
print ' --'
print ' --'
# -----------------------------------------------------------------------------------
# -- End PART 1
# --
# -----------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------
# -- PART 2: Build the html
# -- - the header
# -- - and the sections of diagnostics:
# -- * Atlas Explorer
# -- * Atmosphere
# -- * Blue Ocean - physics
# -- * White Ocean - Sea Ice
# -- * Green Ocean - Biogeochemistry
# -- * Land Surfaces
# -- ...
# -----------------------------------------------------------------------------------
# -- Setup a css style file
# ---------------------------------------------------------------------------- >
style_file = main_cesmep_path+'share/fp_template/cesmep_atlas_style_css'
# -- Head title of the atlas -> default value should be override from diagnostics_${comp}.py
# ---------------------------------------------------------------------------- >
atlas_head_title = component
# -- Get the parameters from the param file -> Priority = 2
# -----------------------------------------------------------------------------------
if os.path.isfile(param_file):
execfile(param_file)
# -- Add the season to the html file name
# -----------------------------------------------------------------------------------
if not index_name:
index_name = 'atlas_'+component+'_'+comparison+'.html'
# -- Automatically zoom on the plot when the mouse is on it
# ---------------------------------------------------------------------------- >
hover = False
# -- Add the compareCompanion (P. Brockmann)
# --> Works as a 'basket' on the html page to select some figures and
# --> display only this selection on a new page
# ---------------------------------------------------------------------------- >
add_compareCompanion = True
# -> Clean the cache if specified by the user
# -----------------------------------------------------------------------------------
if clean_cache and not clean_cache == 'False':
print '!!! Fully clean the cache before starting the atlas !!!'
craz()
print '!!! Cache cleaned !!!'
# -- Execute the diagnostic file ${comparison}/${component}/diagnostics_${component}.py
# -----------------------------------------------------------------------------------
execfile(diagnostics_file)
# -----------------------------------------------------------------------------------
# -- End PART 2
# --
# -----------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------
# -- PART 3: Finalize the index by replacing the paths to the cache for the paths
# -- to the alternate directory
# -----------------------------------------------------------------------------------
# -- ONLY FOR DEVELOPERS - users shouldn't need to modify this section
# -- Contact [email protected]
# -----------------------------------------------------------------------------------
# -- Here we can work either on Ciclad or at TGCC
# -- On Ciclad we feed directly the cache located on the thredds server
# -- and point at this web address.
# -- At TGCC we need to:
# -- - feed the cache on $SCRATCHDIR (set in your environment) and make a hard link in
# -- a target directory (alt_dir_name, based on the current working directory and
# -- subdir)
# -- - at the end of the atlas, we copy the target directory (alt_dir_name) on the
# -- $WORKDIR (work_alt_dir_name) before copying it on the thredds with thredds_cp
# -- - Eventually, we thredds_cp the directory work_alt_dir_name
# -- on the /ccc/work/cont003/thredds/public space
# -- Adding the compareCompanion JavaScript functionality to make a selection of images
if add_compareCompanion:
print 'Add compareCompanion'
index += compareCompanion()
# -- End the index
index += trailer()
# -- Add link to main frontpage
climaf_doc_url = 'https://climaf.readthedocs.io/en/master/'
# -- Replace url to CliMAF documentation with url to C-ESM-EP frontpage
index = str.replace(index, climaf_doc_url, cesmep_frontpage)
# -- Replace CliMAF documentation with C-ESM-EP frontpage of comparison COMPARISON
index = str.replace(index, 'CliMAF documentation', 'Back to C-ESM-EP frontpage of comparison: '+comparison)
# -- Write the atlas html file
outfile = atlas_dir + "/" + index_name
print 'outfile = ', outfile
with open(outfile, "w") as filout:
filout.write(index)
#
if atTGCC:
# -- Ecriture du fichier html dans le repertoire sur scratch
path_to_comparison_outdir_workdir_tgcc = atlas_dir.replace('scratch', 'work')
if not os.path.isdir(path_to_comparison_outdir_workdir_tgcc):
os.makedirs(path_to_comparison_outdir_workdir_tgcc)
else:
print 'rm -rf '+path_to_comparison_outdir_workdir_tgcc+'/*'
os.system('rm -rf '+path_to_comparison_outdir_workdir_tgcc+'/*')
cmd1 = 'cp -r '+atlas_dir+'* '+path_to_comparison_outdir_workdir_tgcc
print cmd1
os.system(cmd1)
#
# -- thredds_cp du repertoire copie sur le work
path_to_comparison_on_web_server = path_to_cesmep_output_rootdir_on_web_server + '/C-ESM-EP/' + comparison + '_' + \
user_login
cmd12 = 'rm -rf '+path_to_comparison_on_web_server+'/'+component
print cmd12
os.system(cmd12)
cmd2 = 'thredds_cp '+path_to_comparison_outdir_workdir_tgcc+' '+path_to_comparison_on_web_server+'/'
print cmd2
os.system(cmd2)
print(' -- ')
print(' -- ')
print(' -- ')
print('Index available at : ' + outfile.replace(path_to_cesmep_output_rootdir, root_url_to_cesmep_outputs))
if atTGCC:
print("The atlas is ready as ", str.replace(index_name, atlas_dir, path_to_comparison_outdir_workdir_tgcc))
else:
print("The atlas is ready as ", index_name)
# -----------------------------------------------------------------------------------
# -- End PART 3
# --
# -----------------------------------------------------------------------------------
# -- Systematically clean the cache
if isinstance(routine_cache_cleaning, list) or isinstance(routine_cache_cleaning, dict):
if not isinstance(routine_cache_cleaning, list):
routine_cache_cleaning = [routine_cache_cleaning]
for clean_args in routine_cache_cleaning:
print 'clean_args = ', clean_args
crm(**clean_args)
elif routine_cache_cleaning == 'figures_only':
craz()
# -----------------------------------------------------------------------------------
# -- End of the atlas
# -----------------------------------------------------------------------------------