lyimo commited on
Commit
e31ae34
·
verified ·
1 Parent(s): c5616e9

Create part1_data.py

Browse files
Files changed (1) hide show
  1. part1_data.py +175 -0
part1_data.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # part1_data.py
2
+
3
+ import os
4
+ import numpy as np
5
+ import pandas as pd
6
+ from datetime import datetime, timedelta
7
+ import requests
8
+ from geopy.geocoders import Nominatim
9
+ from geopy.exc import GeocoderTimedOut
10
+ from scipy import stats
11
+
12
+ # Get API key from environment variable
13
+ OPENWEATHER_API_KEY = os.getenv('OPENWEATHER_API_KEY', 'default_key')
14
+
15
+ class TobaccoAnalyzer:
16
+ def __init__(self):
17
+ self.api_key = OPENWEATHER_API_KEY
18
+ self.optimal_conditions = {
19
+ 'temperature': {'min': 20, 'max': 30},
20
+ 'humidity': {'min': 60, 'max': 80},
21
+ 'rainfall': {'min': 500/365, 'max': 1200/365}
22
+ }
23
+ self.geolocator = Nominatim(user_agent="tobacco_analyzer")
24
+ self.seasons = {
25
+ 1: 'Winter', 2: 'Winter', 3: 'Spring',
26
+ 4: 'Spring', 5: 'Spring', 6: 'Summer',
27
+ 7: 'Summer', 8: 'Summer', 9: 'Fall',
28
+ 10: 'Fall', 11: 'Fall', 12: 'Winter'
29
+ }
30
+
31
+ def geocode_location(self, location_name):
32
+ """Convert location name to coordinates"""
33
+ try:
34
+ location = self.geolocator.geocode(location_name)
35
+ if location:
36
+ return {
37
+ 'lat': location.latitude,
38
+ 'lon': location.longitude,
39
+ 'address': location.address
40
+ }
41
+ return None
42
+ except GeocoderTimedOut:
43
+ return None
44
+
45
+ def get_weather_data(self, lat, lon, historical_days=90, forecast_days=90):
46
+ """Get historical and forecast weather data"""
47
+ historical_data = []
48
+
49
+ # Get historical data
50
+ for day in range(historical_days):
51
+ date = datetime.now() - timedelta(days=day)
52
+ url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={self.api_key}&units=metric&dt={int(date.timestamp())}"
53
+ try:
54
+ response = requests.get(url)
55
+ if response.status_code == 200:
56
+ data = response.json()
57
+ weather_data = {
58
+ 'date': date,
59
+ 'temperature': data['main']['temp'],
60
+ 'humidity': data['main']['humidity'],
61
+ 'rainfall': data.get('rain', {}).get('1h', 0) * 24,
62
+ 'type': 'historical',
63
+ 'description': data['weather'][0]['description']
64
+ }
65
+ historical_data.append(weather_data)
66
+ except Exception as e:
67
+ print(f"Error fetching historical data: {e}")
68
+
69
+ # Get forecast data
70
+ forecast_data = []
71
+ try:
72
+ # Get 5-day forecast
73
+ forecast_url = f"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={self.api_key}&units=metric"
74
+ response = requests.get(forecast_url)
75
+ if response.status_code == 200:
76
+ data = response.json()
77
+ for item in data['list']:
78
+ forecast = {
79
+ 'date': datetime.fromtimestamp(item['dt']),
80
+ 'temperature': item['main']['temp'],
81
+ 'humidity': item['main']['humidity'],
82
+ 'rainfall': item.get('rain', {}).get('3h', 0) * 8,
83
+ 'type': 'forecast_5day',
84
+ 'description': item['weather'][0]['description']
85
+ }
86
+ forecast_data.append(forecast)
87
+
88
+ # Generate extended forecast
89
+ last_date = max(d['date'] for d in forecast_data)
90
+ historical_df = pd.DataFrame(historical_data)
91
+
92
+ for day in range(1, forecast_days - 5):
93
+ date = last_date + timedelta(days=day)
94
+
95
+ # Calculate trends from historical data
96
+ if not historical_df.empty:
97
+ temp_trend = stats.linregress(range(len(historical_df)), historical_df['temperature'])[0]
98
+ humidity_trend = stats.linregress(range(len(historical_df)), historical_df['humidity'])[0]
99
+ rainfall_trend = stats.linregress(range(len(historical_df)), historical_df['rainfall'])[0]
100
+ else:
101
+ temp_trend = humidity_trend = rainfall_trend = 0
102
+
103
+ # Get recent averages
104
+ recent_temps = [d['temperature'] for d in forecast_data[-5:]]
105
+ recent_humidity = [d['humidity'] for d in forecast_data[-5:]]
106
+ recent_rainfall = [d['rainfall'] for d in forecast_data[-5:]]
107
+
108
+ extended_forecast = {
109
+ 'date': date,
110
+ 'temperature': np.mean(recent_temps) + temp_trend * day,
111
+ 'humidity': np.mean(recent_humidity) + humidity_trend * day,
112
+ 'rainfall': np.mean(recent_rainfall) + rainfall_trend * day,
113
+ 'type': 'forecast_extended',
114
+ 'description': 'Extended Forecast'
115
+ }
116
+ forecast_data.append(extended_forecast)
117
+
118
+ except Exception as e:
119
+ print(f"Error fetching forecast data: {e}")
120
+
121
+ # Combine and process all data
122
+ all_data = pd.DataFrame(historical_data + forecast_data)
123
+ all_data = all_data.sort_values('date')
124
+
125
+ # Add analysis columns
126
+ all_data['month'] = all_data['date'].dt.month
127
+ all_data['season'] = all_data['month'].map(self.seasons)
128
+
129
+ # Calculate rolling averages
130
+ all_data['temp_7day_avg'] = all_data['temperature'].rolling(window=7, min_periods=1).mean()
131
+ all_data['humidity_7day_avg'] = all_data['humidity'].rolling(window=7, min_periods=1).mean()
132
+ all_data['rainfall_7day_avg'] = all_data['rainfall'].rolling(window=7, min_periods=1).mean()
133
+
134
+ return all_data
135
+
136
+ def analyze_trends(self, df):
137
+ """Analyze weather trends and patterns"""
138
+ historical = df[df['type'] == 'historical']
139
+ forecast = df[df['type'].isin(['forecast_5day', 'forecast_extended'])]
140
+
141
+ analysis = {
142
+ 'historical': {
143
+ 'temperature': {
144
+ 'mean': historical['temperature'].mean(),
145
+ 'std': historical['temperature'].std(),
146
+ 'trend': stats.linregress(range(len(historical)), historical['temperature'])[0]
147
+ },
148
+ 'humidity': {
149
+ 'mean': historical['humidity'].mean(),
150
+ 'std': historical['humidity'].std(),
151
+ 'trend': stats.linregress(range(len(historical)), historical['humidity'])[0]
152
+ },
153
+ 'rainfall': {
154
+ 'mean': historical['rainfall'].mean(),
155
+ 'std': historical['rainfall'].std(),
156
+ 'trend': stats.linregress(range(len(historical)), historical['rainfall'])[0]
157
+ }
158
+ },
159
+ 'forecast': {
160
+ 'temperature': {
161
+ 'mean': forecast['temperature'].mean(),
162
+ 'std': forecast['temperature'].std(),
163
+ },
164
+ 'humidity': {
165
+ 'mean': forecast['humidity'].mean(),
166
+ 'std': forecast['humidity'].std(),
167
+ },
168
+ 'rainfall': {
169
+ 'mean': forecast['rainfall'].mean(),
170
+ 'std': forecast['rainfall'].std(),
171
+ }
172
+ }
173
+ }
174
+
175
+ return analysis