Beispiel #1
0
def run_auto(datfile, randfile, outfile):

    fd = h5py.File(datfile)
    fr = h5py.File(randfile)

    cd = fd["cart_pts"]
    cr = fr["cart_pts"]

    dat = np.empty(cd.shape)
    rands = np.empty(cr.shape)

    dat[:, :] = cd[:, :]
    rands[:, :] = cr[:, :]

    wps = clustering.wp(dat,
                        rbins,
                        pibins,
                        randoms=rands,
                        do_cross=False,
                        estimator='Landy-Szalay',
                        N_threads='max')

    outarr = np.empty((rbin_centers.shape[0], 2))
    outarr[:, 0] = rbin_centers
    outarr[:, 1] = wps

    np.savetxt(outfile, outarr)
    def sum_stat(self, theta):

        self.model.param_dict["alpha"] = theta[3]
        self.model.param_dict["logMmin"] = theta[2]
        self.model.param_dict["sigma_logM"] = theta[1]
        self.model.param_dict["logM0"] = theta[0]
        self.model.param_dict["logM1"] = theta[4]
        if np.all((prior_range[:, 0] < theta) & (theta < prior_range[:, 1])):
            try:
                self.model.populate_mock()
                nbar = self.model.mock.number_density
                pos = three_dim_pos_bundle(table=self.model.mock.galaxy_table, key1="x", key2="y", key3="z")
                w_p = wp(pos, rbins, pi_bins, period=np.array([L, L, L]))
                return [nbar, w_p]
            except ValueError:
                return [10.0, np.zeros(14)]
        else:
            return [10.0, np.zeros(14)]
    def sum_stat(self, theta):

        self.model.param_dict['alpha'] = theta[3]
        self.model.param_dict['logMmin'] = theta[2]
        self.model.param_dict['sigma_logM'] = theta[1]
        self.model.param_dict['logM0'] = theta[0]
        self.model.param_dict['logM1'] = theta[4]
        if np.all((prior_range[:, 0] < theta) & (theta < prior_range[:, 1])):
            try:
                self.model.populate_mock()
                nbar = self.model.mock.number_density
                pos = three_dim_pos_bundle(table=self.model.mock.galaxy_table,
                                           key1='x',
                                           key2='y',
                                           key3='z')
                w_p = wp(pos, rbins, pi_bins, period=np.array([L, L, L]))
                return [nbar, w_p]
            except ValueError:
                return [10., np.zeros(14)]
        else:
            return [10., np.zeros(14)]
Beispiel #4
0
def run_auto(datfile, randfile, outfile):

    fd = h5py.File(datfile)
    fr = h5py.File(randfile)

    cd = fd["cart_pts"]
    cr = fr["cart_pts"]

    dat = np.empty(cd.shape)
    rands = np.empty(cr.shape)

    dat[:, :] = cd[:, :]
    rands[:, :] = cr[:, :]

    wps = clustering.wp(dat, rbins, pibins, randoms=rands,
                        do_cross=False, estimator='Landy-Szalay',
                        N_threads='max')

    outarr = np.empty((rbin_centers.shape[0], 2))
    outarr[:, 0] = rbin_centers
    outarr[:, 1] = wps

    np.savetxt(outfile, outarr)
