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

Last change on this file since 4221 was 3915, checked in by musat, 3 years ago

Corrections pour la realisation et l'acceleration des cartes des multi-atlas
IM

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