Skip to content

Instantly share code, notes, and snippets.

@NikolausDemmel
Last active October 31, 2022 06:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save NikolausDemmel/8944211 to your computer and use it in GitHub Desktop.
Save NikolausDemmel/8944211 to your computer and use it in GitHub Desktop.
Add license
# BSD 2-Clause License
#
# Copyright (c) 2014-2019, Nikolaus Demmel
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
INBAG=$1
OUTBAG=$2
STARTTIME=$3
DURATION=$4
ENDTIME="($STARTTIME + $DURATION)"
rosbag filter $INBAG $OUTBAG \
"((
( (topic == '/camera/image_mono') or (topic == 'camera/image_mono') or
(topic == '/camera/camera_info') or (topic == 'camera/camera_info') or
(topic == '/bosch_imu/imu0/data_raw') or (topic == 'bosch_imu/imu0/data_raw') or
(topic == '/ps') or (topic == 'ps') or
(topic == '/xsens_imu/imu/data') or (topic == 'xsens_imu/imu/data') or
(topic == '/imu/data') or (topic == 'imu/data') or
(topic == '/vps_pose_covariance') or (topic == 'vps_pose_covariance') or
(topic == '/vps_pose_covariance_camera') or (topic == 'vps_pose_covariance_camera') or
(topic == '/vps_pose_covariance_imu') or (topic == 'vps_pose_covariance_imu') )
and
(m.header.stamp.to_sec() >= $STARTTIME and m.header.stamp.to_sec() < $ENDTIME)
) or
(
( (topic == '/richimage') or (topic == 'richimage') )
and
(m.image.header.stamp.to_sec() >= $STARTTIME and m.image.header.stamp.to_sec() < $ENDTIME)
) or
(
( (topic == '/camera_measurement_info') or (topic == 'camera_measurement_info') )
and
(m.measurement.richimage.image.header.stamp.to_sec() >= $STARTTIME and m.measurement.richimage.image.header.stamp.to_sec() < $ENDTIME)
) or
(
( (topic == '/tf') or (topic == '') )
and
(m.transforms[0].header.stamp.to_sec() >= $STARTTIME and m.transforms[0].header.stamp.to_sec() < $ENDTIME)
))"
#!/usr/bin/env python
# BSD 2-Clause License
#
# Copyright (c) 2014-2019, Nikolaus Demmel
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import argparse
from fnmatch import fnmatchcase
from rosbag import Bag
def main():
parser = argparse.ArgumentParser(description='Merge one or more bag files
with the possibilities of filtering topics.')
parser.add_argument('outputbag',
help='output bag file with topics merged')
parser.add_argument('inputbag', nargs='+',
help='input bag files')
parser.add_argument('-v', '--verbose', action="store_true", default=False,
help='verbose output')
parser.add_argument('-t', '--topics', default="*",
help='string interpreted as a list of topics (wildcards \'*\' and \'?\' allowed) to include in the merged bag file')
args = parser.parse_args()
topics = args.topics.split(' ')
total_included_count = 0
total_skipped_count = 0
if (args.verbose):
print("Writing bag file: " + args.outputbag)
print("Matching topics against patters: '%s'" % ' '.join(topics))
with Bag(args.outputbag, 'w') as o:
for ifile in args.inputbag:
matchedtopics = []
included_count = 0
skipped_count = 0
if (args.verbose):
print("> Reading bag file: " + ifile)
with Bag(ifile, 'r') as ib:
for topic, msg, t in ib:
if any(fnmatchcase(topic, pattern) for pattern in topics):
if not topic in matchedtopics:
matchedtopics.append(topic)
if (args.verbose):
print("Including matched topic '%s'" % topic)
o.write(topic, msg, t)
included_count += 1
else:
skipped_count += 1
total_included_count += included_count
total_skipped_count += skipped_count
if (args.verbose):
print("< Included %d messages and skipped %d" % (included_count, skipped_count))
if (args.verbose):
print("Total: Included %d messages and skipped %d" % (total_included_count, total_skipped_count))
if __name__ == "__main__":
main()
@josephduchesne
Copy link

Hi Nikolaus,

What license is this under? Do you mind releasing it as BSD 3-clause? (the most common one used by ROS projects)?

@NikolausDemmel
Copy link
Author

Sure, I added a BSD 2-clause license, since its even simpler. I hope that works for you.

@josephduchesne
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment