def main():

    nnn = 512
    boxsize = 4.0
    zl = 0.1
    zs = 1.0
    p_mass = 1.0
    dsx = boxsize/nnn
    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)
    #----------------------------------------------------
    # lens parameters for main halo
    xlc1 = 0.0
    xlc2 = 0.0
    ql0 = 0.999999999999
    rc0 = 0.000000000001
    re0 = 1.0
    phi0 = 0.0
    lpar = np.asarray([xlc1, xlc2, re0, rc0, ql0, phi0])

    lpars_list = []
    lpars_list.append(lpar)
    #----------------------------------------------------
    sdens = lpar_nie_kappa(xi1,xi2,lpar)
    #pii,aii1,aii2 = multiple_new_nie_all(xi1,xi2,lpars_list)

    phi,phi1,phi2,td = lf.call_all_about_lensing(sdens,nnn,zl,zs,p_mass,dsx)
def main():

    nnn = 512
    boxsize = 4.0
    dsx = boxsize / nnn
    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)

    # 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
    x = 0
    y = 0
    step = 1
    gr_sig = 0.1

    LeftButton = 0

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

    ic = FPS / 4.0

    i = 0

    zl = 0.1
    zs = 1.0
    p_mass = 1.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])
        # ----------------------------------------------
        sdens = lpar_nie_kappa(xi1, xi2, lpar)

        # phi,ai1,ai2,td,mu,kappa = lf.call_all_about_lensing(sdens,nnn,zl,zs,p_mass,dsx)
        phi, ai1, ai2, td = lf.call_all_about_lensing(sdens, nnn, zl, zs, p_mass, dsx)
        yi1 = xi1 - ai2
        yi2 = xi2 - ai1
        g_image, g_lensimage = lensed_images(xi1, xi2, yi1, yi2, gpar)

        base1[:, :, 0] = g_image * 256
        base1[:, :, 1] = g_image * 256
        base1[:, :, 2] = g_image * 256

        # sktd = (td-td.min())/(td.max()-td.min())*ic/2
        sktd = (td) / (1.5) * ic / 2
        itmp = (i + FPS / 8 - sktd) % (FPS)
        ratio = (ic - itmp) * itmp / (ic / 2.0) ** 2.0
        ratio[ratio < 0] = 0.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

        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():

    sdens = pyfits.getdata("/Users/uranus/Desktop/hlsp_frontier_model_abell2744_cats_v1_kappa.fits")

    print type(sdens)
    kappa = np.array(sdens,dtype='<d')

    #sdens = sdens.astype('<double')

    nnn = np.shape(kappa)[0]
    boxsize = 4.0
    zl = 0.1
    zs = 1.0
    p_mass = 1.0
    dsx = boxsize/nnn
    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)
    #----------------------------------------------------
    # lens parameters for main halo
    xlc1 = 0.0
    xlc2 = 0.0
    ql0 = 0.999999999999
    rc0 = 0.000000000001
    re0 = 1.0
    phi0 = 0.0
    lpar = np.asarray([xlc1, xlc2, re0, rc0, ql0, phi0])

    lpars_list = []
    lpars_list.append(lpar)
    #----------------------------------------------------
    #sdens = lpar_nie_kappa(xi1,xi2,lpar)
    #pii,pii1,pii2 = multiple_new_nie_all(xi1,xi2,lpars_list)

    phi,phi1,phi2,td = lf.call_all_about_lensing(kappa,nnn,zl,zs,p_mass,dsx)

    phi2,phi1 = np.gradient(phi,dsx)
    phi12,phi11 = np.gradient(phi1,dsx)
    phi22,phi21 = np.gradient(phi2,dsx)
    kappac = 0.5*(phi11+phi22)
    mu = 1.0/(1.0-(phi11+phi22)+phi11*phi22-phi12*phi21)
    critical = lf.call_find_critical_curve(mu)
    pl.figure(figsize=(10,10))
    pl.contour(critical)

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

    #sdens = lpar_nie_kappa(xi1,xi2,lpar)

    #pii,pii1,pii2 = multiple_new_nie_all(xi1,xi2,lpars_list)

    #phi,alpha1,alpha2,td = lf.call_all_about_lensing(sdens,nnn,zl,zs,p_mass,dsx)

    #phi12,phi11 = np.gradient(alpha2,dsx)
    #phi22,phi21 = np.gradient(alpha1,dsx)
    #kappai = 0.5*(phi11+phi22)
