-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaduncertspline.py
executable file
·233 lines (188 loc) · 5.74 KB
/
readuncertspline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
import numpy as np
import sys
from io import StringIO
from glob import glob
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
import xml.etree.ElementTree as ET # in python >=2.5
def deriv_spl(x, y, d0, dn):
""" Return the derivative at each point, for the spline
x and y defines the function
d0 derivative at the first point (>1E33 = 0)
dn derivative for the last point
"""
if len(x) != len(y):
raise ValueError("fait Chier")
dy = np.zeros(len(y))
u = np.zeros(len(y))
if(d0 > 1E33):
dy[0] = 0
u[0] = 0
else:
dy[0] = -0.5
u[0] = (3./(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0]) - d0)
for i in range(1, len(y)-1):
sig = (x[i]-x[i-1])/(x[i+1]-x[i-1])
p = sig*dy[i-1]+2.
dy[i] = (sig-1.)/p
u[i] = (y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1])
u[i] = (6.*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p
qn = 0.
if(dn > 1E33):
u[-1] = 0
else:
qn = 0.5
u[-1] = (3./(x[-1]-x[-2]))*(dn - (y[-1]-y[-2])/(x[-1]-x[-2]))
dy[-1] = (u[-1]-qn*u[-2])/(qn*dy[-2]+1.)
tmp = list(range(len(y)-1))
tmp.reverse()
print(dy)
for i in tmp:
dy[i] = dy[i]*dy[i+1]+u[i]
print(dy)
return dy
def splint(x, y, dy, newx):
""" Interpolate the function x-y at newx. use the derivative computed by the precedent function """
klo = 0
khi = len(y)-1
while(khi-klo > 1):
k = (khi+klo) >> 1
if(x[k] < newx):
klo = k
else:
khi = k
h = x[khi]-x[klo]
if(h == 0):
raise ValueError("et encore merde")
a = (x[khi]-newx)/h
b = (newx-x[klo])/h
return a*y[klo]+b*y[khi]+((a**3-a)*dy[klo]+(b**3-b)*dy[khi])*h**2/6.
def spl_interp(x, y, d0, dn, newx):
dy = deriv_spl(x, y, d0, dn)
newy = np.zeros(len(newx))
for i in range(len(newx)):
newy[i] = splint(x, y, dy, newx[i])
return newy
# return dy
def spl_interpn(x, y, newx):
d0 = (y[1]-y[0])/(x[1]-x[0])
dn = (y[-1]-y[-2])/(x[-1]-x[-2])
dy = deriv_spl(x, y, d0, dn)
newy = np.zeros(len(newx))
for i in range(len(newx)):
newy[i] = splint(x, y, dy, newx[i])
return newy
# return dy
def spl_interpexp(x, y, newx):
return np.exp(spl_interpn(x[::-1], y[::-1], newx))
def GetArray(vNode, pname):
# print vNode.find(pname).text
return np.loadtxt(StringIO(vNode.find(pname).text.replace("\n", " ")))
def Getvalue(vNode, pname):
return float(vNode.find(pname).text)
def ReadReport(repname):
root = ET.parse(repname).getroot()
chi2v = Getvalue(root, "chi2v")
calibration = Getvalue(root, "calibration")
print("Chi2 : ", chi2v, "Calibration", calibration)
alts = GetArray(root, "Specie/altitudes")
vals = GetArray(root, "Specie/logvalues")
return alts, vals, chi2v, calibration
def printhisto(data):
plt.hist(data, 10)
def finderro(data):
mu = data.mean()
sigma = data.std()
# mu,sigma=norm.fit(data)
return mu, sigma
def plotnaltuncert(data, alt, nolog=False):
newl = len(alt)
mu = np.zeros((newl))
sig = np.zeros((newl))
for i in range(newl):
mu[i], sig[i] = finderro(data[i, :])
if not nolog:
plt.xscale("log")
plt.errorbar(mu, alt, xerr=sig)
def ExpProfile(pvals, z):
ValMax = pvals[0]
Texo = pvals[1]
alt0 = 120
R = 3396.2
mamu = 44.001
go = 3.71
SCALEH_CONST = 8.3144727E5
gamma = mamu*go*(1+alt0/R)**(-2)/(SCALEH_CONST*Texo)*1E5
H = 1/gamma
return ValMax*np.exp(-(z-alt0)/H)
def ExpMin(pvals, z, ycompar):
return (ycompar-ExpProfile(pvals, z))/ycompar
def expe(zalts, zmeasu):
pretrieve = [1E11, 300]
pfinal = leastsq(ExpMin, pretrieve, args=(zalts, zmeasu))
# print "Initial :",pretrieve
print("Retrieved : ", pfinal)
# xscale("log")
# plot(zmeasu,zalts,label="Data")
# plot(ExpProfile(pfinal[0],zalts),zalts,label="Retrieved")
# legend()
# show()
dens0, texo = pfinal[0]
print(dens0, texo)
return dens0, texo
def findrange(alt, alt0):
""" returns the min, max range in altitude so that we are above alt0"""
# pos = -1
if alt[-1] > alt[0]:
rmax = len(alt)-1
rmin = rmax
while(rmin > -1 and alt[rmin] > alt0):
rmin -= 1
else:
rmin = 0
rmax = rmin
while(rmax < len(alt) and alt[rmax] > alt0):
rmax += 1
return rmin, rmax
def ErrorTexo(data, alt, rmin, rmax):
newl = len(data[0, :])
dens0 = np.zeros((newl))
Texo = np.zeros((newl))
for i in range(newl):
dens0[i], Texo[i] = expe(alt[rmin:rmax], data[rmin:rmax, i])
print("Density uncertainty:", finderro(dens0))
print("Texo uncertainty:", finderro(Texo))
if "__main__" == __name__:
print("Salut les gars")
files = glob("RapportFit.xml*")
print(files)
newalts = np.arange(80, 200, 1)
# xscale("log")
if not (len(files) > 0):
sys.exit()
altitude, v, c2, ca = ReadReport(files[0])
values = np.zeros((len(altitude), len(files)))
newvalues = np.zeros((len(newalts), len(files)))
chi = np.zeros(len(files))
calibration = np.zeros(len(files))
j = 0
for i in files:
print(i)
alts, vals, chis, cals = ReadReport(i)
values[:, j] = vals
chi[j] = chis
calibration[j] = cals
newvalues[:, j] = spl_interpexp(alts, vals, newalts)
# plot(spl_interpexp(alts,vals,newalts),newalts)
j += 1
rmin, rmax = findrange(newalts, 135)
print(rmin, rmax, newalts[rmin], newalts[rmax])
print(newalts)
ErrorTexo(newvalues, newalts, rmin, rmax)
# printhisto(calibration)
# plotnaltuncert(newvalues,newalts)
# plotnaltuncert(values,altitude,True)
# print altitude
# print values
# show()