Reindex .bag using rosbag API
Hello to everyone,
I am trying to reindex a file using rosbag API in a Python script. When running rosbag reindex <.bag file>
in the command line, everything goes as usual (it creates a backup of the .bag file with the extension .orig.bag), and also creates the reindexed .bag file with the original name.
Now, in my Python code, I am going with the following:
import rosbag
#Reindex the .bag file
with rosbag.Bag('example.bag', 'w') as inbag:
inbag.reindex()
print "Reindexing..."
It does not work. Instead of giving the same result than the command line, it just take the input .bag file (in my case originally around 500MB), and it "resets" the file, reducing the weight to 4kB. After that, it doesn´t make anything else.
Is there anything missing that I am maybe forgetting to include in the script? According to the rosbag API, reindex(self)
doesn´t required anything more.
Thank you.
Asked by Eduardo Alvarado on 2018-07-25 02:22:01 UTC
Answers
the two keys lines in the code of the CLI program that you say works well are:
for offset in outbag.reindex():
pass
This will consume the generator mentioned in the documentation as referenced above. That should create a properly indexed outbag
(this is an old question but I came here looking for this answer via google)
Asked by ljburtz on 2021-05-29 23:55:25 UTC
Comments
If you do not need data inside your application, maybe a solution could be to make system call with command line command....
This is not solution but rather alternative approach.
Asked by destogl on 2018-07-25 02:46:58 UTC
Documentations sais following: "Reindexes the bag file. Yields position of each chunk for progress." So it seams you get only first chunk of a file. Maybe one need to run it into a loop?
Asked by destogl on 2018-07-25 02:50:06 UTC
The second point makes sense, but the unindexed .bag does not return any attribute which I could use to run
reindex
into a loop (e.g. it returns 0 messages). From this .bag I only found possible to retrieve thechunk_threshold
(as long as in the contructor I specifyallow_unindexed=True
)Asked by Eduardo Alvarado on 2018-07-25 03:25:09 UTC
But since neither the method
reindex
norwrite
take such attribute, I do not know how I could apply such loop.Asked by Eduardo Alvarado on 2018-07-25 03:26:01 UTC
I've never done any of this, but you seem to only open the
inbag
for writing. No reading. Is that how it's supposed to be done?Asked by gvdhoorn on 2018-07-25 03:34:19 UTC
The code in ros/ros_comm/tools/rosbag/src/rosbag/rosbag_main.py seems to be doing things differently (but it's somewhat hard to follow).
Asked by gvdhoorn on 2018-07-25 03:38:12 UTC
@gvdhoorn: Not sure. Until now when I wanted to create a new .bag based on another (e.g. write a new .bag containing only one topic from the original), I always read the original file with
read_messages
overtopic, msg, t
and write in the new one based on that by usingwrite
.Asked by Eduardo Alvarado on 2018-07-25 03:48:45 UTC
I don´t find the way to do so in this case. I´ll have a look to the link you send me in the meanwhile, thanks.
Asked by Eduardo Alvarado on 2018-07-25 03:49:21 UTC
Does anyone have any ideas? I´ve been trying these without success, I am about to give up :/
Asked by Eduardo Alvarado on 2018-07-30 08:32:48 UTC