import gradio as gr import pandas as pd import numpy as np from datetime import datetime def parse_wind(wind_str): if pd.isna(wind_str): return np.nan, np.nan try: if 'G' in str(wind_str): speed, gust = str(wind_str).split('G') return float(speed), float(gust) return float(wind_str), np.nan except: return np.nan, np.nan def process_weather_data(data_str): # Split the data into lines and remove empty lines lines = [line.strip() for line in data_str.split('\n') if line.strip()] # Find the start of actual data (after headers) start_idx = 0 for i, line in enumerate(lines): if 'Date/Time' in line: start_idx = i + 1 break # Process the data lines data = [] for line in lines[start_idx:]: parts = line.split('\t') if len(parts) >= 8: try: date_str = parts[0].strip() temp = float(parts[1]) if parts[1].strip() else np.nan dew_point = float(parts[2]) if parts[2].strip() else np.nan humidity = float(parts[3]) if parts[3].strip() else np.nan wind_chill = float(parts[4]) if parts[4].strip() else np.nan wind_speed, wind_gust = parse_wind(parts[6]) snow_depth = float(parts[7]) if parts[7].strip() else np.nan data.append({ 'datetime': pd.to_datetime(date_str), 'temperature': temp, 'dew_point': dew_point, 'humidity': humidity, 'wind_chill': wind_chill, 'wind_speed': wind_speed, 'wind_gust': wind_gust, 'snow_depth': snow_depth }) except: continue df = pd.DataFrame(data) # Calculate statistics stats = { 'Temperature Range': f"{df['temperature'].min():.1f}°F to {df['temperature'].max():.1f}°F", 'Average Temperature': f"{df['temperature'].mean():.1f}°F", 'Max Wind Speed': f"{df['wind_speed'].max():.1f} mph", 'Max Wind Gust': f"{df['wind_gust'].max():.1f} mph", 'Average Humidity': f"{df['humidity'].mean():.1f}%", 'Max Snow Depth': f"{df['snow_depth'].max():.1f} inches" } # Create plots temp_fig = gr.Plot() df.plot(x='datetime', y=['temperature', 'wind_chill'], title='Temperature and Wind Chill Over Time', figsize=(12, 6)) temp_fig.pyplot() wind_fig = gr.Plot() df.plot(x='datetime', y=['wind_speed', 'wind_gust'], title='Wind Speed and Gusts Over Time', figsize=(12, 6)) wind_fig.pyplot() stats_html = "
" for key, value in stats.items(): stats_html += f"

{key}: {value}

" stats_html += "
" return stats_html, temp_fig, wind_fig # Create the Gradio interface with gr.Blocks(title="Weather Data Analysis") as demo: gr.Markdown("# Weather Data Analysis") gr.Markdown("Paste weather data from weather.gov/wrh/timeseries in the format shown below:") with gr.Row(): input_text = gr.Textbox( label="Weather Data", placeholder="Paste weather data here...", lines=10 ) analyze_btn = gr.Button("Analyze Weather Data") with gr.Row(): stats_output = gr.HTML(label="Statistics") with gr.Row(): temp_plot = gr.Plot(label="Temperature Plot") wind_plot = gr.Plot(label="Wind Plot") analyze_btn.click( fn=process_weather_data, inputs=[input_text], outputs=[stats_output, temp_plot, wind_plot] ) if __name__ == "__main__": demo.launch()