-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtxt2osi.py
More file actions
88 lines (71 loc) · 2.28 KB
/
Copy pathtxt2osi.py
File metadata and controls
88 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
This program converts txt trace files separated with $$__$$ to OSI trace files which are defined by the length of each OSI message.
Example usage:
python3 txt2osi.py -d trace.txt
python3 txt2osi.py -d trace.txt -o myfile
python3 txt2osi.py -d trace.txt -o myfile -c
python3 txt2osi.py -d trace.txt.lzma -c
python3 txt2osi.py -d trace.txt.lzma
"""
from OSITrace import OSITrace
import struct
import lzma
import argparse
import os
def command_line_arguments():
"""Define and handle command line interface"""
dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
parser = argparse.ArgumentParser(
description="Convert txt trace file to osi trace files.",
prog="txt2osi converter",
)
parser.add_argument(
"--data", "-d", help="Path to the file with serialized data.", type=str
)
parser.add_argument(
"--type",
"-t",
help="Name of the type used to serialize data.",
choices=["SensorView", "GroundTruth", "SensorData"],
default="SensorView",
type=str,
required=False,
)
parser.add_argument(
"--output",
"-o",
help="Output name of the file.",
default="converted.osi",
type=str,
required=False,
)
parser.add_argument(
"--compress",
"-c",
help="Compress the output to a lzma file.",
default=False,
required=False,
action="store_true",
)
return parser.parse_args()
def main():
# Handling of command line arguments
args = command_line_arguments()
# Initialize the OSI trace class
trace = OSITrace()
trace.from_file(path=args.data, type_name=args.type, format_type="separated")
sv = trace.get_messages() # Create an iterator for messages
args.output = args.output.split(".", 1)[0] + ".osi"
if args.output == "converted.osi":
args.output = args.data.split(".", 1)[0] + ".osi"
if args.compress:
f = lzma.open(args.output + ".lzma", "ab")
else:
f = open(args.output, "ab")
for message in sv:
byte_buffer = message.SerializeToString()
f.write(struct.pack("<L", len(byte_buffer)) + byte_buffer)
f.close()
trace.scenario_file.close()
if __name__ == "__main__":
main()