def extinction_correct(band_id, sub_z, sub_ra, sub_dec):
	tot_N = len(sub_z)
	ii = np.int(band_id)
	for jj in range(tot_N):
		ra_g = sub_ra[jj]
		dec_g = sub_dec[jj]
		z_g = sub_z[jj]

		file = dfile + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (band[ii], ra_g, dec_g, z_g)
		data = fits.open(file)
		img = data[0].data
		head_inf = data[0].header
		wcs = awc.WCS(head_inf)

		x0 = np.linspace(0, img.shape[1] - 1, img.shape[1])
		y0 = np.linspace(0, img.shape[0] - 1, img.shape[0])
		img_grid = np.array(np.meshgrid(x0, y0))
		ra_img, dec_img = wcs.all_pix2world(img_grid[0,:], img_grid[1,:], 1)
		pos = SkyCoord(ra_img, dec_img, frame = 'fk5', unit = 'deg')
		BEV = sfd(pos)
		Av = Rv * BEV
		Al = A_wave(l_wave[ii], Rv) * Av
		correct_img = img * 10**(Al / 2.5)

		## save the extinction correct data
		hdu = fits.PrimaryHDU()
		hdu.data = correct_img
		hdu.header = head_inf
		hdu.writeto(tmp + 'test/Extinction_correct_%s_ra%.3f_dec%.3f_z%.3f.fits'%(band[ii], ra_g, dec_g, z_g), overwrite = True)
def single_map_func(band_str, ra_lis, dec_lis, z_lis, img_file, out_file):

    l_x0 = np.linspace(0, 2047, 2048)
    l_y0 = np.linspace(0, 1488, 1489)
    img_grid = np.array(np.meshgrid(l_x0, l_y0))

    Ns = len(z_lis)

    for jj in range(Ns):

        ra_g, dec_g, z_g = ra_lis[jj], dec_lis[jj], z_lis[jj]

        img_data = fits.open(img_file % (band_str, ra_g, dec_g, z_g), )

        img_arr = img_data[0].data
        header = img_data[0].header
        wcs_lis = awc.WCS(header)

        ra_img, dec_img = wcs_lis.all_pix2world(img_grid[0, :], img_grid[1, :],
                                                0)
        pos_img = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')

        p_EBV = E_map.ebv(pos_img)
        A_v = Rv * p_EBV
        A_l = A_wave(l_wave[kk], Rv) * A_v

        ## save the extinction correct data
        hdu = fits.PrimaryHDU()
        hdu.data = A_l
        hdu.header = header
        hdu.writeto(
            out_file % (band_str, ra_g, dec_g, z_g),
            overwrite=True,
        )

    return
def mask_A(band_id, z_set, ra_set, dec_set):

    kk = np.int(band_id)
    Nz = len(z_set)
    param_A = 'default_mask_A.sex'
    out_cat = 'default_mask_A.param'
    out_load_A = '/mnt/ddnfs/data_users/cxkttwl/PC/A_mask_%d_cpus.cat' % rank
    ## size test
    r_res = 1.  # 2.8 for larger R setting
    for q in range(Nz):
        z_g = z_set[q]
        ra_g = ra_set[q]
        dec_g = dec_set[q]

        print('Now band is', band[kk])
        print('*' * 20)
        pro_f = d_file + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (
            band[kk], ra_g, dec_g, z_g)

        data_f = fits.open(pro_f)
        img = data_f[0].data
        head_inf = data_f[0].header
        wcs = awc.WCS(head_inf)
        cx_BCG, cy_BCG = wcs.all_world2pix(ra_g * U.deg, dec_g * U.deg, 1)
        R_ph = rad2asec / (Test_model.angular_diameter_distance(z_g).value)
        R_p = R_ph / pixel

        x0 = np.linspace(0, img.shape[1] - 1, img.shape[1])
        y0 = np.linspace(0, img.shape[0] - 1, img.shape[0])
        img_grid = np.array(np.meshgrid(x0, y0))
        ra_img, dec_img = wcs.all_pix2world(img_grid[0, :], img_grid[1, :], 1)
        pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
        BEV = sfd(pos)
        Av = Rv * BEV * 0.86
        Al = A_wave(l_wave[kk], Rv) * Av
        img = img * 10**(Al / 2.5)

        hdu = fits.PrimaryHDU()
        hdu.data = img
        hdu.header = head_inf
        hdu.writeto('/mnt/ddnfs/data_users/cxkttwl/PC/source_data_%d.fits' %
                    rank,
                    overwrite=True)

        file_source = '/mnt/ddnfs/data_users/cxkttwl/PC/source_data_%d.fits' % rank
        cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s' % (
            param_A, out_load_A, out_cat)
        a = subpro.Popen(cmd, shell=True)
        a.wait()

        source = asc.read(out_load_A)
        Numb = np.array(source['NUMBER'][-1])
        A = np.array(source['A_IMAGE'])
        B = np.array(source['B_IMAGE'])
        theta = np.array(source['THETA_IMAGE'])
        cx = np.array(source['X_IMAGE']) - 1
        cy = np.array(source['Y_IMAGE']) - 1
        p_type = np.array(source['CLASS_STAR'])

        Kron = 6 * r_res  # iso_radius set as 3 times rms (2.8 from size test)
        a = Kron * A
        b = Kron * B

        mask = load + 'bright_star_dr12/source_SQL_Z%.3f_ra%.3f_dec%.3f.txt' % (
            z_g, ra_g, dec_g)
        cat = pds.read_csv(mask, skiprows=1)
        set_ra = np.array(cat['ra'])
        set_dec = np.array(cat['dec'])
        set_mag = np.array(cat['r'])
        OBJ = np.array(cat['type'])
        xt = cat['Column1']
        tau = 10 * r_res  # the mask size set as 10 * FWHM from dr12

        set_A = np.array([
            cat['psffwhm_r'], cat['psffwhm_g'], cat['psffwhm_i']
        ]) * tau / pixel
        set_B = np.array([
            cat['psffwhm_r'], cat['psffwhm_g'], cat['psffwhm_i']
        ]) * tau / pixel
        set_chi = np.zeros(set_A.shape[1], dtype=np.float)

        lln = np.array([
            len(set_A[:, ll][set_A[:, ll] > 0]) for ll in range(set_A.shape[1])
        ])
        lr_iso = np.array(
            [np.max(set_A[:, ll]) for ll in range(set_A.shape[1])])
        sr_iso = np.array(
            [np.max(set_B[:, ll]) for ll in range(set_B.shape[1])])
        # bright stars
        x, y = wcs.all_world2pix(set_ra * U.deg, set_dec * U.deg, 1)
        ia = (x >= 0) & (x <= img.shape[1])
        ib = (y >= 0) & (y <= img.shape[0])
        ie = (set_mag <= 20)
        iq = lln >= 2
        ig = OBJ == 6
        ic = (ia & ib & ie & ig & iq)
        sub_x0 = x[ic]
        sub_y0 = y[ic]
        sub_A0 = lr_iso[ic]
        sub_B0 = sr_iso[ic]
        sub_chi0 = set_chi[ic]

        # saturated source(may not stars)
        xa = ['SATURATED' in qq for qq in xt]
        xv = np.array(xa)
        idx = xv == True
        ipx = (idx & ia & ib)

        sub_x2 = x[ipx]
        sub_y2 = y[ipx]
        sub_A2 = 3 * lr_iso[ipx]
        sub_B2 = 3 * sr_iso[ipx]
        sub_chi2 = set_chi[ipx]

        comx = np.r_[sub_x0[sub_A0 > 0], sub_x2[sub_A2 > 0]]
        comy = np.r_[sub_y0[sub_A0 > 0], sub_y2[sub_A2 > 0]]
        Lr = np.r_[sub_A0[sub_A0 > 0], sub_A2[sub_A2 > 0]]
        Sr = np.r_[sub_B0[sub_A0 > 0], sub_B2[sub_A2 > 0]]
        phi = np.r_[sub_chi0[sub_A0 > 0], sub_chi2[sub_A2 > 0]]

        cx = np.r_[cx, comx]
        cy = np.r_[cy, comy]
        a = np.r_[a, Lr]
        b = np.r_[b, Sr]
        theta = np.r_[theta, phi]
        Numb = Numb + len(comx)

        mask_A = np.ones((img.shape[0], img.shape[1]), dtype=np.float)
        ox = np.linspace(0, img.shape[1] - 1, img.shape[1])
        oy = np.linspace(0, img.shape[0] - 1, img.shape[0])
        basic_coord = np.array(np.meshgrid(ox, oy))
        major = a / 2
        minor = b / 2  # set the star mask based on the major and minor radius
        senior = np.sqrt(major**2 - minor**2)

        tdr = np.sqrt((cx - cx_BCG)**2 + (cy - cy_BCG)**2)
        dr00 = np.where(tdr == np.min(tdr))[0]

        for k in range(Numb):
            xc = cx[k]
            yc = cy[k]

            lr = major[k]
            sr = minor[k]
            cr = senior[k]
            chi = theta[k] * np.pi / 180

            set_r = np.int(np.ceil(1.2 * lr))
            la0 = np.max([np.int(xc - set_r), 0])
            la1 = np.min([np.int(xc + set_r + 1), img.shape[1] - 1])
            lb0 = np.max([np.int(yc - set_r), 0])
            lb1 = np.min([np.int(yc + set_r + 1), img.shape[0] - 1])

            if k == dr00[0]:
                continue
            else:
                df1 = (basic_coord[0, :][lb0:lb1, la0:la1] -
                       xc) * np.cos(chi) + (basic_coord[1, :][lb0:lb1, la0:la1]
                                            - yc) * np.sin(chi)
                df2 = (basic_coord[1, :][lb0:lb1, la0:la1] -
                       yc) * np.cos(chi) - (basic_coord[0, :][lb0:lb1, la0:la1]
                                            - xc) * np.sin(chi)
                fr = df1**2 / lr**2 + df2**2 / sr**2
                jx = fr <= 1

                iu = np.where(jx == True)
                iv = np.ones((jx.shape[0], jx.shape[1]), dtype=np.float)
                iv[iu] = np.nan
                mask_A[lb0:lb1, la0:la1] = mask_A[lb0:lb1, la0:la1] * iv

        mirro_A = mask_A * img

        hdu = fits.PrimaryHDU()
        hdu.data = mirro_A
        hdu.header = head_inf
        hdu.writeto(
            load +
            'mask_data/A_plane/1.5sigma/A_mask_data_%s_ra%.3f_dec%.3f_z%.3f.fits'
            % (band[kk], ra_g, dec_g, z_g),
            overwrite=True)
        '''
		plt.figure()
		ax = plt.imshow(mirro_A, cmap = 'Greys', origin = 'lower', vmin = 1e-3, norm = mpl.colors.LogNorm())
		plt.colorbar(ax, fraction = 0.035, pad =  0.01, label = '$flux[nmaggy]$')

		hsc.circles(cx_BCG, cy_BCG, s = R_p, fc = '', ec = 'b', )
		hsc.circles(cx_BCG, cy_BCG, s = 1.1 * R_p, fc = '', ec = 'b', ls = '--')
		plt.scatter(cx_BCG, cy_BCG, s = 10, marker = 'X', facecolors = '', edgecolors = 'r', linewidth = 0.5, alpha = 0.5)
		plt.title('A mask img ra%.3f dec%.3f z%.3f in %s band' % (ra_g, dec_g, z_g, band[kk] ) )
		plt.xlim(0, mirro_A.shape[1])
		plt.ylim(0, mirro_A.shape[0])
		plt.savefig(
			'/mnt/ddnfs/data_users/cxkttwl/ICL/fig_class/A_mask/A_mask_%s_ra%.3f_dec%.3f_z%.3f.png'%(band[kk], ra_g, dec_g, z_g), dpi = 300)
		plt.close()
		'''
    return
