# -*- coding: utf-8 -*-
"""
Script grab and process bound data for the Penobscot
"""

import noaa_coops as noaa
from datetime import datetime, timedelta

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import geopandas as gp

import dataretrieval.nwis as nwis

# %% Import USGS data from selected stations

# Bangor, Maine:
# Gage height from 2012-10-01 to 2022-06-23
site = '01037050'
# Get usgs data
Bangor_df = nwis.get_record(sites=site, service='iv', start='2013-01-01', end='2022-01-01')
# Convert downloaded data to pandas datetime
Bangor_df['pd_Datetime'] = pd.to_datetime(Bangor_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
# Convert datetime to UTC. Use apply since time zones may vary
Bangor_df['pd_Datetime'] = Bangor_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
# Set the converted datetimes as a datetimeindex
Bangor_df.set_index(pd.DatetimeIndex(Bangor_df['pd_Datetime']), inplace=True)

# West Enfield, Maine:
# Upstream from project
# Discharge from 1990-09-22 to 2022-06-23
# Gage from 2007-10-01 to 2022-06-23
site = '01034500'
Enfield_df = nwis.get_record(sites=site, service='iv', start='1990-09-22', end='2022-01-01',
                             parameterCd='00060,00065')
Enfield_df['pd_Datetime'] = pd.to_datetime(Enfield_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
Enfield_df['pd_Datetime'] = Enfield_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
Enfield_df.set_index(pd.DatetimeIndex(Enfield_df['pd_Datetime']), inplace=True)


# Eddington, Maine:
# Discharge from 1979-04-06 to 1996-09-30
# Gage from 1998-10-01 to 2022-06-22
site = '01036390'
Eddington_df = nwis.get_record(sites=site, service='iv', start='1990-09-22', end='2022-01-01',
                parameterCd='00060,00065')
Eddington_df['pd_Datetime'] = pd.to_datetime(Eddington_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
Eddington_df['pd_Datetime'] = Eddington_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
Eddington_df.set_index(pd.DatetimeIndex(Eddington_df['pd_Datetime']), inplace=True)


# Kenduskeag Stream, Maine:
# Discharge from 2010-02-28 to 2022-06-23
# Gage from 2009-12-22 to 2022-06-23
site = '01037000'
Kenduskeag_df = nwis.get_record(sites=site, service='iv', start='1990-09-22', end='2022-01-01',
                parameterCd='00060,00065')
Kenduskeag_df['pd_Datetime'] = pd.to_datetime(Kenduskeag_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
Kenduskeag_df['pd_Datetime'] = Kenduskeag_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
Kenduskeag_df.set_index(pd.DatetimeIndex(Kenduskeag_df['pd_Datetime']), inplace=True)


## Validation sites
# Winterport, Maine:
# Gage from 2017-09-01 to 2017-12-01
site = '443810068502201'
Winterport_df = nwis.get_record(sites=site, service='iv', start='2017-09-01', end='2017-12-01',
                                parameterCd='00065')
Winterport_df['pd_Datetime'] = pd.to_datetime(Winterport_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
Winterport_df['pd_Datetime'] = Winterport_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
Winterport_df.set_index(pd.DatetimeIndex(Winterport_df['pd_Datetime']), inplace=True)


# Bucksport, Maine:
# Gage from 2017-09-01 to 2017-12-01
site = '443409068471801'
Bucksport_df = nwis.get_record(sites=site, service='iv', start='2017-09-01', end='2017-12-01',
                               parameterCd='00065')
Bucksport_df['pd_Datetime'] = pd.to_datetime(Bucksport_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
Bucksport_df['pd_Datetime'] = Bucksport_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
Bucksport_df.set_index(pd.DatetimeIndex(Bucksport_df['pd_Datetime']), inplace=True)

# FortPoint, Maine:
# Gage from 2017-09-01 to 2017-12-01
site = '442810068480101'
FortPoint_df = nwis.get_record(sites=site, service='iv', start='2017-09-01', end='2017-12-01',
                               parameterCd='00065')
FortPoint_df['pd_Datetime'] = pd.to_datetime(FortPoint_df.index, format='%Y-%m-%dT%H:%M:%S.%f%z')
FortPoint_df['pd_Datetime'] = FortPoint_df['pd_Datetime'].apply(lambda x: x.tz_convert('UTC'))
FortPoint_df.set_index(pd.DatetimeIndex(FortPoint_df['pd_Datetime']), inplace=True)


# %% Approach

# Corrleate upstream discharge at West Enfield with discharge at project site in Eddington, Maine
# from 1990-09-22 (start of Enfield) to 1996-09-30 (end of Eddington)
# Try scaling factor, othersie use gague

# Generate estimated discharge at Eddington from 2017-09-01 to 2017-12-01 using scaling factor

# Add in flow from Kenduskeag Stream to get discharge at Bangor

# Read in NOAA @ Bar Harbor from 2017-09-01 to 2017-12-01

# Compare NOAA @ Bar Harbor with USGS Fort Point

# Save Csv files of discharge at Eddington & WL @ Bar Harbor & WL @ Bangor

# Run model

# Compare against WL @ Winterport & Bucksport & Fort Point


# %%  Import NOAA CO-OPS data at Bar Harbor
barHarbor = noaa.Station(8413320)

barHarbor_df = barHarbor.get_data(
     begin_date="20170901",
     end_date="20171201",
     product="water_level",
     units="metric",
     datum="MSL",
     time_zone="gmt")

barHarbor_df['DateTime'] = barHarbor_df.index
barHarbor_df['DateTime'] = barHarbor_df['DateTime'].dt.tz_localize('UTC')


# %% Plot discharge at West Enfield with discharge at project site in Eddington, Maine
# from 1990-09-22 (start of Enfield) to 1996-09-30 (end of Eddington)

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6))

Enfield_df.loc['1990-9-22':'1996-9-30', '00060'].plot(ax=axes, label='Enfield', style='-')
Eddington_df.loc['1990-9-22':'1996-9-30', '00060'].plot(ax=axes, label='Eddington', style='-')

Enfield_df.shift(freq=pd.Timedelta(hours=+12)).loc['1990-9-22':'1996-9-30', '00060'].multiply(1.05).plot(
     kind='line', ax=axes, label='Enfield Shift')
axes.set_xlim(pd.to_datetime('1993-04-01'), pd.to_datetime('1993-12-01'))


# set formatter
axes.xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y'))
# set font and rotation for date tick labels
plt.gcf().autofmt_xdate()


axes.set_xlabel('Date')

# scatterDF = Enfield_df
# scatterDF['Eddington'] = np.interp(scatterDF.index, Eddington_df.index,
#                                    Eddington_df['00060'])
#
# scatterDF.plot.scatter(x='00060', y='Eddington', ax=axes)

axes.legend()

plt.show()


# %% Create discharge at Bangor by multiplying Eddington * 1.05 and shifting 12 hours + Kenduskeag
# From 2017-09-01 to 2017-12-01
# Create Delft3D .bc

Enfield_shift_df = Enfield_df.copy()
Enfield_shift_df["Enfield_shift"] = Enfield_shift_df.shift(freq=pd.Timedelta(hours=+12)).loc[:, '00060'].multiply(1.05)
Enfield_shift_df.rename(columns={'00060': "Enfield_Base", 'pd_Datetime':'pd_Datetime_Enfield'}, inplace=True)

Enfield_selShift_df    = Enfield_shift_df.loc['2017-9-1':'2017-12-1', :]
Kenduskeag_sel_df      = Kenduskeag_df.copy().loc['2017-9-1':'2017-12-1', :].rename(
                            columns={'00060': "Kenduskeag_Base", 'pd_Datetime':'pd_Datetime_Kenduskeag'})

syn_Bangor_df = pd.merge(Enfield_selShift_df, Kenduskeag_sel_df,
                         left_on = 'pd_Datetime_Enfield', right_on = 'pd_Datetime_Kenduskeag')

syn_Bangor_df['Enfield_shift_filna'] = syn_Bangor_df["Enfield_shift"].interpolate()
syn_Bangor_df['Kenduskeag_filna']    = syn_Bangor_df["Kenduskeag_Base"].interpolate()

syn_Bangor_df['syn_Bangor'] = syn_Bangor_df['Enfield_shift_filna'] + syn_Bangor_df["Kenduskeag_filna"]

# %% Test plot

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6))

syn_Bangor_df.loc[:, 'syn_Bangor'].plot()

plt.show()

# %% Save as csv

syn_Bangor_df['syn_Bangor_m3'] = syn_Bangor_df['syn_Bangor'] * 0.0283168

syn_Bangor_df.loc[:, ['pd_Datetime_Enfield', 'syn_Bangor_m3']].to_csv(
    'C:/Users/arey/files/Projects/Penobscot/Modelling/Inputs/syn_Bangor.csv', index=False)

barHarbor_df.loc[:, 'water_level'].to_csv(
    'C:/Users/arey/files/Projects/Penobscot/Modelling/Inputs/Bar_Harbour.csv')




