def BG_sub_sb_func(
    N_sample,
    jk_sub_sb,
    sb_out_put,
    band_str,
    BG_file,
    trunk_R=2e3,
):
    """
    by default, trunk_R = 2Mpc, which meanse the model signal beyond 2Mpc will be treated as 
    background and subtracted from the observation
    """
    tmp_r, tmp_sb = [], []

    cat = pds.read_csv(BG_file)
    (e_a, e_b, e_x0, e_A, e_alpha, e_B,
     offD) = (np.array(cat['e_a'])[0], np.array(cat['e_b'])[0],
              np.array(cat['e_x0'])[0], np.array(cat['e_A'])[0],
              np.array(cat['e_alpha'])[0], np.array(cat['e_B'])[0],
              np.array(cat['offD'])[0])

    I_e, R_e = np.array(cat['I_e'])[0], np.array(cat['R_e'])[0]
    sb_2Mpc = sersic_func(trunk_R, I_e, R_e)

    for kk in range(N_sample):

        with h5py.File(jk_sub_sb % kk, 'r') as f:
            c_r_arr = np.array(f['r'])
            c_sb_arr = np.array(f['sb'])
            c_sb_err = np.array(f['sb_err'])
            npix = np.array(f['npix'])

        id_Nul = npix < 1
        c_r_arr[id_Nul] = np.nan
        c_sb_arr[id_Nul] = np.nan
        c_sb_err[id_Nul] = np.nan

        full_r_fit = cc_rand_sb_func(c_r_arr, e_a, e_b, e_x0, e_A, e_alpha,
                                     e_B)
        full_BG = full_r_fit - offD + sb_2Mpc
        devi_sb = c_sb_arr - full_BG

        tmp_r.append(c_r_arr)
        tmp_sb.append(devi_sb)

    tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(
        tmp_sb,
        tmp_r,
        band_str,
        N_sample,
    )[4:]

    ## save BG-subtracted pros
    with h5py.File(sb_out_put, 'w') as f:
        f['r'] = np.array(tt_jk_R)
        f['sb'] = np.array(tt_jk_SB)
        f['sb_err'] = np.array(tt_jk_err)

    return
