-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounty.py
65 lines (56 loc) · 1.99 KB
/
county.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
from datetime import datetime
import os
import sys
import aid
import conf
import spew
import us
def to_county_id(csv):
tokens = csv.split('/')
county = tokens[-1].split('_')[-1][:5]
return county
def translate(states):
print('Started translating counties in', states)
path = 'logs/'
aid.mkdir(path)
with open(path + 'counties.' + str(datetime.now()), 'w') as common:
sys.stdout = common
sys.stderr = common
print('Translating', states)
for state in states:
if state == 'input':
continue
aid.log_time('Translating state ID = ' + state)
try:
pp_csvs = spew.find_csvs(conf.pp_prefix, state)
counties = set([to_county_id(csv) for csv in pp_csvs])
print(counties, flush=True)
for county in counties:
try:
prefix = path + state + '/'+ county
stdout = prefix + '.out'
aid.mkdir(stdout)
if os.path.exists(stdout):
print(stdout, 'already exists. Delete it if you want to rerun.')
continue
aid.log_time('Translating county ID = ' + county)
sys.stdout = open(stdout, 'w')
sys.stderr = open(prefix + '.err', 'w')
us.translate(county)
except Exception as e:
aid.log_error(e)
finally:
aid.log_time()
sys.stdout = common
sys.stderr = common
except Exception as e:
aid.log_error(e)
finally:
aid.log_time()
sys.stdout = common
sys.stderr = common
aid.log_time('Done')
def test():
translate(['10'])
if __name__ == "__main__":
translate(os.listdir(conf.path_usa))