nakas commited on
Commit
5e02ff8
·
verified ·
1 Parent(s): 624b475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -38
app.py CHANGED
@@ -121,7 +121,7 @@ def get_noaa_forecast(lat, lon):
121
  periods = forecast_data['properties']['periods']
122
  forecast_text = ""
123
 
124
- for period in periods[:6]: # Show next 3 days (day and night)
125
  forecast_text += f"\n{period['name']}:\n"
126
  forecast_text += f"Temperature: {period['temperature']}°{period['temperatureUnit']}\n"
127
  forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
@@ -130,23 +130,19 @@ def get_noaa_forecast(lat, lon):
130
  temp_c = (period['temperature'] - 32) * 5/9 if period['temperatureUnit'] == 'F' else period['temperature']
131
 
132
  # Add snow-specific forecasts if available
133
- if snow_data and 'snowfall' in snow_data:
134
- matching_snow = [s for s in snow_data['snowfall']
135
- if s['validTime'] == period['startTime']]
 
136
 
137
- if matching_snow:
138
- snow_amount = matching_snow[0]['value'] # in meters
139
- snow_density = calculate_snow_density(temp_c)
140
- swe = snow_amount * (snow_density / 1000) # Convert density to g/cm³
141
-
142
- # Convert to inches for display
143
- snow_inches = snow_amount * 39.37
144
- swe_inches = swe * 39.37
145
-
146
- forecast_text += f"Predicted Snowfall: {snow_inches:.1f} inches\n"
147
- forecast_text += f"Snow Water Equivalent: {swe_inches:.2f} inches\n"
148
- forecast_text += f"Estimated Snow Density: {snow_density} kg/m³ "
149
- forecast_text += f"({get_snow_quality_description(snow_density)})\n"
150
 
151
  forecast_text += f"{period['detailedForecast']}\n"
152
 
@@ -169,31 +165,38 @@ def get_snow_quality_description(density):
169
  else:
170
  return "Dense/settled snow"
171
 
172
- def get_radar_images():
173
- """Get current radar and forecast radar images."""
174
  try:
175
- # Current radar - National composite
176
- current_radar_url = "https://radar.weather.gov/ridge/standard/CONUS_0.gif"
177
- current_radar = download_image(current_radar_url)
178
-
179
- # Get forecast time for NDFD products
180
- now = datetime.utcnow()
181
- forecast_date = now.strftime("%Y%m%d")
182
-
183
- # Alternative URLs that might be more reliable
184
- forecast_6hr_url = "https://graphical.weather.gov/images/conus/QPF06_conus.png"
185
- forecast_12hr_url = "https://graphical.weather.gov/images/conus/QPF12_conus.png"
186
 
187
- forecast_6hr = download_image(forecast_6hr_url)
188
- forecast_12hr = download_image(forecast_12hr_url)
 
 
 
189
 
190
- return {
191
- "current": current_radar,
192
- "forecast_6hr": forecast_6hr,
193
- "forecast_12hr": forecast_12hr
194
- }
 
 
 
 
 
 
 
 
195
  except Exception as e:
196
- print(f"Error getting radar images: {str(e)}")
197
  return None
198
 
199
  def create_map():
 
121
  periods = forecast_data['properties']['periods']
122
  forecast_text = ""
123
 
124
+ for i, period in enumerate(periods[:6]): # Show next 3 days (day and night)
125
  forecast_text += f"\n{period['name']}:\n"
126
  forecast_text += f"Temperature: {period['temperature']}°{period['temperatureUnit']}\n"
127
  forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
 
130
  temp_c = (period['temperature'] - 32) * 5/9 if period['temperatureUnit'] == 'F' else period['temperature']
131
 
132
  # Add snow-specific forecasts if available
133
+ if i < len(snow_data) and snow_data[i]['snowfall'] > 0:
134
+ snow_density = calculate_snow_density(temp_c)
135
+ snow_amount = snow_data[i]['snowfall'] # in meters
136
+ swe = snow_amount * (snow_density / 1000) # Convert density to g/cm³
137
 
138
+ # Convert to inches for display
139
+ snow_inches = snow_amount * 39.37
140
+ swe_inches = swe * 39.37
141
+
142
+ forecast_text += f"Predicted Snowfall: {snow_inches:.1f} inches\n"
143
+ forecast_text += f"Snow Water Equivalent: {swe_inches:.2f} inches\n"
144
+ forecast_text += f"Estimated Snow Density: {snow_density} kg/m³ "
145
+ forecast_text += f"({get_snow_quality_description(snow_density)})\n"
 
 
 
 
 
146
 
147
  forecast_text += f"{period['detailedForecast']}\n"
148
 
 
165
  else:
166
  return "Dense/settled snow"
167
 
168
+ def get_animated_snow_forecast():
169
+ """Get animated snow forecast for the next 12 hours."""
170
  try:
171
+ # Get snow forecast images for different time periods
172
+ base_urls = [
173
+ "https://graphical.weather.gov/images/conus/SnowAmt1_conus.gif",
174
+ "https://graphical.weather.gov/images/conus/SnowAmt2_conus.gif",
175
+ "https://graphical.weather.gov/images/conus/SnowAmt3_conus.gif",
176
+ "https://graphical.weather.gov/images/conus/SnowAmt4_conus.gif"
177
+ ]
 
 
 
 
178
 
179
+ frames = []
180
+ for url in base_urls:
181
+ img = download_image(url)
182
+ if img is not None:
183
+ frames.append(img)
184
 
185
+ if not frames:
186
+ return None
187
+
188
+ # Create animated GIF
189
+ with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as tmp_file:
190
+ frames[0].save(
191
+ tmp_file.name,
192
+ save_all=True,
193
+ append_images=frames[1:],
194
+ duration=1000, # 1 second per frame
195
+ loop=0
196
+ )
197
+ return tmp_file.name
198
  except Exception as e:
199
+ print(f"Error creating animated forecast: {str(e)}")
200
  return None
201
 
202
  def create_map():