### Halifax Wind Speed Processing Code
# Alexander Rey, June 30, 2020

import pandas as pd
import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import statsmodels.api as sm

# %% Import Wind Speed CSV

# halifax_in = pd.read_csv("C:/Users/arey/files/Projects/St_John/2200233.csv",
#                          index_col='DATE', parse_dates=True, low_memory=False)
halifax_in = pd.read_csv("C:/Users/arey/files/Projects/St_John/2200284.csv",
                         index_col='DATE', parse_dates=True, low_memory=False)

halifax_in_split = halifax_in["WND"].str.split(",", n=5, expand=True)
halifax_in.drop(columns=["WND"], inplace=True)

halifax_in["WND0"] = halifax_in_split[0]
halifax_in["WND1"] = halifax_in_split[1]
halifax_in["WND2"] = halifax_in_split[2]
halifax_in["WND3"] = halifax_in_split[3]
halifax_in["WND4"] = halifax_in_split[4]

halifax_offshore = halifax_in[['WND0', 'WND3']].loc[halifax_in['STATION'] == 99539099999]
halifax_offshore['WND0'] = pd.to_numeric(halifax_offshore['WND0'])
halifax_offshore['WND3'] = pd.to_numeric(halifax_offshore['WND3'])
halifax_offshore = halifax_offshore[halifax_offshore.WND3 != 9999]
halifax_offshore = halifax_offshore[halifax_offshore.WND0 != 999]

halifax_offshore = halifax_offshore.reindex(pd.date_range(start='2002-01-01',
                                                      end='2008-12-31',
                                                      freq='1h'))
halifax_offshore['WND0'] = halifax_offshore['WND0'].interpolate(method='nearest',limit_area='inside',limit=24)
halifax_offshore['WND3'] = halifax_offshore['WND3'].interpolate(method='linear',limit_area='inside',limit=24)


# halifax_offshore = halifax_offshore.loc['2004-08-31':'2018-11-29']

halifax_jetty = halifax_in[['WND0', 'WND3']].loc[halifax_in['STATION'] == 71601599999] #    1601599999
halifax_jetty['WND0'] = pd.to_numeric(halifax_jetty['WND0'])
halifax_jetty['WND3'] = pd.to_numeric(halifax_jetty['WND3'])
halifax_jetty = halifax_jetty[halifax_jetty.WND3 != 9999]
halifax_jetty = halifax_jetty[halifax_jetty.WND0 != 999]
# halifax_mcnabs = halifax_mcnabs.loc['2004-08-31':'2018-11-29']

halifax_jetty = halifax_jetty.reindex(pd.date_range(start='2002-01-01',
                                                      end='2008-12-31',
                                                      freq='1h'))
halifax_jetty['WND0'] = halifax_jetty['WND0'].interpolate(method='nearest',limit_area='inside',limit=24)
halifax_jetty['WND3'] = halifax_jetty['WND3'].interpolate(method='linear',limit_area='inside',limit=24)

#% Ignore Missing in Either
offshore_null = halifax_offshore.WND0.isnull()
jetty_null = halifax_jetty.WND0.isnull()

halifax_jetty = halifax_jetty[(offshore_null==False) & (jetty_null==False)]
halifax_offshore = halifax_offshore[(offshore_null==False) & (jetty_null==False)]


# %% Make QQ plot
plt.figure()

# sm.qqplot_2samples(halifax_jetty['WND3'].loc[halifax_offshore['WND0'].isin([110, 170])]/10,
#                    halifax_offshore['WND3'].loc[halifax_offshore['WND0'].isin([110, 170])]/10,
#                    line='r',ylabel='McNabs Island Quantiles',xlabel='Offshore Quantiles')
sm.qqplot_2samples(halifax_jetty['WND3'].loc[halifax_offshore['WND0'].isin([110, 170])]/10,
                   halifax_offshore['WND3'].loc[halifax_offshore['WND0'].isin([110, 170])]/10,
                   line='r',ylabel='Shearwater Jetty Quantiles',xlabel='Offshore Quantiles')
# sm.qqplot_2samples(halifax_jetty['WND3'], halifax_offshore['WND3'], line='r', ylabel='Jetty Quantiles',xlabel='Offshore Quantiles')

plt.title('Halifax Wind (Shearwater Jetty) from 110-170 deg 2002-2008')
# plt.title('Halifax Wind (McNabs Island) from 110-170 deg 2002-2008')

plt.plot([0, 20], [0, 20], 'k-')

# plt.xlabel('Offshore Wind Speed (m/s)')
# plt.ylabel('Potash Terminal Wind Speed (m/s)')
# plt.title('St John Wind Speed Comparision July 2016-June 2020')
# plt.title('St John Wind Speed Comparision July 2016-June 2020, 90-180 deg')

# plt.xlim(0, 25)
# plt.ylim(0, 25)

# cmap = plt.get_cmap('hsv')
# norm = plt.Normalize(0, 360)
# color = cmap(norm(200.))
#
# cbar = plt.colorbar()
# cbar.ax.get_yaxis().labelpad = 15
# cbar.ax.set_ylabel('Offshore Wind Direction', rotation=270)

plt.savefig('Halifax_Jetty_110_170.png')
# plt.savefig('St_John_all.png')
plt.show()

