def run_main(lens_cat, lgal_cat, sagn_cat, sgal_cat):

    # lens_cat = {}
    # lgal_cat = {}
    # sagn_cat = {}
    # sgal_cat = {}

    bsz = 10.0  # arcsec
    nnn = 512
    dsx = bsz / nnn  #arcsec
    dsi = 0.03

    zl = lens_cat['zl']
    zs = sagn_cat['zs']

    xc1 = lens_cat['xc1']
    xc2 = lens_cat['xc2']
    re = re_sv(lens_cat['SigmaV'], zl, zs)
    rc = 0.0
    ql = lens_cat['ql']
    phl = lens_cat['phl']
    esh = lens_cat['ext_shear']
    ash = lens_cat['ext_angle']
    ekp = lens_cat['ext_kappa']

    xi1, xi2 = make_r_coor(nnn, dsx)
    ai1, ai2 = deflection_nie(xi1, xi2, xc1, xc2, re, rc, ql, phl, esh, ash,
                              ekp)
    mua = pcs.call_alphas_to_mu(ai1, ai2, nnn, dsx)

    yi1 = xi1 - ai1
    yi2 = xi2 - ai2
    #----------------------------------------------------------------------
    # Lensed Point Sources
    #
    ys1 = sagn_cat['ys1']
    ys2 = sagn_cat['ys2']
    xroot1, xroot2, nroots = trf.roots_zeros(xi1, xi2, ai1, ai2, ys1, ys2)
    muii = pcs.call_inverse_cic_single(mua, xroot1, xroot2, dsx)
    #------------------------------------------------------------------------------
    # Lensed Galaxies
    #
    ysc1 = ys1
    ysc2 = ys2

    sgal_img = loadin_sgal(sgal_cat)
    lensed_sgal = lv4.call_ray_tracing(sgal_img, yi1, yi2, ysc1, ysc2, dsi)
    #lensed_img = pcs.call_ray_tracing_single(ai1,ai2,nnn,bsz,zl)
    #------------------------------------------------------------------------------
    # Lens Galaxies
    #
    lgal_img = loadin_lgal(lgal_cat)

    #------------------------------------------------------------------------------
    # Stacking All images
    #
    res = stacking_points_and_galaxies(xroot1, xroot2, lroots[:, 70],
                                       final_img, bsz, nnn)
    return res
