Ejemplo n.º 1
0
    def get_circular_velocity( self,
        galaxy_cut,
        length_scale,
        metafile_dir,
        ptypes = data_constants.STANDARD_PTYPES,
        ):
        '''Get the circular velocity at galaxy_cut*length_scale.

        Args:
            galaxy_cut (float) :
                galaxy_cut*length_scale defines the radius around the center of the halo within which to get the mass.

            length_scale (str) :
                galaxy_cut*length_scale defines the radius around the center of the halo within which to get the mass.

            metafile_dir (str) :
                Directory containing metafile data, for getting out the redshift given a snapshot.

            ptypes (list of strs) :
                Particle types to count the mass inside the halo of.

        Returns:
            v_circ (np.ndarray) 
                Circular velocity at galaxy_cut*length_scale using mass from the given ptypes.
        '''

        # Get the redshift, for converting the radius to pkpc/h.
        metafile_reader = read_metafile.MetafileReader( metafile_dir )
        metafile_reader.get_snapshot_times()
        redshift = metafile_reader.snapshot_times['redshift'][self.halos_snum]

        # Get the radius in pkpc/h
        try:
            radius = galaxy_cut*self.halos[length_scale]
        except KeyError:
            radius = galaxy_cut*self.halos_add[length_scale]
        radius /= ( 1. + redshift )

        # Get the mass in Msun/h
        masses = []
        for ptype in ptypes:
            mass_key = self.key_parser.get_enclosed_mass_key( ptype, galaxy_cut, length_scale )
            try:
                ptype_mass = self.halos_add[mass_key]
            except:
                ptype_mass = self.halos[mass_key]
            masses.append( ptype_mass )
        mass = np.array( masses ).sum( axis=0 )

        # Now get the circular velocity out
        # (note that we don't need to bother with converting out the 1/h's, because in this particular case they'll cancel)
        v_circ = astro_utils.circular_velocity( radius, mass )

        return v_circ
Ejemplo n.º 2
0
    def save_custom_mtree_halos( self, snums, halo_ids, metafile_dir, ):
        '''Save a custom merger tree.

        Args:
            snums (array-like or int) :
                What snapshots to generate the custom merger tree for.
                If a single integer, then snums will start at that integer and count backwards by single snapshots for the
                length of halo_ids

            halo_ids (array-like) :
                halo_ids[i] is the AHF_halos halo ID for the merger tree halo at snums[i].

            metafile_dir (str) :
                Directory for the metafile (used to get simulation redshift).

        Modifies:
            self.data_dir/halo_00000_custom.dat (text file) : Saves the custom merger tree at this location.
        '''

        if isinstance( snums, int ):
            snums = np.arange( snums, snums - len( halo_ids ), -1 )

        # Concatenate the data
        ahf_frames = []
        for snum, halo_id in zip( snums, halo_ids, ):

            print( "Getting data for snapshot {}".format( snum ) )

            self.get_halos( snum )

            ahf_frames.append( self.halos.loc[halo_id:halo_id] )

        custom_mtree_halo = pd.concat( ahf_frames )

        # Make sure to store the IDs too
        custom_mtree_halo['ID'] = halo_ids

        # Add in the snapshots, and use them as the index
        custom_mtree_halo['snum'] = snums
        custom_mtree_halo = custom_mtree_halo.set_index( 'snum', )

        # Get and save the redshift
        metafile_reader = read_metafile.MetafileReader( metafile_dir )
        metafile_reader.get_snapshot_times()
        custom_mtree_halo['redshift'] = metafile_reader.snapshot_times['redshift'][snums]

        # Save the data
        save_filepath = os.path.join( self.data_dir, 'halo_00000_custom.dat' )
        custom_mtree_halo.to_csv( save_filepath, sep='\t' )
