
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.
Originally posted by gvdhoorn with karma: 86574 on 2017-08-28
This answer was ACCEPTED on the original site
Post score: 1
Original comments
Comment by jwhendy on 2018-10-15:
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?
Comment by jwhendy on 2018-10-15:
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."
Comment by gvdhoorn on 2018-10-15:
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 ..
Comment by gvdhoorn on 2018-10-15:
.. 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".
Comment by jwhendy on 2018-10-15:
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.
Comment by jwhendy on 2018-10-15:
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.
Comment by jwhendy on 2018-10-15:
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.
Comment by gvdhoorn on 2018-10-15:
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.
Comment by gvdhoorn on 2018-10-15:\
Python can append to files, so it's reasonable to think bags might allow it, too.
of course it can, that is not specific to Python. All files can be appended to -- at the file system level.
Whether the resulting string of bytes still makes sense at the application level is something else ..
Comment by gvdhoorn on 2018-10-15:
.. entirely.
I'm pretty flexible normally but I don't agree with your use of "misleading" here at all.