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