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 single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs, lens_tag=1):
    # dsx_sdss     = 0.396         # pixel size of SDSS detector.
    R = 3.0
    nnn = 400  # Image dimension
    bsz = 9.0  # arcsecs
    dsx = bsz / nnn         # pixel size of SDSS detector.
    nstd = 59  # ^2

    xi1, xi2 = make_r_coor(nnn, dsx)
# ----------------------------------------------------------------------
    dsi = 0.03
    g_source = pyfits.getdata(
        "./gals_sources/439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d") * 10.0
    g_source[g_source <= 0.0001] = 1e-6
# ----------------------------------------------------------------------
    # x coordinate of the center of lens (in units of Einstein radius).
    xc1 = 0.0
    # y coordinate of the center of lens (in units of Einstein radius).
    xc2 = 0.0
    rc = 0.0  # Core size of lens (in units of Einstein radius).
    re = re_sv(vd, zl, zs)  # Einstein radius of lens.
    print "re = ", re
    re_sub = 0.0 * re
    a_sub = a_b_bh(re_sub, re)
    ext_shears = 0.06
    ext_angle = -0.39
    ext_kappa = 0.08

    # ext_shears = 0.0
    # ext_angle = 0.0
    # ext_kappa = 0.0

# ----------------------------------------------------------------------
    #lpar = np.asarray([xc1, xc2, q, rc, re, pha])
    #ai1, ai2, kappa_out, shr1, shr2, mua = lensing_signals_sie(xi1, xi2, lpar)
    #ar = np.sqrt(ai1 * ai1 + ai2 * ai2)

    # psi_nie = psk.potential_nie(xc1, xc2, pha, q, re, rc, ext_shears, ext_angle,
    #                            ext_kappa, xi1, xi2)
    #ai1, ai2 = np.gradient(psi_nie, dsx)

    ai1, ai2 = psk.deflection_nie(xc1, xc2, pha, q, re, rc, ext_shears, ext_angle,
                                  ext_kappa, xi1, xi2)

    as1, as2 = psk.deflection_sub_pJaffe(0.0, -2.169, re_sub, 0.0, a_sub, xi1, xi2)

    yi1 = xi1 - ai1 - as1
    yi2 = xi2 - ai2 - as2

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    g_limage[g_limage <= 0.25] = 1e-6

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

    g_limage = cosccd2mag(g_limage)
    g_limage = mag2sdssccd(g_limage)

    # pl.figure()
    # pl.contourf(g_limage)
    # pl.colorbar()
# -------------------------------------------------------------
    dA = Planck13.comoving_distance(zl).value * 1000. / (1.0 + zl)
    Re = dA * np.sin(R * np.pi / 180. / 3600.)
    counts = Brightness(Re, vd)
    vpar = np.asarray([counts, R, xc1, xc2, q, pha])
    g_lens = de_vaucouleurs_2d(xi1, xi2, vpar)

    g_clean_ccd = g_lens * lens_tag + g_limage
    output_filename = "./fits_outputs/clean_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_clean_ccd, overwrite=True)
# -------------------------------------------------------------
    from scipy.ndimage.filters import gaussian_filter
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
# -------------------------------------------------------------
    g_noise = noise_map(nnn, nnn, np.sqrt(nstd), "Gaussian")
    output_filename = "./fits_outputs/noise_map.fits"
    pyfits.writeto(output_filename, g_noise, overwrite=True)
    g_final = g_images_psf + g_noise
# -------------------------------------------------------------
    g_clean_ccd = g_limage
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
    g_final = g_images_psf + g_noise
    output_filename = "./fits_outputs/lensed_imgs_only.fits"
    pyfits.writeto(output_filename, g_final, overwrite=True)
# -------------------------------------------------------------
    output_filename = "./fits_outputs/full_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_final + g_lens, overwrite=True)

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

    return 0
