Skip to content

Commit 581994e

Browse files
author
Enrique Fernández Perdomo
committed
Merge pull request #6 from clearpathrobotics/add_merge
Add merge
2 parents a394972 + 3c7822c commit 581994e

File tree

2 files changed

+93
-16
lines changed

2 files changed

+93
-16
lines changed

bag_tools/CMakeLists.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,26 @@ install(TARGETS
3131
)
3232

3333
install(PROGRAMS
34-
scripts/stereo_sequence_publisher.py
35-
scripts/replace_msg_time_with_hdr.py
36-
scripts/remove_tf.py
37-
scripts/transform_tf.py
38-
scripts/make_video.py
39-
scripts/image_sequence_publisher.py
40-
scripts/gps_to_std_gt.py
41-
scripts/extract_topics.py
42-
scripts/cut.py
34+
scripts/add_header_time_offset.py
35+
scripts/bag_add_time_offset.py
36+
scripts/batch_process.py
37+
scripts/camera_info_parser.py
38+
scripts/change_camera_info.py
39+
scripts/change_frame_id.py
40+
scripts/change_topics.py
4341
scripts/check_delay.py
4442
scripts/check_drop.py
45-
scripts/change_topics.py
46-
scripts/change_frame_id.py
47-
scripts/change_camera_info.py
48-
scripts/camera_info_parser.py
49-
scripts/batch_process.py
50-
scripts/bag_add_time_offset.py
51-
scripts/add_header_time_offset.py
43+
scripts/cut.py
44+
scripts/extract_topics.py
45+
scripts/gps_to_std_gt.py
46+
scripts/image_sequence_publisher.py
47+
scripts/make_video.py
48+
scripts/merge.py
5249
scripts/plot.py
50+
scripts/remove_tf.py
51+
scripts/replace_msg_time_with_hdr.py
52+
scripts/stereo_sequence_publisher.py
53+
scripts/transform_tf.py
5354
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
5455
)
5556

bag_tools/scripts/merge.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/python
2+
"""
3+
Copyright (c) 2015,
4+
Enrique Fernandez Perdomo
5+
Clearpath Robotics, Inc.
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without
9+
modification, are permitted provided that the following conditions are met:
10+
* Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in the
14+
documentation and/or other materials provided with the distribution.
15+
* Neither the name of Systems, Robotics and Vision Group, University of
16+
the Balearican Islands nor the names of its contributors may be used to
17+
endorse or promote products derived from this software without specific
18+
prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
24+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
"""
31+
32+
import rospy
33+
import rosbag
34+
35+
import argparse
36+
37+
def merge(inbags, outbag='output.bag', topics=None, exclude_topics=[], raw=True):
38+
# Open output bag file:
39+
try:
40+
out = rosbag.Bag(outbag, 'a')
41+
except IOError as e:
42+
rospy.logerr('Failed to open output bag file %s!: %s' % (outbag, e.message))
43+
rospy.signal_shutdown('Failed to open output bag file %s!: %s' % (outbag, e.message))
44+
return
45+
46+
# Write the messages from the input bag files into the output one:
47+
for inbag in inbags:
48+
try:
49+
rospy.loginfo(' Processing input bagfile: %s', inbag)
50+
for topic, msg, t in rosbag.Bag(inbag, 'r').read_messages(topics=topics, raw=raw):
51+
if topic not in args.exclude_topics:
52+
out.write(topic, msg, t, raw=raw)
53+
except IOError as e:
54+
rospy.logerr('Failed to open input bag file %s!: %s' % (inbag, e.message))
55+
rospy.signal_shutdown('Failed to open input bag file %s!: %s' % (inbag, e.message))
56+
return
57+
58+
out.close()
59+
60+
61+
if __name__ == "__main__":
62+
rospy.init_node('merge', anonymous=True)
63+
64+
parser = argparse.ArgumentParser(
65+
description='Merge multiple bag files into a single one.')
66+
parser.add_argument('inbag', help='input bagfile(s)', nargs='+')
67+
parser.add_argument('--output', help='output bag file', default='output.bag')
68+
parser.add_argument('--topics', help='topics to merge from the input bag files', nargs='+', default=None)
69+
parser.add_argument('--exclude_topics', help='topics not to merge from the input bag files', nargs='+', default=[])
70+
args = parser.parse_args()
71+
72+
try:
73+
merge(args.inbag, args.output, args.topics, args.exclude_topics)
74+
except Exception, e:
75+
import traceback
76+
traceback.print_exc()

0 commit comments

Comments
 (0)