diff --git a/python/examples/extract_imu_data.py b/python/examples/extract_imu_data.py index fb4f591e..dfeef6da 100755 --- a/python/examples/extract_imu_data.py +++ b/python/examples/extract_imu_data.py @@ -49,36 +49,39 @@ # Read satellite data from the file. reader = DataLoader(input_path) - result = reader.read(message_types=[IMUOutput, RawIMUOutput], show_progress=True) + result = reader.read(message_types=[IMUOutput, RawIMUOutput], show_progress=True, return_numpy=True) imu_data = result[IMUOutput.MESSAGE_TYPE] raw_imu_data = result[RawIMUOutput.MESSAGE_TYPE] - if len(imu_data.messages) == 0 and len(raw_imu_data.messages) == 0: + if len(imu_data.p1_time) == 0 and len(raw_imu_data.p1_time) == 0: logger.warning('No IMU data found in log file.') sys.exit(2) # Generate a CSV file for corrected IMU data. - if len(imu_data.messages) != 0: + if len(imu_data.p1_time) != 0: path = os.path.join(output_dir, 'imu_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(imu_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Accel X (m/s^2), Y, Z, Gyro X (rad/s), Y, Z\n') - for message in imu_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write('%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n' % - (float(message.p1_time), float(gps_time), *message.accel_mps2, *message.gyro_rps)) + np.savetxt(f, np.concatenate([imu_data.p1_time[:, None], + gps_time[:, None], + imu_data.accel_mps2.T, + imu_data.gyro_rps.T], axis=1), fmt='%.6f') else: logger.info("No corrected IMU data.") # Generate a CSV file for raw IMU data. - if len(raw_imu_data.messages) != 0: + if len(raw_imu_data.p1_time) != 0: path = os.path.join(output_dir, 'raw_imu_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(raw_imu_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Accel X (m/s^2), Y, Z, Gyro X (rad/s), Y, Z, Temp(C)\n') - for message in raw_imu_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write('%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\n' % - (float(message.p1_time), float(gps_time), *message.accel_mps2, *message.gyro_rps, message.temperature_degc)) + np.savetxt(f, np.concatenate([raw_imu_data.p1_time[:, None], + gps_time[:, None], + raw_imu_data.accel_mps2.T, + raw_imu_data.gyro_rps.T, + raw_imu_data.temperature_degc[:, None]], axis=1), fmt='%.6f') else: logger.info("No raw IMU data.") diff --git a/python/examples/extract_vehicle_speed_data.py b/python/examples/extract_vehicle_speed_data.py index e034c738..efddc52d 100755 --- a/python/examples/extract_vehicle_speed_data.py +++ b/python/examples/extract_vehicle_speed_data.py @@ -55,87 +55,75 @@ RawWheelSpeedOutput, VehicleSpeedOutput, RawVehicleSpeedOutput], - show_progress=True) - if all(len(d.messages) == 0 for d in result.values()): + show_progress=True, + return_numpy=True) + if all(len(d.p1_time) == 0 for d in result.values()): logger.warning('No speed data found in log file.') sys.exit(2) wheel_speed_data = result[WheelSpeedOutput.MESSAGE_TYPE] - raw_wheel_speed_data = result[RawWheelSpeedOutput.MESSAGE_TYPE] - vehicle_speed_data = result[VehicleSpeedOutput.MESSAGE_TYPE] - raw_vehicle_speed_data = result[RawVehicleSpeedOutput.MESSAGE_TYPE] - # Generate a CSV file for corrected wheel speed data. - if len(wheel_speed_data.messages) != 0: + if len(wheel_speed_data.p1_time) != 0: path = os.path.join(output_dir, 'wheel_speed_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(wheel_speed_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Front Left Speed (m/s), Front Right Speed (m/s), Back Left Speed (m/s), Back Right Speed (m/s), Gear\n') - for message in wheel_speed_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write( - '%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %d\n' % - (float(message.p1_time), - float(gps_time), - message.front_left_speed_mps, - message.front_right_speed_mps, - message.rear_left_speed_mps, - message.rear_right_speed_mps, - message.gear)) + np.savetxt(f, np.stack([wheel_speed_data.p1_time, + gps_time, + wheel_speed_data.front_left_speed_mps, + wheel_speed_data.front_right_speed_mps, + wheel_speed_data.rear_left_speed_mps, + wheel_speed_data.rear_right_speed_mps, + wheel_speed_data.gear], axis=1), fmt='%.6f') else: logger.info("No corrected wheel speed data.") + raw_wheel_speed_data = result[RawWheelSpeedOutput.MESSAGE_TYPE] # Generate a CSV file for raw wheel speed data. - if len(raw_wheel_speed_data.messages) != 0: + if len(raw_wheel_speed_data.p1_time) != 0: path = os.path.join(output_dir, 'raw_wheel_speed_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(raw_wheel_speed_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Front Left Speed (m/s), Front Right Speed (m/s), Back Left Speed (m/s), Back Right Speed (m/s), Gear\n') - for message in wheel_speed_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write( - '%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %d\n' % - (float(message.p1_time), - float(gps_time), - message.front_left_speed_mps, - message.front_right_speed_mps, - message.rear_left_speed_mps, - message.rear_right_speed_mps, - message.gear)) + np.savetxt(f, np.stack([raw_wheel_speed_data.p1_time, + gps_time, + raw_wheel_speed_data.front_left_speed_mps, + raw_wheel_speed_data.front_right_speed_mps, + raw_wheel_speed_data.rear_left_speed_mps, + raw_wheel_speed_data.rear_right_speed_mps, + raw_wheel_speed_data.gear], axis=1), fmt='%.6f') else: logger.info("No raw wheel speed data.") + vehicle_speed_data = result[VehicleSpeedOutput.MESSAGE_TYPE] # Generate a CSV file for corrected vehicle speed data. - if len(vehicle_speed_data.messages) != 0: + if len(vehicle_speed_data.p1_time) != 0: path = os.path.join(output_dir, 'vehicle_speed_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(vehicle_speed_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Vehicle Speed (m/s), Gear\n') - for message in vehicle_speed_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write( - '%.6f, %.6f, %.6f, %d\n' % - (float(message.p1_time), - float(gps_time), - message.vehicle_speed_mps, - message.gear)) + np.savetxt(path, np.stack([vehicle_speed_data.p1_time, + gps_time, + vehicle_speed_data.vehicle_speed_mps, + vehicle_speed_data.gear], axis=1), fmt='%.6f') else: logger.info("No corrected vehicle speed data.") + raw_vehicle_speed_data = result[RawVehicleSpeedOutput.MESSAGE_TYPE] # Generate a CSV file for raw vehicle speed data. - if len(raw_vehicle_speed_data.messages) != 0: + if len(raw_vehicle_speed_data.p1_time) != 0: path = os.path.join(output_dir, 'raw_vehicle_speed_data.csv') logger.info("Generating '%s'." % path) + gps_time = reader.convert_to_gps_time(raw_vehicle_speed_data.p1_time) with open(path, 'w') as f: f.write('P1 Time (sec), GPS Time (sec), Vehicle Speed (m/s), Gear\n') - for message in vehicle_speed_data.messages: - gps_time = reader.convert_to_gps_time(message.p1_time) - f.write( - '%.6f, %.6f, %.6f, %d\n' % - (float(message.p1_time), - float(gps_time), - message.vehicle_speed_mps, - message.gear)) + np.savetxt(path, np.stack([raw_vehicle_speed_data.p1_time, + gps_time, + raw_vehicle_speed_data.vehicle_speed_mps, + raw_vehicle_speed_data.gear], axis=1), fmt='%.6f') else: logger.info("No raw vehicle speed data.")