def main():

    nnn = 800
    boxsize = 24.0
    dsx = boxsize / nnn
    dsi = dsx * 0.06
    xi1 = np.linspace(-boxsize / 2.0, boxsize / 2.0 - dsx, nnn) + 0.5 * dsx
    xi2 = np.linspace(-boxsize / 2.0, boxsize / 2.0 - dsx, nnn) + 0.5 * dsx
    xi1, xi2 = np.meshgrid(xi1, xi2)

    g_snR = pyfits.getdata("/Users/uranus/Desktop/gamer1/galaxy0004R.fits")
    g_snG = pyfits.getdata("/Users/uranus/Desktop/gamer1/galaxy0004G.fits")
    g_snB = pyfits.getdata("/Users/uranus/Desktop/gamer1/galaxy0004B.fits")

    g_snR = np.array(g_snR, dtype="<d")
    g_snG = np.array(g_snG, dtype="<d")
    g_snB = np.array(g_snB, dtype="<d")

    #g_snR = snf.gaussian_filter(np.array(g_snR, dtype="<d"), 16.5)
    #g_snG = snf.gaussian_filter(np.array(g_snG, dtype="<d"), 16.5)
    #g_snB = snf.gaussian_filter(np.array(g_snB, dtype="<d"), 16.5)

    #cc = find_critical_curve(mu)

    pygame.init()
    FPS = 60
    fpsClock = pygame.time.Clock()

    screen = pygame.display.set_mode((nnn, nnn), 0, 32)

    pygame.display.set_caption("Gravitational Lensing Toy")

    mouse_cursor = pygame.Surface((nnn, nnn))

    #----------------------------------------------------

    base0 = np.zeros((nnn, nnn, 3), 'uint8')
    base1 = np.zeros((nnn, nnn, 3), 'uint8')
    base2 = np.zeros((nnn, nnn, 3), 'uint8')

    #----------------------------------------------------
    # lens parameters for main halo
    xlc1 = 0.0
    xlc2 = 0.0
    ql0 = 0.799999999999
    rc0 = 0.000000000001
    re0 = 4.0
    phi0 = 60.0
    lpar = np.asarray([xlc1, xlc2, re0, rc0, ql0, phi0])

    lpars_list = []
    lpars_list.append(lpar)
    #----------------------------------------------------
    # lens parameters for main halo

    xls1 = 0.7
    xls2 = 0.8
    qls = 0.999999999999
    rcs = 0.000000000001
    res = 0.0
    phis = 0.0
    lpars = np.asarray([xls1, xls2, res, rcs, qls, phis])
    lpars_list.append(lpars)

    scale_factor = 30
    ap0 = 1.0
    l_sig0 = 0.5
    glpar = np.asarray([ap0, l_sig0, xlc1, xlc2, ql0, phi0])

    g_lens = lens_galaxies(xi1, xi2, glpar)

    base0[:, :, 0] = g_lens * 255
    base0[:, :, 1] = g_lens * 180
    base0[:, :, 2] = g_lens * 0

    #rgb(255, 180, 0)

    x = 0
    y = 0
    step = 1
    gr_sig = 0.1

    LeftButton = 0

    g_xcen = 0.0175
    g_ycen = 0.375
    #----------------------------------------------------

    ic = FPS / 12.0

    i = 0
    while True:
        i = i + 1
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == MOUSEMOTION:

                if event.buttons[LeftButton]:
                    rel = event.rel
                    x += rel[0]
                    y += rel[1]

            #----------------------------------------------
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    gr_sig -= 0.1
                    if gr_sig < 0.01:
                        gr_sig = 0.01

                elif event.button == 5:
                    gr_sig += 0.01
                    if gr_sig > 0.4:
                        gr_sig = 0.4

        keys = pygame.key.get_pressed()  # checking pressed keys
        if keys[pygame.K_RIGHT]:
            x += step
            if x > 500:
                x = 500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_RIGHT]:
            x += 30 * step

        if keys[pygame.K_LEFT]:
            x -= step
            if x < -500:
                x = -500

        if keys[pygame.K_LSHIFT] & keys[pygame.K_LEFT]:
            x -= 30 * step

        if keys[pygame.K_UP]:
            y -= step
            if y < -500:
                y = -500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_UP]:
            y -= 30 * step

        if keys[pygame.K_DOWN]:
            y += step
            if y > 500:
                y = 500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_DOWN]:
            y += 30 * step

        #----------------------------------------------
        if keys[pygame.K_MINUS]:
            gr_sig -= 0.01
            if gr_sig < 0.01:
                gr_sig = 0.01

        if keys[pygame.K_EQUALS]:
            gr_sig += 0.01
            if gr_sig > 0.1:
                gr_sig = 0.1

        #gr_sig = 0.005

        #----------------------------------------------
        # parameters of source galaxies.
        #----------------------------------------------
        g_amp = 1.0         # peak brightness value
        g_sig = gr_sig          # Gaussian "sigma" (i.e., size)
        #g_xcen = x * 2.0 / nnn  # x position of center
        #g_ycen = y * 2.0 / nnn  # y position of center
        g_xcen = 0.0175
        g_ycen = 0.375
        g_axrat = 1.0       # minor-to-major axis ratio
        # major-axis position angle (degrees) c.c.w. from y axis
        g_pa = 0.0
        gpar = np.asarray([g_amp, g_sig, g_ycen, g_xcen, g_axrat, g_pa])
        #----------------------------------------------

        #----------------------------------------------
        # parameters of SNs.
        #----------------------------------------------
        g_amp = 1.0         # peak brightness value
        g_sig = 0.1          # Gaussian "sigma" (i.e., size)
        g_xcen = y * 2.0 / nnn + 0.05  # x position of center
        g_ycen = x * 2.0 / nnn + 0.05  # y position of center
        g_axrat = 1.0       # minor-to-major axis ratio
        # major-axis position angle (degrees) c.c.w. from y axis
        g_pa = 0.0

        print g_ycen, g_xcen
        phi, td, ai1, ai2, kappa, mu, yi1, yi2 = nie_all(
            xi1, xi2, xlc1, xlc2, re0, rc0, ql0, phi0, g_ycen, g_xcen)

        g_lsB = lv4.call_ray_tracing(g_snB, yi1, yi2, g_xcen, g_ycen, dsi)
        g_lsR = lv4.call_ray_tracing(g_snR, yi1, yi2, g_xcen, g_ycen, dsi)
        g_lsG = lv4.call_ray_tracing(g_snG, yi1, yi2, g_xcen, g_ycen, dsi)

        base2[:, :, 0] = g_lsR / scale_factor * 256
        base2[:, :, 1] = g_lsG / scale_factor * 256
        base2[:, :, 2] = g_lsB / scale_factor * 256

        wf = base1 + base2

        idx1 = wf >= base0
        idx2 = wf < base0

        base = base0 * 0
        base[idx1] = wf[idx1]
        base[idx2] = base0[idx2]

        #base = wf*base0+(base1+base2)
        pygame.surfarray.blit_array(mouse_cursor, base)

        screen.blit(mouse_cursor, (0, 0))

        # font=pygame.font.SysFont(None,30)
        #text = font.render("( "+str(x)+", "+str(-y)+" )", True, (255, 255, 255))
        #screen.blit(text,(10, 10))
        pygame.display.update()
        fpsClock.tick(FPS)
