"""This file will generate irrigation restriction CDFs for use with the IID
 This file takes 3 arguments, the first argument is the path to the restriction record (does not detrend in this file),
 The second argument is the path to event definition data the KSL standard not run through fix_csv.py,
 3rd outdir
 CDFs will be output in outdir as a csv

 Author: <NAME>, Date: 18/02/2021"""

import pandas as pd
import numpy as np
from pathlib import Path
import sys
import calendar
from datetime import datetime
import warnings
from collections import defaultdict
warnings.filterwarnings("ignore")
import os

if len(sys.argv) != 4:
    print("Please run with 2 arguments, arg1: restriction record csv,  arg2: event definition data csv, arg3: outdir")
    sys.exit()

res_path = Path(sys.argv[1])
res_csv = pd.read_csv(res_path, comment="#")
event_path = Path(sys.argv[2])
event_csv = pd.read_csv(event_path, skiprows=[0])

outdir = sys.argv[3]
if not os.path.exists(outdir):
    os.makedirs(outdir)

def fix_precip(x):
    if x == 1:
        return "D"
    else:
        if not np.isnan(x):
            return "ND"

event_csv["precip"] = event_csv["precip"].apply(fix_precip)
event_csv["prev_precip"] = event_csv["prev_precip"].apply(fix_precip)
event_csv["s_precip_class"] = event_csv["prev_precip"] + "," + event_csv["precip"]
res_csv["month"] = res_csv["month"].transform(lambda x: int(x))
res_csv["year"] = res_csv["year"].transform(lambda x: int(x))

merge_csv = event_csv.merge(res_csv, on=["year", "month"], how="left")

merge_csv["f_rest_mean"] = None
for i in range(merge_csv["year"].min(), merge_csv["year"].max()+1):
    for j in range(1, 13):
        mask = (merge_csv["year"] == i) & (merge_csv["month"] == j)
        merge_csv["f_rest_mean"].loc[mask] = np.mean(merge_csv["f_rest"].loc[mask].values)

merge_csv = merge_csv.drop(merge_csv.loc[merge_csv["day"] != 1.0].index)

bins = [0, 0, 0.25, 0.5, 0.75, 1, 1]

dis_dict = defaultdict(dict)

keys = ["D,D", "D,ND", "ND,ND", "ND,D"]
for key in keys:
    for i in range(1, 13):
        hist = []

        month_df = merge_csv.loc[merge_csv["month"] == i]
        dis_df = month_df.loc[month_df["s_precip_class"] == key]
        values = dis_df["f_rest_mean"].values

        hist.append((values == 0).sum())
        hist.append(((values > 0) & (values < 0.25)).sum())
        hist.append(((values > 0.25) & (values < 0.5)).sum())
        hist.append(((values > 0.5) & (values < 0.75)).sum())
        hist.append(((values > 0.75) & (values < 1)).sum())
        hist.append((values == 1).sum())
        fixed_hist = np.asarray(hist) / np.sum(np.asarray(hist))

        arr = np.flip(np.flip(fixed_hist).cumsum()).tolist()
        arr.append(0)

        print(f"{key}, {calendar.month_abbr[i]}, {arr}")
        dis_dict[key][calendar.month_abbr[i]] = arr


for key in keys:

    df = pd.DataFrame(data=dis_dict[key],
                      index=bins).fillna(0) # fill na to allow me to run event combinations without restrictions

    f = open(os.path.join(outdir, f"Ir_CDFs_{key}.csv"), "w")
    f.write(f"# This file contains Irrigation restriction CDF's for the {key} dissagreation \n")
    f.write(f"# This file is intended to be used by the IID. \n")
    f.write(f"# file generated by f_rest_to_cdf.py   Author: <NAME>    Date: {datetime.now()} \n")
    f.write(f"# restriction record use for generation {res_path} \n")
    f.write(f"# Event data record used for generation {event_path} \n")
    df.to_csv(f)
    f.close()

print("done")
"""This file will generate irrigation restriction CDFs for use with the IID
 This file takes 3 arguments, the first argument is the path to the restriction record (does not detrend in this file),
 The second argument is the path to event definition data the KSL standard not run through fix_csv.py,
 3rd outdir
 CDFs will be output in outdir as a csv

 Author: <NAME>, Date: 18/02/2021"""

