-
Notifications
You must be signed in to change notification settings - Fork 15
/
functions.py
385 lines (309 loc) · 12.4 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import os
import datetime
import time
import requests
import pandas as pd
import json
from geopy.geocoders import Nominatim
def convert_date_to_unix(x):
"""
Convert datetime to unix time in milliseconds.
"""
dt_obj = datetime.datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S')
dt_obj = int(dt_obj.timestamp() * 1000)
return dt_obj
def get_city_coordinates(city_name: str):
"""
Takes city name and returns its latitude and longitude (rounded to 2 digits after dot).
"""
# Initialize Nominatim API (for getting lat and long of the city)
geolocator = Nominatim(user_agent="MyApp")
city = geolocator.geocode(city_name)
latitude = round(city.latitude, 2)
longitude = round(city.longitude, 2)
return latitude, longitude
##################################### EEA
def convert_to_daily(df, pollutant: str):
"""
Returns DataFrame where pollutant column is resampled to days and rounded.
"""
res_df = df.copy()
# convert dates in 'time' column
res_df["date"] = pd.to_datetime(res_df["date"])
# I want data daily, not hourly (mean per each day = 1 datarow per 1 day)
res_df = res_df.set_index('date')
res_df = res_df[pollutant].resample('1d').mean().reset_index()
res_df[pollutant] = res_df[pollutant].fillna(res_df[pollutant].median())
res_df[pollutant] = res_df[pollutant].apply(lambda x: round(x, 0))
return res_df
def find_fullest_csv(csv_links: list, year: str):
candidates = [link for link in csv_links if str(year) in link]
biggest_df = pd.read_csv(candidates[0])
for link in candidates[1:]:
_df = pd.read_csv(link)
if len(biggest_df) < len(_df):
biggest_df = _df
return biggest_df
def get_air_quality_from_eea(city_name: str,
pollutant: str,
start_year: str,
end_year: str):
"""
Takes city name, daterange and returns pandas DataFrame with daily air quality data.
It parses data by 1-year batches, so please specify years, not dates. (example: "2014", "2022"...)
EEA means European Environmental Agency. So it has data for Europe Union countries ONLY.
"""
start_of_cell = time.time()
params = {
'CountryCode': '',
'CityName': city_name,
'Pollutant': pollutant.upper(),
'Year_from': start_year,
'Year_to': end_year,
'Station': '',
'Source': 'All',
'Samplingpoint': '',
'Output': 'TEXT',
'UpdateDate': '',
'TimeCoverage': 'Year'
}
# observations endpoint
base_url = "https://fme.discomap.eea.europa.eu/fmedatastreaming/AirQualityDownload/AQData_Extract.fmw?"
try:
response = requests.get(base_url, params=params)
except ConnectionError:
response = requests.get(base_url, params=params)
response.encoding = response.apparent_encoding
csv_links = response.text.split("\r\n")
res_df = pd.DataFrame()
target_year = int(start_year)
for year in range(int(start_year), int(end_year) + 1):
try:
# find the fullest, the biggest csv file with observations for this particular year
_df = find_fullest_csv(csv_links, year)
# append it to res_df
res_df = pd.concat([res_df, _df])
except IndexError:
print(f"!! Missing data for {year} for {city} city.")
pass
pollutant = pollutant.lower()
if pollutant == "pm2.5":
pollutant = "pm2_5"
res_df = res_df.rename(columns={
'DatetimeBegin': 'date',
'Concentration': pollutant
})
# cut timezones info
res_df['date'] = res_df['date'].apply(lambda x: x[:-6])
# convert dates in 'time' column
res_df['date'] = pd.to_datetime(res_df['date'])
res_df = convert_to_daily(res_df, pollutant)
res_df['city_name'] = city_name
res_df = res_df[['city_name', 'date', pollutant.lower()]]
end_of_cell = time.time()
print(f"Processed {pollutant.upper()} for {city_name} since {start_year} till {end_year}.")
print(f"Took {round(end_of_cell - start_of_cell, 2)} sec.\n")
return res_df
##################################### USEPA
city_code_dict = {}
pollutant_dict = {
'CO': '42101',
'SO2': '42401',
'NO2': '42602',
'O3': '44201',
'PM10': '81102',
'PM2.5': '88101'
}
def get_city_code(city_name: str):
"Encodes city name to be used later for data parsing using USEPA."
if city_code_dict:
city_full = [i for i in city_code_dict.keys() if city_name in i][0]
return city_code_dict[city_full]
else:
params = {
"email": "[email protected]",
"key": "test"
}
response = requests.get("https://aqs.epa.gov/data/api/list/cbsas?", params)
response_json = response.json()
data = response_json["Data"]
for item in data:
city_code_dict[item['value_represented']] = item['code']
return get_city_code(city_name)
def get_air_quality_from_usepa(city_name: str,
pollutant: str,
start_date: str,
end_date: str):
"""
Takes city name, daterange and returns pandas DataFrame with daily air quality data.
USEPA means United States Environmental Protection Agency. So it has data for US ONLY.
"""
start_of_cell = time.time()
res_df = pd.DataFrame()
for start_date_, end_date_ in make_date_intervals(start_date, end_date):
params = {
"email": "[email protected]",
"key": "test",
"param": pollutant_dict[pollutant.upper().replace("_", ".")], # encoded pollutant
"bdate": start_date_,
"edate": end_date_,
"cbsa": get_city_code(city_name) # Core-based statistical area
}
# observations endpoint
base_url = "https://aqs.epa.gov/data/api/dailyData/byCBSA?"
response = requests.get(base_url, params=params)
response_json = response.json()
df_ = pd.DataFrame(response_json["Data"])
pollutant = pollutant.lower()
if pollutant == "pm2.5":
pollutant = "pm2_5"
df_ = df_.rename(columns={
'date_local': 'date',
'arithmetic_mean': pollutant
})
# convert dates in 'date' column
df_['date'] = pd.to_datetime(df_['date'])
df_['city_name'] = city_name
df_ = df_[['city_name', 'date', pollutant]]
res_df = pd.concat([res_df, df_])
# there are duplicated rows (several records for the same day and station). get rid of it.
res_df = res_df.groupby(['date', 'city_name'], as_index=False)[pollutant].mean()
res_df[pollutant] = round(res_df[pollutant], 1)
end_of_cell = time.time()
print(f"Processed {pollutant.upper()} for {city_name} since {start_date} till {end_date}.")
print(f"Took {round(end_of_cell - start_of_cell, 2)} sec.\n")
return res_df
def make_date_intervals(start_date, end_date):
start_dt = datetime.datetime.strptime(start_date, '%Y-%m-%d')
end_dt = datetime.datetime.strptime(end_date, '%Y-%m-%d')
date_intervals = []
for year in range(start_dt.year, end_dt.year + 1):
year_start = datetime.datetime(year, 1, 1)
year_end = datetime.datetime(year, 12, 31)
interval_start = max(start_dt, year_start)
interval_end = min(end_dt, year_end)
if interval_start < interval_end:
date_intervals.append((interval_start.strftime('%Y%m%d'), interval_end.strftime('%Y%m%d')))
return date_intervals
##################################### Weather Open Meteo
def get_weather_data_from_open_meteo(city_name: str,
start_date: str,
end_date: str,
coordinates: list = None,
forecast: bool = False):
"""
Takes [city name OR coordinates] and returns pandas DataFrame with weather data.
Examples of arguments:
coordinates=(47.755, -122.2806), start_date="2023-01-01"
"""
start_of_cell = time.time()
if coordinates:
latitude, longitude = coordinates
else:
latitude, longitude = get_city_coordinates(city_name=city_name)
params = {
'latitude': latitude,
'longitude': longitude,
'daily': ["temperature_2m_max", "temperature_2m_min",
"precipitation_sum", "rain_sum", "snowfall_sum",
"precipitation_hours", "windspeed_10m_max",
"windgusts_10m_max", "winddirection_10m_dominant"],
'start_date': start_date,
'end_date': end_date,
'timezone': "Europe/London"
}
if forecast:
# historical forecast endpoint
base_url = 'https://api.open-meteo.com/v1/forecast'
else:
# historical observations endpoint
base_url = 'https://archive-api.open-meteo.com/v1/archive'
try:
response = requests.get(base_url, params=params)
except ConnectionError:
response = requests.get(base_url, params=params)
response_json = response.json()
res_df = pd.DataFrame(response_json["daily"])
res_df["city_name"] = city_name
# rename columns
res_df = res_df.rename(columns={
"time": "date",
"temperature_2m_max": "temperature_max",
"temperature_2m_min": "temperature_min",
"windspeed_10m_max": "wind_speed_max",
"winddirection_10m_dominant": "wind_direction_dominant",
"windgusts_10m_max": "wind_gusts_max"
})
# change columns order
res_df = res_df[
['city_name', 'date', 'temperature_max', 'temperature_min',
'precipitation_sum', 'rain_sum', 'snowfall_sum',
'precipitation_hours', 'wind_speed_max',
'wind_gusts_max', 'wind_direction_dominant']
]
# convert dates in 'date' column
res_df["date"] = pd.to_datetime(res_df["date"])
end_of_cell = time.time()
print(f"Parsed weather for {city_name} since {start_date} till {end_date}.")
print(f"Took {round(end_of_cell - start_of_cell, 2)} sec.\n")
return res_df
##################################### Air Quality data from Open Meteo
def get_aqi_data_from_open_meteo(city_name: str,
start_date: str,
end_date: str,
coordinates: list = None,
pollutant: str = "pm2_5"):
"""
Takes [city name OR coordinates] and returns pandas DataFrame with AQI data.
Examples of arguments:
...
coordinates=(47.755, -122.2806),
start_date="2023-01-01",
pollutant="no2"
...
"""
start_of_cell = time.time()
if coordinates:
latitude, longitude = coordinates
else:
latitude, longitude = get_city_coordinates(city_name=city_name)
pollutant = pollutant.lower()
if pollutant == "pm2.5":
pollutant = "pm2_5"
# make it work with both "no2" and "nitrogen_dioxide" passed.
if pollutant == "no2":
pollutant = "nitrogen_dioxide"
params = {
'latitude': latitude,
'longitude': longitude,
'hourly': [pollutant],
'start_date': start_date,
'end_date': end_date,
'timezone': "Europe/London"
}
# base endpoint
base_url = "https://air-quality-api.open-meteo.com/v1/air-quality"
try:
response = requests.get(base_url, params=params)
except ConnectionError:
response = requests.get(base_url, params=params)
response_json = response.json()
res_df = pd.DataFrame(response_json["hourly"])
# convert dates
res_df["time"] = pd.to_datetime(res_df["time"])
# resample to days
res_df = res_df.groupby(res_df['time'].dt.date).mean(numeric_only=True).reset_index()
res_df[pollutant] = round(res_df[pollutant], 1)
# rename columns
res_df = res_df.rename(columns={
"time": "date"
})
res_df["city_name"] = city_name
# change columns order
res_df = res_df[
['city_name', 'date', pollutant]
]
end_of_cell = time.time()
print(f"Processed {pollutant.upper()} for {city_name} since {start_date} till {end_date}.")
print(f"Took {round(end_of_cell - start_of_cell, 2)} sec.\n")
return res_df