bag_add_time_offset.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 """
00003 Copyright (c) 2012,
00004 Systems, Robotics and Vision Group
00005 University of the Balearican Islands
00006 All rights reserved.
00007 
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010     * Redistributions of source code must retain the above copyright
00011       notice, this list of conditions and the following disclaimer.
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015     * Neither the name of Systems, Robotics and Vision Group, University of 
00016       the Balearican Islands nor the names of its contributors may be used to 
00017       endorse or promote products derived from this software without specific 
00018       prior written permission.
00019 
00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 """
00031 
00032 
00033 PKG = 'bag_tools' # this package name
00034 
00035 import roslib; roslib.load_manifest(PKG)
00036 import rospy
00037 import rosbag
00038 import os
00039 import sys
00040 import argparse
00041 
00042 def fix_bagfile(inbag, outbag, topics, offset):
00043     print '   Processing input bagfile: ', inbag
00044     print '  Writing to output bagfile: ', outbag
00045     print '            Changing topics: ', topics
00046     print 'Changing publishing time by: ', offset
00047 
00048     outbag = rosbag.Bag(outbag, 'w')
00049 
00050     time_offset = rospy.Duration.from_sec(offset)
00051 
00052     count = {}
00053     for topic in topics:
00054       count[topic] = 0
00055 
00056     for topic, msg, t in rosbag.Bag(inbag).read_messages():
00057         if topic in topics:
00058             outbag.write(topic, msg, t + time_offset)
00059             count[topic] = count[topic] + 1
00060         else:
00061             outbag.write(topic, msg, t)
00062     print 'Closing output bagfile.'
00063     outbag.close()
00064     print 'Changed the following:'
00065     for k, v in count.iteritems():
00066       print k, ': ', v, ' messages.'
00067 
00068 if __name__ == "__main__":
00069 
00070   parser = argparse.ArgumentParser(
00071       description='Shift the publishing time of given topics in input bagfile.')
00072   parser.add_argument('-o', metavar='OUTPUT_BAGFILE', required=True, help='output bagfile')
00073   parser.add_argument('-i', metavar='INPUT_BAGFILE', required=True, help='input bagfile(s)', nargs='+')
00074   parser.add_argument('-of', metavar='OFFSET', required=True, type=float, help='time offset to add in seconds')
00075   parser.add_argument('-t', metavar='TOPIC', required=True, help='topic(s) to change', nargs='+')
00076   args = parser.parse_args()
00077   try:
00078       fix_bagfile(args.i, args.o, arg.t, args.of)
00079   except Exception, e:
00080       import traceback
00081       traceback.print_exc()


bag_tools
Author(s): Stephan Wirth and Miquel Massot
autogenerated on Mon Jan 6 2014 11:48:57