def aveg_jack_PA_SB_func(J_sub_img, J_sub_pix_cont, J_sub_sb, jack_SB_file,
                         N_bin, n_rbins, pix_size, zx, band_str):
    """
	measure the average SB profile of jackknife subsample
	-----------------------------------
	J_sub_img, J_sub_pix_cont : the stacked image and corresponding pixel counts of subsamples
	J_sub_sb : .csv file, the output file of surface brightness profile of subsamples
	jack_SB_file : .csv file, the output of average jackknife subsample surface brightness

	N_bin, n_rbins : the number of subsample (N_bin), and number of radii bins (n_rbins)
	pix_size, zx : pixel scale (in units of arcsec) and redshift of measurement
	band_str : filter information (i.e., 'g', 'r', 'i')
	"""

    #. radius bin
    lim_r = 0

    for nn in range(N_bin):

        with h5py.File(J_sub_img % nn, 'r') as f:
            tmp_img = np.array(f['a'])
        xn, yn = np.int(tmp_img.shape[1] / 2), np.int(tmp_img.shape[0] / 2)

        id_nn = np.isnan(tmp_img)
        eff_y, eff_x = np.where(id_nn == False)
        dR = np.sqrt((eff_y - yn)**2 + (eff_x - xn)**2)
        dR_max = np.int(dR.max()) + 1
        lim_r = np.max([lim_r, dR_max])

    r_bins = np.logspace(0, np.log10(lim_r), n_rbins)

    #. SB measure
    for nn in range(N_bin):

        with h5py.File(J_sub_img % nn, 'r') as f:
            tmp_img = np.array(f['a'])

        with h5py.File(J_sub_pix_cont % nn, 'r') as f:
            tmp_cont = np.array(f['a'])

        xn, yn = np.int(tmp_img.shape[1] / 2), np.int(tmp_img.shape[0] / 2)

        h_array, v_array, d_array = PA_SB_Zx_func(tmp_img, tmp_cont, pix_size,
                                                  xn, yn, zx, r_bins)

        SB_h_R, SB_h, SB_h_err, N_pix_h, nsum_ratio_h = h_array[:]
        SB_v_R, SB_v, SB_v_err, N_pix_v, nsum_ratio_v = v_array[:]
        SB_d_R, SB_d, SB_d_err, N_pix_d, nsum_ratio_d = d_array[:]

        id_hx = N_pix_h < 1.
        SB_h_R[id_hx] = np.nan
        SB_h[id_hx] = np.nan
        SB_h_err[id_hx] = np.nan

        id_vx = N_pix_v < 1.
        SB_v_R[id_vx] = np.nan
        SB_v[id_vx] = np.nan
        SB_v_err[id_vx] = np.nan

        id_dx = N_pix_d < 1.
        SB_d_R[id_dx] = np.nan
        SB_d[id_dx] = np.nan
        SB_d_err[id_dx] = np.nan

        #. save
        keys = [
            'r_h', 'sb_h', 'sb_err_h', 'r_v', 'sb_v', 'sb_err_v', 'r_d',
            'sb_d', 'sb_err_d'
        ]
        values = [
            SB_h_R, SB_h, SB_h_err, SB_v_R, SB_v, SB_v_err, SB_d_R, SB_d,
            SB_d_err
        ]
        fill = dict(zip(keys, values))
        data = pds.DataFrame(fill)
        data.to_csv(J_sub_sb % nn)

    #. average of jackknife
    tmp_h_sb = []
    tmp_h_r = []

    tmp_v_sb = []
    tmp_v_r = []

    tmp_d_sb = []
    tmp_d_r = []

    for nn in range(N_bin):

        n_dat = pds.read_csv(J_sub_sb % nn)

        r_arr = np.array(n_dat['r_h'])
        sb_arr = np.array(n_dat['sb_h'])
        sb_err = np.array(n_dat['sb_err_h'])

        tmp_h_sb.append(sb_arr)
        tmp_h_r.append(r_arr)

        r_arr = np.array(n_dat['r_v'])
        sb_arr = np.array(n_dat['sb_v'])
        sb_err = np.array(n_dat['sb_err_v'])

        tmp_v_sb.append(sb_arr)
        tmp_v_r.append(r_arr)

        r_arr = np.array(n_dat['r_d'])
        sb_arr = np.array(n_dat['sb_d'])
        sb_err = np.array(n_dat['sb_err_d'])

        tmp_d_sb.append(sb_arr)
        tmp_d_r.append(r_arr)

    ## only save the sb result in unit " nanomaggies / arcsec^2 "
    tt_jk_R_h, tt_jk_SB_h, tt_jk_err_h, lim_R_h = jack_SB_func(
        tmp_h_sb, tmp_h_r, band_str, N_bin)[4:]
    tt_jk_R_v, tt_jk_SB_v, tt_jk_err_v, lim_R_v = jack_SB_func(
        tmp_v_sb, tmp_v_r, band_str, N_bin)[4:]
    tt_jk_R_d, tt_jk_SB_d, tt_jk_err_d, lim_R_d = jack_SB_func(
        tmp_d_sb, tmp_d_r, band_str, N_bin)[4:]

    #. adjust the array length
    Len = [len(tt_jk_R_h), len(tt_jk_R_v), len(tt_jk_R_d)]
    L_max = np.max(Len)

    out_R_v = np.ones(L_max, ) * np.nan
    out_SB_v = np.ones(L_max, ) * np.nan
    out_err_v = np.ones(L_max, ) * np.nan

    out_R_v[:Len[0]] = tt_jk_R_v[:Len[0]]
    out_SB_v[:Len[0]] = tt_jk_SB_v[:Len[0]]
    out_err_v[:Len[0]] = tt_jk_err_v[:Len[0]]

    out_R_h = np.ones(L_max, ) * np.nan
    out_SB_h = np.ones(L_max, ) * np.nan
    out_err_h = np.ones(L_max, ) * np.nan

    out_R_h[:Len[1]] = tt_jk_R_h[:Len[1]]
    out_SB_h[:Len[1]] = tt_jk_SB_h[:Len[1]]
    out_err_h[:Len[1]] = tt_jk_err_h[:Len[1]]

    out_R_d = np.ones(L_max, ) * np.nan
    out_SB_d = np.ones(L_max, ) * np.nan
    out_err_d = np.ones(L_max, ) * np.nan

    out_R_d[:Len[2]] = tt_jk_R_d[:Len[2]]
    out_SB_d[:Len[2]] = tt_jk_SB_d[:Len[2]]
    out_err_d[:Len[2]] = tt_jk_err_d[:Len[2]]

    sb_lim_r_h = np.ones(len(out_R_h)) * lim_R_h
    sb_lim_r_v = np.ones(len(out_R_v)) * lim_R_v
    sb_lim_r_d = np.ones(len(out_R_d)) * lim_R_d

    #.
    keys = [
        'r_h', 'sb_h', 'sb_err_h', 'lim_R_h', 'r_v', 'sb_v', 'sb_err_v',
        'lim_R_v', 'r_d', 'sb_d', 'sb_err_d', 'lim_R_d'
    ]
    values = [
        out_R_h, out_SB_h, out_err_h, sb_lim_r_h, out_R_v, out_SB_v, out_err_v,
        sb_lim_r_v, out_R_d, out_SB_d, out_err_d, sb_lim_r_d
    ]

    fill = dict(zip(keys, values))
    data = pds.DataFrame(fill)
    data.to_csv(jack_SB_file)

    return