Beispiel #4
0
def source_compa():
    bins = 90
    kd = -20

    zg = z[kd]
    rag = ra[kd]
    decg = dec[kd]
    x0 = np.linspace(0, 2047, 2048)
    y0 = np.linspace(0, 1488, 1489)
    grd = np.array(np.meshgrid(x0, y0))

    param_A = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.sex'
    out_cat = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.param'
    out_load_A = '/home/xkchen/mywork/ICL/data/SEX/result/mask_A_test.cat'

    tmp_load = '/home/xkchen/mywork/ICL/data/test_data/'
    load = '/home/xkchen/mywork/ICL/data/total_data/sample_02_03/'

    file = 'frame-r-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (rag, decg, zg)
    data = fits.open(load + file)
    img = data[0].data
    Head = data[0].header
    wcs = awc.WCS(Head)
    Da = Test_model.angular_diameter_distance(zg).value
    Ar = rad2asec / Da
    Rp = Ar / pixel
    cx_BCG, cy_BCG = wcs.all_world2pix(rag * U.deg, decg * U.deg, 1)

    ra_img, dec_img = wcs.all_pix2world(grd[0, :], grd[1, :], 1)
    pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
    EBV = sfd(pos)
    Av = Rv * EBV * 0.86
    Al = A_wave(l_wave[2], Rv) * Av
    img1 = img * 10**(Al / 2.5)
    ## threshold 1.3sigma
    hdu = fits.PrimaryHDU()
    hdu.data = img1
    hdu.header = Head
    hdu.writeto(tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' %
                ('r', rag, decg, zg),
                overwrite=True)

    file_source = tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' % (
        'r', rag, decg, zg)
    cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s -DETECT_THRESH %s -ANALYSIS_THRESH %s' % (
        param_A, out_load_A, out_cat, '1.5', '1.5')
    print(cmd)
    a = subpro.Popen(cmd, shell=True)
    a.wait()

    source = asc.read(out_load_A)
    Numb = np.array(source['NUMBER'][-1])
    A = np.array(source['A_IMAGE'])
    B = np.array(source['B_IMAGE'])
    chi1 = np.array(source['THETA_IMAGE'])
    cx1 = np.array(source['X_IMAGE']) - 1
    cy1 = np.array(source['Y_IMAGE']) - 1

    Kron = 5
    Lr1 = Kron * A
    Sr1 = Kron * B

    file_source = tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' % (
        'r', rag, decg, zg)
    cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s -DETECT_THRESH %s -ANALYSIS_THRESH %s' % (
        param_A, out_load_A, out_cat, '1.3', '1.3')
    print(cmd)
    a = subpro.Popen(cmd, shell=True)
    a.wait()

    source = asc.read(out_load_A)
    Numb = np.array(source['NUMBER'][-1])
    A = np.array(source['A_IMAGE'])
    B = np.array(source['B_IMAGE'])
    chi2 = np.array(source['THETA_IMAGE'])
    cx2 = np.array(source['X_IMAGE']) - 1
    cy2 = np.array(source['Y_IMAGE']) - 1

    Kron = 7
    Lr2 = Kron * A
    Sr2 = Kron * B

    a0 = np.max([cx_BCG - 1.1 * Rp, 0])
    a1 = np.min([cx_BCG + 1.1 * Rp, 2047])
    b0 = np.max([cy_BCG - 1.1 * Rp, 0])
    b1 = np.min([cy_BCG + 1.1 * Rp, 1488])

    plt.figure()
    plt.title(r'$Cluster \; ra%.3f \; dec%.3f \; z%.3f$' % (rag, decg, zg))
    plt.imshow(img1,
               cmap='Greys',
               vmin=1e-3,
               origin='lower',
               norm=mpl.colors.LogNorm())
    hsc.circles(cx_BCG, cy_BCG, s=Rp, fc='', ec='b', alpha=0.5)
    hsc.ellipses(cx1,
                 cy1,
                 w=Lr1,
                 h=Sr1,
                 rot=chi1,
                 fc='',
                 ec='g',
                 ls='-',
                 linewidth=0.5,
                 label=r'$1.5 \sigma$',
                 alpha=0.5)
    hsc.ellipses(cx2,
                 cy2,
                 w=Lr2,
                 h=Sr2,
                 rot=chi2,
                 fc='',
                 ec='r',
                 ls='-',
                 linewidth=0.5,
                 label=r'$1.3\sigma$',
                 alpha=0.5)
    plt.xlim(a0, a1)
    plt.ylim(b0, b1)
    plt.savefig('/home/xkchen/mywork/ICL/code/source_C_ra%.3f_dec%.3f.png' %
                (rag, decg),
                dpi=600)
    plt.show()

    raise
    return
