def extract_halo(): # Resimulation mode #mode='2048' mode='1024' base_path = '/media/edoardo/Elements/CLUES/DATA/LGF/' + mode + '/' # Configure the LG model and subpaths if mode == '2048': code_run = cfg.simu_runs() sub_run = cfg.sub_runs() elif mode == '1024': num_run = cfg.gen_runs(0, 80) sub_run = cfg.gen_runs(0, 30) # Read csv list of LGs lgs_csv = 'output/lg_pairs_' + mode + '.csv' df_lgs = pd.read_csv(lgs_csv) print('TotLen: ', len(df_lgs)) x_cols = ['Xc_LG', 'Yc_LG', 'Zc_LG'] x_cols_ahf = ['Xc(6)', 'Yc(7)', 'Zc(8)'] box_center = np.array([5e+4] * 3) r_max = 10000.0 ''' # Do some LG filtering v_max = - 0.0 d_max = 7000.0 df_lgs = df_lgs[df_lgs['Vrad'] < v_max] print('TotLen: ', len(df_lgs)) # Select LGs depending on their distance from the box center ''' #file_ahf = 'snapshot_full_054.z0.000.AHF_halos' #file_ahf = 'snapshot_full_054.z0.001.AHF_halos' file_ahf = 'snapshot_054.z0.000.AHF_halos' # Loop on all the simulations and gather data if mode == '2048': for code in code_run: for sub in sub_run: this_path = base_path + code + '/' + sub + '/' this_ahf = this_path + file_ahf # Check that file exists if os.path.isfile(this_ahf): print('Reading AHF file: ', this_ahf) halo_df = rf.read_ahf_halo(this_ahf, file_mpi=False) halo_select_df = halo_df[halo_df['Mvir(4)'] > m_thresh] out_all_halo_csv = out_base + code + '_' + sub + '.csv' halo_select_df.to_csv(out_all_halo_csv) elif mode == '1024': for ilg, row in df_lgs.iterrows(): num = str('%02d' % int(row['simu_code'])) sub = str('%02d' % int(row['sub_code'])) this_ahf = base_path + num + '_' + sub + '/' + file_ahf this_x = row[x_cols].values #print(this_x) if os.path.isfile(this_ahf): halo_df = rf.read_ahf_halo(this_ahf, file_mpi=True) halo_df['Dist'] = halo_df[x_cols_ahf].T.apply(lambda x: t.distance(x, this_x)) halo_df = halo_df[halo_df['Dist'] < r_max] #print(halo_df.head()) f_out = 'output/LG_' + mode + '/lg_center_' + num + '_' + sub + '.' + str(ilg) + '.csv' print('Saving to: ', f_out) halo_df.to_csv(f_out)
def sub_mass_function(): ''' Compute the subhalo mass functions ''' # Read the | Gyr / z / a | time conversion table time = rf.read_time(data_path) all_halo_mah = [] # Output file base path, save all relevant halo MAHs here out_base_pkl = 'saved/lg_mahs_' # Now loop on all the simulations and gather data for code in code_run[10:11]: this_model = model_run[dict_model[code]] this_model.info(dump=True) for sub in sub_run: this_path = base_path + code + '/' + sub + '/' this_path_mah = base_path_mah + code + '/' + sub + '/' this_ahf = this_path + file_ahf this_mah = this_path_mah + file_mah # Check that file exists if os.path.isfile(this_ahf): out_file_pkl = out_base_pkl + code + '_' + sub + '.pkl' if os.path.isfile(out_file_pkl): print('Loading MAHs from .pkl file: ', out_file_pkl) out_f_pkl = open(out_file_pkl, 'rb') mahs, lg = pkl.load(out_f_pkl) out_f_pkl.close() print('LG properties: ') lg.info() else: print('Reading AHF file: ', this_ahf) halos, halo_df = rf.read_ahf_halo(this_ahf) print('Looking for Local Group candidates...') # The local group model might eventually find more than one pair in the given volume these_lgs = hu.find_lg(halo_df, this_model, cat_center, cat_radius) # SELECT ONE LG print('Found ', len(these_lgs), ' LG candidates with properties: ') try: these_lgs[0].info() lg = these_lgs[0] # Define a gather radius for halos around the LG lg_radius = lg.LG1.r() + lg.LG2.r() + lg.r_halos() lg_radius = 1.2 * lg_radius # TODO there might be more LGs in the area but here I am selecting only one #for this_lg in these_lgs: lg_center = lg.geo_com() id_list = hu.halo_ids_around_center(halos, lg_center, lg_radius) print('Reading MAHs for ', len(id_list), ' halos at ', lg_radius, ' kpc/h around the LG center of mass... ') mahs = rf.read_mah_halo(id_list, this_mah, time) print('Done.') print('Saving MAHs output to file: ', out_file_pkl) out_f_pkl = open(out_file_pkl, 'wb') pkl.dump([mahs, lg], out_f_pkl) out_f_pkl.close() except: print('No LGs found for ', code, sub)
this_lgs = lgs_base + '.csv' print('LGS FILE: ', this_lgs) df_lgs_orig = pd.read_csv(this_lgs) n_lgs_pre = len(df_lgs_orig) print('N pre: ', n_lgs_pre) df_lgs = df_lgs_orig.drop_duplicates(subset=['M_M31']) df_lgs = df_lgs[df_lgs['M_M31'] > m_min] df_lgs = df_lgs[df_lgs['Vrad'] < v_max] print('N post: ', len(df_lgs)) # Read AHF and export it to CSV if mode == 'export_csv': print('AHF FILE: ', this_ahf) df_ahf = rf.read_ahf_halo(this_ahf) df_ahf = t.periodic_boundaries(data=df_ahf, slab_size=r_max, box=box_size) print('AHF FILE TO CSV: ', this_ahf_csv) df_ahf.to_csv(this_ahf_csv) # Read CSV ahf files and extract mass functions around LG candidates elif mode == 'read_csv': print('AHF CSV FILE: ', this_ahf_csv) df_ahf = pd.read_csv(this_ahf_csv) print(df_ahf[x_col_ahf]) # Initialize a string containing all the radii - this will be the dataframe column with the max halo mass radii_str = [] for rad in radii: r_str = 'R_' + str(rad) radii_str.append(r_str)
''' Python Routines for COsmology and Data I/ (PyRCODIO) v0.2 Edoardo Carlesi 2020 [email protected] ahf2csv.py: convert (and compress) AHF halo catalogs to csv files ''' import pandas as pd import sys sys.path.insert(1, '/home/edoardo/CLUES/PyRCODIO/') import read_files as rf this_ahf = sys.argv[1] mpi = sys.argv[2] out_file = this_ahf + '.csv' halo_df = rf.read_ahf_halo(this_ahf, file_mpi=mpi) halo_df.to_csv(out_file)
this_mah = this_path_mah + file_mah # Check that file exists if os.path.isfile(this_ahf): out_file_pkl = out_base_pkl + code + '_' + sub + '.pkl' if os.path.isfile(out_file_pkl): print('Loading MAHs from .pkl file: ', out_file_pkl) out_f_pkl = open(out_file_pkl, 'rb') mahs, lg = pkl.load(out_f_pkl) out_f_pkl.close() print('LG properties: ') lg.info() else: print('Reading AHF file: ', this_ahf) halos, halo_df = rf.read_ahf_halo(this_ahf) print('Looking for Local Group candidates...') # The local group model might eventually find more than one pair in the given volume these_lgs = hu.find_lg(halo_df, this_model, cat_center, cat_radius) # SELECT ONE LG print('Found ', len(these_lgs), ' LG candidates with properties: ') try: these_lgs[0].info() lg = these_lgs[0] # Define a gather radius for halos around the LG lg_radius = lg.LG1.r() + lg.LG2.r() + lg.r_halos()