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