SWeatherpy/main.py
2024-10-21 14:39:42 -05:00

76 lines
2.8 KiB
Python
Executable File

from weathersettings import logger, current_time, argparse
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
from UI import WeatherApp as UI
current_time = current_time
def parse_arguments():
parser = argparse.ArgumentParser(description="SWeatherpy: Weather data collection and display")
parser.add_argument("-l", "--location", help="Location name")
parser.add_argument("-s", "--save", action="store_true", help="Save forecast data")
parser.add_argument("-r", "--radar", action="store_true", help="Display radar images")
parser.add_argument("-g", "--gui", action="store_true", help="Run GUI")
return parser.parse_args()
def gui_main():
gui = UI()
gui.run()
# Main logic
def main():
args = parse_arguments()
if args.gui:
gui_main()
return
if args.location:
location = args.location
else:
location = input("Enter location (City, State): ")
run_gui = input("Run GUI? (y/n) ")
location = input("Enter location (City, State): ")
logger.info(f"Getting weather data for {location} at {current_time}")
save = input("Save data to JSON file? (y/n) ")
radarq = input("Display radar images? (y/n) ")
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)
display_formatted_forecast(formatted_data)
logger.info(f"Formatted forecast data retrieved and displayed for {location}")
nearest_station = find_nearest_nexrad_station(lat, lon)
if nearest_station:
show_radar_image(nearest_station)
else:
logger.error("Failed to retrieve forecast data")
else:
logger.error("Failed to retrieve gridpoint forecast URL")
else:
logger.error(f"Failed to retrieve coordinates for {location}")
if save.lower() in ['y', 'Y', 'yes', 'Yes']:
save_data(formatted_data)
save_radar_image(nearest_station)
logger.info(f"Formatted forecast data saved to JSON file for {location} at {current_time}")
if radarq.lower() in ['y', 'Y', 'yes', 'Yes']:
show_radar_image(nearest_station)
logger.info(f"Radar image displayed for {location}")
if run_gui.lower() in ['y', 'Y', 'yes', 'Yes']:
gui_main()
if __name__ == "__main__":
main()