'''
Created on Mar 14, 2011
Modified on June 25, 2011
@author: Dilum Bandara
@version: 1.1
@license: Python
@copyright: Copyright (c) 2011 Dilum Bandara, Colorado State University
'''

'''Extract time serie of given list of attributes. Use data from extractAttributes.py
   Modify this function if additional attributes are needed
'''

import sys

if len(sys.argv) != 3: 
    print 'Command format: python extractTS.py <input file> <num_of_time_points>'
fIn = sys.argv[1]
num_samples = int(sys.argv[2])
fOut1 = 'CFree.txt'
fOut2 = 'DFree.txt'
fOut3 = 'Tx.txt'
fOut4 = 'MFree.txt'
fOut5 = 'RspT.txt'
fOut6 = 'UpT.txt'
fOut7 = '1mLd.txt'
fOut8 = 'TAve.txt'
fOut9 = 'Drift.txt'
fOut10 = '5mLd.txt'
fOut11 = 'Boot.txt'
fOut12 = 'Rx.txt'

att_to_extract = ['CFree', 'DFree', 'Tx', 'MFree', 'RspT', 'UpT', '1mLd', 'TAve', 'Drift', \
                  '5mLd', 'Boot', 'Rx']
index = []
cFree = {}
dFree = {}
tx = {}
mFree = {}
rspT = {}
upT = {}
onemLd = {}
tAve = {}
drift = {}
fivemLd = {}
boot = {}
rx = {}
num_nodes = 0

fdIn = open(fIn, 'r')
first = True
for line in fdIn:
    tmp = line.split()
    if first == True: #If 1st line ,find what are the attribute indexes to track
        for i in range(0, len(att_to_extract)):
           index.append(tmp.index(att_to_extract[i]))
        first = False
        continue
    if tmp[0] not in cFree: cFree[tmp[0]] = [[tmp[1], tmp[index[0]]]]
    else: cFree[tmp[0]].append([tmp[1], tmp[index[0]]])
    if tmp[0] not in dFree: dFree[tmp[0]] = [[tmp[1], tmp[index[1]]]]
    else: dFree[tmp[0]].append([tmp[1], tmp[index[1]]]) 
    if tmp[0] not in tx: tx[tmp[0]] = [[tmp[1], tmp[index[2]]]]
    else: tx[tmp[0]].append([tmp[1], tmp[index[2]]]) 
    if tmp[0] not in mFree: mFree[tmp[0]] = [[tmp[1], tmp[index[3]]]]
    else: mFree[tmp[0]].append([tmp[1], tmp[index[3]]])
    if tmp[0] not in rspT: rspT[tmp[0]] = [[tmp[1], tmp[index[4]]]]
    else: rspT[tmp[0]].append([tmp[1], tmp[index[4]]]) 
    if tmp[0] not in upT: upT[tmp[0]] = [[tmp[1], tmp[index[5]]]]
    else: upT[tmp[0]].append([tmp[1], tmp[index[5]]]) 
    if tmp[0] not in onemLd: onemLd[tmp[0]] = [[tmp[1], tmp[index[6]]]]
    else: onemLd[tmp[0]].append([tmp[1], tmp[index[6]]]) 
    if tmp[0] not in tAve: tAve[tmp[0]] = [[tmp[1], tmp[index[7]]]]
    else: tAve[tmp[0]].append([tmp[1], tmp[index[7]]]) 
    if tmp[0] not in drift: drift[tmp[0]] = [[tmp[1], tmp[index[8]]]]
    else: drift[tmp[0]].append([tmp[1], tmp[index[8]]]) 
    if tmp[0] not in fivemLd: fivemLd[tmp[0]] = [[tmp[1], tmp[index[9]]]]
    else: fivemLd[tmp[0]].append([tmp[1], tmp[index[9]]]) 
    if tmp[0] not in boot: boot[tmp[0]] = [[tmp[1], tmp[index[10]]]]
    else: boot[tmp[0]].append([tmp[1], tmp[index[10]]]) 
    if tmp[0] not in rx: rx[tmp[0]] = [[tmp[1], tmp[index[11]]]]
    else: rx[tmp[0]].append([tmp[1], tmp[index[11]]]) 
    if num_nodes < int(tmp[0]) + 1: num_nodes = int(tmp[0]) + 1 #0 based index