Beispiel #5
0
def stack_test():

    x0 = 2427
    y0 = 1765
    Nx = np.linspace(0, 4854, 4855)
    Ny = np.linspace(0, 3530, 3531)
    sum_grid = np.array(np.meshgrid(Nx, Ny))

    sum_array_0 = np.zeros((len(Ny), len(Nx)), dtype=np.float)
    count_array_0 = np.ones((len(Ny), len(Nx)), dtype=np.float) * np.nan
    p_count_0 = np.zeros((len(Ny), len(Nx)), dtype=np.float)

    SB_ref = []
    Ar_ref = []
    bins = 60
    for kd in range(stack_N):

        zg = z[kd]
        rag = ra[kd]
        decg = dec[kd]

        param_A = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.sex'
        out_cat = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.param'
        out_load_A = '/home/xkchen/mywork/ICL/data/SEX/result/mask_A_test.cat'

        tmp_load = '/home/xkchen/mywork/ICL/data/test_data/'
        load = '/home/xkchen/mywork/ICL/data/total_data/sample_02_03/'

        file = 'frame-r-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (rag, decg, zg)
        data = fits.getdata(load + file, header=True)
        img = data[0]
        Head = data[1]
        wcs = awc.WCS(Head)
        Da = Test_model.angular_diameter_distance(zg).value
        Ar = rad2asec / Da
        Rp = Ar / pixel

        xt = np.linspace(0, img.shape[1] - 1, img.shape[1])
        yt = np.linspace(0, img.shape[0] - 1, img.shape[0])
        grd = np.array(np.meshgrid(xt, yt))
        cx_BCG, cy_BCG = wcs.all_world2pix(rag * U.deg, decg * U.deg, 1)

        ra_img, dec_img = wcs.all_pix2world(grd[0, :], grd[1, :], 1)
        pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
        EBV = sfd(pos)
        Av = Rv * EBV * 0.86
        Al = A_wave(l_wave[2], Rv) * Av
        img1 = img * 10**(Al / 2.5)

        imgt = flux_recal(img1, zg, z_ref)
        eta = Da_ref / Da
        mu = 1 / eta
        xn, yn, resam = gen(imgt, 1, eta, cx_BCG, cy_BCG)
        xn = np.int(xn)
        yn = np.int(yn)
        if eta > 1:
            resam = resam[1:, 1:]
        elif eta == 1:
            resam = resam[1:-1, 1:-1]
        else:
            resam = resam

        ix0 = np.int(Head['CRPIX1'] * mu)
        iy0 = np.int(Head['CRPIX2'] * mu)
        RA0 = Head['CRVAL1']
        DEC0 = Head['CRVAL2']

        keys = [
            'SIMPLE', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', 'CRPIX1',
            'CRPIX2', 'CENTER_X', 'CENTER_Y', 'CRVAL1', 'CRVAL2', 'CENTER_RA',
            'CENTER_DEC', 'ORIGN_Z'
        ]
        value = [
            'T', 32, 2, resam.shape[1], resam.shape[0], ix0, iy0, xn, yn, RA0,
            DEC0, rag, decg, zg
        ]
        ff = dict(zip(keys, value))
        fil = fits.Header(ff)
        fits.writeto(tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' %
                     ('r', rag, decg, zg),
                     resam,
                     header=fil,
                     overwrite=True)

        file_source = tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' % (
            'r', rag, decg, zg)
        cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s' % (
            param_A, out_load_A, out_cat)
        print(cmd)
        A = subpro.Popen(cmd, shell=True)
        A.wait()

        source = asc.read(out_load_A)
        Numb = np.array(source['NUMBER'][-1])
        A = np.array(source['A_IMAGE'])
        B = np.array(source['B_IMAGE'])
        chi = np.array(source['THETA_IMAGE'])
        cx = np.array(source['X_IMAGE']) - 1
        cy = np.array(source['Y_IMAGE']) - 1

        Kron = 6
        Lr = Kron * A
        Sr = Kron * B

        CX = cx * 1
        CY = cy * 1
        a = Lr * 1
        b = Sr * 1
        theta = chi * 1

        mask_A = np.ones((resam.shape[0], resam.shape[1]), dtype=np.float)
        ox = np.linspace(0, resam.shape[1] - 1, resam.shape[1])
        oy = np.linspace(0, resam.shape[0] - 1, resam.shape[0])
        basic_coord = np.array(np.meshgrid(ox, oy))
        major = a / 2
        minor = b / 2
        senior = np.sqrt(major**2 - minor**2)

        tdr = np.sqrt((CX - xn)**2 + (CY - yn)**2)
        dr00 = np.where(tdr == np.min(tdr))[0]
        for k in range(Numb):
            xc = CX[k]
            yc = CY[k]
            lr = major[k]
            sr = minor[k]
            cr = senior[k]

            set_r = np.int(np.ceil(1.2 * lr))
            la0 = np.int(xc - set_r)
            la1 = np.int(xc + set_r + 1)
            lb0 = np.int(yc - set_r)
            lb1 = np.int(yc + set_r + 1)

            if k == dr00[0]:
                continue
            else:
                phi = theta[k] * np.pi / 180
                df1 = lr**2 - cr**2 * np.cos(phi)**2
                df2 = lr**2 - cr**2 * np.sin(phi)**2
                fr = ((basic_coord[0, :][lb0:lb1, la0:la1] - xc)**2 * df1 +
                      (basic_coord[1, :][lb0:lb1, la0:la1] - yc)**2 * df2 -
                      cr**2 * np.sin(2 * phi) *
                      (basic_coord[0, :][lb0:lb1, la0:la1] - xc) *
                      (basic_coord[1, :][lb0:lb1, la0:la1] - yc))
                idr = fr / (lr**2 * sr**2)
                jx = idr < 1

                iu = np.where(jx == True)
                iv = np.ones((jx.shape[0], jx.shape[1]), dtype=np.float)
                iv[iu] = np.nan
                mask_A[lb0:lb1, la0:la1] = mask_A[lb0:lb1, la0:la1] * iv

        mirro_A = mask_A * resam

        plt.figure()
        ax = plt.subplot(111)
        ax.set_title('source mask after resample %d' % kd)
        ax.imshow(resam,
                  cmap='Greys',
                  vmin=1e-3,
                  origin='lower',
                  norm=mpl.colors.LogNorm())
        hsc.ellipses(CX, CY, w=a, h=b, rot=theta, fc='', ec='r', alpha=0.5)
        plt.xlim(0, resam.shape[1])
        plt.ylim(0, resam.shape[0])
        plt.savefig('/home/xkchen/mywork/ICL/code/source_%d.png' % kd, dpi=300)
        plt.close()

        ox = np.linspace(0, resam.shape[1] - 1, resam.shape[1])
        oy = np.linspace(0, resam.shape[0] - 1, resam.shape[0])
        oo_grd = np.array(np.meshgrid(ox, oy))
        cdr = np.sqrt((oo_grd[0, :] - xn)**2 + (oo_grd[1, :] - yn)**2)
        idd = (cdr > Rpp) & (cdr < 1.1 * Rpp)
        cut_res = mirro_A[idd]
        id_nan = np.isnan(cut_res)
        idx = np.where(id_nan == False)
        bl_array = cut_res[idx]
        back_cc = np.mean(bl_array)

        mirroA = mirro_A - back_cc
        SB2, R2, Anr2, err2 = light_measure(mirroA, bins, 1, Rpp, xn, yn,
                                            pixel, z_ref)
        SB_ = SB2[1:]
        Ar_ = Anr2[1:]
        R_ = R2[1:]
        err_ = err2[1:]
        SB_ref.append(SB_)
        Ar_ref.append(Ar_)

        la0 = np.int(y0 - yn)
        la1 = np.int(y0 - yn + resam.shape[0])
        lb0 = np.int(x0 - xn)
        lb1 = np.int(x0 - xn + resam.shape[1])

        idx = np.isnan(resam)
        idv = np.where(idx == False)
        sum_array_0[la0:la1,
                    lb0:lb1][idv] = sum_array_0[la0:la1,
                                                lb0:lb1][idv] + mirroA[idv]
        count_array_0[la0:la1, lb0:lb1][idv] = resam[idv]
        id_nan = np.isnan(count_array_0)
        id_fals = np.where(id_nan == False)
        p_count_0[id_fals] = p_count_0[id_fals] + 1
        count_array_0[la0:la1, lb0:lb1][idv] = np.nan

    ll0 = [np.min(kk / Angu_ref) for kk in Ar_ref]
    tar0 = np.min(ll0)
    ll1 = [np.max(kk / Angu_ref) for kk in Ar_ref]
    tar1 = np.max(ll1)

    tar_down = tar0 * Angu_ref
    tar_up = tar1 * Angu_ref
    inter_frac = np.logspace(np.log10(tar0), np.log10(tar1), bins)
    inter_ar = inter_frac * Angu_ref

    m_flux = np.ones((stack_N, bins), dtype=np.float) * np.nan
    for pp in range(len(SB_ref)):
        id_count = np.zeros(bins, dtype=np.float)
        tsb = SB_ref[pp]
        tar = Ar_ref[pp] / Angu_ref
        t_flux = 10**((22.5 + 2.5 * np.log10(pixel**2) - tsb) / 2.5)
        for kk in range(len(tar)):
            sub_ar = np.abs(inter_frac - tar[kk])
            id_min = np.where(sub_ar == np.min(sub_ar))[0]
            id_count[id_min[0]] = id_count[id_min[0]] + 1
        id_nuzero = id_count != 0
        id_g = np.where(id_nuzero == True)[0]
        m_flux[pp, id_g] = t_flux

    m_count = np.zeros(bins, dtype=np.float)
    inter_flux = np.zeros(bins, dtype=np.float)
    for pp in range(bins):
        sub_flux = m_flux[:, pp]
        iy = np.isnan(sub_flux)
        iv = np.where(iy == False)[0]
        m_count[pp] = len(iv)
        inter_flux[pp] = inter_flux[pp] + np.sum(sub_flux[iv])
    inter_flux = inter_flux / m_count

    id_nan = np.isnan(inter_flux)
    id_x = id_nan == False
    id_inf = np.isinf(inter_flux)
    id_y = id_inf == False
    id_zero = inter_flux == 0
    id_z = id_zero == False
    id_set = id_x & id_y & id_z

    ref_ar = inter_ar[id_set]
    ref_flux = inter_flux[id_set]
    ref_SB = 22.5 - 2.5 * np.log10(ref_flux) + 2.5 * np.log10(pixel**2)
    f_SB = interp(ref_ar, ref_SB, kind='cubic')

    mean_array_0 = sum_array_0 / p_count_0
    where_are_inf = np.isinf(mean_array_0)
    mean_array_0[where_are_inf] = np.nan
    id_zeros = np.where(p_count_0 == 0)
    mean_array_0[id_zeros] = np.nan

    SB, R, Ar, error = light_measure(mean_array_0, bins, 1, Rpp, x0, y0, pixel,
                                     z_ref)
    SB_0 = SB[1:] + 0
    R_0 = R[1:]
    Ar_0 = Ar[1:]
    err_0 = error[1:]
    Ar0 = (Ar_0 / Angu_ref) * Angu_ref

    plt.figure(figsize=(16, 8))
    gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])
    ax = plt.subplot(gs[0])
    bx = plt.subplot(gs[1])

    ax.plot(ref_ar, ref_SB, 'r-', label='$stacking \, profile$', alpha=0.5)
    ax.plot(Ar0, SB_0, 'g--', label='$stacking \, img$', alpha=0.5)

    bx.plot(Ar0[(Ar0 > tar_down * 1.01) & (Ar0 < tar_up / 1.01)],
            SB_0[(Ar0 > tar_down * 1.01) & (Ar0 < tar_up / 1.01)] -
            f_SB(Ar0[(Ar0 > tar_down * 1.01) & (Ar0 < tar_up / 1.01)]),
            'b*',
            alpha=0.5)

    ax.set_xscale('log')
    ax.set_xlabel('$R[arcsec]$')
    ax.set_ylabel('$SB[mag/arcsec^2]$')
    ax.tick_params(axis='both', which='both', direction='in')
    ax.legend(loc=1, fontsize=12)
    ax.invert_yaxis()

    bx.set_xlabel('$R[arcsec]$')
    bx.set_xscale('log')
    bx.set_ylabel('$ \Delta{SB}[mag/arcsec^2] $')
    bx.tick_params(axis='both', which='both', direction='in')

    plt.subplots_adjust(hspace=0)
    plt.tight_layout()
    plt.savefig('/home/xkchen/mywork/ICL/code/stack_test.png', dpi=300)
    plt.close()

    raise
    return
