-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadd_breaks_to_json.py
48 lines (39 loc) · 1.29 KB
/
add_breaks_to_json.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
import json
import configparser
import os
from datetime import datetime as dt
# Get paths from config
config = configparser.ConfigParser()
config.read("config.ini")
data_json_path = config["output"]["out_json_path"]
breaks_json_path = os.path.join(config["GIF output"]["gif_output"], "breaks-{}.json".format(dt.now().strftime("%Y-%m-%d")))
data_json = {}
with open(data_json_path) as json_file:
data_json = json.load(json_file)
print("Read biothings items from {}.".format(data_json_path))
breaks_json = {}
with open(breaks_json_path) as json_file:
breaks_json = json.load(json_file)
print("Read breaks json from {}.".format(breaks_json_path))
print("Adding breaks .. ")
# Add breaks_json based n admin_level
admin_level_id_map = {
0: "admin0",
1: "US_states",
2: "US_counties",
1.5: "US_metros"
}
for rec in data_json:
if rec["admin_level"] not in admin_level_id_map.keys():
continue
_breaks = next(i for i in breaks_json if i["id"] == admin_level_id_map[rec["admin_level"]])
if _breaks == None:
continue
for k,v in _breaks.items():
if "breaks" not in k:
continue
rec[k] = v
# Write to file
with open(data_json_path, "w") as outfile:
json.dump(data_json, outfile)
print("JSON with breaks written to {}.".format(data_json_path))