#--------------------------------------------------------------------
    #sdens_pad = np.zeros((nnn*2,nnn*2))
    #sdens_pad[nnn/2:nnn/2*3,nnn/2:nnn/2*3] = sdens
    #green_in = green_iso(nnn*2,dsx)
    #phi,alpha1,alpha2,td,mu,kappas = fft_lensing_signals(sdens_pad,green_in,dsx)
#--------------------------------------------------------------------
    #Kc = 1.0
    ##Kc = (1.0+zl)/c*(Dl*Ds/Dls)
    #td = Kc*(0.5*((phi1)**2.0+(phi2)**2.0)-pii)
    #tdi = Kc*(0.5*((pii1)**2.0+(pii2)**2.0)-pii)

    ##levels = [-1.6,-1.2,-0.8,-0.4,0.0,0.4,0.8,1.2,1.6]
    #pl.figure()
    #pl.contourf(xi1,xi2,np.log10(kappai))
    #pl.colorbar()
    #pl.figure()
    #pl.contourf(xi1,xi2,np.log10(kappas))
    #pl.colorbar()

    #pl.figure()
    #pl.imshow((np.sqrt(phi1*phi1+phi2*phi2)-np.sqrt(aii1*aii1+aii2*aii2))/np.sqrt(phi1*phi1+phi2*phi2), aspect='auto', cmap=pl.get_cmap(pl.cm.jet),vmin=-0.01, vmax=0.01 )
    #pl.colorbar()

    ##levels = [-0.8,-0.4,0.0,0.4,0.8,1.2,1.6,2.0]
    #pl.figure()
    ##pl.contourf(xi1,xi2,np.log10(kappac),levels)
    ##pl.colorbar()
    ##print np.max((kappac-sdens)/sdens),np.mean((kappac-sdens)/sdens)
    #pl.imshow((kappac-sdens)/sdens, aspect='auto', cmap=pl.get_cmap(pl.cm.jet),vmin=-0.01, vmax=0.01)
    #pl.colorbar()
    ##pl.contour(xi1,xi2,np.log10(kappac),levels,colors=['k',])
    ##pl.contour(xi1,xi2,np.log10(sdens),levels,colors=['r',])
    pl.plot(kappac[nnn/2-1,:],'k-')
    pl.plot(sdens[nnn/2-1,:],'r-')
    pl.show()

    ##levels = [3.0,2.5,2.0,1.5,1.0,0.5,0.0,-0.5]
    #pl.figure()
    ##pl.contour(xi1,xi2,phi,levels,colors=['k',])
    ##pl.contour(xi1,xi2,pii,levels,colors=['r',])
    ###pl.imshow((phi-(np.median(phi-pii))-pii)/phi, aspect='auto', cmap=pl.get_cmap(pl.cm.jet),vmin=-0.01, vmax=0.01)
    ###pl.imshow((phi-pii)/phi, aspect='auto', cmap=pl.get_cmap(pl.cm.jet))#,vmin=-0.01, vmax=0.01)
    ##pl.colorbar()
    #pl.plot(pii[nnn/2-1,:],'k-')
    #pl.plot(phi[nnn/2-1,:],'r-')
    #pl.show()

    #levels = [-1.6,-1.2,-0.8,-0.4,0.0,0.4,0.8,1.2,1.6]
    #pl.figure()
    ##pl.contour(xi1,xi2,np.sqrt(pii2**2.0+pii1**2.0),levels,colors=['k',])
    ##pl.contour(xi1,xi2,np.sqrt(phi2**2.0+phi1**2.0),levels,colors=['r',])
    ##pl.colorbar()
    #pl.plot(np.sqrt(pii2**2.0+pii1**2.0)[nnn/2-1,:],'k-')
    #pl.plot(np.sqrt(phi2**2.0+phi1**2.0)[nnn/2-1,:],'r-')


    #levels = [-2.0,-1.5,-1.0,-0.5,0.0,0.5,1.0,1.5,2.0]
    #pl.figure()
    #pl.contourf(xi1,xi2,td,levels)#,colors=['k',])
    #pl.colorbar()
    #pl.figure()
    #pl.contourf(xi1,xi2,tdi,levels)#,colors=['r',])
    #pl.colorbar()


    ##----------------------------------------------------
    #data = np.fromfile("./out_iso.bin",dtype=np.double)
    #data = data.reshape((np.sqrt(len(data)),np.sqrt(len(data))))
    #pl.figure()
    #pl.contourf(data)
    #pl.colorbar()

    pl.show()