def main():
    bsz = 9.0
    nnn = 900
    dsx = bsz / nnn
    xi1, xi2 = make_r_coor(nnn, dsx)

    xlc1 = 0.0  # x position of the lens, arcseconds
    xlc2 = 0.0  # y position of the lens, arcseconds

    vd = 320.0
    ql = 0.7  # axis ratio b/a
    lp = 36.0  # orientation, degree
    ext_shears = 0.02
    ext_angle = 79.0
    zl = 0.5
    zs = 2.0  # redshift of the source

    rle = ole.re_sv(vd, zl, zs)
    #----------------------------------------------------------------------
    le = lef.lambda_e_tot(1.0 - ql)

    ai1, ai2 = ole.alphas_sie(xlc1, xlc2, lp, ql, rle, le, ext_shears,
                              ext_angle, 0.0, xi1, xi2)

    yi1 = xi1 - ai1 * 1.0
    yi2 = xi2 - ai2 * 1.0

    al12, al11 = np.gradient(ai1, dsx)
    al22, al21 = np.gradient(ai2, dsx)

    mua = 1.0 / (1.0 - (al11 + al22) + al11 * al22 - al12 * al21)
    #----------------------------------------------------------------------
    ysc1 = 0.0  # x position of the source, arcseconds
    ysc2 = 0.0  # y position of the source, arcseconds
    dsi = 0.03
    #----------------------------------------------------------------------
    mag_tot_sgal = 23.0  # total magnitude of the source
    g_source = pyfits.getdata(
        "../gals_sources/439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source.T, dtype="<d")
    g_source[g_source <= 0.0001] = 1e-6
    g_source = g_source.copy(order='C')

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    mag_lensed_gal = img2_to_mag2(
        np.sum(g_source) * dsi * dsi, mag_tot_sgal,
        np.sum(g_limage) * dsx * dsx)
    #----------------------------------------------------------------------
    idac = g_source == g_source.max()

    yac1 = (np.indices(np.shape(g_source))[0][idac] -
            np.shape(g_source)[0] / 2.0 + 0.5) * dsx
    yac2 = (np.indices(np.shape(g_source))[1][idac] -
            np.shape(g_source)[1] / 2.0 + 0.5) * dsx

    # pl.figure()
    # pl.contourf(g_source.T, levels=[0.0,0.015,0.03,0.045,0.06,0.075,0.09,0.105])
    # pl.plot(np.indices(np.shape(g_source))[0][idac], np.indices(np.shape(g_source))[1][idac], 'rx')
    # pl.colorbar()

    mag_tot_sagn = 22.5
    a_source = g_source * 0.0
    a_source[int((yac1 + bsz / 2.0 - dsx / 2.0) / dsx),
             int((yac2 + bsz / 2.0 - dsx / 2.0) / dsx)] = 1.0
    # a_source = a_source*mag2_to_img2(np.sum(g_source)*dsi*dsi, mag_tot_sgal, mag_tot_sagn)

    agns_rescale = mag2_to_img2(np.sum(g_source)*dsi*dsi, mag_tot_sgal, mag_tot_sagn) \
                /(np.sum(a_source)*dsi*dsi)

    a_source = a_source * agns_rescale

    a_limage = g_limage * 0.0

    xroot1, xroot2, nroots = trf.mapping_triangles(yac1, yac2, xi1, xi2, yi1,
                                                   yi2)

    # if (nroots > len(ximgs[np.nonzero(ximgs)])):
    # xroot = np.sqrt(xroot1*xroot1 + xroot2*xroot2)
    # idx = xroot == xroot.min()
    # xroot1[idx] = 0.0
    # xroot2[idx] = 0.0

    index2 = ((xroot1 + bsz / 2.0) / dsx).astype('int')
    index1 = ((xroot2 + bsz / 2.0) / dsx).astype('int')

    a_limage[index1, index2] = a_source.max() * dsi * dsi / (dsx * dsx)
    a_limage = a_limage * np.abs(mua)

    mag_lensed_agn = img2_to_mag2(
        np.sum(a_source) * dsi * dsi, mag_tot_sagn,
        np.sum(a_limage) * dsx * dsx)
    # print mag_lensed_agn
    # pl.figure()
    # pl.contourf(xi1, xi2, a_limage)
    # pl.plot(xroot1, xroot2, 'rx')
    # pl.colorbar()

    # print mag_lensed_gal
    # pl.figure()
    # pl.contourf(xi1, xi2, g_limage)
    # pl.plot(xroot1, xroot2, 'rx')
    # pl.colorbar()

    f_limage = g_limage + a_limage

    mag_lensed_tot = img2_to_mag2(
        np.sum(g_source) * dsi * dsi, mag_tot_sgal,
        np.sum(f_limage) * dsx * dsx)

    # print mag_lensed_tot
    # pl.figure()
    # pl.contourf(np.log10(f_limage))
    # pl.colorbar()

    #limg_cat = [xlc1, xlc2, mag_tot, Reff_arc, ql, phl, ndex, zl]

    lens_cat = [xlc1, xlc2, ql, vd, lp, zl]
    limg_cat = gli.lens_img_cat(lens_cat)

    limg_hst = sersic.sersic_2d_norm(xi1, xi2, limg_cat[0], limg_cat[1],
                                     limg_cat[3], ql, lp, 4.0)

    limg_rescale = mag2_to_img2(np.sum(g_source)*dsi*dsi, mag_tot_sgal, limg_cat[2]) \
                /(np.sum(limg_hst)*dsx*dsx)

    g_lens = limg_hst * limg_rescale

    fimage = g_limage + a_limage + g_lens

    mag_tot = img2_to_mag2(
        np.sum(g_source) * dsi * dsi, mag_tot_sgal,
        np.sum(fimage) * dsx * dsx)

    fits_out = "./" + str(mag_tot_sgal) + "_" \
                    + str(mag_tot_sagn) + "_" \
                    + str(limg_cat[2]) + "_" \
                    + str(mag_tot) + "_full.fits"

    pyfits.writeto(fits_out, fimage, overwrite=True)
def output_lensed_images(input_fits,yy1,yy2,ys1,ys2,dsi):
    g_source = pyfits.getdata(input_fits)
    g_source = np.array(g_source,dtype="<d")
    #g_source_pin = lv4.call_ray_tracing(g_source,xx1,xx2,g_xcen,g_ycen,dsi)
    g_lensimage = lv4.call_ray_tracing(g_source,yy1,yy2,ys1,ys2,dsi)
    return g_lensimage
Esempio n. 6
0
def single_run_test(ind,ysc1,ysc2,q,vd,pha,zl,zs):
    dsx_sdss     = 0.396         # pixel size of SDSS detector.
    R  = 3.0000     #
    #zl = 0.2     #zl is the redshift of the lens galaxy.
    #zs = 1.0
    #vd = 520    #Velocity Dispersion.
    nnn = 128      #Image dimension
    bsz = dsx_sdss*nnn # arcsecs
    dsx = bsz/nnn         # pixel size of SDSS detector.
    nstd = 59 #^2

    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)
    #----------------------------------------------------------------------
    #ysc1 = 0.2
    #ysc2 = 0.5
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source,dtype="<d")*10.0
    g_source[g_source<=0.0001] = 1e-6
    #print np.sum(g_source)
    #print np.max(g_source)
    #pl.figure()
    #pl.contourf(g_source)
    #pl.colorbar()
    #g_source = p2p.cosccd2mag(g_source)
    ##g_source = p2p.mag2sdssccd(g_source)
    ##print np.max(g_source*13*13*52.0)
    #pl.figure()
    #pl.contourf(g_source)
    #pl.colorbar()
    #----------------------------------------------------------------------
    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  = re_sv(vd,zl,zs)       #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source,yi1,yi2,ysc1,ysc2,dsi)
    g_limage[g_limage<=0.0001] = 1e-6
    g_limage = p2p.cosccd2mag(g_limage)
    g_limage = p2p.mag2sdssccd(g_limage)

    #pl.figure()
    #pl.imshow((g_limage),interpolation='lanczos',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value*1000./(1+zl)
    Re = dA*np.sin(R*np.pi/180./3600.)
    counts  =Brightness(Re,vd)
    vpar = np.asarray([counts,R,xc1,xc2,q,pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1,xi2,vpar)

    #pl.figure()
    #pl.imshow((g_lens),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_clean_ccd = g_lens+g_limage

    #pl.figure()
    #pl.imshow((g_clean_ccd),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_clean_ccd = congrid.congrid(g_clean_ccd,[128,128])

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

    #new_shape=[0,0]
    #new_shape[0]=np.shape(g_psf)[0]*dsx_sdss/dsx
    #new_shape[1]=np.shape(g_psf)[1]*dsx_sdss/dsx
    #g_psf = rebin_psf(g_psf,new_shape)

    g_images_psf = ss.fftconvolve(g_clean_ccd,g_psf,mode="same")
    #g_images_psf = ss.convolve(g_clean_ccd,g_psf,mode="same")
    #g_images_psf = g_clean_ccd

    #pl.figure()
    #pl.imshow((g_psf),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    #g_noise = noise_map(nnn,nnn,np.sqrt(nstd),"Gaussian")
    g_noise = noise_map(128,128,np.sqrt(nstd),"Gaussian")
    g_final = g_images_psf+g_noise

    #pl.figure()
    #pl.imshow((g_final.T),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_final_rebin = congrid.congrid(g_final,[128,128])

    #pl.figure()
    #pl.imshow((g_final_rebin.T),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------

    output_filename = "../output_fits/"+str(ind)+".fits"
    pyfits.writeto(output_filename,g_final_rebin,clobber=True)

    pl.show()

    return 0
Esempio n. 7
0
def single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs):
    dsx_sdss = 0.396  # pixel size of SDSS detector.
    R = 3.0000  #
    nnn = 300  #Image dimension
    bsz = 9.0  # arcsecs
    dsx = bsz / nnn  # pixel size of SDSS detector.
    nstd = 59  #^2

    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)
    #----------------------------------------------------------------------
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d") * 10.0
    g_source[g_source <= 0.0001] = 1e-6
    #----------------------------------------------------------------------
    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 = re_sv(vd, zl, zs)  #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    g_limage[g_limage <= 0.0001] = 1e-6
    g_limage = p2p.cosccd2mag(g_limage)
    g_limage = p2p.mag2sdssccd(g_limage)

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value * 1000. / (1 + zl)
    Re = dA * np.sin(R * np.pi / 180. / 3600.)
    counts = Brightness(Re, vd)
    vpar = np.asarray([counts, R, xc1, xc2, q, pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1, xi2, vpar)

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

    g_clean_ccd1 = g_lens * 0.0 + g_limage
    g_clean_ccd2 = g_lens * 1.0 + g_limage
    from scipy.ndimage.filters import gaussian_filter
    pl.figure()
    pl.contourf(g_clean_ccd1)
    pl.colorbar()
    pl.savefig("./output_pngs/" + str(i) + "_lensed_imgs_only.png")
    #-------------------------------------------------------------
    g_images_psf1 = gaussian_filter(g_clean_ccd1, 2.0)
    g_images_psf2 = gaussian_filter(g_clean_ccd2, 2.0)
    #g_images_psf = ss.convolve(g_clean_ccd,g_psf,mode="same")
    #g_images_psf = g_clean_ccd
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    g_noise = noise_map(nnn, nnn, np.sqrt(nstd), "Gaussian")
    #output_filename = "./output_fits/noise_map.fits"
    #pyfits.writeto(output_filename,g_noise,clobber=True)
    #-------------------------------------------------------------
    g_final1 = g_images_psf1 + g_noise
    output_filename = "./output_fits/" + str(i) + "_lensed_imgs_only.fits"
    pyfits.writeto(output_filename, g_final1, clobber=True)
    #-------------------------------------------------------------
    g_final2 = g_images_psf2 + g_noise
    output_filename = "./output_fits/" + str(i) + "_all_imgs.fits"
    pyfits.writeto(output_filename, g_final2, clobber=True)

    return 0
Esempio n. 8
0
def single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs):
    zeropoint = 18

    dsx_sdss = 0.396  # pixel size of SDSS detector.

    R = 2.9918  #vd is velocity dispersion.
    #zl = 0.2     #zl is the redshift of the lens galaxy.
    #zs = 1.0
    #vd = 520    #Velocity Dispersion.
    nnn = 512  #Image dimension
    bsz = 30.0  # arcsecs
    dsx = bsz / nnn  # pixel size of SDSS detector.
    nstd = 59

    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)
    #----------------------------------------------------------------------
    #ysc1 = 0.2
    #ysc2 = 0.5
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d")
    g_source = p2p.pixcos2pixsdss(g_source)
    #----------------------------------------------------------------------
    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 = re_sv(vd, zl, zs)  #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    g_limage = mag_to_flux(g_limage, zeropoint)

    #pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    #pl.colorbar()
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value * 1000. / (1 + zl)
    Re = dA * np.sin(R * np.pi / 180. / 3600.)
    counts = Brightness(R, vd)
    vpar = np.asarray([counts, Re, xc1, xc2, q, pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1, xi2, vpar)

    g_lens = ncounts_to_flux(g_lens * 1.5e-4, zeropoint)
    #-------------------------------------------------------------
    file_psf = "../PSF_and_noise/sdsspsf.fits"
    g_psf = pyfits.getdata(file_psf) - 1000.0
    g_psf = g_psf / np.sum(g_psf)
    new_shape = [0, 0]
    new_shape[0] = np.shape(g_psf)[0] * dsx_sdss / dsx
    new_shape[1] = np.shape(g_psf)[1] * dsx_sdss / dsx
    g_psf = rebin_psf(g_psf, new_shape)
    print(np.max(g_psf))
    g_limage = ss.fftconvolve(g_limage + g_lens, g_psf, mode="same")

    #pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    #pl.colorbar()
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    g_noise = noise_map(nnn, nnn, nstd, "Gaussian")
    g_noise = ncounts_to_flux(g_noise * 1e-0 + skycount, zeropoint)
    g_limage = g_limage + g_noise

    print np.shape(g_limage)
    g_limage = congrid.congrid(g_limage, [128, 128])
    g_limage = g_limage - np.min(g_limage)

    pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    pl.contourf(g_limage)
    pl.colorbar()
    #-------------------------------------------------------------

    output_filename = "../output_fits/" + str(ind) + ".fits"
    pyfits.writeto(output_filename, g_limage, clobber=True)

    pl.show()

    return 0
Esempio n. 9
0
def single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs, lens_tag=1):
    # dsx_sdss     = 0.396         # pixel size of SDSS detector.
    R = 3.0
    nnn = 400  # Image dimension
    bsz = 9.0  # arcsecs
    dsx = bsz / nnn  # pixel size of SDSS detector.
    nstd = 59  # ^2

    xi1, xi2 = make_r_coor(nnn, dsx)
    # ----------------------------------------------------------------------
    dsi = 0.03
    g_source = pyfits.getdata(
        "./gals_sources/439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d") * 10.0
    g_source[g_source <= 0.0001] = 1e-6
    # ----------------------------------------------------------------------
    # x coordinate of the center of lens (in units of Einstein radius).
    xc1 = 0.0
    # y coordinate of the center of lens (in units of Einstein radius).
    xc2 = 0.0
    rc = 0.0  # Core size of lens (in units of Einstein radius).
    re = re_sv(vd, zl, zs)  # Einstein radius of lens.
    print "re = ", re
    re_sub = 0.0 * re
    a_sub = a_b_bh(re_sub, re)
    ext_shears = 0.06
    ext_angle = -0.39
    ext_kappa = 0.08

    # ext_shears = 0.0
    # ext_angle = 0.0
    # ext_kappa = 0.0

    # ----------------------------------------------------------------------
    #lpar = np.asarray([xc1, xc2, q, rc, re, pha])
    #ai1, ai2, kappa_out, shr1, shr2, mua = lensing_signals_sie(xi1, xi2, lpar)
    #ar = np.sqrt(ai1 * ai1 + ai2 * ai2)

    # psi_nie = psk.potential_nie(xc1, xc2, pha, q, re, rc, ext_shears, ext_angle,
    #                            ext_kappa, xi1, xi2)
    #ai1, ai2 = np.gradient(psi_nie, dsx)

    ai1, ai2 = psk.deflection_nie(xc1, xc2, pha, q, re, rc, ext_shears,
                                  ext_angle, ext_kappa, xi1, xi2)

    as1, as2 = psk.deflection_sub_pJaffe(0.0, -2.169, re_sub, 0.0, a_sub, xi1,
                                         xi2)

    yi1 = xi1 - ai1 - as1
    yi2 = xi2 - ai2 - as2

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    g_limage[g_limage <= 0.25] = 1e-6

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

    g_limage = cosccd2mag(g_limage)
    g_limage = mag2sdssccd(g_limage)

    # pl.figure()
    # pl.contourf(g_limage)
    # pl.colorbar()
    # -------------------------------------------------------------
    dA = Planck13.comoving_distance(zl).value * 1000. / (1.0 + zl)
    Re = dA * np.sin(R * np.pi / 180. / 3600.)
    counts = Brightness(Re, vd)
    vpar = np.asarray([counts, R, xc1, xc2, q, pha])
    g_lens = de_vaucouleurs_2d(xi1, xi2, vpar)

    g_clean_ccd = g_lens * lens_tag + g_limage
    output_filename = "./fits_outputs/clean_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_clean_ccd, overwrite=True)
    # -------------------------------------------------------------
    from scipy.ndimage.filters import gaussian_filter
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
    # -------------------------------------------------------------
    g_noise = noise_map(nnn, nnn, np.sqrt(nstd), "Gaussian")
    output_filename = "./fits_outputs/noise_map.fits"
    pyfits.writeto(output_filename, g_noise, overwrite=True)
    g_final = g_images_psf + g_noise
    # -------------------------------------------------------------
    g_clean_ccd = g_limage
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
    g_final = g_images_psf + g_noise
    output_filename = "./fits_outputs/lensed_imgs_only.fits"
    pyfits.writeto(output_filename, g_final, overwrite=True)
    # -------------------------------------------------------------
    output_filename = "./fits_outputs/full_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_final + g_lens, overwrite=True)

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

    return 0
