source: BOL/Multi_atlas/atlas/atlas_none_IM.py @ 3684

Last change on this file since 3684 was 3684, checked in by idelkadi, 4 years ago

Repository under svn of a first version of Multiatlas diagnostics for LMDZ. This version is adapted to be able to run a LMDZ multiatlas on an individual account on the ciclad machine of the IPSL. In this version, the parts to be modified are identified so as to subsequently adapt it to other machines.
This version is still under development.

File size: 11.3 KB
Line 
1# -*- coding: iso-8859-1 -*-
2# Created : S.Sénési - nov 2015
3
4#
5desc="\nCreation d'un atlas pour une simu, une grille et une liste de variables et de saisons \n"+\
6"  Exemples : \n"+\
7"  >>> python ./atlas.py -v tas,hfls -s NPv3.1ada_1982_1991\n"+\
8""
9# Avec CliMAF, cette etape est loin d'etre necessaire; on la réalise pour 'exposer' ces fichiers
10# dans une arborescence à laquelle sont habitues certains utilisateurs
11
12
13# Répertoire de base pour les entrées et les résultats
14#dir_default='/data/hourdin/LMDZ6/SE/ORIG'
15#dir_default='/prodigfs/fabric/LMDZ6/SE/ORIG'
16dir_default='/prodigfs/ipslfs/dods/fabric/lmdz/SE/ORIG'
17
18# Gestion des options et arguments d'appel
19from optparse import OptionParser
20parser = OptionParser(desc) ; parser.set_usage("%%prog [-h]\n%s" % desc)
21#parser.add_option("-i", "--input", help="repertoire des donnes d'entree(defaut : %s)"%dir_default,
22#                  action="store",default=dir_default)
23parser.add_option("-i", "--input", help="repertoire des donnes d'entree(defaut : %s)",
24                  action="store",default=None)
25parser.add_option("-g", "--grid", help="nom de grille (default: VLR)", action="store",default='VLR')
26parser.add_option("-r", "--region", help="nom de zone (default: GLOB)", action="store",default='GLOB')
27parser.add_option("-p", "--season", help="saison a traiter " "(eg : JJA, DJF, YEAR, defaut=%YEAR)", 
28                  action="store", default='YEAR')
29parser.add_option("-s", "--simulation", help="simulation+annees a traiter (sim_YYY1_YYY2) - laisser vide pour lister",
30                  action="store",default=None)
31parser.add_option("-t", "--reference", help="simulation de reference (sim_YYY1_YYY2, default=OBS) ",
32                  action="store",default='OBS')
33parser.add_option("-v", "--variables", help="liste des variables (separees par des virgules)", action="store",default=None)
34#parser.add_option("--root", help="Path to the root directory", action="store",default=None)
35parser.add_option("--root", help="Path to the root directory", action="store",default=None)
36parser.add_option("-f", "--force", help="force le recalcul de champs existants", 
37                  action="store_true",default=None)
38parser.add_option("-o", "--pdf", help="nom du pdf de sortie (default: atlas_<SIMU>_<SAISON>.pdf)", action="store")
39(opts, args) = parser.parse_args()
40
41#---------------------------------------------------------------------------------
42import math
43from climaf.api import *
44from climaf.html import * 
45# La description de l'organisation des données SE et des alias et rescalings
46# est partagée dans une micro-librairie :
47from lmdz_SE import * # svsg, all_SE_simulations
48from plot_params import plot_params
49#---------------------------------------------------------------------------------
50#
51clog('debug')
52crm(pattern='pme')
53crm(pattern='hflsevap')
54def apply_scale_offset(dat,scale,offset):
55    return ccdo(ccdo(dat,operator='mulc,'+str(float(scale))),operator='addc,'+str(float(offset)))
56#
57#craz()
58if opts.simulation is None:
59    print "Available simulations at %s are : "%opts.input,
60    for s in all_SE_simulations() : print s,
61    exit(0)
62#
63lvars=opts.variables
64if lvars is not None : lvars=lvars.split(',')
65else : lvars=variables_list
66#
67# Preparons une commande pour assembler les sorties Pdf
68if opts.pdf : pdffile=opts.pdf
69else: pdffile="atlas_"+opts.simulation+"_"+opts.season+".pdf"
70pdfargs=["pdfjam","--landscape","-o ",pdffile]
71#
72# Initialisation de l'index html
73index= header("LMDZ Atlas for "+opts.simulation+ " versus "+opts.reference+" ("+opts.season+")") 
74index += cell('PDF',pdffile)
75index += section("2d vars", level=4)
76index += open_table()
77#
78# Titres de colonnes
79ref=opts.reference ; 
80if (ref == 'OBS' ) : text_diff='bias'
81else:                text_diff='diff'
82index+=open_line('VARIABLE')+cell('bias')+cell('rmse')+cell('mean')+cell(ref)+cell(text_diff)+\
83        cell('zonal')+cell('all')+cell('pdf')+close_line()
84#
85# -- Declare the script ml2pl for vertical interpolation
86cscript("ml2pl", "/home/jservon/Evaluation/CliMAF/Atlas_LMDz/ml2pl.sh -p ${var_2} -m ${var_1} ${in_1} ${out} ${in_2}",
87    commuteWithTimeConcatenation=True, commuteWithSpaceConcatenation=True)
88# -- Vertical levels for the vertical interpolation
89fixed_fields("ml2pl",("press_levels.txt","/home/fabric/LMDZ/atlas/press_levels_IM.txt"))
90#
91for variable  in lvars :
92    # Get the model and the reference
93    if opts.root:
94       simu=svsg(opts.simulation,variable,opts.season,opts.grid, root=opts.root)
95    else:
96       simu=svsg(opts.simulation,variable,opts.season,opts.grid)
97    print 'variable = ',variable
98    reff=svsg(opts.reference,variable,opts.season,opts.grid)
99    #
100    # If the variable is a 3D field:
101    #  - interpolate the variable on the standard pressure levels with ml2pl (L. Guez)
102    #  - Compute the difference model-ref with diff_zonmean (computes the zonal mean lat/pressure fields,
103    #    interpolates the model on the ref, both vertically and horizontally, and returns the difference)
104    if is3d(variable) :
105       simu_pres = svsg(opts.simulation,'pres',opts.season,opts.grid)
106       simu = ml2pl(simu,simu_pres)
107       simu = zonmean(simu)
108       reff = zonmean(reff)
109       diff = diff_zonmean(simu,reff)
110    else:
111        if (opts.grid == '' ) : reff=regrid(reff,simu)
112        diff=minus(simu,reff)
113
114    pparams = plot_params(variable,'full_field')
115    # vertical_interval = 'trYMaxF=1000|trYMinF=1'
116    vertical_interval = 'trYMaxF=1000|trYMinF=8'
117    stringFontHeight=0.018
118    if is3d(variable):
119        pparams.update({'options':vertical_interval})
120        stringFontHeight=0.03
121    # Map for simulation
122    simu_fig=plot(simu,title="",
123                  gsnLeftString=variable,
124                  gsnCenterString=opts.simulation,
125                  gsnRightString=opts.season,
126                  gsnStringFontHeightF=stringFontHeight,
127                  mpCenterLonF=0,
128                  **pparams)
129    simu_avg=cvalue(space_average(simu))
130    #
131    # Map for reference
132    ref_fig=plot(reff,title="",
133                 gsnLeftString=variable,
134                 gsnCenterString=ref,
135                 gsnRightString=opts.season,
136                 gsnStringFontHeightF=stringFontHeight,
137                 mpCenterLonF=0,
138                 **pparams)
139    ref_avg=cvalue(space_average(reff))
140    #
141    # Bias (or difference between simulations) map
142    if (ref == 'OBS' ) : p=plot_params(variable,'bias')
143    else:                p=plot_params(variable,'model_model')
144    tmp_aux_params = plot_params(variable,'full_field')
145    scale = 1.0 ; offset = 0.0
146    if 'offset' in tmp_aux_params or 'scale' in tmp_aux_params:
147       if 'offset' in tmp_aux_params:
148          offset = tmp_aux_params['offset']
149       else:
150          offset=0.0
151       if 'scale' in tmp_aux_params:
152          scale = tmp_aux_params['scale']
153       else:
154          scale=1.0
155       wreff = apply_scale_offset(reff,scale,offset)
156       wsimu = apply_scale_offset(simu,scale,offset)
157    else:
158       wreff = reff
159       wsimu = simu
160    #
161    if is3d(variable):
162        p.update({'options':vertical_interval})
163    if variable in ['ua','va','ta','hus','hur']:
164        tmp_levs = tmp_aux_params['colors']
165        p.update({'contours':tmp_levs})
166        diff_fig=plot(diff,wreff,title="", format='png', mpCenterLonF=0,
167                  gsnLeftString=variable,
168                  gsnCenterString=opts.simulation+' - '+ref,
169                  gsnRightString=opts.season,
170                  gsnStringFontHeightF=stringFontHeight,
171                  aux_options='cnLineThicknessF=2|cnLineLabelsOn=True', **p)
172    else:
173        p.update({'contours':1})
174        diff_fig=plot(diff,title="", format='png', mpCenterLonF=0,
175                  gsnLeftString=variable,
176                  gsnCenterString=opts.simulation+' - '+ref,
177                  gsnRightString=opts.season,
178                  gsnStringFontHeightF=stringFontHeight,
179                  **p)
180
181    #
182    # Bias mean value, and RMSD/RMSE
183    diff_avg=cvalue(space_average(diff))
184    rmsd=math.sqrt(cvalue(space_average(ccdo(diff,operator='-b F64 sqr'))))
185    #
186    # Zonal means
187    if not is3d(variable):
188        # -- apply a mask corresponding to the reference
189        mask = div(reff,reff)
190        msimu = mul(wsimu,mask)
191        # -- Compute the zonal mean
192        zmean=ccdo(msimu, operator='zonmean')
193        ref_zmean=ccdo(wreff, operator='zonmean')
194        #
195        sim=opts.simulation
196        #if variable in ['zg500']:
197        #   ref_zmean = ccdo(ref_zmean,operator='-b F32 mulc,1')
198        #   zmean = ccdo(zmean,operator='-b F32 mulc,1')
199        zmean_fig=curves(cens([sim,ref],zmean,ref_zmean),
200                         title="",
201                         lgcols=3,
202                         options=#'tiYAxisString=""|'+\
203                                 #'+\'+\
204                                 'tmYROn=True|'+\
205                                 'tmYRBorderOn=True|'+\
206                                 'tmYLOn=False|'+\
207                                 'tmYUseRight=True|'+\
208                                 'vpXF=0|'+\
209                                 'vpWidthF=0.66|'+\
210                                 'vpHeightF=0.33|'+\
211                                 'tmYRLabelsOn=True|'+\
212                                 'tmXBLabelFontHeightF=0.018|'+\
213                                 'tmYLLabelFontHeightF=0.016|'+\
214                                 'lgLabelFontHeightF=0.018|'+\
215                                 #'pmLegendSide=Bottom|'+\
216                                 'pmLegendOrthogonalPosF=-0.32|'+\
217                                 'pmLegendParallelPosF=1.0|'+\
218                                 'tmXMajorGrid=True|'+\
219                                 'tmYMajorGrid=True|'+\
220                                 'tmXMajorGridLineDashPattern=2|'+\
221                                 'tmYMajorGridLineDashPattern=2|'+\
222                                 'xyLineThicknessF=8|'+\
223                                 'gsnLeftString='+variable+'|'+\
224                                 'gsnCenterString='+opts.simulation+' vs '+ref+'|'+\
225                                 'gsnRightString='+opts.season+'|'+\
226                                 'gsnStringFontHeightF='+str(stringFontHeight))
227    #    # Composite figure
228    if is3d(variable):
229        page=cpage([[simu_fig,ref_fig],[diff_fig,None]],orientation='landscape', page_trim=True, fig_trim=True)
230        pdf_page=cpage([[simu_fig,ref_fig],[diff_fig,None]],orientation='landscape',
231                   page_trim=True, fig_trim=True, format='pdf', title=variable+" "+opts.simulation)
232    else:
233        page=cpage([[simu_fig,ref_fig],[diff_fig,zmean_fig]],orientation='landscape', page_trim=True, fig_trim=True)
234        pdf_page=cpage([[simu_fig,ref_fig],[diff_fig,zmean_fig]],orientation='landscape', 
235                   page_trim=True, fig_trim=True, format='pdf', title=variable+" "+opts.simulation)
236    pdfargs.append(cfile(pdf_page))
237    #
238    thumbnail_size = 200
239    if is3d(variable):
240            index+=open_line(varlongname(variable)+' ('+variable+')')+\
241                    cell("%.2g"%diff_avg,cfile(diff_fig))+\
242                    cell("%.2g"%rmsd,cfile(diff_fig))+\
243                    cell(simu,cfile(simu_fig),thumbnail=thumbnail_size,hover=False)+\
244                    cell(ref,cfile(ref_fig),thumbnail=thumbnail_size,hover=False)+\
245                    cell(text_diff,cfile(diff_fig),thumbnail=thumbnail_size,hover=False)+\
246                    ' '+\
247                    cell('page',cfile(page),thumbnail=thumbnail_size,hover=False)+\
248                    cell('Pdf',cfile(pdf_page))
249            close_line()
250    else:
251            index+=open_line(varlongname(variable)+' ('+variable+')')+\
252                    cell("%.2g"%diff_avg,cfile(diff_fig))+\
253                    cell("%.2g"%rmsd,cfile(diff_fig))+\
254                    cell(simu,cfile(simu_fig),thumbnail=thumbnail_size,hover=False)+\
255                   cell(ref,cfile(ref_fig),thumbnail=thumbnail_size,hover=False)+\
256                    cell(text_diff,cfile(diff_fig),thumbnail=thumbnail_size,hover=False)+\
257                    cell('zonal mean',cfile(zmean_fig),thumbnail=thumbnail_size,hover=False)+\
258                    cell('page',cfile(page),thumbnail=thumbnail_size,hover=False)+\
259                    cell('Pdf',cfile(pdf_page))
260            close_line()
261#
262# Finalisons l'index html
263index += close_table()
264index += trailer()
265#out="index_example.html"
266out="index_example_"+opts.season+"_"+opts.simulation+".html"
267with open(out,"w") as filout : filout.write(index)
268#
269# Creation du Pdf multi-pages
270comm=subprocess.Popen(pdfargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
271#
272import os,os.path ; 
273# print("Attendez un bon peu : lancemement de firefox sur Ciclad....")
274# os.system("firefox file://"+os.path.abspath(os.path.curdir)+"/"+out+"&")
Note: See TracBrowser for help on using the repository browser.