# 读取数据
import numpy as np
import pandas as pd
import preprocess.preprocess
import importlib
import ana.parameters
import datetime
import os
import matplotlib
import openpyxl

file_path = r'D:\Work\Interior\Cloud_BMS\data_ana\7250.csv'
sn_list = pd.read_csv(file_path,encoding='gbk')
sn_list = sn_list.values.tolist()
# print(sn_list)
sn_list = [['PK504B10100004301']]
df_res = pd.DataFrame(columns=['sn', 'date', 'odo', 'capacity', 'energy', 'energy_rate', 'acc_time_drive',
                               'acc_time_stand', 'acc_time_charge', 'avr_stand_tem', 'avr_stand_tem_rate',
                               'max_pwr', 'regen_flag', 'regen_rate', 'avr_speed', 'max_speed', 'avr_drive_tem',
                               'avr_drive_tem_rate', 'center_long', 'center_lat', 'rad'])
df_soh_res = pd.DataFrame(columns=['sn', 'time', 'accum_ah', 'soh_cell01', 'soh_cell02', 'soh_cell03',
                                   'soh_cell04', 'soh_cell05', 'soh_cell06', 'soh_cell07',
                                   'soh_cell08', 'soh_cell09', 'soh_cell10', 'soh_cell11',
                                   'soh_cell12', 'soh_cell13', 'soh_cell14', 'soh_cell15',
                                   'soh_cell16', 'soh_cell17', 'soh_cell18', 'soh_cell19',
                                   'soh_cell20'])
df_cell_res = pd.DataFrame(columns=['sn', 'time', 'status', 'volt_cell01', 'volt_cell02', 'volt_cell03',
                                    'volt_cell04', 'volt_cell05', 'volt_cell06', 'volt_cell07',
                                    'volt_cell08', 'volt_cell09', 'volt_cell10', 'volt_cell11',
                                    'volt_cell12', 'volt_cell13', 'volt_cell14', 'volt_cell15',
                                    'volt_cell16', 'volt_cell17', 'volt_cell18', 'volt_cell19',
                                    'volt_cell20', 'volt_max', 'volt_min', 'volt_avr', 'soc_diff',
                                    'soc_diff_div', 'over_discharge'])

