def get_caesar(self, snap, fname=None, verbose=False): if fname is None: fname = self.sim_extension + '_%s.hdf5' if verbose: print("fname:", fname) fname = self.cs_directory + (fname % snap) return caesar.load(fname)
def read_caesar(c_file, selected_gals): print('Reading caesar file...') sim = caesar.load(c_file, LoadHalo=False) redshift = np.round(sim.simulation.redshift, decimals=2) h = sim.simulation.hubble_constant cosmo = Cosmology(hubble_constant=sim.simulation.hubble_constant, omega_matter=sim.simulation.omega_matter, omega_lambda=sim.simulation.omega_lambda, omega_curvature=0) thubble = cosmo.hubble_time(redshift).in_units("Gyr") H0 = (100 * sim.simulation.hubble_constant) * km / s / Mpc rhocrit = 3. * H0.to('1/s')**2 / (8 * np.pi * sim.simulation.G) mlim = 32 * rhocrit.to('Msun/kpc**3') * sim.simulation.boxsize.to( 'kpc' )**3 * sim.simulation.omega_baryon / sim.simulation.effective_resolution**3 / sim.simulation.scale_factor**3 # galaxy mass resolution limit: 32 gas particle masses gals = np.array([]) # read galaxy particle data for the selected galaxies if isinstance(selected_gals, np.ndarray): gals = np.asarray([ i for i in sim.galaxies if i.GroupID in selected_gals and i.masses['stellar'] > mlim ]) # select resolved galaxies print('Galaxy data from caesar file extracted and saved.') return sim, redshift, h, cosmo, thubble, H0, rhocrit, mlim, gals
def read_caesar(caesarfile): # example of how to read caesar catalog file. this one only reads in M* and SFR, just # to cross-check with Loser file, but Caesar catalogs contain a huge amount of other info sim = caesar.load(caesarfile, LoadHalo=False) z = sim.simulation.redshift ms = np.asarray([i.masses['stellar'] for i in sim.galaxies]) sfr = np.asarray([i.sfr for i in sim.galaxies]) return z, ms, sfr
def get_vol(caesarfile = '/mnt/ceph/users/daisyleung/simba/sim/m50n1024/s50/Groups/m50n1024_036.hdf5'): """ Ran on Rusty, where the caesar file is, and caesar is installed """ import caesar # module purge; module load gcc python3 bubu = caesar.load(caesarfile, LoadHalo=False) volume = bubu.simulation.boxsize.to('Mpccm').d**3 print(volume) return volume
def load_obj_snap(self, idx, redshiftDecimal=2, LoadHalo=False): self.snap = self.snapRange[idx] infile = self.def_caesarFileName() print("Loading Ceasar file: {:}".format(infile)) self.obj = caesar.load(infile, LoadHalo=LoadHalo) self.h = self.obj.simulation.hubble_constant # self.redshift = np.round(self.obj.simulation.redshift, redshiftDecimal) self.redshift = self.obj.simulation.redshift # snap self.snapFile = self.def_snapFileName()
def run_generate_aux_files(caesar_dir, name_prefix, LoadHalo, top=None, savetxt=True, snap=36): """ run info() to save information on galaxies of the snapshot in a .txt """ import caesar infile = caesar_dir + name_prefix + '{:0>3}'.format(int(snap)) + \ '.hdf5' obj = caesar.load(infile, LoadHalo=LoadHalo) snapFile = raw_sim_dir + raw_sim_name_prefix + '{:0>3}'.format(int(snap)) + '.hdf5' output, outName = info(obj, snapFile, top=top, savetxt=savetxt) return output, outName
def get_basic_info_from_caesarCat(snapRange, Nhalo, Ngalaxies, caesar_dir, name_prefix): """ Read the caesar file and get some basic info on num of galaxy in those snapshots """ import caesar for ii, sss in enumerate(snapRange): infile = caesar_dir + name_prefix + '{:0>3}'.format(int(sss)) + \ '.hdf5' print("Loading Ceasar file: {}").format(infile) obj = caesar.load(infile) print('Total number of galaxies found: ' + str(obj.ngalaxies)) Ngal = obj.ngalaxies print('Info on the massive {} halos: '.format(Nhalo)) print(obj.haloinfo(top=Nhalo)) print('Info on the massive {} galaxies across all haloes: '.format( Ngalaxies)) print(obj.galinfo(top=Ngalaxies)) central_galaxy_halo_masses = [ i.halo.masses['total'] for i in obj.galaxies[:Ngalaxies] if i.central ] print('Where {}% are centrals'.format( len(central_galaxy_halo_masses) * 100. / Ngalaxies)) print("The top Ngalaxies most massive galaxies reside in halos ID: ") for ggg in range(Ngalaxies): # all fields: # print(obj.galaxies[ggg].info()) print("Halo ID: {}".format(obj.galaxies[ggg].parent_halo_index)) print( "\nCount the number of galaxies in each massive halo among top Nhalo: " ) for hhh in range(Nhalo): numGal = len(obj.halos[hhh].galaxies) print("Found total of {} galaxies in halo {}".format(numGal, hhh)) return Ngal
def define_ds(snap, raw_sim_dir, raw_sim_name_prefix, caesar_dir, name_prefix, redshiftFile, verbose=False): """ """ import caesar import yt import numpy as np import os import pygad as pg _, zs_table, snaps_table = np.loadtxt(redshiftFile, unpack=True) infile = caesar_dir + name_prefix + '{:0>3}'.format(int(snap)) + \ '.hdf5' print("Loading Ceasar file: {}").format(infile) obj = caesar.load(infile) obj.galaxies.sort(key=lambda x: x.sfr, reverse=True) rawSim = raw_sim_dir + raw_sim_name_prefix + \ '{:>03}'.format(int(snap)) + '.hdf5' ds = yt.load(rawSim, over_refine_factor=1, index_ptype="all") s = pg.Snap(raw_sim_dir + raw_sim_name_prefix + '{:0>3}'.format(int(snap)) + '.hdf5') h = s.cosmology.h() # obj.simulation.hubble_constant if verbose: print_ds_info(h, obj, ds) zred = '{:.3f}'.format(zs_table[snaps_table == snap][0]) return s, h, obj, ds, float(zred)
def halom(sn, rep ='./', indm = 0, anaf1 = hmf1, anaf0 = hmf0, mlow = 1e6, fac = fac0, edge = [0.0, 99.5], nbin = 15, boxs = 4.0 ,base = 'snapshot', ext = '.hdf5', post = 'caesar'): ds = yt.load(rep+base+'_'+str(sn).zfill(3)+ext) z = ds['Redshift']#1/Redf[sn]-1 obj = caesar.load(rep+post+'_'+str(sn).zfill(3)+ext) lh = obj.halos #a = 1/(ds['Redshift']+1) lm0 = np.array([x.masses['total'] for x in lh if x.masses['total']>=mlow]) lm = np.array([(1e10*x.virial_quantities['circular_velocity']**2*x.radii['virial'].to('kpc/h')/G) for x in lh if x.masses['total']>=mlow]) rm0 = np.percentile(np.log10(lm0), edge) rm = np.percentile(np.log10(lm), edge) his1, ed1 = np.histogram(np.log10(lm0), nbin, rm0) b1 = (ed1[1:]+ed1[:-1])/2 bs1 = ed1[1]-ed1[0] his2, ed2 = np.histogram(np.log10(lm), nbin, rm) b2 = (ed2[1:]+ed2[:-1])/2 bs2 = ed2[1]-ed2[0] anaf1.update(z=z) lm1 = np.log10(anaf1.m) ln1 = np.log10(anaf1.dndlog10m) mf_ana1 = interp1d(lm1, ln1) anaf0.update(z=z) lm0 = np.log10(anaf0.m) ln0 = np.log10(anaf0.dndlog10m) mf_ana0 = interp1d(lm0, ln0) plt.figure() plt.errorbar(b1, his1/boxs**3/bs1/fac, yerr=his1**0.5/boxs**3/bs1/fac, label='total mass') plt.errorbar(b2, his2/boxs**3/bs2/fac, yerr=his2**0.5/boxs**3/bs2/fac, ls='--', label='virial mass') plt.plot(b2, 10**mf_ana1(b2),'-.', label='Sheth-Mo-Tormen (WDM)') plt.plot(b2, 10**mf_ana0(b2),':', label='Sheth-Mo-Tormen (CDM)') plt.legend() plt.yscale('log') plt.title(r'Halo mass function for '+lmodel[indm]+' at $z=$'+str(int(z*100)/100),size=14) plt.xlabel(r'$\log(M\ [h^{-1}M_{\odot}])$') plt.ylabel(r'$\frac{dn}{d\log(M)}\ [h^{3}\mathrm{Mpc^{-3}}]$') plt.tight_layout() plt.savefig(rep+'hmass_'+lmodel[indm]+'_'+str(sn)+'.pdf') #plt.show() return lm
def drive(snapdirs, snapname, snapnums, progen=False, progen_rad = False, skipran=False, member_search=True, extension='hdf5', **kwargs): """Driver function for running ``CAESAR`` on multiple snapshots. Can utilize mpi4py to run analysis in parallel given that ``MPI`` and ``mpi4py`` is correctly installed. To do this you must create a script similar to the example below, then execute it via: >>> mpirun -np 8 python my_script.py Parameters ---------- snapdirs : str or list A path to your snapshot directory, or a list of paths to your snapshot directories. snapname : str Formatting of your snapshot name disregarding any integer numbers or file extensions; for example: ``snap_N256L16_`` snapnums : int or list or array A single integer, a list of integers, or an array of integers. These are the snapshot numbers you would like to run CAESAR on. progen : boolean, optional Perform most massive progenitor search. Defaults to False. skipran : boolean, optional Skip running member_search() if CAESAR outputs are already present. Defaults to False. member_search : boolean, optional Perform the member_search() method on each snapshot. Defaults to True. This is useful to set to False if you want to just perform progen for instance. extension : str, optional Specify your snapshot file extension. Defaults to `hdf5` unbind_halos : boolean, optional Unbind halos? Defaults to False unbind_galaxies : boolean, optional Unbind galaxies? Defaults to False b_halo : float, optional Quantity used in the linking length (LL) for halos. LL = mean_interparticle_separation * b_halo. Defaults to ``b_halo = 0.2``. b_galaxy : float, optional Quantity used in the linking length (LL) for galaxies. LL = mean_interparticle_separation * b_galaxy. Defaults to ``b_galaxy = b_halo * 0.2``. ll_cloud: float, optional Linking length in comoving kpc (kpccm_ for clouds. Defaults to same linking length as used for galaxies. fofclouds: boolean, optional Sets whether or not we run 3D FOF for clouds. Default is that this is not run as this isn't the typical use case for Caesar, and slows things down a bit fof6d: boolean, optional Sets whether or not we do 6D FOF for galaxies. if not set, the default is to do normal 3D FOF for galaxies. fof6d_LL_factor: float, optional Sets linking length for fof6d fof6d_mingrp: float, optional Sets minimum group size for fof6d fof6d_velLL: float, optional Sets linking length for velocity in fof6d nproc: int, optional Sets number of processors for fof6d and progen_rad blackholes : boolean, optional Indicate if blackholes are present in your simulation. This must be toggled on manually as there is no clear cut way to determine if PartType5 is a low-res particle, or a black hole. lowres : list, optional If you are running ``CAESAR`` on a Gadget/GIZMO zoom simulation in HDF5 format, you may want to check each halo for low-resolution contamination. By passing in a list of particle types (ex. [2,3,5]) we will check ALL objects for contamination and add the ``contamination`` attribute to all objects. Search distance defaults to 2.5x radii['total']. Examples -------- >>> import numpy as np >>> snapdir = '/Users/bob/Research/N256L16/some_sim' >>> snapname = 'snap_N256L16_' >>> snapnums = np.arange(0,86) >>> >>> import caesar >>> caesar.drive(snapdir, snapname, snapnums, skipran=False, progen=True) """ if isinstance(snapdirs, str): snapdirs = [snapdirs] if isinstance(snapnums, int): snapnums = [int] using_mpi = False try: from mpi4py import MPI comm = MPI.COMM_WORLD nprocs = comm.Get_size() rank = comm.Get_rank() using_mpi = True except: nprocs = 1 rank = 0 if rank == 0: print_art() snaps = [] for snapdir in snapdirs: for snapnum in snapnums: snaps.append(Snapshot(snapdir, snapname, snapnum, extension)) if member_search: rank_snaps = snaps[rank::nprocs] for snap in rank_snaps: snap.member_search(skipran, **kwargs) if (progen == True) and (progen_rad == True): sys.exit('You can only set progen or progen_rad as True; exiting') if progen or progen_rad: if using_mpi: comm.Barrier() verified_snaps = [] missing_snaps = [] for snap in snaps: if not hasattr(snap, 'outfile'): ds = yt.load(snap.snap) snap.set_output_information(ds) if os.path.isfile(snap.outfile): verified_snaps.append(snap) else: missing_snaps.append(snap) if len(missing_snaps) > 0: mylog.warning('Missing the following CAESAR files:') for snap in missing_snaps: mylog.warning(snap.outfile) progen_pairs = [] for i in reversed(range(1,len(verified_snaps))): progen_pairs.append((verified_snaps[i],verified_snaps[i-1])) rank_progen_pairs = progen_pairs[rank::nprocs] for progen_pair in rank_progen_pairs: snap_current = progen_pair[0] snap_progens = progen_pair[1] ds_current = yt.load(snap_current.snap) ds_progens = yt.load(snap_progens.snap) snap_current.set_output_information(ds_current) snap_progens.set_output_information(ds_progens) obj_current = caesar.load(snap_current.outfile) obj_progens = caesar.load(snap_progens.outfile) if progen: progen_finder(obj_current, obj_progens, snap_current.outfile, snap_dir = snap_current.snapdir) progen_finder(obj_progens, obj_current, snap_progens.outfile, snap_dir = snap_current.snapdir) if progen_rad == True: #temporary catch to kill off any sims trying to do progen_Rad with clouds till we fix that if 'fofclouds' in kwargs: raise Exception('Cannot call fofclouds and progen_rad - exiting now') else: run_progen_rad(obj_current,obj_progens,snap_current,snap_progens)
import h5py import caesar import argparse parser = argparse.ArgumentParser() parser.add_argument('ds') parser.add_argument('caesar') parser.add_argument('galaxy', type=int) parser.add_argument('output') args = parser.parse_args() obj = caesar.load(args.caesar, LoadHalo=0) glist = obj.galaxies[args.galaxy].glist slist = obj.galaxies[args.galaxy].slist #bhlist = obj.galaxies[args.galaxy].bhlist with h5py.File(args.ds, 'r') as input_file, h5py.File(args.output, 'w') as output_file: output_file.copy(input_file['Header'], 'Header') output_file.create_group('PartType0') for k in input_file['PartType0']: output_file['PartType0'][k] = input_file['PartType0'][k][:][glist] output_file.create_group('PartType4') for k in input_file['PartType4']: output_file['PartType4'][k] = input_file['PartType4'][k][:][slist] #output_file.create_group('PartType5') #for k in input_file['PartType5']:
import numpy as np import tqdm ########### # Line arguments ########### snapshot_path = sys.argv[1] snap_num = sys.argv[2] galaxy = sys.argv[3] output_path = sys.argv[4] ############## ds = snapshot_path caesar_file = glob.glob(snapshot_path'/output/Groups/caesar_0'+"{:03d}".format(snap_num)+'*.hdf5') obj = caesar.load(caesar_file[0]) #here, you can index through a list of selected galaxies, or just use the input galaxy #galaxy_num = pd.read_csv('/read/some/selection/file)[int(galaxy)] glist = obj.galaxies[int(galaxy)].glist slist = obj.galaxies[int(galaxy)].slist with h5py.File(ds+'/snapshot_0'+str(snap_num)+'.hdf5', 'r') as input_file, h5py.File(output_path+'galaxy_'+str(galaxy)+'.hdf5', 'w') as output_file: output_file.copy(input_file['Header'], 'Header') print('starting with gas attributes now') output_file.create_group('PartType0') for k in tqdm.tqdm(input_file['PartType0']): output_file['PartType0'][k] = input_file['PartType0'][k][:][glist] print('moving to star attributes now')
MEMBERS = np.sort( glob('%s/caesar*.hdf5' % (snapshot_directory + '/Groups_oldprogen/'))) try: LASTSNAP = int(MEMBERS[-1][-16:-12]) except ValueError: LASTSNAP = int(MEMBERS[-1][-8:-5]) g_data = {} h_data = {} snaplist = [] for fileidx, file in enumerate(MEMBERS[::-1][0:270]): print('loading %s' % file) obj = caesar.load(file) snapshotname = obj.simulation.basename g_mstar = [gal.masses['stellar'] for gal in obj.galaxies] g_sfr = [gal.sfr for gal in obj.galaxies] redshift = obj.simulation.redshift g_mgas = [gal.masses['gas'] for gal in obj.galaxies] g_mgas_H2 = [gal.masses['H2'] for gal in obj.galaxies] g_mgas_HI = [gal.masses['HI'] for gal in obj.galaxies] g_mdust = [gal.masses['dust'] for gal in obj.galaxies] try: g_progen_index = [gal.progen_index for gal in obj.galaxies] except AttributeError: g_progen_index = -1 parent_halo_index = [gal.parent_halo_index for gal in obj.galaxies]
import caesar import numpy as np import pprint import argparse parser = argparse.ArgumentParser() parser.add_argument('caesar_file') args = parser.parse_args() obj = caesar.load(args.caesar_file) qobj = caesar.quick_load(args.caesar_file) assert len(obj.clouds) == len(qobj.clouds) for cloud, qcloud in zip(obj.clouds, qobj.clouds): for k, v in cloud.__dict__.items(): if k[0] != '_' and k not in ['obj']: if isinstance(getattr(cloud, k), np.ndarray): if np.any(getattr(cloud, k) != getattr(qcloud, k)): print(k) pprint.pprint(getattr(cloud, k)) pprint.pprint(getattr(qcloud, k)) print() else: if getattr(cloud, k) != getattr(qcloud, k): print(k) print(getattr(cloud, k)) print(getattr(qcloud, k)) print() assert len(obj.galaxies) == len(qobj.galaxies) for galaxy, qgalaxy in zip(obj.galaxies, qobj.galaxies):
import caesar import h5py if __name__ == '__main__': survey = sys.argv[1] model = sys.argv[2] wind = sys.argv[3] if survey == 'dwarfs': snap = '151' elif survey == 'halos': snap = '137' infile = f'/home/rad/data/{model}/{wind}/Groups/{model}_{snap}.hdf5' sim = caesar.load(infile) sample_file = f'/disk01/sapple/cgm/absorption/cos_comparison/cos_samples/{model}/cos_{survey}/samples/{model}_{wind}_cos_{survey}_sample.h5' with h5py.File(sample_file, 'r') as f: gal_ids = np.array(f['gal_ids'][:]) halo_r200_sample = np.ones(len(gal_ids)) * np.nan halo_mass_sample = np.ones(len(gal_ids)) * np.nan halo_r200_sample[~np.isnan(gal_ids)] = \ np.array([sim.galaxies[int(i)].halo.virial_quantities['r200c'].in_units('kpc/h') for i in gal_ids if ~np.isnan(i)]) halo_mass_sample[~np.isnan(gal_ids)] = \ np.log10(np.array([sim.galaxies[int(i)].halo.masses['total'].in_units('Msun') for i in gal_ids if ~np.isnan(i)])) with h5py.File(sample_file, 'a') as f: del f['halo_r200']
profs_file = basic_dir + model + '_' + snap + '/all_profiles_' + angle + '.h5' results_dir = '/home/sapple/simba_sizes/profiles/paper/plotting/data/' + selection results_dir += selection if angle == 'random_orientation': results_dir += '_rand' elif angle == 'rotated_faceon': results_dir += '_rot' mass_bins = [10., 10.5, 11.0] bin_labels = ['10.0-10.5', '10.5-11.0', '>11.0'] mass_bins = [10., 10.6] bin_labels = ['10.0-10.6', '>10.6'] caesar_dir = '/home/rad/data/' + model + '/' + wind + '/Groups/' sim = caesar.load(caesar_dir + model + '_' + snap + '.hdf5', LoadHalo=False) gal_cent = np.array([i.central for i in sim.galaxies]) gal_sm = np.log10( np.array([i.masses['stellar'].in_units('Msun') for i in sim.galaxies])) gal_ids = np.array([False for i in range(len(sim.galaxies))]) gal_ids[gals] = True gal_pos = np.array([i.pos.in_units('kpc/h') for i in sim.galaxies]) boxsize = sim.simulation.boxsize.in_units('kpc/h') for i, b in enumerate(bin_labels): if i != len(mass_bins) - 1: mass_mask = (gal_sm > mass_bins[i]) & (gal_sm < mass_bins[i + 1]) else: mass_mask = gal_sm > mass_bins[i]
import json import numpy as np import yt import caesar from caesar.pyloser import pyloser from caesar.cyloser import compute_AV # _dir = '/cosma7/data/dp104/dc-dave2/sim/m100n1024/s50j7k/' _dir = '/orange/narayanan/desika.narayanan/gizmo_runs/simba/m100n1024/' cs = caesar.load(_dir + 'Groups/m100n1024_078.hdf5') _dat = json.load(open('m100/galaxy_selection.json', 'r')) halos = [cs.halos[n['hidx']] for k, n in _dat['078'].items()] # subset_dir='/blue/narayanan/c.lovell/simba/m100n1024/out/snap_078/' # ds = yt.load(subset_dir+'subset_00000.h5') ds = yt.load(_dir + 'snap_m100n1024_078.hdf5') phot = pyloser.photometry(cs, [halos[0]], ds=ds) phot.ssp_table_file = \ '/home/c.lovell/codes/caesar/FSPS_Chab_EL.hdf5' # '/cosma7/data/dp004/dc-love2/codes/caesar/FSPS_Chab_EL.hdf5' phot.init_pyloser() def transform_coods(theta, phi, tol=20, test=False): alpha = np.arcsin(-1 * np.round(np.sin(theta), tol) * np.round(np.sin(phi), tol))
return turnover_SFR colors = ('b', 'g', 'm', 'c', 'k') # simName = ['m25n256', 'm25n1024', 'm50n512', 'm50n1024', 'm100n1024'] simName = ['m25n1024', 'm50n1024', 'm100n1024'] labels = ['Simba-25', 'Simba-50', 'Simba-100'] sims = [] for ii, nn in enumerate(simName): if nn == 'm25n1024': caesarfile = '/mnt/ceph/users/daisyleung/simba/sim/' + nn + '/s50_new/Groups/' + nn + '_036.hdf5' else: caesarfile = '/mnt/ceph/users/daisyleung/simba/sim/' + nn + '/s50/Groups/' + nn + '_036.hdf5' sims.append(caesar.load(caesarfile, LoadHalo=False)) ncol = 1 nrow = 1 fig, ax = plt.subplots() TYPES = ['SFR'] turnover_SFR = massFunc(sims, labels, ax, ii, addvlineSFRLimit=True) print(np.log10(turnover_SFR)) plt.minorticks_on() plt.xlabel(r'$\log$ SFR $[M_\odot$ yr$^{-1}]$', fontsize=18) plt.ylabel(r'$\log \Phi$ [Mpc$^{-3}$]', fontsize=18) plt.ylim(-6.5, 0.7) plt.xlim(-3.2, 3) plt.subplots_adjust(hspace=.0)
import sys MODEL = sys.argv[1] SNAP = int(sys.argv[2]) WIND = sys.argv[3] band = 'sdss_r' if __name__ == '__main__': snapfile = '/home/rad/data/%s/%s/snap_%s_%03d.hdf5' % (MODEL, WIND, MODEL, SNAP) ds = yt.load(snapfile) caesarfile = '/home/rad/data/%s/%s/Groups/%s_%03d.hdf5' % (MODEL, WIND, MODEL, SNAP) sim = caesar.load(caesarfile) # load caesar file redshift = np.round(sim.simulation.redshift, decimals=2) # get caesar info for galaxies myobjs = sim.galaxies mag = np.asarray([m.absmag[band] for m in myobjs]) # load desired mags print('%s mag original: %g' % (band, mag[0])) ms = np.log10(np.asarray([i.masses['stellar'] for i in myobjs])) mlim = np.log10(32 * sim.simulation.critical_density.value * sim.simulation.boxsize.value**3 * sim.simulation.omega_baryon / sim.simulation.effective_resolution** 3) # galaxy mass resolution limit: 32 gas particle masses sfr = np.asarray([i.sfr for i in myobjs]) ssfr = np.log10( 1.e9 * sfr / ms +
a radial profile and fits a Sersic index for each galaxy. """ from projection import * import numpy as np import caesar from readgadget import readsnap import matplotlib.pyplot as plt from scipy.optimize import curve_fit model = 'm50n512' snap_no = '078' data_dir = '/home/rad/data/'+model+'/s50j7k/' results_dir = '/home/sapple/simba_pretty/quick_projection/'+model+'_'+snap_no+'/galaxies/' obj = caesar.load(data_dir+'Groups/'+model+'_'+snap_no+'.hdf5') snap = data_dir + 'snap_'+model+'_'+snap_no+'.hdf5' h = 0.68 # hubble constant xmin = -20. # [kpc] xmax = 20. # [kpc] Npixels = 100 #512 DR = 1. # kpc z = obj.simulation.redshift masses = np.array([i.masses['stellar'] for i in obj.galaxies]) sfr = np.array([i.sfr for i in obj.galaxies]) mask_mass = (masses > 4e10) & (masses < 6e10) mask_sfr = (sfr > 0.5) & (sfr < 2.5)
model = 'm50n512' wind1 = 's50j7k' wind_options = ['s50nox', 's50nojet', 's50nofb'] snap = '151' survey = 'dwarfs' # ignore these as the main s50 sample has insufficient galaxies ignore_simba_gals, ngals_each = get_ignore_simba_gals(model, survey) sample_file = './m50n512/cos_' + survey + '/samples/' + model + '_' + wind1 + '_cos_' + survey + '_sample.h5' with h5py.File(sample_file, 'r') as f: gal_ids = np.array(f['gal_ids'][:], dtype='int') cos_ids = f['cos_ids'][:] infile = '/home/rad/data/' + model + '/' + wind1 + '/Groups/' + model + '_' + snap + '.hdf5' obj1 = caesar.load(infile) for wind2 in wind_options: match_file = './m50n512/match_halos_' + snap + '.hdf5' with h5py.File(match_file, 'r') as f: prog_index = f[wind1 + '_' + wind2][:] infile = '/home/rad/data/' + model + '/' + wind2 + '/Groups/' + model + '_' + snap + '.hdf5' obj2 = caesar.load(infile) new_gal_ids = np.array([np.nan] * len(gal_ids)) for i, gal_id in enumerate(gal_ids): if not i in ignore_simba_gals: new_gal_ids[i] = check_halo_sample(prog_index, obj1, obj2,
def run_progen(snapdirs, snapname, snapnums, prefix='caesar_', suffix='hdf5', **kwargs): """Function to run progenitor/descendant finder in specified snapshots (or redshifts) in a given directory. Parameters ---------- snapdirs : str or list of str Full path of directory(s) where snapshots are located snapname : str Formatting of snapshot name excluding any integer numbers or file extensions; e.g. 'snap_N256L16_' snapnums : int or list of int Snapshot numbers over which to run progen. Increasing order -> descendants; Decreasing -> progens. prefix : str Prefix for caesar filename; assumes these are in 'Groups' subdir suffix : str Filetype suffix for caesar filename kwargs : Passed to progen_finder() """ from caesar.driver import Snapshot # Find existing snapshots in snapdirs if isinstance(snapdirs, str): snapdirs = [snapdirs] if isinstance(snapnums, int): snapnums = [int] snaps = [] for snapdir in snapdirs: for snapnum in snapnums: snaps.append(Snapshot(snapdir, snapname, snapnum, suffix)) verified_snaps = [] missing_snaps = [] missing = '' for isnap,snap in enumerate(snaps): fullname = snap.snapdir + '/' + snap.snapname + '%.03d'%snap.snapnum + '.' + suffix if not os.path.isfile(fullname): missing_snaps.append(snap) missing = missing+'%d '%(snapnums[isnap]) continue snap.outfile = caesar_filename(snap,prefix,suffix) if not os.path.isfile(snap.outfile): missing_snaps.append(snap) missing = missing+'%d '%(snapnums[isnap]) continue f = h5py.File(snap.outfile,'r') if not '/halo_data' in f: missing_snaps.append(snap) missing = missing+'%d '%(snapnums[isnap]) f.close() continue verified_snaps.append(snap) f.close() if len(missing_snaps) > 0: mylog.warning('Missing snapshot/caesar file, or no halo_data for: %s'%missing) # Collect pairs of snapshot names over which to run progen progen_pairs = [] for i in range(0,len(verified_snaps)-1): progen_pairs.append((verified_snaps[i],verified_snaps[i+1])) # Loop over pairs, find progens for progen_pair in progen_pairs: snap_current = progen_pair[0] snap_progens = progen_pair[1] if snap_current.snapnum < snap_progens.snapnum: mylog.info('Progen: Finding descendants of snap %d in snap %d'%(snap_current.snapnum,snap_progens.snapnum)) else: mylog.info('Progen: Finding progenitors of snap %d in snap %d'%(snap_current.snapnum,snap_progens.snapnum)) obj_current = caesar.load(caesar_filename(snap_current,prefix,suffix)) obj_progens = caesar.load(caesar_filename(snap_progens,prefix,suffix)) progen_finder(obj_current, obj_progens, caesar_filename(snap_current,prefix,suffix), snap_dir=snapdirs[0], **kwargs)
log_dfrad = 0.5 log_frad = np.arange(log_frad_min, log_frad_max + log_dfrad, log_dfrad) sample_dir = f'/disk04/sapple/cgm/absorption/ml_project/data/samples/' snapfile = f'{sample_dir}{model}_{wind}_{snap}.hdf5' gas_hsml = readsnap(snapfile, 'SmoothingLength', 'gas', suppress=1, units=1) # in kpc/h, comoving gas_pos = readsnap(snapfile, 'pos', 'gas', suppress=1, units=1) # in kpc/h, comoving data_dir = f'/home/rad/data/{model}/{wind}/' sim = caesar.load(f'{data_dir}Groups/{model}_{snap}.hdf5') h = sim.simulation.hubble_constant redshift = sim.simulation.redshift sat_mask = ~np.array([i.central for i in sim.galaxies]) sat_ids = np.arange(len(sim.galaxies))[sat_mask] partids = {} for lf in log_frad: partids[f'log_frad_{lf}'] = np.array([]) i = sat_ids[num] with h5py.File( f'{sample_dir}{model}_{wind}_{snap}_satellite_only_{lf}log_frad.h5',
def select_SFgal_from_simba(raw_sim_dir, raw_sim_name_prefix, caesar_dir, name_prefix, snapRange, Ngalaxies, saveggg=None, verbose=False, debug=False): ''' pick out the 'Ngalaxies' most star-forming galaxies across snapshots 'snapRange' Parameters ---------- raw_sim_dir: string where to look for raw snapshot files raw_sim_name_prefix: string prefix to the speciifc volume and resolution of snapshots we are looking caesar_dir: str path to the caesar output files name_prefix: str predix to file of caesar outputs snapRange: list of int snapshots to look for galaxies Ngalaxies: int how many galaxies from each snapshot across all halos do we want as output saveggg: str filename to save output "galnames_selected" Returns ------- galnames_selected: list of string galaxies names indicating the halo ID, snapshot number, and galaxy ID (numbered based on some predefined criterion) ''' import caesar import numpy as np import pandas as pd # store gal info for large number of galaxies, which we then select from galnames = pd.DataFrame({'halo': [], 'snap': [], 'GAL': [], 'SFR': []}) # loop thru snapshots for ii, sss in enumerate(snapRange): infile = caesar_dir + name_prefix + '{:0>3}'.format(int(sss)) + \ '.hdf5' print("Loading Ceasar file: {}").format(infile) obj = caesar.load(infile) # # LoadHalo=False print( "\nSelecing {} Galaxies with the highest SFRs across all halos in this snapshot, but you may want to galaxies based on different criteria." ).format(Ngalaxies) obj.galaxies.sort(key=lambda x: x.sfr, reverse=True) if verbose: print(obj.galinfo(top=Ngalaxies)) for GAL in range(Ngalaxies): try: SFR = float(obj.galaxies[GAL].sfr.d) add_this = pd.DataFrame({ 'halo': [int(obj.galaxies[GAL].parent_halo_index)], 'snap': [sss], 'GAL': [GAL], 'SFR': [SFR] }) galnames = galnames.append(add_this, ignore_index=True) galnames[['halo', 'snap', 'GAL']] = galnames[['halo', 'snap', 'GAL']].astype(int) except: break print( "\nSelecing {} Galaxies with the highest SFRs across snapshot 'snapRange', but you may want to galaxies based on different criteria.\nIf so, edit define_ds()" ).format(Ngalaxies) galnames = galnames.sort_values(['SFR'], ascending=False).reset_index(drop=True) if debug: print(galnames) print( "Note to self: Remember to change variable global_save_files in param.py so that naming is consistent with Ngalaxies we are picking here" ) galnames = galnames[:Ngalaxies] print(galnames) if saveggg is not None: with open(saveggg, 'w') as f: pickle.dump([ggg], f) return galnames
def tage_init(cosmo, redshift): thubble = cosmo.age(redshift).value lz1 = np.arange(np.log10(1 + redshift), np.log10(51), 0.0001) tlz1 = thubble - cosmo.age(10**lz1 - 1.).value return lz1, tlz1 # load in input file #fig,ax = plt.subplots() for SNAP in snaps: for iwind in range(0, len(WIND)): snapfile = '/home/rad/data/' + MODEL + '/' + WIND[ iwind] + '/snap_' + MODEL + '_' + SNAP + '.hdf5' infile = '/home/rad/data/' + MODEL + '/' + WIND[ iwind] + '/Groups/' + MODEL + '_' + SNAP + '.hdf5' sim = caesar.load(infile, LoadHalo=False) redshift = np.round(sim.simulation.redshift, 3) h = sim.simulation.hubble_constant mygals = sim.galaxies print('Doing z=', redshift, 'snapshot', snapfile, 'with', len(mygals), 'galaxies.') # read in galaxy info ids = np.asarray([i.GroupID for i in mygals]) central = np.asarray([i.central for i in mygals]) ms = np.asarray([i.masses['stellar'] for i in mygals]) mbh = np.asarray([i.masses['bh'] for i in mygals]) sfr = np.asarray([i.sfr for i in mygals]) # read in particle info
print('Ignoring certain COS galaxies') import sys sys.exit() if survey == 'dwarfs': from get_cos_info import get_cos_dwarfs cos_rho, cos_M, cos_r200, cos_ssfr = get_cos_dwarfs() snap = '151' elif survey == 'halos': from get_cos_info import get_cos_halos cos_rho, cos_M, cos_r200, cos_ssfr = get_cos_halos() snap = '137' data_dir = '/home/rad/data/'+model+'/'+wind+'/' sim = caesar.load(data_dir+'Groups/'+model+'_'+snap+'.hdf5') gal_pos = np.array([i.pos.in_units('kpc/h') for i in sim.galaxies]) # in kpc/h h = sim.simulation.hubble_constant redshift = sim.simulation.redshift snapfile = data_dir + 'snap_'+model+'_'+snap +'.hdf5' # need PartType0 - SmoothingLength hsml = readsnap(snapfile, 'SmoothingLength', 'gas', suppress=1, units=1) # in kpc/h, comoving gas_pos = readsnap(snapfile, 'pos', 'gas', suppress=1, units=1) # in kpc/h, comoving sample_dir = '/disk01/sapple/cgm/absorption/cos_comparison/cos_samples/'+model+'/cos_'+survey+'/samples/' sample_file = sample_dir+model+'_'+wind+'_cos_'+survey+'_sample.h5' with h5py.File(sample_file, 'r') as f: gal_id = f['gal_ids'][:].astype('int')[sample_gal] pos = f['position'][:][sample_gal] * (1.+redshift) # already in kpc/h, factor of 1+z for comoving
model = 'm100n1024' wind = 's50j7k' snap = '078' h5_dir = '/home/sapple/simba_sizes/profiles/gv_sample/high_redshift/' if not os.path.exists(h5_dir): os.makedirs(h5_dir) plots_dir = h5_dir +model+'_'+snap+'/' if not os.path.exists(plots_dir): os.makedirs(plots_dir) data_dir = '/home/rad/data/'+model+'/'+wind+'/' sim = caesar.load(data_dir+'Groups/'+model+'_'+snap+'.hdf5', LoadHalo=False) gal_cent = np.array([i.central for i in sim.galaxies]) gal_sm = np.array([i.masses['stellar'].in_units('Msun') for i in sim.galaxies]) gal_sfr = np.array([i.sfr.in_units('Msun/yr') for i in sim.galaxies]) gal_ssfr = np.log10(gal_sfr / gal_sm) gal_sm = np.log10(gal_sm) gal_sfr = np.log10(gal_sfr) sf_mask = gal_ssfr > ssfr_lim with h5py.File(h5_dir+'sf_samples.h5', 'a') as f: f.create_dataset(model+'_'+snap, data=np.array(sf_mask)) plt.plot(masses, line, ls='--', lw=1.5, c='m', label=r'$\textrm{sSFR} = 10^{-9.5} \textrm{yr}^{-1}$') plt.axvline(10., ls='--', lw=1.5, c='k')
snap = '151' elif survey == 'halos': from get_cos_info import get_cos_halos cos_rho, cos_M, cos_r200, cos_ssfr = get_cos_halos() snap = '137' cos_ids = np.arange(len(cos_M))[cos_M > mlim] cos_rho = cos_rho[cos_M > mlim] cos_ssfr = cos_ssfr[cos_M > mlim] cos_r200 = cos_r200[cos_M > mlim] cos_M = cos_M[cos_M > mlim] numgals = len(cos_M) print('Loaded COS-Dwarfs survey data') infile = '/home/rad/data/' + model + '/' + wind + '/Groups/' + model + '_' + snap + '.hdf5' sim = caesar.load(infile) gal_cent = np.array([i.central for i in sim.galaxies]) co = yt.utilities.cosmology.Cosmology() hubble = co.hubble_parameter(sim.simulation.redshift).in_units('km/s/kpc') redshift = sim.simulation.redshift quench = (-1.8 + 0.3 * redshift) - 9. # define galaxy as quenched gal_sm = yt.YTArray([ sim.galaxies[i].masses['stellar'].in_units('Msun') for i in range(len(sim.galaxies)) ], 'Msun') gal_sfr = yt.YTArray([ sim.galaxies[i].sfr.in_units('Msun/yr') for i in range(len(sim.galaxies)) ], 'Msun/yr') gal_ssfr = gal_sfr / gal_sm
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100, alpha=1.): cmap_list = cmap(np.linspace(minval, maxval, n)) cmap_list[:, -1] = alpha new_cmap = colors.LinearSegmentedColormap.from_list( 'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval), cmap_list) return new_cmap if __name__ == '__main__': model = sys.argv[1] wind = sys.argv[2] snap = sys.argv[3] sim = caesar.load( f'/home/rad/data/{model}/{wind}/Groups/{model}_{snap}.hdf5') redshift = sim.simulation.redshift cmap = plt.get_cmap('jet_r') cmap = truncate_colormap(cmap, 0.1, 1.0) lines = [ "H1215", "MgII2796", "SiIII1206", "CIV1548", "OVI1031", "NeVIII770" ] plot_lines = [ r'${\rm HI}1215$', r'${\rm MgII}2796$', r'${\rm SiIII}1206$', r'${\rm CIV}1548$', r'${\rm OVI}1031$', r'${\rm NeVIII}770$' ] norients = 8 delta_fr200 = 0.25 min_fr200 = 0.25
extent=[-extent, extent, -extent, extent], cmap=cmap, norm=cNorm) return sm, img snap = '078' _dat = json.load(open('m100/galaxy_selection.json', 'r')) for gidx in ['3', '8', '51', '54', '94', '100', '134', '139']: # gidx = np.array(['3','8','51','54','94','100','134','139'])[g_idx] # if (True): print("gidx:", gidx) cs = caesar.load('%sm100n1024_%s.hdf5' % (sb.cs_directory, snap)) _mstar = np.log10(cs.galaxies[int(gidx)].masses['stellar']) print("Mstar:", _mstar) hcood = np.array(_dat[snap][gidx]['pos']) hidx = _dat[snap][gidx]['hidx'] fdir = '/blue/narayanan/c.lovell/simba/m100n1024/out/snap_078/subset_%05d.h5' % hidx # fdir ='/cosma7/data/dp104/dc-dave2/sim/m100n1024/s50j7k/snap_m100n1024_%s.hdf5'%snap with h5py.File(fdir, 'r') as f: _a = np.float32(1. / (1 + f['Header'].attrs['Redshift'])) _h = np.float32(sb.cosmo.h) _temp = (1. / _h) * _a print(_a, _h, _temp)