# /usr/bin/env python
# -*- coding: UTF-8 -*-

def earth_inside(conv_data,ctnum):

    if ctnum==2:
        flag_a=0;index_a=[]
        flag_c=0;index_c=[]
        length = len(conv_data['ibc'])
        conv_data['iab'] = conv_data['iab'] + conv_data['ibc']
        conv_data['uab'] = conv_data['uab'] + conv_data['ubc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['iab'], conv_data['uab']):
            if i > 0:
                if each_pair_i-conv_data['iab'][0] > conv_data['iab'][0] * 0.07 and conv_data['uab'][
                            0] - each_pair_u > conv_data['uab'][0] * 0.08:
                    if i < length:
                       flag_a=1
                    else:
                        flag_c=1
            i += 1
        if len(index_c) > 0:
            index_c = [x - length for x in index_c]
        set_len_ac = len(set(set(index_a) & set(index_c)))
        if flag_a==1 and flag_c==1 and set_len_ac>0:
            return 1, u'两相接地AC', u'内部故障'
        elif flag_c==1:
            return 1, u'单相接地C', u'内部故障'
        elif flag_a==1:
            return 1, u'单相接地A', u'内部故障'
        else:
            return 0, 'E', u'内部无故障'
    else:
        flag_a=0;index_a=[]
        flag_b=0;index_c=[]
        flag_c=0;index_b=[]
        length = len(conv_data['ic'])
        conv_data['ia']=conv_data['ia']+conv_data['ib']+conv_data['ic']
        conv_data['ua']=conv_data['ua']+conv_data['ub']+conv_data['uc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['ia'], conv_data['ua']):
            if i > 0:
                if each_pair_i - conv_data['ia'][0] > conv_data['ia'][0] * 0.07 and conv_data['ua'][0] - each_pair_u > conv_data['ua'][0] * 0.08:
                    if i < length:
                        flag_a=1
                    elif i >= 2*length:
                        flag_c=1
                    else:
                        flag_b=1
            i += 1
        if len(index_b) > 0:
            index_b = [x - length for x in index_b]
        if len(index_c) > 0:
            index_c = [x - length for x in index_b]
        set_len_ab = len(set(set(index_a) & set(index_b)))
        set_len_ac = len(set(set(index_a) & set(index_c)))
        set_len_bc = len(set(set(index_c) & set(index_b)))
        if flag_a==1 and flag_b==1 and set_len_ab>0:
            return 1, u'两相接地AB', u'内部故障'
        elif flag_a==1 and flag_c==1 and set_len_ac>0:
            return 1, u'两相接地AC', u'内部故障'
        elif flag_b==1 and flag_c==1 and set_len_bc>0:
            return 1, u'两相接地BC', u'内部故障'
        elif flag_a==1:
            return 1, u'单相接地A', u'内部故障'
        elif flag_b==1:
            return 1, u'单相接地B', u'内部故障'
        elif flag_c==1:
            return 1, u'单相接地C', u'内部故障'
        else:
            return 0, 'E',u'内部无故障'

def earth_outside(conv_data,ctnum):

    if ctnum==2:
        flag_a = 0;index_c=[]
        flag_c = 0;index_a=[]
        length = len(conv_data['ibc'])
        conv_data['iab'] = conv_data['iab'] + conv_data['ibc']
        conv_data['uab'] = conv_data['uab'] + conv_data['ubc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['iab'], conv_data['uab']):
            if i > 0:
                if conv_data['iab'][0]-each_pair_i > conv_data['iab'][0] * 0.07 and conv_data['uab'][
                            0] - each_pair_u > conv_data['uab'][0] * 0.08:
                    if i < length:
                        flag_a = 1
                    else:
                        flag_c = 1

            i += 1
        if len(index_c) > 0:
            index_c = [x - length for x in index_c]
        set_len_ac = len(set(set(index_a) & set(index_c)))
        if flag_a==1 and flag_c==1 and set_len_ac>0:
            return 1, u'两相接地AC', u'内部故障'
        elif flag_c == 1:
            return 1, u'单相接地C', u'外部故障'
        elif flag_a == 1:
            return 1, u'单相接地A', u'外部故障'
        else:
            return 0, 'E', u'外部无故障'
    else:
        flag_a = 0;index_a=[]
        flag_b = 0;index_b=[]
        flag_c = 0;index_c=[]
        length = len(conv_data['ic'])
        conv_data['ia'] = conv_data['ia'] + conv_data['ib'] + conv_data['ic']
        conv_data['ua'] = conv_data['ua'] + conv_data['ub'] + conv_data['uc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['ia'], conv_data['ua']):
            if i > 0:
                if  conv_data['ia'][0]-each_pair_i > conv_data['ia'][0] * 0.07 and conv_data['ua'][0] - each_pair_u > \
                                conv_data['ua'][0] * 0.08:
                    if i < length:
                        flag_a = 1
                    elif i >= 2 * length:
                        flag_c = 1
                    else:
                        flag_b = 1
            i += 1
        if len(index_b) > 0:
            index_b = [x - length for x in index_b]
        if len(index_c) > 0:
            index_c = [x - length for x in index_b]
        set_len_ab = len(set(set(index_a) & set(index_b)))
        set_len_ac = len(set(set(index_a) & set(index_c)))
        set_len_bc = len(set(set(index_c) & set(index_b)))
        if flag_a==1 and flag_b==1 and set_len_ab>0:
            return 1, u'两相接地AB', u'内部故障'
        elif flag_a==1 and flag_c==1 and set_len_ac>0:
            return 1, u'两相接地AC', u'内部故障'
        elif flag_b==1 and flag_c==1 and set_len_bc>0:
            return 1, u'两相接地BC', u'内部故障'
        elif flag_a == 1:
            return 1, u'单相接地A', u'外部故障'
        elif flag_b == 1:
            return 1, u'单相接地B', u'外部故障'
        elif flag_c == 1:
            return 1, u'单相接地C', u'外部故障'
        else:
            return 0, 'E', u'外部无故障'