def main():

    #nnn = 512
    #boxsize = 4.0
    #dsx = boxsize/nnn
    #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)


    #cc = find_critical_curve(mu)
    import sys
    filename = sys.argv[1]

    #sdens = pyfits.getdata("/Users/uranus/Desktop/hlsp_frontier_model_abell2744_cats_v1_kappa.fits")
    sdens = pyfits.getdata(filename)
    kappa0 = np.array(sdens,dtype='<d')
    kappa=congrid.congrid(kappa0,[768,768])
    nnn = np.shape(kappa)[0]
    boxsize = 4.0

    dsx = boxsize/nnn
    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)

    pygame.init()
    FPS = 30
    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')
    base3 = np.zeros((nnn,nnn,3),'uint8')
    base4 = 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
    x = 0
    y = 0
    gr_sig = dsx*2

    LeftButton=0

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

    ic = FPS/4.0

    i = 0

    zl = 0.1
    zs = 1.0
    p_mass = 1.0
    #----------------------------------------------------
    phi,phi1,phi2,td = lf.call_all_about_lensing(kappa,nnn,zl,zs,p_mass,dsx)

    phi2,phi1 = np.gradient(phi,dsx)

    phi12,phi11 = np.gradient(phi1,dsx)
    phi22,phi21 = np.gradient(phi2,dsx)
    #kappac = 0.5*(phi11+phi22)
    mu = 1.0/(1.0-(phi11+phi22)+phi11*phi22-phi12*phi21)
    critical = lf.call_find_critical_curve(mu)

    yi1 = xi1-phi1
    yi2 = xi2-phi2

    idx = critical > 0.0
    yif1 = yi1[idx]
    yif2 = yi2[idx]
    caustic = lf.call_find_caustic(yif1,yif2,nnn,nnn,boxsize)

    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*dsx
                    if gr_sig <0.01:
                        gr_sig = 0.01

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

        #----------------------------------------------
        #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])
        #----------------------------------------------
        g_image,g_lensimage = lensed_images(xi1,xi2,yi1,yi2,gpar)

        base1[:,:,0] = g_image*256
        base1[:,:,1] = g_image*256
        base1[:,:,2] = g_image*256

        ##sktd = (td-td.min())/(td.max()-td.min())*ic/2
        #sktd = (td)/(1.5)*ic/2
        #itmp = (i+FPS/8-sktd)%(FPS)
        #ratio = (ic-itmp)*itmp/(ic/2.0)**2.0
        #ratio[ratio<0]=0.0

        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

        base3[:,:,0] = critical*255
        base3[:,:,1] = critical*0
        base3[:,:,2] = critical*0

        base4[:,:,0] = caustic.T*0
        base4[:,:,1] = caustic.T*255
        base4[:,:,2] = caustic.T*0

        base = base1+base2+base3+base4

        #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)