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     rospy.loginfo('   Looking for smallest time in: %s', 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   rospy.loginfo('   Bagfiles start at %s', start_time)
00050   start_time = start_time + rospy.Duration.from_sec(start)
00051   end_time = start_time + rospy.Duration.from_sec(duration)
00052   rospy.loginfo('   Cutting out from %s to %s',start_time, end_time)
00053   outbag = rosbag.Bag(outbagfile, 'w')
00054   num_messages = 0
00055   for inbag in inbags:
00056     rospy.loginfo('   Extracting messages from: %s', 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   rospy.loginfo('     New output bagfile has %s messages', num_messages)
00062 
00063 
00064 if __name__ == "__main__":
00065   rospy.init_node('cut')
00066   parser = argparse.ArgumentParser(
00067       description='Cuts out a section from an input bagfile and writes it to an output bagfile')
00068   parser.add_argument('--inbag', help='input bagfile(s)', nargs='+', required=True)
00069   parser.add_argument('--outbag', help='output bagfile', required=True)
00070   parser.add_argument('--start', help='start time', type=float, required=True)
00071   parser.add_argument('--duration', help='duration of the resulting part', type=float, required=True)
00072   args = parser.parse_args()
00073   try:
00074     cut(args.inbag, args.outbag, args.start, args.duration)
00075   except Exception, e:
00076     import traceback
00077     traceback.print_exc()


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Sat Jun 8 2019 20:10:13