def M2L_slope_func(N_samples,
                   sub_sm_file,
                   sub_m2l_file,
                   aveg_m2l_file,
                   wind_L,
                   poly_order,
                   d_lgR=False):

    tmp_r, tmp_M2L, tmp_d_M2L = [], [], []

    for nn in range(N_samples):

        o_dat = pds.read_csv(sub_sm_file % nn, )
        tt_r = np.array(o_dat['R'])
        tt_M = np.array(o_dat['surf_mass'])
        tt_Li = np.array(o_dat['lumi'])

        tt_M2L = tt_M / tt_Li

        WL, p_order = wind_L, poly_order

        if d_lgR == True:
            delt_x = np.median(np.diff(np.log10(tt_r)))
        else:
            delt_x = 1.0

        tt_dM2L = smooth_slope_func(tt_r, tt_M2L, WL, p_order, delt_x)

        keys = ['R', 'M/Li', 'd_M/L_dlgr']
        values = [tt_r, tt_M2L, tt_dM2L]
        fill = dict(zip(keys, values))
        out_data = pds.DataFrame(fill)
        out_data.to_csv(sub_m2l_file % nn, )

        tmp_r.append(tt_r)
        tmp_M2L.append(tt_M2L)
        tmp_d_M2L.append(tt_dM2L)

    aveg_R, aveg_M2L, aveg_M2L_err = arr_jack_func(tmp_M2L, tmp_r,
                                                   N_samples)[:3]
    aveg_R, aveg_dM2L, aveg_dM2L_err = arr_jack_func(tmp_d_M2L, tmp_r,
                                                     N_samples)[:3]

    keys = ['R', 'M/Li', 'M/Li-err', 'd_M/Li', 'd_M/Li_err']
    values = [aveg_R, aveg_M2L, aveg_M2L_err, aveg_dM2L, aveg_dM2L_err]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(aveg_m2l_file, )

    return
예제 #2
0
def aveg_mass_pro_func(N_samples,
                       band_str,
                       jk_sub_m_file,
                       jk_aveg_m_file,
                       lgM_cov_file,
                       M_cov_file=None):

    ### jack mean and figs
    tmp_r, tmp_mass = [], []
    tmp_c_mass, tmp_lumi = [], []

    for nn in range(N_samples):

        o_dat = pds.read_csv(jk_sub_m_file % nn, )
        sub_R, sub_mass = np.array(o_dat['R']), np.array(o_dat['surf_mass'])
        sub_c_mass, sub_lumi = np.array(o_dat['cumu_mass']), np.array(
            o_dat['lumi'])

        id_nn_0 = np.isnan(sub_mass)
        id_nn_1 = np.isnan(sub_c_mass)
        id_nn_2 = np.isnan(sub_lumi)

        id_nn = id_nn_0 | id_nn_1 | id_nn_2

        #. keep the array length and replace id_nn values
        sub_R[id_nn] = np.nan
        sub_mass[id_nn] = np.nan
        sub_c_mass[id_nn] = np.nan
        sub_lumi[id_nn] = np.nan

        tmp_r.append(sub_R)
        tmp_mass.append(sub_mass)
        tmp_c_mass.append(sub_c_mass)
        tmp_lumi.append(sub_lumi)

    ### jack-mean pf mass and lumi profile
    aveg_R, aveg_surf_m, aveg_surf_m_err = arr_jack_func(
        tmp_mass, tmp_r, N_samples)[:3]
    aveg_R, aveg_cumu_m, aveg_cumu_m_err = arr_jack_func(
        tmp_c_mass, tmp_r, N_samples)[:3]
    aveg_R, aveg_lumi, aveg_lumi_err = arr_jack_func(tmp_lumi, tmp_r,
                                                     N_samples)[:3]
    aveg_R, aveg_lgM, aveg_lgM_err = arr_jack_func(np.log10(tmp_mass), tmp_r,
                                                   N_samples)[:3]

    keys = [
        'R', 'surf_mass', 'surf_mass_err', 'cumu_mass', 'cumu_mass_err',
        'lumi', 'lumi_err', 'lg_M', 'lg_M_err'
    ]
    values = [
        aveg_R, aveg_surf_m, aveg_surf_m_err, aveg_cumu_m, aveg_cumu_m_err,
        aveg_lumi, aveg_lumi_err, aveg_lgM, aveg_lgM_err
    ]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(jk_aveg_m_file)

    ### cov_arr of mass profile
    #.. use lg_mass to calculate cov_arr to avoid to huge value occur
    lg_M_arr = np.log10(tmp_mass)

    R_mean, cov_MX, cor_MX = cov_MX_func(tmp_r, list(lg_M_arr), id_jack=True)

    with h5py.File(lgM_cov_file, 'w') as f:
        f['R_kpc'] = np.array(R_mean)
        f['cov_MX'] = np.array(cov_MX)
        f['cor_MX'] = np.array(cor_MX)

    if M_cov_file is not None:

        R_mean, cov_MX, cor_MX = cov_MX_func(tmp_r, tmp_mass, id_jack=True)

        with h5py.File(M_cov_file, 'w') as f:
            f['R_kpc'] = np.array(R_mean)
            f['cov_MX'] = np.array(cov_MX)
            f['cor_MX'] = np.array(cor_MX)

    return
