Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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