ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Split ROSbag to two

asked 2018-05-01 15:19:27 -0500

ecidon gravatar image

Hi All,

I am using rosindigo on two computers. I am using a ROS bag which includes sensor data from a robot (it's the Cartographer Deutsches museum 2D dataset) used to build a map. I would like to split the bag such that I will be able to run it as if it's collected from two robots, so lets say if the bag is 2000sec long the I would like to split it to two 1000sec bags and treat it as if both halves where collected at the same time by two different robots. Would I be able to do this using rosbag filter? If so how? Would I need to fix time-stamping in the rosbag?

I am a relatively new user so would be happy for pointers...


edit retag flag offensive close merge delete


Split it as in first half and second half w.r.t time?

robotchicken gravatar image robotchicken  ( 2018-05-01 16:01:36 -0500 )edit

The first half time, so lets say the whole bag runs form 0-2000sec and we have a message each second I would like to have sec 1: message 1 from first half + message 1 from second half sec 2: message 2 from first half + message 2 from second half

ecidon gravatar image ecidon  ( 2018-05-01 16:06:29 -0500 )edit
lucasw gravatar image lucasw  ( 2018-05-01 16:43:36 -0500 )edit

Since the messages have to be played simultaneously, the messages would have to be time stamped accordingly. So I am guessing #q99711 wouldn't apply.

Either way, please mark the question as resolved based on the answer that worked.

robotchicken gravatar image robotchicken  ( 2018-05-02 14:27:55 -0500 )edit

Thanks, marked as resolved

ecidon gravatar image ecidon  ( 2018-05-02 15:13:32 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-05-02 08:30:44 -0500

Clemens gravatar image

I think you should use this solution: How to split a recorded rosbag file ?

edit flag offensive delete link more


Sorry for the late reply, this worked for my situation

ecidon gravatar image ecidon  ( 2018-05-02 15:13:08 -0500 )edit

answered 2018-05-01 16:34:23 -0500

robotchicken gravatar image

updated 2018-05-01 16:36:35 -0500

I think you can write the messages into different topics at the same time, in one bag. (Ref 1, Ref 2)

import rosbag
import rospy
from sensor_msgs.msg import LaserScan
from math import floor

# Bag Names
old_bag_name = 'old_bag.bag'        
new_bag_name = 'new_bag.bag'

# List of topic names in the bag(have one topic that is of type LaserScan)
topics = ['scan_topic']

# List of New Topic Names
new_topics = ['new_scan_topic_1', 'new_scan_topic_2']     

# Open Bags for writing and reading
old_bag = rosbag.Bag(old_bag_name)
new_bag = rosbag.Bag(new_bag_name, 'w')

# Array to store Messages in chronological order
scan_array = []

# Read Bag and append messages from topics accordingly
for topic, msg, t in old_bag.read_messages(topics=topics):
    # Based on the topic name, read into an array(has only one topic right now)
    if(topic == "scan_topic"):

# Write message into new bag for each topic, while updating the time stamp
t = 0
half_time = floor(len(scan_array/2))
while t < half_time:

    # Update Time for required messages
    current_time =
    scan_array[t].header.stamp = current_time
    scan_array[time_offest+t].header.stamp = current_time

    # Write messages onto bag for the current_time
    new_bag.write(new_topics[0], scan_array[t], current_time)
    new_bag.write(new_topics[1], scan_array[time_offest+t], current_time)
    t = t + 1
edit flag offensive delete link more

Question Tools


Asked: 2018-05-01 15:19:27 -0500

Seen: 1,628 times

Last updated: May 02 '18