fIn = 'dynamicAttri20101112.txt'
fOut = 'dynamicAttriAve20101112.txt'

nodes = []
Loneminload = []
Lfiveminload = []
Lfreecpu = []
Lfreemem = []
Lgbfree = []
Ltxrate = []
Lrxrate = []
Coneminload = []
Cfiveminload = []
Cfreecpu = []
Cfreemem = []
Cgbfree = []
Ctxrate = []
Crxrate = []

fdIn = open(fIn, 'r')
fdOut = open(fOut, 'w')

for line in fdIn:
    tmp = line.split('\t')
    if tmp[0] in nodes:
        indx = nodes.index(tmp[0])
        # 1minload +/- 2
        if (float(tmp[2]) > -1.0) :
            Coneminload[indx] = Coneminload[indx] + 1
            Loneminload[indx] = Loneminload[indx] + float(tmp[2])
        else:
            Coneminload[indx] = -1

        # 5minload +/- 2
        if (float(tmp[3])> -1.0):
            Cfiveminload[indx] = Cfiveminload[indx] + 1
            Lfiveminload[indx] = Lfiveminload[indx] + float(tmp[3])
        else:
            Cfiveminload[indx] = -1 

        # freecpu +/- 10
        #print tmp[0], tmp[4]
        if tmp[4] is '': Cfreecpu[indx] = -1
        elif (float(tmp[4]) > -1.0):
            Cfreecpu[indx] = Cfreecpu[indx] + 1
            Lfreecpu[indx] = Lfreecpu[indx] + float(tmp[4])
        else:
            Cfreecpu[indx] = -1

        # freemem +/- 10
        if (float(tmp[5]) -1.0):
            Cfreemem[indx] = Cfreemem[indx] + 1
            Lfreemem[indx] = Lfreemem[indx] + float(tmp[5])
        else :
            Cfreemem[indx] = -1

        # gbfree +/- 5
        if (float(tmp[6]) -1.0):
            Cgbfree[indx] = Cgbfree[indx] + 1
            Lgbfree[indx] = Lgbfree[indx] + float(tmp[6])
        else:
            Cgbfree[indx] = -1

        # txrate +/- 1000
        if (float(tmp[7]) > -1.0):
            Ctxrate[indx] = Ctxrate[indx] + 1
            Ltxrate[indx] = Ltxrate[indx] + float(tmp[7])
        else:
            Ctxrate[indx] = -1

        # rxrate +/- 1000
        if (float(tmp[8]) > -1.0):
            Crxrate[indx] = Crxrate[indx] + 1
            Lrxrate[indx] = Lrxrate[indx] + float(tmp[8])
        else :
            Crxrate[indx] = -1
    else:
        nodes.append(tmp[0])

        Loneminload.append(float(tmp[2]))
        if float(tmp[2]) > -1.0: Coneminload.append(1)
        else: Coneminload.append(-1)

        Lfiveminload.append(float(tmp[3]))
        if float(tmp[3]) > -1.0: Cfiveminload.append(1)
        else: Cfiveminload.append(-1)

        if tmp[4] is '': 
            Cfreecpu.append(-1)
            Lfreecpu.append(0.0)
        else:
            Lfreecpu.append(float(tmp[4]))
            if float(tmp[4]) > -1.0: Cfreecpu.append(1)
            else: Cfreecpu.append(-1)

        Lfreemem.append(float(tmp[5]))
        if float(tmp[5]) > -1.0: Cfreemem.append(1)
        else: Cfreemem.append(-1)

        Lgbfree.append(float(tmp[6]))
        if float(tmp[6]) > -1.0: Cgbfree.append(1)
        else: Cgbfree.append(-1)

        Ltxrate.append(float(tmp[7]))
        if float(tmp[7]) > -1.0: Ctxrate.append(1)
        else: Ctxrate.append(-1)

        Lrxrate.append(float(tmp[8]))
        if float(tmp[8]) > -1.0: Crxrate.append(1)
        else: Crxrate.append(-1)

for i in range(0, len(nodes)):
    out = nodes[i] + '\t'
    if Coneminload[i] > 0: out = out +  str(Loneminload[i]/Coneminload[i]) + '\t' 
    else : out = out + '-1\t'

    if Cfiveminload[i] > 0: out = out + str(Lfiveminload[i]/Cfiveminload[i]) + '\t' 
    else : out = out + '-1\t'
    print Cfreecpu[i], Lfreecpu[i]
    if Cfreecpu[i] > 0: out = out + str(Lfreecpu[i]/Cfreecpu[i]) + '\t' 
    else : out = out + '-1\t'

    if Cfreemem[i] > 0: out = out + str(Lfreemem[i]/Cfreemem[i]) + '\t' 
    else : out = out + '-1\t'

    if Cgbfree[i] > 0: out = out + str(Lgbfree[i]/Cgbfree[i]) + '\t' 
    else : out = out + '-1\t'
 
    if Ctxrate[i] > 0: out = out + str(Ltxrate[i]/Ctxrate[i]) + '\t'
    else : out = out + '-1\t'

    if Crxrate[i] > 0: out = out + str(Lrxrate[i]/Crxrate[i]) + '\n'
    else : out = out + '-1\n'
    fdOut.write(out)

fdIn.close()
fdOut.close()
