import sys
import numpy

nID = int(sys.argv[1])

fdIn = open('node' + sys.argv[1] + '.txt', 'r')
fdOut = open('nodeAutoCorr' + sys.argv[1] + '.txt', 'w')
firstLine = True
#nid     time    CSp     NCore   CFree   1mLd    5mLd    15mLd   MSize   MFree   MFree%  DSize   DFree   Tx      Rx      RspT
attValues = [[], [], [], [], [], [], [], []]
autoCorrValues = []

def acf(x, length):
    result = [1]
    for i in range(1, length):
        result.append(numpy.corrcoef(x[:-i], x[i:])[0][1])
    return result

for line in fdIn:
    if firstLine == True:
        fdOut.write('CFree\t1mLd\t15mLd\tMFree\tMFree%\tDFree\tTx\tRx\n')
        firstLine = False
        continue
    tmp = line.split('\t')
    attValues[0].append(float(tmp[4]))
    attValues[1].append(float(tmp[5]))
    attValues[2].append(float(tmp[7]))
    attValues[3].append(float(tmp[9]))
    attValues[4].append(float(tmp[10]))
    attValues[5].append(float(tmp[12]))
    attValues[6].append(float(tmp[13]))
    attValues[7].append(float(tmp[14]))
fdIn.close()
print len(attValues), len(attValues[0])
numAtt = len(attValues)
for i in range(numAtt):
    mu = numpy.mean(attValues[i])
    std = numpy.std(attValues[i])
    print mu, std
    for j in range(len(attValues[i])):
        attValues[i][j] -= mu
        attValues[i][j] /= std
    autoCorrValues.append(acf(attValues[i], len(attValues[i])/2))
print len(autoCorrValues), len(autoCorrValues[0])
for i in range(len(autoCorrValues[0])):
    tmpStr = ''
    for j in range(numAtt):
        tmpStr += str(autoCorrValues[j][i]) + '\t'
    fdOut.write(tmpStr + '\n')
fdOut.close()