Ejemplo n.º 3
0
    def get_accurate_redshift( self, metafile_dir ):
        '''Get a better values of the redshift than what's stored in the Halo filename, by loading them from an external file.

        Args:
            metafile_dir (str): The directory the snapshot_times are stored in.

        Modifies:
            self.mtree_halos (dict of pd.DataFrames): Updates the redshift column
        '''

        # Get the redshift data out
        metafile_reader = read_metafile.MetafileReader( metafile_dir )
        metafile_reader.get_snapshot_times()

        # Replace the old data
        for halo_id in self.mtree_halos.keys():

            mtree_halo = self.mtree_halos[ halo_id ]

            # Read and replace the new redshift
            new_redshift = metafile_reader.snapshot_times['redshift'][ mtree_halo.index ]
            mtree_halo['redshift'] = new_redshift
Ejemplo n.º 4
0
    def get_analytic_concentration( self, metafile_dir, type_of_halo_id='merger_tree' ):
        '''Get analytic values for the halo concentration, using colossus, Benedikt Diemer's cosmology code
        ( https://bitbucket.org/bdiemer/colossus ; http://www.benediktdiemer.com/code/colossus/ ).

        Assumptions:
            - We're using the default formula of Diemer&Kravtstov15
            - We're using the Bryan&Norman1998 version of the virial radius.

        Args:
            metafile_dir (str): The directory the snapshot_times are stored in.
            type_of_halo_id (str): 'merger_tree' if the halo id is a merger tree halo id.
                                                          'halos' if the halo id is a *.AHF_halos halo id.

        Returns
            c_vir (np.array of floats): The concentration, defined as R_vir/r_scale.
        '''

        # Include imports here, because this function may not in general work if colossus is not available,
        # and the rest of the module should still be made useable
        # There may be some warnings here about the version of scipy colossus uses, as opposed to the version galaxy_dive uses
        import colossus.cosmology.cosmology as co_cosmology
        import colossus.halo.concentration as co_concentration

        # Get simulation parameters, for use in creating a cosmology
        metafile_reader = read_metafile.MetafileReader( metafile_dir )
        metafile_reader.get_used_parameters()

        # Setup the cosmology used by the simulations
        sim_cosmo = {
            'flat': True,
            'H0' : float( metafile_reader.used_parameters['HubbleParam'] )*100.,
            'Om0' : float( metafile_reader.used_parameters['Omega0'] ),
            'Ob0' : float( metafile_reader.used_parameters['OmegaBaryon'] ),
            'sigma8' : co_cosmology.cosmologies['WMAP9']['sigma8'], # Use WMAP9 for values we don't store in our simulations explicitly.
            'ns' : co_cosmology.cosmologies['WMAP9']['ns'], # Use WMAP9 for values we don't store in our simulations explicitly.
        }
        cosmo = co_cosmology.setCosmology( 'sim_cosmo', sim_cosmo )

        if type_of_halo_id == 'merger_tree':

            # Loop over all mt halos
            for halo_id in self.mtree_halos.keys():

                # Load the data
                mtree_halo = self.mtree_halos[ halo_id ]

                # Get the concentration out
                c_vir = []
                for m_vir, z in zip( mtree_halo['Mvir'], mtree_halo['redshift'] ):
                    c = co_concentration.concentration( m_vir, 'vir', z, model='diemer15', statistic='median')
                    c_vir.append( c )

                # Turn the concentration into an array
                c_vir = np.array( c_vir )

                # Save the concentration
                mtree_halo['cAnalytic'] = c_vir

        elif type_of_halo_id == 'halos':

            # Get the redshift for the halo file.
            metafile_reader.get_snapshot_times()
            redshift = metafile_reader.snapshot_times['redshift'][self.halos_snum]

            # Get the concentration
            c = co_concentration.concentration( self.halos['Mvir'], 'vir', redshift, model='diemer15', statistic='median')

            return c
Ejemplo n.º 5
0
    def setUp(self):

        self.metafile_reader = read_metafile.MetafileReader(sdir)