def main():
    
    N_threads = 1 #for calculating the TPCFs
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    
    if len(sys.argv)>1:
        catalogue = sys.argv[1]
        sm_low = float(sys.argv[2])
        sm_high = float(sys.argv[3])
    else:
        catalogue = 'sm_9.5_s0.2_sfr_c-1.0_250'
        sm_low=9.5
        sm_high=9.8
    
    if catalogue[-3:]=='250':
        period = [250.0,250.0,250.0]
    if catalogue[-3:]=='125':
        period = [125.0,125.0,125.0]
    else: period = [250.0,250.0,250.0]
    print(period)
    
    filepath_mock = cu.get_output_path() + 'processed_data/campbell_mocks/'
    print('opening mock catalogue:', catalogue+'.hdf5')
    #open catalogue
    f = h5py.File(filepath_mock+catalogue+'.hdf5', 'r') #open catalogue file
    mock = f.get(catalogue)
    print(mock.dtype.names)
    
    #centrals and satellites
    host = np.where(mock['upid']==-1)[0]
    sub = np.where(mock['upid']!=-1)[0]
    host_bool = (mock['upid']==-1)
    sub_bool = (mock['upid']!=-1)
    N_sat = len(sub)
    N_cen = len(host)
    N_gal = len(host)+len(sub)
    print(N_gal, N_cen, N_sat)
    
    #star forming and quenched
    LHS = -11.0
    blue  = (mock['SSFR']>LHS) #indices of blue galaxies
    red   = (mock['SSFR']<LHS) #indicies of red galaxies
    
    #calculate correlation functions
    rp_bins = np.linspace(-2.0,1.4,25)
    rp_bins = 10.0**rp_bins
    rp_bin_centers = (rp_bins[:-1]+rp_bins[1:])/2.0
    
    pi_bins = np.linspace(0,50,50)
    pi_bin_centers = (pi_bins[:-1]+pi_bins[1:])/2.0
    
    #full sample auto correlation
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection = (selection_1 & selection_2 & sub_bool)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    print("number of all galaxies: {0}".format(len(sample1)))
    
    result_all = wp(sample1, rp_bins, pi_bins, period=period,
                    do_auto=True, do_cross=False, estimator='Natural', 
                    N_threads=N_threads, max_sample_size=int(1e7))
    
    #quenched sample auto correlation
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection_1 = (selection_1 & selection_2)
    selection_2 = (red)
    selection = (selection_1 & selection_2 & sub_bool)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    print("number of red galaxies: {0}".format(len(sample1)))
    
    result_q = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads, max_sample_size=int(1e7))
    
    #star-forming sample auto correlation
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection_1 = (selection_1 & selection_2)
    selection_2 = (blue)
    selection = (selection_1 & selection_2 & sub_bool)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    print("number of blue galaxies: {0}".format(len(sample1)))
    
    result_sf = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads, max_sample_size=int(1e7))
    
    #save projected correlation functions
    data_1 = Table([rp_bin_centers,result_all], names=['r', 'wp'])
    data_2 = Table([rp_bin_centers,result_q], names=['r', 'wp'])
    data_3 = Table([rp_bin_centers,result_sf], names=['r', 'wp'])
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    filename_1 = catalogue+'_wp_satellite_all_'+str(sm_low)+'_'+str(sm_high)+'.dat'
    ascii.write(data_1, savepath+filename_1)
    filename_2 = catalogue+'_wp_satellite_q_'+str(sm_low)+'_'+str(sm_high)+'.dat'
    ascii.write(data_2, savepath+filename_2)
    filename_3 = catalogue+'_wp_satellite_sf_'+str(sm_low)+'_'+str(sm_high)+'.dat'
    ascii.write(data_3, savepath+filename_3)
