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