# -*- coding: utf-8 -*-
"""
Script to comapre EFDC boundry conditions against observations
"""

import noaa_coops as noaa
from solarpy import beam_irradiance, irradiance_on_plane
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
gp.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

import scipy as sp
import scipy.ndimage
#from astropy.convolution import Gaussian2DKernel
import contextily as ctx
import os
from shapely.geometry import Point

import pickle

# %%  Import NOAA CO-OPS data at the battery
theBattery = noaa.Station(8518750)

df_air_temperature = theBattery.get_data(
     begin_date="20120101",
     end_date="20121231",
     product="air_temperature",
     units="metric",
     time_zone="gmt")

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


df_water_temperature = theBattery.get_data(
     begin_date="20120101",
     end_date="20121231",
     product="water_temperature",
     units="metric",
     time_zone="gmt")

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

# %% Read in EFDC met bounds
# WEATHER DATA FOR TIME SERIES N
# TASER(M,N) = TIME WHEN THE DATA WAS COLLECTED, IT IS IN DAY IF TCASER(N) IS 86400.
# PATM(M,N) = ATMOSPHERIC [PRESSURE MILLIBAR]
# TDRY(M,N) = DRY BULB ATMOSPHERIC TEMPERATURE [DEGREE C) ISTOPT(2)=1 OR EQUIL TEMP
# ISTOPT(2)=2; AQ VERSION SETS ISTOPT(2)=1.
# TWET(M,N) = WET BULB ATM TEMP IF IRELH=0, RELATIVE HUMIDITY (BETWEEN 0 AND 1) IF
# IRELH=1
# RAIN(M,N) = RAIN FALL RATE LENGTH/TIME, IT IS INCH/DAY IF RAINCVT IS SET TO 7.055560e-006
# (=0.0254/3600.)
# EVAP(M,N) = EVAPORATION RATE IF EVAPCVT>0.
# SOLSWR(M,N) = SOLAR SHORT WAVE RADIATION AT WATER SURFACE ENERGY FLUX/UNIT AREA.
# IT IS IN W/M2 IF SOLRCVT = 1.
# CLOUD(M,N) = FRACTIONAL CLOUD COVER
efdc_air_in = pd.read_csv('//ott-athena/E/INPUT_FILES/BC_WEATHER/20191206/files/NC_bc_weather_2012_191206.inp',
                      delim_whitespace=True, skiprows=10,
                      names=['TASER', 'PATM', 'TDRY', 'TWET', 'RAIN', 'EVAP', 'SOLSWR', 'CLOUD'])

# Remove final row
efdc_air_in.drop(efdc_air_in.tail(1).index, inplace=True)
# Correct type
efdc_air_in['TASER'] = efdc_air_in['TASER'].astype(float)

# Create datetime index
efdc_air_in['DateTime'] = pd.to_datetime(efdc_air_in['TASER'], unit='D', origin=pd.Timestamp('2012-01-01')).\
                            dt.tz_localize('EST').dt.tz_convert('UTC')

efdc_air_in.set_index('DateTime', inplace=True)


# %% Read in EFDC Water Temperature Bounds

efdc_water_in = pd.read_csv('//ott-athena/E/INPUT_FILES/TEMPERATURE_FILES/RM_North_RM_South/190423/NC_bc_temp_Yr2012_20190423.inp',
                      delim_whitespace=True, skiprows=16,
                      names=['TASER', 'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10'])

# Find row when the next boundry is reached and create new df
efdc_water_South = efdc_water_in.iloc[0:np.where(efdc_water_in['TASER'] == 'AQEA_QA_QC')[0][0], :].copy()
efdc_water_North = efdc_water_in.iloc[np.where(efdc_water_in['TASER'] == 'AQEA_QA_QC')[0][0]+2:
                                        np.where(efdc_water_in['TASER'] == 'AQEA_QA_QC')[0][1], :].copy()

# Correct type
efdc_water_South.iloc[:, 0] = efdc_water_South.iloc[:, 0].astype(float)
efdc_water_North.iloc[:, 0] = efdc_water_North.iloc[:, 0].astype(float)

