-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate_magic_constant.py
209 lines (171 loc) · 5.32 KB
/
estimate_magic_constant.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
import numpy as n
import matplotlib.pyplot as plt
import h5py
import glob
import sys
import stuffr
dirname=sys.argv[1]
calfile=sys.argv[2]
# -100 km relative to the peak ne included in fit (100 km below peak ne)
rmin=-100
# 100 km (100 km above peak ne)
rmax=100
# maximum range to include
max_rg=800
if len(sys.argv)>3:
rmin=float(sys.argv[3])
if len(sys.argv)>4:
rmax=float(sys.argv[4])
if len(sys.argv)>5:
max_rg=float(sys.argv[5])
print(rmin)
print(rmax)
# read calibration info
cal=h5py.File(calfile,"r")
# can be positive or negative
pf=n.abs(cal["plasma_frequency"][()])
#print(pf)
#print(pf.shape)
pf_ts=pf[:,0]
pf_ne = (pf[:,1]/8.98)**2.0
fl=glob.glob("%s/pp*.h5"%(dirname))
fl.sort()
#print(fl)
minimum_tx_pwr=400e3
maximum_tsys=2e3
maximum_time_offset = 300.0
debug_cal=True
nan_space_objects=0
nan_noisy_estimates=True
nt=len(fl)
h=h5py.File(fl[0],"r")
nr=len(h["rgs"][()])
rgs=h["rgs"][()]
#print(rgs)
bad_rgi=n.where(rgs > max_rg)[0]
h.close()
P=n.zeros([nt,nr,4])
tv=n.zeros(nt)
P[:,:]=n.nan
DP=n.zeros([nt,nr,4])
P[:,:]=n.nan
so_t=n.array([])
so_r=n.array([])
tv_dt=[]
tx_pwr=n.zeros(nt)
so_count=n.zeros([nt,nr],dtype=int)
tsys=n.zeros(nt)
for i in range(nt):
# print(i)
h=h5py.File(fl[i],"r")
try:
P[i,:,0]=h["Te"][()]
P[i,:,1]=h["Ti"][()]
P[i,:,2]=h["vi"][()]
P[i,:,3]=h["ne"][()]
if "P_tx" in h.keys():
tx_pwr[i]=h["P_tx"][()]
if "T_sys" in h.keys():
tsys[i]=h["T_sys"][()]
if "space_object_times" in h.keys():
so_t=n.concatenate((so_t,h["space_object_times"][()]))
so_r=n.concatenate((so_r,h["space_object_rgs"][()]))
if "space_object_count" in h.keys():
so_count[i,:]=h["space_object_count"][()]
DP[i,:,0]=h["dTe/Ti"][()]
DP[i,:,1]=h["dTi"][()]
DP[i,:,2]=h["dvi"][()]
DP[i,:,3]=h["dne"][()]/h["ne"][()]
if tx_pwr[i] < minimum_tx_pwr:
P[i,:,:]=n.nan
DP[i,:,:]=n.nan
if tsys[i] > maximum_tsys:
P[i,:,:]=n.nan
DP[i,:,:]=n.nan
except:
print("missing key in hdf5 file. probably incompable data.")
tv[i]=0.5*(h["t0"][()]+h["t1"][()])
if nan_noisy_estimates:
P[i,DP[i,:,0]>5,:]=n.nan
P[i,n.isnan(DP[i,:,0]),:]=n.nan
P[i,n.isnan(DP[i,:,1]),:]=n.nan
P[i,n.isnan(DP[i,:,2]),:]=n.nan
P[i,n.isnan(DP[i,:,3]),:]=n.nan
P[i,bad_rgi,:]=n.nan
# space object filter
P[i,so_count[i,:]>nan_space_objects,:]=n.nan
h.close()
uncal_ne = []
cal_ne = []
good_cal_idx=[]
for cal_idx in range(len(pf_ts)):
bi=n.argmin(n.abs(tv-pf_ts[cal_idx]))
dt=tv[bi]-pf_ts[cal_idx]
# print(dt)
if n.abs(dt) < maximum_time_offset:
if n.sum(n.isnan(P[bi,:,3])!=True) < 20:
print("not enough ion-line measurements")
continue
peak_rg=rgs[n.nanargmax(P[bi,:,3])]
print(peak_rg)
cal_rgi=n.where( ((rgs-peak_rg) >= rmin) & ((rgs-peak_rg) < rmax) & (n.isnan(P[bi,:,3]) != True) )[0]
if len(cal_rgi) < 2:
print("not enough ion-line measurements")
continue
good_cal_idx.append(cal_idx)
log_rg=n.log10(rgs[cal_rgi])
log_ne=n.log10(P[bi,cal_rgi,3])
n_meas=len(cal_rgi)
A=n.zeros([n_meas,3])
A[:,0]=1
A[:,1]=log_rg
A[:,2]=log_rg**2.0
xhat=n.linalg.lstsq(A,log_ne)[0]
# logne = xhat[0] + xhat[1]*log_rg + xhat[2]*log_rg**2.0
# peak is at
# dlogne/dlogrg = 0 => log_rg = -xhat[1]/(2*xhat[2])
peak_rg=-xhat[1]/2/xhat[2]
peak_ne = xhat[0] + xhat[1]*peak_rg + xhat[2]*peak_rg**2.0
rgsweep=n.linspace(n.min(log_rg),n.max(log_rg),num=100)
uncal_ne.append(10**peak_ne)
cal_ne.append(pf_ne[cal_idx])
if debug_cal:
# plt.plot(rgs[cal_rgi],P[bi,cal_rgi,3],".")
# plt.show()
plt.plot(log_rg,log_ne,".")
plt.plot(rgsweep,xhat[0]+xhat[1]*rgsweep+xhat[2]*rgsweep**2.0)
plt.axvline(peak_rg)
plt.axhline(peak_ne)
plt.show()
else:
print("no measurement found within reasonable offset from plasma lien cal (best is %1.2f s)"%(dt))
magic_consts=n.array(cal_ne)/n.array(uncal_ne)
if len(magic_consts)>2:
# print(magic_consts)
mc_mean=n.median(magic_consts)
# print(mc_mean)
mc_std=n.std(magic_consts)
print("the magic constant is %1.2f +/- %1.2f (%1.2f %%)"%(mc_mean,mc_std,100.0*mc_std/mc_mean))
if debug_cal:
plt.plot(magic_consts,".")
plt.axhline(mc_mean)
plt.show()
good_cal_idx=n.array(good_cal_idx,dtype=int)
plt.semilogy(pf_ts[good_cal_idx],mc_mean*n.array(uncal_ne),".",label="Ion-line ne")
plt.semilogy(pf_ts[good_cal_idx],n.array(cal_ne),".",label="Plasma-line ne")
plt.xlabel("Time (unix)")
plt.ylabel("Ne")
plt.legend()
plt.show()
ofile="%s/magic_const.h5"%(dirname)
print("saving %s"%(ofile))
ho=h5py.File(ofile,"w")
ho["t_min"]=n.min(pf_ts)
ho["t_max"]=n.max(pf_ts)
ho["magic_constant"]=mc_mean
ho["magic_std"]=mc_std
ho["pl_time"]=pf_ts[good_cal_idx]
ho["pl_freq"]=pf[good_cal_idx,:1]
ho.close()
else:
print("didn't find enough measurements to derive value")