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