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