import matplotlib
# Allow running headless from the command line
matplotlib.use("agg")

from pylab import *
import numpy as np

binSum = [0] * 102
total  = 0
LastYear  = 0
LastMonth = 0
LastDay   = 0
Init = True


if(len(sys.argv) != 2):
  print("Usage: python parseLatency.py filename")

filename = sys.argv[1]

tmp = filename.split('/')
fidRes = open(tmp[1][:-3] + 'res','w')


# Columns:
#  Year
#  Month
#  Day
#  Hour
#  Epoch within the second
#  Num epochs per second (e.g. 20 for 20Hz data)
#  Num of data points for this line of data (written later)
#  Data from ms 0, the last entry is for everything over 100ms
with open(filename) as fid:
  for line in fid:
    data = line.split()

    if(Init == True):
      # First time through
      LastYear  = data[0]
      LastMonth = data[1]
      LastDay   = data[2]
      Init = False

    if( (LastYear == data[0]) and
        (LastMonth == data[1]) and
        (LastDay == data[2])):
      # Still the same day, continue to accumulate the data
      for i in range(7,109):
        binSum[i-7] += int(data[i])
      total += int(data[6])
    else:
      # The day has changed, before we process the current
      # line of data, complete the processing of the last day
      timeSum = 0
      cumSum = [0] * 102
      currTotal = 0
      
      # Build up some sums we need
      for i in range(len(binSum)):
        # Build up the cummulative total
        currTotal += binSum[i]
        cumSum[i] = currTotal

        # bin are spaced 1ms, starting at 0. Convert
        # to total time.
        timeSum += i*binSum[i]
      
      # The input data is quantized to 1ms, we are going to
      # find the percentiles using linear interpolation
      percentiles = [50, 68, 95, 99, 100]
      percentStore = [0] * len(percentiles)
      for per in range(len(percentiles)):
        # Find the percentiles

        limit = int(0.01 * float(percentiles[per]) * float(total))
        for i in range(len(cumSum)):
          if(limit <= cumSum[i]):
            delta = float(cumSum[i] - cumSum[i-1])
            frac = float(limit - cumSum[i-1]) / delta
            percentStore[per] = frac + i - 1
            break
      
      # Columns
      # Year
      # Month
      # Day
      # GPS Week
      # Week seconds
      # Total Epochs
      # Mean
      # Percentiles
      gps_epoch = datetime.datetime(1980,1,6)
      thisEpoch = datetime.datetime(int(LastYear),int(LastMonth),int(LastDay))
      delta = thisEpoch - gps_epoch
      weeks = int(delta.total_seconds() / (86400*7))
      secs  = delta.total_seconds() - 86400*7*weeks 
      fidRes.write("%d %d %d %d %d %d %.2f " %
                 (int(LastYear),int(LastMonth),int(LastDay),
                  weeks,
                  secs,
                  total,
                  float(timeSum) / float(total)))

      for per in range(len(percentiles)):
        fidRes.write("%.2f " % percentStore[per])
      fidRes.write("\n")

      # Initialize the sum with the first set of values from the new hour
      binSum = [0] * 102
      for i in range(7,109):
        binSum[i-7] += int(data[i])
      total = int(data[6])
      LastYear  = data[0]
      LastMonth = data[1]
      LastDay   = data[2]

fidRes.close()




