add arguments for input and output file

This commit is contained in:
interfisch 2021-10-04 18:53:08 +02:00
parent adf7df9b9b
commit 92fbaf3ceb
1 changed files with 26 additions and 13 deletions

View File

@ -1,13 +1,22 @@
import numpy as np import numpy as np
from datetime import datetime from datetime import datetime
import time import time
import argparse
parser = argparse.ArgumentParser(description='Copys, renames and fixes logfiles written by bobbycar sd logger.')
parser.add_argument('input', type=argparse.FileType('r'))
parser.add_argument('output', nargs='?', type=argparse.FileType('w'))
args = parser.parse_args()
ok=True ok=True
inputFilename='LOG00251.TXT' inputFilename=args.input.name
outputFilename=None
if args.output is not None:
outputFilename=args.output.name
print("Filename: "+str(inputFilename)) print("Input Filename: "+str(inputFilename))
with open(inputFilename, 'r') as reader: with open(inputFilename, 'r') as reader:
lines = reader.readlines() lines = reader.readlines()
@ -34,7 +43,7 @@ linesSize = [len(x.split(',')) for x in lines] #count arraysize for every datali
linesOK = np.array(linesSize)==headerSize #mask for okay lines (valid for data lines) linesOK = np.array(linesSize)==headerSize #mask for okay lines (valid for data lines)
timestamp=int(lines[0].split('TIMESTAMP:')[1]) #timestamp when file was created timestamp=int(lines[0].split('TIMESTAMP:')[1]) #timestamp when file was created
#outputFilename=
print("Found "+str(len(lines))+" lines") print("Found "+str(len(lines))+" lines")
@ -44,7 +53,8 @@ print(str(len(datalinesOK))+" Datalines OK")
print("Header Size is "+str(headerSize)) print("Header Size is "+str(headerSize))
filetime = time.strftime('%Y%m%d_%H%M%S', time.localtime(timestamp)) filetime = time.strftime('%Y%m%d_%H%M%S', time.localtime(timestamp))
outputFilename = filetime+".csv" if outputFilename is None:
outputFilename = filetime+".csv"
#is_dst(datetime(2019, 4, 1), timezone="US/Pacific") #is_dst(datetime(2019, 4, 1), timezone="US/Pacific")
print("Timestamp:"+str(timestamp)+" -> "+str(filetime)) print("Timestamp:"+str(timestamp)+" -> "+str(filetime))
@ -55,13 +65,16 @@ print("Local Time:"+time.strftime('%A, %Y-%m-%d %H:%M:%S', time.localtime(timest
print("Writing to: "+str(outputFilename)) print("Writing to: "+str(outputFilename))
linesWritten = 0 linesWritten = 0
with open(outputFilename, 'w') as writer: if ok:
for i,line in enumerate(lines): with open(outputFilename, 'w') as writer:
if i!=0 and (commentlines[i] or linesOK[i]): for i,line in enumerate(lines):
writer.write(line+"\n") if i!=0 and (commentlines[i] or linesOK[i]):
linesWritten+=1 writer.write(line+"\n")
else: linesWritten+=1
print("Skipped "+str(i)+": "+str(line)) else:
print("Skipped "+str(i)+": "+str(line))
print(str(linesWritten)+" lines written to "+str(outputFilename))
print(str(linesWritten)+" lines written to "+str(outputFilename))
else:
print("Failed!")