# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 15:47:46 2024

@author: chuffard
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 13 09:34:42 2023

@author: chuffard
"""

import numpy as np

import pandas as pd

#import spectrum #doesn't want to install...tried everything I can find

import matplotlib.pyplot as plt

import seaborn as sns


###THIS IS THE HOURLY TIME SERIES> SHOULD BE USING THE 5 min time series

# read in data; use first line as header Keep last 1153 samples of attenuance
Main_Hourly_SES_MARS = pd.read_csv('G:/Analyses/MainSES_fluoro_atn_moon_sun_current.csv', parse_dates=True, index_col="datetime")

#fix weird time zone stuff
for col in Main_Hourly_SES_MARS.select_dtypes(include=['datetime64[ns, UTC]']).columns:
    Main_Hourly_SES_MARS[col] = Main_Hourly_SES_MARS[col].apply(lambda x: x.tz_localize(None))


#make a new time series of this rolling mean. FOR THE SES- we'll need to keep the most recent 1153 rows and find the mean of that
Main_Hourly_SES_MARS['rolling_atn_1153'] = Main_Hourly_SES_MARS['atn'].rolling(1153, min_periods=200).mean()

#Now check the slope of this time series
#subset columns
SESMARS1153atn = Main_Hourly_SES_MARS[["atn", "rolling_atn_1153"]]

#find proxy for slope (percent change from one sample to next)
SESMARS1153atn['Percentchange'] = SESMARS1153atn['rolling_atn_1153'].astype(float).pct_change(1)

#subset to remove first 1153 rows (to eliminate artifacts of using rolling mean)  #THIS STEP WILL NO BE NEEDED IN THE FINAL CODE, since we'll only be looking at the most recent 1153 samples
SESslope1153TRIM = SESMARS1153atn.iloc[1153:, ]

#list which samples are trigger vs wait
SESslope1153TRIM["trigger80pc1153slope"] = np.where(SESslope1153TRIM['Percentchange'] >= 0.005446553834749502, 'TriggerSlope', 'WaitSlope')

sns.scatterplot(data=SESslope1153TRIM, x="datetime", y="atn", hue="trigger80pc1153slope")
plt.xticks(rotation=45);
plt.show()



