nakas commited on
Commit
c0e3871
·
verified ·
1 Parent(s): 2b3b323

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -51
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import requests
3
  import folium
4
- from datetime import datetime
5
  import tempfile
6
  import os
7
 
@@ -11,12 +11,12 @@ def get_noaa_forecast(lat, lon):
11
 
12
  try:
13
  # Get the forecast URL for the coordinates
14
- response = requests.get(points_url)
15
  response.raise_for_status()
16
  forecast_url = response.json()['properties']['forecast']
17
 
18
  # Get the actual forecast
19
- forecast_response = requests.get(forecast_url)
20
  forecast_response.raise_for_status()
21
  forecast_data = forecast_response.json()
22
 
@@ -29,83 +29,149 @@ def get_noaa_forecast(lat, lon):
29
  forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
30
  forecast_text += f"{period['detailedForecast']}\n"
31
 
32
- return forecast_text
33
  except requests.exceptions.RequestException as e:
34
  return f"Error fetching forecast: {str(e)}"
35
 
36
- def get_radar_url(lat, lon):
37
- """Get NOAA radar image URL for given coordinates."""
38
- # Calculate the closest radar station (this is a simplified version)
39
- # In a production environment, you'd want to use the actual NOAA radar station directory
40
- base_url = "https://radar.weather.gov/ridge/standard/"
41
- # This returns the latest radar image for the region
42
- # You would need to determine the correct station based on coordinates
43
- return f"{base_url}CONUS_LATEST_loop.gif"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  def create_map():
46
  """Create a folium map centered on the US."""
47
  m = folium.Map(location=[39.8283, -98.5795], zoom_start=4)
 
48
  return m._repr_html_()
49
 
50
- def update_weather(lat, lon, map_click_coords):
51
  """Update weather information based on coordinates."""
52
- # Use map click coordinates if provided, otherwise use manual input
53
- if map_click_coords is not None:
54
- lat, lon = map_click_coords[0], map_click_coords[1]
55
-
56
- # Get forecast and radar
57
- forecast = get_noaa_forecast(lat, lon)
58
- radar_url = get_radar_url(lat, lon)
59
-
60
- # Create updated map with marker
61
- m = folium.Map(location=[lat, lon], zoom_start=8)
62
- folium.Marker([lat, lon], popup=f"Lat: {lat}, Lon: {lon}").add_to(m)
63
-
64
- return forecast, radar_url, m._repr_html_()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # Create Gradio interface
67
- with gr.Blocks() as demo:
68
  gr.Markdown("# NOAA Weather Forecast and Radar")
69
 
70
  with gr.Row():
71
- with gr.Column():
72
- lat_input = gr.Number(label="Latitude", value=39.8283)
73
- lon_input = gr.Number(label="Longitude", value=-98.5795)
 
 
 
 
 
 
 
 
 
 
74
  submit_btn = gr.Button("Get Weather")
75
 
76
- with gr.Column():
77
  map_display = gr.HTML(create_map())
78
- map_click_coords = gr.State(None)
79
 
80
  with gr.Row():
81
- forecast_output = gr.Textbox(label="Forecast", lines=10)
82
- radar_output = gr.Image(label="Radar")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # Handle submit button click
85
  submit_btn.click(
86
  fn=update_weather,
87
- inputs=[lat_input, lon_input, map_click_coords],
88
- outputs=[forecast_output, radar_output, map_display]
 
 
 
 
 
 
89
  )
90
 
91
- # Handle map clicks
92
- # Note: This would require additional JavaScript to capture click events
93
- # In a full implementation, you'd need to add custom JS to capture map clicks
94
-
95
  gr.Markdown("""
96
  ## Instructions
97
- 1. Enter coordinates manually or click on the map
98
  2. Click "Get Weather" to see the forecast and radar
99
- 3. Radar image shows the latest available data
 
 
 
 
 
100
 
101
- Note: This is a basic implementation. A production version would need:
102
- - Proper radar station selection based on location
103
- - Error handling for invalid coordinates
104
- - Rate limiting for API requests
105
- - Better map interaction
106
  """)
107
 
108
- # Launch the app
109
- if __name__ == "__main__":
110
- demo.launch()
111
-
 
1
  import gradio as gr
2
  import requests
3
  import folium
4
+ from datetime import datetime, timedelta
5
  import tempfile
6
  import os
7
 
 
11
 
12
  try:
13
  # Get the forecast URL for the coordinates
14
+ response = requests.get(points_url, timeout=10)
15
  response.raise_for_status()
16
  forecast_url = response.json()['properties']['forecast']
17
 
18
  # Get the actual forecast
19
+ forecast_response = requests.get(forecast_url, timeout=10)
20
  forecast_response.raise_for_status()
21
  forecast_data = forecast_response.json()
22
 
 
29
  forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
30
  forecast_text += f"{period['detailedForecast']}\n"
31
 
32
+ return forecast_text.strip()
33
  except requests.exceptions.RequestException as e:
34
  return f"Error fetching forecast: {str(e)}"
35
 
