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 | |
---|
8 | def planetoplot (namefiles,\ |
---|
9 | vertmode,\ |
---|
10 | proj=None,\ |
---|
11 | back=None,\ |
---|
12 | target=None, |
---|
13 | stride=3,\ |
---|
14 | numplot=2,\ |
---|
15 | var=None,\ |
---|
16 | colorb="def",\ |
---|
17 | winds=True,\ |
---|
18 | addchar=None,\ |
---|
19 | interv=[0,1],\ |
---|
20 | vmin=None,\ |
---|
21 | vmax=None,\ |
---|
22 | tile=False,\ |
---|
23 | zoom=None,\ |
---|
24 | display=True,\ |
---|
25 | itstep=None,\ |
---|
26 | hole=False,\ |
---|
27 | save="gui",\ |
---|
28 | anomaly=False,\ |
---|
29 | var2=None,\ |
---|
30 | ndiv=10,\ |
---|
31 | first=1,\ |
---|
32 | mult=1.,\ |
---|
33 | zetitle="fill",\ |
---|
34 | slon=None,\ |
---|
35 | slat=None,\ |
---|
36 | svert=None,\ |
---|
37 | stime=None): |
---|
38 | |
---|
39 | #################################################################################################################### |
---|
40 | ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png |
---|
41 | |
---|
42 | ################################# |
---|
43 | ### Load librairies and functions |
---|
44 | from netCDF4 import Dataset |
---|
45 | from myplot import getcoord2d,define_proj,makeplotres,simplinterv,vectorfield,ptitle,latinterv,getproj,wrfinterv,dumpbdy,\ |
---|
46 | fmtvar,definecolorvec,defcolorb,getprefix,putpoints,calculate_bounds,errormess,definesubplot,\ |
---|
47 | zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ |
---|
48 | getname,localtime,polarinterv,getsindex,define_axis,determineplot |
---|
49 | from mymath import deg,max,min,mean |
---|
50 | from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel |
---|
51 | from matplotlib.cm import get_cmap |
---|
52 | import numpy as np |
---|
53 | from numpy.core.defchararray import find |
---|
54 | |
---|
55 | ################################ |
---|
56 | ### Which plot needs to be done? |
---|
57 | nlon, nlat, nvert, ntime, mapmode, nslices = determineplot(slon, slat, svert, stime) |
---|
58 | if mapmode == 0: winds=False |
---|
59 | if not isinstance(namefiles, np.ndarray): namefiles = [namefiles] |
---|
60 | zelen = len(namefiles) |
---|
61 | numplot = zelen*nslices |
---|
62 | print "len(namefiles), nslices, numplot: ", zelen, nslices, numplot |
---|
63 | all_var = [[]]*zelen |
---|
64 | all_var2 = [[]]*zelen |
---|
65 | all_title = [[]]*zelen |
---|
66 | |
---|
67 | ######################### |
---|
68 | ### Loop over the files initially separated by comas to be plotted on the same figure |
---|
69 | k = 0 |
---|
70 | firstfile = True |
---|
71 | for namefile in namefiles: |
---|
72 | |
---|
73 | ###################### |
---|
74 | ### Load NETCDF object |
---|
75 | nc = Dataset(namefile) |
---|
76 | |
---|
77 | ################################## |
---|
78 | ### Initial checks and definitions |
---|
79 | ### ... TYPEFILE |
---|
80 | typefile = whatkindfile(nc) |
---|
81 | if firstfile: |
---|
82 | typefile0 = typefile |
---|
83 | elif typefile != typefile0: |
---|
84 | errormess("Not the same kind of files !", [typefile0, typefile]) |
---|
85 | ### ... VAR |
---|
86 | if var not in nc.variables: var = False |
---|
87 | ### ... WINDS |
---|
88 | if winds: |
---|
89 | [uchar,vchar,metwind] = getwinddef(nc) |
---|
90 | if uchar == 'not found': winds = False |
---|
91 | if not var and not winds: errormess("please set at least winds or var",printvar=nc.variables) |
---|
92 | ### ... COORDINATES, could be moved below |
---|
93 | [lon2d,lat2d] = getcoorddef(nc) |
---|
94 | ### ... PROJECTION |
---|
95 | if proj == None: proj = getproj(nc) |
---|
96 | |
---|
97 | ########################################################## |
---|
98 | if typefile == "gcm": |
---|
99 | lat = nc.variables["latitude"][:] |
---|
100 | lon = nc.variables["longitude"][:] |
---|
101 | time = nc.variables["Time"][:] |
---|
102 | vert = nc.variables["altitude"][:] |
---|
103 | #if firstfile: |
---|
104 | # lat0 = lat |
---|
105 | #elif len(lat0) != len(lat): |
---|
106 | # errormess("Not the same latitude lengths !", [len(lat0), len(lat)]) |
---|
107 | #elif sum((lat == lat0) == False) != 0: |
---|
108 | # errormess("Not the same latitudes !", [lat,lat0]) |
---|
109 | ## Faire d'autre checks sur les compatibilites entre fichiers!! |
---|
110 | ########################################################## |
---|
111 | |
---|
112 | if firstfile: |
---|
113 | ########################## |
---|
114 | ### Define plot boundaries |
---|
115 | ### todo: possible areas in latinterv in argument (ex: "Far_South_Pole") |
---|
116 | if proj in ["npstere","spstere"]: [wlon,wlat] = polarinterv(lon2d,lat2d) |
---|
117 | elif proj in ["lcc","laea"]: [wlon,wlat] = wrfinterv(lon2d,lat2d) |
---|
118 | else: [wlon,wlat] = simplinterv(lon2d,lat2d) |
---|
119 | if zoom: [wlon,wlat] = zoomset(wlon,wlat,zoom) |
---|
120 | |
---|
121 | ######################################### |
---|
122 | ### Name for title and graphics save file |
---|
123 | basename = getname(var=var,winds=winds,anomaly=anomaly) |
---|
124 | basename = basename + getstralt(nc,vertmode) ## can be moved elsewhere for a more generic routine |
---|
125 | |
---|
126 | print "var, var2: ", var, var2 |
---|
127 | if var: all_var[k] = getfield(nc,var) |
---|
128 | if var2: all_var2[k] = getfield(nc,var2) |
---|
129 | |
---|
130 | print "k", k |
---|
131 | print "all_var[k].shape", all_var[k].shape |
---|
132 | k += 1 |
---|
133 | firstfile = False |
---|
134 | #### End of for namefile in namefiles |
---|
135 | |
---|
136 | |
---|
137 | ################################## |
---|
138 | ### Open a figure and set subplots |
---|
139 | fig = figure() |
---|
140 | subv,subh = definesubplot( numplot, fig ) |
---|
141 | |
---|
142 | ################################# |
---|
143 | ### Time loop for plotting device |
---|
144 | found_lct = False |
---|
145 | nplot = 1 |
---|
146 | itime = first |
---|
147 | error = False |
---|
148 | if itstep is None and numplot > 0: itstep = int(24./numplot) |
---|
149 | elif numplot <= 0: itstep = 1 |
---|
150 | |
---|
151 | #for nplot in range(numplot): |
---|
152 | while error is False: |
---|
153 | print "nplot", nplot |
---|
154 | print error |
---|
155 | |
---|
156 | ### Which local time ? |
---|
157 | ltst = localtime ( interv[0]+itime*interv[1], 0.5*(wlon[0]+wlon[1]) ) |
---|
158 | |
---|
159 | ### General plot settings |
---|
160 | #print itime, int(ltst), numplot, nplot |
---|
161 | if numplot >= 1: |
---|
162 | if nplot > numplot: break |
---|
163 | if numplot > 1: |
---|
164 | if typefile not in ['geo']: subplot(subv,subh,nplot) |
---|
165 | found_lct = True |
---|
166 | ### If only one local time is requested (numplot < 0) |
---|
167 | elif numplot <= 0: |
---|
168 | if int(ltst) + numplot != 0: |
---|
169 | itime += 1 |
---|
170 | if found_lct is True: break ## because it means LT was found at previous iteration |
---|
171 | else: continue ## continue to iterate to find the correct LT |
---|
172 | else: |
---|
173 | found_lct = True |
---|
174 | |
---|
175 | ### Map projection |
---|
176 | if mapmode == 1: |
---|
177 | m = define_proj(proj,wlon,wlat,back=back) |
---|
178 | x, y = m(lon2d, lat2d) |
---|
179 | |
---|
180 | #################################################################### |
---|
181 | if typefile in ['mesoapi','meso']: |
---|
182 | indexlon = None |
---|
183 | indexlat = None |
---|
184 | indexvert = vertmode |
---|
185 | indextime = itime |
---|
186 | nlon = 1 |
---|
187 | nlat = 1 |
---|
188 | nvert = 1 |
---|
189 | ntime = 1 |
---|
190 | index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%len(namefiles) |
---|
191 | print "index lon,lat,vert,time", max(indexlon), max(indexlat), max(indexvert), max(indextime) |
---|
192 | elif typefile in ['gcm']: |
---|
193 | ## get all indexes to be taken into account for this subplot and then reduce field |
---|
194 | ## 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 |
---|
195 | #print "(nplot-1)%nlon", (nplot-1)%nlon |
---|
196 | indexlon = getsindex(slon,(nplot-1)%nlon,lon) |
---|
197 | print "indexlon", indexlon |
---|
198 | indexlat = getsindex(slat,((nplot-1)//nlon)%nlat,lat) |
---|
199 | indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert) |
---|
200 | indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) |
---|
201 | index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%len(namefiles) |
---|
202 | #print "index lon,lat,vert,time", max(indexlon), max(indexlat), max(indexvert), max(indextime) |
---|
203 | #################################################################### |
---|
204 | |
---|
205 | #### Contour plot |
---|
206 | if var2: |
---|
207 | what_I_plot, error = reducefield(all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert ) |
---|
208 | #what_I_plot = what_I_plot*mult |
---|
209 | if not error: |
---|
210 | if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6) |
---|
211 | zevmin, zevmax = calculate_bounds(what_I_plot) |
---|
212 | zelevels = np.linspace(zevmin,zevmax,num=ndiv+1) #20) |
---|
213 | if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.) |
---|
214 | if mapmode == 0: |
---|
215 | x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ |
---|
216 | indextime,what_I_plot.shape, len(all_var[index_f].shape),vertmode) |
---|
217 | ### If we plot a 2-D field |
---|
218 | if len(what_I_plot.shape) is 2: |
---|
219 | cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5) |
---|
220 | if typefile in ['gcm']: clabel(cs,fmt = '%1.2e') |
---|
221 | ### If we plot a 1-D field |
---|
222 | elif len(what_I_plot.shape) is 1: |
---|
223 | plot(x,y,what_I_plot, zelevels, colors='g', linewidths = 0.33 ) #colors='w' )# , alpha=0.5) |
---|
224 | else: |
---|
225 | errormess("There is an error in reducing field !") |
---|
226 | |
---|
227 | #### Shaded plot |
---|
228 | if var: |
---|
229 | what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert ) |
---|
230 | what_I_plot = what_I_plot*mult |
---|
231 | |
---|
232 | if not error: |
---|
233 | fvar = var |
---|
234 | ### |
---|
235 | if anomaly: |
---|
236 | what_I_plot = 100. * ((what_I_plot / smooth(what_I_plot,12)) - 1.) |
---|
237 | fvar = 'anomaly' |
---|
238 | #if mult != 1: |
---|
239 | # fvar = str(mult) + "*" + var |
---|
240 | ### |
---|
241 | if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6) |
---|
242 | zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax) |
---|
243 | if colorb in ["def","nobar"]: palette = get_cmap(name=defcolorb(fvar.upper())) |
---|
244 | else: palette = get_cmap(name=colorb) |
---|
245 | ### If we plot a 2-D field |
---|
246 | if len(what_I_plot.shape) is 2: |
---|
247 | if mapmode == 0: |
---|
248 | what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ |
---|
249 | indextime,what_I_plot, len(all_var[index_f].shape),vertmode) |
---|
250 | if not tile: |
---|
251 | if not hole: what_I_plot = bounds(what_I_plot,zevmin,zevmax) |
---|
252 | #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) |
---|
253 | print "what_I_plot.shape", what_I_plot.shape |
---|
254 | print "x.shape, y.shape", x.shape, y.shape |
---|
255 | zelevels = np.linspace(zevmin,zevmax,num=ndiv+1) |
---|
256 | #contourf(what_I_plot, zelevels, cmap = palette ) |
---|
257 | contourf(x,y,what_I_plot, zelevels, cmap = palette ) |
---|
258 | else: |
---|
259 | if hole: what_I_plot = nolow(what_I_plot) |
---|
260 | pcolor(x,y,what_I_plot, cmap = palette, \ |
---|
261 | vmin=zevmin, vmax=zevmax ) |
---|
262 | if colorb != 'nobar' and var != 'HGT' : |
---|
263 | #subplot(111) |
---|
264 | #colorbar() |
---|
265 | colorbar(fraction=0.05,pad=0.03,format=fmtvar(fvar.upper()),\ |
---|
266 | ticks=np.linspace(zevmin,zevmax,min([ndiv+1,20])),\ |
---|
267 | extend='neither',spacing='proportional') |
---|
268 | # both min max neither |
---|
269 | ### If we plot a 1-D field |
---|
270 | elif len(what_I_plot.shape) is 1: |
---|
271 | what_I_plot,x,y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ |
---|
272 | indextime,what_I_plot,len(all_var[index_f].shape),vertmode) |
---|
273 | # x = np.array(x) |
---|
274 | # y = np.array(y) |
---|
275 | print "what_I_plot.shape", what_I_plot.shape |
---|
276 | print "x.shape", x.shape |
---|
277 | plot(x,what_I_plot) |
---|
278 | ### If we plot something that is not a 2-D or 1-D field |
---|
279 | ### (maybe plot 3-D field one day or movie ??) |
---|
280 | else: |
---|
281 | print "WARNING!!! ",len(what_I_plot.shape),"-D PLOT NOT SUPPORTED !!!" |
---|
282 | print "field dimensions: ", what_I_plot.shape |
---|
283 | exit() |
---|
284 | |
---|
285 | else: |
---|
286 | errormess("There is an error in reducing field !") |
---|
287 | |
---|
288 | ### Vector plot --- a adapter |
---|
289 | if winds: |
---|
290 | vecx, error = reducefield( getfield(nc,uchar), d4=indextime, d3=indexvert ) |
---|
291 | vecy, error = reducefield( getfield(nc,vchar), d4=indextime, d3=indexvert ) |
---|
292 | #what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert ) |
---|
293 | if not error: |
---|
294 | if typefile in ['mesoapi','meso']: |
---|
295 | [vecx,vecy] = [dumpbdy(vecx,6,stag=uchar), dumpbdy(vecy,6,stag=vchar)] |
---|
296 | key = True |
---|
297 | elif typefile in ['gcm']: |
---|
298 | key = False |
---|
299 | if metwind: [vecx,vecy] = m.rotate_vector(vecx, vecy, lon2d, lat2d) |
---|
300 | if var == False: colorvec = definecolorvec(back) |
---|
301 | else: colorvec = definecolorvec(colorb) |
---|
302 | vectorfield(vecx, vecy,\ |
---|
303 | x, y, stride=stride, csmooth=2,\ |
---|
304 | #scale=15., factor=300., color=colorvec, key=key) |
---|
305 | scale=20., factor=250., color=colorvec, key=key) |
---|
306 | #200. ## or csmooth=stride |
---|
307 | |
---|
308 | ### Next subplot |
---|
309 | if typefile in ['mesoapi','meso']: |
---|
310 | plottitle = basename |
---|
311 | if addchar: plottitle = plottitle + addchar + "_LT"+str(ltst) |
---|
312 | else: plottitle = plottitle + "_LT"+str(ltst) |
---|
313 | else: |
---|
314 | plottitle = basename+' '+namefiles[index_f] |
---|
315 | if mult != 1: plottitle = str(mult) + "*" + plottitle |
---|
316 | if zetitle != "fill": plottitle = zetitle |
---|
317 | # if indexlon is not None: |
---|
318 | # plottitle = plottitle + " lon: " + str(min(lon[indexlon])) +" "+ str(max(lon[indexlon])) |
---|
319 | # if indexlat is not None: |
---|
320 | # plottitle = plottitle + " lat: " + str(min(lat[indexlat])) +" "+ str(max(lat[indexlat])) |
---|
321 | # if indexvert is not None: |
---|
322 | # plottitle = plottitle + " vert: " + str(min(vert[indexvert])) +" "+ str(max(vert[indexvert])) |
---|
323 | # if indextime is not None: |
---|
324 | # plottitle = plottitle + " time: " + str(min(time[indextime])) +" "+ str(max(time[indextime])) |
---|
325 | ptitle( plottitle ) |
---|
326 | itime += itstep |
---|
327 | if nplot >= numplot: |
---|
328 | error = True |
---|
329 | nplot += 1 |
---|
330 | |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | ########################################################################## |
---|
339 | ### Save the figure in a file in the data folder or an user-defined folder |
---|
340 | if typefile in ['meso','mesoapi']: prefix = getprefix(nc) |
---|
341 | elif typefile in ['gcm']: prefix = 'LMD_GCM_' |
---|
342 | else: prefix = '' |
---|
343 | ### |
---|
344 | zeplot = prefix + basename |
---|
345 | if addchar: zeplot = zeplot + addchar |
---|
346 | if numplot <= 0: zeplot = zeplot + "_LT"+str(abs(numplot)) |
---|
347 | ### |
---|
348 | if not target: zeplot = namefile[0:find(namefile,'wrfout')] + zeplot |
---|
349 | else: zeplot = target + "/" + zeplot |
---|
350 | ### |
---|
351 | if found_lct: |
---|
352 | pad_inches_value = 0.35 |
---|
353 | print "save", save |
---|
354 | if save == 'png': |
---|
355 | if display: makeplotres(zeplot,res=100.,pad_inches_value=pad_inches_value) #,erase=True) ## a miniature |
---|
356 | makeplotres(zeplot,res=200.,pad_inches_value=pad_inches_value,disp=False) |
---|
357 | elif save in ['eps','svg','pdf']: |
---|
358 | makeplotres(zeplot, pad_inches_value=pad_inches_value,disp=False,ext=save) |
---|
359 | elif save == 'gui': |
---|
360 | show() |
---|
361 | else: |
---|
362 | print "save mode not supported. using gui instead." |
---|
363 | show() |
---|
364 | else: print "Local time not found" |
---|
365 | |
---|
366 | ############### |
---|
367 | ### Now the end |
---|
368 | return zeplot |
---|