source: trunk/UTIL/PYTHON/powerlaw/ddhist.py @ 1142

Last change on this file since 1142 was 1070, checked in by aslmd, 12 years ago

PYTHON. updated analysis tools for dust devils.

  • Property svn:executable set to *
File size: 5.2 KB
Line 
1#! /usr/bin/env python
2import matplotlib.pyplot as mpl
3import ppplot
4import numpy as np
5import scipy.optimize as sciopt
6
7### SAVED
8#namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc_f18.ncm2_1.txt" ; drop = False ; typefit=1
9#namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc_f18.ncm2_1.txt" ; drop = True ; typefit=2
10#namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc_f18.ncm1_1.txt" ; drop = False ; typefit=1
11#namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc_f18.ncm1_1.txt" ; drop = True ; typefit=1
12#namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc.LMD_LES_MARS.160564.ncm2_1.txt" ; drop = False ; typefit=1 #bof. tous les 10 est mieux.
13#
14###########################################################
15##namefile = "/home/aymeric/Big_Data/LES_dd/psfc_f18.ncm1_1.txt"
16##namefile = "/home/aymeric/Big_Data/LES_dd/psfc_f18.ncm2_1.txt"
17##namefile = "/home/aymeric/Big_Data/LES_dd/psfc.LMD_LES_MARS.160564.ncm1_1.txt"
18##namefile = "/home/aymeric/Big_Data/LES_dd/sav/psfc.LMD_LES_MARS.160564.ncm2_1.txt"
19#namefile = "/home/aymeric/Big_Data/LES_dd/press_ustm_exomars.nc1.txt"
20#namefile = "/home/aymeric/Big_Data/LES_dd/sav/press_ustm_exomars.ncm2_1.txt"
21##namefile = "/home/aymeric/Big_Data/LES_dd/psfc_oldinsight100m.ncm1_1.txt"
22
23
24
25
26case = "188324p"
27case = "191798"
28case = "160564p"
29case = "156487"
30case = "2007p"
31case = "13526p"
32case = "172097"
33namefile = "/planeto/aslmd/LESdata/"+case+".ncm1_1.txt"
34
35
36
37drop = False ; typefit=1
38#drop = True ; typefit=1
39#drop = True ; typefit=2
40##########################################################
41## define bins (we expect fit do not depend too much on this -- to be checked)
42nbins = 7 ; www = 3.
43#nbins = 10 ; www = 2.5
44#nbins = 15 ; www = 2.
45##nbins = 20 ; www = 1.75
46##nbins = 30 ; www = 1.5
47##nbins = 50 ; www = 1.2
48##########################################################
49limrest = 4. # restrict limit (multiple of dx)
50#limrest = 2.
51#limrest = 3.
52#limrest = 0.
53##########################################################
54
55# -- functions for fitting
56# -- http://wiki.scipy.org/Cookbook/FittingData
57# power law
58def fitfunc(x,a,b): return a*(x**(-b))
59# power law + constant
60def fitfunc3(x,a,b,c): return a*(x**(-b)) + c * (x**(-0.5))
61# exponential law
62def fitfunc2(x,a,b): return a*np.exp(-b*x)
63
64# load data
65data = np.loadtxt(namefile,delimiter=";")
66t = data[:,0] ; s = data[:,1] ; d = data[:,2]
67i = data[:,3] ; j = data[:,4] # int?
68
69# a way to guess resolution
70dx = np.min(s)/2. ; print "resolution: ", dx
71
72# choose a variable
73if drop: var = d
74else: var = s
75
76# restrictions
77restrict = (s > 0) # initialization (True everywhere)
78restrict = restrict*(s >= limrest*dx) # remove lowest sizes (detection limit)
79#restrict = restrict*(np.abs(i-j) <= 6.*dx) # condition sur i,j (width,height)
80#restrict = restrict*(d > 0.9) # limit on drop for size (casse la power law? seulement si keep smaller devils)
81#restrict = restrict*(d > 0.5)
82var = var[restrict]
83
84## define bins
85zebins = [np.min(var)]
86for i in range(0,nbins):  zebins.append(zebins[i]*(www**0.5))
87zebins = np.array(zebins)
88middle = 0.5*(zebins[1:] + zebins[:-1])
89binwidth = zebins[1:] - zebins[:-1]
90
91# plot histogram
92yeah = mpl.hist(var,log=True,bins=zebins,normed=True,color='white') ; mpl.xscale('log')
93#yeah = mpl.hist(var,bins=zebins)#,normed=True)
94
95# print info
96print "min %5.2e // max %5.2e" % (np.min(var),np.max(var))
97for iii in range(len(zebins)-1):
98    print "%5.2e in [%5.0f %5.0f] %5.0f" % (yeah[0][iii],zebins[iii],zebins[iii+1],np.abs(zebins[iii+1]-zebins[iii]))
99
100# fitting function to superimpose
101if typefit == 1: xx = sciopt.curve_fit(fitfunc, middle, yeah[0])
102elif typefit == 2: xx = sciopt.curve_fit(fitfunc2, middle, yeah[0])
103elif typefit == 3: xx = sciopt.curve_fit(fitfunc3, middle, yeah[0])
104print "exponent",xx[0][1]
105print "variance %",100.*xx[1][1][1]/xx[0][1]
106
107# plot obtained fit along with actual points
108if typefit == 1: func = fitfunc(middle,xx[0][0],xx[0][1])
109elif typefit == 2: func = fitfunc2(middle,xx[0][0],xx[0][1])
110elif typefit == 3: func = fitfunc3(middle,xx[0][0],xx[0][1],xx[0][2]) 
111func[func<0]=np.nan
112ind = yeah[0]>0
113mpl.plot(middle[ind],yeah[0][ind],'k.')
114mpl.plot(middle[ind],func[ind],'r-')
115mpl.plot(middle[ind],func[ind],'r.')
116
117# print fit vs. actual populations
118total = var.shape[0]
119fit = func*binwidth*total
120pop = yeah[0]*binwidth*total
121for iii in range(len(fit)):
122    if fit[iii] > 0.99 or pop[iii] != 0:
123        print "fit %5.0f actual %5.0f" % (fit[iii],pop[iii])
124
125# plot settings
126ax = mpl.gca() ; ax.set_xbound(lower=np.min(zebins),upper=np.max(zebins))
127if drop: mpl.xlabel("Pressure drop (Pa)") ; ax.set_xbound(lower=1.e-1,upper=10.)
128else: mpl.xlabel("Vortex size (m)") ; ax.set_xbound(lower=10.,upper=5000.)
129mpl.ylabel('Population density $n / N w_{bin}$')
130mpl.title("Statistics on "+str(total)+" detected vortices")
131
132# show plot and end
133mpl.show()
134exit()
135
136################################################################################
137################################################################################
138################################################################################
139
140## UNCOMMENT
141##import plfit,plpva,plplot
142
143#[alpha, xmin, L] = plfit.plfit(var,'xmin',0.3)
144[alpha, xmin, L] = plfit.plfit(var)
145print alpha,xmin
146
147#a = plpva.plpva(var,0.75,'xmin',0.75)
148#print a
149
150
151h = plplot.plplot(var,xmin,alpha)
152
153ppplot.save(mode="png")
154
155#mpl.loglog(h[0], h[1], 'k--',linewidth=2)
156
157#mpl.show()
158
159
160
Note: See TracBrowser for help on using the repository browser.