1#!/usr/bin/python 2 3import sys 4import getopt 5 6def usage(): 7 print "powersum.py [OPTIONS] HZ VOLTAGE [FILE]" 8 print "OPTIONS: " 9 print "-o OFFSET: subtract OFFSET from all data points" 10 print "\nHZ: samples per second in FILE or stdin" 11 sys.exit(0) 12 13offset = 0.0 14voltage = 4.3 15 16parsedargv,argvrem = getopt.getopt(sys.argv[1:], "vo:w:l:h", ["help"]) 17for o,a in parsedargv: 18 if o == '-o': offset = float(a) 19 if o == '-h' or o == '--help': usage() 20 21hz = float(argvrem[0]) 22voltage = float(argvrem[1]) 23if len(argvrem) > 1: 24 f = open(argvrem[2], "r") 25else: 26 f = sys.stdin 27 28totalpower = 0.0 29samplectr = 0 30 31for line in f: 32 try: 33 val = float(line.split(" ")[1]) # xxx take 2nd arg in line 34 val -= offset 35 except: 36 print "Can't parse data line, did you remember the timestamp?" 37 print "data was: %s" % line 38 sys.exit(1) 39 40 samplectr+=1 41 totalpower += val/hz 42 43avecurrent = totalpower * hz *1000 / samplectr 44avepower = avecurrent * voltage 45 46print "%.3f %.3f" % (avecurrent, avepower) 47