#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import argparse
import os

import src.data_loading_helpers_twentythirteen_twentyseventeen as dlhrs
import src.loading_helpers_user_inputs as lhui
import src.workflow_helpers as wfh
from src.geo_mapping_helpers import get_station_information_json_feed
from src.utils import get_timestamped_fname, save_to_parquet_file

PROJ_ROOT = os.getcwd()

parser = argparse.ArgumentParser()
parser.add_argument(
    "--epsg",
    type=int,
    dest="epsg",
    default=6345,
    help="geospatial epsg",
)
parser.add_argument(
    "--crs",
    type=int,
    dest="crs",
    default=4326,
    help="geospatial crs",
)
parser.add_argument(
    "--max-duration",
    type=int,
    dest="max_duration",
    default=45 * 60,
    help="maximum length of valid bikeshare trip",
)
args = parser.parse_args()

raw_data_path = os.path.join(PROJ_ROOT, "data", "raw")
processed_data_path = os.path.join(PROJ_ROOT, "data", "processed")

user_inputs = lhui.get_raw_data_loading_inputs(raw_data_path)
missing_lat_lon_dicts = lhui.get_missing_lat_lon_dicts()
d_missing_lat_lon_2013_2017 = missing_lat_lon_dicts["2013_2017"]
d_missing_lat_lon_2020 = missing_lat_lon_dicts["2013_2017"]
fpaths_periods = {
    "2014": ["q1q2", "q307", "q30809", "q4"],
    "2015": ["q1", "q2", "q307", "q308", "q309", "q4"],
    "2016": ["q1", "q204", "q205", "q206", "q3", "q4"],
    "2017": ["q1", "q2", "q3", "q4"],
}

stations_metadata = get_station_information_json_feed()
df_2013 = dlhrs.load_all_2013_data(
    user_inputs["trips"]["2013"],
    user_inputs["stations"]["2013"],
    user_inputs["date_cols"]["2013"],
)
dfs_2014 = dlhrs.load_all_2014_data(
    user_inputs["trips"]["2014"],
    user_inputs["stations"]["2014"],
    user_inputs["date_cols"]["2014"],
)
dfs_2015 = dlhrs.load_all_2015_data(
    user_inputs["trips"]["2015"],
    user_inputs["stations"]["2015"],
    user_inputs["date_cols"]["2015"],
)
dfs_2016 = dlhrs.load_all_2016_data(
    user_inputs["trips"]["2016"],
    user_inputs["stations"]["2016"],
    user_inputs["date_cols"]["2016"],
)
dfs_2017 = dlhrs.load_all_2017_data(
    user_inputs["trips"]["2017"],
    user_inputs["stations"]["2017"],
    user_inputs["date_cols"]["2017"],
)

objs = [df_2013 + dfs_2014 + dfs_2015 + dfs_2016 + dfs_2017]
twenty_thirteen_seventeen_file_paths = [user_inputs["trips"]["2013"]] + [
    user_inputs["trips"][str(y)][p]
    for y in [2014, 2015, 2016, 2017]
    for p in fpaths_periods[str(y)]
]
data, _ = wfh.featurize_2021_trips(
    twenty_thirteen_seventeen_file_paths,
    objs,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={},
    use_parallel=False,
)
data = wfh.fill_trip_start_lat_lon(data, d_missing_lat_lon_2013_2017)

dfs_2018_2019 = wfh.load_all_2018_2019_data(
    user_inputs["filepaths"]["2018_2019"],
    user_inputs["col_names"]["2014"],
    user_inputs["dicts"]["2018_2019"],
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
)
dfs_2020 = wfh.load_all_2020_data(
    user_inputs["filepaths"]["2020"],
    user_inputs["dicts"]["2020"],
    user_inputs["col_names"]["2020"],
)
data_2018_2019_2020, _ = wfh.featurize_2021_trips(
    user_inputs["filepaths"]["2018_2019"] + user_inputs["filepaths"]["2020"],
    dfs_2018_2019 + dfs_2020,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={
        "trip_id": str,
        "from_station_id": str,
        "to_station_id": str,
    },
    use_parallel=True,
)
data_2018_2019_2020 = wfh.fill_trip_start_lat_lon(
    data_2018_2019_2020, d_missing_lat_lon_2020
)
dfs_2021 = wfh.load_all_2020_data(
    user_inputs["filepaths"]["2021"],
    user_inputs["dicts"]["2020"],
    user_inputs["col_names"]["2020"],
)
data_2021, _ = wfh.featurize_2021_trips(
    user_inputs["filepaths"]["2021"],
    dfs_2021,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={
        "trip_id": str,
        "from_station_id": str,
        "to_station_id": str,
    },
    use_parallel=True,
)

filepaths = [
    os.path.join(processed_data_path, get_timestamped_fname(f"filtered_{y}"))
    for y in ["2013_2017", "2018_2020", "2021"]
]
save_to_parquet_file([data, data_2018_2019_2020, data_2021], filepaths)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import argparse
import os