Beispiel #6
0
def resamp_test():
    for kd in range(10):

        z_ref = 0.25
        Da_ref = Test_model.angular_diameter_distance(z_ref).value
        Angu_ref = (R0 / Da_ref) * rad2asec
        Rpp = Angu_ref / pixel

        bins = 50

        x0 = np.linspace(0, 2047, 2048)
        y0 = np.linspace(0, 1488, 1489)
        grd = np.array(np.meshgrid(x0, y0))
        r_star = 2 * 1.5 / pixel

        zg = z[kd]
        rag = ra[kd]
        decg = dec[kd]

        param_A = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.sex'
        out_cat = '/home/xkchen/mywork/ICL/data/SEX/default_mask_A.param'
        out_load_A = '/home/xkchen/mywork/ICL/data/SEX/result/mask_A_test.cat'

        tmp_load = '/home/xkchen/mywork/ICL/data/test_data/'
        load = '/home/xkchen/mywork/ICL/data/total_data/sample_02_03/'

        file = 'frame-r-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (rag, decg, zg)
        data = fits.open(load + file)
        img = data[0].data
        Head = data[0].header
        wcs = awc.WCS(Head)
        Da = Test_model.angular_diameter_distance(zg).value
        Ar = rad2asec / Da
        Rp = Ar / pixel
        cx_BCG, cy_BCG = wcs.all_world2pix(rag * U.deg, decg * U.deg, 1)

        ra_img, dec_img = wcs.all_pix2world(grd[0, :], grd[1, :], 1)
        pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
        EBV = sfd(pos)
        Av = Rv * EBV * 0.86
        Al = A_wave(l_wave[2], Rv) * Av
        img1 = img * 10**(Al / 2.5)

        ## part1: find the source and mask in original image
        hdu = fits.PrimaryHDU()
        hdu.data = img1
        hdu.header = Head
        hdu.writeto(tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' %
                    ('r', rag, decg, zg),
                    overwrite=True)

        file_source = tmp_load + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits' % (
            'r', rag, decg, zg)
        cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s' % (
            param_A, out_load_A, out_cat)
        print(cmd)
        A = subpro.Popen(cmd, shell=True)
        A.wait()

        source = asc.read(out_load_A)
        Numb = np.array(source['NUMBER'][-1])
        A = np.array(source['A_IMAGE'])
        B = np.array(source['B_IMAGE'])
        chi = np.array(source['THETA_IMAGE'])
        cx = np.array(source['X_IMAGE']) - 1
        cy = np.array(source['Y_IMAGE']) - 1

        Kron = 6
        Lr = Kron * A
        Sr = Kron * B

        cat = pd.read_csv(
            '/home/xkchen/mywork/ICL/data/star_catalog/source_SQL_Z%.3f_ra%.3f_dec%.3f.txt'
            % (zg, rag, decg),
            skiprows=1)
        ra_s = np.array(cat['ra'])
        dec_s = np.array(cat['dec'])
        mag = np.array(cat['r'])
        x_side = img.shape[1]
        y_side = img.shape[0]
        x, y = wcs.all_world2pix(ra_s * U.deg, dec_s * U.deg, 1)
        ia = (x >= 0) & (x <= x_side)
        ib = (y >= 0) & (y <= y_side)
        ie = (mag <= 20)
        ic = ia & ib & ie
        comx = x[ic]
        comy = y[ic]
        comr = np.ones(len(comx), dtype=np.float) * r_star
        com_chi = np.zeros(len(comx), dtype=np.float)

        CX = np.r_[cx, comx]
        CY = np.r_[cy, comy]
        a = np.r_[Lr, 2 * comr]
        b = np.r_[Sr, 2 * comr]
        theta = np.r_[chi, com_chi]
        Numb = Numb + len(comx)

        mask_A = np.ones((img.shape[0], img.shape[1]), dtype=np.float)
        ox = np.linspace(0, img.shape[1] - 1, img.shape[1])
        oy = np.linspace(0, img.shape[0] - 1, img.shape[0])
        basic_coord = np.array(np.meshgrid(ox, oy))
        major = a / 2
        minor = b / 2
        senior = np.sqrt(major**2 - minor**2)

        tdr = np.sqrt((CX - cx_BCG)**2 + (CY - cy_BCG)**2)
        dr00 = np.where(tdr == np.min(tdr))[0]
        for k in range(Numb):
            xc = CX[k]
            yc = CY[k]
            lr = major[k]
            sr = minor[k]
            cr = senior[k]

            set_r = np.int(np.ceil(1.2 * lr))
            la0 = np.int(xc - set_r)
            la1 = np.int(xc + set_r + 1)
            lb0 = np.int(yc - set_r)
            lb1 = np.int(yc + set_r + 1)

            if k == dr00[0]:
                continue
            else:
                phi = theta[k] * np.pi / 180
                df1 = lr**2 - cr**2 * np.cos(phi)**2
                df2 = lr**2 - cr**2 * np.sin(phi)**2
                fr = ((basic_coord[0, :][lb0:lb1, la0:la1] - xc)**2 * df1 +
                      (basic_coord[1, :][lb0:lb1, la0:la1] - yc)**2 * df2 -
                      cr**2 * np.sin(2 * phi) *
                      (basic_coord[0, :][lb0:lb1, la0:la1] - xc) *
                      (basic_coord[1, :][lb0:lb1, la0:la1] - yc))
                idr = fr / (lr**2 * sr**2)
                jx = idr < 1
                #jx = (-1)*jx+1
                #mask_A[lb0: lb1, la0: la1] = mask_A[lb0: lb1, la0: la1] * jx

                iu = np.where(jx == True)
                iv = np.ones((jx.shape[0], jx.shape[1]), dtype=np.float)
                iv[iu] = np.nan
                mask_A[lb0:lb1, la0:la1] = mask_A[lb0:lb1, la0:la1] * iv

        mirro_A = mask_A * img1
        SB2, R2, Anr2, err2 = light_measure(mirro_A, bins, 1, Rp, cx_BCG,
                                            cy_BCG, pixel, zg)
        '''
		## test the flux distribution in each bins
		sub_R = np.logspace(0, np.log10(Rp), bins)
		cdr = np.sqrt((grd[0,:] - cx_BCG)**2 + (grd[1,:] - cy_BCG)**2)
		Nr = len(sub_R)
		for xx in range(Nr - 1):
			idr = (cdr >= sub_R[xx]) & (cdr < sub_R[xx + 1])
			sub_f = mirro_A[idr]
			id_nan = np.isnan(sub_f)
			iv = np.where(id_nan == False)[0]
			bl_array = sub_f[iv]

			plt.figure()
			plt.title('$flux \; distribution \; in \; %d \; bins$' % (xx))
			plt.hist(bl_array, histtype = 'step', color = 'b', normed = True)
			plt.xlabel('$flux[nmagy]$')
			plt.savefig('/home/xkchen/mywork/ICL/code/flux_%dbins_DF_%.3fra_%.3fdec_%.3fz.png' % (xx, rag, decg, zg), dpi = 300)
			plt.close()
		'''
        # flux scale and pixel resample
        eta = Da_ref / Da
        mu = 1 / eta
        SB_ref = SB2 - 10 * np.log10((1 + zg) / (1 + z_ref))
        Ar_ref = Anr2 * mu
        f_SB = interp(Ar_ref, SB_ref, kind='cubic')

        imgt = flux_recal(img1, zg, z_ref)
        mirroA = flux_recal(mirro_A, zg, z_ref)

        xn, yn, resam = gen(mirroA, 1, eta, cx_BCG,
                            cy_BCG)  #############???????
        xn = np.int(xn)
        yn = np.int(yn)
        if eta > 1:
            resam = resam[1:, 1:]
        elif eta == 1:
            resam = resam[1:-1, 1:-1]
        else:
            resam = resam
        SBn, Rn, Anrn, errn = light_measure(resam, bins, 1, Rpp, xn, yn, pixel,
                                            z_ref)
        arn = Anrn[(Anrn >= np.min(Ar_ref)) & (Anrn <= np.max(Ar_ref))]

        plt.figure(figsize=(16, 8))
        gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])
        ax = plt.subplot(gs[0])
        bx = plt.subplot(gs[1])

        ax.plot(Anrn, SBn, 'b-', label='$SB_{resample}$', alpha=0.5)
        ax.plot(Ar_ref, SB_ref, 'g--', label='$SB_{ref}$', alpha=0.5)
        bx.plot(
            arn, SBn[(Anrn >= np.min(Ar_ref)) & (Anrn <= np.max(Ar_ref))] -
            f_SB(arn), 'g*')
        bx.axhline(y=np.mean(SBn[(Anrn >= np.min(Ar_ref))
                                 & (Anrn <= np.max(Ar_ref))] - f_SB(arn)),
                   ls='--',
                   color='b')

        ax.set_ylabel('$SB[mag/arcsec^2]$')
        ax.set_xlabel('$R[arcsec]$')
        ax.set_xscale('log')
        ax.tick_params(axis='both', which='both', direction='in')
        ax.invert_yaxis()
        ax.legend(loc=1, fontsize=15)

        bx.set_xlabel('$R[arcsec]$')
        bx.set_xscale('log')
        bx.set_ylabel('$ \Delta{SB}[mag/arcsec^2] $')
        bx.tick_params(axis='both', which='both', direction='in')
        bx.legend(loc=3, fontsize=12)
        plt.subplots_adjust(hspace=0)
        plt.tight_layout()
        plt.savefig(
            '/home/xkchen/mywork/ICL/code/resamp_err_SB_%d_ra%.3f_dec%.3f_z%.3f.png'
            % (bins, rag, decg, zg),
            dpi=300)
        plt.close()  #############???????????????
        '''
		xn, yn, resam = gen(imgt, 1, eta, cx_BCG, cy_BCG)
		xn = np.int(xn)
		yn = np.int(yn)
		if eta > 1:
		    resam = resam[1:, 1:]
		elif eta == 1:
		    resam = resam[1:-1, 1:-1]
		else:
			resam = resam

		## handle with these pixels around 0
		# from mask
		id_zero = np.where(mask_A == 0)
		tt_mask = mask_A * 1
		tt_mask[id_zero] = np.nan
		ttx, tty, ttmask = gen(tt_mask, 1, eta, cx_BCG, cy_BCG)

		if eta > 1:
		    ttmask = ttmask[1:, 1:]
		elif eta == 1:
		    ttmask = ttmask[1:-1, 1:-1]
		else:
			ttmask = ttmask
		id_nan = np.isnan(ttmask)
		ttmask[id_nan] = 0
		idnzeo = np.where(ttmask != 0)
		ttmask[idnzeo] = 1

		tt_img = ttmask * resam
		SB3, R3, Anr3, err3 = light_measure(tt_img, bins, 1, Rpp, xn, yn, pixel, z_ref)

		# from masked img
		id_zero = np.where(mirroA == 0)
		tt_mirro = mirroA * 1
		tt_mirro[id_zero] = np.nan 
		x2, y2, resam2 = gen(tt_mirro, 1, eta, cx_BCG, cy_BCG)
		x2 = np.int(x2)
		y2 = np.int(y2)
		if eta > 1:
		    resam2 = resam2[1:, 1:]
		elif eta == 1:
		    resam2 = resam2[1:-1, 1:-1]
		else:
			resam2 = resam2
		id_nan = np.isnan(resam2)
		resam2[id_nan] = 0
		SB5, R5, Anr5, err5 = light_measure(resam2, bins, 1, Rpp, x2, y2, pixel, z_ref)

		### part2: mask after resample (mask with the same source)
		CX_ = CX * mu
		CY_ = CY * mu
		a_ = a * mu
		b_ = b * mu

		res_mask_1 = np.ones((resam.shape[0], resam.shape[1]), dtype = np.float)
		ox_ = np.linspace(0, resam.shape[1] - 1, resam.shape[1])
		oy_ = np.linspace(0, resam.shape[0] - 1, resam.shape[0])
		basic_coord = np.array(np.meshgrid(ox_, oy_))
		major = a_ / 2
		minor = b_ / 2
		senior = np.sqrt(major**2 - minor**2)

		tdr = np.sqrt((CX_ - xn)**2 + (CY_ - yn)**2)
		dr00 = np.where(tdr == np.min(tdr))[0]

		for k in range(Numb):
			xc = CX_[k]
			yc = CY_[k]
			lr = major[k]
			sr = minor[k]
			cr = senior[k]

			set_r = np.int(np.ceil(1.2 * lr))
			la0 = np.int(xc - set_r)
			la1 = np.int(xc + set_r +1)
			lb0 = np.int(yc - set_r)
			lb1 = np.int(yc + set_r +1)

			if k == dr00[0] :
				continue
			else:
				phi = theta[k]*np.pi/180
				df1 = lr**2 - cr**2*np.cos(phi)**2
				df2 = lr**2 - cr**2*np.sin(phi)**2
				fr = ((basic_coord[0,:][lb0: lb1, la0: la1] - xc)**2*df1 + (basic_coord[1,:][lb0: lb1, la0: la1] - yc)**2*df2
					- cr**2*np.sin(2*phi)*(basic_coord[0,:][lb0: lb1, la0: la1] - xc)*(basic_coord[1,:][lb0: lb1, la0: la1] - yc))
				idr = fr/(lr**2*sr**2)
				jx = idr<=1
				jx = (-1)*jx+1
				res_mask_1[lb0: lb1, la0: la1] = res_mask_1[lb0: lb1, la0: la1]*jx
		resam1 = res_mask_1 * resam
		SBt, Rt, Anrt, errt = light_measure(resam1, bins, 1, Rpp, xn, yn, pixel, z_ref)

		# cut these pixel which is 0 before resamp
		xm, ym, res_mask_2 = gen(mask_A, 1, eta, cx_BCG, cy_BCG)
		if eta > 1:
		    res_mask_2 = res_mask_2[1:, 1:]
		elif eta == 1:
		    res_mask_2 = res_mask_2[1:-1, 1:-1]
		else:
			res_mask_2 = res_mask_2

		mix, miy, mirro_img = gen(mirroA, 1, eta, cx_BCG, cy_BCG)
		mix = np.int(mix)
		miy = np.int(miy)
		if eta > 1:
		    mirro_img = mirro_img[1:, 1:]
		elif eta == 1:
		    mirro_img = mirro_img[1:-1, 1:-1]
		else:
			mirro_img = mirro_img

		val, cont = sts.find_repeats(res_mask_2)
		ids = np.where(cont == np.max(cont))[0]
		res_mask2 = res_mask_2 / val[ids[0]]
		print('scale factor = ', val[ids[0]])
		SB4, R4, Anr4, err4 = weit_l_measure(mirro_img, res_mask2, bins, 1, Rpp, mix, miy, pixel, z_ref)

		ar4 = Anr4[(Anr4 >= np.min(Ar_ref)) & (Anr4 <= np.max(Ar_ref))]
		ar3 = Anr3[(Anr3 >= np.min(Ar_ref)) & (Anr3 <= np.max(Ar_ref))]
		art = Anrt[(Anrt >= np.min(Ar_ref)) & (Anrt <= np.max(Ar_ref))]

		plt.figure(figsize = (16, 8))
		gs = gridspec.GridSpec(1, 2, width_ratios = [1,1])
		ax = plt.subplot(gs[0])
		bx = plt.subplot(gs[1])

		ax.plot(Anrt, SBt, 'g-.', label = '$SB_{re-load \, source \, at \, z_{0}}$', alpha = 0.5)
		ax.plot(Anr4, SB4, 'b-', label = '$SB_{resample \, mask \, Metrix}$', alpha = 0.5)
		ax.plot(Anr3, SB3, 'r:', label = '$SB_{correct \, resample}$', alpha = 0.5)
		ax.plot(Ar_ref, SB_ref, 'k--', label = '$SB_{ref}$', alpha = 0.5)

		bx.plot(ar4, SB4[(Anr4 >= np.min(Ar_ref)) & (Anr4 <= np.max(Ar_ref))] - f_SB(ar4), 
			'b*', label = '$ [SB_{resample \, mask} - SB_{ref}] $', alpha = 0.5)
		bx.plot(ar3, SB3[(Anr3 >= np.min(Ar_ref)) & (Anr3 <= np.max(Ar_ref))] - f_SB(ar3), 
			'r*', label = '$ [SB_{correct \, resample} - SB_{ref}] $', alpha = 0.5)
		bx.plot(art, SBt[(Anrt >= np.min(Ar_ref)) & (Anrt <= np.max(Ar_ref))] - f_SB(art), 
			'g*', label = '$ [SB_{re-load \, source} - SB_{ref}] $', alpha = 0.5)
		bx.axhline(y = np.mean(SB4[(Anr4 >= np.min(Ar_ref)) & (Anr4 <= np.max(Ar_ref))] - f_SB(ar4)), color = 'b', ls = '--', alpha = 0.5)
		bx.axhline(y = np.mean(SB3[(Anr3 >= np.min(Ar_ref)) & (Anr3 <= np.max(Ar_ref))] - f_SB(ar3)), color = 'r', ls = '--', alpha = 0.5)
		bx.axhline(y = np.mean(SBt[(Anrt >= np.min(Ar_ref)) & (Anrt <= np.max(Ar_ref))] - f_SB(art)), color = 'g', ls = '--', alpha = 0.5)

		ax.set_title('resample SB profile comparation')
		ax.set_ylabel('$SB[mag/arcsec^2]$')
		ax.set_xlabel('$R[arcsec]$')
		ax.set_xscale('log')
		ax.tick_params(axis = 'both', which = 'both', direction = 'in')
		ax.invert_yaxis()
		ax.legend(loc = 1, fontsize = 15)

		bx.set_xlabel('$R[arcsec]$')
		bx.set_xscale('log')
		bx.set_ylabel('$ \Delta{SB}[mag/arcsec^2] $')
		bx.tick_params(axis = 'both', which = 'both', direction = 'in')
		bx.legend(loc = 3, fontsize = 12)
		plt.subplots_adjust(hspace = 0)
		plt.tight_layout()
		plt.savefig('/home/xkchen/mywork/ICL/code/resample_test_SB_%d_ra%.3f_dec%.3f_z%.3f.png' % (bins, rag, decg, zg), dpi = 300)
		plt.close()
		'''
    raise
    return
