Example #1
0
def load_IDs(snap1, snap2, t0, parttype='star'):
    PID_curr = np.array(readsnap(snap1, 'pid', parttype),
                        dtype=np.uint64)  # particle IDs in current snapshot
    PID_prog = np.array(readsnap(snap2, 'pid', parttype),
                        dtype=np.uint64)  # particle IDs in progenitor snapshot
    if parttype == 'star':
        try:
            IDgen_curr = np.array(
                readsnap(snap1, 'ID_Generations', 'star') - 1,
                dtype=np.uint64)  # ID generation info for duplicate IDs
            IDgen_prog = np.array(readsnap(snap2, 'ID_Generations', 'star') -
                                  1,
                                  dtype=np.uint64)
        except:  # older snapshots (like Mufasa) do not contain ID_Generations
            IDgen_curr = np.zeros(len(PID_curr))
            IDgen_prog = np.zeros(len(PID_prog))
        #DN surprisingly, we need one more catch here in case this
        #try/except doesn't work because pygr/readsnap returns an
        #empty array sometimes when there's no ID_Generations in the star particles
        if len(IDgen_curr) == 0:
            IDgen_curr = np.zeros(len(PID_curr))
            IDgen_prog = np.zeros(len(PID_prog))

        print(
            'progen : Read ID info for %d current stars and %d progenitor stars [t=%g s]'
            % (len(PID_curr), len(PID_prog), time.time() - t0))
        maxid = max(max(PID_curr), max(PID_prog))
        #print len(IDgen_curr[IDgen_curr>0])
        PID_curr = np.add(PID_curr, maxid * IDgen_curr)
        PID_prog = np.add(
            PID_prog, maxid * IDgen_prog
        )  #these ugly lines replace         PID_prog += maxid*IDgen_prog because certain versions of numpy make them unsafe casting and error out
    return PID_curr, PID_prog
Example #2
0
def readsnapshot(snapshot, PATH='snapshotdata/snapdir_'):
    """ A program to read in gadget-2 snapshot data with filename 62.5_dm_XXX.k .

    :param snapshot: The number of the snapshot as a string (e.g. '001').
    :param PATH: The filepath to the snapshot data directory.

    :return: A list for each variable in the snapshot data no longer split
    into files, along with the relevant header data.
    """

    # Read in the header data and assign to variables
    head = readheader(PATH + snapshot + '/62.5_dm_' + snapshot, 'header')
    npart = head.get('npartTotal')[1]  # number of particles in snapshot
    z = head.get('redshift')  # redshift of snapshot
    t = head.get('time')  # time of snapshot (age)
    h = head.get('h')
    boxsize = head.get('boxsize')  # size of simulation box
    rhocrit = head.get('rhocrit')  # critical density at time t

    # Read in the snapshot data and assign to variable for returning
    pos = readsnap(PATH + snapshot + '/62.5_dm_' + snapshot, 'pos', 1)
    vel = readsnap(PATH + snapshot + '/62.5_dm_' + snapshot, 'vel', 1)
    pid = readsnap(PATH + snapshot + '/62.5_dm_' + snapshot, 'pid', 1)
    pmass = readsnap(PATH + snapshot + '/62.5_dm_' + snapshot, 'mass', 1)[0]

    return pid, pos, vel, npart, z, t, boxsize, rhocrit, pmass, h
Example #3
0
def N_body_orbit(path, snap_name, snap_n, pid):
    """
    Returns the positions and velocity of a particle given it's index
    """
    pos = readsnap(path + snap_name + '_{:03d}.hdf5'.format(snap_n),'pos', 'dm')
    vel = readsnap(path + snap_name + '_{:03d}.hdf5'.format(snap_n),'vel', 'dm')
    LMCMW_pid = readsnap(path + snap_name +'_{:03d}.hdf5'.format(snap_n), 'pid', 'dm')
    index_id = np.where(LMCMW_pid == pid)[0]
    return pos[index_id], vel[index_id]
def get_projden(pathname,snum,main_part):

  global h
  mass = pg.readsnap(pathname+'snapdir_%03d/snapshot_%03d'%(snum,snum),'mass','star')/h
  pid = pg.readsnap(pathname+'snapdir_%03d/snapshot_%03d'%(snum,snum),'pid','star')
  bob = mass[np.in1d(pid, main_part[:,0].astype('int'))]
  main_part[np.argsort(main_part[np.in1d(main_part[:,0].astype('int'),pid),0]),12] = bob[np.argsort(pid[np.in1d(pid, main_part[:,0].astype('int'))])]*1e10 

  return main_part
Example #5
0
def read_mw_com_snapshot(path, snap_name, n_halo_part):
    """
    Reads MW particles
    """
    pos, vel, pot, ids = reading_snapshots.read_MW_snap_com_coordinates(
        path, snap_name, LMC=True, N_halo_part=n_halo_part, pot=True)
    mass = pygadgetreader.readsnap(path + snap_name, 'mass', 'dm')
    all_ids = pygadgetreader.readsnap(path + snap_name, 'pid', 'dm')
    mw_mass = np.where(all_ids == ids[0])
    return pos, mass[mw_mass]
Example #6
0
def read_sat_com_snapshot(path, snap_name, n_halo_part):
    """
    Read Satellites particles
    """
    pos, vel, lmc_ids = reading_snapshots.read_satellite_snap_com_coordinates(
        path + snap_name, LMC=True, N_halo_part=n_halo_part, pot=True)
    mass = pygadgetreader.readsnap(path + snap_name, 'mass', 'dm')
    ids = pygadgetreader.readsnap(path + snap_name, 'pid', 'dm')
    lmc_mass = np.where(ids == lmc_ids[0])

    return pos, mass[lmc_mass], len(ids)
Example #7
0
def run_caesar(infile, args):
    import yt

    if args['output'] is not None:
        if args['output'].endswith('.hdf5'):
            outfile = args['output']
        else:
            outfile = '%s.hdf5' % args['output']

    elif infile.endswith('.bin') or infile.endswith('.dat'):
        outfile = 'caesar_%s.hdf5' % (infile[:-4])

    else:
        outfile = 'caesar_%s' % (infile)

    from .main import CAESAR
    ds = yt.load(infile)
    if ds.cosmological_simulation == 1:
        obj = CAESAR(yt.load(infile))
    else:
        try:
            import pygadgetreader as pygr
        except ImportError:
            raise ImportError(
                'Whoops! It looks like you need to install pygadgetreader: https://bitbucket.org/rthompson/pygadgetreader/src/default/'
            )

        print('Figuring out the box size for a non-cosmological simulation')
        #find the min/max coordinates and use a bbox
        pos_dm = pygr.readsnap(infile, 'pos', 'dm')
        pos_gas = pygr.readsnap(infile, 'pos', 'gas')
        pos_stars = pygr.readsnap(infile, 'pos', 'stars')

        #BOBBY NOTES: JUST LOAD THIS IN YT4.0 TO AVOID DEPENDENCIES ON PYGR
        maxpos = np.max(
            np.concatenate(
                (pos_stars.flatten(), pos_dm.flatten(), pos_gas.flatten()),
                axis=0))
        minpos = np.min(
            np.concatenate(
                (pos_stars.flatten(), pos_dm.flatten(), pos_gas.flatten()),
                axis=0))
        boxsize = np.max([np.absolute(maxpos), np.absolute(minpos)])
        bbox = [[-boxsize, boxsize], [-boxsize, boxsize], [-boxsize, boxsize]]
        obj = CAESAR(yt.load(infile, bounding_box=bbox))

    obj.member_search(**args)
    obj.save(outfile)
