
'''
    This script is used to analyze historical ML modeling results, including map, boxplot, and taylor diagram
'''
import os,glob,cmaps
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy as sp
import matplotlib as mpl
import hydroeval as he
import matplotlib.ticker as ticker
import scienceplots
from matplotlib.lines import Line2D
import cartopy.feature as cf
import geopandas as gpd
from pyogrio import read_dataframe
import cartopy.crs as ccrs
import cartopy
import warnings,sys
import seaborn as sns
import xarray as xr
from matplotlib.colors import LogNorm, Normalize
warnings.filterwarnings("ignore")

# set custom font (not a good idea)
import matplotlib.font_manager as font_manager
font_manager.fontManager.addfont(os.environ['DATA']+'/fonts/Helvetica/Helvetica.ttf')
font_manager.fontManager.addfont(os.environ['DATA']+'/fonts/Helvetica/Helvetica-Bold.ttf')

plt.style.use(['science','nature','no-latex']) # require install SciencePlots
plt.rcParams.update({"font.size":12, 'font.family':'Helvetica'}) 

def add_histogram(ax, vals, extent, vmin, vmax, vind, cmap):
    ax2 = ax.inset_axes(extent)
    cm = plt.cm.get_cmap(cmap)
    bins = [-np.inf] + np.arange(vmin, vmax+vind, vind).tolist() + [np.inf]
    n, bins, patches = ax2.hist(vals, bins=bins)
    bins[0] = vmin - vind
    bins[-1] = vmax + vind
    norm = mpl.colors.TwoSlopeNorm(vmin=vmin, vmax=vmax, vcenter=(vmin+vmax)/2)
    bin_center = 0.5 * (bins[:-1] + bins[1:])
    for c, p in zip(bin_center, patches):
        plt.setp(p, 'facecolor', cm(norm(c)))
    ax2.get_yaxis().set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.tick_params(top=False, which='both', labelsize  = 8)
    ax2.xaxis.label.set_visible(False)
    ax2.xaxis.set_minor_locator(ticker.NullLocator())
    ax2.spines['left'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.axvline(x=0,color='k')

def plot_map(ax, lons, lats, vals, vmin, vmax, cmap, title, label, norm = None):
    ax.set_global()
    ax.set_ylim([-6525154.6651, 8625154.6651]) 
    ax.set_xlim([-12662826, 15924484]) 
    ax.spines['geo'].set_linewidth(0)
    ax.coastlines(linewidth = .2, color = '#707070')
    ax.add_feature(cf.BORDERS, linewidth = .2, color = '#707070')
    # plot scatter setting
    if norm is None:
        norm = mpl.colors.Normalize(vmin = vmin, vmax = vmax)
    # add colorbar
    ras = ax.scatter(lons, lats, c = vals, norm = norm, cmap = cmap, s = 2, transform = ccrs.PlateCarree(), zorder = 3, linewidths = 0)
    ax2 = ax.inset_axes([.35, .03, 0.25, .03])
    fig.colorbar(ras, cax = ax2, orientation = 'horizontal', extend = 'both')
    ax2.tick_params(labelsize = 9)
    ax2.set_title(label, size = 9, pad = 2)
    # add histograms
    add_histogram(ax, vals, [.62, .2, .15, .2], vmin, vmax, 10, cmap)
    # set plot title
    ax.set_title(title, size = 10, pad = 7)
    return ax

def plot_scatter(ax, x, y, climate, xlabel, ylabel, metrics = True, log = True, normColor = 'log', addDiagnol = True, palette = None):
    ax1 = ax
    df0 = pd.DataFrame({'x':x, 'y':y,'climate':climate})
    # y vs y_pred
    if normColor == 'log':
        g = sns.scatterplot(data = df0, x = 'x', y = 'y', hue = 'climate', ax = ax1, hue_norm = LogNorm(), palette = palette)
    else:
        g = sns.scatterplot(data = df0, x = 'x', y = 'y', hue = 'climate', ax = ax1, palette = palette)
    g.legend_.set_title(None)
    sns.move_legend(ax1, "lower right", handlelength = 1, handletextpad = .4, labelspacing = .3, bbox_to_anchor = (.99, .01), borderpad = 0)
    max0 = max(np.nanmax(x[~np.isinf(x)]), np.nanmax(y))
    min0 = min(np.nanmin(x[~np.isinf(x)]), np.nanmin(y))
    if addDiagnol:
        ax1.axline((min0,min0), (max0,max0), color = 'k', ls = (0,(5,5)))
    if log:
        ax1.set_yscale('log')
        ax1.set_xscale('log')
    ax1.set_xlim(min0*0.95, max0*1.05)
    ax1.set_ylim(min0*0.95, max0*1.05)
    ax1.set_aspect('equal', adjustable='box')
    ax1.set_xlabel(xlabel, fontsize = 9)
    ax1.set_ylabel(ylabel, fontsize = 9)
    if metrics:
        kge, r, beta, alpha = he.evaluator(he.kge, y, x).squeeze()
        nse0 = he.evaluator(he.nse, y, x)[0]
        nRMSE = np.sum((y-x)**2) / np.sum(x**2) * 100
        ax1.text(.05, .95, '{:5s} = {:1.0f}%\n{:5s} = {:1.2f}\n{:5s} = {:1.2f}\n{:5s} = {:1.2f}\n{:5s} = {:1.2f}\n{:5s} = {:1.2f}'.format(
                'nRMSE', nRMSE, 'NSE', nse0, 'KGE', kge, 'r', r, '$\\mathregular{\\beta}$', beta, '$\\mathregular{\\alpha}$', alpha), 
                linespacing = 1.5, 
                transform=ax1.transAxes, 
                size = 6, 
                va = 'top', 
                ha = 'left')
    return ax1

def taylor(fig, ax, df_pred, obs, std_max = 2, legend = False):
    if ax.name != 'polar':
        raise Exception('ax must be a polar axes')

    # Reference std
    stdref = np.std(obs)

    # Samples std,rho,name
    std_list = df_pred.std(axis=0)
    rho_list = np.array([np.corrcoef(df_pred.iloc[:,a].values, obs)[0,1] for a in range(df_pred.shape[1])])
    name_list = df_pred.columns.tolist()
    samples = pd.DataFrame({'a':std_list,'b':rho_list,'name':name_list}).values

    # Create inset axes locator with polar projection
    tmax = np.pi / 2
    ax.set_thetamin(0)
    ax.set_thetamax(tmax / np.pi * 180)

    # add contour of rms
    for i in [0.5, 1]:
        theta0 = np.linspace(0, np.pi)
        radius0 = np.sqrt(1**2 + i**2 - 2 * i * 1 * np.cos(theta0))
        theta1 = np.arccos((radius0 ** 2 + 1**2 - i**2)/(2*1*radius0))
        ax.plot(theta1, radius0, color='k', linewidth=0.2)
        
    # set rlim
    ax.set_rlim([0, std_max])

    # set theta ticks
    major_rlocs = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 1])
    minor_rlocs = np.array([0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.91,0.92,0.93,0.94,0.96,0.97,0.98])
    major_tlocs = np.arccos(major_rlocs)        # Conversion to polar angles
    minor_tlocs = np.arccos(minor_rlocs)
    # set major theta ticks
    ax.set_xticks(major_tlocs)
    ax.set_xticklabels(major_rlocs)

    # set major and minor theta ticklines
    major_tick_length = .06
    minor_tick_length = .03
    for name in ['major', 'minor']:
        if name == 'major':
            l = major_tlocs
            length = major_tick_length
        else:
            l = minor_tlocs
            length = minor_tick_length
        for l0 in l:
            x1, y1 = l0, std_max-length
            x2, y2 = l0, std_max
            minor_tick = Line2D((x1, x2), (y1, y2), color='k', linewidth=0.3)
            ax.add_line(minor_tick)

    # add radius ticks
    rticks = np.arange(0, std_max, 0.5)
    ax.set_rticks(rticks)

    # add grid line
    ax.grid(False)
    ax.add_line(Line2D((np.arccos(0.6), np.arccos(0.6)), (0, std_max), color='k', linewidth=0.2, ls = ':'))
    ax.add_line(Line2D((np.arccos(0.9), np.arccos(0.9)), (0, std_max), color='k', linewidth=0.2, ls = ':'))
    ax.plot(np.linspace(0, np.pi/2), np.repeat(1.5, np.linspace(0, np.pi/2).shape[0]), color = 'k', linewidth = 0.2, ls = (0, (5, 8)))
    ax.plot(np.linspace(0, np.pi/2), np.repeat(1, np.linspace(0, np.pi/2).shape[0]), color = 'k', linewidth = 0.2, ls = (0, (5, 8)))
    ax.plot(np.linspace(0, np.pi/2), np.repeat(0.5, np.linspace(0, np.pi/2).shape[0]), color = 'k', linewidth = 0.2, ls = (0, (5, 8)))

    # add REF point
    ax.plot(0, 1, 'k*', ls='', ms=6, zorder = 10)

    # add samples
    theta = np.arccos(rho_list)
    radius = std_list / stdref
    markers = np.random.choice(['*','.','p','D','>','<','8','s','P','X','o'], len(theta))
    for i,name in enumerate(df_pred.columns):
        if name != 'MME':
            ax.scatter(theta[i], radius[i], marker = markers[i], label = df_pred.columns[i], zorder = 8)
    idx = df_pred.columns.tolist().index('MME')
    ax.scatter(theta[idx], radius[idx], marker = markers[idx], label = 'MME', zorder = 8, color = 'red')
    
    # add legend
    if legend == True:
        ax.legend(loc = 'center left', bbox_to_anchor = (-.02, .47), fontsize = 4, markerscale = 0.6, labelspacing = .3)

