'''
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 = 'Loc.txt'
fOut2 = 'Lati.txt'
fOut3 = 'Longi.txt'
fOut4 = 'NType.txt'
fOut5 = 'KVer.txt'
fOut6 = 'FCName.txt'
fOut7 = 'CSp.txt'
fOut8 = 'NCore.txt'
fOut9 = 'CorePC.txt'
fOut10 = 'MSize.txt'
fOut11 = 'DSize.txt'
fOut12 = 'BWLim.txt'

att_to_extract = ['Loc', 'Lati', 'Longi', 'NType', 'KVer', 'FCName', 'CSp', 'NCore', 'CorePC', \
                  'MSize', 'DSize', 'BWLim']
#att_to_extract = ['Loc', 'CSp', 'NType', 'Lati', 'Longi', 'BWLim', 'SwapUsed', \
#                  'NCore', 'TimerMax', 'KVer', 'MSize', 'NumCores', 'MemSize']
index = []
loc = {}
lati = {}
longi = {}
n_type = {}
ker_ver = {}
fc_name = {}
c_sp = {}
n_core = {}
core_pc = {}
m_size = {}
d_size = {}
bw_limit = {}
#swap_used = {}
#num_cores = {}
#mem_size = {}
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 loc: loc[tmp[0]] = [[tmp[1], tmp[index[0]]]]
    else: loc[tmp[0]].append([tmp[1], tmp[index[0]]])
    if tmp[0] not in lati: lati[tmp[0]] = [[tmp[1], tmp[index[1]]]]
    else: lati[tmp[0]].append([tmp[1], tmp[index[1]]]) 
    if tmp[0] not in longi: longi[tmp[0]] = [[tmp[1], tmp[index[2]]]]
    else: longi[tmp[0]].append([tmp[1], tmp[index[2]]]) 
    if tmp[0] not in n_type: n_type[tmp[0]] = [[tmp[1], tmp[index[3]]]]
    else: n_type[tmp[0]].append([tmp[1], tmp[index[3]]])
    if tmp[0] not in ker_ver: ker_ver[tmp[0]] = [[tmp[1], tmp[index[4]]]]
    else: ker_ver[tmp[0]].append([tmp[1], tmp[index[4]]]) 
    if tmp[0] not in fc_name: fc_name[tmp[0]] = [[tmp[1], tmp[index[5]]]]
    else: fc_name[tmp[0]].append([tmp[1], tmp[index[5]]]) 
    if tmp[0] not in c_sp: c_sp[tmp[0]] = [[tmp[1], tmp[index[6]]]]
    else: c_sp[tmp[0]].append([tmp[1], tmp[index[6]]]) 
    if tmp[0] not in n_core: n_core[tmp[0]] = [[tmp[1], tmp[index[7]]]]
    else: n_core[tmp[0]].append([tmp[1], tmp[index[7]]]) 
    if tmp[0] not in core_pc: core_pc[tmp[0]] = [[tmp[1], tmp[index[8]]]]
    else: core_pc[tmp[0]].append([tmp[1], tmp[index[8]]]) 
    if tmp[0] not in m_size: m_size[tmp[0]] = [[tmp[1], tmp[index[9]]]]
    else: m_size[tmp[0]].append([tmp[1], tmp[index[9]]]) 
    if tmp[0] not in d_size: d_size[tmp[0]] = [[tmp[1], tmp[index[10]]]]
    else: d_size[tmp[0]].append([tmp[1], tmp[index[10]]]) 
    if tmp[0] not in bw_limit: bw_limit[tmp[0]] = [[tmp[1], tmp[index[11]]]]
    else: bw_limit[tmp[0]].append([tmp[1], tmp[index[11]]]) 
    #if tmp[0] not in swap_used: swap_used[tmp[0]] = [[tmp[1], tmp[index[6]]]]
    #else: swap_used[tmp[0]].append([tmp[1], tmp[index[6]]]) 
    #if tmp[0] not in num_cores: num_cores[tmp[0]] = [[tmp[1], tmp[index[7]]]]
    #else: num_cores[tmp[0]].append([tmp[1], tmp[index[7]]]) 
    #if tmp[0] not in mem_size: mem_size[tmp[0]] = [[tmp[1], tmp[index[8]]]]
    #else: mem_size[tmp[0]].append([tmp[1], tmp[index[8]]]) 
    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(loc[str(j)]) != num_samples or loc[str(j)][i][1] == '-1': 
        if j not in skip_list : skip_list.append(j) #Don't add duplicates
    if len(lati[str(j)]) != num_samples or lati[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(longi[str(j)]) != num_samples or longi[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(n_type[str(j)]) != num_samples or n_type[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(ker_ver[str(j)]) != num_samples or ker_ver[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(fc_name[str(j)]) != num_samples or fc_name[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(c_sp[str(j)]) != num_samples or c_sp[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(n_core[str(j)]) != num_samples or n_core[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(core_pc[str(j)]) != num_samples or core_pc[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(m_size[str(j)]) != num_samples or m_size[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(d_size[str(j)]) != num_samples or d_size[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    if len(bw_limit[str(j)]) != num_samples or bw_limit[str(j)][i][1] == '-1':
        if j not in skip_list : skip_list.append(j)
    #if len(num_cores[str(j)]) != num_samples or num_cores[str(j)][i][1] == '-1':
    #    if j not in skip_list : skip_list.append(j)
    #if len(mem_size[str(j)]) != num_samples or mem_size[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')
#fdOut13 = open(fOut13, '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 = ''
    #tmp_str13 = ''
    for j in range(0, num_nodes): 
        if j not in skip_list: 
            tmp_str1 += loc[str(j)][i][1] + '\t'
            tmp_str2 += lati[str(j)][i][1] + '\t'
            tmp_str3 += longi[str(j)][i][1] + '\t'
            tmp_str4 += n_type[str(j)][i][1] + '\t'
            tmp_str5 += ker_ver[str(j)][i][1] + '\t'
            tmp_str6 += fc_name[str(j)][i][1] + '\t'
            tmp_str7 += c_sp[str(j)][i][1] + '\t'
            tmp_str8 += n_core[str(j)][i][1] + '\t'
            tmp_str9 += core_pc[str(j)][i][1] + '\t'
            tmp_str10 += m_size[str(j)][i][1] + '\t'
            tmp_str11 += d_size[str(j)][i][1] + '\t'
            tmp_str12 += bw_limit[str(j)][i][1] + '\t'
            #tmp_str13 = tmp_str13 + mem_size[str(j)][i][1] + '\t'
    tmp_str1 = tmp_str1 + '\n'
    tmp_str2 = tmp_str2 + '\n'
    tmp_str3 = tmp_str3 + '\n'
    tmp_str4 = tmp_str4 + '\n'
    tmp_str5 = tmp_str5 + '\n'
    tmp_str6 = tmp_str6 + '\n'
    tmp_str7 = tmp_str7 + '\n'
    tmp_str8 = tmp_str8 + '\n'
    tmp_str9 = tmp_str9 + '\n'
    tmp_str10 = tmp_str10 + '\n'
    tmp_str11 = tmp_str11 + '\n'
    tmp_str12 = tmp_str12 + '\n'
    #tmp_str13 = tmp_str13 + '\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)
    #fdOut13.write(tmp_str13)
fdOut1.close()
fdOut2.close()
fdOut3.close()
fdOut4.close()
fdOut5.close()
fdOut6.close()
fdOut7.close()
fdOut8.close()
fdOut9.close()
fdOut10.close()
fdOut11.close()
fdOut12.close()
#fdOut13.close()