Beispiel #7
0
def color():
    z_ref = 0.25
    Da_ref = Test_model.angular_diameter_distance(z_ref).value
    Angu_ref = (R0 / Da_ref) * rad2asec
    Rpp = Angu_ref / pixel

    bins = 90
    bint = 90

    kd = 0
    #kd = 19

    x0 = np.linspace(0, 2047, 2048)
    y0 = np.linspace(0, 1488, 1489)
    grd = np.array(np.meshgrid(x0, y0))
    r_star = 2 * 1.5 / pixel

    zg = z[kd]
    rag = ra[kd]
    decg = dec[kd]

    load = '/home/xkchen/mywork/ICL/data/total_data/sample_02_03/'

    file1 = 'frame-g-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (rag, decg, zg)
    data = fits.open(load + file1)
    img01 = data[0].data
    Head = data[0].header
    wcs = awc.WCS(Head)
    Da = Test_model.angular_diameter_distance(zg).value
    Ar = rad2asec / Da
    Rp = Ar / pixel
    cx_BCG, cy_BCG = wcs.all_world2pix(rag * U.deg, decg * U.deg, 1)
    SB01, R01, Anr01, err01 = light_measure(img01, bins, 1, Rp, cx_BCG, cy_BCG,
                                            pixel, zg)

    file2 = 'frame-r-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (rag, decg, zg)
    data = fits.open(load + file2)
    img02 = data[0].data
    SB02, R02, Anr02, err02 = light_measure(img02, bins, 1, Rp, cx_BCG, cy_BCG,
                                            pixel, zg)

    ## redden
    ra_img, dec_img = wcs.all_pix2world(grd[0, :], grd[1, :], 1)
    pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
    EBV = sfd(pos)
    Av = Rv * EBV * 0.86
    Al1 = A_wave(l_wave[1], Rv) * Av
    img1 = img01 * 10**(Al1 / 2.5)
    SB1, R1, Anr1, err1 = light_measure(img1, bins, 1, Rp, cx_BCG, cy_BCG,
                                        pixel, zg)

    Al2 = A_wave(l_wave[2], Rv) * Av
    img2 = img02 * 10**(Al2 / 2.5)
    SB2, R2, Anr2, err2 = light_measure(img2, bins, 1, Rp, cx_BCG, cy_BCG,
                                        pixel, zg)

    DM1 = SB01 - SB02
    DM2 = SB1 - SB2
    plt.figure(figsize=(16, 9))
    gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1])
    ax = plt.subplot(gs[0])
    bx = plt.subplot(gs[1])

    ax.plot(R01, DM1, 'g-', label=r'$g-r_{before \, correct}$', alpha=0.5)
    ax.plot(R1, DM2, 'r-', label=r'$g-r_{after \, correct}$', alpha=0.5)
    ax.set_xlabel('$R[arcsec]$')
    ax.set_ylabel('$g-r$')
    ax.set_xscale('log')
    ax.legend(loc=1)

    bx.plot(R01, DM1 - DM2, 'b-', label=r'$\Delta_{c_{before} - c_{after}}$')
    bx.set_xlabel('$R[arcsec]$')
    bx.set_xscale('log')
    bx.set_ylabel(r'$\delta_{g-r}$')
    bx.set_yscale('log')
    bx.legend(loc=1, fontsize=15)
    plt.savefig('/home/xkchen/mywork/ICL/code/color_test.png', dpi=300)
    plt.close()

    raise
    return
