Ask Your Question

Best way to add a topic to an existing rosbag?

asked 2017-08-28 08:45:00 -0600

vkee gravatar image

updated 2017-08-28 08:45:28 -0600

What would be the best way to add a topic to an existing rosbag?

Play the bag and publish the topic while doing a rosbag record -a?


edit retag flag offensive close merge delete


Add to an bag you just created in w mode or add to a file by opening and appending? The latter is not possible

jwhendy gravatar imagejwhendy ( 2018-10-15 10:55:16 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-08-28 09:18:54 -0600

gvdhoorn gravatar image

updated 2017-08-28 09:21:31 -0600

Use the rosbag API to open the bag, and write it out while adding the new messages for the topic.

Edit: if you already have a bag that has the other topic(s) (or can easily create one), you could attempt a merge instead. No official tools afaik, but there have been some people that implemented similar things. See #q122761 fi.

edit flag offensive delete link more


Based on the rosbag API link (which only provides the 'w' option for writing, and this erases an existing bag) and the merge code, which reads the old bag and re-writes to a new one... the answer is actually "you can't add a topic to an existing bag," right?

jwhendy gravatar imagejwhendy ( 2018-10-15 10:51:55 -0600 )edit

In other words, the first sentence is misleading and I think should be "You can't do this in place. You have to create a new bag in w mode, open the existing bag in r mode, re-write all old messages to the new bag, adding what you want on top."

jwhendy gravatar imagejwhendy ( 2018-10-15 10:53:21 -0600 )edit

I believe "misleading" is a bit strong: only very few storage formats that support streaming interfaces / infrastructure will allow you to append data to the end and still have everything work correctly (ie: stream the contents in chronological order). It's simply not something that ..

gvdhoorn gravatar imagegvdhoorn ( 2018-10-15 10:55:27 -0600 )edit

.. works that way. And rosbag is no exception.

What you write in your comment ("read, write while adding") is at least implied by the "open [..], and write it out while adding the new messages for the topic".

gvdhoorn gravatar imagegvdhoorn ( 2018-10-15 10:56:28 -0600 )edit

I interpreted the question to be about modifying a file. I haven't tried, and wouldn't recommend, but foo = bag(file.bag, 'r') and bar = bag(file.bag, 'w') and then writing foo into bar + extra could be possible. You'd lose the original forever.

jwhendy gravatar imagejwhendy ( 2018-10-15 11:38:19 -0600 )edit

For the answer, it seems simpler to write the details of the method in addition to linking vs. just linking with ambiguous phrasing. This suggests the links could hold the answer. I know how to re-write... and I was hoping to append, so I expected to be enlightened.

jwhendy gravatar imagejwhendy ( 2018-10-15 11:39:46 -0600 )edit

So that's what I meant by misleading. The answer isn't wrong, it just dangled hope before me but when I looked at the links I realized it's what I've already been doing (and could have been spared time reading). Python can append to files, so it's reasonable to think bags might allow it, too.

jwhendy gravatar imagejwhendy ( 2018-10-15 11:41:45 -0600 )edit

The question posted by the OP did not ask for code, it only asked for "the best way". There is no point in repeatedly posting code for the same thing so a hint about where to look for information seems much more appropriate.

gvdhoorn gravatar imagegvdhoorn ( 2018-10-15 11:42:24 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-08-28 08:45:00 -0600

Seen: 2,184 times

Last updated: Aug 28 '17