-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_acs.py
More file actions
130 lines (121 loc) · 3.84 KB
/
parse_acs.py
File metadata and controls
130 lines (121 loc) · 3.84 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
# standard library imports
import argparse
from pathlib import Path
# third-party imports
from loguru import logger
import pandas as pd
# local imports
from settings import (
RAW_ACS_DATA_DIR,
INTERIM_DIR,
ACS_SPAN,
ACS_YEAR,
LOOKUPS_SRC,
PROCESSED_DIR,
)
from src.acs import ACS
if __name__ == "__main__":
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Configure and instantiate logger")
logger.add(
f"log_{__file__}.log".replace(".py", ""), backtrace=False, diagnose=False
)
logger.debug(f"Begin {__file__}")
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Parse arguments")
try:
description = "Parse raw ACS tables and join them into one table"
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-l",
"--lookups_input_src",
default=LOOKUPS_SRC,
help="Path to input file you can edit to select which ACS tables you want",
type=Path,
)
parser.add_argument(
"-r",
"--raw_acs_data_dir",
default=RAW_ACS_DATA_DIR,
help="Directory to download raw ACS data",
)
parser.add_argument(
"-i",
"--interim_dir",
default=INTERIM_DIR,
help="Directory to save parsed ACS files",
)
parser.add_argument(
"-p",
"--processed_dir",
default=PROCESSED_DIR,
help="Directory to save parsed ACS files",
)
parser.add_argument(
"-s",
"--acs_span",
default=ACS_SPAN,
help="Specify which year of ACS data you want",
type=int,
)
parser.add_argument(
"-y",
"--acs_year",
default=ACS_YEAR,
help="Specify which year of ACS data you want",
type=int,
)
args = parser.parse_args()
lookups_input_src = args.lookups_input_src
raw_acs_data_dir = args.raw_acs_data_dir
interim_dir = args.interim_dir
acs_span = args.acs_span
acs_year = args.acs_year
processed_dir = args.processed_dir
logger.debug("Finish parsing arguments")
except Exception:
logger.error("Failed to parse arguments", exc_info=True)
raise
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Get zips, geos, and lookups data")
try:
acs = ACS(
acs_year,
acs_span,
raw_acs_data_dir,
interim_dir,
lookups_input_src,
overwrite=False,
verbose=False,
)
acs.get_data_zips()
acs.get_geos()
acs.get_lookups()
logger.debug("Finish getting zips, geos, and lookups data")
except Exception:
logger.error("Failed to get zips / geos / lookups data", exc_info=True)
raise
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Parse tables")
try:
acs.parse_tables()
logger.debug("Finished parsing tables")
except Exception:
logger.error("Failed to parse tables", exc_info=True)
raise
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Join tables")
try:
acs.join_tables()
logger.debug('Joined tables')
except Exception:
logger.error("Failed to join tables", exc_info=True)
raise
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print("Preprocess tables")
try:
acs.preprocess_tables()
logger.debug('Preprocessed tables')
except Exception:
logger.error("Failed to preprocess tables", exc_info=True)
raise