def phi_nie(sigmav,x1,x2,qe,xcore,z1,z2):
    scr = mm.sigma_crit(z1,z2)
    x = np.sqrt(xcore*xcore+x1*x1*(1.0-qe)+x2*x2*(1.0+qe))
    #res2 = Dds/Ds*4.0*np.pi*sigmav**2.0/vc**2.0*mm.apr
    ac = sigmav**2.0*mm.apr/(mm.G*mm.Da(z1)*scr)
    res = ac*x
    return res
def leq_nie_zeros(sigmav,x1,x2,qe,xcore,z1,z2,y10,y20):
    scr = mm.sigma_crit(z1,z2+1e-7)
    x = np.sqrt(xcore*xcore+x1*x1*(1.0-qe)+x2*x2*(1.0+qe))
    ac = sigmav**2.0*mm.apr/(mm.G*mm.Da(z1)*scr)
    res1 = ac*(1-qe)*x1/x
    res2 = ac*(1+qe)*x2/x
    return x1-res1-y10,x2-res2-y20
def alpha_nie(sigmav,x1,x2,qe,xcore,z1,z2):
    scr = mm.sigma_crit(z1,z2+1e-7)
    x = np.sqrt(xcore*xcore+x1*x1*(1.0-qe)+x2*x2*(1.0+qe))
    ac = sigmav**2.0*mm.apr/(mm.G*mm.Da(z1)*scr)
    res1 = ac*(1-qe)*x1/x
    res2 = ac*(1+qe)*x2/x
    return res1,res2
def kappa_nie(sigmav,x1,x2,qe,xcore,z1,z2):
    scr = mm.sigma_crit(z1,z2)
    x = np.sqrt(xcore*xcore+x1*x1*(1.0-qe)+x2*x2*(1.0+qe))
    ac = sigmav**2.0*mm.apr/(mm.G*mm.Da(z1)*scr)
    res = ac/(2.0*x)
    return res
def get_halo_cc(om10_id):
    db = om10.DB(catalog="./qso_mock.fits")

    lens = db.get_lens(om10_id)
    zl = lens.ZLENS[0]
    zs = lens.ZSRC[0]
    qe = 1.0 / (1.0 - lens.ELLIP[0])
    phi = lens.PHIE[0]
    vd0 = lens.VELDISP[0]  # needed from OM10
    mtotal0 = aa.m200_sv(vd0, zl)  # needed from OM10
    #----------------------------------------------------------------------
    # index, snapid,haloid,subhid,ag_stellar,ag_total,eq_stellar,eq_total,ms_stellar,ms_total,vd_stellar
    #
    snapid, haloid, subhid, ag_total, eq_stellar, ms_total, vd_stellar = np.loadtxt(
        "../produce_e_catalogs/results.dat",
        usecols=(1, 2, 3, 5, 6, 9, 10),
        unpack=True)

    print "Input: ", qe, phi, vd0, mtotal0

    nhalos = len(subhid)

    ksi_om10 = (ag_total/phi-1.0)**2.0/nhalos*0.0+(eq_stellar/qe-1.0)**2.0/nhalos*0.4 \
             + (ms_total/mtotal0-1.0)**2.0/nhalos*0.01+(vd_stellar*(1.0+zl)/vd0-1.0)**2.0/nhalos*100.4

    idx = ksi_om10 == np.min(ksi_om10)
    print "Matched: ", eq_stellar[idx], ag_total[idx], vd_stellar[
        idx], ms_total[idx]

    snapid_n = int(snapid[idx])
    #haloid_n = int(haloid[idx])
    subhid_n = int(subhid[idx])

    bsz = 30.0  # ckpc
    nnn = 512

    stars = il.snapshot.loadSubhalo(basePath, snapid_n, subhid_n, 'stars')
    x1 = stars['Coordinates'][:, 0]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x2 = stars['Coordinates'][:, 1]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x3 = stars['Coordinates'][:, 2]  #/1e3/(1.0+zl) # Mpc/h, comoving

    ptnl_p = stars['Potential']
    idx_ptnl_p_min = ptnl_p == ptnl_p.min()

    xcp1 = x1[idx_ptnl_p_min]
    xcp2 = x2[idx_ptnl_p_min]
    xcp3 = x3[idx_ptnl_p_min]

    (x1new, x2new) = trf.xy_rotate(x1, x2, xcp1, xcp2, -14.0)
    sdens = read_sdens_from_illustris(stars, x1new, x2new, x3 - xcp3,
                                      bsz / 512 * 10.0, nnn) * (1.0 + zl)**2.0

    kappa = sdens / mm.sigma_crit(zl, zs)

    bsz = bsz / 1e3 / mm.Dc(zl) * mm.apr  #arcsec
    dsx = bsz / nnn  #arcsec

    xi1, xi2 = make_r_coor(nnn, dsx)
    ai1, ai2 = pcs.call_cal_alphas0(sdens, nnn, bsz, zl, zs)

    mua = pcs.call_alphas_to_mu(ai1, ai2, nnn, dsx)
    return xi1, xi2, ai1, ai2, mua, kappa