00001 #!/usr/bin/python 00002 # 00003 # Software License Agreement (BSD License) 00004 # 00005 # Copyright (c) 2008, Willow Garage, Inc. 00006 # All rights reserved. 00007 # 00008 # Redistribution and use in source and binary forms, with or without 00009 # modification, are permitted provided that the following conditions 00010 # are met: 00011 # 00012 # * Redistributions of source code must retain the above copyright 00013 # notice, this list of conditions and the following disclaimer. 00014 # * Redistributions in binary form must reproduce the above 00015 # copyright notice, this list of conditions and the following 00016 # disclaimer in the documentation and/or other materials provided 00017 # with the distribution. 00018 # * Neither the name of the Willow Garage nor the names of its 00019 # contributors may be used to endorse or promote products derived 00020 # from this software without specific prior written permission. 00021 # 00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 # POSSIBILITY OF SUCH DAMAGE. 00034 00035 ##\author Kevin Watts 00036 ##\brief Make CSV files smaller for use in spreadsheet software 00037 00038 PKG = 'diagnostic_analysis' 00039 import roslib 00040 roslib.load_manifest(PKG) 00041 00042 import csv, os, sys 00043 00044 ##\brief Makes sparse CSV by skipping every nth value 00045 ##\param csv_file str : CSV filename 00046 ##\param skip int : Write every nth row to sparse CSV 00047 ##\return Path of output file 00048 def make_sparse_skip(csv_file, skip): 00049 output_file = csv_file[:-4] + '_sparse.csv' 00050 00051 input_reader = csv.reader(open(csv_file, 'rb')) 00052 00053 f = open(output_file, 'wb') 00054 output_writer = csv.writer(f) 00055 00056 skip_count = skip 00057 for row in input_reader: 00058 if skip_count == skip: 00059 output_writer.writerow(row) 00060 skip_count = 0 00061 00062 skip_count = skip_count + 1 00063 00064 return output_file 00065 00066 ##\brief Makes sparse CSV with the given number of rows 00067 ##\param csv_file str : CSV filename 00068 ##\param length int : Desired number of rows in CSV 00069 ##\return Path of output file 00070 def make_sparse_length(csv_file, length): 00071 output_file = csv_file[:-4] + '_sprs_len.csv' 00072 00073 input_reader = csv.reader(open(csv_file, 'rb')) 00074 00075 f = open(output_file, 'wb') 00076 output_writer = csv.writer(f) 00077 00078 # Calculate skip count for file 00079 orig_len = len(open(csv_file, 'r').read().split('\n')) 00080 skip = max(int(orig_len / length), 1) 00081 00082 skip_count = skip 00083 for row in input_reader: 00084 if skip_count >= skip: 00085 output_writer.writerow(row) 00086 skip_count = 0 00087 00088 skip_count = skip_count + 1 00089 00090 return output_file