-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (170 loc) · 7.38 KB
/
main.py
File metadata and controls
214 lines (170 loc) · 7.38 KB
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
#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""
run scripts sdr_fs.py and total_spectrum_analyzer_qt5.py for all unprocessed experiments
"""
import os
import sys
import argparse
import json
import logging
import coloredlogs
import h5py
from parsers.configparser_ import ConfigParser
coloredlogs.install(level='PRODUCTION')
LOGGER = logging.getLogger('Main')
def parse_arguments():
"""
:return: dict with passed args to script
"""
parser = argparse.ArgumentParser(description='''automatically call sdr_fs.py
and total_spectrum_analyzer_qt5.py.''', epilog="""Main program.""")
parser.add_argument("source", help="Source Name", type=str)
parser.add_argument("line", help="frequency", type=int)
parser.add_argument("-c", "--config", help="Configuration "
"cfg file", type=str, default="config/config.cfg")
parser.add_argument("-v", "--version", action="version", version='%(prog)s - Version 3.0')
args = parser.parse_args()
return args
def get_args(key):
"""
:param key: argument key
:return: to script passed argument value
"""
return str(parse_arguments().__dict__[key])
def get_configs(section, key):
"""
:param section: configuration file section
:param key: configuration file sections key
:return: configuration file section key value
"""
config_file_path = get_args("config")
config = ConfigParser(config_file_path)
return config.get_config(section, key)
def get_iteration(dir_name):
"""
:param dir_name:
:return: iteration number
"""
return dir_name.split("_")[-1]
def get_station(dir_name):
"""
:param dir_name:
:return: station
"""
return dir_name.split("_")[-2]
def create_iteration_list(path, source, line):
"""
:param line: frequency
:param source: source
:param path: input file path
:return: iterations list
"""
stations = ["ir", "ib"] #list(set(create_station_list(path, source, line)))
iterations_for_source_and_line = [file for file in os.listdir(path)
if source + "_" in file and line in file and os.path.isdir(path + file)]
iterations_for_station = {station: [] for station in
stations} # {get_station(iteration):get_iteration(iteration) for iteration in os.listdir(path) if source in iteration and line in iteration and os.path.isdir(path + iteration)}
for iteration in iterations_for_source_and_line:
iterations_for_station[get_station(iteration)].append(get_iteration(iteration))
for station in stations:
iterations_for_station[station].sort(key=int, reverse=False)
return iterations_for_station
def create_station_list(path, source, line):
"""
:param line: frequency
:param source: source
:param path: input file path
:return: stations list
"""
iterations = [file for file in os.listdir(path) if
source + "_" in file and line in file and os.path.isdir(path + file)]
iterations.sort(key=get_iteration, reverse=False)
stations = [get_station(iteration) for iteration in iterations]
return stations
def create_log_file_list(path, source, line):
"""
:param line: frequency
:param path: log file path
:param source: source
:return: all log files for source
"""
return [log for log in os.listdir(path) if log.startswith(source + "_") and line in log]
def main():
"""
:return: None
"""
source_name = get_args("source")
line = get_args("line")
data_files_path = get_configs('paths', "dataFilePath")
result_path = get_configs('paths', "resultFilePath")
log_path = get_configs('paths', "logPath")
output_path = get_configs('paths', "outputFilePath")
if os.path.exists(data_files_path):
sdr_iterations = create_iteration_list(data_files_path, source_name, line)
else:
sdr_iterations = []
log_path = log_path + "SDR/"
logfile_list = create_log_file_list(log_path, source_name, line)
result_file_name = result_path + source_name + "_" + get_args("line") + ".json"
if os.path.isfile(result_file_name):
pass
else:
os.system("touch " + result_file_name)
result_file = open(result_file_name, "w")
result_file.write("{ \n" + "\n}")
result_file.close()
with open(result_file_name, "r") as result_data:
result = json.load(result_data)
stations = ["ir", "ib"]
processed_iteration = {station: [] for station in stations}
processed_iteration2 = {station: [] for station in stations}
for experiment in result:
if get_station(experiment) == "IRBENE16":
station = "ib"
else:
station = "ir"
iteration_in_result = experiment.split("_")[-1]
if iteration_in_result in sdr_iterations[station] and \
iteration_in_result not in \
processed_iteration[station] and result[experiment]["type"] == "SDR":
processed_iteration[station].append(get_iteration(experiment))
if iteration_in_result in processed_iteration[station] and \
result[experiment]["type"] == "SDR" and result[experiment]["flag"]:
processed_iteration[station].remove(iteration_in_result)
if iteration_in_result not in processed_iteration2[station] and result[experiment]["type"] == "SDR":
processed_iteration2[station].append(iteration_in_result)
for station in stations:
processed_iteration[station].sort(key=int, reverse=False)
processed_iteration2[station].sort(key=int, reverse=False)
for station in stations:
for iteration in sdr_iterations[station]:
if iteration not in processed_iteration[station]:
log_file = source_name + "_" + "f" + line + "_" + station + "_" + iteration + ".log"
sdr_fs_parameter = source_name + " " + line + " " + iteration + " " + log_file
LOGGER.info("Executing python3 " + "sdr_fs.py " + sdr_fs_parameter)
os.system("python3 " + "sdr_fs.py " + sdr_fs_parameter)
if not os.path.exists(log_path + "/" + log_file):
LOGGER.warning("Warning log file " + log_file + " do not exist")
output_files = os.listdir(output_path + "/" + line + "/" + source_name)
for output_file in output_files:
output_file_station = output_file.split("_")[-2].split(".")[0]
output_file_iteration = output_file.split("_")[-1].split(".")[0]
if output_file_station == "IRBENE16":
station = "ib"
else:
station = "ir"
if output_file_iteration not in processed_iteration2[station]:
if output_file.startswith(source_name):
with h5py.File(get_configs("paths", "outputFilePath") + get_args("line") +
"/" + get_args("source") + "/" + output_file, "r") as input_data_file:
input_file_keys = list(input_data_file.keys())
input_data_file.close()
if "amplitude" in input_file_keys:
LOGGER.info("Executing python3 " +
"total_spectrum_analyzer_qt5.py " + output_file + " " + line)
os.system("python3 " +
"total_spectrum_analyzer_qt5.py " + output_file + " " + line)
if __name__ == "__main__":
main()
sys.exit(0)