-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFDAT_main.py
341 lines (282 loc) · 11.9 KB
/
FDAT_main.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
# main.py
# to handle matplotlib configs
from matplotlib_setup import configure_matplotlib
configure_matplotlib()
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QPushButton, QLineEdit,
QFrame, QGridLayout, QMessageBox, QSizePolicy
)
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QFont
from datetime import datetime, timedelta
import sys
import os
import logging
import math
# Configure logging once at the application level
logging.basicConfig(
level=logging.ERROR, # Change from DEBUG to ERROR
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('FDAT.log', encoding='utf-8'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Silence matplotlib warnings
logging.getLogger('matplotlib').setLevel(logging.ERROR)
logging.getLogger('matplotlib.font_manager').setLevel(logging.ERROR)
logging.getLogger('matplotlib.pyplot').setLevel(logging.ERROR)
logging.getLogger('matplotlib.legend').setLevel(logging.ERROR)
# Import custom modules
from plot_window import PlotWindow
from data_manager import DataManager
from settings_manager import SettingsManager
class StartWindow(QMainWindow):
def __init__(self, callback):
super().__init__()
self.callback = callback
self.selected_satellite = None
self.settings_manager = SettingsManager()
self.setup_ui()
def setup_ui(self):
self.setWindowTitle("ForbMod Analysis Tool")
self.setMinimumSize(800, 600)
# Main widget and layout
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# Header
header = self.create_header()
layout.addWidget(header)
# Date selection
date_frame = self.create_date_section()
layout.addWidget(date_frame)
# Satellite selection
satellite_frame = self.create_satellite_section()
layout.addWidget(satellite_frame)
# Analyze button
self.analyze_btn = QPushButton("Start Analysis")
self.analyze_btn.setFont(QFont("Arial", 12, QFont.Bold))
self.analyze_btn.setMinimumHeight(50)
self.analyze_btn.clicked.connect(self.on_analyze_clicked)
layout.addWidget(self.analyze_btn)
self.apply_styles()
def create_header(self):
header = QFrame()
header.setFrameShape(QFrame.StyledPanel)
layout = QVBoxLayout(header)
title = QLabel("ForbMod Analysis Tool")
title.setFont(QFont("Arial", 16, QFont.Bold))
title.setAlignment(Qt.AlignCenter)
description = QLabel("Analyze ICME/Forbush decrease events")
description.setAlignment(Qt.AlignCenter)
# Add observer name input
observer_layout = QHBoxLayout()
observer_label = QLabel("Observer:")
self.observer_input = QLineEdit()
self.observer_input.setText(self.settings_manager.get_observer_name())
self.observer_input.textChanged.connect(self.save_observer_name)
observer_layout.addWidget(observer_label)
observer_layout.addWidget(self.observer_input)
layout.addWidget(title)
layout.addWidget(description)
layout.addLayout(observer_layout)
return header
def save_observer_name(self):
"""Save observer name when changed"""
self.settings_manager.set_observer_name(self.observer_input.text())
def create_date_section(self):
frame = QFrame()
frame.setFrameShape(QFrame.StyledPanel)
layout = QGridLayout(frame)
# Date inputs
start_label = QLabel("Start Date:")
self.start_date_input = QLineEdit()
self.start_date_input.setPlaceholderText("YYYY/MM/DD")
self.start_date_input.setText("1997/10/09") # Default date
end_label = QLabel("End Date:")
self.end_date_input = QLineEdit()
self.end_date_input.setPlaceholderText("YYYY/MM/DD")
self.end_date_input.setText("1997/10/14") # Default date
# Navigation buttons
nav_layout = QHBoxLayout()
for days, text in [(-5, "◀ 5 days"), (-2, "◀ 2 days"),
(2, "2 days ▶"), (5, "5 days ▶")]:
btn = QPushButton(text)
btn.clicked.connect(lambda checked, d=days: self.adjust_dates(d))
nav_layout.addWidget(btn)
layout.addWidget(start_label, 0, 0)
layout.addWidget(self.start_date_input, 0, 1)
layout.addWidget(end_label, 1, 0)
layout.addWidget(self.end_date_input, 1, 1)
layout.addLayout(nav_layout, 2, 0, 1, 2)
return frame
def create_satellite_section(self):
frame = QFrame()
frame.setFrameShape(QFrame.StyledPanel)
layout = QVBoxLayout(frame)
label = QLabel("Select Satellite:")
label.setFont(QFont("Arial", 10, QFont.Bold))
layout.addWidget(label)
grid = QGridLayout()
satellites = {
"SolO": "Apr 2020 - Jul 2024",
"OMNI": "Jan 2007 - Dec 2019",
"ACE": "Sep 1997 - Dec 2022",
"WIND": "Nov 1994 - Sep 2024",
"Helios2": "Jan 1976 - Mar 1980",
"Helios1": "Dec 1974 - Jun 1981"
}
self.satellite_buttons = {}
for i, (sat, years) in enumerate(satellites.items()):
btn = QPushButton(f"{sat}\n{years}")
btn.setCheckable(True)
btn.clicked.connect(lambda checked, s=sat: self.select_satellite(s))
btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
btn.setMinimumHeight(60)
grid.addWidget(btn, i//3, i%3)
self.satellite_buttons[sat] = btn
layout.addLayout(grid)
return frame
def apply_styles(self):
self.setStyleSheet("""
QMainWindow {
background-color: #f0f0f0;
}
QFrame {
background-color: white;
border-radius: 5px;
margin: 5px;
padding: 10px;
}
QPushButton {
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
padding: 8px;
min-width: 100px;
}
QPushButton:hover {
background-color: #1976D2;
}
QPushButton:checked {
background-color: #1565C0;
}
QLineEdit {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
QLabel {
color: #333;
}
""")
def select_satellite(self, satellite):
for sat, btn in self.satellite_buttons.items():
btn.setChecked(sat == satellite)
self.selected_satellite = satellite
def adjust_dates(self, days):
try:
start_date = datetime.strptime(self.start_date_input.text(), "%Y/%m/%d")
end_date = datetime.strptime(self.end_date_input.text(), "%Y/%m/%d")
start_date += timedelta(days=days)
end_date += timedelta(days=days)
self.start_date_input.setText(start_date.strftime("%Y/%m/%d"))
self.end_date_input.setText(end_date.strftime("%Y/%m/%d"))
except ValueError:
QMessageBox.warning(self, "Warning", "Please enter valid dates first.")
def on_analyze_clicked(self):
try:
if not self.selected_satellite:
QMessageBox.warning(self, "Warning", "Please select a satellite first.")
return
start_date = datetime.strptime(self.start_date_input.text(), "%Y/%m/%d")
end_date = datetime.strptime(self.end_date_input.text(), "%Y/%m/%d")
if end_date <= start_date:
QMessageBox.warning(self, "Warning", "End date must be after start date.")
return
# Pass observer name along with other data
self.callback(
self.selected_satellite,
start_date,
end_date,
self.observer_input.text()
)
except ValueError:
QMessageBox.warning(self, "Warning", "Please enter valid dates in YYYY/MM/DD format.")
class ForbModApp:
def __init__(self):
self.data_manager = DataManager()
self.settings_manager = SettingsManager()
self.start_window = None
self.plot_window = None
self.observer_name = None
self.show_start_window()
def show_start_window(self):
self.start_window = StartWindow(callback=self.on_start_window_complete)
self.start_window.show()
def on_start_window_complete(self, satellite, start_date, end_date, observer_name):
try:
logger.info(f"Processing start window completion for {satellite}")
self.observer_name = observer_name
# Set initial parameters including directory creation
self.data_manager.set_initial_params(satellite, start_date, end_date, observer_name)
if self.start_window:
self.start_window.close()
self.show_plot_window()
except Exception as e:
logger.error(f"Error in start window completion: {str(e)}")
QMessageBox.critical(self.start_window, "Error",
f"Failed to process selection: {str(e)}")
def show_plot_window(self):
try:
self.plot_window = PlotWindow(
self.data_manager,
observer_name=self.observer_name,
on_calculate=self.on_plot_window_calculate,
on_dates_changed=self.on_dates_changed
)
self.plot_window.show()
except Exception as e:
logger.error(f"Failed to show plot window: {str(e)}")
QMessageBox.critical(None, "Error", "Failed to open plot window")
def on_plot_window_calculate(self, plot_data):
try:
self.data_manager.update_plot_data(plot_data)
logger.info("Calculation completed")
except Exception as e:
logger.error(f"Error in calculation: {str(e)}")
QMessageBox.critical(self.plot_window, "Error",
"Failed to perform calculation")
def on_dates_changed(self, new_start, new_end):
try:
self.data_manager.update_dates(new_start, new_end)
if self.plot_window:
self.plot_window.close()
self.show_plot_window()
except Exception as e:
logger.error(f"Error changing dates: {str(e)}")
QMessageBox.critical(self.plot_window, "Error",
"Failed to update dates")
def main():
try:
app = QApplication(sys.argv)
app.setStyle('Fusion')
# Set up exception handling for Qt
sys._excepthook = sys.excepthook
def exception_hook(exctype, value, traceback):
logger.error("Uncaught exception", exc_info=(exctype, value, traceback))
sys._excepthook(exctype, value, traceback)
sys.excepthook = exception_hook
forb_mod_app = ForbModApp()
return app.exec_()
except Exception as e:
logger.critical(f"Application failed to start: {str(e)}")
QMessageBox.critical(None, "Critical Error",
"Application failed to start. Check the log for details.")
return 1
if __name__ == "__main__":
sys.exit(main())