# Create datetime index
efdc_water_South['DateTime'] = pd.to_datetime(efdc_water_South['TASER'], unit='D', origin=pd.Timestamp('2012-01-01')).\
                                        dt.tz_localize('EST').dt.tz_convert('UTC')

efdc_water_South.set_index('DateTime', inplace=True)

efdc_water_North['DateTime'] = pd.to_datetime(efdc_water_North['TASER'], unit='D', origin=pd.Timestamp('2012-01-01')).\
                                        dt.tz_localize('EST').dt.tz_convert('UTC')
efdc_water_North.set_index('DateTime', inplace=True)

del efdc_water_in

# %% Import Clear Sly radiation

vnorm = np.array([0, 0, -1])
h = 0  # sea-level
# date = np.arange(datetime(2012, 1, 1), datetime(2012, 12, 31), timedelta(hours=1)).astype(datetime)
solar_date = [datetime(2012, 1, 1) + timedelta(minutes=i) for i in range(0, 60 * 24 * 365, 60)]
solar_date_utc = pd.date_range(start=datetime(2012, 1, 1), end=datetime(2012, 12, 31), freq='1H').tz_localize('EST')

lat = 40.7128  # southern hemisphere

# solar_rad = [beam_irradiance(h, i, lat) for i in solar_date]
solar_rad = [irradiance_on_plane(vnorm, h, i, lat) for i in solar_date]


# %% Merge the dataframes

# met_merged = pd.merge(df_air_temperature, efdc_air_in, how='outer', left_index=True, right_index=True)
met_merged = pd.merge_ordered(df_air_temperature, efdc_air_in, on='DateTime',
                              how='outer', fill_method='ffill')

waterTempSouth_merged = pd.merge_ordered(df_water_temperature, efdc_water_South, on='DateTime',
                              how='outer', fill_method='ffill')

waterTempNorth_merged = pd.merge_ordered(df_water_temperature, efdc_water_North, on='DateTime',
                              how='outer', fill_method='ffill')


#%% Plot air temperature

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

# efdc_air_in['TDRY'].plot(ax=axes, color='black', label='EFDC Air Temperature')
# df_air_temperature['air_temp'].plot(ax=axes, color='blue', label='Obs at The Battery')
# axes.set_xlim(pd.to_datetime('2012-08-01'), pd.to_datetime('2012-08-31'))

# efdc_water_North['L10'].astype(float).plot(ax=axes, color='blue', label='EFDC North Boundry L10')
# efdc_water_North['L5'].astype(float).plot(ax=axes, color='red', label='EFDC North Boundry L5')
# efdc_water_North['L1'].astype(float).plot(ax=axes, color='green', label='EFDC North Boundry L1')

efdc_water_South['L10'].astype(float).plot(ax=axes, color='blue', label='EFDC South Boundry L10')
efdc_water_South['L5'].astype(float).plot(ax=axes, color='red', label='EFDC South Boundry L5')
efdc_water_South['L1'].astype(float).plot(ax=axes, color='green', label='EFDC South Boundry L1')

df_water_temperature['water_temp'].plot(ax=axes, color='black', label='Obs at The Battery')
axes.set_xlim(pd.to_datetime('2012-08-01'), pd.to_datetime('2012-08-31'))

axes.set_ylabel('Temperature (deg C)')
axes.set_xlabel('Date')
axes.legend()

# axes.scatter(met_merged['TDRY'], met_merged['air_temp'])
# axes.scatter(waterTempSouth_merged['water_temp'], waterTempSouth_merged['L5'])
# axes.scatter(waterTempNorth_merged['water_temp'], waterTempSouth_merged['L5'])

plt.show()

#%% Plot Solar Radiation

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

efdc_air_in['SOLSWR'].plot(ax=axes, color='black', label='EFDC Air Temperature')
axes.plot(solar_date_utc[0:-1], solar_rad, color='blue', label='Solar Radiation')

axes.set_xlim(pd.to_datetime('2012-08-01'), pd.to_datetime('2012-08-07'))

axes.set_ylabel('Solar Radiation (w/m^2)')
axes.set_xlabel('Date')
axes.legend()

plt.show()