def sub_color_slope_func(N_samples,
                         sub_arr_file,
                         sub_slope_file,
                         aveg_slope_file,
                         wind_L,
                         poly_order,
                         d_lgR=False):
    """
    sub_arr_file, sub_slope_file, aveg_slope_file : .csv files
    """
    tmp_r, tmp_dgr, tmp_dgi, tmp_dri = [], [], [], []

    for ll in range(N_samples):

        pdat = pds.read_csv(sub_arr_file % ll)
        tt_r, tt_gr, tt_gr_err = np.array(pdat['R']), np.array(
            pdat['g-r']), np.array(pdat['g-r_err'])
        tt_gi, tt_gi_err = np.array(pdat['g-i']), np.array(pdat['g-i_err'])
        tt_ri, tt_ri_err = np.array(pdat['r-i']), np.array(pdat['r-i_err'])

        WL, p_order = wind_L, poly_order

        if d_lgR == True:
            delt_x = np.median(np.diff(np.log10(tt_r)))
        else:
            delt_x = 1.0

        d_gr_dlgr = smooth_slope_func(tt_r, tt_gr, WL, p_order, delt_x)
        d_gi_dlgr = smooth_slope_func(tt_r, tt_gi, WL, p_order, delt_x)
        d_ri_dlgr = smooth_slope_func(tt_r, tt_ri, WL, p_order, delt_x)

        keys = ['R', 'd_gr_dlgr', 'd_gi_dlgr', 'd_ri_dlgr']
        values = [
            tt_r,
            d_gr_dlgr,
            d_gi_dlgr,
            d_ri_dlgr,
        ]

        fill = dict(zip(keys, values))
        out_data = pds.DataFrame(fill)
        out_data.to_csv(sub_slope_file % ll, )

        tmp_r.append(tt_r)
        tmp_dgr.append(d_gr_dlgr)
        tmp_dgi.append(d_gi_dlgr)
        tmp_dri.append(d_ri_dlgr)

    aveg_R_0, aveg_dgr, aveg_dgr_err = arr_jack_func(tmp_dgr, tmp_r,
                                                     N_samples)[:3]
    aveg_R_1, aveg_dgi, aveg_dgi_err = arr_jack_func(tmp_dgi, tmp_r,
                                                     N_samples)[:3]
    aveg_R_2, aveg_dri, aveg_dri_err = arr_jack_func(tmp_dri, tmp_r,
                                                     N_samples)[:3]

    Len_x = np.max([len(aveg_R_0), len(aveg_R_1), len(aveg_R_2)])
    id_L = [len(aveg_R_0), len(aveg_R_1), len(aveg_R_2)].index(Len_x)

    cc_aveg_R = [aveg_R_0, aveg_R_1, aveg_R_2][id_L]

    cc_aveg_dgr = np.zeros(Len_x, )
    cc_aveg_dgr[:len(aveg_dgr)] = aveg_dgr
    cc_aveg_dgr_err = np.zeros(Len_x, )
    cc_aveg_dgr_err[:len(aveg_dgr)] = aveg_dgr_err

    cc_aveg_dgi = np.zeros(Len_x, )
    cc_aveg_dgi[:len(aveg_dgi)] = aveg_dgi
    cc_aveg_dgi_err = np.zeros(Len_x, )
    cc_aveg_dgi_err[:len(aveg_dgi)] = aveg_dgi_err

    cc_aveg_dri = np.zeros(Len_x, )
    cc_aveg_dri[:len(aveg_dri)] = aveg_dri
    cc_aveg_dri_err = np.zeros(Len_x, )
    cc_aveg_dri_err[:len(aveg_dri)] = aveg_dri_err

    keys = [
        'R_kpc', 'd_gr', 'd_gr_err', 'd_gi', 'd_gi_err', 'd_ri', 'd_ri_err'
    ]
    values = [
        cc_aveg_R, cc_aveg_dgr, cc_aveg_dgr_err, cc_aveg_dgi, cc_aveg_dgi_err,
        cc_aveg_dri, cc_aveg_dri_err
    ]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(aveg_slope_file)

    return
def sub_color_func(N_samples,
                   tt_r_file,
                   tt_g_file,
                   tt_i_file,
                   sub_color_file,
                   aveg_C_file,
                   id_dered=False,
                   Al_arr=None,
                   low_R_lim=None):
    """
    Al_arr : extinction array, in order r-band, g-band, i-band
    """
    tmp_r, tmp_gr, tmp_gi, tmp_ri = [], [], [], []

    for ll in range(N_samples):

        p_r_dat = pds.read_csv(tt_r_file % ll)
        tt_r_R, tt_r_sb, tt_r_err = np.array(p_r_dat['R']), np.array(
            p_r_dat['BG_sub_SB']), np.array(p_r_dat['sb_err'])

        p_g_dat = pds.read_csv(tt_g_file % ll)
        tt_g_R, tt_g_sb, tt_g_err = np.array(p_g_dat['R']), np.array(
            p_g_dat['BG_sub_SB']), np.array(p_g_dat['sb_err'])

        p_i_dat = pds.read_csv(tt_i_file % ll)
        tt_i_R, tt_i_sb, tt_i_err = np.array(p_i_dat['R']), np.array(
            p_i_dat['BG_sub_SB']), np.array(p_i_dat['sb_err'])

        if low_R_lim is None:
            idR_lim = tt_r_R <= 1.2e3
            tt_r_R, tt_r_sb, tt_r_err = tt_r_R[idR_lim], tt_r_sb[
                idR_lim], tt_r_err[idR_lim]

            idR_lim = tt_g_R <= 1.2e3
            tt_g_R, tt_g_sb, tt_g_err = tt_g_R[idR_lim], tt_g_sb[
                idR_lim], tt_g_err[idR_lim]

            idR_lim = tt_i_R <= 1.2e3
            tt_i_R, tt_i_sb, tt_i_err = tt_i_R[idR_lim], tt_i_sb[
                idR_lim], tt_i_err[idR_lim]

        else:
            idR_lim = (tt_r_R <= 1.2e3) & (tt_r_R >= low_R_lim)
            tt_r_R, tt_r_sb, tt_r_err = tt_r_R[idR_lim], tt_r_sb[
                idR_lim], tt_r_err[idR_lim]

            idR_lim = (tt_g_R <= 1.2e3) & (tt_g_R >= low_R_lim)
            tt_g_R, tt_g_sb, tt_g_err = tt_g_R[idR_lim], tt_g_sb[
                idR_lim], tt_g_err[idR_lim]

            idR_lim = (tt_i_R <= 1.2e3) & (tt_i_R >= low_R_lim)
            tt_i_R, tt_i_sb, tt_i_err = tt_i_R[idR_lim], tt_i_sb[
                idR_lim], tt_i_err[idR_lim]

        gr_arr, gr_err = color_func(tt_g_sb, tt_g_err, tt_r_sb, tt_r_err)
        gi_arr, gi_err = color_func(tt_g_sb, tt_g_err, tt_i_sb, tt_i_err)
        ri_arr, ri_err = color_func(tt_r_sb, tt_r_err, tt_i_sb, tt_i_err)

        if id_dered == True:
            Al_r, Al_g, Al_i = Al_arr[:]

            gr_arr = gr_arr + np.nanmedian(Al_r) - np.nanmedian(Al_g)
            gi_arr = gi_arr + np.nanmedian(Al_i) - np.nanmedian(Al_g)
            ri_arr = ri_arr + np.nanmedian(Al_i) - np.nanmedian(Al_r)

        keys = ['R', 'g-r', 'g-r_err', 'g-i', 'g-i_err', 'r-i', 'r-i_err']
        values = [tt_g_R, gr_arr, gr_err, gi_arr, gi_err, ri_arr, ri_err]
        fill = dict(zip(keys, values))
        out_data = pds.DataFrame(fill)
        out_data.to_csv(sub_color_file % ll, )

        tmp_r.append(tt_g_R)
        tmp_gr.append(gr_arr)
        tmp_gi.append(gi_arr)
        tmp_ri.append(ri_arr)

    aveg_R_0, aveg_gr, aveg_gr_err = arr_jack_func(tmp_gr, tmp_r,
                                                   N_samples)[:3]
    aveg_R_1, aveg_gi, aveg_gi_err = arr_jack_func(tmp_gi, tmp_r,
                                                   N_samples)[:3]
    aveg_R_2, aveg_ri, aveg_ri_err = arr_jack_func(tmp_ri, tmp_r,
                                                   N_samples)[:3]

    Len_x = np.max([len(aveg_R_0), len(aveg_R_1), len(aveg_R_2)])
    id_L = [len(aveg_R_0), len(aveg_R_1), len(aveg_R_2)].index(Len_x)

    cc_aveg_R = [aveg_R_0, aveg_R_1, aveg_R_2][id_L]

    cc_aveg_gr = np.zeros(Len_x, )
    cc_aveg_gr[:len(aveg_gr)] = aveg_gr
    cc_aveg_gr_err = np.zeros(Len_x, )
    cc_aveg_gr_err[:len(aveg_gr)] = aveg_gr_err

    cc_aveg_gi = np.zeros(Len_x, )
    cc_aveg_gi[:len(aveg_gi)] = aveg_gi
    cc_aveg_gi_err = np.zeros(Len_x, )
    cc_aveg_gi_err[:len(aveg_gi)] = aveg_gi_err

    cc_aveg_ri = np.zeros(Len_x, )
    cc_aveg_ri[:len(aveg_ri)] = aveg_ri
    cc_aveg_ri_err = np.zeros(Len_x, )
    cc_aveg_ri_err[:len(aveg_ri)] = aveg_ri_err

    keys = ['R_kpc', 'g-r', 'g-r_err', 'g-i', 'g-i_err', 'r-i', 'r-i_err']
    values = [
        cc_aveg_R, cc_aveg_gr, cc_aveg_gr_err, cc_aveg_gi, cc_aveg_gi_err,
        cc_aveg_ri, cc_aveg_ri_err
    ]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(aveg_C_file, )

    return
