source: trunk/MESOSCALE_DEV/PLOT/PYTHON/scripts/find_devils.py @ 296

Last change on this file since 296 was 251, checked in by aslmd, 14 years ago

MESOSCALE: python graphics: improved polar plots. added a detection for dust devils + stats.

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1#! /usr/bin/env python
2
3def detsize( xx, res=1, thres=3, loga=False ):
4    import numpy as np
5    import math
6    size = []
7    sizecalc = 1
8    diff = np.asarray( np.roll(xx,-1) - xx )
9    for i in diff:
10        if abs(i) > 1:
11            if sizecalc >= thres: 
12                if loga: addthis = math.log(sizecalc*res)
13                else:    addthis = sizecalc*res
14                size.append(addthis)
15            sizecalc = 1
16        else:
17            sizecalc += 1
18    return size
19
20def getsize(filename):
21
22    import numpy as np
23    from scipy.ndimage.measurements import minimum_position
24    from netCDF4 import Dataset
25   
26    ### LOAD NETCDF DATA
27    filename = "psfc.nc"
28    nc = Dataset(filename) 
29    psfc = nc.variables["PSFC"]
30   
31    ### LOOP on TIME
32    ### NB: a same event could be counted several times...
33    shape = np.array(psfc).shape
34    allsizesx = []
35    allsizesy = []
36    for i in range(0,shape[0]):
37    #for i in range(0,2):
38   
39        print i, ' on ', shape[0]
40        psfc2d = np.array ( psfc [ i, : , : ] )
41   
42        ############### CRITERION
43        ave = np.mean(psfc2d,dtype=np.float64) ## dtype otherwise inaccuracy
44        where = np.where(psfc2d - ave < -0.3)  ## comme le papier Phoenix
45   
46        std = np.std(psfc2d,dtype=np.float64) ## dtype otherwise inaccuracy
47        fac = 2.5  ## not too low, otherwise vortices are not caught. 4 good choice.
48        lim = ave - fac*std
49        print lim, ave, std
50        where = np.where(psfc2d < lim)
51        ############### END CRITERION
52   
53        lab = np.zeros(np.array(psfc2d).shape) ## points to be treated by the minimum_position routine
54        lab[where] = 1.  ## do not treat points close to 'mean' (background) pressure
55   
56        xx = []
57        yy = []
58        while 1 in lab:
59            p = minimum_position(psfc2d,labels=lab)
60            lab[p] = 0 ## once a minimum has been found in a grid point, do not search here again.
61            if p[0] not in xx: xx.append(p[0]) ## if x coordinate not yet in the list add it
62            if p[1] not in yy: yy.append(p[1]) ## if y coordinate not yet in the list add it
63        xx.sort()
64        yy.sort()
65        ### now xx and yy are sorted arrays containing grid points with pressure minimum
66       
67        ######## DETERMINE SIZE OF STRUCTURES
68        ######## this is rather brute-force...
69        sizex = detsize( xx, res = 10, loga=False, thres=2 )
70        sizey = detsize( yy, res = 10, loga=False, thres=2 )
71        ###
72        allsizesx = np.append(allsizesx,sizex)
73        allsizesy = np.append(allsizesy,sizey)
74        ########
75   
76    allsizesx.sort()
77    allsizesy.sort()
78   
79    return allsizesx, allsizesy
80
81def writeascii ( tab, filename ):
82    mydata = tab
83    myfile = open(filename, 'w')
84    for line in mydata:
85        myfile.write(str(line) + '\n')
86    myfile.close()
87    return
88
89import matplotlib.pyplot as plt
90import pickle
91import numpy as np
92import matplotlib.mlab as mlab
93
94save = True
95#save = False
96if save:
97    allsizesx, allsizesy = getsize("psfc.nc")
98    writeascii(allsizesx,'allsizex.txt')
99    writeascii(allsizesy,'allsizey.txt')
100   
101    myfile = open('allsizex.bin', 'wb')
102    pickle.dump(allsizesx, myfile)
103    myfile.close()
104   
105    myfile = open('allsizey.bin', 'wb')
106    pickle.dump(allsizesy, myfile)
107    myfile.close()
108
109
110myfile = open('allsizex.bin', 'r')
111allsizesx = pickle.load(myfile)
112myfile = open('allsizey.bin', 'r')
113allsizesy = pickle.load(myfile)
114
115plothist = np.append(allsizesx,allsizesy)
116plothist.sort()
117
118nbins=100
119zebins = [2.0]
120for i in range(0,nbins):  zebins.append(zebins[i]*np.sqrt(2))
121zebins = np.array(zebins)
122print zebins
123
124zebins = zebins [ zebins > 10. ] 
125zebins = zebins [ zebins < 1000. ]
126print zebins
127
128#### HISTOGRAM
129plt.hist(    plothist,\
130             log=True,\
131             bins=zebins,\
132             #cumulative=-1,\
133        )
134plt.xscale('log')
135
136plt.show()
137
Note: See TracBrowser for help on using the repository browser.