2021-10-04 16:41:16 +00:00
|
|
|
import numpy as np
|
|
|
|
from datetime import datetime
|
|
|
|
import time
|
2021-10-04 16:53:08 +00:00
|
|
|
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()
|
2021-10-04 16:41:16 +00:00
|
|
|
|
|
|
|
ok=True
|
|
|
|
|
2021-10-04 16:53:08 +00:00
|
|
|
inputFilename=args.input.name
|
|
|
|
outputFilename=None
|
|
|
|
if args.output is not None:
|
|
|
|
outputFilename=args.output.name
|
2021-10-04 16:41:16 +00:00
|
|
|
|
|
|
|
|
2021-10-04 16:53:08 +00:00
|
|
|
print("Input Filename: "+str(inputFilename))
|
2021-10-04 16:41:16 +00:00
|
|
|
|
|
|
|
with open(inputFilename, 'r') as reader:
|
|
|
|
lines = reader.readlines()
|
|
|
|
|
|
|
|
|
|
|
|
lines = [x.rstrip("\n") for x in lines] #remove \n
|
2023-07-12 16:39:46 +00:00
|
|
|
commentlinesMask = [True if x.startswith('#') else False for x in lines] #generate mask for lines with comments
|
2021-10-04 16:41:16 +00:00
|
|
|
|
|
|
|
lines=np.array(lines)
|
2023-07-12 16:39:46 +00:00
|
|
|
commentlinesMask=np.array(commentlinesMask)
|
|
|
|
datalines = lines[commentlinesMask==False] #get lines with data
|
2021-10-04 16:41:16 +00:00
|
|
|
header = datalines[0] #header is the first non comment line
|
|
|
|
|
|
|
|
headerSize = len(header.split(',')) #how many elements are expected per line
|
|
|
|
|
|
|
|
datalinesSize = [len(x.split(',')) for x in datalines] #count arraysize for every dataline
|
|
|
|
datalinesOK = datalines[np.array(datalinesSize)==headerSize]
|
|
|
|
datalinesFail = datalines[np.array(datalinesSize)!=headerSize]
|
|
|
|
#datalinesSizeBin = dict((x,datalinesSize.count(x)) for x in set(datalinesSize)) #binning
|
|
|
|
#normalSize = max(datalinesSizeBin, key=datalinesSizeBin.get) #get normal element count by highest bin
|
|
|
|
|
|
|
|
linesSize = [len(x.split(',')) for x in lines] #count arraysize for every dataline
|
|
|
|
linesOK = np.array(linesSize)==headerSize #mask for okay lines (valid for data lines)
|
|
|
|
|
|
|
|
print("Found "+str(len(lines))+" lines")
|
2023-07-12 16:39:46 +00:00
|
|
|
print(str(np.sum(commentlinesMask))+" comments")
|
2021-10-04 16:41:16 +00:00
|
|
|
print(str(len(datalinesFail))+" Datalines Failed")
|
|
|
|
print(str(len(datalinesOK))+" Datalines OK")
|
|
|
|
print("Header Size is "+str(headerSize))
|
|
|
|
|
2023-07-12 16:39:46 +00:00
|
|
|
timestampline=-1
|
|
|
|
timestampfound=False
|
|
|
|
while not timestampfound:
|
|
|
|
timestampline+=1
|
|
|
|
timestampfound = (lines[timestampline].find('TIMESTAMP:')!=-1)
|
|
|
|
|
|
|
|
timestamp=int(lines[timestampline].split('TIMESTAMP:')[1]) #timestamp when file was created
|
|
|
|
|
|
|
|
print("Found Timestamp in line "+str(timestampline))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-04 16:41:16 +00:00
|
|
|
filetime = time.strftime('%Y%m%d_%H%M%S', time.localtime(timestamp))
|
2021-10-04 16:53:08 +00:00
|
|
|
if outputFilename is None:
|
|
|
|
outputFilename = filetime+".csv"
|
2021-10-04 16:41:16 +00:00
|
|
|
|
|
|
|
#is_dst(datetime(2019, 4, 1), timezone="US/Pacific")
|
|
|
|
print("Timestamp:"+str(timestamp)+" -> "+str(filetime))
|
|
|
|
print("UTC: "+ datetime.utcfromtimestamp(timestamp).strftime('%A, %Y-%m-%d %H:%M:%S'))
|
|
|
|
print("Local Time:"+time.strftime('%A, %Y-%m-%d %H:%M:%S', time.localtime(timestamp)))
|
|
|
|
|
|
|
|
|
|
|
|
print("Writing to: "+str(outputFilename))
|
|
|
|
|
2023-07-12 16:39:46 +00:00
|
|
|
print("Size lines="+str(len(lines)))
|
|
|
|
print("Size commentlinesMask="+str(len(commentlinesMask)))
|
|
|
|
print("Size datalines="+str(len(datalines)))
|
|
|
|
print("Size linesOK="+str(len(linesOK)))
|
|
|
|
|
|
|
|
|
2021-10-04 16:41:16 +00:00
|
|
|
linesWritten = 0
|
2021-10-04 16:53:08 +00:00
|
|
|
if ok:
|
|
|
|
with open(outputFilename, 'w') as writer:
|
2023-07-12 16:39:46 +00:00
|
|
|
for i,line in enumerate(datalinesOK):
|
|
|
|
writer.write(line+"\n")
|
|
|
|
linesWritten+=1
|
2021-10-04 16:53:08 +00:00
|
|
|
|
|
|
|
print(str(linesWritten)+" lines written to "+str(outputFilename))
|
|
|
|
else:
|
|
|
|
print("Failed!")
|
2021-10-04 16:41:16 +00:00
|
|
|
|