Example #8
0
def check_restr_column(snapfile, los, restr_column, f_total_column=0.9):

    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
    los_particles = get_los_particles(los, gas_pos, hsml)

    los_restr_column = restr_column[los_particles]
    total_column = np.sum(los_restr_column)
    restr_cumsum = np.cumsum(np.sort(los_restr_column)[::-1])
    try:
        i = next(i for i, x in enumerate(restr_cumsum)
                 if x >= f_total_column * total_column)
        return i
    except:
        return -9999
def stellar_age(pathname,snum,main_part): #Add stellar creation time
  h0 = 71
  om_l = 0.734
  om_m = 0.266
  conv = 3.085677581e+19
  age = pg.readsnap(pathname+'snapdir_%03d/snapshot_%03d'%(snum,snum),'age','star')
  test = pg.readheader(pathname+'snapdir_%03d/snapshot_%03d'%(snum,snum),'time')
  print age[age>test]
  for i in np.arange(len(age)):
    age[i] = scipy.integrate.quad(_a_dot_recip, 0,age[i], (h0, om_m, om_l))[0]*conv
  test = scipy.integrate.quad(_a_dot_recip, 0,test, (h0, om_m, om_l))[0]*conv
  #age = np.array([age]) #So that we can easily take the transpose of age to concatenate with mass_progen
  pid = pg.readsnap(pathname+'snapdir_%03d/snapshot_%03d'%(snum,snum),'pid','star')
  bob = age[np.in1d(pid, main_part[:,0].astype('int'))]
  main_part[np.argsort(main_part[:,0].astype('int')),11] = bob[np.argsort(pid[np.in1d(pid, main_part[:,0].astype('int'))])]/3600/24/365/1e9
  #print main_part[np.argsort(main_part[:,0].astype('int')),0],main_part[np.argsort(main_part[:,0].astype('int')),11]
  #main_part[np.argsort(main_part[np.in1d(main_part[:,0].astype('int'),bob[:,0].astype('int')),0])= bob[np.argsort(bob[np.in1d(bob[:,0].astype('int'),bob[:,0].astype('int')),0])

  return main_part
def reorder_jackknifes(N_jk,side_length,dm_path):
    print "Finding the mapping of LGADGET jackknifes to our jackknifes."
    ndivs = int(round(N_jk**(1./3.)))
    step = side_length/ndivs
    mapping = {}
    for i in xrange(0,2): #N_jk):
        pos = pgr.readsnap(dm_path+".%d"%i,"pos","dm",single=True,suppress=True)
        xi,yi,zi = [int(q) for q in np.mean(pos,0)/step]
        jkindex = zi + ndivs*yi + ndivs*ndivs*xi
        mapping[jkindex] = i
    print "\tMapping complete on %d files."%N_jk
    return mapping
Example #11
0
def index_particle(path, snap_name, R_particle, V_particle, time):
    """
    Finds the index of a particle in a given snapshot given it's
    velocity magnitude and position magnitude.

    Input:
    ------

    path: path to snapshot
    snap_name: snapshot base name.
    R_particle: Galactocentric distance of the particle
    V_particle: Galactocentric velocity of the particle
    time: Number of the snapshot.
    """
    # reading snapshot
    pos = readsnap(path + snap_name + '_{:03d}.hdf5'.format(time),'pos', 'dm')
    vel = readsnap(path + snap_name + '_{:03d}.hdf5'.format(time),'vel', 'dm')
    indexes = readsnap(path + snap_name + '_{:03d}.hdf5'.format(time),'pid', 'dm')

    # Computing the radius of all particles
    R = np.sqrt(pos[:,0]**2.0 + pos[:,1]**2.0 + pos[:,2]**2.0)

    # Selecting the desired radius range
    index_r = np.where(np.abs(R-R_particle) <1)[0]
    vel_cut = vel[index_r]
    pos_cut = pos[index_r]
    index_ir = indexes[index_r]

    # Selecting range of velocities
    V = np.sqrt(vel_cut[:,0]**2.0 + vel_cut[:,1]**2.0 + vel_cut[:,2]**2.0)
    index_v = np.where(np.abs(V-V_particle) == min(np.abs(V-V_particle)))[0]
    if len(index_v)==0:
        print('Error: There are not particles at {} kpc with a velocity of {}km/s\n'.format(R_particle, V_particle))
        print('The particles at this distance have the following range of velocities:', min(vel_cut), max(vel_cut))
        return 0
    else:
        print('Initial position (kpc):', pos_cut[index_v])
        print('Initial velocity (km/s):', vel_cut[index_v])
        return index_ir[index_v]
Example #12
0
def jackknife_dm(outpath, DSF, ndivs):
    """
    Jackknife the dark matter particles from the down-sampled
    dark matter catalog.
    """
    #Create the output directory for this function
    os.system("mkdir -p %s" % outpath + "/down_sampled_dm/JK_dm_cats")

    print "Jackknifing DM particles."
    if os.path.exists(outpath + "/info_files/spatial_limits.txt"):
        limits = np.loadtxt(outpath + "/info_files/spatial_limits.txt")
    else:
        raise Exception(
            "Must find spatial limits before jackknifing dark matter particles."
        )

    dx = (limits[0, 1] - limits[0, 0]) / ndivs
    dy = (limits[1, 1] - limits[1, 0]) / ndivs
    dz = (limits[2, 1] - limits[2, 0]) / ndivs

    jkoutbase = outpath + "/down_sampled_dm/JK_dm_cats/jk_dm_cat_%d.txt"
    jkarray = []
    Njks = ndivs**3
    for i in xrange(0, Njks):
        jkarray.append(open(jkoutbase % i, "w"))

    #Read in the dm particles
    dmpath = outpath + "/down_sampled_dm/down_sampled_dm_DSF%d" % DSF
    dm = pgr.readsnap(dmpath, "pos", "dm")

    for i in xrange(0, len(dm)):
        x, y, z = dm[i]
        i = min(np.floor(x / dx), ndivs - 1)
        j = min(np.floor(y / dy), ndivs - 1)
        k = min(np.floor(z / dz), ndivs - 1)
        index = int(k * ndivs * ndivs + j * ndivs + i)
        jkarray[index].write("%e\t%e\t%e\n" % (x, y, z))

    for i in range(Njks):
        jkarray[i].close()
    print "\tJackknifing DM particles complete."
    return
Example #13
0
def calcalate_hmcf_full(outpath,nbins,limits,Nh,halorandoms,dmrandoms,DSF):
    """
    Calcualte the halo-matter correlation function.
    """
    print "Calculating full halo-matter correlation function."
    #Read in the halos
    redpath = outpath+"/reduced_halo_cats/reduced_halo_cat.txt"
    infile = open(redpath,"r")
    halos = np.zeros((int(Nh),3))
    i = 0
    for line in infile:
        if line[0] is "#": continue
        parts = line.split()
        halos[i,:] = float(parts[x_index]),float(parts[y_index]),float(parts[z_index])
        i+=1
    infile.close()

    #Read in the dm particles
    dmpath = outpath+"/down_sampled_dm/down_sampled_dm_DSF%d"%DSF
    dm = pgr.readsnap(dmpath,"pos","dm")

    #Treecorr interface
    config = {'nbins':nbins,'min_sep':limits[0],'max_sep':limits[1]}
    halorandom_cat = treecorr.Catalog(x=halorandoms[:,0],y=halorandoms[:,1],z=halorandoms[:,2],config=config)
    dmrandom_cat   = treecorr.Catalog(x=dmrandoms[:,0],y=dmrandoms[:,1],z=dmrandoms[:,2],config=config)
    halo_cat = treecorr.Catalog(x=halos[:,0],y=halos[:,1],z=halos[:,2],config=config)
    dm_cat = treecorr.Catalog(x=dm[:,0],y=dm[:,1],z=dm[:,2],config=config)

    DD = treecorr.NNCorrelation(config)
    DR = treecorr.NNCorrelation(config)
    RD = treecorr.NNCorrelation(config)
    RR = treecorr.NNCorrelation(config)

    DD.process(halo_cat,dm_cat)
    DR.process(halo_cat,halorandom_cat)
    RD.process(dm_cat,dmrandom_cat)
    RR.process(halorandom_cat,dmrandom_cat)
    DD.write(outpath+"/halomatter_correlation_function/full_hmcf/full_hmcf.txt",RR,DR,RD)

    print "\tHalo-matter correlation function full complete."
    return
Example #14
0
    def _get_simba_property(self,ptype,prop):
        try:
            import pygadgetreader as pygr
        except:
            return self.dd[ptype, prop].astype(MY_DTYPE)
        snapfile = ('%s/%s'%(self.ds.fullpath,self.ds.basename))
        # set up units coming out of pygr
        prop_unit = {'mass':'Msun', 'pos':'kpccm', 'vel':'km/s', 'pot':'Msun * kpccm**2 / s**2', 'rho':'g / cm**3', 'sfr':'Msun / yr', 'u':'K', 'Dust_Masses':'Msun', 'bhmass':'Msun', 'bhmdot':'Msun / yr', 'hsml':'kpccm'}

        # damn you little h!
        if prop == 'mass' or prop == 'pos':
            hfact = 1./self.ds.hubble_constant
        elif prop == 'rho':
            hfact = self.ds.hubble_constant**2
        else:
            hfact = 1.

        # deal with differences in pygr vs. yt/caesar naming
        if ptype == 'bh': ptype = 'bndry'
        if prop == 'temperature': prop = 'u'
        if prop == 'haloid' or prop == 'dustmass' or prop == 'aform' or prop == 'bhmass' or prop == 'bhmdot': prop = self.get_property_name(ptype, prop)
        if ptype == 'dm2': ptype = 'disk'
        if ptype == 'dm3': ptype = 'bulge'

        # read in the data
        data = pygr.readsnap(snapfile, prop, ptype, units=1, suppress=1) * hfact

        # set to doubles
        if prop == 'HaloID' or prop == 'particle_index':  # this fixes a bug in our Gizmo, that HaloID is output as a float!
            data = data.astype(np.int64)
        else:
            data = data.astype(np.float32)

        if prop in prop_unit.keys():
            data = self.ds.arr(data, prop_unit[prop])
        else:
            data = self.ds.arr(data, '')
        return data
Example #15
0
def calc_DdRd_and_RR(dm_path, N_rand=None, M=1.5, config=config_default):
    """Calculate the DdRd and RR terms for a HMCF. This is because
    these terms take the longest, so it should just be done once
    if the HMCF is done for many mass bins.

    Args:
        dm_path (string): Path to DM catalog
        N_rand (int, optional): number of random points to use. If not specified, then 1.5*N_dm is used.
        M (float): Multiplicative factor to go from N_dm to N_rand. Default is 1.5
        config (dictionary): Configuration of the Corrfunc run. Default is
        config_default

    Returns:
        RR (:obj: Corrfunc.theory.DD): Pair count object from Corrfunc
        DdRd (:obj: Corrfunc.theory.DD): Pair count object from Corrfunc
        Dd (:obj: numpy.array): Numpy array containing the dm catalog
        Rh (:obj: numpy.array): Numpy array containing the halo randoms
    """
    boxsize = config["boxsize"]
    nthreads = config["nthreads"]
    bins = config["bins"]
    Xd, Yd, Zd = pgr.readsnap(dm_path, 'pos', 'dm').T
    Xd = Xd.astype('float64')
    Yd = Yd.astype('float64')
    Zd = Zd.astype('float64')
    N_dm = len(Xd)
    if not N_rand: N_rand = int(N_dm * M)
    X1 = np.random.uniform(0, boxsize, N_rand)
    Y1 = np.random.uniform(0, boxsize, N_rand)
    Z1 = np.random.uniform(0, boxsize, N_rand)
    X2 = np.random.uniform(0, boxsize, N_rand)
    Y2 = np.random.uniform(0, boxsize, N_rand)
    Z2 = np.random.uniform(0, boxsize, N_rand)
    RR = DD(0, nthreads, bins, X1, Y1, Z1, X2=X2, Y2=Y2, Z2=Z2)
    DdRd = DD(0, nthreads, bins, Xd, Yd, Zd, X2=X2, Y2=Y2, Z2=Z2)
    return [RR, DdRd, [Xd, Yd, Zd], [X1, Y1, Z1]]
    plt.ylabel(r"$\xi_{\rm hh}$", fontsize=24)
    plt.subplots_adjust(bottom=0.15)
    plt.show()
    plt.clf()

if dohm:
    if calchm:
        for i in range(len(inds)):
            index = inds[i]
            z = zs[i]
            if z > 0.0: continue
            zstring = zstrings[i]
            print "finding HMCF at z=%.2f"%z
            Xh, Yh, Zh, Np, M, Rich = np.genfromtxt(halopath%(index,index), unpack=True)
            N_h = len(Xh)
            Xd, Yd, Zd = pgr.readsnap(dmpath%(index,index,zstring), 'pos', 'dm').T
            Xd = Xd.astype('float64')
            Yd = Yd.astype('float64')
            Zd = Zd.astype('float64')
            N_dm = len(Xd)
            N_rand = int(N_dm*1.5)
            Xr1 = np.random.uniform(0, boxsize, N_rand)
            Yr1 = np.random.uniform(0, boxsize, N_rand)
            Zr1 = np.random.uniform(0, boxsize, N_rand)
            Xr2 = np.random.uniform(0, boxsize, N_rand)
            Yr2 = np.random.uniform(0, boxsize, N_rand)
            Zr2 = np.random.uniform(0, boxsize, N_rand)

            DhDd = DD(0, nthreads, bins, X1=Xh, Y1=Yh, Z1=Zh, 
                      X2=Xd, Y2=Yd, Z2=Zd)
            DhRh = DD(0, nthreads, bins, Xh, Yh, Zh,
Example #17
0
PPos2=''
PVel2=''
PID2=''
PMass2=''
for i in range(N):
	print i
	n=random.randrange(len(PPos))
	PPos2=PPos2+PPos[n]
	del(PPos[n])
	PVel2=PVel2+PVel[n]
	del(PVel[n])
	PID2=PID2+PID[n]
	del(PID[n])
	



#Create the new file, packing up and concatenating all the data
filecontentnew=head+PPos2+filecontent[268+12*N:268+12*N+8]+PVel2+filecontent[276+24*N:276+24*N+8]+PID2+filecontent[284+28*N:284+28*N+4]
	

with open(outfile, mode='wb') as file:
	file.write(filecontentnew)
	file.close()

#This just checks to make sure the file completed correctly
filename=outfile
print pg.readheader(filename,"dmcount")
print pg.readsnap(filename,"mass",'dm')[0]
Example #18
0
from matplotlib import rc
import pylab as plt
import sys
import struct
import pygadgetreader as pg
import random


def getRad(array):
    return pow(pow(array[0], 2) + pow(array[1], 2) + pow(array[2], 2), 0.5)


filename = "original_nfw"

nparts = pg.readheader(filename, "dmcount")
dmmass = pg.readsnap(filename, "mass", 'dm')[0]

bins = [0 + i / 5.0 for i in range(20)]

pos = pg.readsnap(filename, "pos", "dm")
vel = pg.readsnap(filename, "vel", "dm")

radii = []
vels = []
for i in range(nparts):
    radii.append(getRad(pos[i]))
    vels.append(getRad(vel[i]))

M = [0.0 for i in range(len(bins))]
N = [0 for i in range(len(bins))]
vbar = [0.0 for i in range(len(bins))]
def run_treecorr(config,dm_base_file_path,halo_base_file_path,
                 dm_random_path,halo_random_path,
                 redshift,mass_bounds,N_jks,ndivs,side_length,DS,mapping,
                 cf_output_base):
    print "Performing the treecorr analysis."
    lMmin, lMmax = mass_bounds
    step = side_length/ndivs
    
    #Read in the randoms.
    rand_dm = np.loadtxt(dm_random_path)
    rand_halo = np.loadtxt(halo_random_path)
    print rand_dm.shape, rand_halo.shape

    #Loop over all JK pairs
    for i in xrange(0,1):#ndivs):
        for j in xrange(0,1):#ndivs):
            for k in xrange(0,1):#ndivs:
                #Move the DM randoms
                rand_dm[:,0] += i*step #x
                rand_dm[:,1] += j*step #y
                rand_dm[:,2] += k*step #z

                unmapped_dm_jkindex = k + ndivs*j + ndivs*ndivs*i
                dm_jkindex = mapping[unmapped_dm_jkindex]
                dm_parts_all = pgr.readsnap(dm_base_file_path%dm_jkindex,"pos","dm",single=True,suppress=True)
                N_dm = len(dm_parts_all)
                f_keep = 1./DS
                dm_parts = []
                for dmp in range(len(dm_parts_all)):
                    r = np.random.random()
                    if r <f_keep:
                        dm_parts.append(dm_parts_all[dmp])
                dm_parts = np.array(dm_parts)
                print dm_parts_all.shape,dm_parts.shape

                #Loop over the halo indices
                for ii in xrange(0,1):#ndivs):
                    for jj in xrange(0,1):#ndivs):
                        for kk in xrange(0,1):#ndivs):
                            halo_jkindex = kk + ndivs*jj + ndivs*ndivs*ii
                            
                            #Move the halo randoms
                            rand_halo[:,0] += ii*step #x
                            rand_halo[:,1] += jj*step #y
                            rand_halo[:,2] += kk*step #z

                            halos = np.atleast_2d(np.loadtxt(halo_base_file_path%(redshift,lMmin,lMmax,redshift,lMmin,lMmax,halo_jkindex)))
                            #Input data is M Rs X Y Z
                            halo_pos = halos[:,2:] #Just the positions
                            print halo_pos.shape

                            #Perform the actual treecorr analysis
                            dm_cat = treecorr.Catalog(x=dm_parts[:,0].copy(),y=dm_parts[:,1].copy(),z=dm_parts[:,2].copy(),config=config)
                            halo_cat = treecorr.Catalog(x=halo_pos[:,0].copy(),y=halo_pos[:,1].copy(),z=halo_pos[:,2].copy(),config=config)
                            random_dm_cat = treecorr.Catalog(x=rand_dm[:,0].copy(),y=rand_dm[:,1].copy(),z=rand_dm[:,2].copy(),config=config)
                            random_halo_cat = treecorr.Catalog(x=rand_halo[:,0].copy(),y=rand_halo[:,1].copy(),z=rand_halo[:,2].copy(),config=config)
                            print "\tPerforming cross correlation between DM%d and halos%d"%(dm_jkindex,halo_jkindex)
                            DD = treecorr.NNCorrelation(config)
                            DR = treecorr.NNCorrelation(config)
                            RD = treecorr.NNCorrelation(config)
                            RR = treecorr.NNCorrelation(config)
                            DD.process(dm_cat,halo_cat)
                            print "\tDD complete."
                            RR.process(random_dm_cat,random_halo_cat)
                            print "\tRR complete."
                            DR.process(dm_cat,random_dm_cat)
                            print "\tDR complete."
                            RD.process(halo_cat,random_halo_cat)
                            print "\tRD complete."
                            DD.write(cf_output_base%(redshift,lMmin,lMmax,redshift,lMmin,lMmax,dm_jkindex,halo_jkindex),RR,DR,RD)
                            print "\tWriting complete."

                            #Untranslate the halo randoms
                            rand_halo[:,0] -= ii*step #x
                            rand_halo[:,1] -= jj*step #y
                            rand_halo[:,2] -= kk*step #z
                            continue # end kk
                        continue # end jj
                    continue # end ii
                
                #Untranslate the dm randoms
                rand_dm[:,0] -= i*step #x
                rand_dm[:,1] -= j*step #y
                rand_dm[:,2] -= k*step #z
                continue # end k
            continue # end j
        continue # end i
    print "\tCompleted the treecorr analysis."
    return
Example #20
0
#savedir = '/home/sarah/cgm/budgets/data/'
#sim = caesar.load(caesarfile)

h = sim.simulation.hubble_constant

central = 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])
# Don't panic everyone, these are K units - see Mo & White 2002
#gal_tvir = np.array([i.halo.virial_quantities['temperature'].in_units('km**2/s**2') for i in sim.galaxies])
gal_tvir = np.array([i.halo.virial_quantities['temperature'].in_units('K') for i in sim.galaxies])
gal_ssfr = gal_sfr / gal_sm
gal_sm = np.log10(gal_sm)
gal_ssfr = np.log10(gal_ssfr)

dm_mass = readsnap(snapfile, 'mass', 'dm', suppress=1, units=1) / h # in Mo)
gas_mass = readsnap(snapfile, 'mass', 'gas', suppress=1, units=1) / h # in Mo
gas_sfr = readsnap(snapfile, 'sfr', 'gas', suppress=1, units=1) # in Mo/yr
gas_z = readsnap(snapfile, 'z', 'gas', suppress=1, units=1)
gas_nh = readsnap(snapfile, 'nh', 'gas', suppress=1, units=1) # in g/cm^3
gas_delaytime = readsnap(snapfile, 'DelayTime', 'gas', suppress=1)
gas_temp = readsnap(snapfile, 'u', 'gas', suppress=1, units=1) # in K
dust_mass = readsnap(snapfile, 'Dust_Masses', 'gas', suppress=1, units=1) * dust_mass_factor/ h # in Mo
#dust_mass = np.zeros(len(gas_mass))
star_mass = readsnap(snapfile, 'mass', 'star', suppress=1, units=1) / h # in Mo
star_z = readsnap(snapfile, 'z', 'star', suppress=1, units=1)
bh_mass = readsnap(snapfile, 'mass', 'bndry', suppress=1, units=1) / h # in Mo

phases = ['Cool CGM (T < 10^5)', 'Warm CGM (10^5 < T < 10^6)', 'Hot CGM (T > 10^6)',
          'Cool CGM (T < Tphoto)', 'Warm CGM (Tphoto < T < 0.5Tvir)', 'Hot CGM (T > 0.5Tvir)', 
          'ISM', 'Wind', 'Dust', 'Stars', 'Dark matter', 'Total baryons']
This version of the program has the main body completely rewritten in C
"""



from __future__ import division #so that 3/2 = 1.5, but not 1 as in Python2.* 
import pygadgetreader as pgr
import numpy as np
from numpy import pi
from time import time
from scipy import weave
from scipy.weave import converters

t0 = time()
coord = pgr.readsnap ('snapshot_009', 'pos', 1) 
t1 = time() - t0
maxcoord = 64
numofcells = 20
hx, hy, hz = maxcoord/numofcells, maxcoord/numofcells, maxcoord/numofcells
h = hx #now hx = hy = hz = h
numofsph = numofcells + 1 # = number of knots
R = 1.999*hx
V = 4/3*pi*R**3 
Mpc = 3.085678e24 #in sm
M_S = 1.989e33 #in g
UnitMass = 1e10*M_S 

#nop = int(np.size(coord)/3) #number of particles
nop = int(1e7);
Example #22
0
def read_snap_coordinates(path,
                          snap,
                          N_halo_part,
                          com_frame='MW',
                          galaxy='MW'):
    """
    Returns the MW properties.
    
    Parameters:
    path : str
        Path to the simulations
    snap : name of the snapshot
    LMC : Boolean
        True or False if LMC is present on the snapshot.
    N_halo_part : int
        Number of particles in the MW halo.
    pot : boolean
        True or False if you want the potential back.
    com_frame : str
        Where the coordinates will be centered galactocentric (MW), on the
        satellite (sat), or in the LSR (LSR)
    galaxy : str
        galaxy coordinates to be returned (MW) or (sat)
    Returns:
    --------
    MWpos : 
    MWvel : 
    MWpot : 
    MWmass : 
    
    """
    # Load data
    all_pos = readsnap(path + snap, 'pos', 'dm')
    all_vel = readsnap(path + snap, 'vel', 'dm')
    all_ids = readsnap(path + snap, 'pid', 'dm')
    all_pot = readsnap(path + snap, 'pot', 'dm')
    all_mass = readsnap(path + snap, 'mass', 'dm')

    print("Loading MW particles and LMC particles")

    if galaxy == 'MW':
        print('Loading MW particles')
        pos, vel, ids, pot, mass = host_particles(all_pos, all_vel, all_ids,
                                                  all_pot, all_mass,
                                                  N_halo_part)

    elif galaxy == 'sat':
        print('Loading satellite particles')
        pos, vel, ids, pot, mass = sat_particles(all_pos, all_vel, all_ids,
                                                 all_pot, all_mass,
                                                 N_halo_part)

    if com_frame == 'MW':
        print('Computing coordinates in the MW COM frame')
        pos_disk = readsnap(path + snap, 'pos', 'disk')
        vel_disk = readsnap(path + snap, 'vel', 'disk')
        pot_disk = readsnap(path + snap, 'pot', 'disk')
        pos_cm, vel_cm = com.com_disk_potential(pos_disk, vel_disk, pot_disk)

    elif com_frame == 'sat':
        print('Computing coordinates in the satellite COM frame')
        if galaxy == 'MW':
            LMC_pos, LMC_vel, LMC_ids, LMC_pot, LMC_mass = sat_particles(
                all_pos, all_vel, all_ids, all_pot, all_mass, N_halo_part)
            pos_cm, vel_cm = com.CM(LMC_pos, LMC_vel, LMC_mass)
        else:
            # Guess com to re-center halo and precisely compute the COM
            rlmc = np.sqrt(pos[:, 0]**2 + pos[:, 1]**2 + pos[:, 2]**2)
            truncate = np.where(rlmc < 100)[0]
            # First COM guess
            com1 = com.COM(pos[truncate], vel[truncate],
                           np.ones(len(truncate)))
            pos_recenter = com.re_center(pos[truncate], com1[0])
            print(com1[0])
            pos_cm, vel_cm = com.CM(pos_recenter, vel[truncate],
                                    np.ones(len(mass[truncate])) * mass[0])
            print(pos_cm, vel_cm)
            pos_cm += com1[0]

    elif com_frame == 'LSR':
        print('Computing coordinates in the LSR frame')
        pos_disk = readsnap(path + snap, 'pos', 'disk')
        vel_disk = readsnap(path + snap, 'vel', 'disk')
        pot_disk = readsnap(path + snap, 'pot', 'disk')
        pos_cm, vel_cm = com.com_disk_potential(pos_disk, vel_disk, pot_disk)

        pos_LSR = np.array([-8.34, 0, 0])
        vel_LSR = np.array([11.1, 232.24, 7.25])

        print(pos_LSR)
        print(vel_LSR)

    print(pos_cm, vel_cm)
    pos_new = com.re_center(pos, pos_cm)
    vel_new = com.re_center(vel, vel_cm)
    return pos_new, vel_new, pot, mass, ids
Example #23
0
def progen_finder(obj_current, obj_progens, snap_current, snap_progens):
    """Function to find the most massive progenitor of each CAESAR
    object in the previous snapshot.

    Parameters
    ----------
    obj_current : :class:`main.CAESAR`
        Will search for the progenitors of the objects in this object.
    obj_progens : :class:`main.CAESAR`
        Looking for progenitors in this object.
    snap_current : str
        Name (including path) of the primary snapshot
    snap_progens : yt dataset
        Name (including path) of the secondary snapshot

    """
    try:
        from pygadgetreader import readsnap
    except:
        raise Exception('Please install pyGadgetReader for quick PID reads!\n' \
                        'https://bitbucket.org/rthompson/pygadgetreader')

    caesar_file = snap_current.outfile

    ## halos (use DM)
    data_type  = 'halo'
    if obj_current.nhalos == 0 or obj_progens.nhalos == 0:
        mylog.warning('0 %s found!  skipping progen' % group_type[data_type])
    elif not check_if_progen_is_present(data_type, caesar_file):
        nobjs        = obj_current.nhalos
        PID_current  = readsnap(snap_current.snap, 'pid', 1)
        PID_progens  = readsnap(snap_progens.snap, 'pid', 1)
        list_current = obj_current.global_particle_lists.halo_dmlist
        list_progens = obj_progens.global_particle_lists.halo_dmlist

        prev_indexes = find_progens(PID_current, PID_progens,
                                    list_current, list_progens,
                                    nobjs)
        write_progen_data(obj_current, prev_indexes,
                          data_type, caesar_file)
    else:
        mylog.warning('%s progen data already present, skipping!' % data_type)

        
    if not obj_current.simulation.baryons_present:
        return

    ## galaxies (use stars)
    data_type = 'galaxy'
    if obj_current.ngalaxies == 0 or obj_progens.ngalaxies == 0:
        mylog.warning('0 %s found! skipping progen' % group_types[data_type])
    elif not check_if_progen_is_present(data_type, caesar_file):
        nobjs        = obj_current.ngalaxies
        PID_current  = readsnap(snap_current.snap, 'pid', 4)
        PID_progens  = readsnap(snap_progens.snap, 'pid', 4)
        list_current = obj_current.global_particle_lists.galaxy_slist
        list_progens = obj_progens.global_particle_lists.galaxy_slist

        prev_indexes = find_progens(PID_current, PID_progens,
                                    list_current, list_progens,
                                    nobjs)
        write_progen_data(obj_current, prev_indexes, data_type, caesar_file)
    else:
        mylog.warning('%s progen data already present, skipping!' % data_type)


    ## clouds (use gas)
    data_type = 'cloud'
    if obj_current.nclouds == 0 or obj_progens.nclouds == 0:
        mylog.warning('0 %s found! skipping progen' % group_types[data_type])
    elif not check_if_progen_is_present(data_type, caesar_file):
        nobjs        = obj_current.nclouds
        PID_current  = readsnap(snap_current.snap, 'pid', 0)
        PID_progens  = readsnap(snap_progens.snap, 'pid', 0)
        list_current = obj_current.global_particle_lists.cloud_glist
        list_progens = obj_progens.global_particle_lists.cloud_glist

        prev_indexes = find_progens(PID_current, PID_progens,
                                    list_current, list_progens,
                                    nobjs)
        write_progen_data(obj_current, prev_indexes, data_type, caesar_file)
    else:
        mylog.warning('%s progen data already present, skipping!' % data_type)
    #lmax = sys.argv[3]
    lmax = 2
    #_halo = sys.argv[4]
    #r_s = sys.argv[5]
    #halo_name = sys.argv[6]

    rmax = 300

    #path_snap = './test_halo/LMC6_6.25M_vir_000'
    path_snap = './test_halo/MW2_40M_vir_000'
    # Read coefficients
    S, T = orbit.read_coefficients(coeff_path, 1, nmax, lmax)
    #print(S, T)
    # Compute densities

    mass = pygadgetreader.readsnap(path_snap, 'mass', 'dm')
    M_halo = np.sum(mass)
    print(M_halo)
    r_s = 40.82
    #r_s = 25.158
    #r_s = 2.5158
    G = 43007.1
    print(S[0])
    # read sim
    #pos = pygadgetreader.readsnap(path_snap, 'pos', 'dm')
    #vel = pygadgetreader.readsnap(path_snap, 'vel', 'dm')
    #pot = pygadgetreader.readsnap(path_snap, 'pot', 'dm')

    pos = pygadgetreader.readsnap(path_snap, 'pos', 'dm')
    pot = pygadgetreader.readsnap(path_snap, 'pot', 'dm')
    rcm = [-0.01064566, -0.00043313, 0.00702804]
Example #25
0
with open(infile, mode='rb') as file:
    filecontent = file.read()
    file.close()

#f is the SIDM fraction, Ndm is the total number of particles you want. This script takes the random NFW profile (as input) and makes half the dark matter particles non-interacting. It then adjusts the masses so as to give the desired SIDM fraction (by mass).
f = float(sys.argv[1])
Ndm = -1  #-1 for default (leave total number of particles unchanged). This removes the random sampling.

#This array lists the number of particles of each species
N = struct.unpack("iiiiii", filecontent[4:28])
print N

if (Ndm == -1):
    #This leaves the number of particles unchanged, and therefore we don't rely on the random sampling.
    Ndm = N[1]
    packN = struct.pack('iiiiii', 0, int(Ndm * 0.5), 0, 0,
                        Ndm - int(Ndm * 0.5), 0)
    M = struct.unpack("dddddd", filecontent[28:28 + 8 * 6])
    print M
    packM = struct.pack('dddddd', 0, 2 * f * M[1], 0, 0, 2 * (1 - f) * M[1], 0)
    filecontentnew = filecontent[:4] + packN + packM + filecontent[
        76:100] + packN + filecontent[124:]
with open(outfile, mode='wb') as file:
    file.write(filecontentnew)
    file.close()

#This just checks to make sure the file completed correctly
print pg.readheader(outfile, "dmcount")
print pg.readheader(outfile, "starcount")
print pg.readsnap(outfile, "mass", 'dm')[0]
Example #26
0
def collect_group_IDs(obj, data_type, part_type, snap_dir):
    """Collates list of particle and associated group IDs for all specified objects.
    Returns particle and group ID lists, and a hash list of indexes for particle IDs 
    corresponding to the starting index of each group.

    Parameters
    ----------
    obj : :class:`main.CAESAR`
        Caesar object for which to collect group IDs
    data_type : str
        'halo', 'galaxy', or 'cloud'
    part_type : str
        Particle type
    snap_dir : str
        Path where snapshot files are located; if None, uses obj.simulation.fullpath

    """
    # read in particle IDs
    import pygadgetreader as pygr
    if snap_dir is None:
        #snapfile = obj.simulation.fullpath.decode('utf-8')+'/'+obj.simulation.basename.decode('utf-8')
        if isinstance(obj.simulation.fullpath,str) & isinstance(obj.simulation.basename,str):
            snapfile = obj.simulation.fullpath+'/'+obj.simulation.basename
        else:
            snapfile = obj.simulation.fullpath.decode('utf-8')+'/'+obj.simulation.basename.decode('utf-8')

    else:
        snapfile = snap_dir+'/'+obj.simulation.basename
    all_pids = np.array(pygr.readsnap(snapfile,'pid',part_type,suppress=1),dtype=np.uint64)

    from caesar.fubar_halo import plist_dict
    if data_type == 'halo':
        part_list = 'obj.halos[i].%s'%plist_dict[part_type]
        ngroups = len(obj.halos)
    elif data_type == 'galaxy':
        part_list = 'obj.galaxies[i].%s'%plist_dict[part_type]
        ngroups = len(obj.galaxies)
    elif data_type == 'cloud':
        part_list = 'obj.clouds[i].%s'%plist_dict[part_type]
        ngroups = len(obj.clouds)

    # count number of total particles in groups
    npart = 0
    for i in range(ngroups):
        mylist = eval(part_list)
        npart += len(mylist)

    # fill particle and group ID lists
    pids = np.zeros(npart,dtype=np.int64)
    gids = np.zeros(npart,dtype=np.int32)
    pid_hash = np.zeros(ngroups,dtype=np.int64)
    count = 0
    for i in range(ngroups):
        mylist = eval(part_list)
        pids[count:count+len(mylist)] = all_pids[mylist]
        gids[count:count+len(mylist)] = np.full(len(mylist),i)
        pid_hash[i] = count
        count += len(mylist)
    pid_hash = np.append(pid_hash,npart+1)

    return ngroups, pids, gids, pid_hash
def extract_orbit(snap, id_p, i):
    pos = pygadgetreader.readsnap(snap + '_{:0>3d}'.format(i), 'pos', 'dm')
    ids = pygadgetreader.readsnap(snap + '_{:0>3d}'.format(i), 'pid', 'dm')
    particles = np.in1d(ids, id_p)
    pos_orbit = pos[particles]
    return pos_orbit
Example #28
0
    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

cos_rho = cos_rho[cos_M > mlim]
cos_rho = (np.repeat(cos_rho, ngals_each) * h ) * (1+redshift)

los = np.array([pos[:2].copy(), ]*8)
los[0][0] += cos_rho[sample_gal]
los[1][0] += (cos_rho[sample_gal] / sqrt2); los[1][1] += (cos_rho[sample_gal] / sqrt2)
los[2][1] += cos_rho[sample_gal]
Example #29
0
def create_df(pathname,hist,hnum,dm):
  h0 = 71
  om_l = 0.734
  om_m = 0.266
  conv = 3.085677581e+19
  numcores = 16
  numsnaps = 185
  massp = 1.67262178e-24 #in grams
  gamma = 5./3.
  kb = 1.3806e-26 #in km^2*g/s^2
  for i in np.arange(len(hist)):
    print i
    if hist[184-i,1] != 0:
      switch = 0
      for j in np.arange(numcores): #Bring together all the AHF files for the snapshot
        temph = glob.glob(pathname+'analysis/ahf_snap%03d/ahf.snap_%03d.%04d.z*.*.AHF_halos'%(i,i,j))
        temph = str(temph).strip('[]').replace("'","")
        h = np.genfromtxt(temph)
        temppr = glob.glob(pathname+'analysis/ahf_snap%03d/ahf.snap_%03d.%04d.z*.*.AHF_profiles'%(i,i,j))
        temppr = str(temppr).strip('[]').replace("'","")
        p = np.genfromtxt(temppr)
        if switch == 0 and len(h) >0:
          halo = h
          switch = 1
        if switch == 1:
	  try:
            halo = np.vstack((halo,h))
          except:
	    print "nothing there"
      for j in np.arange(len(halo)): #Find our halo from all the halos of the snapshot
       try:
        if halo[j,0] == hist[184-i,1]:
          mvir = halo[j,3]/.71
	  vmax = halo[j,16]
          xcen = halo[j,5]/1000. #Grab relevant data (can always add more later)
          ycen = halo[j,6]/1000.
          zcen = halo[j,7]/1000.
          rvir = halo[j,11]/1000./.71
	  rmax = halo[j,12]/1000./.71
	  rhalf = 0
          sname = "%ssnapdir_%03d/snapshot_%03d"%(pathname,i,i)
	  bsize = pg.readheader(sname, 'boxsize')
	  if bsize > 25: #mfm007 is in kpc while everything else (even gizdm007) is in Mpc. This standardizes all that.
	    kfix = 1000
	  else:
	    kfix = 1
          red = pg.readheader(sname, 'redshift')
	  a = pg.readheader(sname, 'time')
          yr = scipy.integrate.quad(_a_dot_recip, 0,a, (h0, om_m, om_l))[0]*conv/(60*60*24*365*1e9)
	  dpid = pg.readsnap(sname, 'pid', 'dm')
          dmass = pg.readsnap(sname, 'mass', 'dm')*1.e10/.71
          dpos = pg.readsnap(sname, 'pos', 'dm')/kfix
	  dvel = pg.readsnap(sname, 'vel', 'dm')
          ddiff = np.sqrt((dpos[:,0]-xcen)**2+(dpos[:,1]-ycen)**2+(dpos[:,2]-zcen)**2)/.71
	  dpos = dpos[ddiff<rvir]
	  dvel = dvel[ddiff<rvir]
	  dmass = dmass[ddiff<rvir]
          dpid = dpid[ddiff<rvir]
	  ddiff = ddiff[ddiff<rvir]
	  rhalf = 0
	  hdf = pd.HDFStore('%sanalysis/dataframes/halo%s%s_giz%s_snap%03d.h5'%(pathname,hnum,res,ver,i))
	  hdf.put('particles/dm',pd.DataFrame(np.hstack((dpos,np.matrix(ddiff).T,np.matrix(dmass).T)), index = dpid, columns = (['x','y','z','r','mass'])))
	  if dm == 0:
            mstar = halo[j,64]/.71
            zarray = pg.readsnap(sname, 'zarray', 'gas')
            he_mf = zarray[:,1] #Helium mass fraction
            y_he = he_mf/(4*(1-he_mf))
            ne = pg.readsnap(sname, 'ne', 'gas') #Electron Abundance
            mu = (1+4*y_he)/(1+y_he+ne)
            mmw = mu*massp #mean molecular weight
            u = pg.readsnap(sname,'u','gas') #specific internal energy in km^2/s^2
            temp = mmw * (gamma-1.)*u/kb #temperature of gas
	    gpid = pg.readsnap(sname, 'pid', 'gas')
	    spid = pg.readsnap(sname, 'pid', 'star')
            gmass = pg.readsnap(sname, 'mass', 'gas')*1.e10/.71
            smass = pg.readsnap(sname, 'mass', 'star')*1.e10/.71
            gpos = pg.readsnap(sname, 'pos', 'gas')/kfix
            spos = pg.readsnap(sname, 'pos', 'star')/kfix
            gvel = pg.readsnap(sname, 'vel', 'gas')/kfix
            svel = pg.readsnap(sname, 'vel', 'star')/kfix
            sfr = pg.readsnap(sname, 'sfr', 'gas')
	    sft = pg.readsnap(sname, 'age', 'star')
            tmp = np.array([0,0])
	    gdiff = np.sqrt((gpos[:,0]-xcen)**2+(gpos[:,1]-ycen)**2+(gpos[:,2]-zcen)**2)/.71
            sdiff = np.sqrt((spos[:,0]-xcen)**2+(spos[:,1]-ycen)**2+(spos[:,2]-zcen)**2)/.71
	    gpos = gpos[gdiff<rvir]
	    gvel = gvel[gdiff<rvir]
	    gmass = gmass[gdiff<rvir]
	    temp = temp[gdiff<rvir]
	    sfr = sfr[gdiff<rvir]
            gpid = gpid[gdiff<rvir]
	    gdiff = gdiff[gdiff<rvir]
	    spos = spos[sdiff<rvir]
  	    svel = svel[sdiff<rvir]
	    smass = smass[sdiff<rvir]
	    sft = sft[sdiff<rvir]
            spid = spid[sdiff<rvir]
	    sdiff = sdiff[sdiff<rvir]
            sortsdiff = sdiff[np.argsort(sdiff)]
            cumsmass = np.cumsum(smass[np.argsort(sdiff)])
            q = 0
            print len(sortsdiff), mstar, max(cumsmass)
            while (cumsmass[q]<=mstar/2):
              rhalf = sortsdiff[q]
              q = q+1 
	    hdf.put('particles/gas',pd.DataFrame(np.hstack((gpos,np.matrix(gdiff).T,np.matrix(gmass).T,np.matrix(temp).T,np.matrix(sfr).T)), index = gpid, columns = (['x','y','z','r','mass','temp','sfr'])))
	    hdf.put('particles/star',pd.DataFrame(np.hstack((spos,np.matrix(sdiff).T,np.matrix(smass).T,np.matrix(sft).T)), index = spid, columns = (['x','y','z','r','mass','sft'])))
	  props = np.array([[xcen,ycen,zcen,rvir,rhalf,rmax,vmax,red,yr]])
	  hdf.put('props',pd.DataFrame(props,index = [halo[j,0]], columns = (['halox','haloy','haloz','rvir','rhalf','rmax','vmax','redshift','time'])))
	  hdf.close()

       except Exception,f:
          print f
	  props = np.array([[xcen,ycen,zcen,rvir,rhalf,rmax,vmax,red,yr]])
	  print halo[j,0], props
	  hdf = pd.HDFStore('%sanalysis/dataframes/halo%s%s_giz%s_snap%03d.h5'%(pathname,hnum,res,ver,i))
	  hdf.put('props',pd.DataFrame(props,index = [halo[j,0]], columns = (['halox','haloy','haloz','rvir','rhalf','rmax','vmax','redshift','time'])))
	  hdf.close()
          print "MISS"
Example #30
0
def radpro(pathname,sname,hist,dmo,rhalf,rmax):
  global i, grain, clr, count,conv
  numcores = 16
  massp = 1.67262178e-24 #in grams
  gamma = 5./3.
  kb = 1.3806e-26 #in km^2*g/s^2 
  G = 4.3e-6 #in kpc/M_sun (km/s)^2   
  switch = 0
  for j in np.arange(numcores): #Bring together all the AHF files for the snapshot
    temph = glob.glob(pathname+'ahf_snap%03d/ahf.snap_%03d.%04d.z*.*.AHF_halos'%(i,i,j)) 
    temph = str(temph).strip('[]').replace("'","")
    h = np.genfromtxt(temph)
    temppr = glob.glob(pathname+'ahf_snap%03d/ahf.snap_%03d.%04d.z*.*.AHF_profiles'%(i,i,j))
    temppr = str(temppr).strip('[]').replace("'","")
    p = np.genfromtxt(temppr)
    if switch == 0:
      halo = h
      switch = 1
    if switch == 1:
      try:
        halo = np.vstack((halo,h))
      except:
        print "nothing there"
  for j in np.arange(len(halo)): #Find our halo from all the halos of the snapshot
    if halo[j,0] == hist[184-i,1]:
      mstar = halo[j,64]
      mvir = halo[j,3]
      vmax = halo[j,16]
      xcen = halo[j,5] #Grab relevant data (can always add more later)
      ycen = halo[j,6]
      zcen = halo[j,7]
      rvir = halo[j,11]/.71
      bsize = pg.readheader(sname, 'boxsize')
      if bsize > 25: #mfm007 is in kpc while everything else (even gizdm007) is in Mpc. This standardizes all that.
        kfix = 1
      else:
	kfix = 1000
      mass = pg.readsnap(sname, 'mass', 'dm')*1.e10/.71
      pos = pg.readsnap(sname, 'pos', 'dm')*kfix
      tmp = np.array([0,0])
      diff = np.sqrt((pos[:,0]-xcen)**2+(pos[:,1]-ycen)**2+(pos[:,2]-zcen)**2)/.71
      mass = mass[diff<=rvir]
      diff = diff[diff<=rvir]
      cum = np.cumsum(mass[np.argsort(diff)])

      print xcen,ycen,zcen, rvir,pos
      binz = np.logspace(np.log10(.06),np.log10(rvir),grain)
      massall, bin_edge = np.histogram( diff,bins=binz, weights =mass) 
      x = 10**(np.log10(binz[1:])-np.log10(binz[1]/binz[0])/2)
      if dmo == 0:
        rmax = halo[j,12]/.71
        gmass = pg.readsnap(sname, 'mass', 'gas')*1.e10/.71
        smass = pg.readsnap(sname, 'mass', 'star')*1.e10/.71
        gpos = pg.readsnap(sname, 'pos', 'gas')*kfix
        spos = pg.readsnap(sname, 'pos', 'star')*kfix
        sdiff = np.sqrt((spos[:,0]-xcen)**2+(spos[:,1]-ycen)**2+(spos[:,2]-zcen)**2)/.71
        gdiff = np.sqrt((gpos[:,0]-xcen)**2+(gpos[:,1]-ycen)**2+(gpos[:,2]-zcen)**2)/.71
	smass = smass[sdiff<=rvir]
	gmass = gmass[gdiff<=rvir]
	sdiff = sdiff[sdiff<=rvir]
	gdiff = gdiff[gdiff<=rvir]
	scum = np.cumsum(smass[np.argsort(sdiff)])
	gcum = np.cumsum(gmass[np.argsort(gdiff)])
	totdiff = np.append(np.append(diff,sdiff),gdiff)
	totmass = np.append(np.append(mass,smass),gmass)
	totcum = np.cumsum(totmass[np.argsort(totdiff)])
	irhalf = abs(scum-sum(smass)/2).argmin()
	rhalf = sdiff[np.argsort(sdiff)][irhalf]
	itotrhalf = abs(np.sort(totdiff)-rhalf).argmin()
	print 'rhalf is %f'%rhalf
	irmax = np.sqrt(G*totcum/np.sort(totdiff)).argmax()
	rmax = totdiff[np.argsort(totdiff)][irmax]
	print 'rmax is %f'%rmax


	#print len(sdiff), binz, len(smass)
        gmassall, bin_edge = np.histogram( gdiff,bins=binz, weights =gmass) 
        smassall, bin_edge = np.histogram( sdiff,bins=binz, weights =smass)
	#irhalf = (abs(bin_edge-max(bin_edge[np.cumsum(smassall)<=sum(smassall)/2]))).argmin()

        #print bin_edge[np.cumsum(smassall)<=sum(smassall)/2],rvir,sum(smassall)#sum(smassall), np.cumsum(smassall) #x[irhalf],np.sqrt(G*sum(massall[:irhalf+1])/x[irhalf])
        ax1.scatter(rhalf,np.sqrt(G*totcum[itotrhalf]/rhalf),marker='s',s = 80,color='%s'%clr[count],label='%s'%(hnum[w]))
        #ax1.scatter(rmax,np.sqrt(G*totcum[irmax]/rmax),marker='D',s = 80,color='%s'%clr[count],label='%s Rmax'%(hnum))
        return massall, gmassall, smassall, x, rhalf,rmax
      else:
        massall = 0.83120300751*massall
	irhalf = (abs(np.sort(diff)-rhalf)).argmin()
	irmax = (abs(np.sort(diff)-rmax)).argmin()
	print np.sqrt(G*cum[irhalf]/rhalf), np.sqrt(G*cum[irmax]/rmax)
	ax1.scatter(rhalf,np.sqrt(G*cum[irhalf]/rhalf),marker='^',s = 80,color='%s'%clr[count],label='%s DMO'%(hnum[w]))
	#ax1.scatter(rmax,np.sqrt(G*cum[irmax]/rmax),marker='v',s = 80,color='%s'%clr[count],label='%s DMO Rmax'%(hnum))
        count +=1
        return massall, x
Example #31
0
print N
print M
filecontentnew=filecontent[:264]

print len(filecontentnew)

filecontentnew=filecontentnew+struct.pack("i",12*N)
filecontentnew=filecontentnew+PPos2
print len(PPos2)/12
filecontentnew=filecontentnew+struct.pack("i",12*N)
filecontentnew=filecontentnew+struct.pack("i",12*N)
filecontentnew=filecontentnew+PVel2
filecontentnew=filecontentnew+struct.pack("i",12*N)
filecontentnew=filecontentnew+struct.pack("i",4*N)
filecontentnew=filecontentnew+PID2
filecontentnew=filecontentnew+struct.pack("i",4*N)
#filecontentnew=filecontentnew+struct.pack("i",4*N)
#for i in range(N):filecontentnew=filecontentnew+struct.pack("f",M[1])
#filecontentnew=filecontentnew+struct.pack("i",4*N)

with open(outfile, mode='wb') as file:
	file.write(filecontentnew)
	file.close()

#This just checks to make sure the file completed correctly
filename=outfile
print pg.readheader(filename,"dmcount")
print pg.readheader(filename,"time")
print pg.readheader(filename,"redshift")
print pg.readsnap(filename,"mass","dm")[0]
Example #32
0
    ids_sorted_snap2 = ids_snap2[sorted_ids2]
    pos_snap2_s = pos_snap2[sorted_ids2]
    vel_snap2_s = vel_snap2[sorted_ids2]
    mass_snap2_s = mass_snap2[sorted_ids2]

    common_ids = np.isin(ids_sorted_snap2, ids_sorted_snap1)

    return w_sorted, pos_snap2_s[common_ids], vel_snap2_s[
        common_ids], mass_snap2_s[common_ids]


if __name__ == "__main__":

    snapshot = sys.argv[1]

    pp = readsnap(snapshot, 'pos', 'dm')
    vv = readsnap(snapshot, 'vel', 'dm')
    mass = readsnap(snapshot, 'mass', 'dm')
    Epp = readsnap(snapshot, 'pot', 'dm')
    ids = readsnap(snapshot, 'pid', 'dm')

    rr = np.sqrt(pp[:, 0]**2 + pp[:, 1]**2 + pp[:, 2]**2)

    # truncating the halo

    r_cut = index = np.where(rr < 100)[0]

    pp = pp[r_cut]
    rr = rr[r_cut]
    vv = vv[r_cut]
    mass = mass[r_cut]
Example #33
0
import numpy as np
from pygadgetreader import readsnap
from save_new_dataset import make_new_dataset, prepare_out_file

if __name__ == '__main__':

    model = 'm100n1024'
    wind = 's50'
    snap = '151'
    verbose = 2
    minT = 5.5

    sample_dir = f'/disk04/sapple/cgm/absorption/ml_project/data/samples/'
    snapfile = f'{sample_dir}{model}_{wind}_{snap}.hdf5'
    output_file = f'{sample_dir}{model}_{wind}_{snap}_minT_{minT}.hdf5'

    gas_temp = readsnap(snapfile, 'u', 'gas', suppress=1, units=1)  # in K
    temp_mask = (gas_temp > 10**minT)

    plist = np.arange(len(gas_temp))[temp_mask]

    numpart = np.zeros(6, dtype='int')
    numpart[0] = len(plist)

    prepare_out_file(snapfile, output_file, numpart)
    make_new_dataset(snapfile, output_file, plist, verbose)