for sn in sn_list:
    # 数据读取
    base_path = r'D:\Work\Interior\Cloud_BMS\data_ana\Data_Files'
    time = '_from_2020-04-27_to_2021-04-27.csv'
    bms_file = base_path + r'\\' + sn[0] + '\BMS_' + sn[0] + time
    gps_file = base_path + r'\\' + sn[0] + '\GPS_' + sn[0] + time
    # print(bms_file)
    if not os.path.exists(bms_file):
        print('{} bms file does not exist!!!!!!'.format(sn[0]))
        continue
    if not os.path.exists(gps_file):
        print('{} gps file does not exist!!!!!!'.format(sn[0]))
        continue
    df_bms = pd.read_csv(bms_file, encoding='GB2312')
    df_gps = pd.read_csv(gps_file, encoding='GB2312')
    df_bms = df_bms.drop_duplicates(['时间戳'])
    df_gps = df_gps.drop_duplicates(['时间戳'])
    df_bms = df_bms.reset_index(drop=True)
    df_gps = df_gps.reset_index(drop=True)
    df_bms['时间戳'] = pd.to_datetime(df_bms['时间戳'])
    df_gps['时间戳'] = pd.to_datetime(df_gps['时间戳'])
    print('{} data read Well Done!'.format(sn[0]))
    df_bms.to_csv('result2.csv', index=False, encoding='GB2312')
    # 数据预处理
    # 1、数据分段
    # 2、GPS数据可靠与否的判断(根据GPS计算出的平均车速<2认为可靠)
    importlib.reload(preprocess.preprocess)
    df_bms = preprocess.preprocess.data_split(df_bms)
    df_bms.to_csv('result.csv',encoding='GB2312')
    df_bms, df_gps, prepro_record = preprocess.preprocess.data_gps_judge(df_bms, df_gps, 2)
    print('{} data preprocess Well Done!'.format(sn[0]))
    df_bms.to_csv('result.csv', index=False, encoding='GB2312')
    df_gps.to_csv('result1.csv', index=False, encoding='GB2312')
    importlib.reload(ana.parameters)
    # test = ana.parameters.sta_one_drive_cycle(df_bms, df_gps, prepro_record, time_window=3600*24, step=3600*24, start_time="00:00:00")

    # print(test)
    # 计算Daily数据

    start_time = df_bms.loc[0, '时间戳']
    # print(start_time)
    cur_time = start_time
    index = 0
    while cur_time < df_bms.loc[len(df_bms) - 1, '时间戳']:
        if cur_time.day != start_time.day:
            break
        else:
            index = index + 1
            cur_time = df_bms.loc[index, '时间戳']
    start = '00:00:00'
    start_time = datetime.datetime.strptime(str(df_bms.loc[index, '时间戳'])[0:10] + ' ' + start, '%Y-%m-%d %H:%M:%S')
    # print(index, start_time)
    timeDelta = datetime.timedelta(days=1)
    end_time = start_time + timeDelta
    # print(start_time, end_time)
    while end_time < df_bms.loc[len(df_bms) - 1, '时间戳']:
        # while end_time < df_bms.loc[8500, '时间戳']:
        df_sel_bms = df_bms[(df_bms['时间戳'] >= start_time) & (df_bms['时间戳'] <= end_time)]
        df_sel_bms = df_sel_bms.reset_index(drop=True)
        df_sel_gps = df_gps[(df_gps['时间戳'] >= start_time) & (df_gps['时间戳'] <= end_time)]
        df_sel_gps = df_sel_gps.reset_index(drop=True)
        if len(df_sel_bms) == 0:
            continue
        date = ana.parameters.get_date(df_sel_bms)

        odo_total, odo_sum, odo_plus = ana.parameters.get_daily_odo(df_sel_bms, df_sel_gps)
        print('total={},odo={},plus={}'.format(odo_total, odo_sum, odo_plus))

        # 计算单日里程,单日平均车速,单日最高车速
        # print(start_time)
        odo, avr_speed, max_speed = ana.parameters.get_daily_odo_and_speed(df_sel_bms, df_sel_gps)
        # 计算单日使用容量,单日使用能量
        capacity, energy = ana.parameters.get_daily_capacity_and_energy(df_sel_bms, 55)
        # 计算单日平均电耗:kwh/100km
        if odo > 10:
            energy_rate = energy / odo * 100
        else:
            energy_rate = 0
        # 计算单日累积时间(行驶、静置、充电)
        acc_time_drive = ana.parameters.get_daily_accum_time(df_sel_bms, 'drive')
        acc_time_stand = ana.parameters.get_daily_accum_time(df_sel_bms, 'stand')
        acc_time_charge = ana.parameters.get_daily_accum_time(df_sel_bms, 'charge')
        # 计算过程平均温度,平均温升(静置、行驶)

        avr_stand_tem, avr_stand_tem_rate = ana.parameters.get_daily_stand_temp(df_sel_bms, 'stand')
        avr_drive_tem, avr_drive_tem_rate = ana.parameters.get_daily_stand_temp(df_sel_bms, 'drive')
        # 计算车辆功率
        max_pwr = ana.parameters.get_daily_max_pwr(df_sel_bms, 'drive')
        # 计算regen数据,是否有regen以及regen比例
        regen_flag, regen_rate = ana.parameters.get_daily_regen(df_sel_bms, 'drive')
        # 计算活动范围,圆心和半径

        center_long, center_lat, rad = ana.parameters.get_working_scope(df_sel_bms, df_sel_gps, 'drive')

        df_res = df_res.append({'sn': sn[0],
                                'date': date,
                                'odo': odo,
                                'capacity': capacity,
                                'energy': energy,
                                'energy_rate': energy_rate,
                                'acc_time_drive': acc_time_drive,
                                'acc_time_stand': acc_time_stand,
                                'acc_time_charge': acc_time_charge,
                                'avr_stand_tem': avr_stand_tem,
                                'avr_stand_tem_rate': avr_stand_tem_rate,
                                'max_pwr': max_pwr,
                                'regen_flag': regen_flag,
                                'regen_rate': regen_rate,
                                'avr_speed': avr_speed,
                                'max_speed': max_speed,
                                'avr_drive_tem': avr_drive_tem,
                                'avr_drive_tem_rate': avr_drive_tem_rate,
                                'center_long': center_long,
                                'center_lat': center_lat,
                                'rad': rad}, ignore_index=True)
        start_time = end_time
        end_time = start_time + timeDelta
        # print(end_time,df_bms.loc[len(df_bms) - 1, '时间戳'])
    print('{} daily data process Well Done!'.format(sn[0]))

    # 计算电池周期性数据
    df_soh_res_single, df_cell_res_single = ana.parameters.get_soh(df_bms, sn)
    df_soh_res = pd.concat([df_soh_res, df_soh_res_single], ignore_index=True)
    df_cell_res = pd.concat([df_cell_res, df_cell_res_single], ignore_index=True)
    print('{} period data process Well Done!'.format(sn[0]))

path = r'D:\Work\Interior\Cloud_BMS\data_ana\report\daily_data_analysis.csv'
df_res.to_csv(path, index=False, encoding='GB2312')
path = r'D:\Work\Interior\Cloud_BMS\data_ana\report\period_data_analysis.xlsx'
writer = pd.ExcelWriter(path)
df_soh_res.to_excel(writer, "SOH数据")
df_cell_res.to_excel(writer, "一致性数据")
writer.save()