#!/usr/bin/python

"""
Check timestamps of i2b files against proglog
Usage: check_i2b_timestamps.py [rig nr] [test nr] [allowed nr of seconds difference]
    example: check_i2b_timestamps.py 2 82 30
    outputs a space-delimited txt file to use as input for fix_i2b_timestamps.py, after any desired edits
2011-06-16
updated 2012-08-09 to work with older versions of python (2.2.3) due to old elektrodetest server
@author: Chris Graves (cgra@dtu.dk) (christoph.graves@gmail.com)
"""

import time
import os
import sys

# ----- FUNCTIONS

def load_proglog(file_location):
    fin = open(file_location, "r")
    proglog = fin.read()
    fin.close()
    proglog = proglog.split('\n')
    temp = len(proglog)
    for i in range(temp):
        proglog[i] = proglog[i].split(' : ')
    return proglog

def find_all_in_proglog(what_to_find, proglog):
    lines_found = []
    utimes_found = []
    for i in xrange(len(proglog)):
        if proglog[i][0] != '':
            tmp = proglog[i][0].split(' ')
            if len(tmp[0]) == 3 and len(tmp[1]) == 3 and tmp[-1].isdigit():
                txtdatetime = proglog[i][0]
                utime = time.mktime(time.strptime(txtdatetime))
                if proglog[i][1].find(what_to_find) > -1:
                    utimes_found.append(utime)
                    lines_found.append(proglog[i][0] + ' : ' + proglog[i][1])
    return lines_found, utimes_found
    
def get_i2b_utime(filename): 
    fin = open(filename, "r")
    s = fin.readline(); s = fin.readline(); s = fin.readline()
    # This 3rd line will have the timestamp(s)
    r = s.split()
    return int(float(r[1]))
    # Uncomment and figure out how to fix the following for i2b files that only have
    # d:a:t:e:t:i:m:e without utime - I haven't come across any like that lately but seem to recall some
    #s = r[0].split(':')
    #if s[0] != '':
    #    s.insert(0,'')
    #ut = time.mktime([int(s[1]), int(s[2]), int(s[3]), int(s[4]), int(s[5]), int(s[6]), 0, 0, 0])
    #ut = ut + time.timezone


# ----- MAIN

if len(sys.argv) < 4:
    print 'Usage: check_i2b_timestamps.py [rig nr] [test nr] [allowed nr of seconds difference]'
    sys.exit(0)

rig_nr = sys.argv[1]
test_nr = sys.argv[2]
allowed_nr_seconds_diff = int(sys.argv[3])

base_path = '/home/http/html/rig' + rig_nr + '/' + rig_nr + 'test' + test_nr + '/'
proglog_file =  base_path + 'proglog'
i2b_folder =  base_path + 'impedance/raw/'
output_folder = base_path + 'impedance/'

proglog = load_proglog(proglog_file)
i2bfiles_list = os.listdir(i2b_folder)

nr_of_files_to_fix = 0
nr_of_uncertain_matches = 0
sep = ' '
output = 'result' + sep + 'filename' + sep + 'i2b_datetime' + sep + 'proglog_datetime' + sep + 'i2b_utime' + sep + 'proglog_utime' + sep + 'seconds_to_add\n'
i2bfiles_list.sort()
for filename in i2bfiles_list:
    (shortname, extension) = os.path.splitext(filename)
    imp_utime = 0
    if extension == '.i2b':
        imp_utime = get_i2b_utime(i2b_folder + '/' + filename)
        impfilename_split = shortname.split('_')
        sessnum = impfilename_split[1].replace('s','')
        filenum = impfilename_split[2]
        what_to_find = 'Session ' + sessnum + ', File %04d' % int(filenum)
        lines_found, proglog_utimes_found = find_all_in_proglog(what_to_find, proglog)
        imp_datetime = time.strftime('%Y:%m:%d:%H:%M:%S',time.localtime(int(imp_utime)))
        add_to_output = ''
        if len(lines_found) > 0:
            if len(lines_found) > 1: # In case coincidentally there is another session x, file y for a different Solartron also measured for this test (since the Solartron is not specified in proglog)
                add_to_output += 'CHOICE_'
                nr_of_uncertain_matches += 1
            for i in xrange(len(lines_found)):
                if i > 0: 
                    add_to_output += '\nCHOICE_'
                #imp_meas_time = float(lines_found[i].split()[-1])
                time_diff = proglog_utimes_found[i] - imp_utime
                time_diff_ok = 'OK'
                if abs(time_diff) > abs(allowed_nr_seconds_diff):
                    time_diff_ok = 'FIX!'
                    nr_of_files_to_fix += 1
                proglog_datetime = time.strftime('%Y:%m:%d:%H:%M:%S',time.localtime(int(proglog_utimes_found[i])))
                
		add_to_output += time_diff_ok + sep + filename + sep + imp_datetime + sep + proglog_datetime + sep + str(int(imp_utime)) + sep + str(int(proglog_utimes_found[i])) + sep + str(int(time_diff))
        else:
            add_to_output += 'NOT_FOUND_IN_PROGLOG' + sep + filename + sep + imp_datetime + sep + 'not_found' + sep + str(int(imp_utime)) + sep + '0' + sep + '0'
        print add_to_output
        output += add_to_output + '\n'

print 'Found ' + str(nr_of_files_to_fix) + ' timestamps differing by more than ' + str(allowed_nr_seconds_diff) + ' seconds (including all uncertain matches) (marked "FIX!").'
print 'Found ' + str(nr_of_uncertain_matches) + ' uncertain matches in proglog (marked "CHOICE").'

outfile = output_folder + rig_nr + 'test' + test_nr + '_i2b_times_check_' + str(nr_of_files_to_fix) + '_files_to_fix_' + str(nr_of_uncertain_matches) + '_choices.txt'
fid_output = open(outfile, 'w')
fid_output.write(output)
fid_output.close()
print 'Output file written, ' + outfile + '!'
# It will always say "file written" even if it failed, because try and except don't seem to work on celletest server Python installation
