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'../data/asotr/asotr0{asotr_kit}_data_T.csv'
data = pd.read_csv(fname, sep=';', parse_dates=["timestamp"], date_format="%d.%m.%Y %H:%M:%S.%f")

# fname = f'../../python_cyclo/data/asotr0{asotr_kit}_data_T.csv'
# data = pd.read_csv(fname, sep=';', parse_dates=["timestamp"], date_format="%d.%m.%Y %H:%M:%S.%f")

# date = '20.03.2025'
# period = '1 мин'
# time_begin_orig = date + ' 17:10:11' 
# time_begin1 = date + ' 18:10:17'
# time_begin2 = date + ' 19:10:23'
# step_begin = time_begin2
# duration = 3600
# accuracy = 'seconds'
# name_fig = 'step_response_KDI_20242003.png'

# date = '21.03.2025'
# period = '1 мин'
# time_begin_orig = date + ' 14:00:11' 
# time_begin1 = date + ' 15:00:16'
# time_begin2 = date + ' 16:00:16'
# step_begin = time_begin2
# duration = 3600
# accuracy = 'seconds'
# name_fig = 'step_response_KDI_20242103.png'

# date = '24.03.2025'
# period = '1 сек'
# time_begin_orig = date + ' 19:45:11' 
# time_begin1 = date + ' 20:45:13'
# time_begin2 = date + ' 21:45:17'
# step_begin = time_begin2
# duration = 3600
# accuracy = 'seconds'
# name_fig = 'step_response_KDI_20242403.png'



# interp = {'method': 'polynomial', 'order': 1}

# data_info_list = []
# thermocycle_info_list = []
# cut_step_resp_list = []

# data_info = {'data': data, 'device': 'КДИ', 'channel': 'ch1', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '01.04.2025', 
#         'time_begin': ['01.04.2025 16:27:13', '01.04.2025 18:00:18'], 
#         'duration_sec': 92*60, 'type': 'step', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '01.04.2025 18:53:20', 'step_duration': 25*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)



interp = {'method': 'polynomial', 'order': 1}

data_info_list = []
thermocycle_info_list = []
cut_step_resp_list = []

data_info = {'data': data, 'device': 'летный', 'channel': 'ch1', 'period': '1 мин', 
        'find_accuracy': 'seconds'}
thermocycle_info = {'date': '25.04.2025', 
        'time_begin': ['24.04.2025 22:46:32', '25.04.2025 00:19:33'], 
        'duration_sec': 92*60, 'type': 'step', 'type_ru': 'ступенчатое'}
cut_step_resp = {'time_step_begin': '25.04.2025 01:18:01', 'step_duration': 30*60}

data_info_list.append(data_info)
thermocycle_info_list.append(thermocycle_info)
cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch2', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '25.04.2025', 
#         'time_begin': ['24.04.2025 22:46:32', '25.04.2025 00:19:33'], 
#         'duration_sec': 92*60, 'type': 'step1_to2', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '25.04.2025 01:18:01', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)


# data_info = {'data': data, 'device': 'летный', 'channel': 'ch1', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '25.04.2025', 
#         'time_begin': ['25.04.2025 01:52:34', '25.04.2025 03:25:34'], 
#         'duration_sec': 92*60, 'type': 'impulse', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '25.04.2025 04:24:00', 'step_duration': 15*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch2', 'period': '1 мин', 
#         'find_accuracy2: 'seconds'}
# thermocycle_info = {'date': '25.04.2025', 
#         'time_begin': ['25.04.2025 01:52:34', '25.04.2025 03:25:34'], 
#         'duration_sec': 92*60, 'type': 'impulse1_to2', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '25.04.2025 04:24:00', 'step_duration': 20*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)



# data_info = {'data': data, 'device': 'летный', 'channel': 'ch2', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '30.04.2025', 
#         'time_begin': ['29.04.2025 22:02:54', '29.04.2025 23:35:54'], 
#         'duration_sec': 93*60, 'type': 'step', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '30.04.2025 00:36:01', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch1', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '30.04.2025', 
#         'time_begin': ['29.04.2025 22:02:54', '29.04.2025 23:35:54'], 
#         'duration_sec': 93*60, 'type': 'step2_to1', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '30.04.2025 00:36:01', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch2', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '30.04.2025', 
#         'time_begin': ['30.04.2025 01:09:55', '30.04.2025 02:41:54'], 
#         'duration_sec': 93*60, 'type': 'impulse', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '30.04.2025 03:42:00', 'step_duration': 15*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch1', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '30.04.2025', 
#         'time_begin': ['30.04.2025 01:09:55', '30.04.2025 02:41:54'], 
#         'duration_sec': 93*60, 'type': 'impulse2_to1', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '30.04.2025 03:42:00', 'step_duration': 20*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch4', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '02.05.2025', 
#         'time_begin': ['01.05.2025 22:05:30', '01.05.2025 23:38:40'], 
#         'duration_sec': 93*60, 'type': 'step', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '02.05.2025 00:39:00', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch3', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '02.05.2025', 
#         'time_begin': ['01.05.2025 22:05:30', '01.05.2025 23:38:40'], 
#         'duration_sec': 93*60, 'type': 'step4_to3', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '02.05.2025 00:39:00', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch4', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '02.05.2025', 
#         'time_begin': ['02.05.2025 01:12:30', '02.05.2025 02:46:02'], 
#         'duration_sec': 93*60, 'type': 'impulse', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '02.05.2025 03:45:02', 'step_duration': 15*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch3', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '02.05.2025', 
#         'time_begin': ['02.05.2025 01:12:30', '02.05.2025 02:46:02'], 
#         'duration_sec': 93*60, 'type': 'impulse4_to3', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '02.05.2025 03:45:02', 'step_duration': 20*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)


