import traceback
sep = "\t"
line_sep = "\r\n"

def plot_file(file):
    table = Table.load(file, sep, '#')
    plots = plot(table)

def convert_file(input_file_name, output_file_name, field = 0, pol = 0, keithley_3 = False):    
    with open(input_file_name) as inp:
        lines = inp.readlines()
        with open(output_file_name, "wb") as out:
            out.write("Energy" + sep + "rbkenergy" + sep + "Mag" + sep + "Pol" + sep + "Io" + sep + "TEY" + sep + "Norm" + line_sep)
            s = sep + " "    #File format has a space before numeric values
            for line in lines[1:]:
                line = line.strip()
                if line=="": break
                try:                    
                    (Ecrbk,CADC1, CADC2, NORM, CADC3, CADC4, MCurr, cffrbk, ID1Erbk, ID2Erbk, vTime) = line.split(" ")                    
                    #out.write(Ecrbk + sep + Ecrbk + sep + str(field) + sep + str(pol) + sep + CADC1 + sep + CADC2 + sep + (CADC3 if keithley_3 else NORM) + line_sep)
                    out.write(" " + Ecrbk + s + Ecrbk + s + str(field) + s + str(pol) + s + CADC1 + s + CADC2 + s + (CADC3 if keithley_3 else NORM) + line_sep)
                except:
                    traceback.print_exc()                
                



input = get_context().setup.getDataPath()+"/o151027_016.dat"
#output = get_context().setup.getDataPath()+"/os151027_016.dat"
output = get_context().setup.getDataPath()+"/out.dat"


convert_file(input, output)

import filecmp 
print filecmp.cmp(input, output)


plot_file(output)