Reindex .bag using rosbag API

asked 2018-07-25 02:22:01 -0500

Eduardo Alvarado gravatar image

updated 2018-07-25 02:25:39 -0500

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:
    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.

edit retag flag offensive close merge delete


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.

destogl gravatar imagedestogl ( 2018-07-25 02:46:58 -0500 )edit

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?

destogl gravatar imagedestogl ( 2018-07-25 02:50:06 -0500 )edit

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 the chunk_threshold (as long as in the contructor I specify allow_unindexed=True)

Eduardo Alvarado gravatar imageEduardo Alvarado ( 2018-07-25 03:25:09 -0500 )edit

But since neither the method reindex nor write take such attribute, I do not know how I could apply such loop.

Eduardo Alvarado gravatar imageEduardo Alvarado ( 2018-07-25 03:26:01 -0500 )edit

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?

gvdhoorn gravatar imagegvdhoorn ( 2018-07-25 03:34:19 -0500 )edit

The code in ros/ros_comm/tools/rosbag/src/rosbag/ seems to be doing things differently (but it's somewhat hard to follow).

gvdhoorn gravatar imagegvdhoorn ( 2018-07-25 03:38:12 -0500 )edit

@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 over topic, msg, t and write in the new one based on that by using write.

Eduardo Alvarado gravatar imageEduardo Alvarado ( 2018-07-25 03:48:45 -0500 )edit

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.

Eduardo Alvarado gravatar imageEduardo Alvarado ( 2018-07-25 03:49:21 -0500 )edit