g_xcen = 0.0  	# x position of center (also try (0.0,0.14)
g_ycen = 0.0  	# y position of center
g_axrat = 1.0 	# minor-to-major axis ratio
g_pa = 0.0    	# major-axis position angle (degrees) c.c.w. from x axis
#----------------------------------------------------------------------
#g_source = 0.0*xx1
#gpar = np.asarray([g_amp,g_sig,g_xcen,g_ycen,g_axrat,g_pa])
#g_source = gauss_2d(xx1,xx2,gpar)
#g_lensimage = 0.0*yi1
#g_lensimage = gauss_2d(yi1,yi2,gpar)

#g_source = pyfits.getdata("./compound_R_1_0_S_1_1_u.fits")
input_fits = sys.argv[1]
g_source = pyfits.getdata(input_fits)
g_source = np.array(g_source,dtype="<d")
g_source_pin = lv4.call_ray_tracing(g_source,xx1,xx2,g_xcen,g_ycen,dsi)
g_lensimage = lv4.call_ray_tracing(g_source,yi1,yi2,g_xcen,g_ycen,dsi)


filename = "output.fits"
pyfits.writeto(filename,g_lensimage,clobber=True)
##--------------------------lens images contour------------------------
##levels = [0.15,0.30,0.45,0.60,0.75,0.9,1.05]
#figure(num=None,figsize=(10,5),dpi=80, facecolor='w', edgecolor='k')


