import math
import argparse

import csv

parser = argparse.ArgumentParser(
                    prog = 'File Edit Script',
                    description = 'Read content of text file and write to file',
                    epilog = '')

parser.add_argument('filename')           # positional argument
parser.add_argument('-o', '--output')      # option that takes a value
parser.add_argument('-v', '--verbose', action='store_true')  # on/off flag

args = parser.parse_args()
print(args.filename, args.output, args.verbose)

input_filename=args.filename
output_filename=args.output
if (output_filename is None):
    output_filename=input_filename+".lp"

measurement="bobbycar"
    
with open(input_filename, newline='') as csvfile:
    csvdict = csv.DictReader(csvfile)

    with open(output_filename, 'w', newline='') as wf:
        for irow,row in enumerate(csvdict):
            if (irow%12!=0):
                continue
            #print(row['time'], row['cmd_FrontL'])
            #csvwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
            output_timestamp=int(float(row['timestamp'])*1000000000) #csv timestamp is in seconds, convert to ns
            #output_timestamp=int(float(row['timestamp'])*1000) #timestamp in ms
#            print(list(row.keys())[2:8])
            for key in list(row.keys()):
                if (key != 'timestamp' and key != 'time'):
                    wf.write(measurement+' '+key+'='+row[key]+' '+str(output_timestamp))
                    wf.write('\n')


'''
   
lines=None
with open(args.filename,'r') as rf:
    lines=rf.readlines()
    
lines=[x.rstrip() for x in lines] #remove newline and whitespaces at end
    
#modify content of lines here
    
with open(args.output, 'w') as wf:
    for line in lines:
        wf.write(line)
        wf.write('\n')
'''
