#!/usr/bin/env python
from numpy import *
import numpy as np
import math
from mpmath import *
from scipy.optimize import curve_fit
import scipy.integrate
import scipy
import glob
import os.path
import sys
import os
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import MultipleLocator
from matplotlib.ticker import ScalarFormatter
from matplotlib.ticker import NullFormatter
import numpy.ma as ma
import matplotlib.lines as lines
from matplotlib.lines import Line2D

def read_comm(filename1,filename2,filename3):
	sl = np.zeros(shape=4)
	su = np.zeros(shape=4)
	sr = np.zeros(shape=4)
	trajl = np.zeros(shape=4)
	traju = np.zeros(shape=4)
	trajr = np.zeros(shape=4)
	f=open(filename1,'r')
	ind = 0
	for line in f:
		p = line.split()
		sl[ind] = float(p[4])
		trajl[ind] = float(p[5])
		ind += 1
	f.close()
	f=open(filename2,'r')
	ind = 0
	for line in f:
		p = line.split()
		su[ind] = float(p[4])
		traju[ind] = float(p[5])
		ind += 1
	f.close()
	f=open(filename3,'r')
	ind = 0
	for line in f:
		p = line.split()
		sr[ind] = float(p[4])
		trajr[ind] = float(p[5])
		ind += 1
	f.close()
	return sl,su,sr,trajl,traju,trajr

def main():
	sl1,su1,sr1,trajl1,traju1,trajr1 = read_comm("comm_left_exp_1.dat","comm_up_exp_1.dat","comm_right_exp_1.dat")
	print(np.sum(trajl1+traju1+trajr1))
	sl2,su2,sr2,trajl2,traju2,trajr2 = read_comm("comm_left_exp_2.dat","comm_up_exp_2.dat","comm_right_exp_2.dat")
	print(np.sum(trajl2+traju2+trajr2))
	sl3,su3,sr3,trajl3,traju3,trajr3 = read_comm("comm_left_exp_3.dat","comm_up_exp_3.dat","comm_right_exp_3.dat")
	print(np.sum(trajl3+traju3+trajr3))
	sl4,su4,sr4,trajl4,traju4,trajr4 = read_comm("comm_left_exp_4.dat","comm_up_exp_4.dat","comm_right_exp_4.dat")
	print(np.sum(trajl4+traju4+trajr4))
	sl5,su5,sr5,trajl5,traju5,trajr5 = read_comm("comm_left_exp_5.dat","comm_up_exp_5.dat","comm_right_exp_5.dat")
	print(np.sum(trajl5+traju5+trajr5))
	sltot = sl1+sl2+sl3+sl4+sl5
	sutot = su1+su2+su3+su4+su5
	srtot = sr1+sr2+sr3+sr4+sr5
	trajltot = trajl1+trajl2+trajl3+trajl4+trajl5
	trajutot = traju1+traju2+traju3+traju4+traju5
	trajrtot = trajr1+trajr2+trajr3+trajr4+trajr5
	# ~ print(sltot)
	# ~ print(sutot)
	# ~ print(srtot)
	# ~ print(trajltot)
	# ~ print(trajutot)
	# ~ print(trajrtot)
	ql = sltot/trajltot
	qu = sutot/trajutot
	qr = srtot/trajrtot
	print(ql)
	print(qu)
	print(qr)
	stdl = np.zeros(shape=4)
	stdu = np.zeros(shape=4)
	stdr = np.zeros(shape=4)
	el = np.zeros(shape=4)
	eu = np.zeros(shape=4)
	er = np.zeros(shape=4)
	erl = np.zeros(shape=4)
	eru = np.zeros(shape=4)
	err = np.zeros(shape=4)
	i = 0
	while i<4:
		stdl[i] = np.sqrt(( sltot[i]*(1-ql[i])**2 + (trajltot[i]-sltot[i])*(0-ql[i])**2 )/(trajltot[i]))
		stdu[i] = np.sqrt(( sutot[i]*(1-qu[i])**2 + (trajutot[i]-sutot[i])*(0-qu[i])**2 )/(trajutot[i]))
		stdr[i] = np.sqrt(( srtot[i]*(1-qr[i])**2 + (trajrtot[i]-srtot[i])*(0-qr[i])**2 )/(trajrtot[i]))
		el[i] = stdl[i]/np.sqrt(trajltot[i])
		eu[i] = stdu[i]/np.sqrt(trajutot[i])
		er[i] = stdr[i]/np.sqrt(trajrtot[i])
		erl[i] = el[i]/ql[i]
		eru[i] = eu[i]/qu[i]
		err[i] = er[i]/qr[i]
		i+=1
	print(erl)
	print(eru)
	print(err)
	
	f=open("comm_left5.dat",'w')
	i = 0
	while i < 4:
		f.write(str(ql[i]))
		f.write("\t")
		f.write(str(el[i]))
		f.write("\n")
		i+=1
	f.close()
	
	f=open("comm_up5.dat",'w')
	i = 0
	while i < 4:
		f.write(str(qu[i]))
		f.write("\t")
		f.write(str(eu[i]))
		f.write("\n")
		i+=1
	f.close()
	
	f=open("comm_right5.dat",'w')
	i = 0
	while i < 4:
		f.write(str(qr[i]))
		f.write("\t")
		f.write(str(er[i]))
		f.write("\n")
		i+=1
	f.close()
	
main()
