nakas commited on
Commit
624b475
·
verified ·
1 Parent(s): 4c3b89c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -41,37 +41,42 @@ def get_snow_forecast(lat, lon):
41
 
42
  # Process forecast periods to extract snow data
43
  snow_data = []
 
44
  for period in forecast_data['properties']['periods']:
 
 
 
 
 
 
45
  # Check if snow is mentioned in the forecast
46
  forecast_text = period['detailedForecast'].lower()
47
- if 'snow' in forecast_text:
48
- # Try to extract snow amount from the forecast text
49
- try:
50
- # Look for patterns like "2 inches of snow" or "snow accumulation of 1 to 3 inches"
51
- import re
52
- snow_patterns = [
53
- r'(\d+(?:\.\d+)?)\s*(?:to\s*\d+(?:\.\d+)?)?\s*inches? of snow',
54
- r'snow accumulation of (\d+(?:\.\d+)?)\s*(?:to\s*\d+(?:\.\d+)?)?\s*inches',
55
- ]
56
-
57
- snow_amount = None
58
- for pattern in snow_patterns:
59
- match = re.search(pattern, forecast_text)
60
- if match:
61
  snow_amount = float(match.group(1))
 
62
  break
63
-
64
- if snow_amount is not None:
65
- snow_data.append({
66
- 'validTime': period['startTime'],
67
- 'value': snow_amount / 39.37, # Convert inches to meters
68
- 'temperature': period['temperature']
69
- })
70
- except Exception as e:
71
- print(f"Error parsing snow amount: {str(e)}")
72
- continue
73
 
74
- return {'snowfall': snow_data}
 
 
 
75
  except Exception as e:
76
  print(f"Error fetching snow forecast: {str(e)}")
77
  return None
@@ -127,10 +132,7 @@ def get_noaa_forecast(lat, lon):
127
  # Add snow-specific forecasts if available
128
  if snow_data and 'snowfall' in snow_data:
129
  matching_snow = [s for s in snow_data['snowfall']
130
- if datetime.fromisoformat(s['validTime'].split('/')[0]) <=
131
- datetime.fromisoformat(period['startTime']) <=
132
- datetime.fromisoformat(s['validTime'].split('/')[0]) +
133
- timedelta(hours=int(s['validTime'].split('/')[-1][1:-1]))]
134
 
135
  if matching_snow:
136
  snow_amount = matching_snow[0]['value'] # in meters
 
41
 
42
  # Process forecast periods to extract snow data
43
  snow_data = []
44
+
45
  for period in forecast_data['properties']['periods']:
46
+ period_data = {
47
+ 'time': period['startTime'],
48
+ 'temperature': period['temperature'],
49
+ 'snowfall': 0 # Default to no snow
50
+ }
51
+
52
  # Check if snow is mentioned in the forecast
53
  forecast_text = period['detailedForecast'].lower()
54
+
55
+ if any(word in forecast_text for word in ['snow', 'flurries', 'wintry mix']):
56
+ import re
57
+ # Look for snow amounts in the text
58
+ amount_patterns = [
59
+ r'(\d+(?:\.\d+)?)\s*(?:to\s*\d+(?:\.\d+)?)?\s*inches? of snow',
60
+ r'snow accumulation of (\d+(?:\.\d+)?)\s*(?:to\s*\d+(?:\.\d+)?)?\s*inches',
61
+ r'accumulation of (\d+(?:\.\d+)?)\s*(?:to\s*\d+(?:\.\d+)?)?\s*inches',
62
+ ]
63
+
64
+ for pattern in amount_patterns:
65
+ match = re.search(pattern, forecast_text)
66
+ if match:
67
+ try:
68
  snow_amount = float(match.group(1))
69
+ period_data['snowfall'] = snow_amount / 39.37 # Convert inches to meters
70
  break
71
+ except (ValueError, IndexError):
72
+ continue
73
+
74
+ snow_data.append(period_data)
 
 
 
 
 
 
75
 
76
+ return snow_data
77
+ except Exception as e:
78
+ print(f"Error in snow forecast: {str(e)}")
79
+ return []
80
  except Exception as e:
81
  print(f"Error fetching snow forecast: {str(e)}")
82
  return None
 
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