-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.py
266 lines (217 loc) · 9 KB
/
MainWindow.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
from datetime import datetime
import time
import webbrowser
from UIMainWindowForm import Ui_MainWindowForm
from PyQt5.QtWidgets import QMainWindow, QFileDialog
from PyQt5.QtCore import QSettings
import AboutDialog
import DbUpdate
import app_info
import app_settings
import common
import db
from FixedWidthTextParser.Seismic.SpsParser import Sps21Parser, Point
from Warning import Warning
CHECK_CSV_HEADER = 'line,point,easting,northing,check_easting,check_northing,error_easting,error_northing'
CHECK_EXT = '.check.csv'
NOT_IN_DB_CSV_HEADER = 'line,point,easting,northing'
NOT_IN_DB_EXT = '.not-in-db.csv'
LOG_EXT = '.check.log'
def about():
"""
Displays Application About Dialog
"""
dlg = AboutDialog.AboutDialog()
dlg.exec_()
def help():
webbrowser.open(app_info.HELP_URL)
def license():
webbrowser.open(app_info.LICENSE_URL)
def db_update():
"""
Displays Dialog
"""
dlg = DbUpdate.DbUpdate()
dlg.exec_()
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindowForm()
self.ui.setupUi(self)
self.ui.actionOpen_DB_File.triggered.connect(self.open_db)
self.ui.actionOpen_SPS_File.triggered.connect(self.open_sps)
self.ui.actionProcess.triggered.connect(self.process)
self.ui.actionAbout.triggered.connect(about)
self.ui.actionLicense.triggered.connect(license)
self.ui.actionHelp.triggered.connect(help)
self.ui.actionQuit.triggered.connect(self.close_application)
self.ui.actionCreate_DB.triggered.connect(self.create_db_file)
self.ui.actionUpdate_DB.triggered.connect(db_update)
# status tips copied from tool tip ?
# self.ui.actionOpen_DB_File.setStatusTip(self.ui.actionOpen_DB_File.toolTip())
self.ui.lblDb.setText('')
self.ui.lblSPS.setText('')
self.db_file = None
self.sps_file = None
self.log_file = None
self.settings = QSettings(app_settings.ORG, app_settings.APP)
self.settings_read()
def warning_nothing_to_process(self):
dlg = Warning()
dlg.set_label('Are you dumb ?\nWhat do you want to process ?\nSelect both DB & SPS files')
dlg.exec_()
def open_db(self):
"""
Opens DB file
"""
previous_db_file = self.db_file
self.db_file, _ = QFileDialog.getOpenFileName(
self,
"Open file",
"",
"SQLite (*.sqlite );;All files (*.*)",
# options=QFileDialog.DontUseNativeDialog
)
if not self.db_file:
self.db_file = previous_db_file
self.ui.lblDb.setText(self.db_file)
def open_sps(self):
"""
Opens SPS file
"""
previous_sps_file = self.sps_file
self.sps_file, _ = QFileDialog.getOpenFileName(
self,
"Open file",
"",
"SPS Source (*.S *.SPS *.s *.sps);;SPS Receiver (*.R *.RPS *.r *.rps);;All files (*.*)",
# options=QFileDialog.DontUseNativeDialog
)
if not self.sps_file:
self.sps_file = previous_sps_file
self.ui.lblSPS.setText(self.sps_file)
self.log_file = self.sps_file + LOG_EXT
def process(self):
if self.db_file and self.sps_file:
self.runner()
else:
self.warning_nothing_to_process()
def runner(self):
common.log_file_create(self.log_file)
start_time = time.time()
if self.db_file and self.sps_file:
line_numbers = common.count_file_line_number(self.sps_file)
self.ui.progressBar.setMaximum(line_numbers)
limit_x = float(self.ui.limitX.text())
limit_y = float(self.ui.limitY.text())
self.log(datetime.today().strftime("%Y-%m-%d %H:%M:%S"))
self.log('DB: %s' % self.db_file)
self.log('DB record count: %d' % db.count_db_records(self.db_file))
self.log('SPS: %s' % self.sps_file)
self.log('SPS record count: %d' % line_numbers)
self.log('Limits: E: %.2f, N: %.2f' % (limit_x, limit_y))
result = self.check(limit_x, limit_y)
timing = time.time() - start_time
point_count = result['PC']
not_in_db_count = result['NDB']
offset_count_x = result['OCX']
offset_count_y = result['OCY']
poffset_count_x = offset_count_x / point_count * 100
poffset_count_y = offset_count_y / point_count * 100
self.log('Number of points: %d' % point_count)
self.log('Number of points not in DB: %d' % not_in_db_count)
self.log('Errors Easting: %d points, %.2f%s' % (offset_count_x, poffset_count_x, '%'))
self.log('Errors Nothing: %d points, %.2f%s' % (offset_count_y, poffset_count_y, '%'))
self.log('Elapsed time: %d sec' % timing)
self.print2output('-----------------------------------------------------------------')
def print2output(self, text):
self.ui.txtOutput.append(text)
def log(self, text):
self.print2output(text)
common.log_file_record_add(self.log_file, text)
def settings_save(self):
self.settings.setValue(app_settings.LIMIT_X, self.ui.limitX.text())
self.settings.setValue(app_settings.LIMIT_Y, self.ui.limitY.text())
self.settings.setValue(app_settings.DB_PATH, self.db_file)
def settings_read(self):
self.ui.limitX.setText(self.settings.value(app_settings.LIMIT_X, '0.0', type=str))
self.ui.limitY.setText(self.settings.value(app_settings.LIMIT_Y, '0.0', type=str))
db_file = self.settings.value(app_settings.DB_PATH, '', type=str)
if 0 == len(db_file):
db_file = None
self.db_file = db_file
self.ui.lblDb.setText(db_file)
def close_application(self):
self.settings_save()
self.close()
def create_db_file(self):
db_file, _ = QFileDialog.getSaveFileName(
self,
"Create DB",
"",
"SQLite files (*.sqlite);;All Files (*)",
# options=QFileDialog.DontUseNativeDialog
)
if not db_file:
return
if not db_file.endswith(db.DB_FILE_EXT):
db_file += db.DB_FILE_EXT
db.create_db(db_file)
db_log_file = db_file + common.DB_LOG_FILE_EXT
common.log_file_create(db_log_file)
common.log_file_record_add(db_log_file, f"Created: {datetime.today().strftime('%Y-%m-%d %H:%M:%S')}")
common.log_file_record_add(db_log_file, '-----------------------------------------------------------------')
def check(self, limit_x, limit_y):
connection = db.create_connection(self.db_file)
parser = Sps21Parser()
check_file = self.sps_file + CHECK_EXT
not_in_db_file = self.sps_file + NOT_IN_DB_EXT
common.csv_file_create(check_file, CHECK_CSV_HEADER)
common.csv_file_create(not_in_db_file, NOT_IN_DB_CSV_HEADER)
with open(self.sps_file) as sps:
line = sps.readline()
sps_counter = 0
not_in_db_counter = 0
offset_counter_x = 0
offset_counter_y = 0
while line:
stats = parser.parse_point(line)
if stats is not None:
point = Point(stats)
stats = db.get_record_for_point(connection, point)
if stats is None:
sps_counter += 1
not_in_db_counter += 1
record = "%.2f,%.2f,%.1f,%.1f" \
% (
point.line, point.point, point.easting, point.northing)
common.csv_file_record_add(not_in_db_file, record)
line = sps.readline()
continue
de = point.easting - stats[0]
dn = point.northing - stats[1]
error_easting = 0
if abs(de) > limit_x:
offset_counter_x += 1
error_easting = 1
error_northing = 0
if abs(dn) > limit_y:
offset_counter_y += 1
error_northing = 1
record = "%.2f,%.2f,%.1f,%.1f,%.1f,%.1f,%d,%d" \
% (
point.line, point.point, point.easting, point.northing, de, dn, error_easting,
error_northing)
common.csv_file_record_add(check_file, record)
sps_counter += 1
self.ui.progressBar.setValue(sps_counter)
line = sps.readline()
sps.close()
stats = {
'PC': sps_counter,
'NDB': not_in_db_counter,
'OCX': offset_counter_x,
'OCY': offset_counter_y
}
connection.close()
return stats