Пример #3
0
def stack_BG_sub_func(sat_sb_file,
                      bg_sb_file,
                      band_str,
                      N_sample,
                      out_file,
                      sub_out_file=None):
    """
	sat_sb_file : SB(r) of satellites, .h5 files
	band_str : filter information
	bg_sb_file : background SB(r), .h5 file
	out_file : .csv file
	N_sample : number of subsample
	sub_out_file : output the BG-sub profile of subsamples or not
	"""

    tmp_SB, tmp_R = [], []

    with h5py.File(bg_sb_file, 'r') as f:
        t_bg_r = np.array(f['r'])
        t_bg_sb = np.array(f['sb'])
        t_bg_err = np.array(f['sb_err'])

    interp_mu_F = interp.interp1d(t_bg_r,
                                  t_bg_sb,
                                  kind='linear',
                                  fill_value='extrapolate')

    interp_mu_err = interp.interp1d(t_bg_r,
                                    t_bg_err,
                                    kind='linear',
                                    fill_value='extrapolate')

    for kk in range(N_sample):

        with h5py.File(sat_sb_file % kk, 'r') as f:

            tt_r = np.array(f['r'])
            tt_sb = np.array(f['sb'])
            tt_err = np.array(f['sb_err'])
            tt_npix = np.array(f['npix'])

        id_Nul = tt_npix < 1
        tt_r[id_Nul] = np.nan
        tt_sb[id_Nul] = np.nan
        tt_err[id_Nul] = np.nan

        _kk_sb = tt_sb - interp_mu_F(tt_r)

        if sub_out_file is not None:

            _kk_err = np.sqrt(tt_err**2 + interp_mu_err(tt_r)**2)

            keys = ['r', 'sb', 'sb_err']
            values = [tt_r, _kk_sb, _kk_err]
            fill = dict(zip(keys, values))
            out_data = pds.DataFrame(fill)
            out_data.to_csv(sub_out_file % kk)

        tmp_R.append(tt_r)
        tmp_SB.append(_kk_sb)

    tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(tmp_SB, tmp_R, band_str,
                                                       N_sample)[4:]

    keys = ['r', 'sb', 'sb_err']
    values = [tt_jk_R, tt_jk_SB, tt_jk_err]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(out_file)

    return
            with h5py.File(J_sub_sb % nn, 'r') as f:
                r_arr = np.array(f['r'])[:-1]
                sb_arr = np.array(f['sb'])[:-1]
                sb_err = np.array(f['sb_err'])[:-1]
                npix = np.array(f['npix'])[:-1]
                nratio = np.array(f['nratio'])[:-1]

            idvx = npix < 1.
            sb_arr[idvx] = np.nan
            r_arr[idvx] = np.nan

            tmp_sb.append(sb_arr)
            tmp_r.append(r_arr)

        ## only save the sb result in unit " nanomaggies / arcsec^2 "
        tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(
            tmp_sb, tmp_r, band_str, N_bin)[4:]
        sb_lim_r = np.ones(len(tt_jk_R)) * lim_R

        with h5py.File(jack_SB_file, 'w') as f:
            f['r'] = np.array(tt_jk_R)
            f['sb'] = np.array(tt_jk_SB)
            f['sb_err'] = np.array(tt_jk_err)
            f['lim_r'] = np.array(sb_lim_r)

        order_id = np.arange(0, N_bin, 1)
        order_id = order_id.astype(np.int32)
        weit_aveg_img(
            order_id,
            sub_img,
            sub_pix_cont,
            jack_img,
def jack_main_func(id_cen, N_bin, n_rbins, cat_ra, cat_dec, cat_z, img_x, img_y, img_file, band_str, sub_img,
	sub_pix_cont, sub_sb, J_sub_img, J_sub_pix_cont, J_sub_sb, jack_SB_file, jack_img, jack_cont_arr,
	id_cut = False, N_edg = None, id_Z0 = True, z_ref = None, id_S2N = False, S2N = None, id_sub = True, edg_bins = None,
	sub_rms = None, J_sub_rms = None, jack_rms_arr = None, N_weit = None):
	"""
	combining jackknife stacking process, and 
	save : sub-sample (sub-jack-sample) stacking image, pixel conunt array, surface brightness profiles
	id_cen : 0 - stacking by centering on BCGs, 1 - stacking by centering on img center

	N_bin : number of jackknife sample
	n_rbins : the number of radius bins (int type)

	cat_ra, cat_dec, cat_z : catalog information about the stacking sample, ra, dec, z
	img_x, img_y : BCG position (in image coordinate)

	img_file : img-data name (include file-name structure:'/xxx/xxx/xxx.xxx')
	band_str : the band of imgs, 'str' type

	sub_img, sub_pix_cont, sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of individual sub-sample img stacking result 

	J_sub_img, J_sub_pix_cont, J_sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of jackknife sub-sample img stacking result

	jack_SB_file : file name of the final jackknife stacking SB profile ('/xxx/xxx/xxx.xxx')
	jack_img : mean of the jackknife stacking img ('/xxx/xxx/xxx.xxx')
	jack_cont_arr : mean of the pixel count array ('/xxx/xxx/xxx.xxx')

	id_cut : id_cut == True, cut img edge pixels before stacking, id_cut == False, just stacking original size imgs
	N_edg : the cut region width, in unit of pixel, only applied when id_cut == True, pixels in this region will be set as 
			'no flux' contribution pixels (ie. set as np.nan)
	
	id_Z0 : stacking imgs on observation coordinate (id_Z0 = True, and reference redshift is z_ref) 
			or not (id_Z0 = False, give radius in physical unit, kpc), default is True
	
	id_S2N, S2N :  if set S/N limitation for SB profile measure or not, Default is False (no limitation applied).
					if id_S2N = True, then measure the SB profile, and in region where S/N is lower than S2N with be 
					treated as only one radius bins (edg_bins = None), (or measured according edg_bins.)

	id_sub : measure and save the SB profiles for sub-samples of not, default is True
	
	sub_rms, J_sub_rms : pixel standard deviation of stacking images (for sub-sample and jackknife sub-sample)
	jack_rms_file : the final rms_file (total sample imgs stacking result)

	N_weit : if weight the cluster image with satellite number or not ('None')
			if N_weit != None, is has the same length as the cat_ra array

	"""

	print( 'id_Z0', id_Z0 )
	print( 'id_cut', id_cut )
	print( 'N_edg', N_edg )
	print( 'z_ref', z_ref )


	zN = len( cat_z )

	if N_weit is None:
		wit_n_arr = np.ones( zN,)

	else:
		wit_n_arr = N_weit + 0.


	id_arr = np.arange(0, zN, 1)
	id_group = id_arr % N_bin

	lis_ra, lis_dec, lis_z = [], [], []
	lis_x, lis_y = [], []
	lis_wn = []

	for nn in range( N_bin ):

		id_xbin = np.where( id_group == nn )[0]

		lis_ra.append( cat_ra[ id_xbin ] )
		lis_dec.append( cat_dec[ id_xbin ] )
		lis_z.append( cat_z[ id_xbin ] )

		lis_x.append( img_x[ id_xbin ] )
		lis_y.append( img_y[ id_xbin ] )
		lis_wn.append( wit_n_arr[ id_xbin ] )

	band_id = band.index( band_str )

	## img stacking
	for nn in range(N_bin):

		set_z = lis_z[ nn ]
		set_ra = lis_ra[ nn ]
		set_dec = lis_dec[ nn ]

		set_x = lis_x[ nn ]
		set_y = lis_y[ nn ]

		set_wn = lis_wn[ nn ]

		sub_img_file = sub_img % nn
		sub_cont_file = sub_pix_cont % nn

		if sub_rms is not None:
			sub_rms_file = sub_rms % nn
		else:
			sub_rms_file = None

		if id_cut == False:
			stack_func(img_file, sub_img_file, set_z, set_ra, set_dec, band[ band_id ], set_x, set_y, id_cen,
				rms_file = sub_rms_file, pix_con_file = sub_cont_file, N_weit = set_wn )
		if id_cut == True:
			cut_stack_func(img_file, sub_img_file, set_z, set_ra, set_dec, band[ band_id ], set_x, set_y, id_cen, N_edg,
				rms_file = sub_rms_file, pix_con_file = sub_cont_file, N_weit = set_wn )

	for nn in range( N_bin ):

		id_arry = np.linspace(0, N_bin -1, N_bin)
		id_arry = id_arry.astype(int)
		jack_id = list(id_arry)
		jack_id.remove(jack_id[nn])
		jack_id = np.array(jack_id)

		jack_img_file = J_sub_img % nn
		jack_cont_file = J_sub_pix_cont % nn
		weit_aveg_img(jack_id, sub_img, sub_pix_cont, jack_img_file, sum_weit_file = jack_cont_file,)

		if sub_rms is not None:

			jack_rms_file = J_sub_rms % nn
			weit_aveg_img(jack_id, sub_rms, sub_pix_cont, jack_rms_file,)

	## SB measurement
	if id_sub == True:
		## sub-samples
		SB_pros_func(sub_img, sub_pix_cont, sub_sb, N_bin, n_rbins, id_Z0, z_ref)

	if id_S2N == False:	
		## jackknife sub-samples
		SB_pros_func(J_sub_img, J_sub_pix_cont, J_sub_sb, N_bin, n_rbins, id_Z0, z_ref)

		## final jackknife SB profile
		tmp_sb = []
		tmp_r = []
		for nn in range( N_bin ):
			with h5py.File(J_sub_sb % nn, 'r') as f:
				r_arr = np.array(f['r'])[:-1]
				sb_arr = np.array(f['sb'])[:-1]
				sb_err = np.array(f['sb_err'])[:-1]
				npix = np.array(f['npix'])[:-1]
				nratio = np.array(f['nratio'])[:-1]

			idvx = npix < 1.
			sb_arr[idvx] = np.nan
			r_arr[idvx] = np.nan

			tmp_sb.append(sb_arr)
			tmp_r.append(r_arr)

		## only save the sb result in unit " nanomaggies / arcsec^2 "
		tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(tmp_sb, tmp_r, band[ band_id ], N_bin)[4:]
		sb_lim_r = np.ones( len(tt_jk_R) ) * lim_R

		with h5py.File(jack_SB_file, 'w') as f:
			f['r'] = np.array(tt_jk_R)
			f['sb'] = np.array(tt_jk_SB)
			f['sb_err'] = np.array(tt_jk_err)
			f['lim_r'] = np.array(sb_lim_r)

	else:
		if id_Z0 == True:
			lim_SB_pros_func(J_sub_img, J_sub_pix_cont, J_sub_sb, jack_SB_file, n_rbins, N_bin, S2N, band_id, edg_bins,)
		else:
			zref_lim_SB_adjust_func(J_sub_img, J_sub_pix_cont, J_sub_sb, jack_SB_file, n_rbins, N_bin, S2N, z_ref, band_id, edg_bins,)

	# calculate the directly stacking result( 2D_img, pixel_count array, and rms file [if sub_rms is not None] )
	order_id = np.arange(0, N_bin, 1)
	order_id = order_id.astype( np.int32 )
	weit_aveg_img(order_id, sub_img, sub_pix_cont, jack_img, sum_weit_file = jack_cont_arr,)

	if sub_rms is not None:
		weit_aveg_img(order_id, sub_rms, sub_pix_cont, jack_rms_arr,)

	return
def tmp_SB_func(
    R_bins,
    N_bin,
    J_sub_img,
    J_sub_pix_cont,
    J_sub_sb,
    band_id,
    jack_sb,
    pixel,
):

    r_bins = R_bins

    for nn in range(N_bin):

        with h5py.File(J_sub_img % nn, 'r') as f:
            tmp_img = np.array(f['a'])

        with h5py.File(J_sub_pix_cont % nn, 'r') as f:
            tmp_cont = np.array(f['a'])

        xn, yn = np.int(tmp_img.shape[1] / 2), np.int(tmp_img.shape[0] / 2)

        Intns, Angl_r, Intns_err, npix, nratio = light_measure_Z0_weit(
            tmp_img, tmp_cont, pixel, xn, yn, r_bins)
        sb_arr, sb_err_arr = Intns, Intns_err
        r_arr = Angl_r

        with h5py.File(J_sub_sb % nn, 'w') as f:
            f['r'] = np.array(r_arr)
            f['sb'] = np.array(sb_arr)
            f['sb_err'] = np.array(sb_err_arr)
            f['nratio'] = np.array(nratio)
            f['npix'] = np.array(npix)

    ## final jackknife SB profile
    tmp_sb = []
    tmp_r = []
    for nn in range(N_bin):
        with h5py.File(J_sub_sb % nn, 'r') as f:
            r_arr = np.array(f['r'])[:-1]
            sb_arr = np.array(f['sb'])[:-1]
            sb_err = np.array(f['sb_err'])[:-1]
            npix = np.array(f['npix'])[:-1]
            nratio = np.array(f['nratio'])[:-1]

        idvx = npix < 1.
        sb_arr[idvx] = np.nan
        r_arr[idvx] = np.nan

        tmp_sb.append(sb_arr)
        tmp_r.append(r_arr)

    ## only save the sb result in unit " nanomaggies / arcsec^2 "
    tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(tmp_sb, tmp_r,
                                                       band[band_id],
                                                       N_bin)[4:]
    sb_lim_r = np.ones(len(tt_jk_R)) * lim_R

    with h5py.File(jack_sb, 'w') as f:
        f['r'] = np.array(tt_jk_R)
        f['sb'] = np.array(tt_jk_SB)
        f['sb_err'] = np.array(tt_jk_err)
        f['lim_r'] = np.array(sb_lim_r)

    return
Пример #7
0
def sat_BG_fast_stack_func(bcg_ra,
                           bcg_dec,
                           bcg_z,
                           sat_ra,
                           sat_dec,
                           img_x,
                           img_y,
                           img_file,
                           band_str,
                           id_cen,
                           N_bin,
                           n_rbins,
                           sub_img,
                           sub_pix_cont,
                           sub_sb,
                           J_sub_img,
                           J_sub_pix_cont,
                           J_sub_sb,
                           jack_SB_file,
                           jack_img,
                           jack_cont_arr,
                           rank,
                           id_cut=False,
                           N_edg=None,
                           id_Z0=True,
                           z_ref=None,
                           id_S2N=False,
                           S2N=None,
                           edg_bins=None,
                           id_sub=True,
                           sub_rms=None,
                           J_sub_rms=None,
                           jack_rms_arr=None,
                           weit_img=None):
    """
	combining jackknife stacking process, and 
	save : sub-sample (sub-jack-sample) stacking image, pixel conunt array, surface brightness profiles
	id_cen : 0 - stacking by centering on BCGs, 1 - stacking by centering on img center

	N_bin : number of jackknife sample
	n_rbins : the number of radius bins (int type)

	cat_ra, cat_dec, cat_z : catalog information about the stacking sample, ra, dec, z
	sat_ra, sat_dec : satellites' location
	img_x, img_y : satellites position (in image coordinate)

	img_file : img-data name (include file-name structure:'/xxx/xxx/xxx.xxx')
	band_str : the band of imgs, 'str' type

	sub_img, sub_pix_cont, sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of individual sub-sample img stacking result 

	J_sub_img, J_sub_pix_cont, J_sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of jackknife sub-sample img stacking result

	jack_SB_file : file name of the final jackknife stacking SB profile ('/xxx/xxx/xxx.xxx')
	jack_img : mean of the jackknife stacking img ('/xxx/xxx/xxx.xxx')
	jack_cont_arr : mean of the pixel count array ('/xxx/xxx/xxx.xxx')

	id_cut : id_cut == True, cut img edge pixels before stacking, id_cut == False, just stacking original size imgs
	N_edg : the cut region width, in unit of pixel, only applied when id_cut == True, pixels in this region will be set as 
			'no flux' contribution pixels (ie. set as np.nan)
	
	id_Z0 : stacking imgs on observation coordinate (id_Z0 = True, and reference redshift is z_ref) 
			or not (id_Z0 = False, give radius in physical unit, kpc), default is True
	
	id_S2N, S2N :  if set S/N limitation for SB profile measure or not, Default is False (no limitation applied).
					if id_S2N = True, then measure the SB profile, and in region where S/N is lower than S2N with be 
					treated as only one radius bins (edg_bins = None), (or measured according edg_bins.)

	id_sub : measure and save the SB profiles for sub-samples of not, default is True
	
	sub_rms, J_sub_rms : pixel standard deviation of stacking images (for sub-sample and jackknife sub-sample)
	jack_rms_file : the final rms_file (total sample imgs stacking result)
	
	---------------------------
	weit_img : array use to apply weight to each stacked image (can be the masekd image after resampling), default is array
				has value of 1 only.
	"""

    from img_sat_BG_jack_stack import weit_aveg_img
    from img_sat_BG_jack_stack import SB_pros_func
    from img_sat_BG_jack_stack import aveg_stack_img
    from img_sat_BG_stack import cut_stack_func, stack_func

    zN = len(bcg_z)
    id_arr = np.arange(0, zN, 1)
    id_group = id_arr % N_bin

    for nn in range(rank, rank + 1):

        id_xbin = np.where(id_group == nn)[0]

        set_z = bcg_z[id_xbin]
        set_ra = bcg_ra[id_xbin]
        set_dec = bcg_dec[id_xbin]

        set_s_ra = sat_ra[id_xbin]
        set_s_dec = sat_dec[id_xbin]

        set_x = img_x[id_xbin]
        set_y = img_y[id_xbin]

        sub_img_file = sub_img % nn
        sub_cont_file = sub_pix_cont % nn

        if sub_rms is not None:
            sub_rms_file = sub_rms % nn
        else:
            sub_rms_file = None

        if id_cut == False:
            stack_func(img_file,
                       sub_img_file,
                       set_z,
                       set_ra,
                       set_dec,
                       band_str,
                       set_s_ra,
                       set_s_dec,
                       set_x,
                       set_y,
                       id_cen,
                       rms_file=sub_rms_file,
                       pix_con_file=sub_cont_file,
                       weit_img=weit_img)
        if id_cut == True:
            cut_stack_func(img_file,
                           sub_img_file,
                           set_z,
                           set_ra,
                           set_dec,
                           band_str,
                           set_s_ra,
                           set_s_dec,
                           set_x,
                           set_y,
                           id_cen,
                           N_edg,
                           rms_file=sub_rms_file,
                           pix_con_file=sub_cont_file,
                           weit_img=weit_img)

    commd.Barrier()

    for nn in range(rank, rank + 1):

        id_arry = np.linspace(0, N_bin - 1, N_bin)
        id_arry = id_arry.astype(int)
        jack_id = list(id_arry)
        jack_id.remove(jack_id[nn])
        jack_id = np.array(jack_id)

        jack_img_file = J_sub_img % nn
        jack_cont_file = J_sub_pix_cont % nn
        weit_aveg_img(
            jack_id,
            sub_img,
            sub_pix_cont,
            jack_img_file,
            sum_weit_file=jack_cont_file,
        )

        if sub_rms is not None:

            jack_rms_file = J_sub_rms % nn
            weit_aveg_img(
                jack_id,
                sub_rms,
                sub_pix_cont,
                jack_rms_file,
            )

    commd.Barrier()

    if rank == 0:

        ## SB measurement
        if id_sub == True:
            ## sub-samples
            SB_pros_func(sub_img, sub_pix_cont, sub_sb, N_bin, n_rbins, id_Z0,
                         z_ref)

        if id_S2N == False:
            ## jackknife sub-samples
            SB_pros_func(J_sub_img, J_sub_pix_cont, J_sub_sb, N_bin, n_rbins,
                         id_Z0, z_ref)

            ## final jackknife SB profile
            tmp_sb = []
            tmp_r = []

            for nn in range(N_bin):
                with h5py.File(J_sub_sb % nn, 'r') as f:
                    r_arr = np.array(f['r'])[:-1]
                    sb_arr = np.array(f['sb'])[:-1]
                    sb_err = np.array(f['sb_err'])[:-1]
                    npix = np.array(f['npix'])[:-1]
                    nratio = np.array(f['nratio'])[:-1]

                idvx = npix < 1.
                sb_arr[idvx] = np.nan
                r_arr[idvx] = np.nan

                tmp_sb.append(sb_arr)
                tmp_r.append(r_arr)

            ## only save the sb result in unit " nanomaggies / arcsec^2 "
            tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(
                tmp_sb, tmp_r, band_str, N_bin)[4:]
            sb_lim_r = np.ones(len(tt_jk_R)) * lim_R

            with h5py.File(jack_SB_file, 'w') as f:
                f['r'] = np.array(tt_jk_R)
                f['sb'] = np.array(tt_jk_SB)
                f['sb_err'] = np.array(tt_jk_err)
                f['lim_r'] = np.array(sb_lim_r)

        else:
            if id_Z0 == True:
                lim_SB_pros_func(
                    J_sub_img,
                    J_sub_pix_cont,
                    J_sub_sb,
                    jack_SB_file,
                    n_rbins,
                    N_bin,
                    S2N,
                    band_str,
                    edg_bins,
                )
            else:
                zref_lim_SB_adjust_func(
                    J_sub_img,
                    J_sub_pix_cont,
                    J_sub_sb,
                    jack_SB_file,
                    n_rbins,
                    N_bin,
                    S2N,
                    z_ref,
                    band_str,
                    edg_bins,
                )

        # calculate the directly stacking result( 2D_img, pixel_count array, and rms file [if sub_rms is not None] )
        order_id = np.arange(0, N_bin, 1)
        order_id = order_id.astype(np.int32)
        weit_aveg_img(
            order_id,
            sub_img,
            sub_pix_cont,
            jack_img,
            sum_weit_file=jack_cont_arr,
        )

        if sub_rms is not None:
            weit_aveg_img(order_id, sub_rms, sub_pix_cont, jack_rms_arr)

    print('%d-rank, Done!' % rank)

    return
def sky_jack_main_func(
    id_cen,
    N_bin,
    n_rbins,
    cat_ra,
    cat_dec,
    cat_z,
    img_x,
    img_y,
    img_file,
    band_str,
    sub_img,
    sub_pix_cont,
    sub_sb,
    J_sub_img,
    J_sub_pix_cont,
    J_sub_sb,
    jack_SB_file,
    jack_img,
    jack_cont_arr,
    id_mean=0,
    id_cut=False,
    N_edg=None,
    id_Z0=True,
    z_ref=None,
    id_sub=True,
    sub_rms=None,
    J_sub_rms=None,
    jack_rms_arr=None,
):
    """
	combining jackknife stacking process, and 
	save : sub-sample (sub-jack-sample) stacking image, pixel conunt array, surface brightness profiles
	id_cen : 0 - stacking by centering on BCGs, 1 - stacking by centering on img center,
		     2 - stacking by random center
	N_bin : number of jackknife sample
	n_rbins : the number of radius bins (int type)

	cat_ra, cat_dec, cat_z : catalog information about the stacking sample, ra, dec, z
	img_x, img_y : BCG position (in image coordinate)

	img_file : img-data name (include file-name structure:'/xxx/xxx/xxx.xxx')
	band_str : the band of imgs, 'str' type

	sub_img, sub_pix_cont, sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of individual sub-sample img stacking result 

	J_sub_img, J_sub_pix_cont, J_sub_sb (stacking img, pixel counts array, SB profile): 
	file name (including patch and file name: '/xxx/xxx/xxx.xxx') of jackknife sub-sample img stacking result

	jack_SB_file : file name of the final jackknife stacking SB profile ('/xxx/xxx/xxx.xxx')
	jack_img : mean of the jackknife stacking img ('/xxx/xxx/xxx.xxx')
	jack_cont_arr : mean of the pixel count array ('/xxx/xxx/xxx.xxx')

	id_cut : id_cut == True, cut img edge pixels before stacking, id_cut == False, just stacking original size imgs
	N_edg : the cut region width, in unit of pixel, only applied when id_cut == True, pixels in this region will be set as 
			'no flux' contribution pixels (ie. set as np.nan)
	
	id_Z0 : stacking imgs on observation coordinate (id_Z0 = True) 
			or not (id_Z0 = False, give radius in physical unit, kpc), default is True
	
	z_ref : reference redshift, (used when id_Z0 = False,) stacking imgs at z_ref.
	id_sub : if measure the individual sub-samples profile( id_sub = True), or not (id_sub = False)

	id_mean : 0, 1, 2.  0 - img_add = img; 
	1 - img_add = img - np.mean(img); 2 - img_add = img - np.median(img); Default is id_mean = 0
	
	sub_rms, J_sub_rms : pixel standard deviation of stacking images (for sub-sample and jackknife sub-sample)
	jack_rms_file : the final rms_file (total sample imgs stacking result)
	"""

    zN = len(cat_z)
    id_arr = np.arange(0, zN, 1)
    id_group = id_arr % N_bin

    lis_ra, lis_dec, lis_z = [], [], []
    lis_x, lis_y = [], []
    for nn in range(N_bin):

        id_xbin = np.where(id_group == nn)[0]

        lis_ra.append(cat_ra[id_xbin])
        lis_dec.append(cat_dec[id_xbin])
        lis_z.append(cat_z[id_xbin])
        lis_x.append(img_x[id_xbin])
        lis_y.append(img_y[id_xbin])

    ## img stacking
    for nn in range(N_bin):

        set_z = lis_z[nn]
        set_ra = lis_ra[nn]
        set_dec = lis_dec[nn]

        set_x = lis_x[nn]
        set_y = lis_y[nn]

        sub_img_file = sub_img % nn
        sub_cont_file = sub_pix_cont % nn

        if sub_rms is not None:
            sub_rms_file = sub_rms % nn
        else:
            sub_rms_file = None

        if id_cut == False:
            stack_func(
                img_file,
                sub_img_file,
                set_z,
                set_ra,
                set_dec,
                band_str,
                set_x,
                set_y,
                id_cen,
                rms_file=sub_rms_file,
                pix_con_file=sub_cont_file,
                id_mean=id_mean,
            )

        if id_cut == True:
            cut_stack_func(
                img_file,
                sub_img_file,
                set_z,
                set_ra,
                set_dec,
                band_str,
                set_x,
                set_y,
                id_cen,
                N_edg,
                rms_file=sub_rms_file,
                pix_con_file=sub_cont_file,
                id_mean=id_mean,
            )

    for nn in range(N_bin):

        id_arry = np.linspace(0, N_bin - 1, N_bin)
        id_arry = id_arry.astype(int)
        jack_id = list(id_arry)
        jack_id.remove(jack_id[nn])
        jack_id = np.array(jack_id)

        jack_img_file = J_sub_img % nn
        jack_cont_file = J_sub_pix_cont % nn
        weit_aveg_img(
            jack_id,
            sub_img,
            sub_pix_cont,
            jack_img_file,
            sum_weit_file=jack_cont_file,
        )

        if sub_rms is not None:

            jack_rms_file = J_sub_rms % nn
            weit_aveg_img(
                jack_id,
                sub_rms,
                sub_pix_cont,
                jack_rms_file,
            )

    ## SB measurement
    if id_sub == True:
        ## sub-samples
        SB_pros_func(sub_img, sub_pix_cont, sub_sb, N_bin, n_rbins, id_Z0,
                     z_ref)

    ## jackknife sub-samples
    SB_pros_func(J_sub_img, J_sub_pix_cont, J_sub_sb, N_bin, n_rbins, id_Z0,
                 z_ref)

    ## final jackknife SB profile
    tmp_sb = []
    tmp_r = []
    for nn in range(N_bin):
        with h5py.File(J_sub_sb % nn, 'r') as f:
            r_arr = np.array(f['r'])[:-1]
            sb_arr = np.array(f['sb'])[:-1]
            sb_err = np.array(f['sb_err'])[:-1]
            npix = np.array(f['npix'])[:-1]
            nratio = np.array(f['nratio'])[:-1]

            idvx = npix < 1.
            sb_arr[idvx] = np.nan
            r_arr[idvx] = np.nan

            tmp_sb.append(sb_arr)
            tmp_r.append(r_arr)

    ## only save the sb result in unit " nanomaggies / arcsec^2 "
    tt_jk_R, tt_jk_SB, tt_jk_err, lim_R = jack_SB_func(tmp_sb, tmp_r, band_str,
                                                       N_bin)[4:]
    sb_lim_r = np.ones(len(tt_jk_R)) * lim_R

    with h5py.File(jack_SB_file, 'w') as f:
        f['r'] = np.array(tt_jk_R)
        f['sb'] = np.array(tt_jk_SB)
        f['sb_err'] = np.array(tt_jk_err)
        f['lim_r'] = np.array(sb_lim_r)

    # calculate the directly stacking result( 2D_img, pixel_count array, and rms file [if sub_rms is not None] )
    order_id = np.arange(0, N_bin, 1)
    order_id = order_id.astype(np.int32)
    weit_aveg_img(
        order_id,
        sub_img,
        sub_pix_cont,
        jack_img,
        sum_weit_file=jack_cont_arr,
    )

    if sub_rms is not None:
        weit_aveg_img(
            order_id,
            sub_rms,
            sub_pix_cont,
            jack_rms_arr,
        )

    return