-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLISA_gap_fill.py
349 lines (311 loc) · 10.7 KB
/
LISA_gap_fill.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
import sys
import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack
from datetime import datetime, timedelta
import time
import random
import math
from tqdm import tqdm
#load data
fnamedat='datafiles/g2_alldat_neg1001.txt'
cols=2
truncate=True
if len(sys.argv) == 2:
fnamedat = sys.argv[1]
elif len(sys.argv) == 3:
fnamedat = sys.argv[1]
cols = sys.argv[2]
elif len(sys.argv) == 4:
fnamedat = sys.argv[1]
cols = sys.argv[2]
truncate = False
elif len(sys.argv) == 1:
print("_testing, 2000 data_")
else:
sys.stderr.write(f'filtering {fnamedat}')
#truncate=False
if truncate==True:
file = open(f'datafiles/{fnamedat[10:-4]}_Filtered_Data.txt',"a")
file.truncate(0)
file.close()
data=np.loadtxt(fname=fnamedat, usecols=range(int(cols)))
#splitting data into individual colums
lisa=np.array(data[0:, 1])
#time=np.array(data[0, :0])
def main():
plt.rcParams["figure.figsize"] = (16,11)
global LISA_Data
lisa_original=np.array(lisa)
lisad=filter_data(lisa, 1)
plot_check_filtered_data(lisad, lisa_original, "LISA Data")
write_file(lisad)
'''
def filter3(the_array):
bad_dataL=[]
for i in tqdm(range(the_array.size)):
if the_array[i] < 0:
bad_dataL.append(i)
return bad_dataL
'''
def filter1(the_array):
bad_dataL=[]
for i in tqdm(range(the_array.size)):
if the_array[i] < -1000:
bad_dataL.append(i)
return bad_dataL
def filter2(the_array):
bad_dataL=[]
for i in tqdm(range(the_array.size-2)):
valdiff=((the_array[i]+the_array[i+2])/2-the_array[i+1])
if valdiff<-1000 or valdiff > 1000:
bad_dataL.append(i)
return bad_dataL
def cValid(test_array, bstart, bcl2, filterval):
if filterval ==1:
for i in range(25):
what=25-i
if test_array[1+i+bstart+bcl2] < -1000:
return False
if test_array[bstart-what] < -1000:
return False
elif filterval ==2:
return True
return True
def bValid(test_array, bstart, bcl2, filterval):
if filterval ==1:
for i in range(bcl2):
what=bcl2-i
if test_array[1+i+bstart+bcl2] < -1000:
return False
if test_array[bstart-what] < -1000:
return False
elif filterval ==2:
return True
return True
def pop_bad(bd, bcl):
for i in range(bcl):
bd.pop(0)
return bd
def gap_fill(array, bad_data, done, filterv):
extra_bad=False
while len(bad_data)>0:
n=1
bad_chunk_length=1
while len(bad_data)>n:
if bad_data[n-1]+1==bad_data[n]:
bad_chunk_length+=1
n+=1
else:
break
# if bad_data[0]>52000 and bad_data[0]<52500:
# if bad_chunk_length>1:
# print(bad_chunk_length, bad_data[0])
# if bad_data[0]>63000 and bad_data[0]<66000:
# print[]
#print(bad_chunk_length, bad_data[0])
# if bad_chunk_length>1:
# print(bad_chunk_length, bad_data[0])
if array.size > bad_data[0]+bad_chunk_length:
if bad_chunk_length == 1:
array=TypeA_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
elif bad_chunk_length<25 and bad_chunk_length>1:
bCheck=bValid(array, bad_data[0], bad_chunk_length, filterv)
if bCheck:
array=TypeB_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
#print("YEAH")
if done:
array=TypeA_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
extra_bad=True
bad_data=pop_bad(bad_data, bad_chunk_length)
elif bad_chunk_length>25 and array.size > bad_data[0]+bad_chunk_length+25 and bad_data[0]-25>0:
cCheck=cValid(array, bad_data[0], bad_chunk_length, filterv)
if cCheck:
array=TypeC_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
if done:
bCheck=bValid(array, bad_data[0], bad_chunk_length, filterv)
if bCheck:
array=TypeB_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
TypeA_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
extra_bad=True
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
bCheck=bValid(array, bad_data[0], bad_chunk_length, filterv)
if bCheck:
array=TypeB_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
#print("YEAH")
if done:
array=TypeA_gap(bad_data[0], array, bad_chunk_length)
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
extra_bad=True
bad_data=pop_bad(bad_data, bad_chunk_length)
else:
x1=array[bad_data[0]-1]
for i in range(bad_chunk_length):
array[bad_data[0]]=x1
bad_data.pop(0)
return array, extra_bad
def filter_data(original_array, num):
bd=[]
bad=0
n=1
#print(original_array)
if num == 1:
bd=filter1(original_array)
elif num == 2:
bd=filter2(original_array)
even_worse=False
terrible=False
#for i in range(3000):
#print(original_array[63000+i])
# for i in range(25):
# asdf=25-i
# print(original_array[63148-asdf])
# for i in range(25):
# print(original_array[65683+i])
new_array, terrible=gap_fill(original_array, bd, False, num)
# print("TEST___________________------____")
if terrible==True:
if num == 1:
bd=filter1(new_array)
elif num == 2:
bd=filter2(new_array)
newer_array, even_worse=gap_fill(new_array, bd, False, num)
else:
newer_array=new_array
if even_worse==True:
if num == 1:
bd=filter1(newer_array)
elif num == 2:
bd=filter2(newer_array)
final, dc=gap_fill(newer_array, bd, True, num)
else:
final=newer_array
#print(array)
#numpy.interp
return final
def TypeA_gap(badstart, gap_array, bad_cl):
x1=gap_array[badstart-1]
x2=gap_array[badstart+bad_cl]
z=(x2-x1)/bad_cl
for i in range(bad_cl):
gap_array[badstart+i]=x1+z*(1+i)
return gap_array
def TypeB_gap(badstart, gap_array, bad_cl):
x1=gap_array[badstart-1]
x2=gap_array[badstart+bad_cl]
z=(x2-x1)/bad_cl
sd1=np.array([])
sd2=np.array([])
popsize=bad_cl
for i in range(bad_cl):
hwat=bad_cl-i
sd1=np.append(sd1, float(gap_array[badstart-hwat]))
sd2=np.append(sd2, float(gap_array[badstart+bad_cl+i]))
sd1=np.std(sd1)
sd2=np.std(sd2)
for i in range(bad_cl):
filler=sd1*(bad_cl-i)/(bad_cl+1)+sd2*(i+1)/(bad_cl+1)
gap_array[badstart+i]=x1+z*(1+i)+filler
return gap_array
def TypeC_gap(badstart, gap_array, bad_cl):
x1=gap_array[badstart-1]
x2=gap_array[badstart+bad_cl]
z=(x2-x1)/bad_cl
gap_filler=np.array([])
hanningWin=np.hanning(50)
nnj=1
if bad_cl>50:
nnj=bad_cl//50
if nnj < 1:
nnj=1
if nnj > 30:
nnj=30
hanningWin=np.hanning(nnj*50+1)
hanningWin=hanningWin[1:]
#print(hanningWin.size)
#if badstart>63000 and badstart<66000:
# print("\n\n\n\n\n_______________________")
# for i in range(50):
# print(hanningWin[i], end = " ")
# print("__________________________\n\n\n\n\n")
for extra in range(nnj):
add=extra*50
for i in range(25):
tmpval=25-i
filler=0.5*gap_array[badstart-tmpval]*float(hanningWin[i+add])
gap_filler=np.append(gap_filler, filler)
for i in range(25):
j=25+i
filler=0.5*gap_array[badstart+bad_cl+1+i]*float(hanningWin[j+add])
gap_filler=np.append(gap_filler, filler)
for i in range(bad_cl):
gap_array[badstart+i]=gap_filler[i%len(gap_filler)]##+x1+z*(1+i) #AAAAAAAAAAAAAAAAAAAAAAAAH
return gap_array
'''
if badstart>63000 and badstart<66000:
# print[]
#print(bad_chunk_length, bad_data[0])
if bad_cl>1:
for i in range(25):
tmpval=25-i
filler=0.5*gap_array[badstart-tmpval]*float(hanningWin[i+add])
print(filler, end=" ")
print("\n")
for i in range(25):
j=25+i
filler=0.5*gap_array[badstart+bad_cl+1+i]*float(hanningWin[j+add])
print(filler, end=' ')
'''
#print(bad_cl, badstart)
def saveplot(title, filetypes):
for ftype in filetypes:
filename=f'{title}.{ftype}'
print(f'saving file {filename}')
plt.savefig(filename)
def plot_check_filtered_data(array, original, name):
figure, axis = plt.subplots(2,1)
axis[0].plot(array)
axis[0].set_title(f"ACE Filtered {name} Data")
#scale/unit of signal of time is currently unknown
plt.setp(axis[0], xlabel="Just Indecies(about 64 second sampling freq)")
plt.setp(axis[0], ylabel=f"{name}")
axis[1].plot(original)
axis[1].set_title(f"ACE UnFiltered {name} Data")
#scale/unit of signal of time is currently unknown
plt.setp(axis[1], xlabel="Just Indecies(about 64 second sampling freq)")
plt.setp(axis[1], ylabel=f"{name}")
#saving plot
ftypes=['jpg']
#ftypes=['png']
name=name.replace(" ", "_")
saveplot(f'plots/filtertest/{fnamedat[10:-4]}_filtered_{name}', ftypes)
plt.show()
def write_file(pd):
print("writing to", f'datafiles/{fnamedat[10:-4]}_Filtered_Data.txt')
file = open(f'datafiles/{fnamedat[10:-4]}_Filtered_Data.txt',"a")
#file.write("pd, apr, ps, xdGSE, ydGSE, zdGSE, xpGSE, ypGSE, zpGSE")
#file.write("\n")
for i in tqdm(range(pd.size)):
#file.write(str(time[i]))
#file.write("\t")
file.write(str(pd[i]))
file.write("\n")
file.close()
if __name__ == '__main__':
main()