def pho_mask(band_id, z_set, ra_set, dec_set):

    kk = np.int(band_id)
    Nz = len(z_set)
    param_A = 'default_mask_A.sex'
    out_cat = 'default_mask_A.param'
    out_load_A = tmp + 'A_mask_%d_cpus.cat' % rank
    ## size test
    r_res = 2.8  # 2.8 for larger R setting
    for q in range(Nz):
        z_g = z_set[q]
        ra_g = ra_set[q]
        dec_g = dec_set[q]
        try:
            pro_f = d_file + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (
                band[kk], ra_g, dec_g, z_g)

            data_f = fits.open(pro_f)
            img = data_f[0].data
            head_inf = data_f[0].header
            wcs = awc.WCS(head_inf)
            cx_BCG, cy_BCG = wcs.all_world2pix(ra_g * U.deg, dec_g * U.deg, 1)
            R_ph = rad2asec / (Test_model.angular_diameter_distance(z_g).value)
            R_p = R_ph / pixel

            x0 = np.linspace(0, img.shape[1] - 1, img.shape[1])
            y0 = np.linspace(0, img.shape[0] - 1, img.shape[0])
            img_grid = np.array(np.meshgrid(x0, y0))
            ra_img, dec_img = wcs.all_pix2world(img_grid[0, :], img_grid[1, :],
                                                1)
            pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
            BEV = sfd(pos)
            Av = Rv * BEV * 0.86
            Al = A_wave(l_wave[kk], Rv) * Av
            img = img * 10**(Al / 2.5)

            hdu = fits.PrimaryHDU()
            hdu.data = img
            hdu.header = head_inf
            hdu.writeto(tmp + 'source_data_%d.fits' % rank, overwrite=True)

            file_source = tmp + 'source_data_%d.fits' % rank
            cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s' % (
                param_A, out_load_A, out_cat)
            a = subpro.Popen(cmd, shell=True)
            a.wait()

            source = asc.read(out_load_A)
            Numb = np.array(source['NUMBER'][-1])
            A = np.array(source['A_IMAGE'])
            B = np.array(source['B_IMAGE'])
            theta = np.array(source['THETA_IMAGE'])
            cx = np.array(source['X_IMAGE']) - 1
            cy = np.array(source['Y_IMAGE']) - 1
            p_type = np.array(source['CLASS_STAR'])

            Kron = 6 * r_res  # iso_radius set as 3 times rms (2.8 from size test)
            a = Kron * A
            b = Kron * B

            mask = load + 'photo_z/star_cat/source_SQL_Z%.3f_ra%.3f_dec%.3f.txt' % (
                z_g, ra_g, dec_g)
            cat = pds.read_csv(mask, skiprows=1)
            set_ra = np.array(cat['ra'])
            set_dec = np.array(cat['dec'])
            set_mag = np.array(cat['r'])
            OBJ = np.array(cat['type'])
            xt = cat['Column1']
            tau = 10 * r_res  # the mask size set as 10 * FWHM from dr12

            set_A = np.array([
                cat['psffwhm_r'], cat['psffwhm_g'], cat['psffwhm_i']
            ]) * tau / pixel
            set_B = np.array([
                cat['psffwhm_r'], cat['psffwhm_g'], cat['psffwhm_i']
            ]) * tau / pixel
            set_chi = np.zeros(set_A.shape[1], dtype=np.float)

            lln = np.array([
                len(set_A[:, ll][set_A[:, ll] > 0])
                for ll in range(set_A.shape[1])
            ])
            lr_iso = np.array(
                [np.max(set_A[:, ll]) for ll in range(set_A.shape[1])])
            sr_iso = np.array(
                [np.max(set_B[:, ll]) for ll in range(set_B.shape[1])])
            # bright stars
            x, y = wcs.all_world2pix(set_ra * U.deg, set_dec * U.deg, 1)
            ia = (x >= 0) & (x <= img.shape[1])
            ib = (y >= 0) & (y <= img.shape[0])
            ie = (set_mag <= 20)
            iq = lln >= 2
            ig = OBJ == 6
            ic = (ia & ib & ie & ig & iq)
            sub_x0 = x[ic]
            sub_y0 = y[ic]
            sub_A0 = lr_iso[ic]
            sub_B0 = sr_iso[ic]
            sub_chi0 = set_chi[ic]

            # saturated source(may not stars)
            xa = ['SATURATED' in qq for qq in xt]
            xv = np.array(xa)
            idx = xv == True
            ipx = (idx & ia & ib)

            sub_x2 = x[ipx]
            sub_y2 = y[ipx]
            sub_A2 = 3 * lr_iso[ipx]
            sub_B2 = 3 * sr_iso[ipx]
            sub_chi2 = set_chi[ipx]

            comx = np.r_[sub_x0[sub_A0 > 0], sub_x2[sub_A2 > 0]]
            comy = np.r_[sub_y0[sub_A0 > 0], sub_y2[sub_A2 > 0]]
            Lr = np.r_[sub_A0[sub_A0 > 0], sub_A2[sub_A2 > 0]]
            Sr = np.r_[sub_B0[sub_A0 > 0], sub_B2[sub_A2 > 0]]
            phi = np.r_[sub_chi0[sub_A0 > 0], sub_chi2[sub_A2 > 0]]

            cx = np.r_[cx, comx]
            cy = np.r_[cy, comy]
            a = np.r_[a, Lr]
            b = np.r_[b, Sr]
            theta = np.r_[theta, phi]
            Numb = Numb + len(comx)

            mask_A = np.ones((img.shape[0], img.shape[1]), dtype=np.float)
            ox = np.linspace(0, img.shape[1] - 1, img.shape[1])
            oy = np.linspace(0, img.shape[0] - 1, img.shape[0])
            basic_coord = np.array(np.meshgrid(ox, oy))
            major = a / 2
            minor = b / 2  # set the star mask based on the major and minor radius
            senior = np.sqrt(major**2 - minor**2)

            tdr = np.sqrt((cx - cx_BCG)**2 + (cy - cy_BCG)**2)
            dr00 = np.where(tdr == np.min(tdr))[0]

            for k in range(Numb):
                xc = cx[k]
                yc = cy[k]

                lr = major[k]
                sr = minor[k]
                cr = senior[k]
                chi = theta[k] * np.pi / 180

                set_r = np.int(np.ceil(1.2 * lr))
                la0 = np.max([np.int(xc - set_r), 0])
                la1 = np.min([np.int(xc + set_r + 1), img.shape[1] - 1])
                lb0 = np.max([np.int(yc - set_r), 0])
                lb1 = np.min([np.int(yc + set_r + 1), img.shape[0] - 1])

                if k == dr00[0]:
                    continue
                else:
                    df1 = (basic_coord[0, :][lb0:lb1, la0:la1] - xc) * np.cos(
                        chi) + (basic_coord[1, :][lb0:lb1, la0:la1] -
                                yc) * np.sin(chi)
                    df2 = (basic_coord[1, :][lb0:lb1, la0:la1] - yc) * np.cos(
                        chi) - (basic_coord[0, :][lb0:lb1, la0:la1] -
                                xc) * np.sin(chi)
                    fr = df1**2 / lr**2 + df2**2 / sr**2
                    jx = fr <= 1

                    iu = np.where(jx == True)
                    iv = np.ones((jx.shape[0], jx.shape[1]), dtype=np.float)
                    iv[iu] = np.nan
                    mask_A[lb0:lb1, la0:la1] = mask_A[lb0:lb1, la0:la1] * iv

            mirro_A = mask_A * img
            hdu = fits.PrimaryHDU()
            hdu.data = mirro_A
            hdu.header = head_inf
            hdu.writeto(load +
                        'photo_z/mask/A_mask_%s_ra%.3f_dec%.3f_z%.3f.fits' %
                        (band[kk], ra_g, dec_g, z_g),
                        overwrite=True)
        except FileNotFoundError:
            continue
    return
Beispiel #9
0
def sky_light():

    band_add = ['r', 'i', 'z']
    band_fil = ['u', 'g', 'r', 'i', 'z']
    load = '/mnt/ddnfs/data_users/cxkttwl/ICL/data/'

    param_sky = 'default_sky_mask.sex'
    out_cat = 'default_mask_A.param'
    out_load_sky = './result/sky_mask_test.cat'

    x0 = np.linspace(0, 2047, 2048)
    y0 = np.linspace(0, 1488, 1489)
    img_grid = np.array(np.meshgrid(x0, y0))

    for q in range(len(z)):

        for l in range(len(band_fil)):

            pro_f = d_file + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (
                band_fil[l], ra[q], dec[q], z[q])
            ra_g = ra[q]
            dec_g = dec[q]
            z_g = z[q]

            data_f = fits.open(pro_f)
            img = data_f[0].data
            head_inf = data_f[0].header
            wcs = awc.WCS(head_inf)
            cx_BCG, cy_BCG = wcs.all_world2pix(ra_g * U.deg, dec_g * U.deg, 1)
            R_ph = rad2asec / (Test_model.angular_diameter_distance(
                z[q]).value)
            R_p = R_ph / pixel

            ra_img, dec_img = wcs.all_pix2world(img_grid[0, :], img_grid[1, :],
                                                1)
            pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
            BEV = sfd(pos)
            Av = Rv * BEV * 0.86
            Al = A_wave(l_wave[l], Rv) * Av
            img = img * 10**(Al / 2.5)

            # mask in sky only
            hdu = fits.PrimaryHDU()
            hdu.data = data_f[0].data
            hdu.header = head_inf
            hdu.writeto('source_data.fits', overwrite=True)
            file_source = './source_data.fits'
            cmd = 'sex ' + file_source + ' -c %s -CATALOG_NAME %s -PARAMETERS_NAME %s' % (
                param_sky, out_load_sky, out_cat)
            print(cmd)
            a = subpro.Popen(cmd, shell=True)
            a.wait()

            source = asc.read(out_load_sky)
            Numb = np.array(source['NUMBER'][-1])
            A = np.array(source['A_IMAGE'])
            B = np.array(source['B_IMAGE'])
            theta = np.array(source['THETA_IMAGE'])
            cx = np.array(source['X_IMAGE']) - 1
            cy = np.array(source['Y_IMAGE']) - 1
            p_type = np.array(source['CLASS_STAR'])

            Kron = 6
            a = Kron * A
            b = Kron * B

            ddr = np.sqrt((cx - cx_BCG)**2 + (cy - cy_BCG)**2)
            ix = ddr >= 0.95 * R_p
            iy = ddr <= 1.15 * R_p
            iz = ix & iy
            s_cx = cx[iz]
            s_cy = cy[iz]
            s_a = a[iz]
            s_b = b[iz]
            s_phi = theta[iz]
            s_Num = len(s_b)

            mask_sky = np.ones((img.shape[0], img.shape[1]), dtype=np.float)
            ox = np.linspace(0, 2047, 2048)
            oy = np.linspace(0, 1488, 1489)
            basic_coord = np.array(np.meshgrid(ox, oy))
            major = s_a / 2
            minor = s_b / 2
            senior = np.sqrt(major**2 - minor**2)
            for k in range(s_Num):
                xc = s_cx[k]
                yc = s_cy[k]

                lr = major[k]
                sr = minor[k]
                cr = senior[k]
                chi = s_phi[k] * np.pi / 180

                set_r = np.int(np.ceil(1.2 * lr))
                la0 = np.max([np.int(xc - set_r), 0])
                la1 = np.min([np.int(xc + set_r + 1), img.shape[1] - 1])
                lb0 = np.max([np.int(yc - set_r), 0])
                lb1 = np.min([np.int(yc + set_r + 1), img.shape[0] - 1])

                df1 = lr**2 - cr**2 * np.cos(chi)**2
                df2 = lr**2 - cr**2 * np.sin(chi)**2
                fr = (basic_coord[0,:][lb0: lb1, la0: la1] - xc)**2*df1 +(basic_coord[1,:][lb0: lb1, la0: la1] - yc)**2*df2\
                - cr**2*np.sin(2*chi)*(basic_coord[0,:][lb0: lb1, la0: la1] - xc)*(basic_coord[1,:][lb0: lb1, la0: la1] - yc)
                idr = fr / (lr**2 * sr**2)
                jx = idr <= 1

                iu = np.where(jx == True)
                iv = np.ones((jx.shape[0], jx.shape[1]), dtype=np.float)
                iv[iu] = np.nan
                mask_sky[lb0:lb1, la0:la1] = mask_sky[lb0:lb1, la0:la1] * iv

            mirro_sky = img * mask_sky

            hdu = fits.PrimaryHDU()
            hdu.data = mirro_sky
            hdu.header = head_inf
            hdu.writeto(
                load +
                'mask_data/sky_plane/0.3sigma/sky_mask_data_%s_ra%.3f_dec%.3f_z%.3f.fits'
                % (band_fil[l], ra_g, dec_g, z[q]),
                overwrite=True)

            plt.figure()
            ax = plt.imshow(mirro_sky,
                            cmap='Greys',
                            origin='lower',
                            vmin=1e-3,
                            norm=mpl.colors.LogNorm())
            plt.colorbar(ax, fraction=0.035, pad=0.01, label='$flux[nmaggy]$')
            hsc.ellipses(s_cx,
                         s_cy,
                         w=s_a,
                         h=s_b,
                         rot=s_phi,
                         fc='',
                         ec='r',
                         ls='--',
                         lw=0.5)
            hsc.circles(comx, comy, s=comr, fc='', ec='b', ls='-', lw=0.5)

            hsc.circles(
                cx_BCG,
                cy_BCG,
                s=R_p,
                fc='',
                ec='b',
            )
            hsc.circles(cx_BCG, cy_BCG, s=1.1 * R_p, fc='', ec='b', ls='--')
            plt.scatter(cx_BCG,
                        cy_BCG,
                        s=10,
                        marker='X',
                        facecolors='',
                        edgecolors='r',
                        linewidth=0.5,
                        alpha=0.5)
            plt.title('BL_03 mask img ra%.3f dec%.3f z%.3f in %s band' %
                      (ra_g, dec_g, z_g, band_fil[l]))
            plt.xlim(0, mirro_sky.shape[1])
            plt.ylim(0, mirro_sky.shape[0])
            plt.savefig(
                '/mnt/ddnfs/data_users/cxkttwl/ICL/fig_class/sky_03_mask/BL_mask_%s_ra%.3f_dec%.3f_z%.3f.png'
                % (band_fil[l], ra[q], dec[q], z[q]),
                dpi=300)
            plt.close()

        print(q)
    return