import pandas as pd
import numpy as np
from pathlib import Path
import sys
import calendar
from datetime import datetime
import warnings
from collections import defaultdict
warnings.filterwarnings("ignore")
import os

if len(sys.argv) != 4:
    print("Please run with 2 arguments, arg1: restriction record csv,  arg2: event definition data csv, arg3: outdir")
    sys.exit()

res_path = Path(sys.argv[1])
res_csv = pd.read_csv(res_path, comment="#")
event_path = Path(sys.argv[2])
event_csv = pd.read_csv(event_path, skiprows=[0])

outdir = sys.argv[3]
if not os.path.exists(outdir):
    os.makedirs(outdir)

def fix_precip(x):
    if x == 1:
        return "D"
    else:
        if not np.isnan(x):
            return "ND"

event_csv["precip"] = event_csv["precip"].apply(fix_precip)
event_csv["prev_precip"] = event_csv["prev_precip"].apply(fix_precip)
event_csv["s_precip_class"] = event_csv["prev_precip"] + "," + event_csv["precip"]
res_csv["month"] = res_csv["month"].transform(lambda x: int(x))
res_csv["year"] = res_csv["year"].transform(lambda x: int(x))

merge_csv = event_csv.merge(res_csv, on=["year", "month"], how="left")

merge_csv["f_rest_mean"] = None
for i in range(merge_csv["year"].min(), merge_csv["year"].max()+1):
    for j in range(1, 13):
        mask = (merge_csv["year"] == i) & (merge_csv["month"] == j)
        merge_csv["f_rest_mean"].loc[mask] = np.mean(merge_csv["f_rest"].loc[mask].values)

merge_csv = merge_csv.drop(merge_csv.loc[merge_csv["day"] != 1.0].index)

bins = [0, 0, 0.25, 0.5, 0.75, 1, 1]

dis_dict = defaultdict(dict)

keys = ["D,D", "D,ND", "ND,ND", "ND,D"]
for key in keys:
    for i in range(1, 13):
        hist = []

        month_df = merge_csv.loc[merge_csv["month"] == i]
        dis_df = month_df.loc[month_df["s_precip_class"] == key]
        values = dis_df["f_rest_mean"].values

        hist.append((values == 0).sum())
        hist.append(((values > 0) & (values < 0.25)).sum())
        hist.append(((values > 0.25) & (values < 0.5)).sum())
        hist.append(((values > 0.5) & (values < 0.75)).sum())
        hist.append(((values > 0.75) & (values < 1)).sum())
        hist.append((values == 1).sum())
        fixed_hist = np.asarray(hist) / np.sum(np.asarray(hist))

        arr = np.flip(np.flip(fixed_hist).cumsum()).tolist()
        arr.append(0)

        print(f"{key}, {calendar.month_abbr[i]}, {arr}")
        dis_dict[key][calendar.month_abbr[i]] = arr


for key in keys:

    df = pd.DataFrame(data=dis_dict[key],
                      index=bins).fillna(0) # fill na to allow me to run event combinations without restrictions

    f = open(os.path.join(outdir, f"Ir_CDFs_{key}.csv"), "w")
    f.write(f"# This file contains Irrigation restriction CDF's for the {key} dissagreation \n")
    f.write(f"# This file is intended to be used by the IID. \n")
    f.write(f"# file generated by f_rest_to_cdf.py   Author: <NAME>    Date: {datetime.now()} \n")
    f.write(f"# restriction record use for generation {res_path} \n")
    f.write(f"# Event data record used for generation {event_path} \n")
    df.to_csv(f)
    f.close()

print("done")
This file will generate irrigation restriction CDFs for use with the IID  This file takes 3 arguments, the first argument is the path to the restriction record (does not detrend in this file),  The second argument is the path to event definition data the KSL standard not run through fix_csv.py,  3rd outdir  CDFs will be output in outdir as a csv   Author: <NAME>, Date: 18/02/2021 # fill na to allow me to run event combinations without restrictions