cut.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 PKG = 'bag_tools' # this package name
00033 
00034 import roslib; roslib.load_manifest(PKG)
00035 import rospy
00036 import rosbag
00037 import os
00038 import sys
00039 import argparse
00040 
00041 def cut(inbags, outbagfile, start, duration):
00042   start_time = rospy.Time.from_sec(999999999999)
00043   for inbag in inbags:
00044     print '   Looking for smallest time in:', inbag
00045     for topic, msg, t in rosbag.Bag(inbag,'r').read_messages():
00046       if t < start_time:
00047         start_time = t
00048       break
00049   print '   Bagfiles start at', start_time
00050   start_time = start_time + rospy.Duration.from_sec(start)
00051   end_time = start_time + rospy.Duration.from_sec(duration)
00052   print '   Cutting out from', start_time, 'to', end_time
00053   outbag = rosbag.Bag(outbagfile, 'w')
00054   num_messages = 0
00055   for inbag in inbags:
00056     print '   Extracting messages from:', inbag
00057     for topic, msg, t in rosbag.Bag(inbag,'r').read_messages(start_time=start_time, end_time=end_time):
00058       outbag.write(topic, msg, t)
00059       num_messages = num_messages + 1
00060   outbag.close()
00061   print'    New output bagfile has', num_messages, 'messages'
00062 
00063 
00064 if __name__ == "__main__":
00065   parser = argparse.ArgumentParser(
00066       description='Cuts out a section from an input bagfile and writes it to an output bagfile')
00067   parser.add_argument('--inbag', help='input bagfile(s)', nargs='+', required=True)
00068   parser.add_argument('--outbag', help='output bagfile', required=True)
00069   parser.add_argument('--start', help='start time', type=float, required=True)
00070   parser.add_argument('--duration', help='duration of the resulting part', type=float, required=True)
00071   args = parser.parse_args()
00072   try:
00073     cut(args.inbag, args.outbag, args.start, args.duration)
00074   except Exception, e:
00075     import traceback
00076     traceback.print_exc()


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Fri Aug 28 2015 13:15:12