#a = axes([0.05,0.1,0.4,0.8])
#a.set_xlim(-boxsize/4.0,boxsize/4.0)
#a.set_ylim(-boxsize/4.0,boxsize/4.0)
#a.contourf(xx1,xx2,g_source_pin)
##a.contour(yi1,yi2,mua,0,colors=('g'),linewidths = 2.0)
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 single_run_test(ind,ysc1,ysc2,q,vd,pha,zl,zs):
    zeropoint = 18

    dsx_sdss     = 0.396         # pixel size of SDSS detector.


    R  = 2.9918     #vd is velocity dispersion.
    #zl = 0.2     #zl is the redshift of the lens galaxy.
    #zs = 1.0
    #vd = 520    #Velocity Dispersion.
    nnn = 512      #Image dimension
    bsz = 30.0 # arcsecs
    dsx = bsz/nnn         # pixel size of SDSS detector.
    nstd = 59

    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)
    #----------------------------------------------------------------------
    #ysc1 = 0.2
    #ysc2 = 0.5
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source,dtype="<d")
    g_source = p2p.pixcos2pixsdss(g_source)
    #----------------------------------------------------------------------
    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  = re_sv(vd,zl,zs)       #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source,yi1,yi2,ysc1,ysc2,dsi)
    g_limage = mag_to_flux(g_limage,zeropoint)

    #pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    #pl.colorbar()
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value*1000./(1+zl)
    Re = dA*np.sin(R*np.pi/180./3600.)
    counts  =Brightness(R,vd)
    vpar = np.asarray([counts,Re,xc1,xc2,q,pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1,xi2,vpar)

    g_lens = ncounts_to_flux(g_lens*1.5e-4,zeropoint)
    #-------------------------------------------------------------
    file_psf = "../PSF_and_noise/sdsspsf.fits"
    g_psf = pyfits.getdata(file_psf)-1000.0
    g_psf = g_psf/np.sum(g_psf)
    new_shape=[0,0]
    new_shape[0]=np.shape(g_psf)[0]*dsx_sdss/dsx
    new_shape[1]=np.shape(g_psf)[1]*dsx_sdss/dsx
    g_psf = rebin_psf(g_psf,new_shape)
    print(np.max(g_psf))
    g_limage = ss.fftconvolve(g_limage+g_lens,g_psf,mode="same")

    #pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    #pl.colorbar()
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    g_noise = noise_map(nnn,nnn,nstd,"Gaussian")
    g_noise = ncounts_to_flux(g_noise*1e-0+skycount,zeropoint)
    g_limage = g_limage+g_noise

    print np.shape(g_limage)
    g_limage = congrid.congrid(g_limage,[128,128])
    g_limage = g_limage-np.min(g_limage)

    pl.figure()
    #pl.contourf(xi1,xi2,g_limage)
    pl.contourf(g_limage)
    pl.colorbar()
    #-------------------------------------------------------------

    output_filename = "../output_fits/"+str(ind)+".fits"
    pyfits.writeto(output_filename,g_limage,clobber=True)

    pl.show()

    return 0
def single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs, lens_tag=1):
    nnn = 400  # Image dimension
    bsz = 9.0  # arcsecs
    dsx = bsz / nnn         # pixel size of SDSS detector.
    nstd = 0.001  # ^2

    xi1, xi2 = make_r_coor(nnn, dsx)
# ----------------------------------------------------------------------
    dsi = 0.03
    g_source = pyfits.getdata(
        "./gals_sources/439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d")
    g_std = np.std(g_source)
    print g_std
    g_source[g_source <= 6.0*g_std] = 1e-6
# ----------------------------------------------------------------------
    # x coordinate of the center of lens (in units of Einstein radius).
    xc1 = 0.0
    # y coordinate of the center of lens (in units of Einstein radius).
    xc2 = 0.0
    rc = 0.0  # Core size of lens (in units of Einstein radius).
    re = re_sv(vd, zl, zs)  # Einstein radius of lens.
    re_sub = 0.0 * re
    a_sub = a_b_bh(re_sub, re)
    ext_shears = 0.06
    ext_angle = -0.39
    ext_kappa = 0.08

    # ext_shears = 0.0
    # ext_angle = 0.0
    # ext_kappa = 0.0
