Last active
December 20, 2015 02:09
-
-
Save wiesson/6054747 to your computer and use it in GitHub Desktop.
Python script to find, combine and calculate the average values of csv data for the combustion engine intern/course at the at the University of Bayreuth. Usage as follows: python calc_average.py /folder list. Next, accept with (y) yes, or deny with (n) - Make sure you made a copy of the files before!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# usage: python calc_average.py folder list | |
# y / n | |
import os, sys | |
from decimal import * | |
def csv_average(file_input, file_folder, amount): | |
file_name = file_input | |
file_amount = amount | |
file_folder = file_folder | |
suffix = ".csv" | |
output_file = file_name+suffix | |
li = [] | |
for files in range(file_amount): | |
if files < 9: | |
li.append(file_folder+"/"+file_name+"_0"+str(files+1)+suffix) | |
else: | |
li.append(file_folder+"/"+file_name+"_"+str(files+1)+suffix) | |
if os.path.exists(output_file): | |
f = file(output_file, "r+") | |
else: | |
f = file(output_file, "w+") | |
li_values = [[] for i in range(int(file_amount))] | |
# load csv to list | |
for item in range(len(li)): | |
f = open(li[item]) | |
y = f.readlines() | |
for item_val in range(len(y)): | |
z = y[item_val].split(',') | |
if len(z) is not 2: | |
pass | |
else: | |
try: | |
li_values[item].append( | |
[ | |
[float(z[0])], | |
[float(z[1])] | |
]) | |
except: | |
pass | |
print "> processing: %s" % li[item] | |
li_average = [ | |
[],[] | |
] | |
el1 = 0.00 | |
el2 = 0.00 | |
len_values = len(li_values) | |
# 7818 | |
for element in range(len(li_values[0])): | |
# 10 | |
for item in range(len_values): | |
el1 = el1 + li_values[item][element][0][0] | |
el2 = el2 + li_values[item][element][1][0] | |
#print element, el1/file_amount, el2/file_amount | |
li_average[0].append(float(el1/len_values)) | |
li_average[1].append(float(el2/len_values)) | |
el1 = 0.00 | |
el2 = 0.00 | |
fo = open(output_file, 'w+') | |
fo.write("Zeit, Kanal A") | |
fo.write("\n") | |
fo.write("(ms), (V)") | |
fo.write("\n") | |
for items in range(len(li_average[0])): | |
fo.write("%s" % li_average[0][items]) | |
fo.write(",") | |
fo.write("%s" % li_average[1][items]) | |
fo.write("\n") | |
print "done" | |
print "saved as: %s" % output_file | |
def main(): | |
if len(sys.argv) > 1: | |
file_folder = sys.argv[1] | |
file_input = sys.argv[2] | |
file_names_temp = [] | |
file_names = [] | |
file_count = 0 | |
if sys.argv[2] == "list": | |
for dirname, dirnames, filenames in os.walk(file_folder): | |
for filename in filenames: | |
file_names_temp.append(filename[:-7]) | |
# print os.path.join(dirname, filename) | |
for item in file_names_temp: | |
if item not in file_names: | |
file_names.append(item) | |
file_count = 0 | |
else: | |
file_count = file_count + 1 | |
file_count = file_count + 1 | |
input = raw_input("found %i items, do average calculation for each file? (y) / (n): " % len(file_names)) | |
if input == "y": | |
for item in file_names: | |
print "----" | |
print item | |
csv_average(item, file_folder, file_count) | |
if input == "n": | |
print "bye" | |
exit(0) | |
else: | |
print "no file name" | |
exit | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment