shaheerawan3 commited on
Commit
f0aab4e
·
verified ·
1 Parent(s): 7faaaac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -1
app.py CHANGED
@@ -36,7 +36,90 @@ class DataCollector:
36
  'Hyderabad': {'lat': 25.3960, 'lon': 68.3578}
37
  }
38
 
39
- # [Previous methods remain the same...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def create_ml_features(self, weather_data):
42
  """Create features for ML predictions"""
 
36
  'Hyderabad': {'lat': 25.3960, 'lon': 68.3578}
37
  }
38
 
39
+ def fetch_weather_data(self):
40
+ """Fetch weather data from OpenMeteo"""
41
+ weather_data = []
42
+ for city, coords in self.cities.items():
43
+ try:
44
+ url = f"https://api.open-meteo.com/v1/forecast?latitude={coords['lat']}&longitude={coords['lon']}&hourly=temperature_2m,relativehumidity_2m,precipitation,windspeed_10m&daily=temperature_2m_max,temperature_2m_min,precipitation_sum&timezone=auto&past_days=7"
45
+ response = requests.get(url)
46
+ data = response.json()
47
+
48
+ # Hourly data
49
+ hourly_df = pd.DataFrame({
50
+ 'datetime': pd.to_datetime(data['hourly']['time']),
51
+ 'temperature': data['hourly']['temperature_2m'],
52
+ 'humidity': data['hourly']['relativehumidity_2m'],
53
+ 'precipitation': data['hourly']['precipitation'],
54
+ 'wind_speed': data['hourly']['windspeed_10m']
55
+ })
56
+
57
+ # Daily data
58
+ daily_df = pd.DataFrame({
59
+ 'date': pd.to_datetime(data['daily']['time']),
60
+ 'temp_max': data['daily']['temperature_2m_max'],
61
+ 'temp_min': data['daily']['temperature_2m_min'],
62
+ 'precipitation_sum': data['daily']['precipitation_sum']
63
+ })
64
+
65
+ weather_data.append({
66
+ 'city': city,
67
+ 'hourly': hourly_df,
68
+ 'daily': daily_df,
69
+ 'coords': coords
70
+ })
71
+ except Exception as e:
72
+ st.error(f"Error fetching weather data for {city}: {e}")
73
+ continue
74
+
75
+ return weather_data if weather_data else None
76
+
77
+ def fetch_usgs_earthquake_data(self):
78
+ """Fetch earthquake data from USGS website"""
79
+ try:
80
+ # USGS API endpoint for past month's earthquakes
81
+ url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson"
82
+ response = requests.get(url)
83
+ data = response.json()
84
+
85
+ # Filter for Pakistan region
86
+ pakistan_data = {
87
+ "type": "FeatureCollection",
88
+ "features": [
89
+ feature for feature in data["features"]
90
+ if 60.878 <= feature["geometry"]["coordinates"][0] <= 77.840
91
+ and 23.692 <= feature["geometry"]["coordinates"][1] <= 37.097
92
+ ]
93
+ }
94
+ return pakistan_data
95
+ except Exception as e:
96
+ st.error(f"Error fetching earthquake data: {e}")
97
+ return None
98
+
99
+ def fetch_air_quality_data(self):
100
+ """Fetch air quality data from OpenMeteo"""
101
+ aqi_data = []
102
+ for city, coords in self.cities.items():
103
+ try:
104
+ url = f"https://air-quality-api.open-meteo.com/v1/air-quality?latitude={coords['lat']}&longitude={coords['lon']}&hourly=pm10,pm2_5,carbon_monoxide,nitrogen_dioxide,ozone&timezone=auto&past_days=7"
105
+ response = requests.get(url)
106
+ data = response.json()
107
+
108
+ df = pd.DataFrame({
109
+ 'datetime': pd.to_datetime(data['hourly']['time']),
110
+ 'PM10': data['hourly']['pm10'],
111
+ 'PM2.5': data['hourly']['pm2_5'],
112
+ 'CO': data['hourly']['carbon_monoxide'],
113
+ 'NO2': data['hourly']['nitrogen_dioxide'],
114
+ 'O3': data['hourly']['ozone'],
115
+ 'city': city
116
+ })
117
+ aqi_data.append(df)
118
+ except Exception as e:
119
+ st.error(f"Error fetching AQI data for {city}: {e}")
120
+ continue
121
+
122
+ return pd.concat(aqi_data, ignore_index=True) if aqi_data else None
123
 
124
  def create_ml_features(self, weather_data):
125
  """Create features for ML predictions"""