<<<<<<< HEAD
'''
基于某个周期（一天，一周...）的指标统计库

'''
__author__ = 'lmstack'

import CONFIGURE.PathSetting as PathSetting
import sys
sys.path.append(PathSetting.backend_path)
sys.path.append(PathSetting.middle_path)
import datetime
import Tools
import pandas as pd
import numpy as np
import IndexStaByOneCycle

class IndexStaByPeriod():
    def __init__(self):
        self.indexStaByOneCycle = IndexStaByOneCycle.IndexStaByOneCycle()
        pass

    def drive_odo_sta(self, df_bms, df_gps):
        '''
        计算周期内行车累积行驶里程
        ---------输入参数------------
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_odo ： 累积里程， 如果该周期内gps均无效，则返回None
        invalid_rate : 该周期内gps无效的bms数据行所占比例
        '''
        invalid_count = 0
        total_count = 0
        sum_odo = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_after_combine'])))
        if len(data_number_list) == 0:
            return {'sum_odo':0, 'invalid_rate':0}
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_after_combine'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)

            total_count += len(df_sel_bms)
            if df_sel_bms.loc[0, 'gps_rely'] != 1:
                invalid_count += len(df_sel_bms)
                continue
            else:
                df_sel_gps = df_gps[(df_gps['时间戳']>df_sel_bms.loc[0,'时间戳']) & (df_gps['时间戳']<df_sel_bms.loc[len(df_sel_bms)-1,'时间戳'])]
                df_sel_gps = df_sel_gps.reset_index(drop=True)
                odo = self.indexStaByOneCycle.odo_sta(np.array(df_sel_gps['odo']))
                if not pd.isnull(odo):
                    sum_odo += odo
        invalid_rate = invalid_count/total_count
        return {'sum_odo':sum_odo, 'invalid_rate':invalid_rate}
    
    
    #该函数未完成， TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    def _energy_consump_sta(self, cap, df_bms, df_gps):
        '''
        计算周期内百公里能耗
        ---------输入参数------------
        df_bms : 本周期内的bms数据
        df_gps : 本周期内的gps数据
        ---------输出参数------------
        本周期内的百公里能耗
        '''
        if not df_bms.empty and not df_gps.empty:
            # 计算能耗
            energy_sum = 0
            data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
            for data_number in data_number_list[:]:
                df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
                df_sel_bms = df_sel_bms.reset_index(drop=True)
                soc_array = np.array(df_sel_bms['SOC[%]'])
                soh_array = np.array(df_sel_bms['SOH[%]'])
                volt_array = np.array(df_sel_bms['总电压[V]'])
                energy = self.indexStaByOneCycle.energy_sta(cap, soc_array, soh_array, volt_array)
                if not pd.isnull(energy):
                    energy_sum += energy
            # 计算里程
            pass # TODO!!!!!!!!!!!!!!!!!!!!!
            return 0
        else:
            return None

    def drive_soc_sta(self, df_bms):
        '''
        计算周期内行车净累积soc
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积soc
       '''   

        sum_soc = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_soc
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            sum_soc += abs(df_sel_bms.loc[0, 'SOC[%]'] - df_sel_bms.loc[len(df_sel_bms)-1, 'SOC[%]'])
        return sum_soc

    def drive_time_sta(self, df_bms):
        '''
        计算周期内累计行车时长/h
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的累计行车时长
       '''   

        sum_time = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_time
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            sum_time += (df_sel_bms.loc[len(df_sel_bms)-1, '时间戳'] - df_sel_bms.loc[0, '时间戳']).total_seconds()
        return sum_time / 3600.0

    def drive_capacity_sta(self, cap, df_bms):
        '''
        计算周期内行车净累积ah
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积ah
       '''   

        sum_ah = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_ah
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            soc_array = np.array(df_sel_bms['SOC[%]'])
            soh_array = np.array(df_sel_bms['SOH[%]'])
            sum_ah += self.indexStaByOneCycle.capacity_sta(cap, soc_array, soh_array)
        return sum_ah

    def drive_energy_sta(self, cap, df_bms):
        '''
        计算周期内行车净累积能量
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积能量
       '''   

        sum_energy = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_energy
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            soc_array = np.array(df_sel_bms['SOC[%]'])
            soh_array = np.array(df_sel_bms['SOH[%]'])
            volt_array = np.array(df_sel_bms['总电压[V]'])
            sum_energy += self.indexStaByOneCycle.energy_sta(cap, soc_array, soh_array, volt_array)
=======
'''
基于某个周期（一天，一周...）的指标统计库

'''
__author__ = 'lmstack'

# import CONFIGURE.PathSetting as PathSetting
# import sys
# sys.path.append(PathSetting.backend_path)
# sys.path.append(PathSetting.middle_path)
import datetime
import Tools
import pandas as pd
import numpy as np
from LIB.MIDDLE import IndexStaByOneCycle

