Changeset 792 for trunk/UTIL
- Timestamp:
- Sep 21, 2012, 3:03:27 PM (13 years ago)
- Location:
- trunk/UTIL/PYTHON
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/mymath.py
r647 r792 229 229 return idx 230 230 231 # Author: A.C. 231 232 def fig2data ( fig ): 232 233 import numpy … … 248 249 return buf 249 250 251 # Author: A.C. 250 252 def fig2img ( fig ): 251 253 import Image … … 260 262 w, h, d = buf.shape 261 263 return Image.fromstring( "RGBA", ( w ,h ), buf.tostring( ) ) 264 265 # Author: A.C. 266 # Convert a single layer image object (greyscale) to an array 267 def image2array(im): 268 import numpy as np 269 if im.mode not in ("L", "F"): 270 raise ValueError, ("can only convert single-layer images", im.mode) 271 if im.mode == "L": 272 a = np.fromstring(im.tostring(), np.uint8) 273 else: 274 a = np.fromstring(im.tostring(), np.float32) 275 a.shape = im.size[1], im.size[0] 276 return a 277 278 # Author: A.C. 279 # Convert a 2D array to a single layer image object (greyscale) 280 def array2image(a): 281 import numpy as np 282 import Image 283 if a.dtype == np.uint8: 284 mode = "L" 285 elif a.dtype == np.float32: 286 mode = "F" 287 else: 288 raise ValueError, "unsupported image mode" 289 return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring()) 290 -
trunk/UTIL/PYTHON/myplot.py
r783 r792 1541 1541 zt=(zteta+220.)*(zptot/p0)**(r_cp) 1542 1542 return zt 1543 1544 # Author : A.C. 1545 # Find the lon and lat index of the dust devil with the largest pressure gradient 1546 # Steps : 1547 # 1/ convert the chosen PSFC frame to an image of the PSFC anomaly with respect to the mean 1548 # 2/ apply the Sobel operator 1549 # (The Sobel operator performs a 2-D spatial gradient measurement on an image and so emphasizes regions of high spatial frequency that correspond to edges.) 1550 # 3/ find the maximum of the resulting field 1551 # 4/ find the points in a 5 pixel radius around the maximum for which the value of the Sobel transform is greater than half the maximum 1552 # 5/ define a slab of points encompassing the above selected points, including the potential points 'inside' them (if the above points are a hollow circle for example) 1553 # 6/ in this slab, find the point at which the surface pressure is minimum 1554 def find_devil(nc,indextime): 1555 import numpy as np 1556 from scipy import ndimage 1557 from mymath import array2image,image2array 1558 1559 varinfile = nc.variables.keys() 1560 if "PSFC" not in varinfile: errormess("You need PSFC in your file to find dust devils") 1561 else: psfc_full=getfield(nc,'PSFC') 1562 psfc,error=reducefield( psfc_full, d4=indextime) 1563 psfcim=array2image(1000.*(psfc-psfc.mean())) 1564 sx = ndimage.sobel(psfcim, axis=0, mode='constant') ; sy = ndimage.sobel(psfcim, axis=1, mode='constant') 1565 sob = np.hypot(sx, sy) 1566 zemax=np.max(sob) 1567 goodvalues = sob[sob >= zemax/2] 1568 ix = np.in1d(sob.ravel(), goodvalues).reshape(sob.shape) 1569 idxs,idys=np.where(ix) 1570 maxvalue = sob[sob == zemax] 1571 ixmax = np.in1d(sob.ravel(), maxvalue[0]).reshape(sob.shape) 1572 idxmax,idymax=np.where(ixmax) 1573 valok=[] 1574 for i in np.arange(len(idxs)): 1575 a=np.sqrt((idxmax-idxs[i])**2 + (idymax-idys[i])**2) 1576 if 0 < a <= 5.*np.sqrt(2.): valok.append(goodvalues[i]) 1577 ix = np.in1d(sob.ravel(), valok).reshape(sob.shape) 1578 idxs,idys=np.where(ix) 1579 hyperslab=psfc[np.min(idxs):np.max(idxs),np.min(idys):np.max(idys)] 1580 idxsub,idysub=np.where(hyperslab==hyperslab.min()) 1581 idx=idxsub[0]+np.min(idxs) ; idy=idysub[0]+np.min(idys) 1582 return np.int(idx),np.int(idy) -
trunk/UTIL/PYTHON/myscript.py
r763 r792 52 52 parser.add_option('--blon', action='store',dest='blon', type="int", default=None, help='reference lon [computed]') 53 53 parser.add_option('--mark', action='append',dest='mark', type="string", default=None, help='superimpose a crossmark at given lon,lat [None]') 54 parser.add_option('--finddevil', action='store_true',dest='mdevil', default=False, help='superimpose a crossmark where the steepest dust devil is [False]') 54 55 55 56 ### SPECIFIC FOR SLICING [MAPMODE 0] -
trunk/UTIL/PYTHON/planetoplot.py
r782 r792 72 72 lstyle=None,\ 73 73 cross=None,\ 74 markdevil=False,\ 74 75 facwind=1.,\ 75 76 trycol=False,\ … … 87 88 zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ 88 89 getname,localtime,check_localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds,\ 89 getdimfromvar,select_getfield 90 getdimfromvar,select_getfield,find_devil 90 91 from mymath import deg,max,min,mean,writeascii,fig2data,fig2img 91 92 import matplotlib as mpl … … 274 275 lonp,latp = ( lon2d[indices[0,0,0],indices[0,0,1]] , lat2d[indices[0,0,0],indices[0,0,1]] ) 275 276 ### we get rid of boundary relaxation zone for plots. important to do that now and not before. 276 if typefile in ['meso'] and mapmode == 1: lon2d = dumpbdy(lon2d,6) ; lat2d = dumpbdy(lat2d,6) 277 if typefile in ['meso'] and mapmode == 1: 278 if '9999' not in getattr(nc,'START_DATE'): lon2d = dumpbdy(lon2d,6) ; lat2d = dumpbdy(lat2d,6) 277 279 ### we read the keyword for vertical dimension. we take care for vertical staggering. 278 280 if varname in ['PHTOT','W']: vertdim='BOTTOM-TOP_PATCH_END_STAG' … … 589 591 m = define_proj(proj,wlon,wlat,back=back,blat=blat,blon=blon) ## this is dirty, defined above but out of imov loop 590 592 x, y = m(lon2d, lat2d) ## this is dirty, defined above but out of imov loop 591 if typefile in ['meso'] and mapmode == 1: what_I_plot_frame = dumpbdy(what_I_plot_frame,6,condition=True) 593 if typefile in ['meso'] and mapmode == 1: 594 if '9999' not in getattr(nc,'START_DATE'): what_I_plot_frame = dumpbdy(what_I_plot_frame,6,condition=True) 592 595 # if typefile in ['mesoideal']: what_I_plot_frame = dumpbdy(what_I_plot_frame,0,stag='W',condition=dumped_vert_stag) 593 596 … … 693 696 if mapmode == 1: m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette, alpha=trans) 694 697 elif mapmode == 0: contourf( x, y, what_I_plot_frame, zelevels, cmap = palette, alpha=trans) 695 if cross is not None and mapmode == 1:696 idx,idy=m(cross[0][0],cross[0][1])697 mpl.pyplot.plot([idx],[idy],'+k',mew=0.5,ms=18)698 698 else: 699 699 if mapmode == 1: m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans) 700 700 elif mapmode == 0: pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans) 701 701 702 if (cross is not None or markdevil) and mapmode == 1: 703 if cross is not None: idx,idy=m(cross[0][0],cross[0][1]) 704 elif markdevil: 705 idx,idy=find_devil(nc,indextime) 706 idx,idy=x[idx,idy],y[idx,idy] 707 mpl.pyplot.plot([idx],[idy],'+k',mew=0.5,ms=18) 702 708 703 709 if colorb not in ['nobar','onebar']: … … 713 719 if winds: 714 720 if typefile in ['meso']: 715 [vecx_frame,vecy_frame] = [dumpbdy(vecx_frame,6,stag=uchar,condition=True), dumpbdy(vecy_frame,6,stag=vchar,condition=True)]721 if '9999' not in getattr(nc,'START_DATE') : [vecx_frame,vecy_frame] = [dumpbdy(vecx_frame,6,stag=uchar,condition=True), dumpbdy(vecy_frame,6,stag=vchar,condition=True)] 716 722 key = True 717 723 if fvar in ['UV','uv','uvmet']: key = False -
trunk/UTIL/PYTHON/pp.py
r763 r792 176 176 mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\ 177 177 redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\ 178 lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark), facwind=opt.facwind,\178 lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),markdevil=opt.mdevil,facwind=opt.facwind,\ 179 179 trycol=opt.trycol,streamflag=opt.stream,analysis=opt.analysis) 180 180 print 'DONE: '+name
Note: See TracChangeset
for help on using the changeset viewer.