
lowtrigger = 0.007
hightrigger = 0.011

#See if it passes the low trigger
StationMcup["rm150Mass"] = np.where(StationMcup['Slope_rm_150'] >= lowtrigger, 'LowTrigger', 'WaitSlope')

#find the rolling rate low triggers in past 635 samples.
StationMcup['low_trigger_rate'] = (StationMcup['rm150Mass'] == 'LowTrigger').rolling(953, min_periods=2).sum()


#Line to help keep tabs on whether the switch has been flipped and we're now using the high trigger
StationMcup['max_rate'] = StationMcup['low_trigger_rate'].cummax()

try:
    # Ensure 'max_rate' column is not empty and the values are valid
    if 'max_rate' in StationMcup.columns and not StationMcup['max_rate'].isnull().all():
        # Apply condition element-wise to the 'max_rate' column
        StationMcup["Fire_trigger"] = np.where(
            StationMcup['max_rate'].abs() > 3,
            np.where(StationMcup['Slope_rm_150'] >= hightrigger, 'Fire', 'Wait'),
            np.where(StationMcup['Slope_rm_150'] >= lowtrigger, 'Fire', 'Wait')
        )
    else:
        print("The 'max_rate' column is missing or contains all NaN values.")
except Exception as e:
    print(f"An error occurred: {e}")


StationMcup.to_csv("//atlas/ProjectLibrary/902305_Event_Detection_with_SES/Deployments/MARS2024B-2025/SES_dataprep.csv")