def point_ray_tracing(xi1,xi2,ai1,ai2,mu,spar):
    g_limage = xi1*0.0

    xroot1,xroot2,nroots = trf.roots_zeros(xi1,xi2,ai1,ai2,spar[0],spar[1])

    idr1 = ((np.array(xroot1)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    idr2 = ((np.array(xroot2)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    g_limage[idr1,idr2] = g_limage[idr1,idr2] + spar[2]*np.abs(mua[idr1,idr2])

    return g_limage
def point_ray_tracing(xi1, xi2, ai1, ai2, spar, bsz, nnn):
    g_limage = xi1 * 0.0
    dsx = bsz / nnn

    xroot1, xroot2, nroots = trf.roots_zeros(xi1, xi2, ai1, ai2, spar[0],
                                             spar[1])

    idr1 = ((np.array(xroot1) + bsz / 2.0 - dsx / 2.0) / dsx).astype('int')
    idr2 = ((np.array(xroot2) + bsz / 2.0 - dsx / 2.0) / dsx).astype('int')
    g_limage[idr1, idr2] = spar[2]

    return g_limage
示例#4
0
def point_ray_tracing(xi1, xi2, ai1, ai2, mu, spar):
    g_limage = xi1 * 0.0

    xroot1, xroot2, nroots = trf.roots_zeros(xi1, xi2, ai1, ai2, spar[0],
                                             spar[1])

    idr1 = ((np.array(xroot1) + bsz / 2.0 - dsx / 2.0) / dsx).astype('int')
    idr2 = ((np.array(xroot2) + bsz / 2.0 - dsx / 2.0) / dsx).astype('int')
    g_limage[idr1,
             idr2] = g_limage[idr1, idr2] + spar[2] * np.abs(mua[idr1, idr2])

    return g_limage
def run_main():
    #----------------------------------------------------------------------
    SnapID = 135
    HaloID = 50

    zl = 0.5
    zs = 2.0
    nnn = 512

    dm = il.snapshot.loadHalo(basePath, SnapID, HaloID, 'dm')
    #npp = dm['count']
    x1 = dm['Coordinates'][:, 0]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x2 = dm['Coordinates'][:, 1]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x3 = dm['Coordinates'][:, 2]  #/1e3/(1.0+zl) # Mpc/h, comoving

    ptnl_p = dm['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]

    bsz = 50.0  # kpc/h
    sdens = read_sdens_from_illustris(dm, x1 - xcp1, x2 - xcp2, x3 - xcp3, bsz,
                                      nnn)
    #kappa = sdens/mm.sigma_crit(zl,zs)
    #----------------------------------------------------------------------
    bsz = bsz / 1e3 / mm.Da(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)
    yi1 = xi1 - ai1
    yi2 = xi2 - ai2
    phi = pcs.call_cal_phi(sdens, nnn, bsz, zl, zs)
    mua = pcs.call_alphas_to_mu(ai1, ai2, nnn, dsx)
    #----------------------------------------------------------------------
    ys1 = 0.0
    ys2 = 0.0
    xroot1, xroot2, nroots = trf.roots_zeros(xi1, xi2, ai1, ai2, ys1, ys2)

    #pl.figure()
    #pl.plot(ys1,ys2,'ko')
    #pl.plot(xroot1,xroot2,'rs')
    #pl.contour(xi1,xi2,mua)
    #pl.contour(yi1,yi2,mua)
    #pl.colorbar()

    muii = pcs.call_inverse_cic_single(mua, xroot1, xroot2, dsx)
    phii = pcs.call_inverse_cic_single(phi, xroot1, xroot2, dsx)

    Kc = (1.0 + zl) / mm.vc * (mm.Da(zl) * mm.Da(zs) / mm.Da2(
        zl, zs)) * mm.Mpc / (1e3 * mm.dy) / mm.apr / mm.apr
    td = Kc * (0.5 * ((xroot1 - ys1)**2.0 + (xroot2 - ys2)**2.0) - phii)

    time_days, mags = np.loadtxt("./SN_opsim.csv",
                                 dtype='string',
                                 delimiter=',',
                                 usecols=(1, 4),
                                 unpack=True)
    time_days = time_days.astype("double")

    ldays = np.zeros((nroots, len(time_days)))
    #for i in xrange(nroots):
    #ldays[i,:] = time_days+td[i]
    ldays[0, :] = time_days + td[0]
    ldays[1, :] = time_days + td[1]
    ldays[2, :] = time_days + td[2]

    mags = mags.astype("double")

    lmags = np.zeros((nroots, len(time_days)))
    #for i in xrange(nroots):
    #lmags[i,:] = mags-2.5*np.log10(np.abs(muii[i]))
    lmags[0, :] = mags - 2.5 * np.log10(np.abs(muii[0]))
    lmags[1, :] = mags - 2.5 * np.log10(np.abs(muii[1]))
    lmags[2, :] = mags - 2.5 * np.log10(np.abs(muii[2]))

    cmap = mpl.cm.jet_r

    #------------------------------------------------------------------------------
    #linestyle=linestyle, color=color
    pl.figure(figsize=(8, 8))
    pl.xlabel("Days")
    pl.ylabel("Mags")
    pl.ylim(27, 21)
    #for i in xrange(nroots):
    #pl.plot(ldays[i,:]-49500+820,lmags[i,:],linestyle='-',color=cmap(i/float(nroots)))
    pl.plot(ldays[0, :] - 49500 + 820, lmags[0, :], linestyle='-', color="r")
    pl.plot(ldays[1, :] - 49500 + 820, lmags[1, :], linestyle='-', color="g")
    pl.plot(ldays[2, :] - 49500 + 820, lmags[2, :], linestyle='-', color="b")

    pl.figure(figsize=(8, 8))
    pl.xlim(-5.0, 5.0)
    pl.ylim(-5.0, 5.0)
    pl.xlabel("arcsec")
    pl.ylabel("arcsec")
    #for i in xrange(nroots):
    #pl.plot(xroot1[i],xroot2[i],marker='o',color=cmap(i/float(nroots)))
    #markersize=10
    pl.plot(xroot1[0], xroot2[0], marker='o', color="r", markersize=10)
    pl.plot(xroot1[1], xroot2[1], marker='o', color="g", markersize=10)
    pl.plot(xroot1[2], xroot2[2], marker='o', color="b", markersize=10)

    pl.contour(xi1, xi2, mua, colors=('r', ))
    pl.contour(yi1, yi2, mua, colors=('g', ))
    pl.plot(ys1, ys2, 'ks')

    print td
示例#6
0
    mua = pcs.call_alphas_to_mu(ai1, ai2, nnn, dsx)

    #om10.plot_lens(lenses)

    pl.figure()
    pl.contour(xi1, xi2, mua)
    pl.colorbar()

    pl.figure()
    pl.plot(spars[0], spars[1], 'ko')
    pl.contour(xi1 - ai1, xi2 - ai2, mua)
    pl.colorbar()

    [ys1, ys2] = calculate_new_ys(ipars[0], ipars[1], ai1, ai2, dsx)
    xrt1, xrt2, nrts = trf.roots_zeros(xi1, xi2, ai1, ai2, ys1, ys2)
    #xrt1,xrt2,nrts = trf.roots_zeros(xi1,xi2,ai1,ai2,spars[0],spars[1])
    #mui = pcs.call_inverse_cic_single(mua,xrt1,xrt2,dsx)

    print nrts

    #print mui
    #print len(xrt1)
    #print xrt1

    mui = lenses.MAG[0]
    ms = lenses.MAGI_IN[0]
    lenses.NIMG[0] = nrts
    muis = ms - 2.5 * np.log10(np.abs(mui[:nrts]))
    lenses.XIMG[0][:] = 0.0
    lenses.XIMG[0][:nrts] = xrt1
def run_main():
    #----------------------------------------------------------------------
    SnapID = 135
    HaloID = 50

    bsz = 200.0  # kpc/h
    zl = 0.5
    zs = 2.0
    #zs0= 10.0
    nnn = 512
    dsi = 0.03

    dm = il.snapshot.loadHalo(basePath, SnapID, HaloID, 'dm')
    #npp = dm['count']
    x1 = dm['Coordinates'][:, 0]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x2 = dm['Coordinates'][:, 1]  #/1e3/(1.0+zl) # Mpc/h, comoving
    x3 = dm['Coordinates'][:, 2]  #/1e3/(1.0+zl) # Mpc/h, comoving

    ptnl_p = dm['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]

    sdens = read_sdens_from_illustris(dm, x1 - xcp1, x2 - xcp2, x3 - xcp3, bsz,
                                      nnn)
    #kappa = sdens/mm.sigma_crit(zl,zs)
    #----------------------------------------------------------------------
    bsz = bsz / 1e3 / mm.Da(zl) * mm.apr  #arcsec
    dsx = bsz / nnn  #arcsec

    print bsz
    print dsx

    xi1, xi2 = make_r_coor(nnn, dsx)
    ai1, ai2 = pcs.call_cal_alphas0(sdens, nnn, bsz, zl, zs)
    yi1 = xi1 - ai1
    yi2 = xi2 - ai2
    phi = pcs.call_cal_phi(sdens, nnn, bsz, zl, zs)
    mua = pcs.call_alphas_to_mu(ai1, ai2, nnn, dsx)
    #----------------------------------------------------------------------
    ys1 = 0.0
    ys2 = 0.0
    xroot1, xroot2, nroots = trf.roots_zeros(xi1, xi2, ai1, ai2, ys1, ys2)

    #pl.figure()
    #pl.plot(ys1,ys2,'ko')
    #pl.plot(xroot1,xroot2,'rs')
    #pl.contour(xi1,xi2,mua)
    #pl.contour(yi1,yi2,mua)
    #pl.colorbar()

    muii = pcs.call_inverse_cic_single(mua, xroot1, xroot2, dsx)
    phii = pcs.call_inverse_cic_single(phi, xroot1, xroot2, dsx)

    Kc = (1.0 + zl) / mm.vc * (mm.Da(zl) * mm.Da(zs) / mm.Da2(
        zl, zs)) * mm.Mpc / (1e3 * mm.dy) / mm.apr / mm.apr
    td = Kc * (0.5 * ((xroot1 - ys1)**2.0 + (xroot2 - ys2)**2.0) - phii)

    time_days, mags = np.loadtxt("./SN_opsim.csv",
                                 dtype='string',
                                 delimiter=',',
                                 usecols=(1, 4),
                                 unpack=True)
    time_days = time_days.astype("double")
    #------------------------------------------------------------------------------
    ldays = np.zeros((nroots, len(time_days)))
    for i in xrange(nroots):
        ldays[i, :] = time_days + td[i]

    mags = mags.astype("double")
    lmags = np.zeros((nroots, len(time_days)))

    for i in xrange(nroots):
        lmags[i, :] = mags - 2.5 * np.log10(np.abs(muii[i]))

    #cmap = mpl.cm.jet_r
    cmap = mpl.cm.gist_earth

    pl.figure(figsize=(8, 8))
    pl.xlabel("Days")
    pl.ylabel("Mags")
    pl.ylim(30, 22)
    for i in xrange(nroots):
        pl.plot(ldays[i, :] - 49500 + 31010 - 50,
                lmags[i, :],
                linestyle='-',
                color=cmap(i / float(nroots)),
                markersize=10)
    #pl.plot(ldays[0,:]-49500+31010-50,lmags[0,:],linestyle='-',color="k")
    #pl.plot(ldays[1,:]-49500+31010-50,lmags[1,:],linestyle='-',color="b")
    #pl.plot(ldays[2,:]-49500+31010-50,lmags[2,:],linestyle='-',color="g")
    #pl.plot(ldays[3,:]-49500+31010-50,lmags[3,:],linestyle='-',color="m")
    #pl.plot(ldays[4,:]-49500+31010-50,lmags[4,:],linestyle='-',color="r")

    pl.figure(figsize=(8, 8))
    #pl.xlim(-bsz/2.0,bsz/2.0)
    #pl.ylim(-bsz/2.0,bsz/2.0)
    pl.xlim(-10.0, 10.0)
    pl.ylim(-10.0, 10.0)
    pl.xlabel("arcsec")
    pl.ylabel("arcsec")
    for i in xrange(nroots):
        pl.plot(xroot1[i],
                xroot2[i],
                marker='o',
                color=cmap(i / float(nroots)),
                markersize=10)
    #pl.plot(xroot1[0],xroot2[0],marker='o',color="k",markersize=15) #
    #pl.plot(xroot1[1],xroot2[1],marker='o',color="b",markersize=15) #
    #pl.plot(xroot1[2],xroot2[2],marker='o',color="g",markersize=15) #
    #pl.plot(xroot1[3],xroot2[3],marker='o',color="m",markersize=15) #
    #pl.plot(xroot1[4],xroot2[4],marker='o',color="r",markersize=15) #

    pl.contour(xi1, xi2, mua, colors=('r', ))
    pl.contour(yi1, yi2, mua, colors=('g', ))
    pl.plot(ys1, ys2, 'cs')
    print td
    #------------------------------------------------------------------------------
    ysc1 = 0.0
    ysc2 = 0.0

    g_source = pyfits.getdata(
        "./dry_run_pipeline/439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d")
    pl.figure()
    pl.contourf(g_source)
    pl.colorbar()
    std_srcs = np.std(g_source)
    print std_srcs, std_srcs * 4.0
    gthhd = 6.0 * std_srcs
    g_source = g_source - gthhd
    g_source[g_source <= 0.0] = 0.0
    lensed_img = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    #lensed_img = pcs.call_ray_tracing_single(ai1,ai2,nnn,bsz,zl)
    lenses_img = slf.loadin_fits(HaloID, zl, bsz, nnn)

    lroots = 10.0**((lmags - 25.523) / (-2.5))

    final_img = lensed_img + lenses_img / 100.0
    res = stacking_points_and_galaxies(xroot1, xroot2, lroots[:, 70],
                                       final_img, bsz, nnn)
    pl.figure()
    pl.contourf(xi1, xi2, res)
    pl.colorbar()
    pyfits.writeto("all.fits", res, clobber=True)
    return res
def main():
    zl = 0.17
    zs = 3.0
    sigmav = 600           #km/s
    re0 = aa.re_sv(sigmav,zl,zs)
    me0 = aa.m200_sv(sigmav,zl)


    #zs_arr = np.linspace(zl,10.0,100)
    #re_arr = zs_arr*0.0
    #for i in xrange(len(zs_arr)):
        #re_arr[i] = aa.re_sv(sigmav,zl,zs_arr[i])

    #pl.figure()
    #pl.plot(zs_arr,re_arr,'-')



    nrbins = 5
    nnn = 128*nrbins
    #dsx = boxsize/nnn
    dsx = 0.396/nrbins # arcsec
    bsz = dsx*nnn # in the units of Einstein Radius
    print re0,me0,bsz

    #nnn = 512
    #bsz = 40 # in the units of Einstein Radius
    #dsx = bsz/nnn # arcsec

    xx01 = np.linspace(-bsz/2.0,bsz/2.0,nnn)+0.5*dsx
    xx02 = np.linspace(-bsz/2.0,bsz/2.0,nnn)+0.5*dsx
    xi2,xi1 = np.meshgrid(xx01,xx02)
    #----------------------------------------------------------------------
    xc1 = 0.0       #x coordinate of the center of lens (in units of Einstein radius).
    xc2 = 0.0       #y coordinate of the center of lens (in units of Einstein radius).
    q   = 0.7       #Ellipticity of lens.
    rc  = 0.0       #Core size of lens (in units of Einstein radius).
    re  = re0       #Einstein radius of lens.
    pha = 5.0      #Orintation of lens.
    lpar = np.asarray([xc1,xc2,q,rc,re,pha])
    #----------------------------------------------------------------------
    ai1,ai2,mua = lens_equation_sie(xi1,xi2,lpar)
    yi1 = xi1-ai1
    yi2 = xi2-ai2

    #pl.figure()
    #pl.contour(xi2,xi1,mua)
    #pl.colorbar()

    #pl.figure()
    #pl.contour(yi2,yi1,mua)
    #pl.colorbar()

    mags_of_sources = 100.0
    #ys1 = 0.85
    #ys2 = -0.22

    #ys11 = ys1
    #ys12 = ys2

    #xroot1,xroot2,nroots = trf.roots_zeros(xi1,xi2,ai1,ai2,ys1,ys2)

    #idr1 = ((np.array(xroot1)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    #idr2 = ((np.array(xroot2)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    g_limage = xi1*0.0
    #g_limage[idr1,idr2] = mags_of_sources*np.abs(mua[idr1,idr2])

    ys1 = 0.67
    ys2 = 0.75

    ys21 = ys1
    ys22 = ys2

    xroot1,xroot2,nroots = trf.roots_zeros(xi1,xi2,ai1,ai2,ys1,ys2)

    #pl.figure()
    #pl.xlim(-bsz/2,bsz/2)
    #pl.ylim(-bsz/2,bsz/2)
    #pl.plot(ys11,ys12,'wo')
    #pl.plot(ys21,ys22,'yo')

    idr1 = ((np.array(xroot1)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    idr2 = ((np.array(xroot2)+bsz/2.0-dsx/2.0)/dsx).astype('int')
    g_limage[idr1,idr2] = g_limage[idr1,idr2] + mags_of_sources*np.abs(mua[idr1,idr2])

    #pl.figure()
    #pl.contourf(g_limage)
    #pl.colorbar()

    #pl.figure()
    #pl.contour(yi1,yi2,mua)
    #pl.colorbar()
    #print xroot1,xroot2
    #pl.figure(figsize=(10,10))
    #pl.xlim(-2.0,2.0)
    #pl.ylim(-2.0,2.0)
    #pl.plot(xroot1,xroot2,'go')
    #pl.plot(ys1,ys2,'ko')
    #----------------------------------------------------------------------
    g_amp = 0.1*re      # peak brightness value
    g_sig = 300.*re      # Gaussian "sigma" (i.e., size)
    g_xcen = xc1    # x position of center (also try (0.0,0.14)
    g_ycen = xc2    # y position of center
    g_axrat = q     # minor-to-major axis ratio
    g_pa = pha+90      # major-axis position angle (degrees) c.c.w. from x axis
    gpar = np.asarray([g_amp,g_sig,g_xcen,g_ycen,g_axrat,g_pa])
    #----------------------------------------------------------------------
    #g_simage = gauss_2d(xi1,xi2,gpar) # modeling source as 2d Gaussian with input parameters.
    g_lensimage = gauss_2d(xi1,xi2,gpar)
    g_limage = g_limage + g_lensimage/3.0/nrbins**2.0

    #pl.figure()
    #pl.contourf(g_lensimage)
    #pl.colorbar()

    #file_psf = "./sdsspsf.fits"
    #g_psf = pyfits.getdata(file_psf)-1000.0
    #g_psf = g_psf/np.sum(g_psf)

    #g_psf = congrid.congrid(g_psf,[np.shape(g_psf)[0]*nrbins,np.shape(g_psf)[1]*nrbins])
    #g_psf = sil.RectBivariateSpline(g_psf,
                                #bbox=[np.shape(g_psf)[0]*nrbins,np.shape(g_psf)[1]*nrbins,np.shape(g_psf)[0]*nrbins,np.shape(g_psf)[1]*nrbins], kx=3, ky=3, s=0)

    #print np.shape(g_psf),np.max(g_lensimage)

    #pl.figure()
    ##pl.contourf(g_limage)
    ##pl.colorbar()
    #pl.plot(ys11,ys12,'wo')
    #pl.plot(ys21,ys22,'yo')


    #file_noise = "./sdssgal.fits"
    #g_noise = pyfits.getdata(file_noise)-1000.0
    #g_limage = ss.fftconvolve(g_limage,g_psf,mode="same")

    #file_noise = "./sdssgal.fits"
    #g_noise = pyfits.getdata(file_noise)-1000.0
    nstd = 0.5001
    g_noise=nstd*np.random.normal(0.0,1.0,(nnn,nnn))
    g_limage = gaussian_filter(g_limage,5)

    ##pl.figure()
    ##pl.contourf(g_limage)
    #pl.colorbar()

    g_limage = g_limage+g_noise

    output_filename = "./test.fits"
    pyfits.writeto(output_filename,g_limage,clobber=True)


    return 0