def main():

    #load in fiducial mock
    #all galaxies
    sample='all'
    filepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    sm_bin = '9.5_10.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_all_a = np.array(data['wp'])
    sm_bin = '10.0_10.5'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_all_b = np.array(data['wp'])
    sm_bin = '10.5_11.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_all_c = np.array(data['wp'])
    #quenched galaxies
    sample='q'
    filepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    sm_bin = '9.5_10.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_q_a = np.array(data['wp'])
    sm_bin = '10.0_10.5'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_q_b = np.array(data['wp'])
    sm_bin = '10.5_11.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_q_c = np.array(data['wp'])
    #SF galaxies
    sample='sf'
    filepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    sm_bin = '9.5_10.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_sf_a = np.array(data['wp'])
    sm_bin = '10.0_10.5'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_sf_b = np.array(data['wp'])
    sm_bin = '10.5_11.0'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu_sf_c = np.array(data['wp'])

    mu = np.hstack((mu_all_a, mu_all_b, mu_all_c,mu_q_a, mu_q_b, mu_q_c,mu_sf_a, mu_sf_b, mu_sf_c))
    
    #load covariance matrix
    filepath = './'
    filename = 'big_cov_8_8_8.npy'
    cov = np.matrix(np.load(filepath+filename))
    inv_cov = cov.I
    
    
    #mock parameters
    rhos = np.linspace(0.0,-1.0,25)
    sigmas = np.linspace(0.0,0.3,25)
    dm_sim ='Chinchilla_250'
    box_size=250.0
    period = np.array([box_size]*3)
    sm_lim = 9.5
    
    save_filename_1 = 'chinchilla_rho_3_sigma_2_jacknife_8_8_8_chi_2_array'
    save_filename_2 = 'chinchilla_rho_3_sigma_2_jacknife_8_8_8_uber_array'
    
    #correlation function parameters
    N_threads=4
    
    rp_bins = np.linspace(-1.0,1.4,25)
    rp_bins = 10.0**rp_bins
    rp_bin_centers = (rp_bins[:-1]+rp_bins[1:])/2.0
            
    pi_bins = np.linspace(0,50,50)
    pi_bin_centers = (pi_bins[:-1]+pi_bins[1:])/2.0
    
    X = np.zeros((len(sigmas),len(rhos)))
    uber_result = np.zeros((len(sigmas),len(rhos), 24*3*3))
    for i,sigma in enumerate(sigmas):
        for j,rho in enumerate(rhos):
            print(i,j,sigma, rho)
            
            mock = return_mock(sm_lim = sm_lim, sigma = sigma, rho = rho, sim = dm_sim)
            
            #calculate correlation functions
            #star forming and quenched
            LHS = -11.0
            blue  = (mock['SSFR']>LHS) #indices of blue galaxies
            red   = (mock['SSFR']<LHS) #indicies of red galaxies
            
            sm_low=9.5
            sm_high=10.0
            
            #full sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection = (selection_1 & selection_2)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of all galaxies: {0}".format(len(sample1)))
            
            result_all_a = wp(sample1, rp_bins, pi_bins, period=period,
                    do_auto=True, do_cross=False, estimator='Natural', 
                    N_threads=N_threads,max_sample_size=int(1e7))
            
            #quenched sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (red)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of red galaxies: {0}".format(len(sample1)))
            
            result_q_a = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            #star-forming sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (blue)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of blue galaxies: {0}".format(len(sample1)))
            
            result_sf_a = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            sm_low=10.0
            sm_high=10.5
            
            #full sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection = (selection_1 & selection_2)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of all galaxies: {0}".format(len(sample1)))
            
            result_all_b = wp(sample1, rp_bins, pi_bins, period=period,
                    do_auto=True, do_cross=False, estimator='Natural', 
                    N_threads=N_threads,max_sample_size=int(1e7))
            
            #quenched sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (red)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of red galaxies: {0}".format(len(sample1)))
            
            result_q_b = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            #star-forming sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (blue)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of blue galaxies: {0}".format(len(sample1)))
            
            result_sf_b = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            sm_low=10.5
            sm_high=11.0
            
            #full sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection = (selection_1 & selection_2)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of all galaxies: {0}".format(len(sample1)))
            
            result_all_c = wp(sample1, rp_bins, pi_bins, period=period,
                    do_auto=True, do_cross=False, estimator='Natural', 
                    N_threads=N_threads,max_sample_size=int(1e7))
            
            #quenched sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (red)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of red galaxies: {0}".format(len(sample1)))
            
            result_q_c = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            #star-forming sample auto correlation
            selection_1 = (mock['Mstar']>sm_low)
            selection_2 = (mock['Mstar']<sm_high)
            selection_3 = (blue)
            selection = (selection_1 & selection_2 & selection_3)
            sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
            print("number of blue galaxies: {0}".format(len(sample1)))
            
            result_sf_c = wp(sample1, rp_bins, pi_bins, period=period,
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
            
            result = np.hstack((result_all_a, result_all_b, result_all_c,\
                                result_q_a, result_q_b, result_q_c,\
                                result_sf_a, result_sf_b, result_sf_c))
            
            uber_result[i,j] = result
            
            Y = np.matrix((result-mu))
            
            X_p = Y*inv_cov*Y.T
            X[i,j] = X_p
            
            print(X_p)
    
    np.save('./'+save_filename_1,X)
    np.save('./'+save_filename_2,uber_result)
    
    fig = plt.figure(figsize=(3.3,3.3))
    fig.subplots_adjust(hspace=0, wspace=0, left=0.2, right=0.9, bottom=0.2, top=0.9)
    plt.imshow(X, origin='lower',\
               extent=[sigmas[0],sigmas[-1],rhos[0],rhos[-1]],interpolation='nearest',\
               aspect='auto',cmap='cool')
    plt.plot([0.2],[-0.8],'x',color='black',ms=10)
    plt.xlabel(r'$\sigma_{\rm SMHM}$')
    plt.ylabel(r'$\rho_{\rm SSFR}$')
    plt.colorbar()
    plt.show(block=False)
    fig.savefig('/Users/duncan/Desktop/chi_squared_plot.pdf')
def main():
    
    N_threads = 4 #for calculating the TPCFs
    np.random.seed(0)
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    
    catalogue = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250'
    
    Nran = 10**6 #number of randoms...
    Nsub = np.array([8,8,8]) #jackknife sample splitting
    calculate_RR = False
    RR_filename = 'RR_8_8_8_10e6'
    
    if len(sys.argv)==3:
        sm_low=float(sys.argv[1])
        sm_high=float(sys.argv[2])
    else:
        sm_low=10.5
        sm_high=11.0
        
    print("Running with {} to {} stellar mass bin.".format(sm_low,sm_high))
    
    if catalogue[-3:]=='250':
        period = np.array([250.0,250.0,250.0])
    if catalogue[-3:]=='125':
        period = np.array([125.0,125.0,125.0])
    else: period = np.array([250.0,250.0,250.0])
    print(period)
    
    filepath_mock = cu.get_output_path() + 'processed_data/campbell_mocks/'
    print('opening mock catalogue:', catalogue+'.hdf5')
    #open catalogue
    f = h5py.File(filepath_mock+catalogue+'.hdf5', 'r') #open catalogue file
    mock = f.get(catalogue)
    print(mock.dtype.names)
    
    #centrals and satellites
    host = np.where(mock['upid']==-1)[0]
    sub = np.where(mock['upid']!=-1)[0]
    host_bool = (mock['upid']==-1)
    sub_bool = (mock['upid']!=-1)
    N_sat = len(sub)
    N_cen = len(host)
    N_gal = len(host)+len(sub)
    print(N_gal, N_cen, N_sat)
    
    #star forming and quenched
    LHS = -11.0
    blue  = (mock['SSFR']>LHS) #indices of blue galaxies
    red   = (mock['SSFR']<LHS) #indicies of red galaxies
    
    #calculate correlation functions
    rp_bins = np.linspace(-1.0,1.4,25)
    rp_bins = 10.0**rp_bins
    rp_bin_centers = (rp_bins[:-1]+rp_bins[1:])/2.0
    
    pi_bins = np.linspace(0,50,50)
    pi_bin_centers = (pi_bins[:-1]+pi_bins[1:])/2.0
    
    
    #calculate jackknife samples
    labels = get_subvolume_labels(np.vstack((mock['x'],mock['y'],mock['z'])).T, Nsub,\
                                  np.array([250.0,250.0,250.0]))
    
    #calculate RR once!
    if calculate_RR==True:
        print('calculating RR')
        randoms = np.random.random((Nran,3))*250.0
        random_labels = get_subvolume_labels(randoms, Nsub, np.array([250.0,250.0,250.0]))
        r_selection = (random_labels!=1)
        RR = xy_z_npairs(randoms[r_selection], randoms[r_selection], rp_bins, pi_bins,\
                         period=period, N_threads=N_threads)
        RR = np.diff(np.diff(RR,axis=0),axis=1)
        NR = len(randoms[r_selection])
        np.save('./' + RR_filename,RR)
    else:
        print('loading RR')
        randoms = np.random.random((Nran,3))*250.0
        random_labels = get_subvolume_labels(randoms, Nsub, np.array([250.0,250.0,250.0]))
        r_selection = (random_labels!=1)
        NR = len(randoms[r_selection])
        RR = np.load('./' + RR_filename+'.npy')
    
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection = (selection_1 & selection_2)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    
    result_all = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    result_all[0,:] = wp(sample1, rp_bins, pi_bins, period=period,
                         do_auto=True, do_cross=False, estimator='Natural',
                         N_threads=N_threads,max_sample_size=int(1e7))
    
    #loops over jackknife samples
    for i in range(1,np.max(labels)+1):
        print("all", i)
        #full sample auto correlation
        selection_3 = (labels!=i)
        selection = (selection_1 & selection_2 & selection_3)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        N1 = len(sample1)
        print("number of all galaxies: {0}".format(N1))
    
        DD = xy_z_npairs(sample1, sample1, rp_bins, pi_bins, period=period,\
                         N_threads=N_threads)
        DD = np.diff(np.diff(DD,axis=0),axis=1)
        xi = _TP_estimator(DD,None,RR,N1,N1,NR,NR,'Natural')
        result_all[i,:] = integrate_2D_xi(xi,pi_bins)
    
    
    result_q = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection_3 = (red)
    selection = (selection_1 & selection_2 & selection_3)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    result_q[0,:] = wp(sample1, rp_bins, pi_bins, period=period,
                       do_auto=True, do_cross=False, estimator='Natural', 
                       N_threads=N_threads,max_sample_size=int(1e7))
    
    for i in range(1,np.max(labels)+1):
        print("red", i)
        #quenched sample auto correlation
        selection_4 = (labels!=i)
        selection = (selection_1 & selection_2 & selection_3 & selection_4)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        N1 = len(sample1)
        print("number of red galaxies: {0}".format(N1))
    
        DD = xy_z_npairs(sample1, sample1, rp_bins, pi_bins, period=period,\
                         N_threads=N_threads)
        DD = np.diff(np.diff(DD,axis=0),axis=1)
        xi = _TP_estimator(DD,None,RR,N1,N1,NR,NR,'Natural')
        result_q[i,:] = integrate_2D_xi(xi,pi_bins)
    
    
    result_sf = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection_3 = (blue)
    selection = (selection_1 & selection_2 & selection_3)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
    result_sf[0,:] = wp(sample1, rp_bins, pi_bins, period=period,
                       do_auto=True, do_cross=False, estimator='Natural', 
                       N_threads=N_threads,max_sample_size=int(1e7))
    
    for i in range(1,np.max(labels)+1):
        print("blue", i)
        #star-forming sample auto correlation
        selection_4 = (labels!=i)
        selection = (selection_1 & selection_2 & selection_3 & selection_4)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        N1 = len(sample1)
        print("number of blue galaxies: {0}".format(N1))
    
        DD = xy_z_npairs(sample1, sample1, rp_bins, pi_bins, period=period,\
                         N_threads=N_threads)
        DD = np.diff(np.diff(DD,axis=0),axis=1)
        xi = _TP_estimator(DD,None,RR,N1,N1,NR,NR,'Natural')
        result_sf[i,:] = integrate_2D_xi(xi,pi_bins)
    
    result_cross = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    selection_1 = (mock['Mstar']>sm_low)
    selection_2 = (mock['Mstar']<sm_high)
    selection_3 = (blue)
    selection_a = (selection_1 & selection_2 & selection_3)
    sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection_a]
    selection_3 = (red)
    selection_b = (selection_1 & selection_2 & selection_3)
    sample2 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection_b]
    result_cross[0,:] = wp(sample1, rp_bins, pi_bins, sample2=sample2, period=period,
                           do_auto=False, do_cross=True, estimator='Natural',
                           N_threads=N_threads, max_sample_size=int(1e7))
    
    for i in range(1,np.max(labels)+1):
        print("cross", i)
        #star-forming sample auto correlation
        selection_4 = (labels!=i)
        selection_aa = (selection_a & selection_4)
        selection_bb = (selection_b & selection_4)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection_aa]
        sample2 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection_bb]
        N1 = len(sample1)
        N2 = len(sample2)
        print("number of blue by red galaxies: {0} x {1}".format(N1, N2))
    
        DD = xy_z_npairs(sample1, sample2, rp_bins, pi_bins, period=period,\
                         N_threads=N_threads)
        DD = np.diff(np.diff(DD,axis=0),axis=1)
        xi = _TP_estimator(DD,None,RR,N1,N2,NR,NR,'Natural')
        result_cross[i,:] = integrate_2D_xi(xi,pi_bins)
    
    all_full = result_all[0,:]
    all_sub = result_all[1:,:]
    
    q_full = result_q[0,:]
    q_sub = result_q[1:,:]
    
    sf_full = result_sf[0,:]
    sf_sub = result_sf[1:,:]
    
    cross_full = result_cross[0,:]
    cross_sub = result_cross[1:,:]
    
    #save raw results
    filename_1 = catalogue+'_wp_fiducial_all_'+str(sm_low)+'_'+str(sm_high)
    np.save(filename_1+'_result', result_all)
    filename_2 = catalogue+'_wp_fiducial_q_'+str(sm_low)+'_'+str(sm_high)
    np.save(filename_2+'_result', result_q)
    filename_3 = catalogue+'_wp_fiducial_sf_'+str(sm_low)+'_'+str(sm_high)
    np.save(filename_3+'_result', result_sf)
    filename_4 = catalogue+'_wp_fiducial_cross_'+str(sm_low)+'_'+str(sm_high)
    np.save(filename_4+'_result', result_cross)
    
    #calculate covariance matrices
    cov_all = covariance_matrix(all_sub,all_full,np.max(labels))
    cov_red = covariance_matrix(q_sub,q_full,np.max(labels))
    cov_blue = covariance_matrix(sf_sub,sf_full,np.max(labels))
    cov_cross = covariance_matrix(cross_sub,cross_full,np.max(labels))
    
    #save projected correlation functions
    data_1 = Table([rp_bin_centers,all_full], names=['r', 'wp'])
    data_2 = Table([rp_bin_centers,q_full], names=['r', 'wp'])
    data_3 = Table([rp_bin_centers,sf_full], names=['r', 'wp'])
    data_4 = Table([rp_bin_centers,cross_full], names=['r', 'wp'])
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    
    filename_1 = catalogue+'_wp_fiducial_all_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_1, savepath+filename_1+'.dat')
    np.save(filename_1+'_cov', cov_all)
    
    filename_2 = catalogue+'_wp_fiducial_q_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_2, savepath+filename_2+'.dat')
    np.save(filename_2+'_cov', cov_red)
    
    filename_3 = catalogue+'_wp_fiducial_sf_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_3, savepath+filename_3+'.dat')
    np.save(filename_3+'_cov', cov_blue)
    
    filename_4 = catalogue+'_wp_fiducial_cross_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_4, savepath+filename_4+'.dat')
    np.save(filename_4+'_cov', cov_cross)
    
    plt.figure()
    plt.errorbar(rp_bin_centers, all_full, yerr=np.sqrt(np.diagonal(cov_all)),\
                 color='black')
    plt.errorbar(rp_bin_centers, q_full, yerr=np.sqrt(np.diagonal(cov_red)),\
                 color='red')
    plt.errorbar(rp_bin_centers, sf_full, yerr=np.sqrt(np.diagonal(cov_blue)),\
                 color='blue')
    plt.errorbar(rp_bin_centers, cross_full, yerr=np.sqrt(np.diagonal(cov_cross)),\
                 color='green')
    plt.xscale('log')
    plt.yscale('log')
    plt.show(block=False)
