Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
140 lines (101 loc) · 3.81 KB
/
Copy pathparser.py
File metadata and controls
140 lines (101 loc) · 3.81 KB
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
import datetime
import requests
import pandas as pd
class Place:
def __init__(self, name, id, lat, long) -> None:
self.name = name
self.id = id
self.lat = lat
self.long = long
places = [
Place('Beijing', 2038349, 39.9042, 116.4074),
Place('Zhangjiakou', 2033194, 40.7675, 114.8863),
Place('Qinhuangdao', 1797593, 39.9354, 119.6005),
Place('Shijiazhuang', 1795266, 38.0428, 114.5149),
Place('Tianjin', 1792943, 39.3434, 117.3616),
]
start_time = datetime.datetime(2017, 3, 31, hour=0)
end_time = datetime.datetime.now()
_dates = pd.date_range(start_time, end_time, freq="1h")
pollution_params = ['co', 'no2', 'o3', 'pm10', 'pm25', 'so2']
weather_params = ['pressure', 'windSpeed', 'windBearing']
beijing_hours = [1, 3, 6, 12, 24, 48, 72, 168]
bj_pm = 'Beijing pm25'
def get_header():
header = []
for place in places:
for param in pollution_params + weather_params:
header.append('{} {}'.format(place.name, param))
for hours in beijing_hours:
header.append('{} {}'.format(bj_pm, hours))
return header
df = pd.DataFrame(index=_dates, columns=get_header())
def get_place_pollution(place, r_start, r_end):
key = 'x9HE9OTzXpQIRFaHZljhePhOq6egQ8ko2AnGmOq0l6BfWm4w'
url = 'https://api.origins-china.cn/v1/places/{}/history?series=raw&begin={}&end={}&key={}'
r = requests.get(url.format(place.id, r_start, r_end, key))
info = r.json()['info.aqi']
for hour_info in info:
pollution = hour_info['data']
timestamp = hour_info['ts']
timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
for param in pollution_params:
try:
val = pollution[param]
except KeyError:
val = None
finally:
col = '{} {}'.format(place.name, param)
df.at[timestamp, col] = val
def get_pollution():
delta = datetime.timedelta(days=30)
time_format = '%Y-%m-%dT%H:%M:%SZ'
r_time = start_time
while r_time < end_time:
r_start = r_time.strftime(time_format)
r_time += delta
r_end = r_time.strftime(time_format)
for place in places:
get_place_pollution(place, r_start, r_end)
def add_beijing_pm():
for row in df.itertuples(index=True):
timestamp = row[0]
for hours in beijing_hours:
delta = datetime.timedelta(hours=hours)
r_time = timestamp + delta
try:
pm_val = df.at[r_time, bj_pm]
except KeyError:
pm_val = None
finally:
df.at[timestamp, '{} {}'.format(bj_pm, hours)] = pm_val
def get_weather():
key = '4317146d63a0e039a4110e3ea201bd3d'
url = 'https://api.darksky.net/forecast/{}/{},{},{}'
delta = datetime.timedelta(hours=24)
r_time = start_time
while r_time < end_time:
unix_time = int(r_time.timestamp())
for place in places:
r = requests.get(url.format(key, place.lat, place.long, unix_time))
hourly_weather = r.json()['hourly']['data']
for weather in hourly_weather:
timestamp = datetime.datetime.fromtimestamp(weather['time'])
for param in weather_params:
try:
val = weather[param]
except KeyError:
val = None
finally:
col = '{} {}'.format(place.name, param)
df.at[timestamp, col] = val
r_time += delta
def save_df():
get_pollution()
get_weather()
add_beijing_pm()
time_format = '%Y-%m-%d %H:%M'
time_header = 'Timestamp (UTC)'
df[1:].to_csv('weather.csv', index_label=time_header, date_format=time_format)
if __name__ == '__main__':
save_df()