36
+ def get_radar_images():
37
+ """Get current radar and forecast radar images."""
38
+ try:
39
+ # Current radar
40
+ current_radar = "https://radar.weather.gov/ridge/standard/CONUS_0.gif"
41
+
42
+ # Forecast radar - using NDFD precipitation forecast images
43
+ # These are probability of precipitation forecasts for different time periods
44
+ now = datetime.utcnow()
45
+ forecast_date = now.strftime("%Y%m%d")
46
+
47
+ # 6-hour and 12-hour precipitation probability forecasts
48
+ forecast_6hr = f"https://digital.weather.gov/images/conus/MaxPcpn6_{forecast_date}00.png"
49
+ forecast_12hr = f"https://digital.weather.gov/images/conus/MaxPcpn12_{forecast_date}00.png"
50
+
51
+ return {
52
+ "current": current_radar,
53
+ "forecast_6hr": forecast_6hr,
54
+ "forecast_12hr": forecast_12hr
55
+ }
56
+ except Exception as e:
57
+ return None
58
 
59
  def create_map():
60
  """Create a folium map centered on the US."""
61
  m = folium.Map(location=[39.8283, -98.5795], zoom_start=4)
62
+ m.add_child(folium.ClickForLatLng()) # Enable click events
63
  return m._repr_html_()
64
 
65
+ def update_weather(lat, lon):
66
  """Update weather information based on coordinates."""
67
+ try:
68
+ # Validate coordinates
69
+ lat = float(lat)
70
+ lon = float(lon)
71
+ if not (-90 <= lat <= 90 and -180 <= lon <= 180):
72
+ return "Invalid coordinates. Latitude must be between -90 and 90, longitude between -180 and 180.", None, None, None, create_map()
73
+
74
+ # Get forecast and radar
75
+ forecast = get_noaa_forecast(lat, lon)
76
+ radar_images = get_radar_images()
77
+
78
+ if radar_images is None:
79
+ return forecast, None, None, None, create_map()
80
+
81
+ # Create updated map with marker
82
+ m = folium.Map(location=[lat, lon], zoom_start=8)
83
+ folium.Marker([lat, lon], popup=f"Lat: {lat:.4f}, Lon: {lon:.4f}").add_to(m)
84
+ m.add_child(folium.ClickForLatLng()) # Enable click events
85
+
86
+ return (
87
+ forecast,
88
+ radar_images["current"],
89
+ radar_images["forecast_6hr"],
90
+ radar_images["forecast_12hr"],
91
+ m._repr_html_()
92
+ )
93
+ except ValueError as e:
94
+ return f"Error: Invalid coordinate format - {str(e)}", None, None, None, create_map()
95
+ except Exception as e:
96
+ return f"Error: {str(e)}", None, None, None, create_map()
97
 
98
  # Create Gradio interface
99
+ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
100
  gr.Markdown("# NOAA Weather Forecast and Radar")
101
 
102
  with gr.Row():
103
+ with gr.Column(scale=1):
104
+ lat_input = gr.Number(
105
+ label="Latitude",
106
+ value=39.8283,
107
+ minimum=-90,
108
+ maximum=90
109
+ )
110
+ lon_input = gr.Number(
111
+ label="Longitude",
112
+ value=-98.5795,
113
+ minimum=-180,
114
+ maximum=180
115
+ )
116
  submit_btn = gr.Button("Get Weather")
117
 
118
+ with gr.Column(scale=2):
119
  map_display = gr.HTML(create_map())
 
120
 
121
  with gr.Row():
122
+ forecast_output = gr.Textbox(
123
+ label="Forecast",
124
+ lines=12,
125
+ placeholder="Enter coordinates or click 'Get Weather' to see the forecast..."
126
+ )
127
+
128
+ with gr.Row():
129
+ with gr.Column():
130
+ current_radar = gr.Image(
131
+ label="Current Radar",
132
+ show_label=True,
133
+ container=True
134
+ )
135
+ with gr.Column():
136
+ forecast_6hr = gr.Image(
137
+ label="6-Hour Precipitation Forecast",
138
+ show_label=True,
139
+ container=True
140
+ )
141
+ with gr.Column():
142
+ forecast_12hr = gr.Image(
143
+ label="12-Hour Precipitation Forecast",
144
+ show_label=True,
145
+ container=True
146
+ )
147
 
148
  # Handle submit button click
149
  submit_btn.click(
150
  fn=update_weather,
151
+ inputs=[lat_input, lon_input],
152
+ outputs=[
153
+ forecast_output,
154
+ current_radar,
155
+ forecast_6hr,
156
+ forecast_12hr,
157
+ map_display
158
+ ]
159
  )
160
 
 
 
 
 
161
  gr.Markdown("""
162
  ## Instructions
163
+ 1. Enter coordinates manually or click on the map to select a location
164
  2. Click "Get Weather" to see the forecast and radar
165
+ 3. View current radar and precipitation forecasts
166
+
167
+ **Legend:**
168
+ - Current Radar: Shows current precipitation
169
+ - 6-Hour Forecast: Shows probability of precipitation over next 6 hours
170
+ - 12-Hour Forecast: Shows probability of precipitation over next 12 hours
171
 
172
+ **Note**: This app uses the NOAA Weather API and may have occasional delays or service interruptions.
173
+ Data is provided by the National Weather Service (weather.gov).
 
 
 
174
  """)
175
 
176
+ # For Hugging Face Spaces, we need to export the demo
177
+ demo.queue(concurrency_count=3).launch()