'''
Created on May 9, 2016

@author: cloud1
'''
'''
Created on May 9, 2016

@author: nimdrak
'''
import operator
import os
import sys


nick='/home/controller/IoT/normal_packet/output'

linkname=[]

#for (path, dir, files) in os.walk(nick):
for (path, dir, files) in os.walk(sys.argv[1]):
    if dir!=[]:
        for i in range(0,len(dir)):
            linkname.append(dir[i])
print '\n'
print 'link parsing starts!'
print linkname
s1 = set(linkname)
s2 = set(['original','information'])            
linkname =list( s1 - s2)
#linkname=linkname - ['original']

for i in range(0,len(linkname)):
    a = linkname[i]
#    dirname=nick + '/' + a + '/'
    dirname=sys.argv[1] + '/' + a + '/'
    #dirname='/home/cloud1/IoT/output_txt/link_data/interval_10_30_2_r_proper_mean/c_interval_10_30_2_0.09/'        
    
    filenames = os.listdir(dirname)
    #for filename in filenames:
    #    full_filename=os.path.join(dirname, filename)
    #    ext = os.path.splitext(full_filename)[-1]                           
    #    if ext == '.txt':
            #print(full_filename)
            
    file_name=[]
    data_vector=[]
    all_data_vector=[]
    sum_data_vector=[]
    time_vector=[]
    len_data_vector=[]
    data_number=0
    
    
    for (path, dir, files) in os.walk(dirname):
        for filename in files:
            ext = os.path.splitext(filename)[-1]
    #        print filename
            if 'bw' in filename:
 #               print("%s %s" % (path, filename));
                
                file_name.append(filename)
    
    #            print path+'/'+filename
                bw_data=open(path+'/'+file_name[data_number],'r')
                
                
                line=bw_data.readline()
                result=line.split()
                data_vector.append(result)
    #            print data_vector
                
                while line:
                    line=bw_data.readline()
                    result=line.split()
                    if result==[]:
                        break
                    data_vector.append(result)
                    
                data_number=data_number+1    
                #print data_vector
                all_data_vector.append(data_vector)
                data_vector=[]
            

    # check empty line and append 0
    for i in range(0,len(all_data_vector)):
        len_data_vector.append(len(all_data_vector[i]))
    print "link case ",a,len_data_vector
#    max_length=max(len_data_vector)
#    max_length=180
    max_length=int(sys.argv[2])
#    print "max ", max_length
    
    for i in range(0,len(all_data_vector)):
        while(len(all_data_vector[i]) < max_length):
     #       print len(all_data_vector[i])
            all_data_vector[i].append(['0'])
#            print len(all_data_vector[i])
#            print all_data_vector[i]
#            print "finished"
#            print len(all_data_vector[i])
#        print len(all_data_vector[i])
    
#    print "link case ",a,len(all_data_vector[i])
    
    for i in range(0,len(all_data_vector[0])):
        sum_data_vector.append(0)
    
    
    
    for j in range(0,len(all_data_vector[0])):
        for i in range(0,data_number):
            sum_data_vector[j]=sum_data_vector[j]+(float(all_data_vector[i][j][0]))
    
    
#    print sum_data_vector;
    
    
    
    f_sum_result1=open(sys.argv[1]+'/'+'sum_'+ a + '_result.txt','w')
#    f_sum_result1=open(nick+'/'+'sum_'+ a + '_result.txt','w')
    
    for i in range(0,len(all_data_vector[0])):
        data1 = str(sum_data_vector[i]) + '\n'
        f_sum_result1.write(data1)
        
    f_sum_result1.close()
    