nakas commited on
Commit
212cba8
·
verified ·
1 Parent(s): d0a7dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -12
app.py CHANGED
@@ -4,6 +4,19 @@ import folium
4
  from datetime import datetime, timedelta
5
  import tempfile
6
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def get_noaa_forecast(lat, lon):
9
  """Get NOAA forecast data for given coordinates."""
@@ -36,17 +49,20 @@ def get_noaa_forecast(lat, lon):
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,
@@ -54,6 +70,7 @@ def get_radar_images():
54
  "forecast_12hr": forecast_12hr
55
  }
56
  except Exception as e:
 
57
  return None
58
 
59
  def create_map():
@@ -130,19 +147,22 @@ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
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
@@ -166,8 +186,8 @@ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
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).
 
4
  from datetime import datetime, timedelta
5
  import tempfile
6
  import os
7
+ from PIL import Image
8
+ import io
9
+
10
+ def download_image(url):
11
+ """Download image from URL and return as PIL Image."""
12
+ try:
13
+ response = requests.get(url, timeout=10)
14
+ response.raise_for_status()
15
+ image = Image.open(io.BytesIO(response.content))
16
+ return image
17
+ except Exception as e:
18
+ print(f"Error downloading image from {url}: {str(e)}")
19
+ return None
20
 
21
  def get_noaa_forecast(lat, lon):
22
  """Get NOAA forecast data for given coordinates."""
 
49
  def get_radar_images():
50
  """Get current radar and forecast radar images."""
51
  try:
52
+ # Current radar - National composite
53
+ current_radar_url = "https://radar.weather.gov/ridge/standard/CONUS_0.gif"
54
+ current_radar = download_image(current_radar_url)
55
 
56
+ # Get forecast time for NDFD products
 
57
  now = datetime.utcnow()
58
  forecast_date = now.strftime("%Y%m%d")
59
 
60
+ # Alternative URLs that might be more reliable
61
+ forecast_6hr_url = "https://graphical.weather.gov/images/conus/QPF06_conus.png"
62
+ forecast_12hr_url = "https://graphical.weather.gov/images/conus/QPF12_conus.png"
63
+
64
+ forecast_6hr = download_image(forecast_6hr_url)
65
+ forecast_12hr = download_image(forecast_12hr_url)
66
 
67
  return {
68
  "current": current_radar,
 
70
  "forecast_12hr": forecast_12hr
71
  }
72
  except Exception as e:
73
+ print(f"Error getting radar images: {str(e)}")
74
  return None
75
 
76
  def create_map():
 
147
  current_radar = gr.Image(
148
  label="Current Radar",
149
  show_label=True,
150
+ container=True,
151
+ type="pil"
152
  )
153
  with gr.Column():
154
  forecast_6hr = gr.Image(
155
  label="6-Hour Precipitation Forecast",
156
  show_label=True,
157
+ container=True,
158
+ type="pil"
159
  )
160
  with gr.Column():
161
  forecast_12hr = gr.Image(
162
  label="12-Hour Precipitation Forecast",
163
  show_label=True,
164
+ container=True,
165
+ type="pil"
166
  )
167
 
168
  # Handle submit button click
 
186
 
187
  **Legend:**
188
  - Current Radar: Shows current precipitation
189
+ - 6-Hour Forecast: Shows quantitative precipitation forecast for next 6 hours
190
+ - 12-Hour Forecast: Shows quantitative precipitation forecast for next 12 hours
191
 
192
  **Note**: This app uses the NOAA Weather API and may have occasional delays or service interruptions.
193
  Data is provided by the National Weather Service (weather.gov).