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