Skip to content

Instantly share code, notes, and snippets.

@mfehr
Last active April 11, 2020 20:18
Show Gist options
  • Save mfehr/c40465c7e196d8554a5054d4f8860a97 to your computer and use it in GitHub Desktop.
Save mfehr/c40465c7e196d8554a5054d4f8860a97 to your computer and use it in GitHub Desktop.
Combine rosbags
#!/usr/bin/env bash
set -e
ROSBAG_FOLDER=$1
TOPICS="${@:2}"
ROSBAG_PATH_OUT=${ROSBAG_FOLDER}/combined.bag
i=0
ROSBAG_PATH_A=""
ROSBAG_PATH_B=""
ROSBAG_PATH_TMP=${ROSBAG_FOLDER}/tmp.bag
ALL_ROSBAGS=$(ls $ROSBAG_FOLDER/*.bag)
echo "All rosbags: ${ALL_ROSBAGS}"
for ROSBAG_PATH in $ROSBAG_FOLDER/*.bag; do
echo "Iteration $i"
if [ $i -eq 0 ]
then
ROSBAG_PATH_A=${ROSBAG_PATH}
else
ROSBAG_PATH_B=${ROSBAG_PATH}
echo "./combine_bags.py -i1 $ROSBAG_PATH_A -t1 $TOPICS -i2 $ROSBAG_PATH_B -t2 $TOPICS -o $ROSBAG_PATH_OUT"
./combine_bags.py -i1 $ROSBAG_PATH_A -t1 $TOPICS -i2 $ROSBAG_PATH_B -t2 $TOPICS -o $ROSBAG_PATH_OUT
# Make the output the new input bag A.
rm -rf $ROSBAG_PATH_TMP
mv $ROSBAG_PATH_OUT $ROSBAG_PATH_TMP
ROSBAG_PATH_A=${ROSBAG_PATH_TMP}
fi
((i=i+1))
done
# Move the tmp back to the output location.
mv $ROSBAG_PATH_TMP $ROSBAG_PATH_OUT
#!/usr/bin/env python
import roslib
import rosbag
import rospy
import sys
import argparse
def main(argv):
inputfile1 = ''
topics1 = ''
inputfile2 = ''
topics2 = ''
outputfile = ''
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-o', dest='outputfile', required=True)
parser.add_argument('-i1', dest='inputfile1', required=True)
parser.add_argument('-t1', dest='topics1', nargs='*', type=str)
parser.add_argument('-i2', dest='inputfile2', required=True)
parser.add_argument('-t2', dest='topics2', nargs='*', type=str)
parsed_args = parser.parse_args()
inputfile1 = parsed_args.inputfile1
inputfile2 = parsed_args.inputfile2
outputfile = parsed_args.outputfile
topics1 = parsed_args.topics1
topics2 = parsed_args.topics2
print 'Input 1 file is "', inputfile1
print 'Input 1 topics are "', topics1
print 'Input 2 file is "', inputfile2
print 'Input 2 topics are "', topics2
print 'Output file is "', outputfile
rospy.init_node('bag_combiner')
outbag = rosbag.Bag(outputfile, 'w')
print "[ --- combine bags --- ]"
try:
for topic, msg, t in rosbag.Bag(inputfile1).read_messages(topics=topics1):
if topic == "/tf" or topic == "/tf_static":
outbag.write(topic, msg, msg.transforms[0].header.stamp)
else:
outbag.write(topic, msg, msg.header.stamp)
finally:
print ""
print ": Finished iterating through bag 1."
print ""
try:
for topic, msg, t in rosbag.Bag(inputfile2).read_messages(topics=topics2):
if topic == "/tf" or topic == "/tf_static":
outbag.write(topic, msg, msg.transforms[0].header.stamp)
else:
outbag.write(topic, msg, msg.header.stamp)
finally:
print ""
print ": Finished iterating through bag 2."
print ""
outbag.close()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment