def __init__(self, base, num, long_ids=False, snapbase='snap', double_output=False, verbose=False, run=None): self.base = base self.snapbase = snapbase self.num = num self.num_pad = str(num).zfill(3) self.verbose = verbose self.part_types = [0, 1, 4, 5] keysel = [ "ngroups", "nsubs", "GroupLenType", "GroupNsubs", "GroupFirstSub", "SubhaloLenType" ] self.cat = readsubfHDF5.subfind_catalog(base, num, long_ids=long_ids, double_output=double_output, keysel=keysel) if not hasattr(self.cat, "GroupLenType"): raise RuntimeError("Subfind catalog has no group or subhalo " "information.") self.filenames = naming.get_snap_filenames(self.base, self.snapbase, self.num) offsets = readsubfHDF5.get_offsets(self.cat, self.part_types, self.num, run) self.group_offset, self.halo_offset = offsets head = readsnapHDF5.snapshot_header(self.filenames[0]) self.file_num = head.filenum assert (self.file_num == len(self.filenames)) ntypes = 6 self.file_type_numbers = np.zeros([self.file_num, ntypes], dtype="int64") cumcount = np.zeros(ntypes, dtype="int64") # Store in file_type_numbers[i, :] the cumulative number of particles # in all previous files. Note we never need to open the last file. for i in range(0, self.file_num - 1): if self.verbose: print "READHALO: initial read of file: %s" % self.filenames[i] head = readsnapHDF5.snapshot_header(self.filenames[i]) cumcount[:] += head.npart[:] self.file_type_numbers[i + 1, :] = cumcount[:]
def __init__(self, this_file, next_file=None, interp_coef=0.0, interp=True, dust=True, **kwargs): if next_file==None or interp==False: # no interp print "have a single file, no interp" print this_file self.head = ws.snapshot_header(this_file) if dust: print "loading dust data blocks" self.gas_xyz = np.array(ws.read_block(this_file,'POS ', parttype=0)) try: self.gas_hsml = np.array(ws.read_block(this_file,'HSML', parttype=0)) except: print "failed to load HSML values directly. Try using cell volumes instead" rho = np.array(ws.read_block(this_file,'RHO ', parttype=0)) mass = np.array(ws.read_block(this_file,'MASS', parttype=0)) vol = mass / rho self.gas_hsml = (0.75 * vol / 3.14159 ) ** 0.33333 * 3 self.gas_mass = np.array(ws.read_block(this_file,'MASS', parttype=0)) self.gas_u = np.array(ws.read_block(this_file,'U ', parttype=0)) self.gas_rho = np.array(ws.read_block(this_file,'RHO ', parttype=0)) self.gas_numh = np.array(ws.read_block(this_file,'NH ', parttype=0)) self.gas_nume = np.array(ws.read_block(this_file,'NE ', parttype=0)) try: self.gas_z = np.array(ws.read_block(this_file,'Z ', parttype=0)) except: self.gas_z = np.array(ws.read_block(this_file,'GZ ', parttype=0)) else: self.gas_xyz = np.zeros( (10,3) ) self.gas_hsml = np.zeros( 10 ) self.gas_mass = np.zeros( 10 ) self.gas_u = np.zeros( 10 ) self.gas_rho = np.zeros( 10 ) self.gas_numh = np.zeros( 10 ) self.gas_nume = np.zeros( 10 ) self.gas_z = np.zeros( 10 ) self.star_xyz = np.array(ws.read_block(this_file,'POS ', parttype=4)) self.star_mass = np.array(ws.read_block(this_file,'MASS', parttype=4)) try: self.star_bt = np.array(ws.read_block(this_file,'AGE ',parttype=4)) self.star_z = np.array(ws.read_block(this_file,'Z ',parttype=4)) except: try: self.star_bt = np.array(ws.read_block(this_file,'GAGE',parttype=4)) self.star_z = np.array(ws.read_block(this_file,'GZ ',parttype=4)) except: print "failed to load stars properly. Setting to zero to avoid failure, but this will not produce an image." self.star_xyz = np.zeros( (10,3) ) self.star_bt = np.zeros( (10) ) self.star_z = np.zeros( (10) ) self.star_mass = np.zeros( (10) )
def __init__(self, this_file, next_file=None, interp_coef=0.0, interp=True, dust=True, fof_num=-1, sub_num=-1, **kwargs): self.head = ws.snapshot_header(this_file) if dust: print "loading dust data blocks" self.gas_xyz = np.array(ws.read_block(this_file,'POS ', parttype=0)) try: self.gas_hsml = np.array(ws.read_block(this_file,'HSML', parttype=0)) except: print "failed to load HSML values directly. Try using cell volumes instead" rho = np.array(ws.read_block(this_file,'RHO ', parttype=0)) mass = np.array(ws.read_block(this_file,'MASS', parttype=0)) vol = mass / rho self.gas_hsml = (0.75 * vol / 3.14159 ) ** 0.33333 * 3 self.gas_mass = np.array(ws.read_block(this_file,'MASS', parttype=0)) self.gas_u = np.array(ws.read_block(this_file,'U ', parttype=0)) self.gas_rho = np.array(ws.read_block(this_file,'RHO ', parttype=0)) self.gas_numh = np.array(ws.read_block(this_file,'NH ', parttype=0)) self.gas_nume = np.array(ws.read_block(this_file,'NE ', parttype=0)) try: self.gas_z = np.array(ws.read_block(this_file,'Z ', parttype=0)) except: self.gas_z = np.array(ws.read_block(this_file,'GZ ', parttype=0)) else: self.gas_xyz = np.zeros( (10,3) ) self.gas_hsml = np.zeros( 10 ) self.gas_mass = np.zeros( 10 ) self.gas_u = np.zeros( 10 ) self.gas_rho = np.zeros( 10 ) self.gas_numh = np.zeros( 10 ) self.gas_nume = np.zeros( 10 ) self.gas_z = np.zeros( 10 ) import simread.readhaloHDF5 as hr print this_file snapdir = this_file[:this_file.index('output')+7] run=this_file[this_file.index('Runs/')+5:this_file.index('output') ].replace('/','') print run snapnum = int(this_file[-3:]) halo_reader = hr.HaloReader( snapdir, snapnum, run=run ) self.star_xyz = halo_reader.read( 'POS ', 4, fof_num, sub_num ) self.star_bt = halo_reader.read( 'GAGE', 4, fof_num, sub_num ) self.star_z = halo_reader.read( 'GZ ', 4, fof_num, sub_num ) self.star_mass = halo_reader.read( 'MASS', 4, fof_num, sub_num ) print "loaded stellar data from "+this_file
np.log10(min_bin_mass)) NUM_COLORS = n_bins cm = plt.get_cmap('nipy_spectral') cNorm = colors.Normalize(vmin=0, vmax=n_bins) fig, ax = plt.subplots() for run in ['explicit_feedback_fiducial']: #'dm_runs', 'Illustris-Dark-1']: if run == 'dm_runs': snaparray = np.array([6]) #range(7) if run == 'Illustris-Dark-1': snaparray = np.array([135]) if run == 'explicit_feedback_fiducial': snaparray = range(12) for snapnum in snaparray: header = ws.snapshot_header('../' + run + '/output/snapdir_' + str(snapnum).zfill(3) + '/snapshot_' + str(snapnum).zfill(3) + '.0.hdf5') cat = subf.subfind_catalog('../' + run + '/', snapnum, keysel=['Group_M_Crit200', 'GroupMass']) if run == 'dm_runs' or run == 'explicit_feedback_fiducial': volume = (header.boxsize / little_h)**3.0 if run == 'Illustris-Dark-1': volume = (header.boxsize / little_h / 1000.0)**3.0 print header.boxsize masses = np.array(cat.Group_M_Crit200[:]) * 1e10 / little_h #masses = np.array( cat.GroupMass[:] ) * 1e10 / little_h
def __init__(self, path, min_time=None, max_time=None, n_frames=None, npixels=256, tag='', interp=False, log_rho_min=-6.0, log_rho_max=4.0, log_t_min=1.0, log_t_max=8.0, ignore_central_region=False, **kwargs): print path self.sdir = path snap_list = np.array(glob.glob(path + '/snap*_*.hdf5')) snap_list = snap_list[:-1] if len(snap_list) == 1: snap_list = np.array([snap_list[0], snap_list[0], snap_list[0]]) print snap_list try: all_snapnrs = np.array([ int(file[file.index('snapshot_') + 9:file.index('.hdf5')]) for file in snap_list ], dtype=int) except: all_snapnrs = np.array([ int(file[file.index('snap_') + 5:file.index('.hdf5')]) for file in snap_list ], dtype=int) all_snapnrs = np.sort(all_snapnrs) print all_snapnrs first_snapnr = all_snapnrs[0] new_snap_list = [None] * all_snapnrs.shape[0] #(snap_list.shape[0] self.all_snapnrs = all_snapnrs snap_timing = np.zeros(all_snapnrs.shape[0]) for index in np.arange(all_snapnrs.shape[0]): try: this_file = path + "snapshot_" + str( index + int(first_snapnr)).zfill(3) + ".hdf5" this_file = path + "snapshot_" + str( all_snapnrs[index]).zfill(3) + ".hdf5" #index+int(first_snapnr)).zfill(3)+".hdf5" new_snap_list[index] = this_file print this_file head = ws.snapshot_header(this_file[:this_file.index('.hdf5')]) snap_timing[index] = head.time except: this_file = path + "snapshot_" + str( all_snapnrs[index]).zfill(4) + ".hdf5" new_snap_list[index] = this_file print this_file head = ws.snapshot_header(this_file[:this_file.index('.hdf5')]) snap_timing[index] = head.time snap_timing[index] = -1.0 print "snapshot failed..." self.interp = interp self.savetag = tag self.snap_list = np.array(new_snap_list) self.snap_timing = snap_timing self.npixels = npixels self.log_rho_min = log_rho_min self.log_rho_max = log_rho_max self.log_t_min = log_t_min self.log_t_max = log_t_max self.ignore_central_region = ignore_central_region if (not os.path.exists("./plots/frames/" + self.savetag)): os.makedirs("./plots/frames/" + self.savetag) if min_time == None: self.min_movie_time = np.min(snap_timing) else: self.min_movie_time = min_time if max_time == None: self.max_movie_time = np.max(snap_timing) else: self.max_movie_time = max_time if n_frames != None: self.n_frames = n_frames else: self.n_frames = snap_list.shape[0] self.dt_frames = (self.max_movie_time - self.min_movie_time) / (1.0 * self.n_frames - 1.0)
print "cut=", rtmp.max(), rtmp[ind].max(), Rcut ws.write_block(f, "POS ", 0, np.array([x_gas[ind],y_gas[ind],z_gas[ind]]).T) ws.write_block(f, "VEL ", 0, np.array([vx_gas[ind],vy_gas[ind],vz_gas[ind]]).T) ws.write_block(f, "U ", 0, utherm[ind]) ws.write_block(f, "ID ", 0, np.arange(1,N_gas+1)) if (add_halo): massarr=np.array([gas_mass,halo_mass,0,0,0,0], dtype="float64") npart=np.array([N_gas,N_halo,0,0,0,0], dtype="uint32") ws.write_block(f, "POS ", 1, np.array([x_halo,y_halo,z_halo]).T) ws.write_block(f, "VEL ", 1, np.array([vx_halo,vy_halo,vz_halo]).T) ws.write_block(f, "MASS", 1, np.repeat(halo_mass, N_gas)) ws.write_block(f, "ID ", 1, np.arange(1+N_gas,N_gas+N_halo+2)) else: massarr=np.array([gas_mass,0,0,0,0,0], dtype="float64") npart=np.array([N_gas,0,0,0,0,0], dtype="uint32") header=ws.snapshot_header(npart=npart, nall=npart, massarr=massarr) ws.writeheader(f, header) ws.closefile(f) print "done." print "\n-FINISHED-\n" print "r200 = ", NFW_r200 print "rs = ", NFW_rs print "delta = ", NFW_delta print "m_gas = ", gas_mass print "N_gas = ", N_gas if (add_halo): print "m_halo = ", halo_mass print "N_halo = ", N_halo
def __init__(self, path, tag, factor=1, **kwargs): snap_list = np.array(glob.glob(path + '/snap*_*hdf5')) print path, snap_list all_snapnrs = np.sort( np.array([ int(file[file.index('snapshot_') + 9:file.index('.hdf5')]) for file in snap_list ], dtype=int)) first_snapnr = all_snapnrs[0] new_snap_list = [None] * snap_list.shape[0] snap_timing = np.zeros(snap_list.shape[0]) for index in np.arange(snap_list.shape[0]): this_file = path + "snapshot_" + str( index * factor + int(first_snapnr)).zfill(3) + ".hdf5" print this_file new_snap_list[index] = this_file try: head = ws.snapshot_header(this_file[:this_file.index('.hdf5')]) snap_timing[index] = head.time except: snap_timing[index] = -1.0 self.first_snapnr = first_snapnr self.savetag = tag self.snap_list = np.array(new_snap_list) self.snap_timing = snap_timing self.n_snaps = self.snap_list.shape[0] print " " print tag print " " self.alloc_loc_arrays() this_file = path + "snapshot_" + str( int(first_snapnr)).zfill(3) + ".hdf5" print this_file gas_xyz = np.array(ws.read_block(this_file, 'POS ', parttype=0)) star_xyz = np.append( np.array(ws.read_block(this_file, 'POS ', parttype=2)), np.array(ws.read_block(this_file, 'POS ', parttype=3)), axis=0) star_mass = np.append( np.array(ws.read_block(this_file, 'MASS', parttype=2)), np.array(ws.read_block(this_file, 'MASS', parttype=3)), axis=0) * 1e10 bh_id = np.array(ws.read_block(this_file, 'ID ', parttype=5)) bh_xyz = np.array(ws.read_block(this_file, 'POS ', parttype=5)) bh_order = np.argsort(bh_id) bh_xyz = bh_xyz[bh_order, :] # bh_xyz = np.array(ws.read_block(this_file,'POS ', parttype=5)) # calculate stellar offsets, to determine each galaxy's initial stellar mass (for MZ rleaiton) dr = np.zeros((bh_xyz.shape[0], star_xyz.shape[0])) for iii in np.arange(bh_xyz.shape[0]): dx2 = (star_xyz[:, 0] - bh_xyz[iii, 0])**2 dy2 = (star_xyz[:, 1] - bh_xyz[iii, 1])**2 dz2 = (star_xyz[:, 2] - bh_xyz[iii, 2])**2 dr[iii, :] = np.sqrt( dx2 + dy2 + dz2) # offset from this BH; and then you have it. min_dr = np.min(dr, axis=0) # now the min_dr, regardless of n_bh print bh_xyz print dr print dr.shape print min_dr.shape m_gal_1 = 0 m_gal_2 = 0 for iii in np.arange(min_dr.shape[0]): if min_dr[iii] == dr[0, iii]: m_gal_1 += star_mass[iii] else: m_gal_2 += star_mass[iii] print m_gal_1, m_gal_2 print np.log10(m_gal_1), np.log10(m_gal_2) x1 = np.log10(m_gal_1) x2 = np.log10(m_gal_2) z10_tmp = 27.7911 - 6.94493 * x1 + 0.808097 * x1 * x1 - 0.0301508 * x1 * x1 * x1 z20_tmp = 27.7911 - 6.94493 * x2 + 0.808097 * x2 * x2 - 0.0301508 * x2 * x2 * x2 print " Initial Metallicity Summary: " print " We think galaxy 1 has a mass of {:.3f} and a metallicity of {:.3f}".format( x1, z10_tmp) print " We think galaxy 2 has a mass of {:.3f} and a metallicity of {:.3f}".format( x2, z20_tmp) # calculate gas offsets, to determine each galaxy's metallicity gradient dr = np.zeros((bh_xyz.shape[0], gas_xyz.shape[0])) for iii in np.arange(bh_xyz.shape[0]): dx2 = (gas_xyz[:, 0] - bh_xyz[iii, 0])**2 dy2 = (gas_xyz[:, 1] - bh_xyz[iii, 1])**2 dz2 = (gas_xyz[:, 2] - bh_xyz[iii, 2])**2 dr[iii, :] = np.sqrt( dx2 + dy2 + dz2) # offset from this BH; and then you have it. min_dr = np.min( dr, axis=0) # now the min_dr, regardless of n_bh, (right?!) tmp_ids = np.array(ws.read_block(this_file, 'ID ', parttype=0)) self.init_ids = np.zeros(tmp_ids.max() + 1) self.init_metallicity = np.zeros(tmp_ids.max() + 1) for iii in np.arange(min_dr.shape[0]): if min_dr[iii] == dr[0, iii]: x = np.log10(m_gal_1) else: x = np.log10(m_gal_2) oh_12 = 27.7911 - 6.94493 * x + 0.808097 * x * x - 0.0301508 * x * x * x z_zsolar = 10.0**( oh_12 - 12) / 0.0004 # central metallicity in solar units. this_id = tmp_ids[iii] this_z = 0.0127 * z_zsolar * 10.0**(-0.1 * min_dr[iii]) self.init_ids[this_id] = this_id self.init_metallicity[this_id] = this_z
def write_one_snapshot(self, snap_index, **kwargs): this_file = self.snap_list[snap_index] header = ws.snapshot_header(this_file) self.values['time'][snap_index] = header.time print this_file gas_xyz = np.array(ws.read_block(this_file, 'POS ', parttype=0)) gas_sfr = np.array(ws.read_block(this_file, 'SFR ', parttype=0)) gas_z = np.array(ws.read_block(this_file, 'Z ', parttype=0)) gas_z = gas_z[:, 0] gas_mass = np.array(ws.read_block(this_file, 'MASS', parttype=0)) gas_ids = np.array(ws.read_block(this_file, 'ID ', parttype=0)) gas_u = np.array(ws.read_block(this_file, 'U ', parttype=0)) gas_ne = np.array(ws.read_block(this_file, 'NE ', parttype=0)) gas_t = units.gas_code_to_temperature(gas_u, gas_ne) for index, value in enumerate(gas_ids): # IDs, add init metallicity #print index, value, gas_z.shape, self.init_metallicity.shape try: gas_z[index] += self.init_metallicity[ value] # all that matters here is that init metallicty can be indexed by ID except: try: gas_z[index] += self.init_metallicity[value & 2**30 - 1] except: gas_z[index] += 0.0 # confused... sys.exit() try: star_xyz = np.array(ws.read_block(this_file, 'POS ', parttype=4)) star_mass = np.array(ws.read_block(this_file, 'MASS', parttype=4)) star_ages = np.array(ws.read_block(this_file, 'AGE ', parttype=4)) star_ages = header.time - star_ages except: star_xyz = np.zeros((10, 3)) star_mass = np.zeros(10) star_ages = np.zeros(10) bh_id = np.array(ws.read_block(this_file, 'ID ', parttype=5)) bh_xyz = np.array(ws.read_block(this_file, 'POS ', parttype=5)) bh_order = np.argsort(bh_id) bh_xyz = bh_xyz[bh_order, :] if bh_xyz.shape[0] == 1: self.values['sep'][snap_index] = 0 else: self.values['sep'][snap_index] = np.sqrt( (bh_xyz[0, 0] - bh_xyz[1, 0])**2 + (bh_xyz[0, 1] - bh_xyz[1, 1])**2 + (bh_xyz[0, 2] - bh_xyz[1, 2])**2) dr = np.zeros((bh_xyz.shape[0], gas_xyz.shape[0])) for iii in np.arange(bh_xyz.shape[0]): dx2 = (gas_xyz[:, 0] - bh_xyz[iii, 0])**2 dy2 = (gas_xyz[:, 1] - bh_xyz[iii, 1])**2 dz2 = (gas_xyz[:, 2] - bh_xyz[iii, 2])**2 dr[iii, :] = np.sqrt( dx2 + dy2 + dz2) # offset from this BH; and then you have it. min_dr = np.min( dr, axis=0) # now the min_dr, regardless of n_bh, (right?!) all_molec_gas_mass = gas_mass[gas_t < 100] all_molec_gas_min_dr = min_dr[gas_t < 100] all_molec_gas_dr = dr[:, gas_t < 100] all_atomic_gas_mass = gas_mass[(gas_t > 100) & (gas_t < 1000)] all_atomic_gas_min_dr = min_dr[(gas_t > 100) & (gas_t < 1000)] all_atomic_gas_dr = dr[:, (gas_t > 100) & (gas_t < 1000)] all_warm_gas_mass = gas_mass[(gas_t > 1000) & (gas_t < 1e5)] all_warm_gas_min_dr = min_dr[(gas_t > 1000) & (gas_t < 1e5)] all_warm_gas_dr = dr[:, (gas_t > 1000) & (gas_t < 1e5)] all_hot_gas_mass = gas_mass[(gas_t > 1e5)] all_hot_gas_min_dr = min_dr[(gas_t > 1e5)] all_hot_gas_dr = dr[:, (gas_t > 1e5)] for iii in np.arange(bh_xyz.shape[0]): for jjj in np.arange(self.rad_bins.shape[0]): this_index = (dr[iii, :] < self.rad_bins[jjj]) & (np.equal( dr[iii, :], min_dr)) self.values['sfr'][iii, jjj, snap_index] = np.sum(gas_sfr[this_index]) self.values['z'][iii, jjj, snap_index] = np.median(gas_z[this_index]) self.values['mgas'][iii, jjj, snap_index] = np.sum(gas_mass[this_index]) this_index = ( all_molec_gas_dr[iii, :] < self.rad_bins[jjj]) & (np.equal( all_molec_gas_dr[iii, :], all_molec_gas_min_dr)) self.values['m_molec_gas'][iii, jjj, snap_index] = np.sum( all_molec_gas_mass[this_index]) this_index = (all_atomic_gas_dr[iii, :] < self.rad_bins[jjj] ) & (np.equal(all_atomic_gas_dr[iii, :], all_atomic_gas_min_dr)) self.values['m_atomic_gas'][iii, jjj, snap_index] = np.sum( all_atomic_gas_mass[this_index]) this_index = (all_warm_gas_dr[iii, :] < self.rad_bins[jjj]) & ( np.equal(all_warm_gas_dr[iii, :], all_warm_gas_min_dr)) self.values['m_warm_gas'][iii, jjj, snap_index] = np.sum( all_warm_gas_mass[this_index]) this_index = (all_hot_gas_dr[iii, :] < self.rad_bins[jjj]) & ( np.equal(all_hot_gas_dr[iii, :], all_hot_gas_min_dr)) self.values['m_hot_gas'][iii, jjj, snap_index] = np.sum( all_hot_gas_mass[this_index]) dr = np.zeros((bh_xyz.shape[0], star_xyz.shape[0])) for iii in np.arange(bh_xyz.shape[0]): dx2 = (star_xyz[:, 0] - bh_xyz[iii, 0])**2 dy2 = (star_xyz[:, 1] - bh_xyz[iii, 1])**2 dz2 = (star_xyz[:, 2] - bh_xyz[iii, 2])**2 dr[iii, :] = np.sqrt( dx2 + dy2 + dz2) # offset from this BH; and then you have it. min_dr = np.min( dr, axis=0) # now the min_dr, regardless of n_bh, (right?!) for iii in np.arange(bh_xyz.shape[0]): for jjj in np.arange(self.rad_bins.shape[0]): this_index = (dr[iii, :] < self.rad_bins[jjj]) & (np.equal( dr[iii, :], min_dr)) if np.sum(this_index) > 0: self.values['stellar_ages'][iii, jjj, snap_index] = np.median( star_ages[this_index]) self.values['mstar'][iii, jjj, snap_index] = np.sum( star_mass[this_index]) this_index = (dr[iii, :] < self.rad_bins[jjj]) & (np.equal( dr[iii, :], min_dr)) & (star_ages < 0.01) if np.sum(this_index) > 0: self.values['sfr_sm'][iii, jjj, snap_index] = np.sum( star_mass[this_index]) / 0.01 * 10.0 this_index = (dr[iii, :] < self.rad_bins[jjj]) & (np.equal( dr[iii, :], min_dr)) & (star_ages < 0.1) if np.sum(this_index) > 0: self.values['sfr_sm2'][iii, jjj, snap_index] = np.sum( star_mass[this_index]) / 0.1 * 10.0
fig_.subplots_adjust(left=0.0, bottom=0.0, top=1.0, right=1.0, hspace=0, wspace=0) ax1_.axis('off') ax2_.axis('off') ax3_.axis('off') import units.springel_units as units header = ws.snapshot_header('../' + run_base + '/output/snapdir_' + str(snapnum).zfill(3) + '/snapshot_' + str(snapnum).zfill(3)) star_pos = ws.read_block( '../' + run_base + '/output/snapdir_' + str(snapnum).zfill(3) + '/snapshot_' + str(snapnum).zfill(3), "POS ", 4) star_bt = ws.read_block( '../' + run_base + '/output/snapdir_' + str(snapnum).zfill(3) + '/snapshot_' + str(snapnum).zfill(3), "GAGE", 4) star_hsml = ws.read_block( '../' + run_base + '/output/snapdir_' + str(snapnum).zfill(3) + '/snapshot_' + str(snapnum).zfill(3), "STH ", 4) star_cp1 = ws.read_block(
__email__ = "*****@*****.**" __status__ = "Public Release. v1.0." # generic module imports import numpy as np import sys # my module imports import simread.readsnapHDF5 as ws ic1 = str(sys.argv[1]) # it's assumed these are hdf5 files ic2 = str(sys.argv[2]) h1 = ws.snapshot_header(ic1) # load header 1 npart1 = h1.nall h2 = ws.snapshot_header(ic2) # load header 2 npart2 = h2.nall nall = h1.nall + h2.nall all_blocks1 = ws.return_tags(ic1) f = ws.openfile('new_ics.hdf5') my_header = h1 last_ID = 1 for parttype in np.arange(6): this_part_blocks = all_blocks1[parttype] for block in this_part_blocks: print parttype, block
def __init__(self, path, min_time=None, max_time=None, n_frames=None, fov=15.0, npixels=256, tag='', frametag='', interp=False, zoom_factor=0.0, IFU=False, plottype='gas', threecolor=True, dynrange=0.6e5, maxden=0.01, plot_stars=False, theta=0, proj_depth=0, arepo=0, set_bh_center_and_leave=0, center_pos=[0, 0, 0], **kwargs): print path self.sdir = path snap_list = np.array(glob.glob(path + '/snap*')) #_*hdf5')) print snap_list try: all_snapnrs = np.array([ int(file[file.index('snapshot_') + 9:file.index('.hdf5')]) for file in snap_list ], dtype=int) except: try: all_snapnrs = np.array([ int(file[file.index('snap_') + 5:file.index('.hdf5')]) for file in snap_list ], dtype=int) except: all_snapnrs = np.array([ int(file[file.index('snapdir_') + 8:]) for file in snap_list ], dtype=int) all_snapnrs = np.sort(all_snapnrs) # all_snapnrs = all_snapnrs[ all_snapnrs > 0 ] print all_snapnrs first_snapnr = all_snapnrs[0] new_snap_list = [None] * all_snapnrs.shape[0] #(snap_list.shape[0] self.all_snapnrs = all_snapnrs snap_timing = np.zeros(all_snapnrs.shape[0]) for index in np.arange(all_snapnrs.shape[0]): try: this_file = path + "snapshot_" + str( index + int(first_snapnr)).zfill(3) + ".hdf5" new_snap_list[index] = this_file # print this_file head = ws.snapshot_header(this_file[:this_file.index('.hdf5')]) snap_timing[index] = head.time except: this_file = path + "snapdir_" + str( index + int(first_snapnr)).zfill(3) + "/snapshot_" + str( index + int(first_snapnr)).zfill(3) #+".0.hdf5" new_snap_list[index] = this_file # print this_file head = ws.snapshot_header( this_file) #[:this_file.index('.hdf5')]) snap_timing[index] = head.time # snap_timing[index] = -1.0 # print "snapshot failed..." self.interp = interp self.savetag = tag self.frametag = frametag self.snap_list = np.array(new_snap_list) self.snap_timing = snap_timing self.fov = fov self.npixels = npixels self.maxden = maxden self.dynrange = dynrange self.zoom_factor = zoom_factor self.IFU = IFU # True / False self.plottype = plottype # gas, sfr, stellar XYZ band self.threecolor = threecolor # True / False self.plot_stars = plot_stars self.theta = theta self.proj_depth = proj_depth self.arepo = arepo print "snap timing:" print snap_timing if (not os.path.exists("./plots/frames/" + self.savetag)): os.makedirs("./plots/frames/" + self.savetag) if min_time == None: self.min_movie_time = np.min(snap_timing) else: self.min_movie_time = min_time if max_time == None: self.max_movie_time = np.max(snap_timing) else: self.max_movie_time = max_time if n_frames != None: self.n_frames = n_frames else: self.n_frames = snap_list.shape[0] self.dt_frames = (self.max_movie_time - self.min_movie_time) / (1.0 * self.n_frames - 1.0) if center_pos == [0, 0, 0]: print "here!" print set_bh_center_and_leave if set_bh_center_and_leave == 1: bhpos = ws.read_block(self.snap_list[0], 'POS ', 5)[0] self.center_pos = bhpos else: self.center_pos = np.array([0, 0, 0]) else: self.center_pos = center_pos
def __init__(self, this_file, next_file=None, interp_coef=0.0, interp=True, crude=False, ra_range=None, dec_range=None, ra_center=None, dec_center=None, zrange=None, convert_to_sg=False, \ **kwargs): if next_file==None or interp==False: # no interp print "have a single file, no interp" self.head = ws.snapshot_header(this_file) self.xyz = np.array(ws.read_block(this_file,'POS ', parttype=1)) self.mass = np.array(ws.read_block(this_file,'MASS', parttype=1)) npart = np.shape(self.xyz)[0]*1.0 if crude: self.hsml = np.zeros_like( self.mass ) + self.head.boxsize / ( npart**0.333 ) * 1.0 else: sys.exit() self.hsml = calc_hsml.get_particle_hsml( self.xyz[:,0], self.xyz[:,1], self.xyz[:,2] , **kwargs ) if True: #ra_center is not None and dec_center is not None: # need to do some rotation if True: # this likely makes the most sense. MW is at the center. tmp_x = self.xyz[:,0] - self.head.boxsize/2.0 tmp_y = self.xyz[:,1] - self.head.boxsize/2.0 tmp_z = self.xyz[:,2] - self.head.boxsize/2.0 else: # this could be possible, but puts the MW at the origin, at the edge of the box. tmp_x = self.xyz[:,0] tmp_y = self.xyz[:,1] tmp_z = self.xyz[:,2] tmp_x[ tmp_x > self.head.boxsize/2.0 ] -= self.head.boxsize tmp_y[ tmp_y > self.head.boxsize/2.0 ] -= self.head.boxsize tmp_z[ tmp_z > self.head.boxsize/2.0 ] -= self.head.boxsize tmp_r = np.sqrt( tmp_x**2 + tmp_y**2 + tmp_z**2 ) dec = np.arcsin( tmp_z / tmp_r ) * 180.0 / 3.14159 ra = np.arctan2( tmp_y, tmp_x ) * 180.0 / 3.14159 c = SkyCoord(ra=ra*u.degree, dec=dec*u.degree) if convert_to_sg: new_coords = c.transform_to('supergalactic') self.xyz[:,0] = new_coords.cartesian.x * tmp_r + self.head.boxsize/2.0 self.xyz[:,1] = new_coords.cartesian.y * tmp_r + self.head.boxsize/2.0 self.xyz[:,2] = new_coords.cartesian.z * tmp_r + self.head.boxsize/2.0 else: self.xyz[:,0] = c.cartesian.x * tmp_r + self.head.boxsize/2.0 self.xyz[:,1] = c.cartesian.y * tmp_r + self.head.boxsize/2.0 self.xyz[:,2] = c.cartesian.z * tmp_r + self.head.boxsize/2.0 new_r = np.sqrt( self.xyz[:,0] **2 + self.xyz[:,1] **2 + self.xyz[:,2] **2 ) if ra_range is not None and dec_range is not None: tmp_x = self.xyz[:,0] - self.head.boxsize/2.0 tmp_y = self.xyz[:,1] - self.head.boxsize/2.0 tmp_z = self.xyz[:,2] - self.head.boxsize/2.0 tmp_r = np.sqrt( tmp_x**2 + tmp_y**2 + tmp_z**2 ) dec = np.arcsin( tmp_z / tmp_r ) ra = np.arctan2( tmp_y, tmp_x ) in_box_index = ( ra > np.min(ra_range) ) & ( ra < np.max(ra_range) ) & \ ( dec> np.min(dec_range)) & (dec < np.max(dec_range)) print "Found {:d} out of {:d} particles in our projection wedge".format( np.sum(in_box_index), len(in_box_index) ) # sys.exit() self.xyz = self.xyz[in_box_index,:] self.mass = self.mass[in_box_index] self.hsml = self.hsml[in_box_index] else: # TODO: FINISH THIS BLOCK head = ws.snapshot_header(this_file) gas_id1 = np.array(ws.read_block(this_file,'ID ', parttype=0)) order = np.argsort(gas_id1) gas_id1 = gas_id1[order] gas_xyz1 = np.array(ws.read_block(this_file,'POS ', parttype=0))[order,:] gas_hsml1 = np.array(ws.read_block(this_file,'HSML', parttype=0))[order] gas_mass1 = np.array(ws.read_block(this_file,'MASS', parttype=0))[order] gas_u1 = np.array(ws.read_block(this_file,'U ', parttype=0))[order] gas_nume1 = np.array(ws.read_block(this_file,'NE ', parttype=0))[order] try: gas_z1 = np.array(ws.read_block(this_file,'Z ', parttype=0)) gas_z1 = np.sum( gas_z1[order,2:], axis=1) except: gas_z1 = np.array(ws.read_block(this_file,'GZ ', parttype=0))[order] gas_id2 = np.array(ws.read_block(next_file,'ID ', parttype=0)) order = np.argsort(gas_id2) gas_id2 = gas_id2[order] gas_xyz2 = np.array(ws.read_block(next_file,'POS ', parttype=0))[order,:] gas_hsml2 = np.array(ws.read_block(next_file,'HSML', parttype=0))[order] gas_mass2 = np.array(ws.read_block(next_file,'MASS', parttype=0))[order] gas_u2 = np.array(ws.read_block(next_file,'U ', parttype=0))[order] gas_nume2 = np.array(ws.read_block(next_file,'NE ', parttype=0))[order] try: gas_z2 = np.array(ws.read_block(next_file,'Z ', parttype=0)) gas_z2 = np.sum( gas_z2[order,2:], axis=1) except: gas_z2 = np.array(ws.read_block(next_file,'GZ ', parttype=0))[order] gas_id2 = np.array(ws.read_block(next_file,'ID ', parttype=0)) i1 = np.in1d( gas_id1, gas_id2 ) gas_xyz1 = gas_xyz1[i1,:] gas_hsml1= gas_hsml1[i1] gas_mass1= gas_mass1[i1] gas_u1 = gas_u1[i1] gas_nume1= gas_nume1[i1] gas_z1 = gas_z1[i1] i1 = np.in1d( gas_id2, gas_id1 ) gas_xyz2 = gas_xyz2[i1,:] gas_hsml2= gas_hsml2[i1] gas_mass2= gas_mass2[i1] gas_u2 = gas_u2[i1] gas_nume2= gas_nume2[i1] gas_z2 = gas_z2[i1] self.gas_xyz = (1.0 - interp_coef) * gas_xyz1 + interp_coef * gas_xyz2 self.gas_hsml= (1.0 - interp_coef) * gas_hsml1 + interp_coef * gas_hsml2 self.gas_mass= (1.0 - interp_coef) * gas_mass1 + interp_coef * gas_mass2 self.gas_u = (1.0 - interp_coef) * gas_u1 + interp_coef * gas_u2 self.gas_nume= (1.0 - interp_coef) * gas_nume1 + interp_coef * gas_nume2 self.gas_z = (1.0 - interp_coef) * gas_z1 + interp_coef * gas_z2
def read(self, block_name, parttype, fof_num, sub_num): if sub_num < 0 and fof_num < 0: # Load all of the non-FoF particles. off = (self.group_offset[-1, parttype] + self.cat.GroupLenType[-1, parttype]) left = 1e9 # reads the rest. print off, left if sub_num >= 0 and fof_num < 0: off = self.halo_offset[sub_num, parttype] left = self.cat.SubhaloLenType[sub_num, parttype] if fof_num >= 0 and sub_num < 0: off = self.group_offset[fof_num, parttype] left = self.cat.GroupLenType[fof_num, parttype] if sub_num >= 0 and fof_num >= 0: real_sub_num = sub_num + self.cat.GroupFirstSub[fof_num] off = self.halo_offset[real_sub_num, parttype] left = self.cat.SubhaloLenType[real_sub_num, parttype] if left == 0: if self.verbose: print "READHALO: no particles of type...returning" return # Get first file that contains particles of required halo/fof/etc. findex = np.argmax(self.file_type_numbers[:, parttype] > off) - 1 # np.argmax returns 0 when the offset corresponds to a particle # in the last file. if findex == -1: findex = self.file_num - 1 # Convert the overall offset to an offset for just the file given by # findex by subtracting off the number of particles in previous files. for fnr in range(0, findex): off -= (self.file_type_numbers[fnr + 1, parttype] - self.file_type_numbers[fnr, parttype]) # Read data from file(s). first = True for fnr in range(findex, self.file_num): path = self.filenames[fnr] head = readsnapHDF5.snapshot_header(path) nloc = head.npart[parttype] if nloc > off: if self.verbose: print "READHALO: data found in %s" % path start = off if nloc - off > left: # All remaining particles are in this file. count = left else: # Read to end of file. count = nloc - off block = readsnapHDF5.read_block(path, block_name, parttype, slab_start=start, slab_len=count) if first: data = block first = False else: data = np.append(data, block, axis=0) left -= count off += count if left == 0: break off -= nloc gc.collect() return data
def __init__(self, path, min_time=None, max_time=None, n_frames=None, fov=15.0, npixels=256, tag='', gas_dynrange=3e2, gas_maxden=1e-2, sfr_dynrange=3e2, sfr_maxden=1e-5, star_dynrange=3e2, star_maxden=1e-5, **kwargs): if path != None: print path self.sdir = path snap_list = np.array(glob.glob(path + '/snap*_???.hdf5')) all_snapnrs = np.array([ int(file[file.index('snapshot_') + 9:file.index('.hdf5')]) for file in snap_list ], dtype=int) all_snapnrs = np.sort(all_snapnrs) all_snapnrs = all_snapnrs[all_snapnrs > 0] first_snapnr = all_snapnrs[0] new_snap_list = [None] * all_snapnrs.shape[0] #(snap_list.shape[0] self.all_snapnrs = all_snapnrs snap_timing = np.zeros(all_snapnrs.shape[0]) for index in np.arange(all_snapnrs.shape[0]): this_file = path + "snapshot_" + str( index + int(first_snapnr)).zfill(3) + ".hdf5" new_snap_list[index] = this_file print this_file head = ws.snapshot_header(this_file[:this_file.index('.hdf5')]) snap_timing[index] = head.time self.savetag = tag self.snap_list = np.array(new_snap_list) self.snap_timing = snap_timing self.fov = fov self.npixels = npixels self.gas_dynrange = gas_dynrange self.gas_maxden = gas_maxden self.sfr_dynrange = sfr_dynrange self.sfr_maxden = sfr_maxden self.star_dynrange = star_dynrange self.star_maxden = star_maxden print self.star_dynrange, self.star_maxden if min_time == None: self.min_movie_time = np.min(snap_timing) else: self.min_movie_time = min_time if max_time == None: self.max_movie_time = np.max(snap_timing) else: self.max_movie_time = max_time if n_frames != None: self.n_frames = n_frames else: self.n_frames = snap_list.shape[0] self.dt_frames = (self.max_movie_time - self.min_movie_time) / ( 1.0 * self.n_frames - 1.0)