# -*- coding: utf-8 -*-
"""
Created on Wed Feb 25 17:05:42 2026

@author: chuffard
"""
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from datetime import datetime
import numpy as np
import pandas as pd
import sys
import os.path
import warnings
import glob
from pathlib import Path
#find 97.1 and 99.1th percentiles for fluorescence slope at MARS and also at Station Mprint(percentilefull_991)
#print(percentilefull_991)
#1.7495666666666636
#print(percentilefull_971)
# 1.0466666666666242
#from full Station M dataset

#now see how it is for the full MARS fluormenter dataset
# path = Path(r'//atlas/ProjectLibrary/902305_Event_Detection_with_SES/Deployments')

# # Set the directory to search ('.' for current directory)

# # Find all files with 'fluorometer' in the name recursively
# fluoro_files = list(path.rglob('*fluorometer*'))
# fluoro_filesdf = pd.DataFrame(fluoro_files)

path = '//atlas/ProjectLibrary/902305_Event_Detection_with_SES/Analyses/Explore/all_fluoro'

# #%% Run atn
# ##before deployment fix inf in atn code
# # Get all matching file paths as a list
# fluoro_files = glob.glob(path+'/*fluorom*')

# fluorescence = pd.concat((pd.read_csv(f) for f in fluoro_files), ignore_index=True)
# fluorescence["Datetime"] = pd.to_datetime(fluorescence["SES Timestamp (ISO601)"])
# fluorescence['Fluorescense_690'] = fluorescence['Fluorescense 690 Counts']-fluorescence['Measure Ref 690 Counts']
# fluorescence['Fluorescense_590'] = fluorescence['Fluorescense 590 Counts']-fluorescence['Measure Ref 590 Counts']
# fluorescence_sorted = fluorescence.sort_values(by='Datetime')
# fluorescence_sorted.drop_duplicates(inplace=True)
# fluorescence_sorted.to_csv(path+'/Full_MARS_fluoro.csv')


Full_MARS_fluoro = r"//atlas/ProjectLibrary/902305_Event_Detection_with_SES/Analyses/Explore/all_fluoro/Full_MARS_fluoro.csv"
Full_MARS_fluoro = pd.read_csv(Full_MARS_fluoro)
Full_MARS_fluoro.columns
# ndex(['Unnamed: 0', 'SES Timestamp (ISO601)', 'Dark Ref Integration Time',
#        'Dark Ref 690 Counts', 'Dark Ref 590 Counts',
#        'Measure Ref Integration Time', 'Measure Ref 690 Counts',
#        'Measure Ref 590 Counts', 'Luminescense Integration Time',
#        'Luminescense 690 Counts', 'Luminescense 590 Counts',
#        'Fluorescense Integration Time', 'Fluorescense 690 Counts',
#        'Fluorescense 590 Counts', 'Phosphorescense Integration Time',
#        'Phosphorescense 690 Counts', 'Phosphorescense 590 Counts',
#        'Constant Target Fluorescense Integration Time',
#        'Constant Target Fluorescense 690 Counts',
#        'Constant Target Fluorescense 590 Counts', 'Code',
#        'Fluorescence690DATA', 'Fluorescence590DATA', 'Unnamed: 21', 'Datetime',
#        'Fluorescense_690', 'Fluorescense_590'],
#       dtype='str')
Full_MARS_fluoro["DateTime"] = pd.to_datetime(Full_MARS_fluoro['SES Timestamp (ISO601)'])

Full_MARS_fluoro = Full_MARS_fluoro.sort_values(by='DateTime')
Full_MARS_fluoro = Full_MARS_fluoro[Full_MARS_fluoro['Fluorescense_690'] != 0]

Full_MARS_fluoro['rm150Fluorescense_690'] = Full_MARS_fluoro['Fluorescense_690'].rolling(150, min_periods=23).mean()
Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] = Full_MARS_fluoro['rm150Fluorescense_690'].diff()

positive_Full_MARS_fluoro =  Full_MARS_fluoro[Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] > 0]
percentiles_995 = positive_Full_MARS_fluoro['Slope_rm_150Fluorescense_690'].quantile(0.995)
percentiles_99MARS = positive_Full_MARS_fluoro['Slope_rm_150Fluorescense_690'].quantile(0.99)
#np.float64(1.4267333333333445)
#Station m #np.float64(1.686666666666619)
percentiles_991 = positive_Full_MARS_fluoro['Slope_rm_150Fluorescense_690'].quantile(0.991)
percentiles_971 = positive_Full_MARS_fluoro['Slope_rm_150Fluorescense_690'].quantile(0.971)

print(percentiles_99MARS)#(1.4267333333333445)
print(percentiles_995)#(2.5462000000000207)
print(percentiles_991)#1.9377066666666227
print(percentiles_971)#0.6666666666666479 
print("hello")
#Station M
# print(percentilefull_991)
# #1.7495666666666636
# print(percentilefull_971)
# # 1.0466666666666242
#print(percentilefull_995)# np.float64(2.331500000000036)
hightriggerFluoro= 2.4
lowtriggerFluoro= 1.8                
a = percentiles_995-percentiles_991
print(a)
numeric_colsfluoro = Full_MARS_fluoro.select_dtypes(include=['number']).columns

datetime_column_name = Full_MARS_fluoro['DateTime']

with PdfPages('all_sensor_plots MARS fluoro.pdf') as pdf:
    for col in numeric_colsfluoro:
        plt.figure(figsize=(10, 6)) # Create a new figure
        plt.scatter(datetime_column_name, Full_MARS_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

Full_MARS_fluoro["trigger150slopefluoro995"] = np.where(Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] >= percentiles_995, 'TriggerSlope', 'WaitSlope')
Full_MARS_fluoro["trigger150slopefluoro991"] = np.where(Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] >= percentiles_991, 'TriggerSlope', 'WaitSlope')
Full_MARS_fluoro["trigger150slopefluoro99"] = np.where(Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] >= percentiles_99MARS, 'TriggerSlope', 'WaitSlope')

Full_MARS_fluoro['test'] = np.where(Full_MARS_fluoro['Slope_rm_150Fluorescense_690'] >= 0, 'TriggerSlope', 'WaitSlope')

palette =["#3776ab", "#FF8C00"]
fig, axes = plt.subplots(3, 1, figsize=(8, 10), sharex=True)

# 3. Create the first scatterplot (Top)
fig, axes = plt.subplots(2, 1, figsize=(10, 10))

# 2. Plot on the first axis (axes[0])
sns.scatterplot(data=Full_MARS_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=Full_MARS_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+'/AllMarsFluoro.pdf', dpi=300, bbox_inches='tight')