def main():
    
    N_threads = 4 #for calculating the TPCFs
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    
    catalogue = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250'
    sm_low=10.0
    sm_high=10.5
    Nran = 10**6
    Nsub = np.array([2,2,2])
    
    if catalogue[-3:]=='250':
        period = [250.0,250.0,250.0]
    if catalogue[-3:]=='125':
        period = [125.0,125.0,125.0]
    else: period = [250.0,250.0,250.0]
    print(period)
    
    filepath_mock = cu.get_output_path() + 'processed_data/campbell_mocks/'
    print('opening mock catalogue:', catalogue+'.hdf5')
    #open catalogue
    f = h5py.File(filepath_mock+catalogue+'.hdf5', 'r') #open catalogue file
    mock = f.get(catalogue)
    print(mock.dtype.names)
    
    #centrals and satellites
    host = np.where(mock['upid']==-1)[0]
    sub = np.where(mock['upid']!=-1)[0]
    host_bool = (mock['upid']==-1)
    sub_bool = (mock['upid']!=-1)
    N_sat = len(sub)
    N_cen = len(host)
    N_gal = len(host)+len(sub)
    print(N_gal, N_cen, N_sat)
    
    #star forming and quenched
    LHS = -11.0
    blue  = (mock['SSFR']>LHS) #indices of blue galaxies
    red   = (mock['SSFR']<LHS) #indicies of red galaxies
    
    #calculate correlation functions
    rp_bins = np.linspace(-1.0,1.4,25)
    rp_bins = 10.0**rp_bins
    rp_bin_centers = (rp_bins[:-1]+rp_bins[1:])/2.0
    
    pi_bins = np.linspace(0,50,50)
    pi_bin_centers = (pi_bins[:-1]+pi_bins[1:])/2.0
    
    #calculate jackknife samples
    labels = get_subvolume_labels(np.vstack((mock['x'],mock['y'],mock['z'])).T, Nsub, np.array([250.0,250.0,250.0]))
    
    randoms = np.random.random((Nran,3))*250.0
    random_labels = get_subvolume_labels(randoms, Nsub, np.array([250.0,250.0,250.0]))
    
    #loops over jackknife samples
    result_all = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    for i in range(0,np.max(labels)+1):
        print("all", i)
        #full sample auto correlation
        selection_1 = (mock['Mstar']>sm_low)
        selection_2 = (mock['Mstar']<sm_high)
        selection_3 = (labels!=i)
        r_selection = (random_labels!=i)
        selection = (selection_1 & selection_2 & selection_3)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        print("number of all galaxies: {0}".format(len(sample1)))
    
        result_all[i,:] = wp(sample1, rp_bins, pi_bins, period=period, randoms=randoms[r_selection],
                             do_auto=True, do_cross=False, estimator='Natural', 
                             N_threads=N_threads,max_sample_size=int(1e7))
    
    result_q = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    for i in range(0,np.max(labels)+1):
        print("red", i)
        #quenched sample auto correlation
        selection_1 = (mock['Mstar']>sm_low)
        selection_2 = (mock['Mstar']<sm_high)
        selection_3 = (labels!=i)
        selection_1 = (selection_1 & selection_2 & selection_3)
        selection_2 = (red)
        r_selection = (random_labels!=i)
        selection = (selection_1 & selection_2)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        print("number of red galaxies: {0}".format(len(sample1)))
    
        result_q[i,:] = wp(sample1, rp_bins, pi_bins, period=period, randoms=randoms[r_selection],
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
    
    result_sf = np.zeros((np.max(labels)+1,len(rp_bins)-1))
    for i in range(0,np.max(labels)+1):
        print("blue", i)
        #star-forming sample auto correlation
        selection_1 = (mock['Mstar']>sm_low)
        selection_2 = (mock['Mstar']<sm_high)
        selection_3 = (labels!=i)
        selection_1 = (selection_1 & selection_2 & selection_3)
        selection_2 = (blue)
        r_selection = (random_labels!=i)
        selection = (selection_1 & selection_2)
        sample1 = np.vstack((mock['x'],mock['y'],mock['z'])).T[selection]
        print("number of blue galaxies: {0}".format(len(sample1)))
    
        result_sf[i,:] = wp(sample1, rp_bins, pi_bins, period=period, randoms=randoms[r_selection],
                  do_auto=True, do_cross=False, estimator='Natural', 
                  N_threads=N_threads,max_sample_size=int(1e7))
    
    all_full = result_all[0,:]
    all_sub = result_all[1:,:]
    
    q_full = result_q[0,:]
    q_sub = result_q[1:,:]
    
    sf_full = result_sf[0,:]
    sf_sub = result_sf[1:,:]
    
    cov_all = covariance_matrix(all_sub,all_full,np.max(labels))
    cov_red = covariance_matrix(q_sub,q_full,np.max(labels))
    cov_blue = covariance_matrix(sf_sub,sf_full,np.max(labels))
    
    #save projected correlation functions
    data_1 = Table([rp_bin_centers,all_full], names=['r', 'wp'])
    data_2 = Table([rp_bin_centers,q_full], names=['r', 'wp'])
    data_3 = Table([rp_bin_centers,sf_full], names=['r', 'wp'])
    
    savepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    filename_1 = catalogue+'_wp_fiducial_all_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_1, savepath+filename_1+'.dat')
    np.save(filename_1+'_cov', cov_all)
    filename_2 = catalogue+'_wp_fiducial_q_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_2, savepath+filename_2+'.dat')
    np.save(filename_2+'_cov', cov_red)
    filename_3 = catalogue+'_wp_fiducial_sf_'+str(sm_low)+'_'+str(sm_high)
    ascii.write(data_3, savepath+filename_3+'.dat')
    np.save(filename_3+'_cov', cov_blue)
    
    plt.figure()
    plt.errorbar(rp_bin_centers, all_full, yerr=np.sqrt(np.diagonal(cov_all)), color='black')
    plt.errorbar(rp_bin_centers, q_full, yerr=np.sqrt(np.diagonal(cov_red)), color='red')
    plt.errorbar(rp_bin_centers, sf_full, yerr=np.sqrt(np.diagonal(cov_blue)), color='blue')
    plt.xscale('log')
    plt.yscale('log')
    plt.show()