# data_info = {'data': data, 'device': 'летный', 'channel': 'ch3', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '04.05.2025', 
#         'time_begin': ['03.05.2025 22:12:11', '03.05.2025 23:45:10'], 
#         'duration_sec': 93*60, 'type': 'step', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '04.05.2025 00:42:01', 'step_duration': 26*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch4', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '04.05.2025', 
#         'time_begin': ['03.05.2025 22:12:11', '03.05.2025 23:45:10'], 
#         'duration_sec': 93*60, 'type': 'step3_to4', 'type_ru': 'ступенчатое'}
# cut_step_resp = {'time_step_begin': '04.05.2025 00:42:01', 'step_duration': 30*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch3', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '04.05.2025', 
#         'time_begin': ['04.05.2025 01:19:10', '04.05.2025 02:52:11'], 
#         'duration_sec': 93*60, 'type': 'impulse', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '04.05.2025 03:48:01', 'step_duration': 15*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)

# data_info = {'data': data, 'device': 'летный', 'channel': 'ch4', 'period': '1 мин', 
#         'find_accuracy': 'seconds'}
# thermocycle_info = {'date': '04.05.2025', 
#         'time_begin': ['04.05.2025 01:19:10', '04.05.2025 02:52:11'], 
#         'duration_sec': 93*60, 'type': 'impulse3_to4', 'type_ru': 'импульсное'}
# cut_step_resp = {'time_step_begin': '04.05.2025 03:48:01', 'step_duration': 20*60}

# data_info_list.append(data_info)
# thermocycle_info_list.append(thermocycle_info)
# cut_step_resp_list.append(cut_step_resp)


def get_step_response(data_info, thermocycle_info, cut_step_resp):
    name = f'{data_info["channel"]}_{thermocycle_info["type"]}_response_{data_info["device"]}_{thermocycle_info["date"].replace(".","")}'
    plot_info = {'title': 'Реакция на ' + thermocycle_info['type_ru'] + ' воздействие', 
            'ox_dtime_format': "%H:%M:%S", 'legend_pos': ['upper left', 'lower left'],
            'name_fig': f'../plots/response/{name}.png', 'font': 10}
    
    asotr.plot_step_response_in_thermocycle(data_info, thermocycle_info, interp, 
            cut_step_resp, plot_info)
    
    step_resp_cut, _, _ = asotr.get_step_response_diff(data_info['data'], thermocycle_info, 
            channel=data_info['channel'], interp=interp, accuracy=data_info['find_accuracy'], 
            cut_step_resp=cut_step_resp)
    
    max_ = len(step_resp_cut)
    
    step_resp_cut.to_csv(f'../data/asotr/response/asotr0{asotr_kit}_{data_info["channel"]}_{thermocycle_info["type"]}_{thermocycle_info["date"].replace(".","")}.csv', index=False, sep=';', encoding='utf-8-sig', decimal='.')
    
    
    title = f'{plot_info["title"]}, канал {data_info["channel"][2]} АСОТР, {data_info["device"]} СПИН-X1-МВН, период опроса {data_info["period"]} ({thermocycle_info["date"]})'
    
    fig = plt.figure(figsize=(6, 10), dpi=200)
    fig.suptitle(title, fontsize=plot_info['font'])
    ax1 = fig.add_subplot(1,1,1)
    
    ax1.plot(step_resp_cut['timestamp'].iloc[0:max_], step_resp_cut['temp'].iloc[0:max_], '-',
            label='реакция на ' + thermocycle_info['type_ru'] + ' воздействие с термоциклом')
    
    date_formatter = dates.DateFormatter(plot_info['ox_dtime_format'])
    ax1.xaxis.set_major_formatter(date_formatter)
    ax1.legend(loc=plot_info["legend_pos"][0], fontsize=plot_info['font'])
    ax1.grid(True)
    ax1.tick_params(axis='both', width=1, labelsize=plot_info['font'])
    ax1.set_ylabel(r'$T_{норм}$, $^\circ$C', fontsize=plot_info['font'])
    ax1.set_xlabel('Время, ЧЧ:MM', fontsize=plot_info['font'])
    
    plt.tight_layout()
    fig.savefig(plot_info["name_fig"])
    plt.show()

for i, elem in enumerate(data_info_list):
    get_step_response(data_info_list[i], thermocycle_info_list[i], cut_step_resp_list[i])

