Changeset 405
- Timestamp:
- Nov 21, 2011, 6:27:42 PM (13 years ago)
- Location:
- trunk/UTIL/PYTHON
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/myplot.py
r400 r405 67 67 68 68 ## Author: AS + TN + AC 69 def reducefield (input,d4=None,d3=None,d2=None,d1=None,yint=False,alt=None ):69 def reducefield (input,d4=None,d3=None,d2=None,d1=None,yint=False,alt=None,anomaly=False): 70 70 ### we do it the reverse way to be compliant with netcdf "t z y x" or "t y x" or "y x" 71 71 ### it would be actually better to name d4 d3 d2 d1 as t z y x 72 ### ... note, anomaly is only computed over d1 and d2 for the moment 72 73 import numpy as np 73 74 from mymath import max,mean 75 csmooth = 12 ## a fair amount of grid points 74 76 dimension = np.array(input).ndim 75 77 shape = np.array(input).shape 76 78 #print 'd1,d2,d3,d4: ',d1,d2,d3,d4 77 79 print 'IN REDUCEFIELD dim,shape: ',dimension,shape 80 if anomaly: print 'ANOMALY ANOMALY' 78 81 output = input 79 82 error = False … … 110 113 output = mean(input[d4,:,:,:],axis=0) 111 114 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 115 if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.) 112 116 output = mean(output[d2,:],axis=0) 113 117 output = mean(output[d1],axis=0) … … 115 119 output = mean(input[d4,:,:,:],axis=0) 116 120 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 121 if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.) 117 122 output = mean(output[d2,:],axis=0) 118 123 elif d4 is not None and d3 is not None and d1 is not None: 119 124 output = mean(input[d4,:,:,:],axis=0) 120 125 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 126 if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.) 121 127 output = mean(output[:,d1],axis=1) 122 128 elif d4 is not None and d2 is not None and d1 is not None: 123 129 output = mean(input[d4,:,:,:],axis=0) 130 if anomaly: 131 for k in range(output.shape[0]): output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.) 124 132 output = mean(output[:,d2,:],axis=1) 125 133 output = mean(output[:,d1],axis=1) 134 #noperturb = smooth1d(output,window_len=7) 135 #lenlen = len(output) ; output = output[1:lenlen-7] ; yeye = noperturb[4:lenlen-4] 136 #plot(output) ; plot(yeye) ; show() ; plot(output-yeye) ; show() 126 137 elif d3 is not None and d2 is not None and d1 is not None: 127 138 output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3) 139 if anomaly: 140 for k in range(output.shape[0]): output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.) 128 141 output = mean(output[:,d2,:],axis=1) 129 142 output = mean(output[:,d1],axis=1) … … 131 144 output = mean(input[d4,:,:,:],axis=0) 132 145 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 146 if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.) 133 147 elif d4 is not None and d2 is not None: 134 148 output = mean(input[d4,:,:,:],axis=0) … … 248 262 from numpy import array 249 263 from string import rstrip 250 namefile = rstrip( rstrip( namefile, chars="_z"), chars="_zabg")264 namefile = rstrip( rstrip( rstrip( namefile, chars="_z"), chars="_zabg"), chars="_p") 251 265 #### we assume that wrfout is next to wrfout_z and wrfout_zabg 252 266 nc = Dataset(namefile) … … 394 408 [lon2d,lat2d] = [lon,lat] 395 409 return lon2d,lat2d 410 411 ## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth 412 def smooth1d(x,window_len=11,window='hanning'): 413 import numpy 414 """smooth the data using a window with requested size. 415 This method is based on the convolution of a scaled window with the signal. 416 The signal is prepared by introducing reflected copies of the signal 417 (with the window size) in both ends so that transient parts are minimized 418 in the begining and end part of the output signal. 419 input: 420 x: the input signal 421 window_len: the dimension of the smoothing window; should be an odd integer 422 window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' 423 flat window will produce a moving average smoothing. 424 output: 425 the smoothed signal 426 example: 427 t=linspace(-2,2,0.1) 428 x=sin(t)+randn(len(t))*0.1 429 y=smooth(x) 430 see also: 431 numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve 432 scipy.signal.lfilter 433 TODO: the window parameter could be the window itself if an array instead of a string 434 """ 435 x = numpy.array(x) 436 if x.ndim != 1: 437 raise ValueError, "smooth only accepts 1 dimension arrays." 438 if x.size < window_len: 439 raise ValueError, "Input vector needs to be bigger than window size." 440 if window_len<3: 441 return x 442 if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']: 443 raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'" 444 s=numpy.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]] 445 #print(len(s)) 446 if window == 'flat': #moving average 447 w=numpy.ones(window_len,'d') 448 else: 449 w=eval('numpy.'+window+'(window_len)') 450 y=numpy.convolve(w/w.sum(),s,mode='valid') 451 return y 396 452 397 453 ## Author: AS … … 607 663 def fmtvar (whichvar="def"): 608 664 fmtvar = { \ 609 " tk": "%.0f",\665 "TK": "%.0f",\ 610 666 "T_NADIR_DAY": "%.0f",\ 611 667 "T_NADIR_NIT": "%.0f",\ 612 668 "TEMP_DAY": "%.0f",\ 613 669 "TEMP_NIGHT": "%.0f",\ 614 " tpot": "%.0f",\670 "TPOT": "%.0f",\ 615 671 "TSURF": "%.0f",\ 616 672 "def": "%.1e",\ … … 623 679 "VMR_ICE": "%.1e",\ 624 680 "MTOT": "%.1f",\ 625 " anomaly": "%.1f",\681 "ANOMALY": "%.1f",\ 626 682 "W": "%.1f",\ 627 683 "WMAX_TH": "%.1f",\ 628 684 "QSURFICE": "%.0f",\ 629 "U m": "%.0f",\685 "UM": "%.0f",\ 630 686 "ALBBARE": "%.2f",\ 631 687 "TAU": "%.1f",\ … … 650 706 "def": "spectral",\ 651 707 "HGT": "spectral",\ 652 " tk": "gist_heat",\708 "TK": "gist_heat",\ 653 709 "TSURF": "RdBu_r",\ 654 710 "QH2O": "PuBu",\ … … 666 722 "W": "jet",\ 667 723 "WMAX_TH": "spectral",\ 668 " anomaly": "RdBu_r",\724 "ANOMALY": "RdBu_r",\ 669 725 "QSURFICE": "hot_r",\ 670 726 "ALBBARE": "spectral",\ -
trunk/UTIL/PYTHON/planetoplot.py
r402 r405 258 258 if len(var) != 1 or len(namefiles) != 1: indextime = first 259 259 else: 260 indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) 260 indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) 261 if indextime is not None: ltst = localtime ( interv[0]+indextime*interv[1], 0.5*(wlon[0]+wlon[1]) ) 261 262 262 263 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 … … 292 293 varname = all_varname[index_f] 293 294 if varname: 294 what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert , yint=yintegral, alt=vert )295 what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert , yint=yintegral, alt=vert, anomaly=anomaly) 295 296 what_I_plot = what_I_plot*mult 296 297 if not error: 297 fvar = varname 298 ### 299 if anomaly: 300 what_I_plot = 100. * ((what_I_plot / smooth(what_I_plot,12)) - 1.) 301 fvar = 'anomaly' 302 #if mult != 1: 303 # fvar = str(mult) + "*" + var 304 ### 298 fvar = varname 299 if anomaly: fvar = 'anomaly' 300 ##### MAPMODE-SPECIFIC SETTINGS ##### 305 301 if mapmode == 1: 306 302 if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6) … … 308 304 what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\ 309 305 indextime,what_I_plot, len(all_var[index_f].shape),vertmode) 310 if (fileref is not None) and (index_f is numplot-1): 311 zevmin, zevmax = calculate_bounds(what_I_plot,vmin=minop,vmax=maxop) 312 else: 313 zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax) 306 zxmin, zxmax = xaxis ; zymin, zymax = yaxis 307 if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) 308 if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax) 309 if zymin is not None: mpl.pyplot.ylim(ymin=zymin) 310 if zymax is not None: mpl.pyplot.ylim(ymax=zymax) 311 if invert_y: lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima) 312 if ylog: mpl.pyplot.semilogy() 313 if (fileref is not None) and (index_f is numplot-1): zevmin, zevmax = calculate_bounds(what_I_plot,vmin=minop,vmax=maxop) 314 else: zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax) 314 315 if colorb in ["def","nobar"]: palette = get_cmap(name=defcolorb(fvar.upper())) 315 316 elif (fileref is not None) and (index_f is numplot-1): palette = get_cmap(name="RdBu_r") … … 321 322 #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) 322 323 zelevels = np.linspace(zevmin,zevmax,num=ticks) 323 #contourf(what_I_plot, zelevels, cmap = palette )324 325 324 if mapmode == 1: m.contourf( x, y, what_I_plot, zelevels, cmap = palette) 326 325 elif mapmode == 0: contourf( x, y, what_I_plot, zelevels, cmap = palette) 327 328 zxmin, zxmax = xaxis ; zymin, zymax = yaxis329 if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin)330 if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax)331 if zymin is not None: mpl.pyplot.ylim(ymin=zymin)332 if zymax is not None: mpl.pyplot.ylim(ymax=zymax)333 if invert_y: lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima)334 if ylog: mpl.pyplot.semilogy()335 336 326 else: 337 327 if hole: what_I_plot = nolow(what_I_plot) 338 if mapmode == 1: 339 m.pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 340 elif mapmode == 0: 341 pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 342 328 if mapmode == 1: m.pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 329 elif mapmode == 0: pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 343 330 if colorb != 'nobar' and varname != 'HGT' : 344 331 if (fileref is not None) and (index_f is numplot-1): … … 350 337 ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),\ 351 338 extend='neither',spacing='proportional') 352 353 339 # both min max neither 354 340 ##### 1D field
Note: See TracChangeset
for help on using the changeset viewer.