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