print 'Number of nodes\t' + str(num_nodes)
fdIn.close()
#Remove nodes with missing values or -1
skip_list = []
for j in range(0, num_nodes): #Check each node
    if len(cFree[str(j)]) != num_samples or cFree[str(j)][i][1] == '-1': 
        if j not in skip_list : skip_list.append(j) #Don't add duplicates
    if len(dFree[str(j)]) != num_samples or dFree[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(tx[str(j)]) != num_samples or tx[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(mFree[str(j)]) != num_samples or mFree[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(rspT[str(j)]) != num_samples or rspT[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(upT[str(j)]) != num_samples or upT[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(onemLd[str(j)]) != num_samples or onemLd[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(tAve[str(j)]) != num_samples or tAve[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(drift[str(j)]) != num_samples or drift[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(fivemLd[str(j)]) != num_samples or fivemLd[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(boot[str(j)]) != num_samples or boot[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(rx[str(j)]) != num_samples or rx[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)

print 'Skipping nodes\t' + str(len(skip_list))

fdOut1 = open(fOut1, 'w')
fdOut2 = open(fOut2, 'w')
fdOut3 = open(fOut3, 'w')
fdOut4 = open(fOut4, 'w')
fdOut5 = open(fOut5, 'w')
fdOut6 = open(fOut6, 'w')
fdOut7 = open(fOut7, 'w')
fdOut8 = open(fOut8, 'w')
fdOut9 = open(fOut9, 'w')
fdOut10 = open(fOut10, 'w')
fdOut11 = open(fOut11, 'w')
fdOut12 = open(fOut12, 'w')
#Write to new file. Arrange as columns
for i in range(0, num_samples):
    tmp_str1 = ''
    tmp_str2 = ''
    tmp_str3 = ''
    tmp_str4 = ''
    tmp_str5 = ''
    tmp_str6 = ''
    tmp_str7 = ''
    tmp_str8 = ''
    tmp_str9 = ''
    tmp_str10 = ''
    tmp_str11 = ''
    tmp_str12 = ''
    for j in range(0, num_nodes): 
        if j not in skip_list: 
            tmp_str1 += cFree[str(j)][i][1] + '\t'
            tmp_str2 += dFree[str(j)][i][1] + '\t'
            tmp_str3 += tx[str(j)][i][1] + '\t'
            tmp_str4 += mFree[str(j)][i][1] + '\t'
            tmp_str5 += rspT[str(j)][i][1] + '\t'
            tmp_str6 += upT[str(j)][i][1] + '\t'
            tmp_str7 += onemLd[str(j)][i][1] + '\t'
            tmp_str8 += tAve[str(j)][i][1] + '\t'
            tmp_str9 += drift[str(j)][i][1] + '\t'
            tmp_str10 += fivemLd[str(j)][i][1] + '\t'
            tmp_str11 += boot[str(j)][i][1] + '\t'
            tmp_str12 += rx[str(j)][i][1] + '\t'
    tmp_str1 += '\n'
    tmp_str2 += '\n'
    tmp_str3 += '\n'
    tmp_str4 += '\n'
    tmp_str5 += '\n'
    tmp_str6 += '\n'
    tmp_str7 += '\n'
    tmp_str8 += '\n'
    tmp_str9 += '\n'
    tmp_str10 += '\n'
    tmp_str11 += '\n'
    tmp_str12 += '\n'
    fdOut1.write(tmp_str1)
    fdOut2.write(tmp_str2)
    fdOut3.write(tmp_str3)
    fdOut4.write(tmp_str4)
    fdOut5.write(tmp_str5)
    fdOut6.write(tmp_str6)
    fdOut7.write(tmp_str7)
    fdOut8.write(tmp_str8)
    fdOut9.write(tmp_str9)
    fdOut10.write(tmp_str10)
    fdOut11.write(tmp_str11)
    fdOut12.write(tmp_str12)
fdOut1.close()
fdOut2.close()
fdOut3.close()
fdOut4.close()
fdOut5.close()
fdOut6.close()
fdOut7.close()
fdOut8.close()
fdOut9.close()
fdOut10.close()
fdOut11.close()
fdOut12.close()
