|
1 | 1 | def setup_fs(s3, key="", secret="", endpoint="", cert=""): |
2 | 2 | """Given a boolean specifying whether to use local disk or S3, setup filesystem |
3 | 3 | Syntax examples: AWS (http://s3.us-east-2.amazonaws.com), MinIO (http://192.168.0.1:9000) |
4 | | - The cert input is relevant if you're using MinIO with TLS enabled, for specifying the path to the certficiate |
| 4 | + The cert input is relevant if you're using MinIO with TLS enabled, for specifying the path to the certficiate. |
| 5 | +
|
| 6 | + The block_size is set to accomodate files up to 55 MB in size. If your log files are larger, adjust this value accordingly |
5 | 7 | """ |
6 | 8 |
|
7 | 9 | if s3: |
8 | 10 | import s3fs |
9 | 11 |
|
| 12 | + block_size = 55 * 1024 * 1024 |
| 13 | + |
10 | 14 | if "amazonaws" in endpoint: |
11 | | - fs = s3fs.S3FileSystem(key=key, secret=secret) |
| 15 | + fs = s3fs.S3FileSystem(key=key, secret=secret, default_block_size=block_size) |
12 | 16 | elif cert != "": |
13 | | - fs = s3fs.S3FileSystem(key=key, secret=secret, client_kwargs={"endpoint_url": endpoint, "verify": cert}) |
| 17 | + fs = s3fs.S3FileSystem( |
| 18 | + key=key, secret=secret, client_kwargs={"endpoint_url": endpoint, "verify": cert}, default_block_size=block_size |
| 19 | + ) |
14 | 20 | else: |
15 | | - fs = s3fs.S3FileSystem(key=key, secret=secret, client_kwargs={"endpoint_url": endpoint},) |
| 21 | + fs = s3fs.S3FileSystem( |
| 22 | + key=key, secret=secret, client_kwargs={"endpoint_url": endpoint}, default_block_size=block_size |
| 23 | + ) |
16 | 24 |
|
17 | 25 | else: |
18 | 26 | from pathlib import Path |
@@ -52,37 +60,31 @@ def list_log_files(fs, devices, start_times, verbose=True): |
52 | 60 | for idx, device in enumerate(devices): |
53 | 61 | start = start_times[idx] |
54 | 62 | log_files_device = canedge_browser.get_log_files(fs, [device], start_date=start) |
55 | | - |
56 | | - # exclude the 1st log file if the last timestamp is before the start timestamp |
57 | | - if len(log_files_device) > 0: |
58 | | - with fs.open(log_files_device[0], "rb") as handle: |
59 | | - mdf_file = mdf_iter.MdfFile(handle) |
60 | | - df_raw_lin = mdf_file.get_data_frame_lin() |
61 | | - df_raw_lin["IDE"] = 0 |
62 | | - df_raw_can = mdf_file.get_data_frame() |
63 | | - df_raw = df_raw_can.append(df_raw_lin) |
64 | | - end_time = df_raw.index[-1] |
65 | | - |
66 | | - if end_time < start: |
67 | | - log_files_device = log_files_device[1:] |
68 | | - |
69 | | - log_files.extend(log_files_device) |
| 63 | + log_files.extend(log_files_device) |
70 | 64 |
|
71 | 65 | if verbose: |
72 | 66 | print(f"Found {len(log_files)} log files\n") |
73 | 67 |
|
74 | 68 | return log_files |
75 | 69 |
|
76 | 70 |
|
77 | | -def restructure_data(df_phys, res, full_col_names=True): |
| 71 | +def restructure_data(df_phys, res, full_col_names=False, pgn_names=False): |
78 | 72 | import pandas as pd |
| 73 | + from J1939_PGN import J1939_PGN |
79 | 74 |
|
80 | 75 | df_phys_join = pd.DataFrame({"TimeStamp": []}) |
81 | 76 | if not df_phys.empty: |
82 | 77 | for message, df_phys_message in df_phys.groupby("CAN ID"): |
83 | 78 | for signal, data in df_phys_message.groupby("Signal"): |
84 | | - if full_col_names == True: |
85 | | - col_name = str(hex(int(message))).upper()[2:] + "." + str(signal) |
| 79 | + |
| 80 | + pgn = J1939_PGN(int(message)).pgn |
| 81 | + |
| 82 | + if full_col_names == True and pgn_names == False: |
| 83 | + col_name = str(hex(int(message))).upper()[2:] + "." + signal |
| 84 | + elif full_col_names == True and pgn_names == True: |
| 85 | + col_name = str(hex(int(message))).upper()[2:] + "." + str(pgn) + "." + signal |
| 86 | + elif full_col_names == False and pgn_names == True: |
| 87 | + col_name = str(pgn) + "." + signal |
86 | 88 | else: |
87 | 89 | col_name = signal |
88 | 90 |
|
@@ -178,18 +180,23 @@ def filter_signals(self, df_phys): |
178 | 180 |
|
179 | 181 | return df_phys |
180 | 182 |
|
181 | | - def get_raw_data(self, log_file): |
182 | | - """Extract a df of raw data and device ID from log file |
| 183 | + def get_raw_data(self, log_file, lin=False): |
| 184 | + """Extract a df of raw data and device ID from log file. |
| 185 | + Optionally include LIN bus data by setting lin=True |
183 | 186 | """ |
184 | 187 | import mdf_iter |
185 | 188 |
|
186 | 189 | with self.fs.open(log_file, "rb") as handle: |
187 | 190 | mdf_file = mdf_iter.MdfFile(handle) |
188 | 191 | device_id = self.get_device_id(mdf_file) |
189 | | - df_raw_lin = mdf_file.get_data_frame_lin() |
190 | | - df_raw_lin["IDE"] = 0 |
191 | | - df_raw_can = mdf_file.get_data_frame() |
192 | | - df_raw = df_raw_can.append(df_raw_lin) |
| 192 | + |
| 193 | + if lin: |
| 194 | + df_raw_lin = mdf_file.get_data_frame_lin() |
| 195 | + df_raw_lin["IDE"] = 0 |
| 196 | + df_raw_can = mdf_file.get_data_frame() |
| 197 | + df_raw = df_raw_can.append(df_raw_lin) |
| 198 | + else: |
| 199 | + df_raw = mdf_file.get_data_frame() |
193 | 200 |
|
194 | 201 | return df_raw, device_id |
195 | 202 |
|
@@ -342,7 +349,6 @@ def construct_new_tp_frame(self, base_frame, payload_concatenated, can_id): |
342 | 349 |
|
343 | 350 | def combine_tp_frames(self, df_raw): |
344 | 351 | import pandas as pd |
345 | | - import sys |
346 | 352 |
|
347 | 353 | bam_pgn_hex = self.frame_struct["bam_pgn_hex"] |
348 | 354 | res_id_list = [int(res_id, 16) for res_id in self.frame_struct["res_id_list_hex"]] |
|
0 commit comments