forked from itamblyn/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunning_average.py
executable file
·61 lines (46 loc) · 1.4 KB
/
running_average.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import os, sys
import numpy as np
def example_function(a,b,c=17.5,d=True):
"""
variables that do not
"""
# Now just code in the function
sum = a+b+c
if d:
print 'd = True'
else:
print 'd != True'
return sum
def main():
"""
read an array from file, compute the running average, write to disk
"""
# A clean way to ask for user input
try:
# Attempt to retrieve required input from user
prog = sys.argv[0]
inputFilename = sys.argv[1]
stride = int(sys.argv[2])
except IndexError:
print '\nusage: '+prog+' inputFilename\n'
sys.exit(0)
# Execute the function defined above
inputFile = open(inputFilename,'r')
x = []
y = []
for line in inputFile.readlines():
if line.split() != '#': # looks for header
x.append(float(line.split()[0]))
y.append(float(line.split()[1]))
outputFile = open('raverage.dat','w')
# for i in np.arange(stride, len(y) - stride):
for i in np.arange(0, len(y)):
if i > stride or i > len(y) - stride:
outputFile.write(str(x[i]) +' '+str(np.average(y[(i-stride):(i+stride+1)]))+'\n')
else:
outputFile.write(str(x[i]) +' '+str(y[i])+'\n')
outputFile.close()
# This executes main() only if executed from shell
if __name__ == '__main__':
main()