2021-06-28 16:16:14 -04:00
|
|
|
import csv
|
2021-08-05 15:35:54 -04:00
|
|
|
import os
|
2021-08-02 12:16:38 -04:00
|
|
|
from pathlib import Path
|
2021-06-28 16:16:14 -04:00
|
|
|
|
2021-08-05 15:35:54 -04:00
|
|
|
import pandas as pd
|
|
|
|
from data_pipeline.config import settings
|
|
|
|
from data_pipeline.utils import (
|
|
|
|
get_module_logger,
|
2021-07-20 14:55:39 -04:00
|
|
|
remove_all_dirs_from_dir,
|
2021-08-05 15:35:54 -04:00
|
|
|
remove_files_from_dir,
|
2021-07-20 14:55:39 -04:00
|
|
|
unzip_file_from_url,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger = get_module_logger(__name__)
|
2021-06-28 16:16:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def reset_data_directories(data_path: Path) -> None:
|
|
|
|
census_data_path = data_path / "census"
|
|
|
|
|
|
|
|
# csv
|
|
|
|
csv_path = census_data_path / "csv"
|
|
|
|
remove_files_from_dir(csv_path, ".csv")
|
|
|
|
|
|
|
|
# geojson
|
|
|
|
geojson_path = census_data_path / "geojson"
|
|
|
|
remove_files_from_dir(geojson_path, ".json")
|
|
|
|
|
|
|
|
# shp
|
|
|
|
shp_path = census_data_path / "shp"
|
|
|
|
remove_all_dirs_from_dir(shp_path)
|
|
|
|
|
|
|
|
|
|
|
|
def get_state_fips_codes(data_path: Path) -> list:
|
|
|
|
fips_csv_path = data_path / "census" / "csv" / "fips_states_2010.csv"
|
|
|
|
|
|
|
|
# check if file exists
|
|
|
|
if not os.path.isfile(fips_csv_path):
|
2021-08-02 12:16:38 -04:00
|
|
|
logger.info("Downloading fips from S3 repository")
|
2021-06-28 16:16:14 -04:00
|
|
|
unzip_file_from_url(
|
2021-08-05 12:55:21 -04:00
|
|
|
settings.AWS_JUSTICE40_DATASOURCES_URL + "/fips_states_2010.zip",
|
2021-06-28 16:16:14 -04:00
|
|
|
data_path / "tmp",
|
|
|
|
data_path / "census" / "csv",
|
|
|
|
)
|
|
|
|
|
|
|
|
fips_state_list = []
|
2021-09-14 17:28:59 -04:00
|
|
|
with open(fips_csv_path, encoding="utf-8") as csv_file:
|
2021-06-28 16:16:14 -04:00
|
|
|
csv_reader = csv.reader(csv_file, delimiter=",")
|
|
|
|
line_count = 0
|
|
|
|
|
|
|
|
for row in csv_reader:
|
|
|
|
if line_count == 0:
|
|
|
|
line_count += 1
|
|
|
|
else:
|
|
|
|
fips = row[0].strip()
|
|
|
|
fips_state_list.append(fips)
|
|
|
|
return fips_state_list
|
2021-07-26 08:02:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_state_information(data_path: Path) -> pd.DataFrame:
|
|
|
|
"""Load the full state file as a dataframe.
|
|
|
|
|
|
|
|
Useful because of the state regional information.
|
|
|
|
"""
|
|
|
|
fips_csv_path = data_path / "census" / "csv" / "fips_states_2010.csv"
|
|
|
|
|
|
|
|
df = pd.read_csv(fips_csv_path)
|
|
|
|
|
|
|
|
# Left pad the FIPS codes with 0s
|
|
|
|
df["fips"] = df["fips"].astype(str).apply(lambda x: x.zfill(2))
|
|
|
|
|
|
|
|
return df
|