merge.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 # Copied from https://raw.githubusercontent.com/srv/srv_tools/kinetic/bag_tools/scripts/merge.py since this script is
00003 # not released for indigo.
00004 """
00005 Copyright (c) 2015,
00006 Enrique Fernandez Perdomo
00007 Clearpath Robotics, Inc.
00008 All rights reserved.
00009 
00010 Redistribution and use in source and binary forms, with or without
00011 modification, are permitted provided that the following conditions are met:
00012     * Redistributions of source code must retain the above copyright
00013       notice, this list of conditions and the following disclaimer.
00014     * Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in the
00016       documentation and/or other materials provided with the distribution.
00017     * Neither the name of Systems, Robotics and Vision Group, University of
00018       the Balearican Islands nor the names of its contributors may be used to
00019       endorse or promote products derived from this software without specific
00020       prior written permission.
00021 
00022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00023 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00024 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00026 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00029 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 """
00033 
00034 from __future__ import print_function
00035 
00036 import rosbag
00037 
00038 import argparse
00039 import os
00040 import sys
00041 
00042 def merge(inbags, outbag='output.bag', topics=None, exclude_topics=[], raw=True):
00043   # Open output bag file:
00044   try:
00045     out = rosbag.Bag(outbag, 'a' if os.path.exists(outbag) else 'w')
00046   except IOError as e:
00047     print('Failed to open output bag file %s!: %s' % (outbag, e.message), file=sys.stderr)
00048     return 127
00049 
00050   # Write the messages from the input bag files into the output one:
00051   for inbag in inbags:
00052     try:
00053       print('   Processing input bagfile: %s' % inbag)
00054       for topic, msg, t in rosbag.Bag(inbag, 'r').read_messages(topics=topics, raw=raw):
00055         if topic not in args.exclude_topics:
00056           out.write(topic, msg, t, raw=raw)
00057     except IOError as e:
00058       print('Failed to open input bag file %s!: %s' % (inbag, e.message), file=sys.stderr)
00059       return 127
00060 
00061   out.close()
00062 
00063   return 0
00064 
00065 
00066 if __name__ == "__main__":
00067   parser = argparse.ArgumentParser(
00068       description='Merge multiple bag files into a single one.')
00069   parser.add_argument('inbag', help='input bagfile(s)', nargs='+')
00070   parser.add_argument('--output', help='output bag file', default='output.bag')
00071   parser.add_argument('--topics', help='topics to merge from the input bag files', nargs='+', default=None)
00072   parser.add_argument('--exclude_topics', help='topics not to merge from the input bag files', nargs='+', default=[])
00073   args = parser.parse_args()
00074 
00075   try:
00076     sys.exit(merge(args.inbag, args.output, args.topics, args.exclude_topics))
00077   except Exception, e:
00078     import traceback
00079     traceback.print_exc()


movie_publisher
Author(s): Martin Pecka
autogenerated on Mon Mar 18 2019 02:30:46