-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnilu_data.py
90 lines (73 loc) · 2.75 KB
/
nilu_data.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
import sys
import os
import json
import multiprocessing as mp
import requests
from datetime import datetime
def format_url(api, station, component, year, month):
return f'{api}/obs/historical/{year}-{month}-01/{year}{month+1}-01/{station}?component={component}'
def download_to_json(urls: str) -> dict:
results = []
for url in urls:
req = requests.get(url)
results += [req.json() if req.status_code == 200 else []]
return results
def download_nilu_historical_data(args):
api, station, components, from_date, to_date, savepath = args
# Compiling list of all urls to gather data from:
urls = []
for year in range(from_date.year, to_date.year, 1):
for month in range(1, 12, 1):
urls += [format_url(api, station, components, year, month)]
# Downloading (may take some time):
results = download_to_json(urls)
# Filtering empty results:
results = [result for result in results if len(result) > 0]
# Joining results together into single dict:
try:
result = {
key: value
for key, value in results[0][0].items()
if key not in ["component", "values"]
}
air_data = {component: [] for component in components.split(",")}
for r in results:
for data in r:
air_data[data["component"]] += data["values"]
result["values"] = air_data
except IndexError:
result = {}
# Writing to file:
filepath = os.path.join(savepath, "nilu", "{}.json".format(station))
with open(filepath, "w") as f:
json.dump(result, f)
print(" - Saved {} data to {}".format(station, filepath))
return result
def download_to(
savepath="./data", from_date=datetime(2014, 1, 1), to_date=datetime(2020, 1, 1)
):
# Check if the data already exists:
if os.path.isdir(savepath):
print("Found directory: {}".format(os.path.abspath(savepath)))
else:
os.makedirs(savepath, exist_ok=True)
print("Created directory: {}".format(os.path.abspath(savepath)))
os.makedirs(os.path.join(savepath, "nilu"), exist_ok=True)
# Downloading Airquality data from:
api = "https://api.nilu.no"
stations = [
res["station"]
for res in requests.get(api + "/lookup/stations?area=Trondheim").json()
]
components = ",".join(
[res["component"] for res in requests.get(api + "/lookup/components").json()]
)
print("Downloading air-quality data for {} stations".format(len(stations)))
packed_args = [
(api, station_id, components, from_date, to_date, savepath)
for station_id in stations
]
pool = mp.Pool()
processes = pool.map_async(download_nilu_historical_data, packed_args)
pool.close()
return processes.get()