bag_helper.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2009, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
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
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICTS
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 """
00034 Helper functions for bag files and timestamps.
00035 """
00036 
00037 import time
00038 
00039 import rospy
00040 
00041 def stamp_to_str(t):
00042     """
00043     Convert a rospy.Time to a human-readable string.
00044     
00045     @param t: time to convert
00046     @type  t: rospy.Time
00047     """
00048     t_sec = t.to_sec()
00049     if t < rospy.Time.from_sec(60 * 60 * 24 * 365 * 5):
00050         # Display timestamps earlier than 1975 as seconds
00051         return '%.3fs' % t_sec
00052     else:
00053         return time.strftime('%b %d %Y %H:%M:%S', time.localtime(t_sec)) + '.%03d' % (t.nsecs / 1000000)
00054 
00055 def get_topics(bag):
00056     """
00057     Get an alphabetical list of all the unique topics in the bag.
00058     
00059     @return: sorted list of topics
00060     @rtype:  list of str
00061     """
00062     return sorted(set([c.topic for c in bag._get_connections()]))
00063 
00064 def get_start_stamp(bag):
00065     """
00066     Get the earliest timestamp in the bag.
00067     
00068     @param bag: bag file
00069     @type  bag: rosbag.Bag
00070     @return: earliest timestamp
00071     @rtype:  rospy.Time
00072     """
00073     start_stamp = None
00074     for connection_start_stamp in [index[0].time for index in bag._connection_indexes.values()]:
00075         if not start_stamp or connection_start_stamp < start_stamp:
00076             start_stamp = connection_start_stamp
00077     return start_stamp
00078 
00079 def get_end_stamp(bag):
00080     """
00081     Get the latest timestamp in the bag.
00082     
00083     @param bag: bag file
00084     @type  bag: rosbag.Bag
00085     @return: latest timestamp
00086     @rtype:  rospy.Time
00087     """
00088     end_stamp = None
00089     for connection_end_stamp in [index[-1].time for index in bag._connection_indexes.values()]:
00090         if not end_stamp or connection_end_stamp > end_stamp:
00091             end_stamp = connection_end_stamp
00092 
00093     return end_stamp
00094 
00095 def get_topics_by_datatype(bag):
00096     """
00097     Get all the message types in the bag and their associated topics.
00098     
00099     @param bag: bag file
00100     @type  bag: rosbag.Bag
00101     @return: mapping from message typename to list of topics
00102     @rtype:  dict of str to list of str
00103     """
00104     topics_by_datatype = {}
00105     for c in bag._get_connections():
00106         topics_by_datatype.setdefault(c.datatype, []).append(c.topic)
00107     
00108     return topics_by_datatype
00109 
00110 def get_datatype(bag, topic):
00111     """
00112     Get the datatype of the given topic.
00113     
00114     @param bag: bag file
00115     @type  bag: rosbag.Bag
00116     @return: message typename
00117     @rtype:  str
00118     """
00119     for c in bag._get_connections(topic):
00120         return c.datatype
00121 
00122     return None


rxbag
Author(s): Tim Field
autogenerated on Mon Oct 6 2014 07:26:07