#!@PYTHON@
import sys,math
wrerr=sys.stderr.write
wrout=sys.stdout.write
# get arguments
if (len(sys.argv) < 3):
print "Usage: "+sys.argv[0]+"
"
sys.exit(1)
else:
dir=sys.argv[1]
data=sys.argv[2]
length=float(sys.argv[3])
input=open(dir+"/"+data,"r")
abc=[]
for line in input:
if "#" in line:
continue
entries=line.split()
for n in range(3):
abc.append(abs(float(entries[n+4])))
input.close()
# simply output all sorted pairs
wrout("Output of sorted data pairs ... ")
output = open(dir+"/"+data+".out", "w")
output.write("#Nr. absolute value\n")
abc.sort()
for i in range(len(abc)):
output.write("%d\t %lg\n" % (i, abc[i]))
output.close()
wrout("done.\n")
# then bin and output
nr=int(((abc[-1])/length)+0.5)
bin=[] # contains list of values
print "Number of bins "+str(nr)+" with length "+str(length)+"."
# initialise bins with 0
wrout("Gathering into bins ... ")
for i in range(nr):
bin.append([0])
# go through all values and increase bin
for i in range(len(abc)):
index=int(((abc[i])/length)-0.5)
bin[index][0]+=1
bin[index].append(abc[i])
#print ("%lg goes into bin %d, containg %d." % (abc[i], index, bin[index][0]))
wrout("done.\n")
wrout("Output of bins to file ... ")
output = open(dir+"/"+data+".bin","w")
output.write("#Bin -- length: %lg\t -- count\taverage\terror\n" % (length))
max=[0, 0, 0]
MAX=[-1,-1,-1]
for i in range(nr):
# determine greatest three bins
for n in range(3):
if (bin[i][0]>max[n]):
for m in range(n,2):
max[m+1] = max[m]
MAX[m+1] = MAX[m]
max[n] = bin[i][0]
MAX[n] = i
break
# calculate average and error per bin
average=0
error=0
if bin[i][0] != 0:
for m in range(1,bin[i][0]+1):
average+=bin[i][m]*bin[i][m]
average=math.sqrt(average/bin[i][0])
for m in range(1,bin[i][0]+1):
tmp=bin[i][m]-average
error+=tmp*tmp
error=math.sqrt(error)/bin[i][0]
#output
output.write("%d\t %lg\t%lg\t%lg\n" % (i, bin[i][0], average, error))
output.close()
wrout("done.\n")
# output average of each of the three greatest bins
for n in range(3):
average=0
error=0
if bin[ MAX[n] ][0] != 0:
for m in range(1,bin[ MAX[n] ][0]+1):
average+=bin[ MAX[n] ][m]*bin[ MAX[n] ][m]
average=math.sqrt(average/bin[ MAX[n] ][0])
for m in range(1,bin[ MAX[n] ][0]+1):
tmp=bin[ MAX[n] ][m]-average
error+=tmp*tmp
error=math.sqrt(error)/bin[ MAX[n] ][0]
print "Bin "+str(MAX[n])+" has "+str(max[n])+" values, average is "+str(average)+" +/- "+str(error)+"."