# ----------------------------------------------------------------------
    ai1, ai2 = psk.deflection_nie(xc1, xc2, pha, q, re, rc, ext_shears, ext_angle,
                                  ext_kappa, xi1, xi2)

    as1, as2 = psk.deflection_sub_pJaffe(0.0, -2.169, re_sub, 0.0, a_sub, xi1, xi2)

    yi1 = xi1 - ai1 - as1
    yi2 = xi2 - ai2 - as2

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    # g_limage[g_limage <= 0.25] = 1e-6
# -------------------------------------------------------------
    afactor = 0.01
    Reff = 3.0
    ell = q
    ori = pha
    g_lens = de_vaucouleurs_2d(xi1, xi2, xc1, xc2, afactor, Reff, ell, ori)

    g_clean_ccd = g_lens + g_limage
    output_filename = "./fits_outputs/clean_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_clean_ccd, overwrite=True)
# -------------------------------------------------------------
    from scipy.ndimage.filters import gaussian_filter
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
# -------------------------------------------------------------
    g_noise = noise_map(nnn, nnn, np.sqrt(nstd), "Gaussian")
    output_filename = "./fits_outputs/noise_map.fits"
    pyfits.writeto(output_filename, g_noise, overwrite=True)
    g_final = g_images_psf + g_noise
# -------------------------------------------------------------
    g_clean_ccd = g_limage
    g_images_psf = gaussian_filter(g_clean_ccd, 2.0)
    g_final = g_images_psf + g_noise
    output_filename = "./fits_outputs/lensed_imgs_only.fits"
    pyfits.writeto(output_filename, g_final, overwrite=True)
# -------------------------------------------------------------
    output_filename = "./fits_outputs/full_lensed_imgs.fits"
    pyfits.writeto(output_filename, g_final + g_lens, overwrite=True)

    pl.figure()
    pl.contourf(g_final + g_lens)
    pl.colorbar()
# -------------------------------------------------------------
    # al11,al12 = np.gradient(al1,dsx)
    # al21,al22 = np.gradient(al2,dsx)
    # mua = 1.0/(1.0-(al11+al22)+al11*al22-al12*al21)

    return 0
def main():

    nnn = 512
    boxsize = 4.0
    dsx = boxsize / nnn
    dsi = dsx * 2.0
    xi1 = np.linspace(-boxsize / 2.0, boxsize / 2.0 - dsx, nnn) + 0.5 * dsx
    xi2 = np.linspace(-boxsize / 2.0, boxsize / 2.0 - dsx, nnn) + 0.5 * dsx
    xi1, xi2 = np.meshgrid(xi1, xi2)

    ysc1 = 0.0
    ysc2 = 0.0

    g_sn = pyfits.getdata("./compound_R_1_0_S_1_1_u.fits")
    g_sn = np.array(g_sn, dtype="<d")

    # cc = find_critical_curve(mu)

    pygame.init()
    FPS = 60
    fpsClock = pygame.time.Clock()

    screen = pygame.display.set_mode((nnn, nnn), 0, 32)

    pygame.display.set_caption("Gravitational Lensing Toy")

    mouse_cursor = pygame.Surface((nnn, nnn))

    # ----------------------------------------------------

    base0 = np.zeros((nnn, nnn, 3), "uint8")
    base1 = np.zeros((nnn, nnn, 3), "uint8")
    base2 = np.zeros((nnn, nnn, 3), "uint8")

    # ----------------------------------------------------
    # lens parameters for main halo
    xlc1 = 0.0
    xlc2 = 0.0
    ql0 = 0.699999999999
    rc0 = 0.000000000001
    re0 = 1.0
    phi0 = 30.0
    lpar = np.asarray([xlc1, xlc2, re0, rc0, ql0, phi0])

    lpars_list = []
    lpars_list.append(lpar)
    # ----------------------------------------------------
    # lens parameters for main halo
    xls1 = 0.7
    xls2 = 0.8
    qls = 0.999999999999
    rcs = 0.000000000001
    res = 0.0
    phis = 0.0
    lpars = np.asarray([xls1, xls2, res, rcs, qls, phis])
    lpars_list.append(lpars)

    ap0 = 1.0
    l_sig0 = 0.5
    glpar = np.asarray([ap0, l_sig0, xlc1, xlc2, ql0, phi0])

    g_lens = lens_galaxies(xi1, xi2, glpar)

    # base0[:,:,0] = g_lens*256
    # base0[:,:,1] = g_lens*128
    # base0[:,:,2] = g_lens*0

    base0[:, :, 0] = g_lens * 0
    base0[:, :, 1] = g_lens * 0
    base0[:, :, 2] = g_lens * 0

    x = 0
    y = 0
    step = 1
    gr_sig = 0.1

    LeftButton = 0

    # ----------------------------------------------------

    ic = FPS / 12.0

    i = 0
    while True:
        i = i + 1
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == MOUSEMOTION:

                if event.buttons[LeftButton]:
                    rel = event.rel
                    x += rel[0]
                    y += rel[1]

            # ----------------------------------------------
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    gr_sig -= 0.1
                    if gr_sig < 0.01:
                        gr_sig = 0.01

                elif event.button == 5:
                    gr_sig += 0.01
                    if gr_sig > 0.4:
                        gr_sig = 0.4

        keys = pygame.key.get_pressed()  # checking pressed keys
        if keys[pygame.K_RIGHT]:
            x += step
            if x > 500:
                x = 500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_RIGHT]:
            x += 30 * step

        if keys[pygame.K_LEFT]:
            x -= step
            if x < -500:
                x = -500

        if keys[pygame.K_LSHIFT] & keys[pygame.K_LEFT]:
            x -= 30 * step

        if keys[pygame.K_UP]:
            y -= step
            if y < -500:
                y = -500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_UP]:
            y -= 30 * step

        if keys[pygame.K_DOWN]:
            y += step
            if y > 500:
                y = 500
        if keys[pygame.K_LSHIFT] & keys[pygame.K_DOWN]:
            y += 30 * step

        # ----------------------------------------------
        if keys[pygame.K_MINUS]:
            gr_sig -= 0.01
            if gr_sig < 0.01:
                gr_sig = 0.01

        if keys[pygame.K_EQUALS]:
            gr_sig += 0.01
            if gr_sig > 0.1:
                gr_sig = 0.1

        # gr_sig = 0.005

        # ----------------------------------------------
        # parameters of source galaxies.
        # ----------------------------------------------
        g_amp = 1.0  # peak brightness value
        g_sig = gr_sig  # Gaussian "sigma" (i.e., size)
        g_xcen = x * 2.0 / nnn  # x position of center
        g_ycen = y * 2.0 / nnn  # y position of center
        g_axrat = 1.0  # minor-to-major axis ratio
        g_pa = 0.0  # major-axis position angle (degrees) c.c.w. from y axis
        gpar = np.asarray([g_amp, g_sig, g_ycen, g_xcen, g_axrat, g_pa])
        # ----------------------------------------------

        # ----------------------------------------------
        # parameters of SNs.
        # ----------------------------------------------
        g_amp = 1.0  # peak brightness value
        g_sig = 0.1  # Gaussian "sigma" (i.e., size)
        g_xcen = y * 2.0 / nnn + 0.05  # x position of center
        g_ycen = x * 2.0 / nnn + 0.05  # y position of center
        g_axrat = 1.0  # minor-to-major axis ratio
        g_pa = 0.0  # major-axis position angle (degrees) c.c.w. from y axis
        gpsn = np.asarray([g_amp, g_sig, g_ycen, g_xcen, g_axrat, g_pa])

        phi, td, ai1, ai2, kappa, mu, yi1, yi2 = nie_all(xi1, xi2, xlc1, xlc2, re0, rc0, ql0, phi0, g_ycen, g_xcen)
        g_image, g_lensimage = lensed_images(xi1, xi2, yi1, yi2, gpar)
        g_image = g_image * 0.0
        g_lensimage = g_lensimage * 0.0
        # g_sn,g_lsn = lensed_images_point(xi1,xi2,yi1,yi2,gpsn)

        # g_sn = tophat_2d(xi1,xi2,gpsn)
        # g_sn_pin = lv4.call_ray_tracing(g_sn,xi1,xi2,ysc1,ysc2,dsi)
        # g_lsn = lv4.call_ray_tracing(g_sn,yi1,yi2,ysc1,ysc2,dsi)

        g_sn_pin = lv4.call_ray_tracing(g_sn, xi1, xi2, g_xcen, g_ycen, dsi)
        g_lsn = lv4.call_ray_tracing(g_sn, yi1, yi2, g_xcen, g_ycen, dsi)

        # sktd = td/td.max()*ic
        # itmp = (i+30-sktd)%(FPS)
        # ratio = (ic-itmp)*itmp/(ic/2.0)**2.0

        # sktd0 = 0.0*sktd
        # itmp0 = (i+90-sktd0)%(FPS)
        # ratio0 = (ic-itmp0)*itmp0/(ic/2.0)**2.0

        # ratio[ratio<0]=0.0
        # ratio0[ratio0<0]=0.0

        # sktd = td/td.max()*ic
        # itmp = (i)%(FPS+30)
        # ratio = gauss_1d(itmp,2.0*ic+sktd-20,ic,2.0)

        # ratio0 = gauss_1d(itmp,2.0*ic,ic,2.0)

        sktd = td / td.max() * ic
        itmp = (i) % (FPS)
        ratio = parabola_1d(itmp, 30 + sktd, ic, 2.0 / ic ** 2.0)
        ratio0 = parabola_1d(itmp, 0.0 * sktd, ic, 2.0 / ic ** 2.0)

        # base2[:,:,0] = g_lensimage*102*(1+ratio)
        # base2[:,:,1] = g_lensimage*178*(1+ratio)
        # base2[:,:,2] = g_lensimage*256*(1+ratio)
        # base2[:,:,0] = g_lensimage*102*(1.0+ratio)/2
        # base2[:,:,1] = g_lensimage*178*(1.0+ratio)/2
        # base2[:,:,2] = g_lensimage*256*(1.0+ratio)/2

        base1[:, :, 0] = g_sn_pin * 100 * (1.0 + ratio0) / 2 + g_image * 256
        base1[:, :, 1] = g_sn_pin * 100 * (1.0 + ratio0) / 2 + g_image * 256
        base1[:, :, 2] = g_sn_pin * 100 * (1.0 + ratio0) / 2 + g_image * 256

        base2[:, :, 0] = g_lsn * 100 * (1.0 + ratio) / 2 + g_lensimage * 102
        base2[:, :, 1] = g_lsn * 100 * (1.0 + ratio) / 2 + g_lensimage * 178
        base2[:, :, 2] = g_lsn * 100 * (1.0 + ratio) / 2 + g_lensimage * 256

        wf = base1 + base2

        idx1 = wf >= base0
        idx2 = wf < base0

        base = base0 * 0
        base[idx1] = wf[idx1]
        base[idx2] = base0[idx2]

        # base = wf*base0+(base1+base2)
        pygame.surfarray.blit_array(mouse_cursor, base)

        screen.blit(mouse_cursor, (0, 0))

        # font=pygame.font.SysFont(None,30)
        # text = font.render("( "+str(x)+", "+str(-y)+" )", True, (255, 255, 255))
        # screen.blit(text,(10, 10))
        pygame.display.update()
        fpsClock.tick(FPS)
