111 lines
4.4 KiB
Python
111 lines
4.4 KiB
Python
|
|
|
|
from weathersettings import (logger, current_time, sys, QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
|
QLabel, QLineEdit, QPushButton, QTextEdit, QCheckBox, QPixmap, Qt, QMainWindow)
|
|
from locweatherdata import get_coordinates_from_location, get_gridpoint_forecast, get_forecast, save_data
|
|
from forecastformat import format_forecast, display_formatted_forecast
|
|
from radar import show_radar_image, find_nearest_nexrad_station, save_radar_image
|
|
|
|
|
|
class WeatherApp(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("Weather Forecast App")
|
|
self.setGeometry(100, 100, 800, 600)
|
|
|
|
central_widget = QWidget()
|
|
self.setCentralWidget(central_widget)
|
|
layout = QVBoxLayout(central_widget)
|
|
|
|
# Input section
|
|
input_layout = QHBoxLayout()
|
|
self.location_input = QLineEdit()
|
|
self.location_input.setPlaceholderText("Enter location name")
|
|
input_layout.addWidget(self.location_input)
|
|
|
|
self.search_button = QPushButton("Search")
|
|
self.search_button.clicked.connect(self.get_weather_data)
|
|
input_layout.addWidget(self.search_button)
|
|
|
|
layout.addLayout(input_layout)
|
|
|
|
# Options
|
|
options_layout = QHBoxLayout()
|
|
self.save_checkbox = QCheckBox("Save data to JSON file")
|
|
options_layout.addWidget(self.save_checkbox)
|
|
|
|
self.radar_checkbox = QCheckBox("Display radar images")
|
|
options_layout.addWidget(self.radar_checkbox)
|
|
|
|
layout.addLayout(options_layout)
|
|
|
|
# Forecast display
|
|
self.forecast_display = QTextEdit()
|
|
self.forecast_display.setReadOnly(True)
|
|
layout.addWidget(self.forecast_display)
|
|
|
|
# Radar image display
|
|
self.radar_image = QLabel()
|
|
self.radar_image.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
layout.addWidget(self.radar_image)
|
|
|
|
|
|
def get_weather_data(self):
|
|
location = self.location_input.text()
|
|
logger.info(f"Getting weather data for {location} at {current_time}")
|
|
|
|
coords = get_coordinates_from_location(location)
|
|
if coords:
|
|
lat, lon = coords
|
|
gridpoint_url = get_gridpoint_forecast(lat, lon)
|
|
if gridpoint_url:
|
|
forecast_data = get_forecast(gridpoint_url)
|
|
if forecast_data:
|
|
formatted_data = format_forecast(forecast_data)
|
|
self.display_forecast(formatted_data)
|
|
logger.info(f"Formatted forecast data retrieved and displayed for {location}")
|
|
|
|
if self.save_checkbox.isChecked():
|
|
save_data(formatted_data)
|
|
logger.info(f"Formatted forecast data saved to JSON file for {location} at {current_time}")
|
|
|
|
nearest_station = find_nearest_nexrad_station(lat, lon)
|
|
if nearest_station and self.radar_checkbox.isChecked():
|
|
self.display_radar(nearest_station)
|
|
logger.info(f"Radar image displayed for {location}")
|
|
|
|
if self.save_checkbox.isChecked():
|
|
save_radar_image(nearest_station)
|
|
else:
|
|
self.forecast_display.setText("Failed to retrieve forecast data")
|
|
logger.error("Failed to retrieve forecast data")
|
|
else:
|
|
self.forecast_display.setText("Failed to retrieve gridpoint forecast URL")
|
|
logger.error("Failed to retrieve gridpoint forecast URL")
|
|
else:
|
|
self.forecast_display.setText(f"Failed to retrieve coordinates for {location}")
|
|
logger.error(f"Failed to retrieve coordinates for {location}")
|
|
|
|
|
|
def display_forecast(self, formatted_data):
|
|
self.forecast_display.setText(display_formatted_forecast(formatted_data))
|
|
|
|
|
|
def display_radar(self, station):
|
|
radar_image_path = show_radar_image(station)
|
|
if radar_image_path:
|
|
pixmap = QPixmap(radar_image_path)
|
|
self.radar_image.setPixmap(pixmap.scaled(400, 400, Qt.AspectRatioMode.KeepAspectRatio))
|
|
else:
|
|
self.radar_image.setText("Failed to load radar image")
|
|
|
|
|
|
def main():
|
|
app = QApplication(sys.argv)
|
|
window = WeatherApp()
|
|
window.show()
|
|
sys.exit(app.exec())
|
|
|
|
if __name__ == "__main__":
|
|
main()
|