示例#1
0
    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[:]
                 'explicit_feedback_256_soft', \
#                 'explicit_feedback_256_soft_old_dens', \
#                 'explicit_feedback_512', \
                 #'explicit_feedback_512_soft_amd']:         #gas_prod1_runs', 'gas_cool_runs', 'gas_adiab_runs', 'dm_runs']:



                ]:
    print "\n\n\n"
    print '../' + run_base + '/output/snap*'
    snaplist = glob.glob('../' + run_base + '/output/snap*')
    n_snaps = len(snaplist)
    print snaplist
    for snapnum in [11]:  #range(n_snaps):        #range(13):
        cat = subf.subfind_catalog('../' + run_base + '/',
                                   snapnum,
                                   keysel=['GroupPos', 'GroupLenType'])

        for groupnr in range(np.min([10, len(cat.GroupLenType[:, 0])])):
            if cat.GroupLenType[groupnr, 4] > 500:
                center = cat.GroupPos[groupnr, :]
                print center

                fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(3, 1))

                image  = stellar_images.stellar_image( '../'+run_base+'/output/', snapnum, center=center, \
                                                               xrange=[-0.01,0.01], yrange=[-0.01,0.01], pixels=npixels , \
                                                               cosmo_wrap=True, massmap=True, dust=True, cosmo=True,\
                                                               maxden=1.5e6, dynrange=1e3, projaxis=0, unit_length_mpc=True)

                ax1.imshow(image)
smf2 = np.zeros( n_bins )

# ============================================== #
scalefactors = illustris.load_scalefactors()
redshifts = 1.0/scalefactors - 1.0
snapshots = np.arange(redshifts.shape[0])
volume=75.0 ** 3 /(little_h * little_h * little_h)				#  volume in Mpc^3
# ============================================== #

fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(1, 1, 1)

for redshift in target_redshifts:
    diff = np.abs( redshift - redshifts )
    snap = snapshots[ diff == diff.min() ]
    cat = readsubfHDF5.subfind_catalog(dir, snap[0], keysel=['SubhaloMassType', 'SubhaloMassInRadType'])
    stellar_masses = cat.SubhaloMassType[:,4] * 1e10 / little_h
    stellar_masses2= cat.SubhaloMassInRadType[:,4] * 1e10 / little_h



    output = open('./data/stellar_mass_function_z'+str(redshift)+'.txt', 'w')
    output.write('# stellar mass function data.  '+author+'  '+date+'  \n')
    output.write('# col1 = stellar mass (in solar masses) \n')
    output.write('# col2 = number density (in #/Mpc^3/dex)  -- measured with total stellar mass\n')
    output.write('# col3 = number density (in #/Mpc^3/dex)  -- measured with stellar mass in twice the stellar half mass radius\n')
    output.write('# col4 = raw number of galaxies in each bin for *** col 2 *** mass function\n')
    output.write('# col5 = raw number of galaxies in each bin for *** col 3 *** mass function\n')

    output.write('\n')
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

        gal_count = np.zeros(n_bins)
        halo_mass_func = np.zeros(n_bins)
示例#5
0
fig,ax=plt.subplots( figsize=(6,5))

for run in ['explicit_feedback_256', 'explicit_feedback_256_soft', 'explicit_feedback_512', 'explicit_feedback_512_soft_amd']:

    snaplist = glob.glob( '../'+run+'/output/snapdir_*' )
    print snaplist
    snaparray = range( len( snaplist) ) 
    print snaparray
#    sys.exit()
#    snaparray = range( 13 ) 

    for snapnum in snaparray:

        fig_,ax_=plt.subplots( figsize=(6,5))
        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=['SubhaloMassInRadType', 'SubhaloMassType', 'SubhaloGrNr', 'GroupFirstSub' ] )

        subhalo_number = range( cat.SubhaloGrNr.shape[0] )

        is_central = subhalo_number == cat.GroupFirstSub[cat.SubhaloGrNr] #  cat.SubhaloGrNr == 


        stellar_mass = np.array( cat.SubhaloMassInRadType[is_central,4] ) * 1e10 / little_h
        halo_mass    = np.array( cat.SubhaloMassType[is_central,1] )  * 1e10 / little_h
        smhm = stellar_mass / halo_mass 

        z_str = "{:.1f}".format( header.redshift )
        z_str = r"$\mathrm{z="+z_str+"}$"

#        gal_count = np.zeros( n_bins )
#        halo_mass_func = np.zeros( n_bins )
示例#6
0
for run in [     'explicit_feedback_256', \
                 'explicit_feedback_256_soft', \
                 'explicit_feedback_512', \
                 'explicit_feedback_512_soft_amd']:
    snaplist = glob.glob('../' + run_base + '/output/snap*')
    n_snaps = len(snaplist)
    print snaplist
    snaparray = range(len(snaplist))

    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=['SubhaloMassInRadType'])

        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

        z_str = "{:.1f}".format(header.redshift)
        z_str = r"$\mathrm{z=" + z_str + "}$"

        print header.boxsize

        masses = np.array(cat.SubhaloMassInRadType[:, 4]) * 1e10 / little_h
        #masses = np.array( cat.GroupMass[:] ) * 1e10 / little_h
