source: BOL/Multi_atlas/atlas/atlas_none.py @ 3840

Last change on this file since 3840 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'
16#dir_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')
54#crm(pattern='tasmax')
55#crm(pattern='tasmin')
56def apply_scale_offset(dat,scale,offset):
57    return ccdo(ccdo(dat,operator='mulc,'+str(float(scale))),operator='addc,'+str(float(offset)))
58#
59#craz()
60if opts.simulation is None:
61    print "Available simulations at %s are : "%opts.input,
62    for s in all_SE_simulations() : print s,
63    exit(0)
64#
65lvars=opts.variables
66if lvars is not None : lvars=lvars.split(',')
67else : lvars=variables_list
68#
69# Preparons une commande pour assembler les sorties Pdf
70if opts.pdf : pdffile=opts.pdf
71else: pdffile="atlas_"+opts.simulation+"_"+opts.season+".pdf"
72pdfargs=["pdfjam","--landscape","-o ",pdffile]
73#
74# Initialisation de l'index html
75index= header("LMDZ Atlas for "+opts.simulation+ " versus "+opts.reference+" ("+opts.season+")") 
76index += cell('PDF',pdffile)
77index += section("2d vars", level=4)
78index += open_table()
79#
80# Titres de colonnes
81ref=opts.reference ; 
82if (ref == 'OBS' ) : text_diff='bias'
83else:                text_diff='diff'
84index+=open_line('VARIABLE')+cell('bias')+cell('rmse')+cell('mean')+cell(ref)+cell(text_diff)+\
85        cell('zonal')+cell('all')+cell('pdf')+close_line()
86#
87# -- Declare the script ml2pl for vertical interpolation
88cscript("ml2pl", "/home/jservon/Evaluation/CliMAF/Atlas_LMDz/ml2pl.sh -p ${var_2} -m ${var_1} ${in_1} ${out} ${in_2}",
89    commuteWithTimeConcatenation=True, commuteWithSpaceConcatenation=True)
90# -- Vertical levels for the vertical interpolation
91fixed_fields("ml2pl",("press_levels.txt","/home/fabric/LMDZ/atlas/press_levels.txt"))
92#
93for variable  in lvars :
94    # Get the model and the reference
95    if opts.root:
96       simu=svsg(opts.simulation,variable,opts.season,opts.grid, root=opts.root)
97    else:
98       simu=svsg(opts.simulation,variable,opts.season,opts.grid)
99    print 'variable = ',variable
100    reff=svsg(opts.reference,variable,opts.season,opts.grid)
101    #
102    # If the variable is a 3D field:
103    #  - interpolate the variable on the standard pressure levels with ml2pl (L. Guez)
104    #  - Compute the difference model-ref with diff_zonmean (computes the zonal mean lat/pressure fields,
105    #    interpolates the model on the ref, both vertically and horizontally, and returns the difference)
106    if is3d(variable) :
107       simu_pres = svsg(opts.simulation,'pres',opts.season,opts.grid)
108       simu = ml2pl(simu,simu_pres)
109       simu = zonmean(simu)
110       reff = zonmean(reff)
111       diff = diff_zonmean(simu,reff)
112    else:
113        if (opts.grid == '' ) : reff=regrid(reff,simu)
114        diff=minus(simu,reff)
115
116    pparams = plot_params(variable,'full_field')
117    vertical_interval = 'trYMaxF=1000|trYMinF=1'
118    stringFontHeight=0.018
119    if is3d(variable):
120        pparams.update({'options':vertical_interval})
121        stringFontHeight=0.03
122    # Map for simulation
123    simu_fig=plot(simu,title="",
124                  gsnLeftString=variable,
125                  gsnCenterString=opts.simulation,
126                  gsnRightString=opts.season,
127                  gsnStringFontHeightF=stringFontHeight,
128                  mpCenterLonF=0,
129                  **pparams)
130    simu_avg=cvalue(space_average(simu))
131    #
132    # Map for reference
133    ref_fig=plot(reff,title="",
134                 gsnLeftString=variable,
135                 gsnCenterString=ref,
136                 gsnRightString=opts.season,
137                 gsnStringFontHeightF=stringFontHeight,
138                 mpCenterLonF=0,
139                 **pparams)
140    ref_avg=cvalue(space_average(reff))
141    #
142    # Bias (or difference between simulations) map
143    if (ref == 'OBS' ) : p=plot_params(variable,'bias')
144    else:                p=plot_params(variable,'model_model')
145    tmp_aux_params = plot_params(variable,'full_field')
146    scale = 1.0 ; offset = 0.0
147    if 'offset' in tmp_aux_params or 'scale' in tmp_aux_params:
148       if 'offset' in tmp_aux_params:
149          offset = tmp_aux_params['offset']
150       else:
151          offset=0.0
152       if 'scale' in tmp_aux_params:
153          scale = tmp_aux_params['scale']
154       else:
155          scale=1.0
156       wreff = apply_scale_offset(reff,scale,offset)
157       wsimu = apply_scale_offset(simu,scale,offset)
158    else:
159       wreff = reff
160       wsimu = simu
161    #
162    if is3d(variable):
163        p.update({'options':vertical_interval})
164    if variable in ['ua','va','ta','hus','hur']:
165        tmp_levs = tmp_aux_params['colors']
166        p.update({'contours':tmp_levs})
167        diff_fig=plot(diff,wreff,title="", format='png', mpCenterLonF=0,
168                  gsnLeftString=variable,
169                  gsnCenterString=opts.simulation+' - '+ref,
170                  gsnRightString=opts.season,
171                  gsnStringFontHeightF=stringFontHeight,
172                  aux_options='cnLineThicknessF=2|cnLineLabelsOn=True', **p)
173    else:
174        p.update({'contours':1})
175        diff_fig=plot(diff,title="", format='png', mpCenterLonF=0,
176                  gsnLeftString=variable,
177                  gsnCenterString=opts.simulation+' - '+ref,
178                  gsnRightString=opts.season,
179                  gsnStringFontHeightF=stringFontHeight,
180                  **p)
181
182    #
183    # Bias mean value, and RMSD/RMSE
184    diff_avg=cvalue(space_average(diff))
185    rmsd=math.sqrt(cvalue(space_average(ccdo(diff,operator='-b F64 sqr'))))
186    #
187    # Zonal means
188    if not is3d(variable):
189        # -- apply a mask corresponding to the reference
190        mask = div(reff,reff)
191        msimu = mul(wsimu,mask)
192        # -- Compute the zonal mean
193        zmean=ccdo(msimu, operator='zonmean')
194        ref_zmean=ccdo(wreff, operator='zonmean')
195        #
196        sim=opts.simulation
197        #if variable in ['zg500']:
198        #   ref_zmean = ccdo(ref_zmean,operator='-b F32 mulc,1')
199        #   zmean = ccdo(zmean,operator='-b F32 mulc,1')
200        zmean_fig=curves(cens([sim,ref],zmean,ref_zmean),
201                         title="",
202                         lgcols=3,
203                         options=#'tiYAxisString=""|'+\
204                                 #'+\'+\
205                                 'tmYROn=True|'+\
206                                 'tmYRBorderOn=True|'+\
207                                 'tmYLOn=False|'+\
208                                 'tmYUseRight=True|'+\
209                                 'vpXF=0|'+\
210                                 'vpWidthF=0.66|'+\
211                                 'vpHeightF=0.33|'+\
212                                 'tmYRLabelsOn=True|'+\
213                                 'tmXBLabelFontHeightF=0.018|'+\
214                                 'tmYLLabelFontHeightF=0.016|'+\
215                                 'lgLabelFontHeightF=0.018|'+\
216                                 #'pmLegendSide=Bottom|'+\
217                                 'pmLegendOrthogonalPosF=-0.32|'+\
218                                 'pmLegendParallelPosF=1.0|'+\
219                                 'tmXMajorGrid=True|'+\
220                                 'tmYMajorGrid=True|'+\
221                                 'tmXMajorGridLineDashPattern=2|'+\
222                                 'tmYMajorGridLineDashPattern=2|'+\
223                                 'xyLineThicknessF=8|'+\
224                                 'gsnLeftString='+variable+'|'+\
225                                 'gsnCenterString='+opts.simulation+' vs '+ref+'|'+\
226                                 'gsnRightString='+opts.season+'|'+\
227                                 'gsnStringFontHeightF='+str(stringFontHeight))
228    #    # Composite figure
229    if is3d(variable):
230        page=cpage([[simu_fig,ref_fig],[diff_fig,None]],orientation='landscape', page_trim=True, fig_trim=True)
231        pdf_page=cpage([[simu_fig,ref_fig],[diff_fig,None]],orientation='landscape',
232                   page_trim=True, fig_trim=True, format='pdf', title=variable+" "+opts.simulation)
233    else:
234        page=cpage([[simu_fig,ref_fig],[diff_fig,zmean_fig]],orientation='landscape', page_trim=True, fig_trim=True)
235        pdf_page=cpage([[simu_fig,ref_fig],[diff_fig,zmean_fig]],orientation='landscape', 
236                   page_trim=True, fig_trim=True, format='pdf', title=variable+" "+opts.simulation)
237    pdfargs.append(cfile(pdf_page))
238    #
239    thumbnail_size = 200
240    if is3d(variable):
241            index+=open_line(varlongname(variable)+' ('+variable+')')+\
242                    cell("%.2g"%diff_avg,cfile(diff_fig))+\
243                    cell("%.2g"%rmsd,cfile(diff_fig))+\
244                    cell(simu,cfile(simu_fig),thumbnail=thumbnail_size,hover=False)+\
245                    cell(ref,cfile(ref_fig),thumbnail=thumbnail_size,hover=False)+\
246                    cell(text_diff,cfile(diff_fig),thumbnail=thumbnail_size,hover=False)+\
247                    ' '+\
248                    cell('page',cfile(page),thumbnail=thumbnail_size,hover=False)+\
249                    cell('Pdf',cfile(pdf_page))
250            close_line()
251    else:
252            index+=open_line(varlongname(variable)+' ('+variable+')')+\
253                    cell("%.2g"%diff_avg,cfile(diff_fig))+\
254                    cell("%.2g"%rmsd,cfile(diff_fig))+\
255                    cell(simu,cfile(simu_fig),thumbnail=thumbnail_size,hover=False)+\
256                   cell(ref,cfile(ref_fig),thumbnail=thumbnail_size,hover=False)+\
257                    cell(text_diff,cfile(diff_fig),thumbnail=thumbnail_size,hover=False)+\
258                    cell('zonal mean',cfile(zmean_fig),thumbnail=thumbnail_size,hover=False)+\
259                    cell('page',cfile(page),thumbnail=thumbnail_size,hover=False)+\
260                    cell('Pdf',cfile(pdf_page))
261            close_line()
262#
263# Finalisons l'index html
264index += close_table()
265index += trailer()
266#out="index_example.html"
267out="index_example_"+opts.season+"_"+opts.simulation+".html"
268with open(out,"w") as filout : filout.write(index)
269#
270# Creation du Pdf multi-pages
271comm=subprocess.Popen(pdfargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
272#
273import os,os.path ; 
274# print("Attendez un bon peu : lancemement de firefox sur Ciclad....")
275# os.system("firefox file://"+os.path.abspath(os.path.curdir)+"/"+out+"&")
Note: See TracBrowser for help on using the repository browser.