def spec_mask_B(band_id, z_set, ra_set, dec_set):
	Nz = len(z_set)
	kk = np.int(band_id)

	for q in range(Nz):
		ra_g, dec_g, z_g = ra_set[q], dec_set[q], z_set[q]
		file = d_file + 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (band[kk], ra_g, dec_g, z_g)

		data = fits.open(file)
		img = data[0].data
		head_inf = data[0].header
		wcs = awc.WCS(head_inf)
		x_side = data[0].data.shape[1]
		y_side = data[0].data.shape[0]

		x0 = np.linspace(0, img.shape[1] - 1, img.shape[1])
		y0 = np.linspace(0, img.shape[0] - 1, img.shape[0])
		img_grid = np.array(np.meshgrid(x0, y0))
		ra_img, dec_img = wcs.all_pix2world(img_grid[0,:], img_grid[1,:], 1)
		pos = SkyCoord(ra_img, dec_img, frame = 'fk5', unit = 'deg')
		BEV = sfd(pos)
		Av = Rv * BEV * 0.86
		Al = A_wave(l_wave[kk], Rv) * Av
		img = img * 10**(Al / 2.5)

		R_ph = rad2asec/(Test_model.angular_diameter_distance(z_g).value)
		R_p = R_ph/pixel
		cenx, ceny = wcs.all_world2pix(ra_g*U.deg, dec_g*U.deg, 1)

		mask = load + 'bright_star_dr12/source_SQL_Z%.3f_ra%.3f_dec%.3f.txt'%(z_g, ra_g, dec_g)
		cat = pds.read_csv(mask, skiprows = 1)
		set_ra = np.array(cat['ra'])
		set_dec = np.array(cat['dec'])
		set_mag = np.array(cat['r'])
		OBJ = np.array(cat['type'])
		xt = cat['Column1']
		tau = 10 * 3 # the mask size set as tau * FWHM from dr12

		set_A = np.array( [ cat['psffwhm_r'] , cat['psffwhm_g'], cat['psffwhm_i']]) * tau / pixel
		set_B = np.array( [ cat['psffwhm_r'] , cat['psffwhm_g'], cat['psffwhm_i']]) * tau / pixel
		set_chi = np.zeros(set_A.shape[1], dtype = np.float)

		lln = np.array([len(set_A[:,ll][set_A[:,ll] > 0 ]) for ll in range(set_A.shape[1]) ])
		lr_iso = np.array([np.max(set_A[:,ll]) for ll in range(set_A.shape[1]) ])
		sr_iso = np.array([np.max(set_B[:,ll]) for ll in range(set_B.shape[1]) ])

		# bright stars
		x, y = wcs.all_world2pix(set_ra * U.deg, set_dec * U.deg, 1)
		ia = (x >= 0) & (x <= img.shape[1])
		ib = (y >= 0) & (y <= img.shape[0])
		ie = (set_mag <= 20)
		iq = lln >= 2
		ig = OBJ == 6
		ic = (ia & ib & ie & ig & iq)
		sub_x0 = x[ic]
		sub_y0 = y[ic]
		sub_A0 = lr_iso[ic]
		sub_B0 = sr_iso[ic]
		sub_chi0 = set_chi[ic]
		# saturated source(may not stars)
		xa = ['SATURATED' in pp for pp in xt]
		xv = np.array(xa)
		idx = xv == True
		ipx = (idx & ia & ib)

		sub_x2 = x[ipx]
		sub_y2 = y[ipx]
		sub_A2 = 3 * lr_iso[ipx]
		sub_B2 = 3 * sr_iso[ipx]
		sub_chi2 = set_chi[ipx]

		comx = np.r_[sub_x0[sub_A0 > 0], sub_x2[sub_A2 > 0]]
		comy = np.r_[sub_y0[sub_A0 > 0], sub_y2[sub_A2 > 0]]
		Lr = np.r_[sub_A0[sub_A0 > 0], sub_A2[sub_A2 > 0]]
		Sr = np.r_[sub_B0[sub_A0 > 0], sub_B2[sub_A2 > 0]]
		phi = np.r_[sub_chi0[sub_A0 > 0], sub_chi2[sub_A2 > 0]]

		Numb = len(comx)
		mask_B = np.ones((img.shape[0], img.shape[1]), dtype = np.float)
		ox = np.linspace(0, img.shape[1] - 1, img.shape[1])
		oy = np.linspace(0, img.shape[0] - 1, img.shape[0])
		basic_coord = np.array(np.meshgrid(ox,oy))

		major = Lr / 2
		minor = Sr / 2
		senior = np.sqrt(major**2 - minor**2)
		## mask B
		for k in range(Numb):
			xc = comx[k]
			yc = comy[k]
			lr = major[k]
			sr = minor[k]
			cr = senior[k]
			theta = phi[k] * np.pi / 180

			set_r = np.int(np.ceil(1.2 * lr))
			la0 = np.max( [np.int(xc - set_r), 0])
			la1 = np.min( [np.int(xc + set_r +1), img.shape[1] - 1] )
			lb0 = np.max( [np.int(yc - set_r), 0] ) 
			lb1 = np.min( [np.int(yc + set_r +1), img.shape[0] - 1] )

			df1 = (basic_coord[0,:][lb0: lb1, la0: la1] - xc)* np.cos(theta) + (basic_coord[1,:][lb0: lb1, la0: la1] - yc)* np.sin(theta)
			df2 = (basic_coord[1,:][lb0: lb1, la0: la1] - yc)* np.cos(theta) - (basic_coord[0,:][lb0: lb1, la0: la1] - xc)* np.sin(theta)
			fr = df1**2 / lr**2 + df2**2 / sr**2
			jx = fr <= 1

			iu = np.where(jx == True)
			iv = np.ones((jx.shape[0], jx.shape[1]), dtype = np.float)
			iv[iu] = np.nan
			mask_B[lb0: lb1, la0: la1] = mask_B[lb0: lb1, la0: la1] * iv
		mirro_B = mask_B * img

		hdu = fits.PrimaryHDU()
		hdu.data = mirro_B
		hdu.header = head_inf
		hdu.writeto(load + 'mask_data/B_plane/1.5sigma/B_mask_data_%s_ra%.3f_dec%.3f_z%.3f.fits'%(band[kk], ra_g, dec_g, z_g),overwrite = True)

		plt.figure()
		ax = plt.imshow(mirro_B, cmap = 'Greys', origin = 'lower', vmin = 1e-7, vmax = 1e2, norm = mpl.colors.LogNorm())
		plt.colorbar(ax, fraction = 0.035, pad =  0.01, label = '$flux[nmaggy]$')
		hsc.circles(cenx, ceny, s = R_p, fc = '', ec = 'b', ls = '-')
		hsc.circles(cenx, ceny, s = 1.1 * R_p, fc = '', ec = 'b', ls = '--')
		plt.scatter(cenx, ceny, s = 10, marker = 'X', facecolors = '', edgecolors = 'r', linewidth = 0.5, alpha = 0.5)
		plt.title('B mask img ra%.3f dec%.3f z%.3f in %s band' % (ra_g, dec_g, z_g, band[kk]))
		plt.xlim(0, mirro_B.shape[1])
		plt.ylim(0, mirro_B.shape[0])
		plt.savefig(
		'/mnt/ddnfs/data_users/cxkttwl/ICL/fig_class/spec_mask/B_mask_%s_ra%.3f_dec%.3f_z%.3f.png'%(band[kk], ra_g, dec_g, z_g), dpi = 300)
		plt.close()

	return
