#!/usr/bin/env python3

from matplotlib import mlab
import matplotlib.pyplot as plt
import numpy as np
import csv
import glob
import os

print("INFO: Now creating the percentile graphs...\n")

def graph_plot():
    b = []
    name=[]
    myFiles = glob.glob('*.csv')
    a=0
    for i in range(len(myFiles)):
        with open(myFiles[a],'r') as csvfile:
            plots = csv.reader(csvfile, delimiter=',')
            for row in plots:
                try:
                    #Time in msec
                    b.append(float(row[1]))
                except Exception:
                    pass
        b.pop(0)
        
        x = sorted(b)
        freq=len(b)
        y = np.arange(freq) / float(freq)
        
        plt.xlabel(f"Inter-Packet Delay (msec)")
        plt.ylabel('CDF')

        # plt.title('CDF of packet sizes')

        plt.plot(x, y, color="red")
        name=myFiles[a].split('_')
        plt.title(f"CDF_{name[0]}")
        plt.savefig(f'{myFiles[a]}_cdf.plot.png')
        plt.clf()
        a+=1
os.chdir('convShaper2_cerberus')
p=os.listdir(os.getcwd())
c=0
for i in range(len(p)):
    try:
        os.chdir(p[c])
        c=c+1
        graph_plot()
    except NotADirectoryError:
        pass
    except FileNotFoundError:
        pass
    os.chdir('..')

os.chdir('..')
os.chdir('convShaper2_convSrc3')
p=os.listdir(os.getcwd())
d=0
for i in range(len(p)):
    try:
        os.chdir(p[d])
        d+=1
        graph_plot()
    except NotADirectoryError:
        pass
    except FileNotFoundError:
        pass
    os.chdir('..')

os.chdir('..')
os.chdir('convSrc3_cerberus')

p=os.listdir(os.getcwd())
e=0
for i in range(len(p)):
    try:
        os.chdir(p[e])
        e+=1
        graph_plot()
    except NotADirectoryError:
        pass
    except FileNotFoundError:
        pass
    os.chdir('..')
print("INFO: Done with the Percentile graphs, check them in the respective directories\n")