示例#7
0
def stellar_image(dir='./', snapnum=0, band_ids = [9,10,11], 
                  cosmo=False, 
                  this_file=None, center=None,
                  snapbase='snapshot',
                  illustris_tng=False,
                  tng_subnr=None,
                  tng_groupnr=None,
                  cosmo_wrap=False,
                  massmap=False,
                  projaxis=0,
                  **kwargs):

    if this_file==None:
        snap_ext = "000"+str(snapnum)
        snap_ext = snap_ext[-3:]
        this_file = dir+'/'+snapbase+'_'+snap_ext
        if not os.path.exists(this_file+'.hdf5'):
            this_file = dir+'/snapdir_'+snap_ext+'/'+snapbase+'_'+snap_ext

        print "setting snapshot to {:s}".format( this_file )

    if illustris_tng==True:
        print "Making a TNG stellar image."
        star_data = tng_stellar_image_data(this_file, fof_num=tng_groupnr, sub_num=tng_subnr, **kwargs)
    else:
        star_data = stellar_image_data(this_file, **kwargs)

    if cosmo_wrap:
        boxsize = star_data.head.boxsize      #np.max( star_data.gas_xyz ) - np.min( star_data.gas_xyz )

    if illustris_tng and isinstance( tng_subnr, int ):
        image_center = readsubf.subfind_catalog(  dir, snapnum, subcat=True, grpcat=False,
                       keysel=['SubhaloPos'] )
        center = image_center.SubhaloPos[tng_subnr, :] 
    elif illustris_tng and isinstance( tng_groupnr, int ):
        image_center = readsubf.subfind_catalog(  dir, snapnum, subcat=True, grpcat=True,
                       keysel=['SubhaloPos', 'GroupFirstSub'] )
        center = image_center.SubhaloPos[ image_center.GroupFirstSub[tng_groupnr], :]
    elif center is not None:
        center = center
    else:
        try:
            bh_id      = np.array(ws.read_block(this_file,'ID  ', parttype=5))
            bh_xyz      = np.array(ws.read_block(this_file,'POS ', parttype=5))
            center = [ np.mean(bh_xyz[0,0]), np.mean(bh_xyz[0,1]), np.mean(bh_xyz[0,2]) ]
        except:
            center = [0.0, 0.0, 0.0 ]

    star_data.gas_xyz[:,0] -= np.mean(center[0])
    star_data.gas_xyz[:,1] -= np.mean(center[1])
    star_data.gas_xyz[:,2] -= np.mean(center[2])
    star_data.star_xyz[:,0] -= np.mean(center[0])
    star_data.star_xyz[:,1] -= np.mean(center[1])
    star_data.star_xyz[:,2] -= np.mean(center[2])

    if cosmo_wrap:
        star_data.star_xyz[ star_data.star_xyz[:,0] > boxsize/2.0 ,0] -= boxsize
        star_data.star_xyz[ star_data.star_xyz[:,1] > boxsize/2.0 ,1] -= boxsize
        star_data.star_xyz[ star_data.star_xyz[:,2] > boxsize/2.0 ,2] -= boxsize
        star_data.star_xyz[ star_data.star_xyz[:,0] < -1.0* boxsize/2.0 ,0] += boxsize
        star_data.star_xyz[ star_data.star_xyz[:,1] < -1.0* boxsize/2.0 ,1] += boxsize
        star_data.star_xyz[ star_data.star_xyz[:,2] < -1.0* boxsize/2.0 ,2] += boxsize

        star_data.gas_xyz[ star_data.gas_xyz[:,0] > boxsize/2.0 ,0] -= boxsize
        star_data.gas_xyz[ star_data.gas_xyz[:,1] > boxsize/2.0 ,1] -= boxsize
        star_data.gas_xyz[ star_data.gas_xyz[:,2] > boxsize/2.0 ,2] -= boxsize
        star_data.gas_xyz[ star_data.gas_xyz[:,0] < -1.0* boxsize/2.0 ,0] += boxsize
        star_data.gas_xyz[ star_data.gas_xyz[:,1] < -1.0* boxsize/2.0 ,1] += boxsize
        star_data.gas_xyz[ star_data.gas_xyz[:,2] < -1.0* boxsize/2.0 ,2] += boxsize


    try:
        xrange = kwargs['xrange']
        yrange = kwargs['yrange']
        if xrange != None and yrange != None:
            fov = np.max( [xrange, yrange]  )
            ok = (star_data.star_xyz[:,0] > -1.1*fov) & (star_data.star_xyz[:,0] < 1.1*fov) & \
                 (star_data.star_xyz[:,1] > -1.1*fov) & (star_data.star_xyz[:,1] < 1.1*fov) & \
                 (star_data.star_xyz[:,2] > -1.1*fov) & (star_data.star_xyz[:,2] < 1.1*fov) & \
                 (star_data.star_bt > 0)

        if np.sum(ok) > 10:
              star_data.star_xyz = star_data.star_xyz[ok,:]
              star_data.star_bt  = star_data.star_bt[ok]
              star_data.star_z   = star_data.star_z[ok]
              star_data.star_mass= star_data.star_mass[ok]
        else:
              print "WARN:  I found 10 or less particles in the FOV.  Expect an error."
    except:
        print "Didnt find xrange/yrange values.  Not clipping.  Using some default range..."


    if cosmo:
	star_age = np.array([ units.age_from_a(star_data.star_bt[index], a0=star_data.head.time) for index in np.arange(star_data.star_bt.shape[0])  ])
    else:
        star_age = star_data.head.time - star_data.star_bt

    star_hsml   = calc_hsml.get_particle_hsml( star_data.star_xyz[:,0], star_data.star_xyz[:,1], star_data.star_xyz[:,2] , **kwargs ) 
    print "min/max of star_hslm : {:f}|{:f}".format(np.min(star_hsml), np.max(star_hsml)) 

    if projaxis==0:
        x_axis_index = 0
        y_axis_index = 1
        z_axis_index = 2
    if projaxis==1:
        x_axis_index = 2
        y_axis_index = 0
        z_axis_index = 1
    if projaxis==2:
        x_axis_index = 1
        y_axis_index = 2
        z_axis_index = 0

    print x_axis_index, y_axis_index, z_axis_index


    # This calls "stellar raytrace" which itself is a wrapper for "raytrace projection compute".
    # out_gas should have units of mass/area; out_u/g/r have funky units, but are ~luminosity/area
    # NO CLIPPING TO THIS POINT!
    out_gas,out_u,out_g,out_r = projection.stellar_raytrace(    star_data.star_xyz[:,x_axis_index], star_data.star_xyz[:,y_axis_index], star_data.star_xyz[:,z_axis_index], \
                                                                star_data.star_mass[:], star_age[:], star_data.star_z[:], star_hsml[:], \
                                                                star_data.gas_xyz[:,x_axis_index], star_data.gas_xyz[:,y_axis_index], star_data.gas_xyz[:,z_axis_index], star_data.gas_mass, \
                                                                star_data.gas_z, star_data.gas_hsml, \
                                                                band_ids=band_ids, \
                                                                **kwargs)

    print "within stellar_image the u,g,r images have sizes and min/max values of:"
    print out_gas.shape
    print out_u.shape
    print out_g.shape
    print out_r.shape
    print np.min(out_u), np.max(out_u)
    print np.min(out_g), np.max(out_g)
    print np.min(out_r), np.max(out_r)


    
    if(np.array(out_gas).size<=1):
        out_gas=out_u=out_g=out_r=np.zeros((pixels,pixels))
        image24=massmap=np.zeros((pixels,pixels,3))
        print "SIZE OF OUTPUT IMAGE IS ZERO.  RETURNING IMAGES WITH JUST ZEROS."
    else:
        ## make the resulting maps into an image
        #  THIS IS WHERE CLIPPING SHOULD HAPPEN USING maxden and dynrange
        image24, massmap = makepic.make_threeband_image_process_bandmaps( out_r,out_g,out_u, **kwargs)
        print "Making a three-band image from the R-G-B maps."

    print "within stellar_image image24 has a size:"
    print image24.shape
    print "and a min/max in the first channel of"
    print np.min(image24[:,:,0]), np.max(image24[:,:,0])



    include_lighting=0
    if (include_lighting==1):
        #light = matplotlib.colors.LightSource(azdeg=0,altdeg=65)
        light = viscolors.CustomLightSource(azdeg=0,altdeg=65)
        if (len(massmap.shape)>2):
            ## do some clipping to regulate the lighting:
            elevation = massmap.sum(axis=2)
            minden = maxden / dynrange
            elevation = (elevation - minden) / (maxden - minden)
            elevation[elevation < 0.] = 0.
            elevation[elevation > 1.] = 1.
            elevation *= maxden
            grad_max = maxden / 5.
            grad_max = maxden / 6.
            #image24_lit = light.shade_rgb(image24, massmap.sum(axis=2))
            image24_lit = light.shade_rgb(image24, elevation, vmin=-grad_max, vmax=grad_max)
        else:
            image24_lit = light.shade(image24, massmap)
        image24 = image24_lit


    return image24