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

Last change on this file since 1062 was 1053, checked in by aslmd, 11 years ago

UTIL PYTHON. tools to study dust devils in LES.

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