class IndexStaByPeriod():
    def __init__(self):
        self.indexStaByOneCycle = IndexStaByOneCycle.IndexStaByOneCycle()
        pass

    def drive_odo_sta(self, df_bms, df_gps):
        '''
        计算周期内行车累积行驶里程
        ---------输入参数------------
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_odo ： 累积里程， 如果该周期内gps均无效，则返回None
        invalid_rate : 该周期内gps无效的bms数据行所占比例
        '''
        invalid_count = 0
        total_count = 0
        sum_odo = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_after_combine'])))
        if len(data_number_list) == 0:
            return {'sum_odo':0, 'invalid_rate':0}
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_after_combine'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)

            total_count += len(df_sel_bms)
            if df_sel_bms.loc[0, 'gps_rely'] != 1:
                invalid_count += len(df_sel_bms)
                continue
            else:
                df_sel_gps = df_gps[(df_gps['时间戳']>df_sel_bms.loc[0,'时间戳']) & (df_gps['时间戳']<df_sel_bms.loc[len(df_sel_bms)-1,'时间戳'])]
                df_sel_gps = df_sel_gps.reset_index(drop=True)
                odo = self.indexStaByOneCycle.odo_sta(np.array(df_sel_gps['odo']))
                if not pd.isnull(odo):
                    sum_odo += odo
        invalid_rate = invalid_count/total_count
        return {'sum_odo':sum_odo, 'invalid_rate':invalid_rate}
    
    
    #该函数未完成， TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    def _energy_consump_sta(self, cap, df_bms, df_gps):
        '''
        计算周期内百公里能耗
        ---------输入参数------------
        df_bms : 本周期内的bms数据
        df_gps : 本周期内的gps数据
        ---------输出参数------------
        本周期内的百公里能耗
        '''
        if not df_bms.empty and not df_gps.empty:
            # 计算能耗
            energy_sum = 0
            data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
            for data_number in data_number_list[:]:
                df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
                df_sel_bms = df_sel_bms.reset_index(drop=True)
                soc_array = np.array(df_sel_bms['SOC[%]'])
                soh_array = np.array(df_sel_bms['SOH[%]'])
                volt_array = np.array(df_sel_bms['总电压[V]'])
                energy = self.indexStaByOneCycle.energy_sta(cap, soc_array, soh_array, volt_array)
                if not pd.isnull(energy):
                    energy_sum += energy
            # 计算里程
            pass # TODO!!!!!!!!!!!!!!!!!!!!!
            return 0
        else:
            return None

    def drive_soc_sta(self, df_bms):
        '''
        计算周期内行车净累积soc
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积soc
       '''   

        sum_soc = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_soc
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            sum_soc += abs(df_sel_bms.loc[0, 'SOC[%]'] - df_sel_bms.loc[len(df_sel_bms)-1, 'SOC[%]'])
        return sum_soc

    def drive_time_sta(self, df_bms):
        '''
        计算周期内累计行车时长/h
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的累计行车时长
       '''   

        sum_time = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_time
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            sum_time += (df_sel_bms.loc[len(df_sel_bms)-1, '时间戳'] - df_sel_bms.loc[0, '时间戳']).total_seconds()
        return sum_time / 3600.0

    def drive_capacity_sta(self, cap, df_bms):
        '''
        计算周期内行车净累积ah
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积ah
       '''   

        sum_ah = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_ah
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            soc_array = np.array(df_sel_bms['SOC[%]'])
            soh_array = np.array(df_sel_bms['SOH[%]'])
            sum_ah += self.indexStaByOneCycle.capacity_sta(cap, soc_array, soh_array)
        return sum_ah

    def drive_energy_sta(self, cap, df_bms):
        '''
        计算周期内行车净累积能量
        ---------输入参数------------
        cap : 标称容量
        df_bms : 一段周期内的预处理后的bms数据
        df_gps : 一段周期内的预处理后的gps数据
        ---------输出参数------------
        sum_ah : 本周期的净累积能量
       '''   

        sum_energy = 0
        data_number_list = sorted(list(set(df_bms[(df_bms['data_status'].isin(['drive']))]['data_split_by_status_time'])))
        if len(data_number_list) == 0:
            return sum_energy
        for data_number in data_number_list[:]:
            df_sel_bms = df_bms[df_bms['data_split_by_status_time'] == data_number]
            df_sel_bms = df_sel_bms.reset_index(drop=True)
            soc_array = np.array(df_sel_bms['SOC[%]'])
            soh_array = np.array(df_sel_bms['SOH[%]'])
            volt_array = np.array(df_sel_bms['总电压[V]'])
            sum_energy += self.indexStaByOneCycle.energy_sta(cap, soc_array, soh_array, volt_array)
>>>>>>> 65a87ae16013552e359df047df19f46fc4e6eb08
        return sum_energy