1 | ####################### |
---|
2 | ##### PLANETOPLOT ##### |
---|
3 | ####################### |
---|
4 | |
---|
5 | ### A. Spiga -- LMD -- 06~09/2011 -- General building and mapping capabilities |
---|
6 | ### T. Navarro -- LMD -- 10~11/2011 -- Improved use for GCM and added sections + 1Dplot capabilities |
---|
7 | ### A. Colaitis -- LMD -- 11/2011 -- Mostly minor improvements and inter-plot operation capabilities + zrecast interpolation for gcm |
---|
8 | ### A. Spiga -- LMD -- 11~12/2011 -- Extended multivar subplot capabilities + cosmetic changes + general cleaning and tests |
---|
9 | ### A. Colaitis -- LMD -- 12/2011 -- Added movie capability [mencoder must be installed] |
---|
10 | ### A. Spiga -- LMD -- 12/2011 -- Added HTML animated page capability + general tests of consistency [winds, etc...] |
---|
11 | |
---|
12 | def planetoplot (namefiles,\ |
---|
13 | level=0,\ |
---|
14 | vertmode=0,\ |
---|
15 | proj=None,\ |
---|
16 | back=None,\ |
---|
17 | target=None, |
---|
18 | stride=3,\ |
---|
19 | var=None,\ |
---|
20 | colorb="def",\ |
---|
21 | winds=False,\ |
---|
22 | addchar=None,\ |
---|
23 | interv=[0,1],\ |
---|
24 | vmin=None,\ |
---|
25 | vmax=None,\ |
---|
26 | tile=False,\ |
---|
27 | zoom=None,\ |
---|
28 | display=True,\ |
---|
29 | hole=False,\ |
---|
30 | save="gui",\ |
---|
31 | anomaly=False,\ |
---|
32 | var2=None,\ |
---|
33 | ndiv=10,\ |
---|
34 | mult=1.,\ |
---|
35 | zetitle="fill",\ |
---|
36 | slon=None,\ |
---|
37 | slat=None,\ |
---|
38 | svert=None,\ |
---|
39 | stime=None,\ |
---|
40 | outputname=None,\ |
---|
41 | resolution=200,\ |
---|
42 | ope=None,\ |
---|
43 | fileref=None,\ |
---|
44 | minop=0.,\ |
---|
45 | maxop=0.,\ |
---|
46 | titleref="fill",\ |
---|
47 | invert_y=False,\ |
---|
48 | xaxis=[None,None],\ |
---|
49 | yaxis=[None,None],\ |
---|
50 | ylog=False,\ |
---|
51 | yintegral=False,\ |
---|
52 | blat=None,\ |
---|
53 | blon=None,\ |
---|
54 | tsat=False,\ |
---|
55 | flagnolow=False,\ |
---|
56 | mrate=None,\ |
---|
57 | mquality=False): |
---|
58 | |
---|
59 | |
---|
60 | #################################################################################################################### |
---|
61 | ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png |
---|
62 | |
---|
63 | ################################# |
---|
64 | ### Load librairies and functions |
---|
65 | from netCDF4 import Dataset |
---|
66 | from myplot import getcoord2d,define_proj,makeplotres,simplinterv,vectorfield,ptitle,latinterv,getproj,wrfinterv,dumpbdy,\ |
---|
67 | fmtvar,definecolorvec,defcolorb,getprefix,putpoints,calculate_bounds,errormess,definesubplot,\ |
---|
68 | zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ |
---|
69 | getname,localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds |
---|
70 | from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img |
---|
71 | import matplotlib as mpl |
---|
72 | from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel, title, close |
---|
73 | from matplotlib.cm import get_cmap |
---|
74 | import numpy as np |
---|
75 | from numpy.core.defchararray import find |
---|
76 | from videosink import VideoSink |
---|
77 | import subprocess |
---|
78 | |
---|
79 | ################################ |
---|
80 | ### Preliminary stuff |
---|
81 | ################################ |
---|
82 | print "********************************************" |
---|
83 | print "********** WELCOME TO PLANETOPLOT **********" |
---|
84 | print "********************************************" |
---|
85 | if not isinstance(namefiles, np.ndarray): namefiles = [namefiles] |
---|
86 | if not isinstance(var, np.ndarray): var = [var] |
---|
87 | |
---|
88 | ################################ |
---|
89 | ### Which plot needs to be done? |
---|
90 | ################################ |
---|
91 | nlon, nlat, nvert, ntime, mapmode, nslices = determineplot(slon, slat, svert, stime) |
---|
92 | vlon = None ; vlat = None |
---|
93 | if slon is not None: vlon = slon[0][0] |
---|
94 | if slat is not None: vlat = slat[0][0] |
---|
95 | if mapmode == 0: winds=False |
---|
96 | elif mapmode == 1: |
---|
97 | if svert is None: svert = readslices(str(level)) ; nvert=1 |
---|
98 | if stime is None and mrate is None: |
---|
99 | stime = readslices(str(0)) ; ntime=1 ## this is a default choice |
---|
100 | print "WELL... nothing about time axis. I took default: first time reference stored in file." |
---|
101 | if mrate is not None and len(var) > 1: errormess("multivar not allowed in movies. should be fixed soon!") |
---|
102 | zelen = len(namefiles)*len(var) |
---|
103 | numplot = zelen*nslices |
---|
104 | print "********** FILES, SLICES, VARS, TOTAL PLOTS: ", len(namefiles), nslices, len(var), numplot |
---|
105 | if ope is not None: |
---|
106 | if fileref is not None: zelen = zelen + 2 |
---|
107 | elif "var" in ope: zelen = zelen + 1 |
---|
108 | all_var = [[]]*zelen ; all_var2 = [[]]*zelen ; all_title = [[]]*zelen ; all_varname = [[]]*zelen ; all_namefile = [[]]*zelen ; all_time = [[]]*zelen ; all_windu = [[]]*zelen ; all_windv = [[]]*zelen |
---|
109 | |
---|
110 | ################################################################################################# |
---|
111 | ### Loop over the files + vars initially separated by commas to be plotted on the same figure ### |
---|
112 | ################################################################################################# |
---|
113 | k = 0 ; firstfile = True |
---|
114 | for nnn in range(len(namefiles)): |
---|
115 | for vvv in range(len(var)): |
---|
116 | |
---|
117 | print "********** LOOP..... THIS IS SUBPLOT NUMBER.....",k |
---|
118 | |
---|
119 | ###################### |
---|
120 | ### Load NETCDF object |
---|
121 | namefile = namefiles[nnn] ; print "********** THE NAMEFILE IS....", namefile |
---|
122 | nc = Dataset(namefile) |
---|
123 | |
---|
124 | ################################## |
---|
125 | ### Initial checks and definitions |
---|
126 | ### ... TYPEFILE |
---|
127 | typefile = whatkindfile(nc) |
---|
128 | if typefile in ['mesoideal']: mapmode=0;winds=False |
---|
129 | if firstfile: print "********** MAPMODE: ", mapmode |
---|
130 | if firstfile: typefile0 = typefile |
---|
131 | elif typefile != typefile0: errormess("Not the same kind of files !", [typefile0, typefile]) |
---|
132 | ### ... VAR |
---|
133 | varname=var[vvv] |
---|
134 | print "********** THE VAR IS....",varname, var2 |
---|
135 | if varname not in nc.variables: varname = False |
---|
136 | ### ... WINDS |
---|
137 | if winds: |
---|
138 | [uchar,vchar,metwind] = getwinddef(nc) |
---|
139 | if uchar == 'not found': winds = False |
---|
140 | if not varname and not winds: errormess("please set at least winds or var",printvar=nc.variables) |
---|
141 | ### ... COORDINATES, could be moved below |
---|
142 | [lon2d,lat2d] = getcoorddef(nc) |
---|
143 | ### ... PROJECTION |
---|
144 | if ((proj == None) and (typefile not in ['mesoideal'])): proj = getproj(nc) |
---|
145 | |
---|
146 | ########################################################## |
---|
147 | if typefile == "gcm": |
---|
148 | lat = nc.variables["latitude"][:] ; lon = nc.variables["longitude"][:] |
---|
149 | if "Time" in nc.variables: time = nc.variables["Time"][:] |
---|
150 | elif "time" in nc.variables: time = nc.variables["time"][:] |
---|
151 | else: errormess("no time axis found.") |
---|
152 | vert = nc.variables["altitude"][:] |
---|
153 | elif typefile in ['meso','mesoapi','geo','mesoideal']: |
---|
154 | if vlon is not None or vlat is not None: indices = bidimfind(lon2d,lat2d,vlon,vlat) ; print '********** INDICES: ', indices |
---|
155 | if slon is not None: slon[0][0] = indices[0] ; slon[0][1] = indices[0] |
---|
156 | if slat is not None: slat[0][0] = indices[1] ; slat[0][1] = indices[1] |
---|
157 | if varname in ['PHTOT','W']: vertdim='BOTTOM-TOP_PATCH_END_STAG' |
---|
158 | else: vertdim='BOTTOM-TOP_PATCH_END_UNSTAG' |
---|
159 | if (var2 is not None and var2 not in ['PHTOT','W']): |
---|
160 | vertdim='BOTTOM-TOP_PATCH_END_UNSTAG' |
---|
161 | dumped_vert_stag=True |
---|
162 | if varname in ['V']: latdim='SOUTH-NORTH_PATCH_END_STAG' |
---|
163 | else: latdim='SOUTH-NORTH_PATCH_END_UNSTAG' |
---|
164 | if varname in ['U']: londim='WEST-EAST_PATCH_END_STAG' |
---|
165 | else: londim='WEST-EAST_PATCH_END_UNSTAG' |
---|
166 | lon = np.arange(0,getattr(nc,londim),1) ; lat = np.arange(0,getattr(nc,latdim),1) |
---|
167 | if "Times" in nc.variables:time = np.arange(0,len(nc.variables["Times"]),1) |
---|
168 | elif "Time" in nc.variables:time = np.arange(0,len(nc.variables["Time"]),1) |
---|
169 | if typefile in ['geo']: vert = [0.] ; stime = readslices(str(0)) |
---|
170 | else: |
---|
171 | if vertmode is None: vertmode=0 |
---|
172 | if vertmode == 0: vert = np.arange(0,getattr(nc,vertdim),1) |
---|
173 | else: vert = nc.variables["vert"][:] |
---|
174 | #if firstfile: |
---|
175 | # lat0 = lat |
---|
176 | #elif len(lat0) != len(lat): |
---|
177 | # errormess("Not the same latitude lengths !", [len(lat0), len(lat)]) |
---|
178 | #elif sum((lat == lat0) == False) != 0: |
---|
179 | # errormess("Not the same latitudes !", [lat,lat0]) |
---|
180 | ## Faire d'autre checks sur les compatibilites entre fichiers!! |
---|
181 | ########################################################## |
---|
182 | |
---|
183 | if firstfile: |
---|
184 | ########################## |
---|
185 | ### Define plot boundaries |
---|
186 | ### todo: possible areas in latinterv in argument (ex: "Far_South_Pole") |
---|
187 | if proj in ["npstere","spstere"]: [wlon,wlat] = polarinterv(lon2d,lat2d) |
---|
188 | elif proj in ["lcc","laea"]: [wlon,wlat] = wrfinterv(lon2d,lat2d) |
---|
189 | else: [wlon,wlat] = simplinterv(lon2d,lat2d) |
---|
190 | if zoom: [wlon,wlat] = zoomset(wlon,wlat,zoom) |
---|
191 | |
---|
192 | all_varname[k] = varname |
---|
193 | all_namefile[k] = namefile |
---|
194 | all_time[k] = time |
---|
195 | if var2: all_var2[k] = getfield(nc,var2) |
---|
196 | if winds: all_windu[k] = getfield(nc,uchar) ; all_windv[k] = getfield(nc,vchar) |
---|
197 | ##### SPECIFIC |
---|
198 | if varname in ["temp","t","T_nadir_nit","T_nadir_day","temp_day","temp_night"] and tsat: |
---|
199 | tt=getfield(nc,varname) ; print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE" |
---|
200 | if type(tt).__name__=='MaskedArray': tt.set_fill_value([np.NaN]) ; tinput=tt.filled() |
---|
201 | else: tinput=tt |
---|
202 | all_var[k]=get_tsat(vert,tinput,zlon=lon,zlat=lat,zalt=vert,ztime=time) |
---|
203 | else: |
---|
204 | ##### GENERAL STUFF HERE |
---|
205 | all_var[k] = getfield(nc,varname) |
---|
206 | print "********** all_var[k].shape", all_var[k].shape |
---|
207 | k += 1 |
---|
208 | firstfile = False |
---|
209 | #### End of for namefile in namefiles |
---|
210 | |
---|
211 | ################################## |
---|
212 | ### Operation on files |
---|
213 | if ope is not None: |
---|
214 | print ope |
---|
215 | if "var" not in ope: |
---|
216 | if len(var) > 1: errormess("for this operation... please set only one var !") |
---|
217 | if ope in ["-","+"]: |
---|
218 | if fileref is not None: all_var[k] = getfield(Dataset(fileref),all_varname[k-1]) ; all_varname[k] = all_varname[k-1] ; all_time[k] = all_time[k-1] ; all_namefile[k] = all_namefile[k-1] |
---|
219 | else: errormess("fileref is missing!") |
---|
220 | if ope == "-": all_var[k+1]= all_var[k-1] - all_var[k] |
---|
221 | elif ope == "+": all_var[k+1]= all_var[k-1] + all_var[k] |
---|
222 | all_varname[k+1] = all_varname[k] ; all_time[k+1] = all_time[k] ; all_namefile[k+1] = all_namefile[k] ; numplot = numplot+2 |
---|
223 | elif ope in ["cat"]: |
---|
224 | tab = all_var[0];k = 1 |
---|
225 | while k != len(namefiles): |
---|
226 | tab = np.append(tab,all_var[k],axis=0) ; k += 1 |
---|
227 | all_time[0] = np.arange(0,len(tab),1) ### AS: time reference is too simplistic, should be better |
---|
228 | all_var[0] = np.array(tab) ; numplot = 1 |
---|
229 | else: errormess(ope+" : non-implemented operation. Check pp.py --help") |
---|
230 | else: |
---|
231 | if len(namefiles) > 1: errormess("for this operation... please set only one file !") |
---|
232 | if len(var) > 2: errormess("not sure this works for more than 2 vars... please check.") |
---|
233 | if ope in ["div_var"]: all_var[k] = all_var[k-2] / all_var[k-1] ; insert = '_div_' |
---|
234 | elif ope in ["mul_var"]: all_var[k] = all_var[k-2] * all_var[k-1] ; insert = '_mul_' |
---|
235 | elif ope in ["add_var"]: all_var[k] = all_var[k-2] + all_var[k-1] ; insert = '_add_' |
---|
236 | elif ope in ["sub_var"]: all_var[k] = all_var[k-2] - all_var[k-1] ; insert = '_sub_' |
---|
237 | else: errormess(ope+" : non-implemented operation. Check pp.py --help") |
---|
238 | numplot = numplot + 1 ; all_time[k] = all_time[k-1] ; all_namefile[k] = all_namefile[k-1] |
---|
239 | all_varname[k] = all_varname[k-2] + insert + all_varname[k-1] |
---|
240 | |
---|
241 | ################################## |
---|
242 | ### Open a figure and set subplots |
---|
243 | fig = figure() |
---|
244 | subv,subh = definesubplot( numplot, fig ) |
---|
245 | |
---|
246 | ################################# |
---|
247 | ### Time loop for plotting device |
---|
248 | nplot = 1 ; error = False |
---|
249 | print "********************************************" |
---|
250 | while error is False: |
---|
251 | print "********** NPLOT", nplot |
---|
252 | |
---|
253 | ### General plot settings |
---|
254 | if nplot > numplot: break |
---|
255 | if numplot > 1: subplot(subv,subh,nplot) |
---|
256 | |
---|
257 | ### Map projection |
---|
258 | if mapmode == 1: m = define_proj(proj,wlon,wlat,back=back,blat=blat,blon=blon) ; x, y = m(lon2d, lat2d) |
---|
259 | elif mapmode ==0: m = None ; x = None ; y = None |
---|
260 | |
---|
261 | #################################################################### |
---|
262 | ## get all indexes to be taken into account for this subplot and then reduce field |
---|
263 | ## We plot 1) all lon slices 2) all lat slices 3) all vert slices 4) all time slices and then go to the next slice |
---|
264 | indexlon = getsindex(slon,(nplot-1)%nlon,lon) |
---|
265 | indexlat = getsindex(slat,((nplot-1)//nlon)%nlat,lat) |
---|
266 | indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert) |
---|
267 | if ope is not None: |
---|
268 | if fileref is not None: index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(namefiles)+2) ## OK only 1 var, see test in the beginning |
---|
269 | elif "var" in ope: index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(var)+1) ## OK only 1 file, see test in the beginning |
---|
270 | elif "cat" in ope: index_f = 0 |
---|
271 | else: yeah = len(namefiles)*len(var) ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah |
---|
272 | time = all_time[index_f] |
---|
273 | if stime is not None: |
---|
274 | if stime[0][0] < 0: |
---|
275 | if typefile in ['mesoapi','meso']: |
---|
276 | for i in range(len(time)): time[i] = localtime ( interv[0]+time[i]*interv[1], 0.5*(wlon[0]+wlon[1]) ) |
---|
277 | print "OK... WORKING WITH LOCAL TIMES" |
---|
278 | else: errormess("local times not supported. not too hard to modify the code though.") |
---|
279 | if mrate is not None: indextime = None |
---|
280 | else: indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) |
---|
281 | ltst = None |
---|
282 | if typefile in ['mesoapi','meso'] and indextime is not None: ltst = localtime ( interv[0]+indextime*interv[1], 0.5*(wlon[0]+wlon[1]) ) |
---|
283 | print "********** index lon, lat, vert, time ",indexlon,indexlat,indexvert,indextime |
---|
284 | #################################################################### |
---|
285 | error = False |
---|
286 | varname = all_varname[index_f] |
---|
287 | if varname: ### what is shaded. |
---|
288 | what_I_plot, error = reducefield( all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat, d3=indexvert, \ |
---|
289 | yint=yintegral, alt=vert, anomaly=anomaly ) |
---|
290 | what_I_plot = what_I_plot*mult |
---|
291 | if var2: ### what is contoured. |
---|
292 | what_I_plot_contour, error = reducefield( all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert, \ |
---|
293 | yint=yintegral, alt=vert ) |
---|
294 | if winds: ### what is plot as vectors. |
---|
295 | vecx, error = reducefield( all_windu[index_f], d4=indextime, d3=indexvert, yint=yintegral, alt=vert) |
---|
296 | vecy, error = reducefield( all_windv[index_f], d4=indextime, d3=indexvert, yint=yintegral, alt=vert) |
---|
297 | #################################################################### |
---|
298 | |
---|
299 | if not error: |
---|
300 | ticks = ndiv + 1 |
---|
301 | fvar = varname |
---|
302 | if anomaly: fvar = 'anomaly' |
---|
303 | if mapmode == 0: |
---|
304 | itime=indextime |
---|
305 | if len(what_I_plot.shape) is 3:itime=[0] |
---|
306 | what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ |
---|
307 | itime,what_I_plot, len(all_var[index_f].shape),vertmode) |
---|
308 | zxmin, zxmax = xaxis ; zymin, zymax = yaxis |
---|
309 | if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) |
---|
310 | if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax) |
---|
311 | if zymin is not None: mpl.pyplot.ylim(ymin=zymin) |
---|
312 | if zymax is not None: mpl.pyplot.ylim(ymax=zymax) |
---|
313 | if invert_y: lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima) |
---|
314 | if ylog: mpl.pyplot.semilogy() |
---|
315 | |
---|
316 | if (fileref is not None) and (index_f is numplot-1): zevmin, zevmax = calculate_bounds(what_I_plot,vmin=minop,vmax=maxop) |
---|
317 | else: zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax) |
---|
318 | if colorb in ["def","nobar"]: palette = get_cmap(name=defcolorb(fvar.upper())) |
---|
319 | elif (fileref is not None) and (index_f is numplot-1): palette = get_cmap(name="RdBu_r") |
---|
320 | else: palette = get_cmap(name=colorb) |
---|
321 | |
---|
322 | ##### simple 2D field and movies of 2D fields |
---|
323 | if len(what_I_plot.shape) >= 2: |
---|
324 | if (len(what_I_plot.shape) is 3 and mrate is None): errormess("3D field. Use --rate RATE for movie or specify --time TIME. Exit.") |
---|
325 | if mrate is not None: iend=len(time)-1 |
---|
326 | else: iend=0 |
---|
327 | imov = 0 |
---|
328 | if var2: which = "contour" ## have to start with contours rather than shading |
---|
329 | else: which = "regular" |
---|
330 | while imov <= iend: |
---|
331 | print "-> frame ",imov+1, which |
---|
332 | if which == "regular": |
---|
333 | if mrate is None: what_I_plot_frame = what_I_plot |
---|
334 | else: what_I_plot_frame = what_I_plot[imov,:,:] |
---|
335 | if winds: |
---|
336 | if mrate is None: vecx_frame = vecx ; vecy_frame = vecy |
---|
337 | else: vecx_frame = vecx[imov,:,:] ; vecy_frame = vecy[imov,:,:] |
---|
338 | elif which == "contour": |
---|
339 | if mrate is None or what_I_plot_contour.ndim < 3: what_I_plot_frame = what_I_plot_contour |
---|
340 | else: what_I_plot_frame = what_I_plot_contour[imov,:,:] |
---|
341 | if mrate is not None: |
---|
342 | if mapmode == 1: |
---|
343 | m = define_proj(proj,wlon,wlat,back=back,blat=blat,blon=blon) ## this is dirty, defined above but out of imov loop |
---|
344 | x, y = m(lon2d, lat2d) ## this is dirty, defined above but out of imov loop |
---|
345 | if typefile in ['mesoapi','meso'] and mapmode == 1: what_I_plot_frame = dumpbdy(what_I_plot_frame,6,condition=True) |
---|
346 | if typefile in ['mesoideal']: what_I_plot_frame = dumpbdy(what_I_plot_frame,0,stag='W',condition=dumped_vert_stag) |
---|
347 | |
---|
348 | if imov >= 0: |
---|
349 | # Renew axis directives for movie frames which are not the first one. |
---|
350 | zxmin, zxmax = xaxis ; zymin, zymax = yaxis |
---|
351 | if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) |
---|
352 | if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax) |
---|
353 | if zymin is not None: mpl.pyplot.ylim(ymin=zymin) |
---|
354 | if zymax is not None: mpl.pyplot.ylim(ymax=zymax) |
---|
355 | if invert_y: lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima) |
---|
356 | if ylog: mpl.pyplot.semilogy() |
---|
357 | |
---|
358 | if which == "regular": |
---|
359 | if hole: what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax) |
---|
360 | else: what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax) |
---|
361 | if flagnolow: what_I_plot_frame = nolow(what_I_plot_frame) |
---|
362 | if not tile: |
---|
363 | #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) |
---|
364 | zelevels = np.linspace(zevmin,zevmax,num=ticks) |
---|
365 | if mapmode == 1: m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette) |
---|
366 | elif mapmode == 0: contourf( x, y, what_I_plot_frame, zelevels, cmap = palette) |
---|
367 | else: |
---|
368 | if mapmode == 1: m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax ) |
---|
369 | elif mapmode == 0: pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax ) |
---|
370 | if colorb != 'nobar': |
---|
371 | if (fileref is not None) and (index_f is numplot-1): daformat = "%.3f" |
---|
372 | else: daformat = fmtvar(fvar.upper()) |
---|
373 | colorbar( fraction=0.05,pad=0.03,format=daformat,\ |
---|
374 | ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),extend='neither',spacing='proportional' ) |
---|
375 | if winds: |
---|
376 | if typefile in ['mesoapi','meso']: |
---|
377 | [vecx_frame,vecy_frame] = [dumpbdy(vecx_frame,6,stag=uchar,condition=True), dumpbdy(vecy_frame,6,stag=vchar,condition=True)] |
---|
378 | key = True |
---|
379 | elif typefile in ['gcm']: |
---|
380 | key = False |
---|
381 | if metwind: [vecx_frame,vecy_frame] = m.rotate_vector(vecx_frame, vecy_frame, lon2d, lat2d) |
---|
382 | if var: colorvec = definecolorvec(back) |
---|
383 | else: colorvec = definecolorvec(colorb) |
---|
384 | vectorfield(vecx_frame, vecy_frame, x, y, stride=stride, csmooth=2,\ |
---|
385 | #scale=15., factor=300., color=colorvec, key=key) |
---|
386 | scale=20., factor=250., color=colorvec, key=key) |
---|
387 | #200. ## or csmooth=stride |
---|
388 | elif which == "contour": |
---|
389 | zevminc, zevmaxc = calculate_bounds(what_I_plot_frame) |
---|
390 | zelevels = np.linspace(zevminc,zevmaxc,ticks/2) #20) |
---|
391 | if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.) |
---|
392 | if mapmode == 0: |
---|
393 | what_I_plot_frame, x, y = define_axis( lon,lat,vert,time,indexlon,indexlat,indexvert,\ |
---|
394 | itime,what_I_plot_frame, len(all_var2[index_f].shape),vertmode ) |
---|
395 | cs = contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5) |
---|
396 | elif mapmode == 1: cs = m.contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5) |
---|
397 | |
---|
398 | |
---|
399 | if which == "regular": |
---|
400 | if mrate is not None: |
---|
401 | ### THIS IS A MENCODER MOVIE |
---|
402 | if mrate > 0: |
---|
403 | figframe=mpl.pyplot.gcf() |
---|
404 | if mquality: figframe.set_dpi(600.) |
---|
405 | else: figframe.set_dpi(200.) |
---|
406 | mframe=fig2img(figframe) |
---|
407 | if imov == 0: |
---|
408 | moviename='movie' ;W,H = figframe.canvas.get_width_height() |
---|
409 | video = VideoSink((H,W), moviename, rate=mrate, byteorder="rgba") |
---|
410 | video.run(mframe) ; close() |
---|
411 | if imov == iend: video.close() |
---|
412 | ### THIS IS A WEBPAGE MOVIE |
---|
413 | else: |
---|
414 | nameframe = "image"+str(1000+imov) |
---|
415 | makeplotres(nameframe,res=100.,disp=False) ; close() |
---|
416 | if imov == 0: myfile = open("zepics", 'w') |
---|
417 | myfile.write("modImages["+str(imov)+"] = '"+nameframe+"_100.png';"+ '\n') |
---|
418 | if imov == iend: |
---|
419 | myfile.write("first_image = 0;"+ '\n') |
---|
420 | myfile.write("last_image = "+str(iend)+";"+ '\n') |
---|
421 | myfile.close() |
---|
422 | if var2: which = "contour" |
---|
423 | imov = imov+1 |
---|
424 | elif which == "contour": |
---|
425 | which = "regular" |
---|
426 | |
---|
427 | ##### 1D field |
---|
428 | elif len(what_I_plot.shape) is 1: |
---|
429 | plot(x,what_I_plot) |
---|
430 | if save == 'txt': writeascii(np.transpose(what_I_plot),'profile'+str(nplot)+'.txt') |
---|
431 | |
---|
432 | #### Other cases: (maybe plot 3-D field one day ??) |
---|
433 | else: |
---|
434 | print "WARNING!!! ",len(what_I_plot.shape),"-D PLOT NOT SUPPORTED !!! dimensions: ",what_I_plot.shape |
---|
435 | errormess("Are you sure you did not forget to prescribe a dimension ?") |
---|
436 | else: |
---|
437 | errormess("There is an error in reducing field !") |
---|
438 | |
---|
439 | ### Next subplot |
---|
440 | basename = getname(var=varname,winds=winds,anomaly=anomaly) |
---|
441 | basename = basename + getstralt(nc,level) |
---|
442 | if mrate is not None: basename = "movie_" + basename |
---|
443 | if typefile in ['mesoapi','meso']: |
---|
444 | if slon is not None: basename = basename + "_lon_" + str(int(lon2d[indices[1],indices[0]])) |
---|
445 | if slat is not None: basename = basename + "_lat_" + str(int(lat2d[indices[1],indices[0]])) |
---|
446 | plottitle = basename |
---|
447 | if addchar: |
---|
448 | [addchar,gogol,gogol2] = getlschar ( all_namefile[index_f] ) |
---|
449 | plottitle = plottitle + addchar + "_LT" |
---|
450 | else: plottitle = plottitle + "_LT" |
---|
451 | if ltst is not None: plottitle = plottitle + str(ltst) |
---|
452 | else: |
---|
453 | if fileref is not None: |
---|
454 | if index_f is numplot-1: plottitle = basename+' '+"fig(1) "+ope+" fig(2)" |
---|
455 | elif index_f is numplot-2: plottitle = basename+' '+fileref |
---|
456 | else: plottitle = basename+' '+namefiles[0]#index_f] |
---|
457 | else: plottitle = basename+' '+namefiles[0]#index_f] |
---|
458 | if mult != 1: plottitle = str(mult) + "*" + plottitle |
---|
459 | if zetitle != "fill": |
---|
460 | plottitle = zetitle |
---|
461 | if titleref is "fill": titleref=zetitle |
---|
462 | if fileref is not None: |
---|
463 | if index_f is numplot-2: plottitle = titleref |
---|
464 | if index_f is numplot-1: plottitle = "fig(1) "+ope+" fig(2)" |
---|
465 | # if indexlon is not None: plottitle = plottitle + " lon: " + str(min(lon[indexlon])) +" "+ str(max(lon[indexlon])) |
---|
466 | # if indexlat is not None: plottitle = plottitle + " lat: " + str(min(lat[indexlat])) +" "+ str(max(lat[indexlat])) |
---|
467 | # if indexvert is not None: plottitle = plottitle + " vert: " + str(min(vert[indexvert])) +" "+ str(max(vert[indexvert])) |
---|
468 | # if indextime is not None: plottitle = plottitle + " time: " + str(min(time[indextime])) +" "+ str(max(time[indextime])) |
---|
469 | title( plottitle ) |
---|
470 | if nplot >= numplot: error = True |
---|
471 | nplot += 1 |
---|
472 | |
---|
473 | |
---|
474 | |
---|
475 | |
---|
476 | |
---|
477 | |
---|
478 | |
---|
479 | |
---|
480 | ########################################################################## |
---|
481 | ### Save the figure in a file in the data folder or an user-defined folder |
---|
482 | if outputname is None: |
---|
483 | if typefile in ['meso','mesoapi']: prefix = getprefix(nc) |
---|
484 | elif typefile in ['gcm']: prefix = 'LMD_GCM_' |
---|
485 | else: prefix = '' |
---|
486 | ### |
---|
487 | zeplot = prefix + basename |
---|
488 | if addchar: zeplot = zeplot + addchar |
---|
489 | if numplot <= 0: zeplot = zeplot + "_LT"+str(abs(numplot)) |
---|
490 | ### |
---|
491 | if not target: zeplot = namefile[0:find(namefile,'wrfout')] + zeplot |
---|
492 | else: zeplot = target + "/" + zeplot |
---|
493 | ### |
---|
494 | else: |
---|
495 | zeplot=outputname |
---|
496 | |
---|
497 | if mrate is None: |
---|
498 | pad_inches_value = 0.35 |
---|
499 | print "********** SAVE ", save |
---|
500 | if save == 'png': |
---|
501 | if display: makeplotres(zeplot,res=100.,pad_inches_value=pad_inches_value) #,erase=True) ## a miniature |
---|
502 | makeplotres(zeplot,res=resolution,pad_inches_value=pad_inches_value,disp=False) |
---|
503 | elif save in ['eps','svg','pdf']: makeplotres(zeplot,pad_inches_value=pad_inches_value,disp=False,ext=save) |
---|
504 | elif save == 'gui': show() |
---|
505 | elif save == 'txt': print "Saved results in txt file." |
---|
506 | else: |
---|
507 | print "INFO: save mode not supported. using gui instead." |
---|
508 | show() |
---|
509 | |
---|
510 | ################################### |
---|
511 | #### Getting more out of this video -- PROBLEMS WITH CREATED VIDEOS |
---|
512 | # |
---|
513 | #if mrate is not None: |
---|
514 | # print "Re-encoding movie.. first pass" |
---|
515 | # video.first_pass(filename=moviename,quality=mquality,rate=mrate) |
---|
516 | # print "Re-encoding movie.. second pass" |
---|
517 | # video.second_pass(filename=moviename,quality=mquality,rate=mrate) |
---|
518 | |
---|
519 | ############### |
---|
520 | ### Now the end |
---|
521 | return zeplot |
---|