8 from optparse
import OptionParser
10 from datetime
import datetime
20 for s
in type(msg).__slots__:
21 val = msg.__getattribute__(s)
25 if msg_str.find(
",")
is not -1:
27 msg_str = msg_str.strip(
"(")
28 msg_str = msg_str.strip(
")")
29 msg_str = msg_str.strip(
" ")
31 msg_str =
"\"" + msg_str +
"\"" 32 stream.write(
"," + msg_str)
40 for s
in type(msg).__slots__:
41 val = msg.__getattribute__(s)
42 if flatten
and type(val) == tuple:
43 for i
in range(len(val)):
44 stream.write(
"," +
".".join([parent_content_name, s, str(i)]))
46 stream.write(
"," +
".".join([parent_content_name, s]))
48 stream.write(
"," + parent_content_name)
51 nowtime = datetime.now().strftime(
"%Y%m%d-%H%M%S")
55 ret = form.replace(
'%t', topic_name.replace(
'/',
'-'))
56 ret = ret.replace(
'%s', str(seq))
58 ret = ret.replace(
'%d', nowtime)
59 ret = ret.replace(
'%u', str(uuid.uuid4()))
67 if options.start_time:
68 stime = rospy.Time(options.start_time)
71 etime = rospy.Time(options.end_time)
72 except Exception
as e:
73 rospy.logfatal(
'failed to load bag file: %s', e)
77 for topic, msg, time
in bag.read_messages(topics=options.topic_names,
80 if streamdict.has_key(topic):
81 stream = streamdict[topic]
84 streamdict[topic] = stream
91 stream.write(datetime.fromtimestamp(time.to_time()).strftime(
'%Y/%m/%d/%H:%M:%S.%f'))
94 [s.close
for s
in streamdict.values()]
95 except Exception
as e:
96 rospy.logwarn(
"fail: %s", e)
100 if __name__ ==
'__main__':
101 rospy.init_node(
'bag2csv', anonymous=
True)
102 parser = OptionParser(usage=
"%prog [options] bagfile")
103 parser.add_option(
"-a",
"--all", dest=
"all_topics",
105 help=
"exports all topics", default=
False)
106 parser.add_option(
"-t",
"--topic", dest=
"topic_names",
108 help=
"white list topic names", metavar=
"TOPIC_NAME")
109 parser.add_option(
"-O",
"--output", dest=
"output_file_format",
110 help=
"output file names format\n%t: topic name\n%s: sequential number\n%d: datetime (now)\n%u: uuid\ne.g.: -O jskbag-$t-$d.csv", metavar=
"DESTINATION")
111 parser.add_option(
"-s",
"--start-time", dest=
"start_time",
112 help=
"start time of bagfile", type=
"float")
113 parser.add_option(
"-e",
"--end-time", dest=
"end_time",
114 help=
"end time of bagfile", type=
"float")
115 parser.add_option(
"-n",
"--no-header", dest=
"header",
116 action=
"store_false", default=
True,
117 help=
"no header / flatten array value")
118 parser.add_option(
"-f",
"--flatten", dest=
"flatten",
120 help=
"output the list type topic with flatten format", default=
False)
121 (options, args) = parser.parse_args()
def message_type_to_csv(stream, msg, parent_content_name="", flatten=False)
def format_csv_filename(form, topic_name)
def message_to_csv(stream, msg, flatten=False)
def bag_to_csv(options, args)