# read file for spatial evaluation
fnames = glob.glob('../results/*/cv_scatter_[A-Z]*[A-Za-z0-9]_r1i1*_log_500km2_addLonLat.csv')
fnames = [a for a in fnames if 'AGRF' not in a]
df_all = [pd.read_csv(fname).assign(model=os.path.basename(fname).split('_')[2]) for fname in fnames]
df_all0 = pd.concat(df_all)

# read file for temporal evaluation
fnames = glob.glob('../results/*/train_test*addLonLat_2001-2014*csv')
df_all = [pd.read_csv(fname).assign(model=os.path.basename(fname).split('_')[2]) for fname in fnames]
df_all1 = pd.concat(df_all)

# calculate MME bias
df_all_mme0 = df_all0.groupby(['lon','lat','ohdb_id']).agg({'RL10':'mean','pred':'mean','climate':lambda x: x.iloc[0]}).reset_index()
df_all_mme0['model'] = 'MME'
df_all_mme0['bias'] = (df_all_mme0['pred'] - df_all_mme0['RL10']) / df_all_mme0['RL10'].values * 100
df_all_mme0.to_csv('temp.csv', index = False)

df_all_mme1 = df_all1.groupby(['lon','lat','ohdb_id']).agg({'RL10':'mean','pred':'mean','climate':lambda x: x.iloc[0]}).reset_index()
df_all_mme1['model'] = 'MME'
df_all_mme1['bias'] = (df_all_mme1['pred'] - df_all_mme1['RL10']) / df_all_mme1['RL10'].values * 100
df_all_mme1.to_csv('temp.csv', index = False)
print(df_all_mme1.loc[df_all_mme1.bias.abs()<=50,:].shape[0]/df_all_mme1.shape[0])

