
import pandas as pd
import matplotlib.pyplot as mp
import matplotlib.patches as mpatches
import matplotlib.transforms as mtrans
import seaborn as sns
from statistics import mean
import numpy as np
from matplotlib.offsetbox import AnchoredText

df1 = pd.read_csv("../output/Res/htp.prn", sep="\s+",na_values="-999")
res1 = df1['DAY'].apply(lambda x: '{0:0>3}'.format(x))
df1['DateJul'] = df1['YR'].apply(str)+res1.apply(str)
df1['DateInt'] = df1['DateJul'].astype(int)
df1['Date'] = [pd.to_datetime(e[:4]) + pd.to_timedelta(int(e[4:]) - 1, unit='D') for e in df1['DateJul']]
df1['Date'] = pd.to_datetime(df1['Date'], format='%Y-%m-%d')
df1.index = df1['Date']
df2 = df1.drop(["DateJul","DateInt"], axis=1)

htp1 = df2[(df2["SUB"] == 1) & (df2["HRU"] == 1)]

val_mean_day = df2.groupby(df2.Date.dt.day).mean()

val_mean_day_p1 = htp1.loc['2009-1-1':'2015-12-31'].groupby(htp1["DAY"]).mean()
val_mean_day_p2 = htp1.loc['2071-1-1':'2100-12-31'].groupby(htp1["DAY"]).mean()
val_mean_day_diff = val_mean_day_p2 -val_mean_day_p1

print(val_mean_day_p1)
print(val_mean_day_p2)
        
fig, ax = mp.subplots(2, 2)

ax = mp.subplot(221)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["PCP"], color='blue', label='PCP ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["SURQ"], color='black', label='SURQ ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["PERC"], color='lightblue', label='PERC ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["et"], color='orange', label='ETa ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["ALAI"], color='green', label='LAI ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["IRR"], color='magenta', label='IRR ',
        marker='o', linestyle='dashed',linewidth=3, markersize=1)
mp.plot(val_mean_day_p1["DAY"], val_mean_day_p1["ws"]*6, color='darkred', label='WS ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.ylim(0, 10)
mp.legend()
mp.grid()
        
ax = mp.subplot(222)
mp.plot(val_mean_day_p2["DAY"], val_mean_day_p2["PCP"], color='blue', label='PCP ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p2["DAY"], val_mean_day_p2["SURQ"], color='black', label='SURQ ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p2["DAY"], val_mean_day_p2["PERC"], color='lightblue', label='PERC ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p2["DAY"], val_mean_day_p2["et"], color='orange', label='ETa ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_p2["DAY"], val_mean_day_p2["ALAI"], color='green', label='LAI ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.ylim(0, 10)
mp.legend()
mp.grid()

ax = mp.subplot(223)
mp.plot(val_mean_day_diff["PCP"], color='blue', label='PCP ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_diff["SURQ"], color='black', label='SURQ ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_diff["PERC"], color='lightblue', label='PERC ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.plot(val_mean_day_diff["et"], color='orange', label='ETa ',
        marker='o', linestyle='dashed',linewidth=1, markersize=1)
mp.legend()
mp.grid()

mp.show()


#df1.loc['1951-1-1':'1960-12-31']