Esempio n. 15
0
def single_run_test(ind, ysc1, ysc2, q, vd, pha, zl, zs):
    dsx_sdss = 0.396  # pixel size of SDSS detector.
    R = 3.0000  #
    #zl = 0.2     #zl is the redshift of the lens galaxy.
    #zs = 1.0
    #vd = 520    #Velocity Dispersion.
    nnn = 128  #Image dimension
    bsz = dsx_sdss * nnn  # arcsecs
    dsx = bsz / nnn  # pixel size of SDSS detector.
    nstd = 59  #^2

    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)
    #----------------------------------------------------------------------
    #ysc1 = 0.2
    #ysc2 = 0.5
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source, dtype="<d") * 10.0
    g_source[g_source <= 0.0001] = 1e-6
    #print np.sum(g_source)
    #print np.max(g_source)
    #pl.figure()
    #pl.contourf(g_source)
    #pl.colorbar()
    #g_source = p2p.cosccd2mag(g_source)
    ##g_source = p2p.mag2sdssccd(g_source)
    ##print np.max(g_source*13*13*52.0)
    #pl.figure()
    #pl.contourf(g_source)
    #pl.colorbar()
    #----------------------------------------------------------------------
    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 = re_sv(vd, zl, zs)  #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source, yi1, yi2, ysc1, ysc2, dsi)
    g_limage[g_limage <= 0.0001] = 1e-6
    g_limage = p2p.cosccd2mag(g_limage)
    g_limage = p2p.mag2sdssccd(g_limage)

    #pl.figure()
    #pl.imshow((g_limage),interpolation='lanczos',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value * 1000. / (1 + zl)
    Re = dA * np.sin(R * np.pi / 180. / 3600.)
    counts = Brightness(Re, vd)
    vpar = np.asarray([counts, R, xc1, xc2, q, pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1, xi2, vpar)

    #pl.figure()
    #pl.imshow((g_lens),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_clean_ccd = g_lens + g_limage

    #pl.figure()
    #pl.imshow((g_clean_ccd),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_clean_ccd = congrid.congrid(g_clean_ccd, [128, 128])

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

    #new_shape=[0,0]
    #new_shape[0]=np.shape(g_psf)[0]*dsx_sdss/dsx
    #new_shape[1]=np.shape(g_psf)[1]*dsx_sdss/dsx
    #g_psf = rebin_psf(g_psf,new_shape)

    g_images_psf = ss.fftconvolve(g_clean_ccd, g_psf, mode="same")
    #g_images_psf = ss.convolve(g_clean_ccd,g_psf,mode="same")
    #g_images_psf = g_clean_ccd

    #pl.figure()
    #pl.imshow((g_psf),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    #g_noise = noise_map(nnn,nnn,np.sqrt(nstd),"Gaussian")
    g_noise = noise_map(128, 128, np.sqrt(nstd), "Gaussian")
    g_final = g_images_psf + g_noise

    #pl.figure()
    #pl.imshow((g_final.T),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    g_final_rebin = congrid.congrid(g_final, [128, 128])

    #pl.figure()
    #pl.imshow((g_final_rebin.T),interpolation='nearest',cmap=cm.gray)
    #pl.colorbar()

    #-------------------------------------------------------------

    output_filename = "../output_fits/" + str(ind) + ".fits"
    pyfits.writeto(output_filename, g_final_rebin, clobber=True)

    pl.show()

    return 0
def single_run_test(ind,ysc1,ysc2,q,vd,pha,zl,zs):
    dsx_sdss     = 0.396         # pixel size of SDSS detector.
    R  = 3.0000     #
    nnn = 300      #Image dimension
    bsz = 9.0 # arcsecs
    dsx = bsz/nnn         # pixel size of SDSS detector.
    nstd = 59 #^2

    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)
    #----------------------------------------------------------------------
    dsi = 0.03
    g_source = pyfits.getdata("./439.0_149.482739_1.889989_processed.fits")
    g_source = np.array(g_source,dtype="<d")*10.0
    g_source[g_source<=0.0001] = 1e-6
    #----------------------------------------------------------------------
    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  = re_sv(vd,zl,zs)       #Einstein radius of lens.
    #pha = 45.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

    g_limage = lv4.call_ray_tracing(g_source,xi1,xi2,ysc1,ysc2,dsi)
    g_limage[g_limage<=0.0001] = 1e-6
    g_limage = p2p.cosccd2mag(g_limage)
    g_limage = p2p.mag2sdssccd(g_limage)

    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    dA = Planck13.comoving_distance(zl).value*1000./(1+zl)
    Re = dA*np.sin(R*np.pi/180./3600.)
    counts  =Brightness(Re,vd)
    vpar = np.asarray([counts,R,xc1,xc2,q,pha])
    #g_lens = deVaucouleurs(xi1,xi2,xc1,xc2,counts,R,1.0-q,pha)
    g_lens = de_vaucouleurs_2d(xi1,xi2,vpar)

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

    g_clean_ccd1 = g_lens*0.0+g_limage
    g_clean_ccd2 = g_lens*1.0+g_limage
    from scipy.ndimage.filters import gaussian_filter
    pl.figure()
    pl.contourf(g_clean_ccd1)
    pl.colorbar()
    pl.savefig("/Users/uranus/GitHub/data_arc_finding_cnn/unlensed_output_pngs/"+str(i)+"_lensed_imgs_only.png")
    #-------------------------------------------------------------
    g_images_psf1 = gaussian_filter(g_clean_ccd1, 2.0)
    g_images_psf2 = gaussian_filter(g_clean_ccd2, 2.0)
    #g_images_psf = ss.convolve(g_clean_ccd,g_psf,mode="same")
    #g_images_psf = g_clean_ccd
    #-------------------------------------------------------------
    # Need to be Caliborate the mags
    g_noise = noise_map(nnn,nnn,np.sqrt(nstd),"Gaussian")
    #output_filename = "./output_fits/noise_map.fits"
    #pyfits.writeto(output_filename,g_noise,clobber=True)
    #-------------------------------------------------------------
    g_final1 = g_images_psf1+g_noise
    output_filename = "/Users/uranus/GitHub/data_arc_finding_cnn/unlensed_output_fits/"+str(i)+"_lensed_imgs_only.fits"
    pyfits.writeto(output_filename,g_final1,clobber=True)
    #-------------------------------------------------------------
    g_final2 = g_images_psf2+g_noise
    output_filename = "/Users/uranus/GitHub/data_arc_finding_cnn/unlensed_output_fits/"+str(i)+"_all_imgs.fits"
    pyfits.writeto(output_filename,g_final2,clobber=True)

    return 0
