import pandas as pd
import matplotlib.pyplot as plt
import sys
from importlib import reload
sys.path.append('./')
import asotr
reload(asotr)
import matplotlib.pyplot as plt
from matplotlib import dates
import pandas as pd
from datetime import datetime

asotr_kit = 1
fname = f'../../python_cyclo/data/asotr0{asotr_kit}_data_T.csv' 

dateparse = lambda x: datetime.strptime(x, "%d.%m.%Y %H:%M:%S.%f")
data = pd.read_csv(fname, sep=';', parse_dates=['timestamp'], date_parser=dateparse)

interp = {'method': 'polynomial', 'order': 2}
thermocycle_info = {'date': '14.03.2025', 'type': 'impuse'}
cut_step_resp = {'orig_time_step_begin': '14.03.2025 13:49:32', 'orig_step_duration': 20*60}
data_info = {'data': data, 'channel': 'ch1', 'period': '1 сек', 'find_accuracy': 'seconds'}

name = f'{thermocycle_info["type"]}_response_{thermocycle_info["date"].replace(".","")}'

plot_info = {'title': 'Реакция на импульсное воздействие', 
        'ox_dtime_format': "%H:%M:%S", 'legend_pos': ['upper right', 'lower left'],
        'name_fig': f'../plots/response/{name}.png', 
        'font': 10}

tstamp_orig_begin = cut_step_resp['orig_time_step_begin']

_, interp_imp_resp = asotr.cut_norm_data(data_info['data'], tstamp_orig_begin, 
        cut_step_resp['orig_step_duration'], channel='ch1', interp=interp, 
        accuracy=data_info['find_accuracy'])


interp_imp_resp.to_csv(f'../data/asotr/response/asotr0{asotr_kit}_{name}.csv', index=False, sep=';', 
        encoding='utf-8-sig', decimal='.')

asotr.plot_imp_response(interp_imp_resp, data_info, plot_info, thermocycle_info)
