Skip to content

Instantly share code, notes, and snippets.

@johannah
Created August 3, 2021 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannah/16fbb4c9bf45e96340496b8cccdfba98 to your computer and use it in GitHub Desktop.
Save johannah/16fbb4c9bf45e96340496b8cccdfba98 to your computer and use it in GitHub Desktop.
'''
This script saves each topic in a bagfile as a csv.
Accepts a filename as an optional argument. Operates on all bagfiles in current directory if no argument provided
Originally Written by Nick Speal in May 2013 at McGill University's Aerospace Mechatronics Laboratory
Modified by J.Hansen in 2021
'''
import rosbag, sys, csv
import time
import string
import os #for file management make directory
import shutil #for file management, copy file
import numpy as np
from glob import glob
input_name = sys.argv[1]
if os.path.isdir(input_name):
bag_files = glob(os.path.join(input_name, "*.bag"))
else:
bag_files = [input_name]
for bag_file in bag_files:
print('reading %s'%bag_file)
bag = rosbag.Bag(bag_file)
bag_contents = bag.read_messages()
bag_name = bag.filename
#create a new directory
folder = bag_name.rstrip('.bag')
if not os.path.exists(folder):
os.makedirs(folder)
# get list of topics from the bag
list_of_topics = []
for topic, msg, t in bag_contents:
if topic not in list_of_topics:
list_of_topics.append(topic)
for topic_name in list_of_topics:
#Create a new CSV file for each topic
filename = os.path.join(folder, topic_name.replace('/','__')+'.csv')
with open(filename, 'w+') as csvfile:
filewriter = csv.writer(csvfile, delimiter = ',')
first_iteration = True #allows header row
for subtopic, msg, t in bag.read_messages(topic_name): # for each instant in time that has data for topicName
#parse data from this instant, which is of the form of multiple lines of "Name: value\n"
# - put it in the form of a list of 2-element lists
msg_string = str(msg)
msg_list = msg_string.split('\n')
instantaneous_list_of_data = []
for name_value_pair in msg_list:
split_pair = name_value_pair.split(':')
for i in range(len(split_pair)): #should be 0 to 1
split_pair[i] = split_pair[i].strip()
instantaneous_list_of_data.append(split_pair)
#write the first row from the first element of each pair
if first_iteration: # header
headers = ["rosbagTimestamp"] #first column header
for pair in instantaneous_list_of_data:
headers.append(pair[0])
filewriter.writerow(headers)
first_iteration = False
# write the value from each pair to the file
values = [str(t)] #first column will have rosbag timestamp
for pair in instantaneous_list_of_data:
if len(pair) > 1:
values.append(pair[1])
filewriter.writerow(values)
bag.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment