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

'''Extract time serie partitions from given data.
'''
import sys

if len(sys.argv) != 3: 
    print 'Incorrect options. Enter <num_nodes> <max_time>'
    sys.exit(0)
num_nodes = int(sys.argv[1])
max_time = int(sys.argv[2])

ts_parti_size = 144 #TODO size of time series partition if time series is stationary
fConfig = 'TSPartiConfig.txt'
fCFree = 'CFree.txt'
fDFree = 'DFree.txt'
fTx = 'Tx.txt'
fMFree = 'MFree.txt'
fRspT = 'RspT.txt'
fUpT = 'UpT.txt'
fOneMLd = '1mLd.txt'
fTAve = 'TAve.txt'
fDrift = 'Drift.txt'
fFiveMLd = '5mLd.txt'
fBoot = 'Boot.txt'
fRx = 'Rx.txt'
fOut = 'TSLibrary.txt'

class Config(object):
    def __init__(self, nid, num_cores, mem_size, location):
        self.nid = nid
        self.num_cores = num_cores
        self.mem_size = mem_size
        self.location = location
class TS(object):
    def __init__(self):
        self.cFree = []
        self.dFree = []
        self.tx = []
        self.mFree = []
        self.rspT = []
        self.upT = []
        self.onemLd = []
        self.tAve = []
        self.drift = []
        self.fivemLd = []
        self.boot = []
        self.rx = []

#att_to_extract = [CFree, DFree, Tx, MFree, RspT, UpT, 1mLd, TAve, Drift, 5mLd, Boot, Rx] #This is the order don't change
config_list = []
#read config file
fdConfig = open(fConfig, 'r')
for line in fdConfig:
    tmp = line.split()
    locations = []
    for i in range(3, len(tmp)): locations.append(int(tmp[i]))
    if locations[-1] != max_time: locations.append(max_time) #Add end of time series
    else:
        locations = []
        for k in range(1, int(max_time/ts_parti_size)):
            locations.append(ts_parti_size * k) #TODO change for different sizes
        locations.append(max_time)
    config_list.append(Config(int(tmp[0]), tmp[1], tmp[2], locations))
fdConfig.close()
print 'End reading config file...'

#read data
node_list = {}
for i in range(0, num_nodes): node_list[i] = TS() #Initialize
fdCFree = open(fCFree, 'r')
for line in fdCFree:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].cFree.append(tmp[i])
fdCFree.close()
fdDFree = open(fDFree, 'r')
for line in fdDFree:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].dFree.append(tmp[i])
fdDFree.close()
fdTx = open(fTx, 'r')
for line in fdTx:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].tx.append(tmp[i])
fdTx.close()
fdMFree = open(fMFree, 'r')
for line in fdMFree:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].mFree.append(tmp[i])
fdMFree.close()
fdRspT = open(fRspT, 'r')
for line in fdRspT:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].rspT.append(tmp[i])
fdRspT.close()
fdUpT = open(fUpT, 'r')
for line in fdUpT:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].upT.append(tmp[i])
fdUpT.close()
fdOneMLd = open(fOneMLd, 'r')
for line in fdOneMLd:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].onemLd.append(tmp[i])
fdOneMLd.close()
fdTAve = open(fTAve, 'r')
for line in fdTAve:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].tAve.append(tmp[i])
fdTAve.close()
fdDrift = open(fDrift, 'r')
for line in fdDrift:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].drift.append(tmp[i])
fdDrift.close()
fdFiveMLd = open(fFiveMLd, 'r')
for line in fdFiveMLd:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].fivemLd.append(tmp[i])
fdFiveMLd.close()
fdBoot = open(fBoot, 'r')
for line in fdBoot:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].boot.append(tmp[i])
fdBoot.close()
fdRx = open(fRx, 'r')
for line in fdRx:
    tmp = line.split()
    for i in range(0, len(tmp)): #For each column
        node_list[i].rx.append(tmp[i])
fdRx.close()
print 'End reading data...'

#Partition time series
fdOut = open(fOut, 'w')
#Write format of output
fdOut.write('#NCore\tMSize\tlength\t<CFree>\t<DFree>\t<Tx>\t<MFree>\t<RspT>\t<UpT>\t<1mLd>\t<TAve>\t<Drift>\t<5mLd>\t<Boot>\t<Rx>\n') 
for i in range(0, len(config_list)):
    for j in range(0, len(config_list[i].location)):
        tmp_str = config_list[i].num_cores + '\t' + config_list[i].mem_size + '\t' 
        if j == 0: 
            tmp_len = config_list[i].location[j]
            start = 0
        else: 
            tmp_len = config_list[i].location[j] - config_list[i].location[j - 1] 
            start = config_list[i].location[j - 1]      
        tmp_str = tmp_str + str(tmp_len) + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].cFree[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].dFree[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].tx[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].mFree[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].rspT[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].upT[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].onemLd[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].tAve[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].drift[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].fivemLd[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str += node_list[config_list[i].nid].boot[k] + '\t'
        for k in range(start, config_list[i].location[j]): #Extract time series
           tmp_str = tmp_str + node_list[config_list[i].nid].rx[k] + '\t'
        fdOut.write(tmp_str + '\n')

fdOut.close()