def main():

    nnn = 512
    dsi = 0.2
    dsx = 0.2
    boxsize = 0.2*nnn
    zzl = 0.5
    zs0 = 10.0

    xi1 = np.linspace(-boxsize/2.0,boxsize/2.0-dsx,nnn)+0.5*dsx
    xi2 = np.linspace(-boxsize/2.0,boxsize/2.0-dsx,nnn)+0.5*dsx
    xi1,xi2 = np.meshgrid(xi1,xi2)

    limages = np.zeros((nnn,nnn))
    #----------------------------------------------------
    # lens parameters for main halo
    xlc1 = -1.5
    xlc2 = 0.0
    ql0 = 0.699999999999
    rc0 = 0.000000000001
    re0 = 6.0
    #phi0 = 30.0
    band = sys.argv[1]

    ai01,ai02 = nie_alphas(xi1,xi2,xlc1,xlc2,re0,rc0,ql0)
    gimage = pyfits.getdata("../gals_lens_galsim/test_"+band+".fits")
    #----------------------------------------------------


    ysc1 = 0.0
    ysc2 = 0.0
    factor_z = 0;
    factor_z0 = mm.Da(zs0)/mm.Da2(zzl,zs0);

    s = " "
    input_fits_dir = "../catsim_galsim_fits/"
    input_fits_sh = s.join(("ls", input_fits_dir,"|grep",band+".fits"))
    input_fits = sp.check_output(input_fits_sh,shell=True)
    input_fits = input_fits.rstrip()
    input_fits_list = input_fits.split("\n")

    for i in input_fits_list:

        array = i.split("_")
        zzsd = np.double(array[1])
        zzsu = np.double(array[2])
        zzs = (zzsd+zzsu)*0.5

        srcs = pyfits.getdata(input_fits_dir+i)
        srcs = np.array(srcs,dtype="<d")

        if zzs < zzl:
            factor_z=0.0
        else:
            factor_z=mm.Da2(zzl,zzs)/mm.Da(zzs)*factor_z0

        #print factor_z

        ai1 = ai01*factor_z
        ai2 = ai02*factor_z

        yi1 = xi1 - ai1
        yi2 = xi2 - ai2

        #g_sn_pin = lv4.call_ray_tracing(g_sn,xi1,xi2,ysc1,ysc2,dsi)
        limages_tmp = lv4.call_ray_tracing(srcs,yi1,yi2,ysc1,ysc2,dsi)
        limages = limages + limages_tmp

    #limages[128:384,128:384] = limages[128:384,128:384]+gimage
    output_file_name = "".join(("../lensed_images/lensed_twinkles_",band,".fits"))
    pyfits.writeto(output_file_name,limages,clobber=True)

    limages[128:384,128:384] = limages[128:384,128:384]+gimage
    output_file_name = "".join(("../final_images/final_twinkles_",band,".fits"))
    pyfits.writeto(output_file_name,limages,clobber=True)

    return 0