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

Reindex .bag using rosbag API

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

Eduardo Alvarado gravatar image

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

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.

edit retag flag offensive close merge delete

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.

destogl gravatar image destogl  ( 2018-07-25 02:46:58 -0600 )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 image destogl  ( 2018-07-25 02:50:06 -0600 )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 image Eduardo Alvarado  ( 2018-07-25 03:25:09 -0600 )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 image Eduardo Alvarado  ( 2018-07-25 03:26:01 -0600 )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 image gvdhoorn  ( 2018-07-25 03:34:19 -0600 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2018-07-25 03:38:12 -0600 )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 image Eduardo Alvarado  ( 2018-07-25 03:48:45 -0600 )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 image Eduardo Alvarado  ( 2018-07-25 03:49:21 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-05-29 23:55:25 -0600

ljburtz gravatar image

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)

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-07-25 02:22:01 -0600

Seen: 4,681 times

Last updated: May 29 '21