import numpy as np
import sys
# by Garance Lancaster
def ellipticity(shear_cmpl1,shear_cmpl2,eps_int_cmplx1,eps_int_cmplx2):
            
    shear_cmplx = shear_cmpl1 + shear_cmpl2*(0+1j) #value of the shear                           
    eps_int_cmplx = eps_int_cmplx1 + eps_int_cmplx2*(0+1j) #value of the IA/noise to be added                                    
    eps_obs_cmplx = (shear_cmplx + eps_int_cmplx)/(1.0 + eps_int_cmplx*shear_cmplx.conj()) #combinaison of both signal
    eps_obs1 = eps_obs_cmplx.real #real part    
    eps_obs2= eps_obs_cmplx.imag #imaginary part                                                            
    return(eps_obs1,eps_obs2)

A_IA=float(sys.argv[1])
C2=float(sys.argv[2])
filename_in=sys.argv[3]
filename_out=sys.argv[4]

print("Combining ellipticities from catalogue", filename_in)


#loading of catalogues 
cat=np.loadtxt(filename_in)
x=cat[:,0]
y=cat[:,1]
g1=cat[:,3]
g2=cat[:,4]
e1_deltaNLA=cat[:,9]
e2_deltaNLA=cat[:,10]
e1_TT=cat[:,11]
e2_TT=cat[:,12]
e1_IA = A_IA*e1_deltaNLA + C2*e1_TT
e2_IA = A_IA*e2_deltaNLA + C2*e2_TT

e1_obs, e2_obs = ellipticity(g1,g2,e1_IA, e2_IA)
results = np.array([x, y, e1_obs, e2_obs]);
print(np.shape(x), np.shape(results))
np.savetxt(filename_out,results.T)

