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 math
00038 import time
00039 import rospy
00040
00041
00042 def stamp_to_str(t):
00043 """
00044 Convert a rospy.Time to a human-readable string.
00045
00046 @param t: time to convert
00047 @type t: rospy.Time
00048 """
00049 t_sec = t.to_sec()
00050 if t < rospy.Time.from_sec(60 * 60 * 24 * 365 * 5):
00051
00052 return '%.3fs' % t_sec
00053 else:
00054 return time.strftime('%b %d %Y %H:%M:%S', time.localtime(t_sec)) + '.%03d' % (t.nsecs / 1000000)
00055
00056
00057 def get_topics(bag):
00058 """
00059 Get an alphabetical list of all the unique topics in the bag.
00060
00061 @return: sorted list of topics
00062 @rtype: list of str
00063 """
00064 return sorted(set([c.topic for c in bag._get_connections()]))
00065
00066
00067 def get_start_stamp(bag):
00068 """
00069 Get the earliest timestamp in the bag.
00070
00071 @param bag: bag file
00072 @type bag: rosbag.Bag
00073 @return: earliest timestamp
00074 @rtype: rospy.Time
00075 """
00076 start_stamp = None
00077 for connection_start_stamp in [index[0].time for index in bag._connection_indexes.values()]:
00078 if not start_stamp or connection_start_stamp < start_stamp:
00079 start_stamp = connection_start_stamp
00080 return start_stamp
00081
00082
00083 def get_end_stamp(bag):
00084 """
00085 Get the latest timestamp in the bag.
00086
00087 @param bag: bag file
00088 @type bag: rosbag.Bag
00089 @return: latest timestamp
00090 @rtype: rospy.Time
00091 """
00092 end_stamp = None
00093 for connection_end_stamp in [index[-1].time for index in bag._connection_indexes.values()]:
00094 if not end_stamp or connection_end_stamp > end_stamp:
00095 end_stamp = connection_end_stamp
00096
00097 return end_stamp
00098
00099
00100 def get_topics_by_datatype(bag):
00101 """
00102 Get all the message types in the bag and their associated topics.
00103
00104 @param bag: bag file
00105 @type bag: rosbag.Bag
00106 @return: mapping from message typename to list of topics
00107 @rtype: dict of str to list of str
00108 """
00109 topics_by_datatype = {}
00110 for c in bag._get_connections():
00111 topics_by_datatype.setdefault(c.datatype, []).append(c.topic)
00112
00113 return topics_by_datatype
00114
00115
00116 def get_datatype(bag, topic):
00117 """
00118 Get the datatype of the given topic.
00119
00120 @param bag: bag file
00121 @type bag: rosbag.Bag
00122 @return: message typename
00123 @rtype: str
00124 """
00125 for c in bag._get_connections(topic):
00126 return c.datatype
00127
00128 return None
00129
00130
00131 def filesize_to_str(size):
00132 size_name = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
00133 i = int(math.floor(math.log(size, 1024)))
00134 p = math.pow(1024, i)
00135 s = round(size / p, 2)
00136 if s > 0:
00137 return '%s %s' % (s, size_name[i])
00138 return '0 B'