#!/usr/bin/env python import sys,os defthreshold = 20 debug = 0 minint = 10 try: ifname = sys.argv[1] except IndexError: print 'Usage: ',sys.argv[0],' inputfile [threshold]' sys.exit(1) try: threshold = int(sys.argv[2]) except IndexError: threshold = defthreshold except ValueError: print 'Usage: ',sys.argv[0],' inputfile [threshold]' sys.exit(1) try: ifile = open(ifname,'r') except IOError: print 'Error opening file ',ifname,' for input' sys.exit(1) lnr = 0 lastnr = 0 inr = 0 lastpeak = 0 while 1: lnr += 1 line = ifile.readline() # EOF if line == '': break # read a line try: newnr = int(line) except ValueError: print 'Parse error at line %d: %s' % (lnr,line) # compare with last value assuming there is a large value (i.e. not for the first line) if lastnr == 0: lastnr = newnr else: if (lastnr - newnr > threshold): if debug: print 'Peak at %d, previous: %d, new: %d' % (lnr,lastnr,newnr) # calculate interval from last peak if there was a previous peak if lastpeak == 0: lastpeak = lnr else: # ignore if two peaks are very close together (probably oscillation) if (lnr-lastpeak >= minint): inr += 1 print 'Interval %d: %d' % (inr,lnr-lastpeak) lastpeak = lnr