# -*- coding: utf-8 -*-
"""
Created on Thu Jun 18 16:48:51 2026

@author: chuffard
"""

# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 16:07:51 2024

@author: chuffard
"""

#SES 2024 MARS data
# -*- coding: utf-8 -*-
"""
Created on Mon May 13 13:39:04 2024

@author: chuffard
"""
# libraries
import seaborn as sns
import matplotlib.pyplot as pl
import math
import pandas as pd
from shapely.geometry import Polygon
import datetime
import seaborn as sns
import statistics
import matplotlib.pyplot as plt
import os
import re
from scipy.cluster.hierarchy import dendrogram, linkage
import plotly.figure_factory as ff
import pandas as pd
import numpy as np
import scipy.cluster.hierarchy
from scipy.cluster.hierarchy import ward, dendrogram, leaves_list
from scipy.spatial.distance import pdist
import numpy.ma as ma
from scipy import stats
from scipy.stats import zscore
from numpy.lib.stride_tricks import as_strided
import scipy.stats
#from numpy.lib import pad
#import tanglegram as tg
import warnings
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates

def letter_annotation(ax, xoffset, yoffset, letter):
 ax.text(xoffset, yoffset, letter, transform=ax.transAxes,
         size=12, weight='bold')
 




#import attenuance

SES_RAW_sorted=pd.read_csv("W:/Analyses/2024 MARS deployment/old not red atn/SES_RAW_sorted.csv", parse_dates=True)
SES_RAW_sorted["date_time"] = pd.to_datetime(SES_RAW_sorted["datetime"])

# SES_RAW_sorted = pd.read_csv("G:/Analyses/2024 MARS deployment/SES_RAW_sorted.csv", parse_dates=True)

# SES_RAW_sorted["date_time"] = pd.to_datetime(SES_RAW_sorted["datetime"])
SES_RAW_sorted = SES_RAW_sorted.drop('datetime', axis=1)

SES_RAW_sorted.plot(x='date_time', y='atn')

SES_RAW_sorted = SES_RAW_sorted[(SES_RAW_sorted['date_time'] <= '2024-07-10')]

SES_RAW_sorted['rm12'] = SES_RAW_sorted['atn'].rolling(12, min_periods=3).mean()
SES_RAW_sorted['stdev12'] = SES_RAW_sorted['atn'].rolling(12, min_periods=3).std()

                    
SES_RAW_sorted.columns
# Index(['Unnamed: 0', 'file_name', 'atn', 'datetemp', 'collect_time', 'YMD',
#        'YMD2', 'year', 'month', 'day', 'HMS', 'hour', 'minutes', 'second',
#        'rm150', 'Slope_rm_150', 'trigger150slope', 'Count', 'Count635',
#        'date_time', 'rm12', 'stdev12'],
#       dtype='object')
#drop inf values
SES_RAW_sorted = SES_RAW_sorted.replace([np.inf, -np.inf], np.nan).dropna(subset=['atn'])
#filter out periods with 0 collect time
SES_RAW_sorted =SES_RAW_sorted[SES_RAW_sorted['file_name'].str.contains('5min_Ext')]


#SES_RAW_sorted['rm150'] = SES_RAW_sorted['atn'].rolling(150, min_periods=23).mean()

# McLaneData = pd.read_excel('G:/Deployments/MARS-2024/Trigger trap/Carbon_data/Trigger_trap_filter_log.xlsx', sheet_name='Cups')
# McLaneData['date_time'] = pd.to_datetime(McLaneData['datetime'])
# McLaneData = McLaneData.drop('datetime', axis=1)

 
#High triggers

HighTriggers = SES_RAW_sorted[SES_RAW_sorted['trigger150slope'] == 'TriggerSlope']
PreImages = SES_RAW_sorted[SES_RAW_sorted['Illustrator'] == 'Y']
#DailyAvg

#_____________________________________PLOTS__________________________________________

#____________________________________TIME SERIES PANEL


# instanciate figure and axes instances
fig,axes = plt.subplots(2,1, figsize=(8,11), dpi=300)
params = {'mathtext.default': 'regular' } 

# first plot
axes[0].errorbar(SES_RAW_sorted['date_time'], SES_RAW_sorted['rm12'], yerr=SES_RAW_sorted['stdev12'], capsize=0, ecolor='lightblue', alpha = 0.5)
#sns.scatterplot(data=SES_RAW_trim, x="date_time", y = 'atn',color = 'black',ax=axes[0], alpha = 0.25)
sns.lineplot(data=SES_RAW_sorted, x="date_time", y = 'rm12',color = 'red',ax=axes[0]) 
sns.scatterplot(data=HighTriggers, x="date_time", y = 'atn', color = 'red',ax=axes[0],s=70,marker='s') 
sns.scatterplot(data=PreImages, x="date_time", y = 'atn', color = 'black',ax=axes[0],s=70,marker='s') 
axes[0].set_xlabel("")
axes[0].set_ylabel("Attenuance ")
axes[0].tick_params(axis='x', labelrotation=45)
plt.savefig("my_plot.pdf", bbox_inches="tight")
#sns.scatterplot(data=Triggers, x="date_time", y = 'atn', color = 'red',ax=axes[0],s=50)  #first position

