-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaver_general.py
More file actions
executable file
·136 lines (122 loc) · 4.56 KB
/
aver_general.py
File metadata and controls
executable file
·136 lines (122 loc) · 4.56 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
# Arnfinn Hykkerud Steindal, Tromso Apr 2010; May 2012
# Read a file (-i FILENAME) and make an average of
# values in the first row (if not given as -r ROW).
import sys
import math
from optparse import OptionParser
import re
def aver(mylist):
tot = 0.0
for i in mylist:
tot=tot+i
return tot/len(mylist)
def f(x,y):
return min(y-x, y-x+2*math.pi, y-x-2*math.pi, key=abs)
def degaver(mylist):
sini = 0.0
cosi = 0.0
for i in mylist:
sini = sini + math.sin(math.radians(i))
cosi = cosi + math.cos(math.radians(i))
val = math.degrees(math.atan2(sini/len(mylist),cosi/len(mylist)))
if val<0:
val = 360+val
return val
def degstdev(mylist):
ave=degaver(mylist)
avecos=math.cos(math.radians(ave))
avesin=math.sin(math.radians(ave))
stancos = 0.0
stansin = 0.0
stan = 0.0
for i in mylist:
degdiff = math.degrees(f(math.radians(i),math.radians(ave)))
stan = stan + degdiff*degdiff
return math.sqrt(stan/(len(mylist)-1))
def stdev(mylist):
ave = aver(mylist)
stan=0.0
for i in mylist:
stan=stan+(i-ave)*(i-ave)
return math.sqrt(stan/(len(mylist)-1))
def ev2nm(mylist):
newlist=[]
for i in mylist:
newlist.append(1240.0/i)
return newlist
parser = OptionParser()
parser.add_option("-i", "--input",dest="filename", help="the file to read")
parser.add_option("-r", "--row",type="int", dest="row", default=1, help="The row where numbers are located")
parser.add_option("-d", "--decimals",type="int", dest="dec", default=4, help="Number of decimals in the printet numbers")
parser.add_option("-p", "--print",action="store_true", default=False, dest="printing", help="Print all the values")
parser.add_option("--ev2nm",action="store_true", default=False, dest="ev2nm", help="Convert from eV to nm before calculating average")
parser.add_option("--av2",action="store_true", default=False, dest="aver2", help="Calculate the average of numbers where two are weighted")
parser.add_option("--deg",action="store_true", default=False, dest="deg", help="Calculate the average of degrees (problem with numbers around zero)")
parser.add_option("--simple",action="store_true", default=False, dest="simple", help="Simple print layout (average stdev)")
parser.add_option("--avnum",type="int", dest="avernum", default=0,help="Calculate the average of the N first values")
(options, args) = parser.parse_args()
file = open(options.filename,'r')
row = options.row-1
dec = str(options.dec)
deg = options.deg
if options.avernum>0:
lines = file.readlines()[0:options.avernum]
else:
lines = file.readlines()
tab=[]
for line in lines:
words = line.split()
if options.aver2:
oneex = float(re.sub("\D","",words[0].split(".")[0])+"."+re.sub("\D","",words[0].split(".")[1]))
onetr = float(re.sub("\D","",words[1].split(".")[0])+"."+re.sub("\D","",words[1].split(".")[1]))
twoex = float(re.sub("\D","",words[2].split(".")[0])+"."+re.sub("\D","",words[2].split(".")[1]))
twotr = float(re.sub("\D","",words[3].split(".")[0])+"."+re.sub("\D","",words[3].split(".")[1]))
ettall=str((oneex*onetr + twoex*twotr)/(onetr + twotr))
# print "two "+ettall
else:
try:
ettall = words[row]
except:
continue
if "(" in ettall:
ettall = ettall.replace("(","").replace(")","")
try:
test = float(ettall)
except:
# skip loop if ettall is not a number
continue
if float(ettall) < 0:
# dirty fix! negative numbers became positive in the re.sub stuff
ettall = float(ettall)
else:
# print "one " +ettall
try:
# the script did not eat 1.0e-4 kind of numbers
ettall = float(ettall)
except:
if "." in ettall:
ettall = float(re.sub("\D","",ettall.split(".")[0])+"."+re.sub("\D","",ettall.split(".")[1]))
# ettall = float(re.sub("\D","",words[row].split(".")[0])+"."+re.sub("\D","",words[row].split(".")[1]))
else:
ettall = float(re.sub("\D","",ettall))
# ettall = float(re.sub("\D","",words[row]))
if options.printing:
print ettall
tab.append(ettall)
if options.ev2nm:
tab = ev2nm(tab)
if deg:
ave=degaver(tab)
stan = degstdev(tab)
else:
ave = aver(tab)
stan = stdev(tab)
if options.simple:
print str(ave) + " " + str(stan)
else:
print "Number of values = "+str(len(tab))
string = "Average value = %."+dec+"f"
print string % (ave)
string = "Standard deviation = %."+dec+"f"
print string % (stan)