-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxtparser.py
executable file
·183 lines (156 loc) · 6.74 KB
/
txtparser.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
# -*- coding: utf-8 -*-
#
# # Dobroslav P. Egorov
# # Kotel'nikov Institute of Radio-engineering and Electronics of RAS
# # 2019
#
from session import Session, Point
from typing import Tuple
import re
import time
import settings
from switch import Switch
from termcolor import colored
from borland_datetime import TDateTime
from interquartile import Eliminate
class TFile:
def __init__(self, path: str, mode: str = ''):
self.mode = mode
if not mode:
self.mode = 'standard'
self.path = path
self.session = Session()
@staticmethod
def __parse_line(text: str) -> list:
return [elem for elem in re.split("[\t ]", re.sub("[\r\n]", '', text)) if elem]
def parse(self, shift=True, rm_zeros=True, sort_freqs=True, sort_time=False,
outliers_elimination=False, upper_threshold_val: float = None) -> None:
print("Loading data from txt...\t", end='', flush=True)
start_time = time.time()
print('Current parser mode: ' + self.mode)
with Switch(self.mode) as case:
if case("standard"):
# стандартный режим
t_file = open(self.path, "r", encoding='cp1251')
t_file.readline()
for line in t_file:
try:
l_data = TFile.__parse_line(line)
YYYY, MM, DD = l_data[0].split('.')
hh, mm, ss = l_data[1].split(':')
ms = l_data[2]
timestamp = TDateTime(YYYY, MM, DD, hh, mm, ss, ms).toDouble()
self.session.add(float(l_data[5])/1000, Point(timestamp, float(l_data[6])))
self.session.add(float(l_data[7])/1000, Point(timestamp, float(l_data[8])))
except ValueError:
continue
t_file.close()
if case("DDMMYY"):
t_file = open(self.path, "r", encoding='cp1251')
t_file.readline()
for line in t_file:
try:
l_data = TFile.__parse_line(line)
DD, MM, YY = l_data[0].split('.')
YYYY = str(int(YY) + 2000)
hh, mm, ss = l_data[1].split(':')
ms = l_data[2]
timestamp = TDateTime(YYYY, MM, DD, hh, mm, ss, ms).toDouble()
self.session.add(float(l_data[5])/1000, Point(timestamp, float(l_data[6])))
self.session.add(float(l_data[7])/1000, Point(timestamp, float(l_data[8])))
except ValueError:
continue
t_file.close()
if case("p"):
# по процессорному времени
t_file = open(self.path, "r")
t_file.readline()
for line in t_file:
l_data = TFile.__parse_line(line)
timestamp = float(l_data[3])
self.session.add(float(l_data[5])/1000, Point(timestamp, float(l_data[6])))
self.session.add(float(l_data[7])/1000, Point(timestamp, float(l_data[8])))
t_file.close()
if case("g1"):
# режим гетеродин|1|x|
t_file = open(self.path, "r")
t_file.readline()
for line in t_file:
l_data = TFile.__parse_line(line)
YYYY, MM, DD = l_data[0].split('.')
hh, mm, ss = l_data[1].split(':')
ms = l_data[2]
timestamp = TDateTime(YYYY, MM, DD, hh, mm, ss, ms).toDouble()
self.session.add(float(l_data[4])/1000, Point(timestamp, float(l_data[6])))
t_file.close()
if case("g2"):
# режим гетеродин|x|1|
t_file = open(self.path, "r")
t_file.readline()
for line in t_file:
l_data = TFile.__parse_line(line)
YYYY, MM, DD = l_data[0].split('.')
hh, mm, ss = l_data[1].split(':')
ms = l_data[2]
timestamp = TDateTime(YYYY, MM, DD, hh, mm, ss, ms).toDouble()
self.session.add(float(l_data[4])/1000, Point(timestamp, float(l_data[8])))
t_file.close()
print('{:.3f} sec\t'.format(time.time() - start_time), end='')
print('[' + colored('OK', 'green') + ']')
if shift:
print('Overlay elimination...\t')
self.shift()
if rm_zeros:
print('Removing zeros...\t')
self.remove_zeros()
if sort_freqs:
print('Frequency sorting...\t')
self.sort_frequencies()
if sort_time:
print('Time sorting...\t')
self.sort_time()
if outliers_elimination:
print('Outliers elimination...\t')
self.outliers_elimination()
if upper_threshold_val:
print('Setting threshold...\t')
self.set_upp_threshold(upper_threshold_val)
def shift(self) -> None:
s_arr = settings.parameter.freqs.shifted
ssize = self.session.get_series(settings.parameter.freqs.all[0]).length
msize = self.session.get_series(s_arr[0]).length
for f in s_arr:
s = self.session.get_series(f)
if s.is_empty:
continue
if s.length > 1.25 * ssize:
k = 0
data = []
for i in range(0, s.length - 1, 2):
p = s.data[i]
p.merge(s.data[i+1])
data.append(p)
k += 1
self.session.replace(s.key, data)
if k < msize:
msize = k
def remove_zeros(self) -> None:
self.session.remove_zeros(timeQ=True, valQ=True)
def sort_frequencies(self) -> None:
self.session.sort()
def remove_time_zeros(self) -> None:
self.session.remove_zeros(timeQ=True, valQ=False)
def remove_val_zeros(self) -> None:
self.session.remove_zeros(timeQ=False, valQ=True)
def sort_time(self) -> None:
self.session.time_sorting()
def getData(self) -> Session:
return self.session
def getTimeBounds(self) -> Tuple[float, float]:
return self.session.get_time_bounds()
def outliers_elimination(self) -> None:
self.session.apply_to_series(Eliminate.time_series)
def set_upp_threshold(self, threshold: float) -> None:
self.session.set_upper_threshold(threshold)
def cutData(self, start_t: float, stop_t: float) -> None:
self.session.cut(start_t, stop_t)