Changeset 405 for trunk/UTIL/PYTHON/myplot.py
- Timestamp:
- Nov 21, 2011, 6:27:42 PM (13 years ago)
- File:
-
- 1 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",\
Note: See TracChangeset
for help on using the changeset viewer.