Skip to content

Commit

Permalink
commit hook changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jday7879 committed Nov 4, 2024
1 parent 4633bda commit 82df270
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions mbs_results/utilities/csw_to_spp_converter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import fnmatch
from os import listdir
from os.path import isfile, join
import pandas as pd

import pandas as pd
from utils import convert_column_to_datetime

def get_patern_df(
filepath: str,
pattern: str
) -> pd.DataFrame:

def get_patern_df(filepath: str, pattern: str) -> pd.DataFrame:
"""Loads as pd dataframe all csv files with pattern.
Parameters
Expand All @@ -23,7 +21,7 @@ def get_patern_df(
pd.DataFrame
Dataframe containg data from all selected files.
"""

filenames = [
filename for filename in listdir(filepath) if isfile(join(filepath, filename))
]
Expand All @@ -33,10 +31,11 @@ def get_patern_df(

return df


def get_qv_and_cp_data(
cp_path: str,
qv_path: str,
) -> pd.DataFrame:
) -> pd.DataFrame:
"""Reads and joins qv and cp data.
Parameters
Expand All @@ -45,28 +44,29 @@ def get_qv_and_cp_data(
Filepath to folder containing cp data.
qv_path : str
Filepath to folder containing qv data.
Returns
-------
pd.DataFrame
Dataframe containing combined qv and cp data.
"""
qv_df = get_patern_df(qv_path,"qv*.csv")
cp_df = get_patern_df(cp_path,"cp*.csv")
qv_and_cp = pd.merge(qv_df,cp_df,how = "left",on = ["period","reference"])

qv_df = get_patern_df(qv_path, "qv*.csv")
cp_df = get_patern_df(cp_path, "cp*.csv")

qv_and_cp = pd.merge(qv_df, cp_df, how="left", on=["period", "reference"])

return qv_and_cp


def csw_to_spp(
cp_path: str,
qv_path: str,
output_path: str,
column_map: dict,
period: str,
period_range: int
) -> None:
period_range: int,
) -> None:
"""Combines cp and qv files, filters and renames columns based on a mapping, and
then saves the output as a json file.
Expand All @@ -79,23 +79,26 @@ def csw_to_spp(
output_path : str
Filepath to save json file.
column_map : dict
Dictionary containing desired columns from qv and cp data as keys and their
Dictionary containing desired columns from qv and cp data as keys and their
desired names as values.
period : str
Date to filter output on (YYYY-MM-DD).
period_range : str
Number of months from the period and previous to include in the output.
"""
qv_and_cp = get_qv_and_cp_data(cp_path,qv_path)
qv_and_cp = get_qv_and_cp_data(cp_path, qv_path)

qv_and_cp["period"] = convert_column_to_datetime(qv_and_cp["period"])

period = pd.Timestamp(period)

qv_and_cp = qv_and_cp[(qv_and_cp['period'] > period - pd.DateOffset(months=period_range)) & (qv_and_cp['period'] <= period)]

qv_and_cp["period"] = qv_and_cp["period"].dt.strftime('%Y%m')


qv_and_cp = qv_and_cp[
(qv_and_cp["period"] > period - pd.DateOffset(months=period_range))
& (qv_and_cp["period"] <= period)
]

qv_and_cp["period"] = qv_and_cp["period"].dt.strftime("%Y%m")

qv_and_cp = qv_and_cp[column_map.keys()].rename(columns=column_map)

qv_and_cp.to_json(f"{output_path}_{period.strftime('%Y%m')}_{period_range}.json")

0 comments on commit 82df270

Please sign in to comment.