# -*- coding: utf-8 -*-
"""
Created on Wed Mar 18 14:51:26 2026

@author: chuffard
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 25 10:03:16 2026

@author: chuffard
"""

"""
Created on Thu Feb 19 15:29:05 2026

@author: chuffard
"""

#Station M fluorescence
from scipy import stats
import numpy as np
import seaborn as sns
import os
import glob
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from datetime import datetime
import sys
import os.path
import warnings

path = '//Thalassa/ProjectLibrary/902305_Event_Detection_with_SES/Analyses/Station M data'

# Get all matching file paths as a list
StationM_fluoro_files = glob.glob(path+'/*fluorom*')

Station_M_fluoro = pd.concat((pd.read_csv(f) for f in StationM_fluoro_files), ignore_index=True)


Station_M_fluoro['Fluorescense_690'] = Station_M_fluoro['Fluorescense 690 Counts']-Station_M_fluoro['Measure Ref 690 Counts']
Station_M_fluoro['Fluorescense_590'] = Station_M_fluoro['Fluorescense 590 Counts']-Station_M_fluoro['Measure Ref 590 Counts']


Station_M_fluoro= Station_M_fluoro[['Pulse', 'SES Timestamp (ISO601)','Fluorescense_690','Fluorescense_590']]

Station_M_fluoro["DateTime"] = pd.to_datetime(Station_M_fluoro['SES Timestamp (ISO601)'])

#%%

#### 
Station_M_fluoro = Station_M_fluoro.sort_values(by='DateTime')

Station_M_fluoro['rm150Fluorescense_690'] = Station_M_fluoro['Fluorescense_690'].rolling(150, min_periods=23).mean()
Station_M_fluoro['Slope_rm_150Fluorescense_690'] = Station_M_fluoro['rm150Fluorescense_690'].diff()

positive_Slope_rm_150Fluorescense_690 =  Station_M_fluoro[Station_M_fluoro['Slope_rm_150Fluorescense_690'] > 0]

percentiles_991 = positive_Slope_rm_150Fluorescense_690.groupby('Pulse')['Slope_rm_150Fluorescense_690'].quantile(0.991)
percentiles_971 = positive_Slope_rm_150Fluorescense_690.groupby('Pulse')['Slope_rm_150Fluorescense_690'].quantile(0.971)

print(percentiles_991)

# 60    4.335987
# 61    0.716233
# 62    2.082307
# 64    1.593333
# 66    2.586100
# 68    0.632100
# 70    0.777000
# 71    0.427667

print(percentiles_971)
# 60    3.159523
# 61    0.380000
# 62    1.578007
# 64    1.283833
# 66    1.609640
# 68    0.435800
# 70    0.500000
# 71    0.246667

#Full dataset percentiles
percentilefull_999 = positive_Slope_rm_150Fluorescense_690['Slope_rm_150Fluorescense_690'].quantile(0.999)
percentilefull_995 = positive_Slope_rm_150Fluorescense_690['Slope_rm_150Fluorescense_690'].quantile(0.995)
print(percentilefull_995)# np.float64(2.331500000000036)
percentilefull_991 = positive_Slope_rm_150Fluorescense_690['Slope_rm_150Fluorescense_690'].quantile(0.991)
percentilefull_971 = positive_Slope_rm_150Fluorescense_690['Slope_rm_150Fluorescense_690'].quantile(0.971)
print(percentilefull_999)#3.2669333333333848
print(percentilefull_995)#2.2594999999999863
print(percentilefull_991)#1.7439333333333298
print(percentilefull_971)#1.0492999999999975


#There's one datapoint with a bad timestamp, shows as in the 1970's. delete it
Station_M_fluoro = Station_M_fluoro[(Station_M_fluoro['DateTime'] >= '2011-01-01')]
                 
                 
numeric_colsfluoro = Station_M_fluoro.select_dtypes(include=['number']).columns
datetime_column_name = Station_M_fluoro['DateTime']

# 3. Save plots to a multi-page PDF
with PdfPages('all_sensor_plotsStation M fluoro.pdf') as pdf:
    for col in numeric_colsfluoro:
        plt.figure(figsize=(10, 6)) # Create a new figure
        plt.scatter(datetime_column_name, Station_M_fluoro[col], label=col, alpha = 0.3)
        plt.title(f'Line Plot of {col}')
   #     plt.xlabel('DateTime_Hour')
        plt.ylabel('Value')
        plt.legend()
        plt.xticks(rotation=45) 
        plt.grid(True)
        # Save the current figure to the PDF
        pdf.savefig()
        plt.close() # Close to free up memory

#Now add triggering
Station_M_fluoro["trigger150slopefluoro99"] = np.where(Station_M_fluoro['Slope_rm_150Fluorescense_690'] >= percentilefull_995, 'TriggerSlope', 'WaitSlope')





#

#%%
# 1. Create a figure with 1 row and 3 columns
fig, axes = plt.subplots(2, 1, figsize=(10, 10))

# 2. Plot on the first axis (axes[0])
sns.scatterplot(data=Station_M_fluoro, x='DateTime', y='Slope_rm_150Fluorescense_690', hue='trigger150slopefluoro99', ax=axes[0], alpha = 0.3)
axes[0].set_title('99')

# 3. Plot on the second axis (axes[1])
sns.scatterplot(data=Station_M_fluoro, x='DateTime', y='rm150Fluorescense_690', hue='trigger150slopefluoro99', ax=axes[1], alpha = 0.3)
axes[1].set_title('99')


# Adjust layout to prevent overlap
plt.tight_layout()
plt.show()

fig.savefig(path+'/stationMRM150690triggers99.pdf', dpi=300, bbox_inches='tight')