def jack_aveg_SB_func(N_samples,
                      band_str,
                      ra,
                      dec,
                      z,
                      prof_cat,
                      jk_sub_file,
                      jk_aveg_file,
                      r_bins,
                      z_ref=None):
    """
	ra, dec, z : ra, dec, redshift of BCG
	N_samples : number of jackknife subsample
	prof_cat : the profMean table of SDSS photometric table

	"""
    zN = len(ra)
    id_arr = np.arange(0, zN, 1)
    id_group = id_arr % N_samples

    lis_ra, lis_dec, lis_z = [], [], []

    ## sub-sample
    for nn in range(N_samples):

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

        lis_ra.append(ra[id_xbin])
        lis_dec.append(dec[id_xbin])
        lis_z.append(z[id_xbin])

    ## jackknife sub-sample
    for nn in range(N_samples):

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

        set_ra, set_dec, set_z = np.array([]), np.array([]), np.array([])

        for oo in (jack_id):
            set_ra = np.r_[set_ra, lis_ra[oo]]
            set_dec = np.r_[set_dec, lis_dec[oo]]
            set_z = np.r_[set_z, lis_z[oo]]

        _nn_sub_file = jk_sub_file % nn  ## read SDSS photo_data

        BCG_SB_pros_func(band_str,
                         set_z,
                         set_ra,
                         set_dec,
                         prof_cat,
                         jk_sub_file,
                         r_bins,
                         z_ref=z_ref)

    ## mean of jackknife sample
    tmp_r, tmp_sb = [], []
    for nn in range(N_samples):

        pro_dat = pds.read_csv(jk_sub_file % nn)

        tt_r, tt_sb = np.array(pro_dat['R_ref']), np.array(pro_dat['SB_fdens'])

        tmp_r.append(tt_r)
        tmp_sb.append(tt_sb)

    mean_R, mean_sb, mean_sb_err, lim_R = arr_jack_func(
        tmp_sb, tmp_r, N_samples)

    keys = ['R', 'aveg_sb', 'aveg_sb_err']
    values = [mean_R, mean_sb, mean_sb_err]
    fill = dict(zip(keys, values))
    out_data = pds.DataFrame(fill)
    out_data.to_csv(jk_aveg_file)

    return
            tt_r2i[idx_lim] = np.nan

            dered_tt_g2r[idx_lim] = np.nan
            dered_tt_g2i[idx_lim] = np.nan
            dered_tt_r2i[idx_lim] = np.nan

            jk_rs.append(tt_R)
            jk_g2r.append(tt_g2r)
            jk_g2i.append(tt_g2i)
            jk_r2i.append(tt_r2i)

            dered_jk_g2r.append(dered_tt_g2r)
            dered_jk_g2i.append(dered_tt_g2i)
            dered_jk_r2i.append(dered_tt_r2i)

        m_jk_R, m_jk_g2r, m_jk_g2r_err, std_lim_R = arr_jack_func(
            jk_g2r, jk_rs, N_samples)
        m_jk_R, m_jk_g2i, m_jk_g2i_err, std_lim_R = arr_jack_func(
            jk_g2i, jk_rs, N_samples)
        m_jk_R, m_jk_r2i, m_jk_r2i_err, std_lim_R = arr_jack_func(
            jk_r2i, jk_rs, N_samples)

        m_dered_jk_R, m_dered_jk_g2r, m_dered_jk_g2r_err, std_lim_R = arr_jack_func(
            dered_jk_g2r, jk_rs, N_samples)
        m_dered_jk_R, m_dered_jk_g2i, m_dered_jk_g2i_err, std_lim_R = arr_jack_func(
            dered_jk_g2i, jk_rs, N_samples)
        m_dered_jk_R, m_dered_jk_r2i, m_dered_jk_r2i_err, std_lim_R = arr_jack_func(
            dered_jk_r2i, jk_rs, N_samples)

        ## save
        keys = [
            'R(cMpc/h)', 'g2r', 'g2r_err', 'g2i', 'g2i_err', 'r2i', 'r2i_err',