import src.data_loading_helpers_twentythirteen_twentyseventeen as dlhrs
import src.loading_helpers_user_inputs as lhui
import src.workflow_helpers as wfh
from src.geo_mapping_helpers import get_station_information_json_feed
from src.utils import get_timestamped_fname, save_to_parquet_file

PROJ_ROOT = os.getcwd()

parser = argparse.ArgumentParser()
parser.add_argument(
    "--epsg",
    type=int,
    dest="epsg",
    default=6345,
    help="geospatial epsg",
)
parser.add_argument(
    "--crs",
    type=int,
    dest="crs",
    default=4326,
    help="geospatial crs",
)
parser.add_argument(
    "--max-duration",
    type=int,
    dest="max_duration",
    default=45 * 60,
    help="maximum length of valid bikeshare trip",
)
args = parser.parse_args()

raw_data_path = os.path.join(PROJ_ROOT, "data", "raw")
processed_data_path = os.path.join(PROJ_ROOT, "data", "processed")

user_inputs = lhui.get_raw_data_loading_inputs(raw_data_path)
missing_lat_lon_dicts = lhui.get_missing_lat_lon_dicts()
d_missing_lat_lon_2013_2017 = missing_lat_lon_dicts["2013_2017"]
d_missing_lat_lon_2020 = missing_lat_lon_dicts["2013_2017"]
fpaths_periods = {
    "2014": ["q1q2", "q307", "q30809", "q4"],
    "2015": ["q1", "q2", "q307", "q308", "q309", "q4"],
    "2016": ["q1", "q204", "q205", "q206", "q3", "q4"],
    "2017": ["q1", "q2", "q3", "q4"],
}

stations_metadata = get_station_information_json_feed()
df_2013 = dlhrs.load_all_2013_data(
    user_inputs["trips"]["2013"],
    user_inputs["stations"]["2013"],
    user_inputs["date_cols"]["2013"],
)
dfs_2014 = dlhrs.load_all_2014_data(
    user_inputs["trips"]["2014"],
    user_inputs["stations"]["2014"],
    user_inputs["date_cols"]["2014"],
)
dfs_2015 = dlhrs.load_all_2015_data(
    user_inputs["trips"]["2015"],
    user_inputs["stations"]["2015"],
    user_inputs["date_cols"]["2015"],
)
dfs_2016 = dlhrs.load_all_2016_data(
    user_inputs["trips"]["2016"],
    user_inputs["stations"]["2016"],
    user_inputs["date_cols"]["2016"],
)
dfs_2017 = dlhrs.load_all_2017_data(
    user_inputs["trips"]["2017"],
    user_inputs["stations"]["2017"],
    user_inputs["date_cols"]["2017"],
)

objs = [df_2013 + dfs_2014 + dfs_2015 + dfs_2016 + dfs_2017]
twenty_thirteen_seventeen_file_paths = [user_inputs["trips"]["2013"]] + [
    user_inputs["trips"][str(y)][p]
    for y in [2014, 2015, 2016, 2017]
    for p in fpaths_periods[str(y)]
]
data, _ = wfh.featurize_2021_trips(
    twenty_thirteen_seventeen_file_paths,
    objs,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={},
    use_parallel=False,
)
data = wfh.fill_trip_start_lat_lon(data, d_missing_lat_lon_2013_2017)

dfs_2018_2019 = wfh.load_all_2018_2019_data(
    user_inputs["filepaths"]["2018_2019"],
    user_inputs["col_names"]["2014"],
    user_inputs["dicts"]["2018_2019"],
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
)
dfs_2020 = wfh.load_all_2020_data(
    user_inputs["filepaths"]["2020"],
    user_inputs["dicts"]["2020"],
    user_inputs["col_names"]["2020"],
)
data_2018_2019_2020, _ = wfh.featurize_2021_trips(
    user_inputs["filepaths"]["2018_2019"] + user_inputs["filepaths"]["2020"],
    dfs_2018_2019 + dfs_2020,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={
        "trip_id": str,
        "from_station_id": str,
        "to_station_id": str,
    },
    use_parallel=True,
)
data_2018_2019_2020 = wfh.fill_trip_start_lat_lon(
    data_2018_2019_2020, d_missing_lat_lon_2020
)
dfs_2021 = wfh.load_all_2020_data(
    user_inputs["filepaths"]["2021"],
    user_inputs["dicts"]["2020"],
    user_inputs["col_names"]["2020"],
)
data_2021, _ = wfh.featurize_2021_trips(
    user_inputs["filepaths"]["2021"],
    dfs_2021,
    stations_metadata,
    user_inputs["station_renaming_dict"],
    user_inputs["non_stations"],
    max_duration=args.max_duration,
    crs=args.crs,
    epsg=args.epsg,
    dtype_changer_dict={
        "trip_id": str,
        "from_station_id": str,
        "to_station_id": str,
    },
    use_parallel=True,
)

filepaths = [
    os.path.join(processed_data_path, get_timestamped_fname(f"filtered_{y}"))
    for y in ["2013_2017", "2018_2020", "2021"]
]
save_to_parquet_file([data, data_2018_2019_2020, data_2021], filepaths)

#!/usr/bin/env python3 # -*- coding: utf-8 -*-