Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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'
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 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
|
| 173 |
-
"""Get
|
| 174 |
try:
|
| 175 |
-
#
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 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 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
-
print(f"Error
|
| 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():
|