# summarize for several river basins: Amazon, Mississippi, Yangtze, Danube, Murray, Orange Rivers
gdf_tmp = read_dataframe('../data/geography/BasinATLAS_v10_lev03_six_select.gpkg')
tmp = gpd.GeoDataFrame(data = df_all_mme1, geometry = gpd.points_from_xy(df_all_mme1.lon.values, df_all_mme1.lat.values))
join = gpd.sjoin(tmp, gdf_tmp)
df_basin_kge = join.groupby('basin_name').apply(lambda x: he.kge(x.pred.values, x.RL10.values)[0][0]).reset_index()
df_basin_kge.columns = ['basin_name','KGE']
print(df_basin_kge)

# merge
df_all = pd.concat([df_all0[['lon','lat','ohdb_id','climate','RL10','pred','model']], df_all_mme0])
df_all['bias'] = (df_all['pred'] - df_all['RL10']) / df_all['RL10'].values * 100

# plot 1: map
fig, ax1 = plt.subplots(figsize=(8, 6), subplot_kw={'projection':ccrs.EqualEarth()})
lons = df_all_mme1.lon.values
lats = df_all_mme1.lat.values
vals = df_all_mme1['bias'].values
vmin, vmax = -100, 100
cmap = plt.cm.RdBu_r
norm = mpl.colors.Normalize(vmin = vmin, vmax = vmax)
title = None
label = 'Relative bias (%)'
plot_map(ax1, lons, lats, vals, vmin, vmax, cmap, title, label, norm = norm)

