-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandumpLogFilter.py
More file actions
74 lines (57 loc) · 2.99 KB
/
candumpLogFilter.py
File metadata and controls
74 lines (57 loc) · 2.99 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
# This code was generated by ChatGPT, a large language model trained by OpenAI,
# based on the GPT-3.5 architecture. ChatGPT is a general-purpose language model
# that can perform a wide variety of tasks, including generating code. Please
# note that while ChatGPT tries to generate correct and functional code, it may
# not always produce optimal or secure code. Therefore, it is important to
# review and test the code generated by ChatGPT carefully before using it in any
# production environment.
"""
This script filters a candump log file for a set of specific CAN IDs that are provided as a command line argument. The
script reads from a file that is provided as an input argument and writes the filtered data to a new file in the same
directory as the input file. The new file has the same name as the input file, but with the filtered IDs appended to it.
The script also normalizes the time stamps to start at 0, and estimates progress through the file. Progress is printed
to the console every time the percentage of bytes read changes by an integer amount.
"""
import os
import sys
# Check if a file path argument is provided
if len(sys.argv) < 2:
print('Usage: python filter_candump.py <file_path> <can_id>...')
sys.exit(1)
# Get the file path and CAN IDs to filter
file_path = sys.argv[1]
can_ids = set(sys.argv[2:])
# Get the directory and base name of the input file
dir_name = os.path.dirname(file_path)
base_name = os.path.basename(file_path)
# Construct the output file path with filtered IDs appended
output_file_path = os.path.join(dir_name, f"{os.path.splitext(base_name)[0]}_{'_'.join(can_ids)}.log")
# Initialize the time offset and the progress variables
time_offset = None
bytes_read = 0
progress_percent = 0
# Open the input and output files
with open(file_path) as input_file, open(output_file_path, 'w') as output_file:
for line in input_file:
# Update the progress percentage
bytes_read += len(line)
new_progress_percent = int(bytes_read / os.stat(file_path).st_size * 100)
if new_progress_percent != progress_percent:
progress_percent = new_progress_percent
print(f"Progress: {progress_percent}%")
# Split the line into its components
parts = line.strip().split()
# Check if the line contains a CAN frame
if len(parts) == 3 and parts[1] == 'can0':
# Extract the timestamp and the CAN ID from the frame
timestamp = float(parts[0][1:-1])
can_id = parts[2].split('#')[0]
# Normalize the timestamp if it hasn't been done yet
if time_offset is None:
time_offset = timestamp
# Check if the CAN ID is in the set of IDs to filter
if can_id in can_ids:
# Write the line to the output file if the ID matches
output_file.write(f"({timestamp - time_offset:.6f}) {' '.join(parts[1:])}\n")
# Print a message indicating the output file path
print(f"Filtered data written to {output_file_path}")