1 | #! /usr/bin/env python |
---|
2 | import matplotlib.pyplot as mpl |
---|
3 | import ppplot |
---|
4 | import numpy as np |
---|
5 | import 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 | drop = 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 |
---|
29 | nbins = 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 |
---|
38 | def fitfunc(x,a,b): return a*(x**(-b)) |
---|
39 | # power law + constant |
---|
40 | def fitfunc3(x,a,b,c): return a*(x**(-b)) + c * (x**(-0.5)) |
---|
41 | # exponential law |
---|
42 | def fitfunc2(x,a,b): return a*np.exp(-b*x) |
---|
43 | |
---|
44 | # load data |
---|
45 | data = np.loadtxt(namefile,delimiter=";") |
---|
46 | t = data[:,0] ; s = data[:,1] ; d = data[:,2] |
---|
47 | i = data[:,3] ; j = data[:,4] # int? |
---|
48 | |
---|
49 | # a way to guess resolution |
---|
50 | dx = np.min(s)/2. ; print "resolution: ", dx |
---|
51 | |
---|
52 | # choose a variable |
---|
53 | if drop: var = d |
---|
54 | else: var = s |
---|
55 | |
---|
56 | # restrictions |
---|
57 | restrict = (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) |
---|
62 | var = var[restrict] |
---|
63 | |
---|
64 | ## define bins |
---|
65 | zebins = [np.min(var)] |
---|
66 | for i in range(0,nbins): zebins.append(zebins[i]*(www**0.5)) |
---|
67 | zebins = np.array(zebins) |
---|
68 | middle = 0.5*(zebins[1:] + zebins[:-1]) |
---|
69 | binwidth = zebins[1:] - zebins[:-1] |
---|
70 | |
---|
71 | # plot histogram |
---|
72 | yeah = 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 |
---|
76 | print "min %5.2e // max %5.2e" % (np.min(var),np.max(var)) |
---|
77 | for 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 |
---|
81 | if typefit == 1: xx = sciopt.curve_fit(fitfunc, middle, yeah[0]) |
---|
82 | elif typefit == 2: xx = sciopt.curve_fit(fitfunc2, middle, yeah[0]) |
---|
83 | elif typefit == 3: xx = sciopt.curve_fit(fitfunc3, middle, yeah[0]) |
---|
84 | print "exponent",xx[0][1] |
---|
85 | print "variance %",100.*xx[1][1][1]/xx[0][1] |
---|
86 | |
---|
87 | # plot obtained fit along with actual points |
---|
88 | if typefit == 1: func = fitfunc(middle,xx[0][0],xx[0][1]) |
---|
89 | elif typefit == 2: func = fitfunc2(middle,xx[0][0],xx[0][1]) |
---|
90 | elif typefit == 3: func = fitfunc3(middle,xx[0][0],xx[0][1],xx[0][2]) |
---|
91 | func[func<0]=np.nan |
---|
92 | ind = yeah[0]>0 |
---|
93 | mpl.plot(middle[ind],yeah[0][ind],'k.') |
---|
94 | mpl.plot(middle[ind],func[ind],'r-') |
---|
95 | mpl.plot(middle[ind],func[ind],'r.') |
---|
96 | |
---|
97 | # print fit vs. actual populations |
---|
98 | total = var.shape[0] |
---|
99 | fit = func*binwidth*total |
---|
100 | pop = yeah[0]*binwidth*total |
---|
101 | for 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 |
---|
106 | ax = mpl.gca() ; ax.set_xbound(lower=np.min(zebins),upper=np.max(zebins)) |
---|
107 | if drop: mpl.xlabel("Pressure drop (Pa)") ; ax.set_xbound(lower=1.e-1,upper=10.) |
---|
108 | else: mpl.xlabel("Vortex size (m)") ; ax.set_xbound(lower=10.,upper=5000.) |
---|
109 | mpl.ylabel('Population density $n / N w_{bin}$') |
---|
110 | mpl.title("Statistics on "+str(total)+" detected vortices") |
---|
111 | |
---|
112 | # show plot and end |
---|
113 | mpl.show() |
---|
114 | exit() |
---|
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) |
---|
125 | print alpha,xmin |
---|
126 | |
---|
127 | #a = plpva.plpva(var,0.75,'xmin',0.75) |
---|
128 | #print a |
---|
129 | |
---|
130 | |
---|
131 | h = plplot.plplot(var,xmin,alpha) |
---|
132 | |
---|
133 | ppplot.save(mode="png") |
---|
134 | |
---|
135 | #mpl.loglog(h[0], h[1], 'k--',linewidth=2) |
---|
136 | |
---|
137 | #mpl.show() |
---|
138 | |
---|
139 | |
---|
140 | |
---|