# add climate background
ds0 = xr.open_dataset('../data/koppen_5class_0p5.nc')
cmaplist = ['#F8D347','#C7B18A',"#65C2A5","#A89BAF", "#B3B3B3"]
cmap0 = mpl.colors.LinearSegmentedColormap.from_list(
    'Custom cmap', cmaplist, 5)
bounds = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5]
norm0 = mpl.colors.BoundaryNorm(bounds, 5)
ds0.koppen.plot(ax = ax1, transform = ccrs.PlateCarree(),
                norm = norm0, cmap = cmap0, zorder = 0, alpha = .7,
                add_colorbar = False, rasterized = True)
ds0.close()

# add inset bar plot to show KGE for selected basins
ax_inset = ax1.inset_axes([.07, .1, .09, .2])
sns.barplot(data = df_basin_kge, x = 'KGE', y = 'basin_name', ax = ax_inset, width = .5)
ax_inset.set_ylabel(None)
ax_inset.set_xlabel('KGE', fontsize = 7)
ax_inset.spines['right'].set_visible(False)
ax_inset.tick_params(top = False, right = False, which='both', labelsize  = 7)
ax_inset.xaxis.set_minor_locator(ticker.NullLocator())
ax_inset.yaxis.set_minor_locator(ticker.NullLocator())
ax_inset.spines['top'].set_visible(False)
ax_inset.spines['right'].set_visible(False)

# plot 3: boxplot
ax3 = ax1.inset_axes([0.07, -.6, .25, .5])
sns.boxplot(data = df_all_mme1, x = 'climate', y = 'bias', showfliers = False, hue = 'climate', ax = ax3, width = .5, palette = cmaplist)
ax3.set_ylabel('Relative bias (%)', fontsize = 9)
ax3.set_xlabel('Climate zone', fontsize = 9)
ax3.set_ylim(None, ax3.get_ylim()[-1]*1.2)
ax3.xaxis.set_minor_locator(ticker.NullLocator())
# add number of samples
num = df_all_mme1.groupby('climate').count().lon
for text0 in ax3.get_xticklabels():
    x, y = text0.get_position()
    label0 = text0.get_text()
    num0 = num.loc[label0]
    y0 = ax3.get_ylim()[-1] * .9
    ax3.text(x, y0, num0, color = '#707070', weight = 'bold', fontsize = 7, va = 'top', ha = 'center')
print(df_all_mme1.groupby('climate')['bias'].mean())

# plot 2: scatter
ax2 = ax3.inset_axes([1.34, 0, 1.1, 1])
x = df_all_mme0.RL10.values
y = df_all_mme0['pred'].values
climate = df_all_mme0.climate.values
xlabel = 'Observed 10yr flood ($\mathregular{m^3/s}$)'
ylabel = 'Predicted 10yr flood ($\mathregular{m^3/s}$)'
cmaplist = ["#A89BAF", "#65C2A5", '#C7B18A', '#F8D347']
plot_scatter(ax2, x, y, climate, xlabel, ylabel, metrics = True, log = True, normColor = 'norm', addDiagnol = True, palette = cmaplist)

# plot 3: taylor diagram
ax4 = ax2.inset_axes([1.2, 0, 1, 1], projection = 'polar')
df_all1 = pd.concat([df_all0[['model','ohdb_id','pred']], df_all_mme0[['model','ohdb_id','pred']]])
obs = df_all0[['model','ohdb_id','RL10']].pivot_table(index='ohdb_id',columns='model',values = 'RL10').iloc[:,0].values
df_pred = df_all1.pivot_table(index='ohdb_id',columns='model',values = 'pred')
taylor(fig, ax4, df_pred, obs, legend = True)
ax4.set_xlabel('SD (normalized)', fontsize = 9, labelpad = 10)
ax4.text(np.pi/4, 2.1, 'Correlation', ha = 'left', fontsize = 9, rotation = -45)

fig.text(0.13, 0.75, 'a', weight = 'bold', size = 12, va = 'center', ha = 'center')
fig.text(0.13, 0.185, 'b', weight = 'bold', size = 12, va = 'center', ha = 'center')
fig.text(0.4, 0.185, 'c', weight = 'bold', size = 12, va = 'center', ha = 'center')
fig.text(0.67, 0.185, 'd', weight = 'bold', size = 12, va = 'center', ha = 'center')

fig.savefig('../picture/fig1.png', dpi = 600, transparent=False)