[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] |
---|
[475] | 10 | ### A. Spiga -- LMD -- 12/2011 -- Added HTML animated page capability + general tests of consistency [winds, etc...] + consistent generic movie loop |
---|
[525] | 11 | ### J. Leconte -- LMD -- 02/2012 -- Added area weighted averaging. Compatibility with terrestrial gcm. |
---|
[579] | 12 | ### A. Spiga -- LMD -- 03/2012 -- Cleaning and improved comments |
---|
[345] | 13 | def planetoplot (namefiles,\ |
---|
[351] | 14 | level=0,\ |
---|
[350] | 15 | vertmode=0,\ |
---|
[345] | 16 | proj=None,\ |
---|
| 17 | back=None,\ |
---|
| 18 | target=None, |
---|
| 19 | stride=3,\ |
---|
| 20 | var=None,\ |
---|
| 21 | colorb="def",\ |
---|
[399] | 22 | winds=False,\ |
---|
[345] | 23 | addchar=None,\ |
---|
| 24 | interv=[0,1],\ |
---|
| 25 | vmin=None,\ |
---|
| 26 | vmax=None,\ |
---|
| 27 | tile=False,\ |
---|
| 28 | zoom=None,\ |
---|
| 29 | display=True,\ |
---|
| 30 | hole=False,\ |
---|
| 31 | save="gui",\ |
---|
| 32 | anomaly=False,\ |
---|
| 33 | var2=None,\ |
---|
| 34 | ndiv=10,\ |
---|
| 35 | mult=1.,\ |
---|
[578] | 36 | zetitle=["fill"],\ |
---|
[345] | 37 | slon=None,\ |
---|
| 38 | slat=None,\ |
---|
| 39 | svert=None,\ |
---|
[359] | 40 | stime=None,\ |
---|
| 41 | outputname=None,\ |
---|
| 42 | resolution=200,\ |
---|
| 43 | ope=None,\ |
---|
| 44 | fileref=None,\ |
---|
| 45 | minop=0.,\ |
---|
| 46 | maxop=0.,\ |
---|
[363] | 47 | titleref="fill",\ |
---|
[369] | 48 | invert_y=False,\ |
---|
| 49 | xaxis=[None,None],\ |
---|
[372] | 50 | yaxis=[None,None],\ |
---|
[382] | 51 | ylog=False,\ |
---|
[385] | 52 | yintegral=False,\ |
---|
[388] | 53 | blat=None,\ |
---|
[451] | 54 | blon=None,\ |
---|
[418] | 55 | tsat=False,\ |
---|
[430] | 56 | flagnolow=False,\ |
---|
[444] | 57 | mrate=None,\ |
---|
[453] | 58 | mquality=False,\ |
---|
[457] | 59 | trans=1,\ |
---|
[468] | 60 | zarea=None,\ |
---|
[483] | 61 | axtime=None,\ |
---|
[502] | 62 | redope=None,\ |
---|
[507] | 63 | seevar=False,\ |
---|
[502] | 64 | xlab=None,\ |
---|
[569] | 65 | ylab=None,\ |
---|
[612] | 66 | lbls=None,\ |
---|
| 67 | lstyle=None,\ |
---|
| 68 | cross=None): |
---|
[345] | 69 | |
---|
| 70 | #################################################################################################################### |
---|
| 71 | ### Colorbars http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png |
---|
| 72 | |
---|
| 73 | ################################# |
---|
| 74 | ### Load librairies and functions |
---|
| 75 | from netCDF4 import Dataset |
---|
| 76 | from myplot import getcoord2d,define_proj,makeplotres,simplinterv,vectorfield,ptitle,latinterv,getproj,wrfinterv,dumpbdy,\ |
---|
| 77 | fmtvar,definecolorvec,defcolorb,getprefix,putpoints,calculate_bounds,errormess,definesubplot,\ |
---|
| 78 | zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ |
---|
[612] | 79 | getname,localtime,check_localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds,\ |
---|
| 80 | windamplitude,slopeamplitude,deltat0t1 |
---|
[430] | 81 | from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img |
---|
[363] | 82 | import matplotlib as mpl |
---|
[637] | 83 | from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel, title, close, legend, xlabel, axis, ylabel, subplots_adjust, axes, clabel |
---|
[345] | 84 | from matplotlib.cm import get_cmap |
---|
[490] | 85 | #from mpl_toolkits.basemap import cm |
---|
[345] | 86 | import numpy as np |
---|
| 87 | from numpy.core.defchararray import find |
---|
[430] | 88 | from videosink import VideoSink |
---|
[444] | 89 | import subprocess |
---|
[490] | 90 | #from singlet import singlet |
---|
[587] | 91 | from itertools import cycle |
---|
[359] | 92 | |
---|
[579] | 93 | ######################### |
---|
| 94 | ### PRELIMINARY STUFF ### |
---|
| 95 | ######################### |
---|
| 96 | ### we say hello |
---|
[392] | 97 | print "********************************************" |
---|
| 98 | print "********** WELCOME TO PLANETOPLOT **********" |
---|
| 99 | print "********************************************" |
---|
[579] | 100 | ### we ensure namefiles and var are arrays |
---|
[392] | 101 | if not isinstance(namefiles, np.ndarray): namefiles = [namefiles] |
---|
| 102 | if not isinstance(var, np.ndarray): var = [var] |
---|
[579] | 103 | ### we initialize a few variables |
---|
| 104 | initime=-1 ; sslon = None ; sslat = None |
---|
[582] | 105 | if slon is not None: sslon = np.zeros([1,2]) |
---|
| 106 | if slat is not None: sslat = np.zeros([1,2]) |
---|
[579] | 107 | k = 0 ; firstfile = True ; count = 0 |
---|
| 108 | ### we avoid specific cases not yet implemented |
---|
| 109 | if mrate is not None and len(var) > 1: errormess("multivar not allowed in movies. should be fixed soon!") |
---|
| 110 | ### we prepare number of plot fields [zelen] and plot instances [numplot] according to user choices |
---|
| 111 | ### --> we support multifile and multivar plots : files + vars separated by commas are on the same figure |
---|
[349] | 112 | nlon, nlat, nvert, ntime, mapmode, nslices = determineplot(slon, slat, svert, stime) |
---|
[392] | 113 | zelen = len(namefiles)*len(var) |
---|
[424] | 114 | numplot = zelen*nslices |
---|
[392] | 115 | print "********** FILES, SLICES, VARS, TOTAL PLOTS: ", len(namefiles), nslices, len(var), numplot |
---|
[579] | 116 | print "********** MAPMODE: ", mapmode |
---|
| 117 | ### we correct number of plot fields for possible operation (substract, etc...) |
---|
[423] | 118 | if ope is not None: |
---|
| 119 | if fileref is not None: zelen = zelen + 2 |
---|
| 120 | elif "var" in ope: zelen = zelen + 1 |
---|
[579] | 121 | ### we define the arrays for plot fields -- which will be placed within multiplot loops |
---|
| 122 | all_var = [[]]*zelen ; all_var2 = [[]]*zelen |
---|
| 123 | all_title = [[]]*zelen ; all_varname = [[]]*zelen ; all_namefile = [[]]*zelen ; all_time = [[]]*zelen |
---|
| 124 | all_windu = [[]]*zelen ; all_windv = [[]]*zelen |
---|
[392] | 125 | |
---|
[579] | 126 | ############################# |
---|
| 127 | ### LOOP OVER PLOT FIELDS ### |
---|
| 128 | ############################# |
---|
[392] | 129 | for nnn in range(len(namefiles)): |
---|
| 130 | for vvv in range(len(var)): |
---|
| 131 | |
---|
[579] | 132 | ### we load each NETCDF objects in namefiles |
---|
[468] | 133 | namefile = namefiles[nnn] |
---|
[345] | 134 | nc = Dataset(namefile) |
---|
[579] | 135 | ### we explore the variables in the file |
---|
[507] | 136 | varinfile = nc.variables.keys() |
---|
| 137 | if seevar: print varinfile ; exit() |
---|
[579] | 138 | ### we define the type of file we have (gcm, meso, etc...) |
---|
[507] | 139 | typefile = whatkindfile(nc) |
---|
[579] | 140 | if firstfile: typefile0 = typefile |
---|
| 141 | elif typefile != typefile0: errormess("Not the same kind of files !", [typefile0, typefile]) |
---|
| 142 | ### we care for input file being 1D simulations |
---|
[637] | 143 | if "longitude" in nc.dimensions and "latitude" in nc.dimensions: is1d = len(nc.variables["longitude"][:])*len(nc.variables["latitude"][:]) |
---|
| 144 | elif "lon" in nc.dimensions and "lat" in nc.dimensions: is1d = len(nc.variables["lon"][:])*len(nc.variables["lat"][:]) |
---|
| 145 | if typefile in ['gcm','earthgcm'] and is1d == 1: mapmode=0 ; winds=False |
---|
[579] | 146 | ### we create default vert and time prescriptions if not here in case mapping mode is on (lat/lon maps) |
---|
[548] | 147 | if redope is None and mapmode == 1: |
---|
[477] | 148 | if svert is None: svert = readslices(str(level)) ; nvert=1 |
---|
| 149 | if stime is None and mrate is None: |
---|
| 150 | stime = readslices(str(0)) ; ntime=1 ## this is a default choice |
---|
| 151 | print "WELL... nothing about time axis. I took default: first time reference stored in file." |
---|
[579] | 152 | ### we get the names of variables to be read. in case only one available, we choose this one. |
---|
[507] | 153 | varname=var[vvv] |
---|
[612] | 154 | if ((varname not in nc.variables) and not ((typefile in ['meso']) and (varname in ['UV','uv','uvmet','slopexy','SLOPEXY','deltat','DELTAT']))): |
---|
[507] | 155 | if len(varinfile) == 1: varname = varinfile[0] |
---|
| 156 | else: varname = False |
---|
[579] | 157 | ### we get the names of wind variables to be read (if any) |
---|
[349] | 158 | if winds: |
---|
[345] | 159 | [uchar,vchar,metwind] = getwinddef(nc) |
---|
| 160 | if uchar == 'not found': winds = False |
---|
[579] | 161 | ### we tell the user that either no var or no wind is not acceptable |
---|
[392] | 162 | if not varname and not winds: errormess("please set at least winds or var",printvar=nc.variables) |
---|
[579] | 163 | ### we get the coordinates lat/lon to be used |
---|
[399] | 164 | [lon2d,lat2d] = getcoorddef(nc) |
---|
[579] | 165 | ### we get an adapted map projection if none is provided by the user |
---|
[548] | 166 | if proj == None: proj = getproj(nc) |
---|
[579] | 167 | ### we define plot boundaries given projection or user choices |
---|
[489] | 168 | if firstfile: |
---|
| 169 | if proj in ["npstere","spstere"]: [wlon,wlat] = polarinterv(lon2d,lat2d) |
---|
| 170 | elif proj in ["lcc","laea"]: [wlon,wlat] = wrfinterv(lon2d,lat2d) |
---|
| 171 | else: [wlon,wlat] = simplinterv(lon2d,lat2d) |
---|
| 172 | if zoom: [wlon,wlat] = zoomset(wlon,wlat,zoom) |
---|
[558] | 173 | elif zarea is not None: [wlon,wlat] = latinterv(area=zarea) |
---|
[489] | 174 | |
---|
[490] | 175 | ########################################################## |
---|
| 176 | ############ LOAD 4D DIMENSIONS : x, y, z, t ############# |
---|
| 177 | ########################################################## |
---|
[558] | 178 | if typefile in ["gcm","earthgcm","ecmwf"]: |
---|
[610] | 179 | if slon is not None: sslon = slon |
---|
| 180 | if slat is not None: sslat = slat |
---|
[558] | 181 | ### LAT |
---|
| 182 | if "latitude" in nc.variables: lat = nc.variables["latitude"][:] |
---|
| 183 | elif "lat" in nc.variables: lat = nc.variables["lat"][:] |
---|
| 184 | ### LON |
---|
| 185 | if "longitude" in nc.variables: lon = nc.variables["longitude"][:] |
---|
| 186 | elif "lon" in nc.variables: lon = nc.variables["lon"][:] |
---|
| 187 | ### ALT |
---|
| 188 | if "altitude" in nc.variables: vert = nc.variables["altitude"][:] |
---|
| 189 | elif "Alt" in nc.variables: vert = nc.variables["Alt"][:] |
---|
| 190 | elif "lev" in nc.variables: vert = nc.variables["lev"][:] |
---|
| 191 | else: vert = [0.] |
---|
| 192 | ### AIRE (to weight means with the area) |
---|
| 193 | if "aire" in nc.variables: area = nc.variables["aire"][:,:] |
---|
[525] | 194 | else: area = None |
---|
| 195 | ### TIME |
---|
| 196 | if "Time" in nc.variables: time = nc.variables["Time"][:] |
---|
| 197 | elif "time" in nc.variables: time = nc.variables["time"][:] |
---|
| 198 | elif "time_counter" in nc.variables: time = nc.variables["time_counter"][:]/86400. #### time counter cinverstion from s-> days |
---|
| 199 | else: errormess("no time axis found.") |
---|
[468] | 200 | if axtime in ["ls","sol"]: errormess("not supported. should not be too difficult though.") |
---|
[494] | 201 | # for 1D plots (no need for longitude computation): |
---|
| 202 | if axtime in ["lt"]: |
---|
| 203 | if initime == -1: initime=input("Please type initial local time:") |
---|
| 204 | time = (initime+time*24)%24 |
---|
| 205 | print "LOCAL TIMES.... ", time |
---|
[548] | 206 | elif typefile in ['meso','geo']: |
---|
[525] | 207 | area = None ## not active for the moment |
---|
[490] | 208 | ###### STUFF TO GET THE CORRECT LAT/LON FROM MESOSCALE FILES WITH 2D LAT/LON ARRAYS |
---|
| 209 | ###### principle: calculate correct indices then repopulate slon and slat |
---|
| 210 | if slon is not None or slat is not None: |
---|
[612] | 211 | if firstfile and save == 'png' and typefile == 'meso' and "HGT" in varinfile and display: iwantawhereplot = nc #show a topo map with a cross on the chosen point |
---|
[562] | 212 | else: iwantawhereplot = None #do not show anything, just select indices |
---|
[490] | 213 | numlon = 1 ; numlat = 1 |
---|
[610] | 214 | if slon is not None: numlon = slon.shape[1] |
---|
| 215 | if slat is not None: numlat = slat.shape[1] |
---|
[490] | 216 | indices = np.ones([numlon,numlat,2]) ; vlon = None ; vlat = None |
---|
| 217 | for iii in range(numlon): |
---|
| 218 | for jjj in range(numlat): |
---|
[610] | 219 | if slon is not None: vlon = slon[0][iii] ### note: slon[:][0] does not work |
---|
| 220 | if slat is not None: vlat = slat[0][jjj] ### note: slon[:][0] does not work |
---|
[490] | 221 | indices[iii,jjj,:] = bidimfind(lon2d,lat2d,vlon,vlat,file=iwantawhereplot) |
---|
| 222 | lonp,latp = ( lon2d[indices[iii,jjj,0],indices[iii,jjj,1]] , lat2d[indices[iii,jjj,0],indices[iii,jjj,1]] ) |
---|
[587] | 223 | ### possible bug here if several --lat |
---|
[490] | 224 | for iii in range(numlon): |
---|
| 225 | for jjj in range(numlat): |
---|
[610] | 226 | if slon is not None: sslon[0][iii] = indices[iii,0,1] #...this is idx |
---|
| 227 | if slat is not None: sslat[0][jjj] = indices[0,jjj,0] #...this is idy |
---|
[490] | 228 | lonp,latp = ( lon2d[indices[0,0,0],indices[0,0,1]] , lat2d[indices[0,0,0],indices[0,0,1]] ) |
---|
| 229 | ###### |
---|
[548] | 230 | if typefile in ['meso'] and mapmode == 1: lon2d = dumpbdy(lon2d,6) ; lat2d = dumpbdy(lat2d,6) ### important to do that now and not before |
---|
[490] | 231 | ###### |
---|
[393] | 232 | if varname in ['PHTOT','W']: vertdim='BOTTOM-TOP_PATCH_END_STAG' |
---|
| 233 | else: vertdim='BOTTOM-TOP_PATCH_END_UNSTAG' |
---|
[490] | 234 | if (var2 is not None and var2 not in ['PHTOT','W']): dumped_vert_stag=True ; vertdim='BOTTOM-TOP_PATCH_END_UNSTAG' |
---|
| 235 | else: dumped_vert_stag=False |
---|
[393] | 236 | if varname in ['V']: latdim='SOUTH-NORTH_PATCH_END_STAG' |
---|
| 237 | else: latdim='SOUTH-NORTH_PATCH_END_UNSTAG' |
---|
| 238 | if varname in ['U']: londim='WEST-EAST_PATCH_END_STAG' |
---|
| 239 | else: londim='WEST-EAST_PATCH_END_UNSTAG' |
---|
[402] | 240 | lon = np.arange(0,getattr(nc,londim),1) ; lat = np.arange(0,getattr(nc,latdim),1) |
---|
[468] | 241 | ### |
---|
| 242 | if axtime in ["ls","sol"]: |
---|
| 243 | lstab, soltab, lttab = getlschar ( namefile, getaxis = True ) |
---|
| 244 | if axtime == "ls": time = lstab |
---|
| 245 | elif axtime == "sol": time = soltab |
---|
| 246 | else: |
---|
[613] | 247 | if "Times" in nc.dimensions: time = count + np.arange(0,len(nc.dimensions["Times"]),1) |
---|
| 248 | elif "Time" in nc.dimensions: time = count + np.arange(0,len(nc.dimensions["Time"]),1) |
---|
| 249 | else: time = count + np.arange(0,1,1) |
---|
[569] | 250 | if ope in ["cat"]: |
---|
| 251 | if nnn > 0: count = time[-1] + 1 ## so that a cat is possible with simple subscripts |
---|
| 252 | else: count = 0 |
---|
| 253 | else: count = 0 |
---|
[489] | 254 | if axtime in ["lt"]: |
---|
[569] | 255 | ftime = np.zeros(len(time)) |
---|
| 256 | for i in range(len(time)): |
---|
| 257 | ftime[i] = localtime ( interv[0]+time[i]*interv[1], 0.5*(wlon[0]+wlon[1]) ) |
---|
| 258 | time=ftime |
---|
| 259 | time=check_localtime(time) |
---|
[489] | 260 | print "LOCAL TIMES.... ", time |
---|
[569] | 261 | |
---|
[468] | 262 | ### |
---|
[426] | 263 | if typefile in ['geo']: vert = [0.] ; stime = readslices(str(0)) |
---|
| 264 | else: |
---|
| 265 | if vertmode is None: vertmode=0 |
---|
| 266 | if vertmode == 0: vert = np.arange(0,getattr(nc,vertdim),1) |
---|
[562] | 267 | elif vertmode == -1: vert = nc.variables["PHTOT"][0,:,0,0]/3.72 ; vert = np.array(vert[0:len(vert)-1]) #; print vert |
---|
[610] | 268 | elif vertmode == 1 or vertmode == 2: vert = nc.variables["vert"][:] ## pressure in Pa |
---|
| 269 | else: vert = nc.variables["vert"][:]/1000. ## altitude in km |
---|
[350] | 270 | #if firstfile: |
---|
| 271 | # lat0 = lat |
---|
| 272 | #elif len(lat0) != len(lat): |
---|
| 273 | # errormess("Not the same latitude lengths !", [len(lat0), len(lat)]) |
---|
| 274 | #elif sum((lat == lat0) == False) != 0: |
---|
| 275 | # errormess("Not the same latitudes !", [lat,lat0]) |
---|
| 276 | ## Faire d'autre checks sur les compatibilites entre fichiers!! |
---|
[349] | 277 | ########################################################## |
---|
[490] | 278 | ########################################################## |
---|
| 279 | ########################################################## |
---|
[345] | 280 | |
---|
[402] | 281 | all_varname[k] = varname |
---|
| 282 | all_namefile[k] = namefile |
---|
[406] | 283 | all_time[k] = time |
---|
[612] | 284 | if var2: |
---|
[637] | 285 | all_var2[k] = getfield(nc,var2) |
---|
[612] | 286 | if ((var2 in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (var2 not in nc.variables)): all_var2[k] = slopeamplitude(nc) |
---|
[451] | 287 | if winds: all_windu[k] = getfield(nc,uchar) ; all_windv[k] = getfield(nc,vchar) |
---|
[571] | 288 | ##################### |
---|
| 289 | ##### GETFIELDS ##### |
---|
| 290 | ##################### |
---|
| 291 | ##### SPECIFIC CASES |
---|
| 292 | ##### 1. saturation temperature |
---|
[398] | 293 | if varname in ["temp","t","T_nadir_nit","T_nadir_day","temp_day","temp_night"] and tsat: |
---|
[392] | 294 | tt=getfield(nc,varname) ; print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE" |
---|
| 295 | if type(tt).__name__=='MaskedArray': tt.set_fill_value([np.NaN]) ; tinput=tt.filled() |
---|
| 296 | else: tinput=tt |
---|
[391] | 297 | all_var[k]=get_tsat(vert,tinput,zlon=lon,zlat=lat,zalt=vert,ztime=time) |
---|
[571] | 298 | ##### 2. wind amplitude |
---|
| 299 | elif ((varname in ['UV','uv','uvmet']) and (typefile in ['meso']) and (varname not in nc.variables)): |
---|
| 300 | all_var[k]=windamplitude(nc) |
---|
[612] | 301 | elif ((varname in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (varname not in nc.variables)): |
---|
| 302 | all_var[k]=slopeamplitude(nc) |
---|
| 303 | elif ((varname in ['DELTAT','deltat']) and (typefile in ['meso']) and (varname not in nc.variables)): |
---|
| 304 | all_var[k]=deltat0t1(nc) |
---|
[388] | 305 | else: |
---|
[392] | 306 | ##### GENERAL STUFF HERE |
---|
[571] | 307 | all_var[k] = getfield(nc,varname) |
---|
[468] | 308 | print "**** GOT SUBDATA:",k," NAMEFILE:",namefile," VAR:",varname, var2 ; k += 1 ; firstfile = False |
---|
[345] | 309 | #### End of for namefile in namefiles |
---|
| 310 | |
---|
[359] | 311 | ################################## |
---|
[380] | 312 | ### Operation on files |
---|
| 313 | if ope is not None: |
---|
[468] | 314 | print "********** OPERATION: ",ope |
---|
[422] | 315 | if "var" not in ope: |
---|
| 316 | if len(var) > 1: errormess("for this operation... please set only one var !") |
---|
[457] | 317 | if ope in ["-","+","-%"]: |
---|
[569] | 318 | if fileref is not None: |
---|
[571] | 319 | ncref = Dataset(fileref) |
---|
| 320 | if ((all_varname[k-1] in ['UV','uv','uvmet']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)): |
---|
| 321 | all_var[k] = windamplitude(ncref) |
---|
[612] | 322 | elif ((all_varname[k-1] in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)): |
---|
| 323 | all_var[k] = slopeamplitude(ncref) |
---|
| 324 | elif ((all_varname[k-1] in ['DELTAT','deltat']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)): |
---|
| 325 | all_var[k]=deltat0t1(ncref) |
---|
[569] | 326 | else: |
---|
[571] | 327 | all_var[k] = getfield(ncref,all_varname[k-1]) |
---|
[569] | 328 | all_varname[k] = all_varname[k-1] ; all_time[k] = all_time[k-1] ; all_namefile[k] = all_namefile[k-1] ; all_var2[k] = all_var2[k-1] |
---|
[612] | 329 | if winds: all_windu[k] = getfield(ncref,uchar) ; all_windv[k] = getfield(ncref,vchar) |
---|
[569] | 330 | else: errormess("fileref is missing!") |
---|
[380] | 331 | if ope == "-": all_var[k+1]= all_var[k-1] - all_var[k] |
---|
| 332 | elif ope == "+": all_var[k+1]= all_var[k-1] + all_var[k] |
---|
[502] | 333 | elif ope == "-%": |
---|
| 334 | masked = np.ma.masked_where(all_var[k] == 0,all_var[k]) |
---|
| 335 | masked.set_fill_value([np.NaN]) |
---|
| 336 | all_var[k+1]= 100.*(all_var[k-1] - masked)/masked |
---|
[569] | 337 | all_varname[k+1] = all_varname[k] ; all_time[k+1] = all_time[k] ; all_namefile[k+1] = all_namefile[k] ; all_var2[k+1] = all_var2[k] ; numplot = numplot+2 |
---|
[612] | 338 | if winds: all_windu[k+1] = all_windu[k-1]-all_windu[k] ; all_windv[k+1] = all_windv[k-1] - all_windv[k] |
---|
[380] | 339 | elif ope in ["cat"]: |
---|
[468] | 340 | tabtime = all_time[0];tab = all_var[0];k = 1 |
---|
[462] | 341 | if var2: tab2 = all_var2[0] |
---|
[475] | 342 | while k != len(namefiles) and len(all_time[k]) != 0: |
---|
[462] | 343 | if var2: tab2 = np.append(tab2,all_var2[k],axis=0) |
---|
[468] | 344 | tabtime = np.append(tabtime,all_time[k]) ; tab = np.append(tab,all_var[k],axis=0) ; k += 1 |
---|
| 345 | all_time[0] = np.array(tabtime) ; all_var[0] = np.array(tab) ; numplot = 1 |
---|
[462] | 346 | if var2: all_var2[0] = np.array(tab2) |
---|
[422] | 347 | else: errormess(ope+" : non-implemented operation. Check pp.py --help") |
---|
| 348 | else: |
---|
| 349 | if len(namefiles) > 1: errormess("for this operation... please set only one file !") |
---|
| 350 | if len(var) > 2: errormess("not sure this works for more than 2 vars... please check.") |
---|
| 351 | if ope in ["div_var"]: all_var[k] = all_var[k-2] / all_var[k-1] ; insert = '_div_' |
---|
| 352 | elif ope in ["mul_var"]: all_var[k] = all_var[k-2] * all_var[k-1] ; insert = '_mul_' |
---|
| 353 | elif ope in ["add_var"]: all_var[k] = all_var[k-2] + all_var[k-1] ; insert = '_add_' |
---|
| 354 | elif ope in ["sub_var"]: all_var[k] = all_var[k-2] - all_var[k-1] ; insert = '_sub_' |
---|
| 355 | else: errormess(ope+" : non-implemented operation. Check pp.py --help") |
---|
| 356 | numplot = numplot + 1 ; all_time[k] = all_time[k-1] ; all_namefile[k] = all_namefile[k-1] |
---|
| 357 | all_varname[k] = all_varname[k-2] + insert + all_varname[k-1] |
---|
[345] | 358 | ################################## |
---|
| 359 | ### Open a figure and set subplots |
---|
| 360 | fig = figure() |
---|
| 361 | subv,subh = definesubplot( numplot, fig ) |
---|
[457] | 362 | if ope in ['-','-%']: subv,subh = 2,2 |
---|
[345] | 363 | |
---|
| 364 | ################################# |
---|
| 365 | ### Time loop for plotting device |
---|
[448] | 366 | nplot = 1 ; error = False |
---|
[612] | 367 | if lstyle is not None: linecycler = cycle(lstyle) |
---|
| 368 | else: linecycler = cycle(["-","--","-.",":"]) |
---|
[392] | 369 | print "********************************************" |
---|
[345] | 370 | while error is False: |
---|
[458] | 371 | |
---|
| 372 | print "********** NPLOT", nplot |
---|
[424] | 373 | if nplot > numplot: break |
---|
[345] | 374 | |
---|
[424] | 375 | #################################################################### |
---|
[392] | 376 | ## get all indexes to be taken into account for this subplot and then reduce field |
---|
| 377 | ## 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 |
---|
[569] | 378 | indexlon = getsindex(sslon,(nplot-1)%nlon,lon) |
---|
| 379 | indexlat = getsindex(sslat,((nplot-1)//nlon)%nlat,lat) |
---|
[392] | 380 | indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert) |
---|
[423] | 381 | if ope is not None: |
---|
| 382 | 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 |
---|
| 383 | 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] | 384 | elif "cat" in ope: index_f = 0 |
---|
[423] | 385 | else: yeah = len(namefiles)*len(var) ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah |
---|
[407] | 386 | time = all_time[index_f] |
---|
[435] | 387 | if mrate is not None: indextime = None |
---|
| 388 | else: indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) |
---|
[426] | 389 | ltst = None |
---|
[548] | 390 | if typefile in ['meso'] and indextime is not None: ltst = localtime ( interv[0]+indextime*interv[1], 0.5*(wlon[0]+wlon[1]) ) |
---|
[468] | 391 | print "********** INDEX LON:",indexlon," LAT:",indexlat," VERT:",indexvert," TIME:",indextime |
---|
[490] | 392 | ##var = nc.variables["phisinit"][:,:] |
---|
| 393 | ##contourf(np.transpose(var),30,cmap = get_cmap(name="Greys_r") ) ; axis('off') ; plot(indexlat,indexlon,'mx',mew=4.0,ms=20.0) |
---|
| 394 | ##show() |
---|
| 395 | ##exit() |
---|
| 396 | #truc = True |
---|
| 397 | #truc = False |
---|
| 398 | #if truc: indexvert = None |
---|
[424] | 399 | #################################################################### |
---|
[475] | 400 | ########## REDUCE FIELDS |
---|
| 401 | #################################################################### |
---|
[448] | 402 | error = False |
---|
[392] | 403 | varname = all_varname[index_f] |
---|
[448] | 404 | if varname: ### what is shaded. |
---|
[436] | 405 | what_I_plot, error = reducefield( all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat, d3=indexvert, \ |
---|
[525] | 406 | yint=yintegral, alt=vert, anomaly=anomaly, redope=redope, mesharea=area ) |
---|
[516] | 407 | if mult != 2718.: what_I_plot = what_I_plot*mult |
---|
| 408 | else: what_I_plot = np.log10(what_I_plot) ; print "log plot" |
---|
[451] | 409 | if var2: ### what is contoured. |
---|
[448] | 410 | what_I_plot_contour, error = reducefield( all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert, \ |
---|
| 411 | yint=yintegral, alt=vert ) |
---|
[451] | 412 | if winds: ### what is plot as vectors. |
---|
| 413 | vecx, error = reducefield( all_windu[index_f], d4=indextime, d3=indexvert, yint=yintegral, alt=vert) |
---|
| 414 | vecy, error = reducefield( all_windv[index_f], d4=indextime, d3=indexvert, yint=yintegral, alt=vert) |
---|
[490] | 415 | if varname in [uchar,vchar]: what_I_plot = np.sqrt( np.square(vecx) + np.square(vecy) ) ; varname = "wind" |
---|
| 416 | |
---|
| 417 | ##################################################################### |
---|
| 418 | #if truc: |
---|
| 419 | # nx = what_I_plot.shape[2] ; ny = what_I_plot.shape[1] ; nz = what_I_plot.shape[0] |
---|
| 420 | # for k in range(nz): print k,' over ',nz ; what_I_plot[k,:,:] = what_I_plot[k,:,:] / smooth(what_I_plot[k,:,:],12) |
---|
| 421 | # for iii in range(nx): |
---|
| 422 | # for jjj in range(ny): |
---|
| 423 | # deviation = what_I_plot[:,jjj,iii] ; mx = max(deviation) ; mn = min(deviation) |
---|
| 424 | # if iii > 6 and iii < nx-6 and jjj > 6 and jjj < ny-6: what_I_plot[0,jjj,iii],rel = singlet(deviation,vert/1000.) ### z must be in km |
---|
| 425 | # else: what_I_plot[0,jjj,iii] = 0. |
---|
| 426 | # if np.abs(what_I_plot[0,jjj,iii]) > 1.5: |
---|
| 427 | # print iii,jjj,what_I_plot[0,jjj,iii],int(abs(1.-mx)*100.),int(abs(1.-mn)*100.) |
---|
| 428 | # plot(rel) |
---|
| 429 | # show() |
---|
| 430 | # anomaly = True ### pour avoir les bons reglages plots |
---|
| 431 | # what_I_plot = what_I_plot[0,:,:] |
---|
| 432 | ##################################################################### |
---|
| 433 | |
---|
[448] | 434 | #################################################################### |
---|
[458] | 435 | ### General plot settings |
---|
| 436 | changesubplot = (numplot > 1) and (len(what_I_plot.shape) != 1) ## default for 1D plots: superimposed. to be reworked for better flexibility. |
---|
[518] | 437 | if changesubplot: subplot(subv,subh,nplot) #; subplots_adjust(wspace=0,hspace=0) |
---|
[458] | 438 | #################################################################### |
---|
[475] | 439 | if error: |
---|
| 440 | errormess("There is an error in reducing field !") |
---|
| 441 | else: |
---|
[448] | 442 | ticks = ndiv + 1 |
---|
[405] | 443 | fvar = varname |
---|
| 444 | if anomaly: fvar = 'anomaly' |
---|
[475] | 445 | ### |
---|
| 446 | if mapmode == 0: ### could this be moved inside imov loop ? |
---|
[430] | 447 | itime=indextime |
---|
[587] | 448 | if len(what_I_plot.shape) == 3: itime=[0] |
---|
[475] | 449 | m = None ; x = None ; y = None |
---|
[608] | 450 | latyeah = lat ; lonyeah = lon |
---|
| 451 | if typefile in ['meso']: |
---|
| 452 | # now that the section is determined we can set the real lat |
---|
| 453 | # ... or for now, a temptative one. |
---|
| 454 | if slon is not None: latyeah = lat2d[:,0] |
---|
| 455 | if slat is not None: lonyeah = lon2d[0,:] |
---|
| 456 | what_I_plot, x, y = define_axis(lonyeah,latyeah,vert,time,indexlon,indexlat,indexvert,\ |
---|
[430] | 457 | itime,what_I_plot, len(all_var[index_f].shape),vertmode) |
---|
[475] | 458 | ### |
---|
| 459 | if (fileref is not None) and (index_f == numplot-1): zevmin, zevmax = calculate_bounds(what_I_plot,vmin=minop,vmax=maxop) |
---|
[405] | 460 | else: zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax) |
---|
[475] | 461 | if (fileref is not None) and (index_f == numplot-1): colorb = "RdBu_r" |
---|
[518] | 462 | if colorb in ["def","nobar","onebar"]: palette = get_cmap(name=defcolorb(fvar.upper())) |
---|
[436] | 463 | else: palette = get_cmap(name=colorb) |
---|
[490] | 464 | #palette = cm.GMT_split |
---|
[489] | 465 | ##### 1. ELIMINATE 0D or >3D CASES |
---|
| 466 | if len(what_I_plot.shape) == 0: |
---|
| 467 | print "VALUE VALUE VALUE VALUE ::: ",what_I_plot |
---|
| 468 | save = 'donothing' |
---|
| 469 | elif len(what_I_plot.shape) >= 4: |
---|
[475] | 470 | print "WARNING!!! ",len(what_I_plot.shape),"-D PLOT NOT SUPPORTED !!! dimensions: ",what_I_plot.shape |
---|
| 471 | errormess("Are you sure you did not forget to prescribe a dimension ?") |
---|
| 472 | ##### 2. HANDLE simple 1D/2D field and movies of 1D/2D fields |
---|
| 473 | else: |
---|
[434] | 474 | if mrate is not None: iend=len(time)-1 |
---|
[448] | 475 | else: iend=0 |
---|
| 476 | imov = 0 |
---|
[475] | 477 | if len(what_I_plot.shape) == 3: |
---|
| 478 | if var2: which = "contour" ## have to start with contours rather than shading |
---|
| 479 | else: which = "regular" |
---|
| 480 | if mrate is None: errormess("3D field. Use --rate RATE for movie or specify --time TIME. Exit.") |
---|
| 481 | elif len(what_I_plot.shape) == 2: |
---|
| 482 | if var2: which = "contour" ## have to start with contours rather than shading |
---|
| 483 | else: which = "regular" |
---|
| 484 | if mrate is not None: which = "unidim" |
---|
| 485 | elif len(what_I_plot.shape) == 1: |
---|
| 486 | which = "unidim" |
---|
[562] | 487 | if what_I_plot.shape[-1] == 1: print "VALUE VALUE VALUE VALUE ::: ", what_I_plot[0] ; save = 'donothing' |
---|
[475] | 488 | ##### IMOV LOOP #### IMOV LOOP |
---|
[430] | 489 | while imov <= iend: |
---|
[448] | 490 | print "-> frame ",imov+1, which |
---|
| 491 | if which == "regular": |
---|
| 492 | if mrate is None: what_I_plot_frame = what_I_plot |
---|
| 493 | else: what_I_plot_frame = what_I_plot[imov,:,:] |
---|
[451] | 494 | if winds: |
---|
| 495 | if mrate is None: vecx_frame = vecx ; vecy_frame = vecy |
---|
| 496 | else: vecx_frame = vecx[imov,:,:] ; vecy_frame = vecy[imov,:,:] |
---|
[448] | 497 | elif which == "contour": |
---|
| 498 | if mrate is None or what_I_plot_contour.ndim < 3: what_I_plot_frame = what_I_plot_contour |
---|
| 499 | else: what_I_plot_frame = what_I_plot_contour[imov,:,:] |
---|
[475] | 500 | elif which == "unidim": |
---|
| 501 | if mrate is None: what_I_plot_frame = what_I_plot |
---|
| 502 | else: what_I_plot_frame = what_I_plot[:,imov] ## because swapaxes |
---|
| 503 | #if mrate is not None: |
---|
| 504 | if mapmode == 1: |
---|
[451] | 505 | m = define_proj(proj,wlon,wlat,back=back,blat=blat,blon=blon) ## this is dirty, defined above but out of imov loop |
---|
| 506 | x, y = m(lon2d, lat2d) ## this is dirty, defined above but out of imov loop |
---|
[548] | 507 | if typefile in ['meso'] and mapmode == 1: what_I_plot_frame = dumpbdy(what_I_plot_frame,6,condition=True) |
---|
[464] | 508 | # if typefile in ['mesoideal']: what_I_plot_frame = dumpbdy(what_I_plot_frame,0,stag='W',condition=dumped_vert_stag) |
---|
[444] | 509 | |
---|
[475] | 510 | if which == "unidim": |
---|
[614] | 511 | if lbls is not None: lbl=lbls[nplot-1] |
---|
[612] | 512 | else: |
---|
| 513 | lbl = "" |
---|
| 514 | if indexlat is not None: lbl = lbl + " ix" + str(indexlat[0]) |
---|
| 515 | if indexlon is not None: lbl = lbl + " iy" + str(indexlon[0]) |
---|
| 516 | if indexvert is not None: lbl = lbl + " iz" + str(indexvert[0]) |
---|
| 517 | if indextime is not None: lbl = lbl + " it" + str(indextime[0]) |
---|
| 518 | if lbl == "": lbl = namefiles[index_f] |
---|
[569] | 519 | |
---|
[475] | 520 | if mrate is not None: x = y ## because swapaxes... |
---|
[490] | 521 | #what_I_plot_frame = np.diff(what_I_plot_frame, n=1) ; x = x[1:] |
---|
[587] | 522 | |
---|
| 523 | zeline = next(linecycler) ## "-" for simple lines |
---|
| 524 | if tile: zemarker = 'x' |
---|
| 525 | else: zemarker = None |
---|
[579] | 526 | this_is_a_regular_plot = (indexvert is not None) or (indextime is None) or (indexlat is None) or (indexlon is None) |
---|
[587] | 527 | if this_is_a_regular_plot: plot(x,what_I_plot_frame,zeline,label=lbl,marker=zemarker) ## vertical profile |
---|
| 528 | else: plot(what_I_plot_frame,x,zeline,label=lbl,marker=zemarker) ## regular plot |
---|
[475] | 529 | if nplot > 1: legend(loc='best') |
---|
[502] | 530 | if indextime is None and axtime is not None and xlab is None: xlabel(axtime.upper()) ## define the right label |
---|
[475] | 531 | if save == 'txt': writeascii(np.transpose(what_I_plot),'profile'+str(nplot*1000+imov)+'.txt') |
---|
| 532 | |
---|
[569] | 533 | elif which == "regular": |
---|
[448] | 534 | if hole: what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax) |
---|
| 535 | else: what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax) |
---|
| 536 | if flagnolow: what_I_plot_frame = nolow(what_I_plot_frame) |
---|
| 537 | if not tile: |
---|
| 538 | #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) |
---|
| 539 | zelevels = np.linspace(zevmin,zevmax,num=ticks) |
---|
[548] | 540 | #what_I_plot_frame = smooth(what_I_plot_frame,100) |
---|
[453] | 541 | if mapmode == 1: m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette, alpha=trans) |
---|
| 542 | elif mapmode == 0: contourf( x, y, what_I_plot_frame, zelevels, cmap = palette, alpha=trans) |
---|
[612] | 543 | if cross is not None and mapmode == 1: |
---|
| 544 | idx,idy=m(cross[0][0],cross[0][1]) |
---|
| 545 | mpl.pyplot.plot([idx],[idy],'+k',mew=0.5,ms=18) |
---|
[448] | 546 | else: |
---|
[458] | 547 | if mapmode == 1: m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans) |
---|
| 548 | elif mapmode == 0: pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans) |
---|
[475] | 549 | |
---|
[518] | 550 | if colorb not in ['nobar','onebar']: |
---|
[475] | 551 | if (fileref is not None) and (index_f == numplot-1): daformat = "%.3f" |
---|
[468] | 552 | elif mult != 1: daformat = "%.1f" |
---|
[448] | 553 | else: daformat = fmtvar(fvar.upper()) |
---|
[603] | 554 | #if proj in ['moll','cyl']: zeorientation="horizontal" ; zepad = 0.05 |
---|
| 555 | if proj in ['moll']: zeorientation="horizontal" ; zepad = 0.05 |
---|
[599] | 556 | else: zeorientation="vertical" ; zepad = 0.03 |
---|
| 557 | zecb = colorbar( fraction=0.05,pad=zepad,format=daformat,orientation=zeorientation,\ |
---|
[468] | 558 | ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,21])),extend='neither',spacing='proportional' ) |
---|
[578] | 559 | if zeorientation == "horizontal" and zetitle[0] != "fill": zecb.ax.set_xlabel(zetitle[index_f]) ; zetitle[index_f]="" |
---|
[451] | 560 | if winds: |
---|
[548] | 561 | if typefile in ['meso']: |
---|
[451] | 562 | [vecx_frame,vecy_frame] = [dumpbdy(vecx_frame,6,stag=uchar,condition=True), dumpbdy(vecy_frame,6,stag=vchar,condition=True)] |
---|
| 563 | key = True |
---|
[637] | 564 | if fvar in ['UV','uv','uvmet']: key = False |
---|
[451] | 565 | elif typefile in ['gcm']: |
---|
| 566 | key = False |
---|
[548] | 567 | if metwind and mapmode == 1: [vecx_frame,vecy_frame] = m.rotate_vector(vecx_frame, vecy_frame, lon2d, lat2d) |
---|
[451] | 568 | if var: colorvec = definecolorvec(back) |
---|
| 569 | else: colorvec = definecolorvec(colorb) |
---|
| 570 | vectorfield(vecx_frame, vecy_frame, x, y, stride=stride, csmooth=2,\ |
---|
| 571 | #scale=15., factor=300., color=colorvec, key=key) |
---|
| 572 | scale=20., factor=250., color=colorvec, key=key) |
---|
| 573 | #200. ## or csmooth=stride |
---|
[448] | 574 | elif which == "contour": |
---|
[562] | 575 | zevminc, zevmaxc = calculate_bounds(what_I_plot_frame, vmin=min(what_I_plot_frame), vmax=max(what_I_plot_frame)) |
---|
[448] | 576 | zelevels = np.linspace(zevminc,zevmaxc,ticks/2) #20) |
---|
[637] | 577 | if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,1000.) |
---|
| 578 | elif var2 == 'tpot': zelevels = np.arange(270,370,5) |
---|
| 579 | elif var2 == 'tk': zelevels = np.arange(150,250,5) |
---|
[608] | 580 | if mapmode == 0: |
---|
[637] | 581 | what_I_plot_frame, x, y = define_axis( lonyeah,latyeah,vert,time,indexlon,indexlat,indexvert,\ |
---|
[448] | 582 | itime,what_I_plot_frame, len(all_var2[index_f].shape),vertmode ) |
---|
[637] | 583 | ## is this needed? only if len(all_var2[index_f].shape) != len(all_var[index_f].shape) |
---|
| 584 | cs = contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 0.33)#, alpha=0.5, linestyles='solid') |
---|
| 585 | clabel(cs, inline=1, fontsize = 4.*rcParams['font.size']/5., fmt=fmtvar(var2.upper())) |
---|
| 586 | elif mapmode == 1: cs = m.contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 0.33)#, alpha=0.5, linestyles='solid') |
---|
[475] | 587 | if which in ["regular","unidim"]: |
---|
[448] | 588 | |
---|
[475] | 589 | if nplot > 1 and which == "unidim": |
---|
| 590 | pass ## because we superimpose nplot instances |
---|
| 591 | else: |
---|
| 592 | # Axis directives for movie frames [including the first one). |
---|
| 593 | zxmin, zxmax = xaxis ; zymin, zymax = yaxis |
---|
| 594 | if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) |
---|
| 595 | if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax) |
---|
| 596 | if zymin is not None: mpl.pyplot.ylim(ymin=zymin) |
---|
| 597 | if zymax is not None: mpl.pyplot.ylim(ymax=zymax) |
---|
| 598 | if ylog: mpl.pyplot.semilogy() |
---|
| 599 | if invert_y: ax = mpl.pyplot.gca() ; ax.set_ylim(ax.get_ylim()[::-1]) |
---|
[502] | 600 | if xlab is not None: xlabel(xlab) |
---|
| 601 | if ylab is not None: ylabel(ylab) |
---|
[475] | 602 | |
---|
[448] | 603 | if mrate is not None: |
---|
[451] | 604 | ### THIS IS A MENCODER MOVIE |
---|
| 605 | if mrate > 0: |
---|
| 606 | figframe=mpl.pyplot.gcf() |
---|
| 607 | if mquality: figframe.set_dpi(600.) |
---|
| 608 | else: figframe.set_dpi(200.) |
---|
| 609 | mframe=fig2img(figframe) |
---|
| 610 | if imov == 0: |
---|
| 611 | moviename='movie' ;W,H = figframe.canvas.get_width_height() |
---|
| 612 | video = VideoSink((H,W), moviename, rate=mrate, byteorder="rgba") |
---|
| 613 | video.run(mframe) ; close() |
---|
| 614 | if imov == iend: video.close() |
---|
| 615 | ### THIS IS A WEBPAGE MOVIE |
---|
| 616 | else: |
---|
| 617 | nameframe = "image"+str(1000+imov) |
---|
| 618 | makeplotres(nameframe,res=100.,disp=False) ; close() |
---|
| 619 | if imov == 0: myfile = open("zepics", 'w') |
---|
| 620 | myfile.write("modImages["+str(imov)+"] = '"+nameframe+"_100.png';"+ '\n') |
---|
| 621 | if imov == iend: |
---|
| 622 | myfile.write("first_image = 0;"+ '\n') |
---|
| 623 | myfile.write("last_image = "+str(iend)+";"+ '\n') |
---|
| 624 | myfile.close() |
---|
[475] | 625 | if var2 and which == "regular": which = "contour" |
---|
[448] | 626 | imov = imov+1 |
---|
| 627 | elif which == "contour": |
---|
| 628 | which = "regular" |
---|
| 629 | |
---|
[345] | 630 | ### Next subplot |
---|
[483] | 631 | zevarname = varname |
---|
| 632 | if redope is not None: zevarname = zevarname + "_" + redope |
---|
| 633 | basename = getname(var=zevarname,var2=var2,winds=winds,anomaly=anomaly) |
---|
[462] | 634 | if len(what_I_plot.shape) > 3: |
---|
| 635 | basename = basename + getstralt(nc,level) |
---|
[451] | 636 | if mrate is not None: basename = "movie_" + basename |
---|
[548] | 637 | if typefile in ['meso']: |
---|
[569] | 638 | if sslon is not None: basename = basename + "_lon_" + str(int(round(lonp))) |
---|
| 639 | if sslat is not None: basename = basename + "_lat_" + str(int(round(latp))) |
---|
[349] | 640 | plottitle = basename |
---|
[468] | 641 | ### dans le nouveau systeme time=ls,sol,lt cette ligne pourrait ne servir a rien (ou deplacer au dessus) |
---|
| 642 | if addchar and indextime is not None: [addchar,gogol,gogol2] = getlschar ( all_namefile[index_f] ) ; plottitle = plottitle + addchar |
---|
[485] | 643 | ### en fait redope is None doit etre remplace par : n'est ni maxt ni mint |
---|
| 644 | if redope is None and ltst is not None and ( (mapmode == 0) or (proj in ["lcc","laea","merc","nsper"]) ): plottitle = plottitle + "_LT" + str(ltst) |
---|
[349] | 645 | else: |
---|
[359] | 646 | if fileref is not None: |
---|
[402] | 647 | if index_f is numplot-1: plottitle = basename+' '+"fig(1) "+ope+" fig(2)" |
---|
| 648 | elif index_f is numplot-2: plottitle = basename+' '+fileref |
---|
| 649 | else: plottitle = basename+' '+namefiles[0]#index_f] |
---|
| 650 | else: plottitle = basename+' '+namefiles[0]#index_f] |
---|
[468] | 651 | if mult != 1: plottitle = '{:.0e}'.format(mult) + "*" + plottitle |
---|
[578] | 652 | if zetitle[0] != "fill": |
---|
[580] | 653 | if index_f < len(zetitle): plottitle = zetitle[index_f] |
---|
| 654 | else: plottitle = zetitle[0] |
---|
[578] | 655 | if titleref is "fill": titleref=zetitle[index_f] |
---|
[359] | 656 | if fileref is not None: |
---|
[402] | 657 | if index_f is numplot-2: plottitle = titleref |
---|
| 658 | if index_f is numplot-1: plottitle = "fig(1) "+ope+" fig(2)" |
---|
| 659 | # if indexlon is not None: plottitle = plottitle + " lon: " + str(min(lon[indexlon])) +" "+ str(max(lon[indexlon])) |
---|
| 660 | # if indexlat is not None: plottitle = plottitle + " lat: " + str(min(lat[indexlat])) +" "+ str(max(lat[indexlat])) |
---|
| 661 | # if indexvert is not None: plottitle = plottitle + " vert: " + str(min(vert[indexvert])) +" "+ str(max(vert[indexvert])) |
---|
| 662 | # if indextime is not None: plottitle = plottitle + " time: " + str(min(time[indextime])) +" "+ str(max(time[indextime])) |
---|
[518] | 663 | if colorb != "onebar": title( plottitle ) |
---|
[402] | 664 | if nplot >= numplot: error = True |
---|
[345] | 665 | nplot += 1 |
---|
| 666 | |
---|
[518] | 667 | if colorb == "onebar": |
---|
| 668 | cax = axes([0.1, 0.2, 0.8, 0.03]) # a ameliorer |
---|
| 669 | zecb = colorbar(cax=cax, orientation="horizontal", format=fmtvar(fvar.upper()),\ |
---|
| 670 | ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,21])),extend='neither',spacing='proportional') |
---|
[578] | 671 | if zetitle[0] != "fill": zecb.ax.set_xlabel(zetitle[index_f]) ; zetitle[index_f]="" |
---|
[345] | 672 | |
---|
[612] | 673 | |
---|
[345] | 674 | ########################################################################## |
---|
| 675 | ### Save the figure in a file in the data folder or an user-defined folder |
---|
[359] | 676 | if outputname is None: |
---|
[548] | 677 | if typefile in ['meso']: prefix = getprefix(nc) |
---|
[359] | 678 | elif typefile in ['gcm']: prefix = 'LMD_GCM_' |
---|
| 679 | else: prefix = '' |
---|
[345] | 680 | ### |
---|
[359] | 681 | zeplot = prefix + basename |
---|
| 682 | if addchar: zeplot = zeplot + addchar |
---|
| 683 | if numplot <= 0: zeplot = zeplot + "_LT"+str(abs(numplot)) |
---|
[345] | 684 | ### |
---|
[359] | 685 | if not target: zeplot = namefile[0:find(namefile,'wrfout')] + zeplot |
---|
| 686 | else: zeplot = target + "/" + zeplot |
---|
[345] | 687 | ### |
---|
[359] | 688 | else: |
---|
| 689 | zeplot=outputname |
---|
| 690 | |
---|
[436] | 691 | if mrate is None: |
---|
| 692 | pad_inches_value = 0.35 |
---|
| 693 | print "********** SAVE ", save |
---|
| 694 | if save == 'png': |
---|
| 695 | if display: makeplotres(zeplot,res=100.,pad_inches_value=pad_inches_value) #,erase=True) ## a miniature |
---|
| 696 | makeplotres(zeplot,res=resolution,pad_inches_value=pad_inches_value,disp=False) |
---|
| 697 | elif save in ['eps','svg','pdf']: makeplotres(zeplot,pad_inches_value=pad_inches_value,disp=False,ext=save) |
---|
| 698 | elif save == 'gui': show() |
---|
[489] | 699 | elif save == 'donothing': pass |
---|
[436] | 700 | elif save == 'txt': print "Saved results in txt file." |
---|
| 701 | else: |
---|
| 702 | print "INFO: save mode not supported. using gui instead." |
---|
| 703 | show() |
---|
[345] | 704 | |
---|
[447] | 705 | ################################### |
---|
| 706 | #### Getting more out of this video -- PROBLEMS WITH CREATED VIDEOS |
---|
| 707 | # |
---|
| 708 | #if mrate is not None: |
---|
| 709 | # print "Re-encoding movie.. first pass" |
---|
| 710 | # video.first_pass(filename=moviename,quality=mquality,rate=mrate) |
---|
| 711 | # print "Re-encoding movie.. second pass" |
---|
| 712 | # video.second_pass(filename=moviename,quality=mquality,rate=mrate) |
---|
[444] | 713 | |
---|
[345] | 714 | ############### |
---|
| 715 | ### Now the end |
---|
| 716 | return zeplot |
---|