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.
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.
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?
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
)But since neither the method
reindex
norwrite
take such attribute, I do not know how I could apply such loop.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?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: 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
.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.