def extin_pro():
    load = '/home/xkchen/mywork/ICL/data/total_data/'
    idu = (com_Mag >= 17) & (com_Mag <= 18)

    x0 = np.linspace(0, 2047, 2048)
    y0 = np.linspace(0, 1488, 1489)
    img_grid = np.array(np.meshgrid(x0, y0))

    sub_ra = ra[idu]
    sub_dec = dec[idu]
    sub_z = z[idu]
    zN = 20
    cat_Rii = np.array([
        0.23, 0.68, 1.03, 1.76, 3.00, 4.63, 7.43, 11.42, 18.20, 28.20, 44.21,
        69.00, 107.81, 168.20, 263.00
    ])

    R_smal, R_max, bins = 1, 200, 55

    plt.figure(figsize=(20, 20))
    gs = gridspec.GridSpec(zN // 5, 5)

    for kk in range(zN):
        ra_g = sub_ra[kk]
        dec_g = sub_dec[kk]
        z_g = sub_z[kk]
        Da_g = Test_model.angular_diameter_distance(z_g).value

        ## original image
        file = 'frame-%s-ra%.3f-dec%.3f-redshift%.3f.fits.bz2' % (
            'g', ra_g, dec_g, z_g)  ## data
        data = fits.open(load + file)
        img = data[0].data
        wcs = awc.WCS(data[0].header)
        cx, cy = wcs.all_world2pix(ra_g * U.deg, dec_g * U.deg, 1)

        Intns, Intns_r, Intns_err, Npix = light_measure(
            img, bins, R_smal, R_max, cx, cy, pixel, z_g)
        id_nan = np.isnan(Intns)
        flux = Intns[id_nan == False]
        flux_err = Intns_err[id_nan == False]
        sub_R0 = Intns_r[id_nan == False]
        flux0 = flux + flux_err
        flux1 = flux - flux_err

        sub_SB0 = 22.5 - 2.5 * np.log10(flux) + 2.5 * np.log10(pixel**2)
        SB0 = 22.5 - 2.5 * np.log10(flux0) + 2.5 * np.log10(pixel**2)
        SB1 = 22.5 - 2.5 * np.log10(flux1) + 2.5 * np.log10(pixel**2)
        err0 = sub_SB0 - SB0
        err1 = SB1 - sub_SB0
        id_nan = np.isnan(SB1)
        err1[id_nan] = 100.
        sub0_err0 = err0 * 1
        sub0_err1 = err1 * 1

        ## SFD 1998
        ra_img, dec_img = wcs.all_pix2world(img_grid[0, :], img_grid[1, :], 1)
        pos = SkyCoord(ra_img, dec_img, frame='fk5', unit='deg')
        BEV_98 = sfd(pos)
        Av_98 = Rv * BEV_98
        Al = A_wave(l_wave[1], Rv) * Av_98
        c0_img = img * 10**(Al / 2.5)

        Intns, Intns_r, Intns_err, Npix = light_measure(
            c0_img, bins, R_smal, R_max, cx, cy, pixel, z_g)
        id_nan = np.isnan(Intns)
        flux = Intns[id_nan == False]
        flux_err = Intns_err[id_nan == False]
        sub_R1 = Intns_r[id_nan == False]
        flux0 = flux + flux_err
        flux1 = flux - flux_err

        sub_SB1 = 22.5 - 2.5 * np.log10(flux) + 2.5 * np.log10(pixel**2)
        SB0 = 22.5 - 2.5 * np.log10(flux0) + 2.5 * np.log10(pixel**2)
        SB1 = 22.5 - 2.5 * np.log10(flux1) + 2.5 * np.log10(pixel**2)
        err0 = sub_SB1 - SB0
        err1 = SB1 - sub_SB1
        id_nan = np.isnan(SB1)
        err1[id_nan] = 100.
        sub1_err0 = err0 * 1
        sub1_err1 = err1 * 1

        ## SFD 2011
        BEV_11 = sfd(pos) * 0.86
        Av_11 = Rv * BEV_11
        Al = A_wave(l_wave[1], Rv) * Av_11
        c1_img = img * 10**(Al / 2.5)

        Intns, Intns_r, Intns_err, Npix = light_measure(
            c1_img, bins, R_smal, R_max, cx, cy, pixel, z_g)
        id_nan = np.isnan(Intns)
        flux = Intns[id_nan == False]
        flux_err = Intns_err[id_nan == False]
        sub_R2 = Intns_r[id_nan == False]
        flux0 = flux + flux_err
        flux1 = flux - flux_err

        sub_SB2 = 22.5 - 2.5 * np.log10(flux) + 2.5 * np.log10(pixel**2)
        SB0 = 22.5 - 2.5 * np.log10(flux0) + 2.5 * np.log10(pixel**2)
        SB1 = 22.5 - 2.5 * np.log10(flux1) + 2.5 * np.log10(pixel**2)
        err0 = sub_SB2 - SB0
        err1 = SB1 - sub_SB2
        id_nan = np.isnan(SB1)
        err1[id_nan] = 100.
        sub2_err0 = err0 * 1
        sub2_err1 = err1 * 1

        ## SDSS catalogue
        cat_pro = pds.read_csv(
            '/home/xkchen/mywork/ICL/data/BCG_pros/BCG_prof_Z%.3f_ra%.3f_dec%.3f.txt'
            % (z_g, ra_g, dec_g),
            skiprows=1)
        dat_band = np.array(cat_pro.band)
        dat_bins = np.array(cat_pro.bin)
        dat_pro = np.array(cat_pro.profMean)  # in unit of nmaggy / arcsec^2
        dat_pro_err = np.array(cat_pro.profErr)

        idx = dat_band == 1
        tt_pro = dat_pro[idx]
        tt_bin = dat_bins[idx]
        tt_proErr = dat_pro_err[idx]
        tt_r = (cat_Rii[tt_bin] * Da_g / rad2asec) * 1e3  # arcsec --> kpc

        cat_SB = 22.5 - 2.5 * np.log10(tt_pro)
        SB0 = 22.5 - 2.5 * np.log10(tt_pro + tt_proErr)
        SB1 = 22.5 - 2.5 * np.log10(tt_pro - tt_proErr)
        err0 = cat_SB - SB0
        err1 = SB1 - cat_SB
        id_nan = np.isnan(cat_SB)
        cc_SB, SB0, SB1 = cat_SB[id_nan == False], SB0[id_nan == False], SB1[
            id_nan == False]
        cc_r, err0, err1 = tt_r[id_nan == False], err0[id_nan == False], err1[
            id_nan == False]
        id_nan = np.isnan(SB1)
        err1[id_nan] = 100.
        cc_err0 = err0 * 1
        cc_err1 = err1 * 1

        ax = plt.subplot(gs[kk // 5, kk % 5])
        ax.errorbar(sub_R0,
                    sub_SB0,
                    yerr=[sub0_err0, sub0_err1],
                    xerr=None,
                    ls='-',
                    fmt='r.',
                    label='No correction',
                    alpha=0.5)
        ax.errorbar(sub_R1,
                    sub_SB1,
                    yerr=[sub1_err0, sub1_err1],
                    xerr=None,
                    ls='-',
                    fmt='g.',
                    label='SFD 1998',
                    alpha=0.5)
        ax.errorbar(sub_R2,
                    sub_SB2,
                    yerr=[sub2_err0, sub2_err1],
                    xerr=None,
                    ls='-',
                    fmt='b.',
                    label='SFD 2011',
                    alpha=0.5)
        ax.errorbar(cc_r,
                    cc_SB,
                    yerr=[cc_err0, cc_err1],
                    xerr=None,
                    ls='--',
                    fmt='k.',
                    label='SDSS cat.',
                    alpha=0.5)

        ax.set_xscale('log')
        ax.set_xlabel('k[kpc]')
        ax.set_ylabel('SB[mag/arcsec^2]')
        ax.set_ylim(20, 28)
        ax.set_xlim(1, 200)
        ax.legend(loc=1)
        ax.invert_yaxis()

    plt.tight_layout()
    plt.savefig('Extinct_BCG_pros_g_band.png', dpi=300)
    plt.close()

    raise
Beispiel #12
0
def main():

    x0 = np.linspace(0, 2047, 2048)
    y0 = np.linspace(0, 1488, 1489)
    """
	# total catalogue with spec-redshift
	with h5py.File('/mnt/ddnfs/data_users/cxkttwl/ICL/data/mpi_h5/sample_catalog.h5', 'r') as f:
		catalogue = np.array(f['a'])
	z = catalogue[0]
	ra = catalogue[1]
	dec = catalogue[2]
	"""
    for kk in range(3):

        with h5py.File(
                load + 'random_cat/cat_select/rand_%s_band_catalog.h5' %
            (band[kk]), 'r') as f:
            tmp_array = np.array(f['a'])
        ra, dec, z, rich = np.array(tmp_array[0]), np.array(
            tmp_array[1]), np.array(tmp_array[2]), np.array(tmp_array[3])
        zN = len(z)

        set_z, set_ra, set_dec = z, ra, dec

        DN = len(set_z)
        m, n = divmod(DN, cpus)
        N_sub0, N_sub1 = m * rank, (rank + 1) * m
        if rank == cpus - 1:
            N_sub1 += n
        map_stack(kk, set_ra[N_sub0:N_sub1], set_dec[N_sub0:N_sub1],
                  set_z[N_sub0:N_sub1])
        commd.Barrier()

        if rank == 0:

            mean_map11 = np.zeros((len(y0), len(x0)), dtype=np.float)

            for pp in range(cpus):

                with h5py.File(
                        tmp + 'stack_dust-map-11_%d_in_%s_band.h5' %
                    (pp, band[kk]), 'r') as f:
                    map_11 = np.array(f['a'])
                mean_map11 = mean_map11 + map_11

            mean_map11 = mean_map11 / DN

            with h5py.File(
                    load + 'random_cat/angle_stack/map_11_stack_%s_band.h5' %
                (band[kk], ), 'w') as f:
                f['a'] = np.array(mean_map11)

            plt.figure(figsize=(7.5, 4.5))
            gf1 = plt.imshow(mean_map11, cmap='rainbow', origin='lower')
            plt.title('$map_{2011} \; stack \; in \; %s \; band$' %
                      (band[kk], ))
            plt.colorbar(gf1, fraction=0.035, pad=0.01, label='$ E_{B-V} $')
            plt.subplots_adjust(left=0.01, right=0.85)
            plt.savefig(
                load +
                'random_cat/angle_stack/map_11_stack_%s_band.png' % band[kk],
                dpi=600)
            plt.close()

            Av = Rv * mean_map11
            Al = A_wave(l_wave[kk], Rv) * Av
            devi_flux = 10**(Al / 2.5) - 1

            plt.figure(figsize=(7.5, 4.5))
            plt.title(
                '$ A_{\\lambda} \,[based \; on \; map_{2011} \; %s \; band]$' %
                (band[kk], ))
            gf1 = plt.imshow(Al, cmap='rainbow', origin='lower')
            plt.colorbar(
                gf1,
                fraction=0.035,
                pad=0.01,
            )
            plt.subplots_adjust(left=0.01, right=0.85)
            plt.savefig(load +
                        'random_cat/angle_stack/Al_map-11_stack_%s_band.png' %
                        band[kk],
                        dpi=300)
            plt.close()

            plt.figure(figsize=(7.5, 4.5))
            plt.title(
                '$ 10^{ A_{\\lambda} / 2.5} - 1 \,[based \; on \; map_{2011} \; %s \; band]$'
                % (band[kk], ))
            gf1 = plt.imshow(devi_flux, cmap='rainbow', origin='lower')
            plt.colorbar(
                gf1,
                fraction=0.035,
                pad=0.01,
            )
            plt.subplots_adjust(left=0.01, right=0.85)
            plt.savefig(
                load +
                'random_cat/angle_stack/flux_devi_map-11_stack_%s_band.png' %
                band[kk],
                dpi=300)
            plt.close()

        commd.Barrier()