def read_galaxy_database(query,
                         filename,
                         username,
                         password,
                         simname,
                         data_request=0):
    if data_request == 1:

        mySims = np.array([(simname, 12.)])
        con = sql.connect(username, password=password)

        for sim_name, sim_size in mySims:
            # print sim_name

            myData = sql.execute_query(con, query)

            # print myData

            pickle_out = open(filename, 'wb')
            pickle.dump(myData, pickle_out)
            pickle_out.close()

    elif data_request == 0:
        pickle_in = open(filename, 'rb')
        myData = pickle.load(pickle_in)
        pickle_in.close()

    return myData
    def __init__(self, randomNumber, simulation, dataStorageLocation):
        '''
        The initialisation of this class initially will define all the constants that will be required during the module. These can be changed directly and then run the                      UpdateInformation module to update all the dependants. It also creates the dictionaries that will be used to store data during the processing. This initialisation requires           the parameters:
        - randomNumber: A random number used to limit the number of results queried from the database
        - simulation: The simulation that the we are interested in querying
        - dataStorageLocation: The location that the pickle files will be loaded from and saved to
        '''

        #Defining Values
        self.ageUniverse = 13.799  # Age of the Universe used in Time Linking estimate gotten from
        self.massPrecision = 0.05  # Mass Precision Used in Galaxy Matching
        self.dataStoragePath = dataStorageLocation  # The path to the storage for files that will be save and loaded
        self.MinimumNumberInBin = 10  # The minimum number of values that need to be in a bin in order for it to be statistically signicant enough to take the median

        #Defining all the Storage Tables
        self.dataStorage = {}  # Main Raw Data Storage
        self.binningStorage = {}  # Post Processing Storage for Binned Data
        self.comparisonData = {}  # Storage of Galaxies that have been matched
        self.propertiesQueried = {
        }  # Storage of the properties stored for each DataSet
        self.defaultQueries = {}  # Storage of Default Queries
        self.timeData = astropy.table.Table(
        )  # Storage for the Time Data (relation between redshift, snapshot numbers, lookbacktime and age of the universe)
        self.timeReference = {
        }  # A Reference Dictionary used to get corresponding redshfit and lookback time for a given redshift

        # DataBase Connection
        self.con = sql.connect("scp763", password="******")

        # PreInitialisation of Stored Queries
        self.simName = simulation
        self.randNum = randomNumber
        self.generateQueries()

        # Query Time Data from the Eagle Database to be used by the time linking module
        # Get the data from the Eagle Database
        snapShotData = sql.execute_query(self.con,
                                         self.defaultQueries['TimeQuery'])
        #Add the queried data to the storage Astropy Table self.timeData
        for label in snapShotData.dtype.names:
            self.timeData[label] = snapShotData[label]
        del snapShotData, label

        # Also calculate the corresping age of the universe at each snapshot and add it as a column
        self.timeData[
            'AgeOfUniverse'] = self.ageUniverse - self.timeData['LookBackTime']

        # Creating a dictionary used for referenceing the snap shot to the redshift and lookbacktime
        for SN, LB, z, t in zip(self.timeData['SnapNum'],
                                self.timeData['LookBackTime'],
                                self.timeData['Z'],
                                self.timeData['AgeOfUniverse']):
            self.timeReference[SN] = [LB, z, t]
Beispiel #3
0
    def __init__(self, ID, model='REFERENCE', run='L0100N1504'):
        self.model = model_dict[model]
        self.run = run
        if isinstance(ID, int):
            self.MainDescendantID = ID
            #query to get other descendant info:
            query = f'SELECT\
            gal.Snapnum as snapnum,\
            gal.GroupNumber as groupnum,\
            gal.SubGroupNumber as subgroupnum,\
            gal.TopLeafID as TopLeafID\
            FROM\
            {model_dict[model]+run}_Subhalo as gal\
            WHERE\
            gal.GalaxyID = {ID}'

            result = sql.execute_query(con, query)
            self.MainDescendantGroupNumber = result['groupnum']
            self.MainDescendantSubGroupNumber = result['subgroupnum']
            self.MainDescendantSnapNum = result['snapnum']
            self.MainDescendantTopLeafID = result['TopLeafID']

        else:
            # is [GroupNumber,SubGroupNumber,SnapNum]
            self.MainDescendantGroupNumber = ID[0]
            self.MainDescendantSubGroupNumber = ID[1]
            self.MainDescendantSnapNum = ID[2]
            query = f'SELECT\
            gal.GalaxyID as galaxyid,\
            gal.TopLeafID as TopLeafID\
            FROM\
            {model_dict[model]+run}_Subhalo as gal\
            WHERE\
            gal.GroupNumber = {ID[0]} and\
            gal.SubGroupNumber = {ID[1]} and\
            gal.SnapNum = {ID[2]}'

            result = sql.execute_query(con, query)
            self.MainDescendantID = result['galaxyid']
            self.MainDescendantTopLeafID = result['TopLeafID']
Beispiel #4
0
    def __init__(self):
        query = 'SELECT\
        snap.Snapnum as snapnum,\
        snap.Redshift as redshift,\
        snap.LookbackTime as tlookback,\
        snap.LumDistance as lumdistance\
        FROM\
        Snapshots as snap'

        snapinfo = sql.execute_query(con, query)
        self.Snapnum = snapinfo['snapnum']
        self.Redshift = snapinfo['redshift']
        self.LookbackTime = snapinfo['tlookback']
        self.LumDistance = snapinfo['lumdistance']
Beispiel #5
0
    def get_all_progenitors(self, mass_limit=1e9):
        """ return mass, coordinates and redshift of all progenitors """
        query = f'SELECT\
                 prog.GalaxyID as GalaxyID,\
                 prog.MassType_Star as mstar,\
                 prog.CentreOfPotential_x as x,\
                 prog.CentreOfPotential_y as y,\
                 prog.CentreOfPotential_z as z,\
                 prog.SnapNum as snapnum,\
                 prog.Redshift as redshift,\
                 prog.DescendantID as DescendantID,\
                 prog.LastProgID as LastProgID,\
                 prog.TopLeafID as  TopLeafID\
                 FROM\
                 {self.model+self.run}_Subhalo as des,\
                 {self.model+self.run}_Subhalo as prog\
                 WHERE\
                 des.GalaxyID = {self.MainDescendantID} and\
                 des.SnapNum = {self.MainDescendantSnapNum} and\
                 prog.MassType_star > {mass_limit} and\
                 prog.GalaxyID between {self.MainDescendantID} and des.LastProgID'

        result = sql.execute_query(con, query)
        #identify main branch
        mainbranch = result['TopLeafID'] == self.MainDescendantTopLeafID
        #store all progeny off the main branch (we want these separate for a few reasons...)
        self.ProgenitorIDs = result['GalaxyID'][~mainbranch]
        self.ProgenitorStellarMass = result['mstar'][~mainbranch]
        self.ProgenitorCoPs = np.dstack(
            [result['x'], result['y'], result['z']])[0][~mainbranch]
        self.ProgenitorSnapNums = result['snapnum'][~mainbranch]
        self.ProgenitorRedshift = result['redshift'][~mainbranch]
        self.ProgenitorDescendantID = result['DescendantID'][~mainbranch]
        self.ProgenitorLastProgID = result['LastProgID'][~mainbranch]
        self.ProgenitorTopLeafID = result['TopLeafID'][~mainbranch]
        #store the Main Branch separately
        self.MainBranchIDs = result['GalaxyID'][mainbranch]
        self.MainBranchStellarMass = result['mstar'][mainbranch]
        self.MainBranchCoPs = np.dstack(
            [result['x'], result['y'], result['z']])[0][mainbranch]
        self.MainBranchSnapNums = result['snapnum'][mainbranch]
        self.MainBranchRedshift = result['redshift'][mainbranch]
        self.MainBranchDescendantID = result['DescendantID'][mainbranch]
        self.MainBranchLastProgID = result['LastProgID'][mainbranch]
        self.MainBranchTopLeafID = result['TopLeafID'][mainbranch]
Beispiel #6
0
def query(username, password, query, outfile):

    print('Connecting to EAGLE database...')
    con = sql.connect(username, password=password)
    print('Connected.')

    print('Executing query...')

    data = sql.execute_query(con, query)

    out_data = {}

    for name in data.dtype.names:
        out_data[name] = data[name]

    with open(outfile, 'w') as output:
        pickle.dump(out_data, output)
        output.close()

    print('Dumped output to ', outfile)
def database_query(username,password,query,outfile):

    import eagleSqlTools as sql
    import pickle

    print 'Connecting to EAGLE database...'
    con = sql.connect(username, password=password)
    print 'Connected.'

    print 'Executing query...'

    data = sql.execute_query(con, query)

    out_data = {}

    for name in data.dtype.names:
        out_data[name] = data[name]

    with open(outfile, 'w') as output:
        pickle.dump(out_data,output)
        output.close()

    print 'Dumped output to ',outfile
Beispiel #8
0
    def __init__(self,
                 snapPath=None,
                 snapBase=None,
                 fof=None,
                 sub=None,
                 db_user=None,
                 db_key=None,
                 subBoxSize=50. * U.kpc,
                 distance=3. * U.Mpc,
                 vpeculiar=0 * U.km / U.s,
                 rotation={'L_coords': (60. * U.deg, 0. * U.deg)},
                 ra=0. * U.deg,
                 dec=0. * U.deg,
                 print_query=False):

        if snapPath is None:
            raise ValueError('Provide snapPath argument to EAGLESource.')
        if snapBase is None:
            raise ValueError('Provide snapBase argument to EAGLESource.')
        if fof is None:
            raise ValueError('Provide fof argument to EAGLESource.')
        if sub is None:
            raise ValueError('Provide sub argument to EAGLESource.')
        if db_user is None:
            raise ValueError('Provide EAGLE database username.')

        # optional dependencies for this source class
        from eagleSqlTools import connect, execute_query
        from pyread_eagle import EagleSnapshot
        from Hdecompose.atomic_frac import atomic_frac
        import h5py

        snapNum = int(snapBase.split('_')[1])
        volCode = normpath(snapPath).split(sep)[-2]
        query = \
            'SELECT '\
            '  sh.redshift as redshift, '\
            '  sh.CentreOfPotential_x as x, '\
            '  sh.CentreOfPotential_y as y, '\
            '  sh.CentreOfPotential_z as z, '\
            '  sh.Velocity_x as vx, '\
            '  sh.Velocity_y as vy, '\
            '  sh.Velocity_z as vz '\
            'FROM '\
            '  {:s}_SubHalo as sh '.format(volCode) + \
            'WHERE '\
            '  sh.Snapnum = {:d} '.format(snapNum) + \
            '  and sh.GroupNumber = {:d} '.format(fof) + \
            '  and sh.SubGroupNumber = {:d}'.format(sub)
        if print_query:
            print('-----EAGLE-DB-QUERY-----')
            print(query)
            print('-------QUERY-ENDS-------')
        if db_key is None:
            print('EAGLE database')
        q = execute_query(connect(db_user, db_key), query)
        redshift = q['redshift']
        a = np.power(1 + redshift, -1)
        cop = np.array([q[coord] for coord in 'xyz']) * a * U.Mpc
        vcent = np.array([q['v' + coord] for coord in 'xyz']) * U.km / U.s

        snapFile = join(snapPath, snapBase + '.0.hdf5')

        with h5py.File(snapFile, 'r') as f:
            h = f['RuntimePars'].attrs['HubbleParam']
            subBoxSize = (subBoxSize * h / a).to(U.Mpc).value
            centre = (cop * h / a).to(U.Mpc).value
            eagle_data = EagleSnapshot(snapFile)
            region = np.vstack(
                (centre - subBoxSize, centre + subBoxSize)).T.flatten()
            eagle_data.select_region(*region)
            lbox = f['/Header'].attrs['BoxSize'] * U.Mpc / h
            fH = f['/RuntimePars'].attrs['InitAbundance_Hydrogen']
            fHe = f['/RuntimePars'].attrs['InitAbundance_Helium']
            proton_mass = f['/Constants'].attrs['PROTONMASS'] * U.g
            mu = 1 / (fH + .25 * fHe)
            gamma = f['/RuntimePars'].attrs['EOS_Jeans_GammaEffective']
            T0 = f['/RuntimePars'].attrs['EOS_Jeans_TempNorm_K'] * U.K

            def fetch(att, ptype=0):
                # gas is type 0, only need gas properties
                tmp = eagle_data.read_dataset(ptype, att)
                dset = f['/PartType{:d}/{:s}'.format(ptype, att)]
                aexp = dset.attrs.get('aexp-scale-exponent')
                hexp = dset.attrs.get('h-scale-exponent')
                return np.array(tmp, dtype='f8') * np.power(a, aexp) \
                    * np.power(h, hexp)

            code_to_g = f['/Units'].attrs['UnitMass_in_g'] * U.g
            code_to_cm = f['/Units'].attrs['UnitLength_in_cm'] * U.cm
            code_to_cm_s = f['/Units'].attrs['UnitVelocity_in_cm_per_s'] \
                * U.cm / U.s
            ng_g = fetch('GroupNumber')
            particles = dict(
                xyz_g=(fetch('Coordinates') * code_to_cm).to(U.kpc),
                vxyz_g=(fetch('Velocity') * code_to_cm_s).to(U.km / U.s),
                T_g=fetch('Temperature') * U.K,
                hsm_g=(fetch('SmoothingLength') * code_to_cm).to(U.kpc) *
                find_fwhm(WendlandC2Kernel().kernel))
            rho_g = fetch('Density') * U.g * U.cm**-3
            SFR_g = fetch('StarFormationRate')
            Habundance_g = fetch('ElementAbundance/Hydrogen')

        particles['mHI_g'] = (atomic_frac(redshift,
                                          rho_g * Habundance_g /
                                          (mu * proton_mass),
                                          particles['T_g'],
                                          rho_g,
                                          Habundance_g,
                                          onlyA1=True,
                                          EAGLE_corrections=True,
                                          SFR=SFR_g,
                                          mu=mu,
                                          gamma=gamma,
                                          fH=fH,
                                          T0=T0) * code_to_g).to(U.solMass)

        mask = ng_g == fof
        for k, v in particles.items():
            particles[k] = v[mask]

        particles['xyz_g'] -= cop
        particles['xyz_g'][particles['xyz_g'] > lbox / 2.] -= lbox.to(U.kpc)
        particles['xyz_g'][particles['xyz_g'] < -lbox / 2.] += lbox.to(U.kpc)
        particles['vxyz_g'] -= vcent

        super().__init__(distance=distance,
                         vpeculiar=vpeculiar,
                         rotation=rotation,
                         ra=ra,
                         dec=dec,
                         h=h,
                         **particles)
        return
    def DatabaseQuery(self,
                      title,
                      queryOrQueryTitle,
                      defaultQuery=False,
                      loadFromPickle=False,
                      convertToAstropyOnLoad=True):
        ''' 
        The aim of this module will be to query the EAGLE database, import this data into the python module and sort it for easy access for automation but also allowing for                  intuitive manual access. This moduel takes the following parameters:
        
        - title: A title for the Data that is going to be queried (this will be the label of data in the storage dictionary)
        - Query: This should either be a fully formed SQL query startiing with the word SELECT with no spaces beforehand or if the parameter defaultQuery is set to True a string               stating the name of one of the defaul queries availble in the package.
        - defaultQuery: This parameter controls whether the module is expecting a custon SQL query or will be using a defualt query stored in the module
        - loadFromPickle: If this parameter is set to true the module will instead load the data from a previously saved pickle file and organise it as required 
        - convertToAstropyOnLoad: If this parameter is true the imported pickle file will be converted to a Astropy table (mainly used to stop incosistant length dictionaries being            converted, as this would rasie and error)

        '''

        #Firstly we will test loadFromPickle to known whether there is a pickle file to be loaded or the database will need to be queried
        if loadFromPickle:
            # Firstly we need to check the data type of the imported data. This is only going to support data which is already in the astropy.table.table.Table format or in hte dict               type which would then need to be sorted into the astropy.table.table.Table
            # Loading the specified pickle file
            queriedData = pickle.load(
                open("{}{}.p".format(self.dataStoragePath, queryOrQueryTitle),
                     "rb"))

            #Checking if the Pickle file is a astropy.table.table.Table type
            if isinstance(queriedData, astropy.table.table.Table):
                # Saving the astropy table to the central data storage
                self.dataStorage[title] = queriedData
                self.propertiesQueried[title] = queriedData.keys()

                del queriedData
            # Checking if the Pickle file is a dict type
            elif isinstance(queriedData, dict):
                if convertToAstropyOnLoad:
                    # We will continue to sort this data as if it were a query from the database with some debug timing
                    print(
                        'Imported Data File is in Diction Format \n Sorting Data into Astropy Table ...'
                    )
                    ts = time.time()

                    #Creation Of Table
                    dataTable = astropy.table.Table()

                    for label in queriedData.keys():
                        dataTable[label] = queriedData[label]

                    #Storing Data in Dictionary
                    self.dataStorage[title] = dataTable
                    self.propertiesQueried[title] = queriedData.dtypes.names

                    # Spare Variable Clean Up
                    del queriedData, dataTable, label

                    #Debug Timing Finalising
                    sortTime = time.time() - ts
                    t = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
                    log = '{}   |  Sort Time: {}s'.format(
                        t, round(sortTime, 2))
                    print(log)
                    del ts, sortTime, t, log

                else:
                    self.dataStorage[title] = queriedData
                    self.propertiesQueried[title] = queriedData.keys()

                    del queriedData

            #If it does not match the two supported formats return an error statement
            else:
                print(
                    "ERROR \n Chosen file does not match the supported formats. Please ensure the contents are either a Dictionary obeject or Astropy Table \n Aborting Function"
                )

        else:
            # In this first section we will determine which query will be sent to the database by checking if a default query was selected
            if defaultQuery:
                # If a default query was selected we are grabbing a query from the dictionary stored in the class.
                query = self.defaultQueries[queryOrQueryTitle]

            else:
                # If a default query was not selected we first test if the query has the appropriate start
                if queryOrQueryTitle[:6] != "SELECT":
                    print(
                        "ERROR in query Formatting: \n Please ensure all queries begin with the phrase SELECT and that all commands are formatted in uppercase \n ABORTING QUERY"
                    )
                    return
                else:
                    query = queryOrQueryTitle

            # Now the query for the array is selected the data base will be sent the query. There is additional code around the query to keep track of how long the query is taking                 which is useful for when large datasets are requested

            ####################
            # Debug Timing SetUp
            print('{} | Querying Database ... '.format(
                datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")))
            ts = time.time()

            # Query the Database
            queriedData = sql.execute_query(self.con, query)

            # Debug Timing Finalising
            queryTime = time.time() - ts
            t = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
            log = '{}   |  Query Time: {}s'.format(t, round(queryTime, 2))
            print(log)
            del ts, queryTime, t, log

            # Now the Database has been queried and the data has been stored in the variable queriedData, we are going to re sort this data to store it in a Astropy Table as this                  allows for better visualisation of the dataset when manually analysing the data
            # Firstly We Create the Astropy table and we also include a debuging timer to ensure time is not being lost when sorting the data

            ####################
            # Debug Timing SetUp
            print('Sorting Data into Astropy Table ...')
            ts = time.time()

            if convertToAstropyOnLoad:
                #Creation Of Table
                dataTable = astropy.table.Table()
                for label in queriedData.dtype.names:
                    dataTable[label] = queriedData[label]

                #Storing Data in Dictionary
                self.dataStorage[title] = dataTable
                self.propertiesQueried[title] = queriedData.dtype.names

                # Spare Variable Clean Up
                del queriedData, dataTable, label
            else:
                self.dataStorage[title] = queriedData

            #Debug Timing Finalising
            sortTime = time.time() - ts
            t = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
            log = '{}   |  Sort Time: {}s'.format(t, round(sortTime, 2))
            print(log)
            del ts, sortTime, t, log
             FOF.Group_R_Crit200 as r_vir \
             FROM \
             {0}_SubHalo as H, \
             {0}_SubHalo as S, \
             {0}_FOF as FOF \
             WHERE \
             H.GalaxyID = {1:.0f} \
             and H.GroupID = FOF.GroupID \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_x  - (S.CentreOfPotential_x - FLOOR((S.CentreOfPotential_x+{2:.0f})/ {5:.0f}))) \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_y  - (S.CentreOfPotential_y - FLOOR((S.CentreOfPotential_y+{3:.0f})/ {5:.0f}))) \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_z  - (S.CentreOfPotential_z - FLOOR((S.CentreOfPotential_z+{4:.0f})/ {5:.0f}))) \
             and S.Snapnum = 28 \
             and S.MassType_Star between 1E9 and 1E12'.format(
    sim, gid, dx, dy, dz, box_size)

sats_info = sql.execute_query(con, normal_sat_query)

sat_len = len(sats_info["Sgid"])
# print(sat_len)
# retrieving SubGroupNumber and FoF for a satellite
fof_sub_query = \
    'SELECT \
S.GalaxyID as gid, \
S.GroupNumber as fof, \
S.SubGroupNumber as sub \
FROM \
{0}_SubHalo as S \
WHERE \
S.GalaxyID = {1}'                 .format(sim, sats_info["Sgid"][sat_index])

fof_sub_info = sql.execute_query(con, fof_sub_query)
Beispiel #11
0
             SH.GalaxyID as gid, \
             SH.GroupNumber as fof,\
             SH.SubGroupNumber as sub, \
             SH.CentreOfPotential_x as copx, \
             SH.CentreOfPotential_y as copy, \
             SH.CentreOfPotential_z as copz, \
             FOF.Group_R_Crit200 as r_vir \
             FROM\
             {0}_SubHalo as SH, \
             {0}_FOF as FOF\
             WHERE \
             FOF.GroupID = SH.GroupID \
             and SH.SnapNum = 28 \
             and SH.MassType_DM > {1}".format(sim, host_mass)

host_ids = sql.execute_query(con, host_query)
# print(len(host_ids))
#
# if int(host_ids['sub'][host_index]) != 0:
#     sys.exit()

# print((host_ids['gid'][0]))

t = time.time()

he_index = host_index

t1 = time.time()


# Handling hosts that are close to the sides of the box
Beispiel #12
0
	            SH.TopLeafID as TopLeafID, \
	            SH.DescendantID as DescendantID, \
	            SH.CentreOfMass_x as x, \
	            SH.CentreOfMass_y as y, \
	            AP.Mass_Star as stellar_mass, \
	            AP.SFR as sfr \
	                   FROM \
	            %s_Aperture as AP, \
	            %s_SubHalo as SH \
	                   WHERE \
	            SH.GalaxyID = AP.GalaxyID and \
	            AP.Mass_Star > 1e8 and \
	            AP.ApertureSize = 30 and \
	            SH.SnapNum = %i " % (sim[0], sim[0], snap.id)

    galaxyData = sql.execute_query(connection, myQuery)
    pylab.savetxt(
        '../data/eagle_simulations/snap%02i.dat' % snap.id,
        galaxyData,
        fmt='%10i  %10i  %10i  %10i  %7.3f  %7.3f  %.3e  %.1e',
        header=
        ' GalaxyID  LastProgID  TopLeafID  DescendantID  x_cMpc  y_cMpc  stellarMass  sfr'
    )
    galaxyData = galaxyData.reshape(-1)

    ###  grabbing group data
    myQuery = "SELECT \
				  FOF.GroupID as id, \
				  FOF.GroupCentreOfPotential_x as x, \
				  FOF.GroupCentreOfPotential_y as y, \
				  FOF.GroupMass as Mgroup, \
Beispiel #13
0
             FROM \
             {0}_SubHalo as H, \
             {0}_SubHalo as S, \
             {0}_FOF as FOF \
             WHERE \
             H.GalaxyID = {1:.0f} \
             and H.GroupID = FOF.GroupID \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_x  - (S.CentreOfPotential_x - FLOOR((S.CentreOfPotential_x+{2:.0f})/ {5:.0f}))) \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_y  - (S.CentreOfPotential_y - FLOOR((S.CentreOfPotential_y+{3:.0f})/ {5:.0f}))) \
             and 0.0033*FOF.Group_R_Crit200 > ABS( H.CentreOfPotential_z  - (S.CentreOfPotential_z - FLOOR((S.CentreOfPotential_z+{4:.0f})/ {5:.0f}))) \
             and S.Snapnum = 28 \
             and S.MassType_Star between 1E8 and 1E12'.format(
    sim, gid, dx, dy, dz, box_size)

#print( len(sats_info["Sgid"]))

sats_info = sql.execute_query(con, normal_sat_query)
print(sats_info)
try:
    f = open("hostid_satnumber_rvir_33_{0}.txt".format(sim_box), "a")
    f.write("{0} {1} {2} {3} {4} {5} {6}\n".format(gid, fof, sub, dx, dy, dz,
                                                   len(sats_info)))
    f.close()
    print(len(sats_info))
except TypeError as error:
    pass

# f = open("hostid_satnumber_{0}.txt".format(sim_box), "a")
# f.write("{0:.0f} 0 \n".format(host_index))
# f.close()
Beispiel #14
0
             SH.GalaxyID as gid, \
             SH.GroupNumber as fof,\
             SH.SubGroupNumber as sub, \
             SH.CentreOfPotential_x as copx, \
             SH.CentreOfPotential_y as copy, \
             SH.CentreOfPotential_z as copz, \
             FOF.Group_R_Crit200 as r_vir \
             FROM\
             {0}_SubHalo as SH, \
             {0}_FOF as FOF\
             WHERE \
             FOF.GroupID = SH.GroupID \
             and SH.SnapNum = 28 \
             and SH.MassType_DM > {1}".format(sim, host_mass)

host_ids = sql.execute_query(con, host_query)


def edge_proximity(host_info, box_size):  # copy this
    offsetx = np.array([])
    offsety = np.array([])
    offsetz = np.array([])
    gid = np.array([])
    fof = np.array([])
    sub = np.array([])
    halfbox = box_size / 2
    for host in host_info:
        x, y, z = 0, 0, 0
        r_vir = host['r_vir'] * 0.0033
        if (host['copx'] - r_vir < 0):
            x = halfbox
Beispiel #15
0
	projection = []
        for line in data:
 		words = line.split()
                if words[0] == "#column":
	        	continue
                for i in range(5,len(words)):
			projection.append(words[2])
                        if words[i] != "-1":
	                        string += "(Mass = (SELECT MAX(Mass) FROM RefL0100N1504_Subhalo WHERE DescendantID = %s) AND DescendantID = %s) OR "%(words[i],words[i])
			if words[i] == "-1":
                                string = string[:-3]
				break
                                #print string
                realstring.append(string)
		string = "SELECT GroupNumber, SubGroupNumber, CentreOfPotential_x, CentreOfPotential_y, CentreOfPotential_z, HalfMassRad_DM, Mass, CentreOfMass_x, CentreOfMass_y, CentreOfMass_z, Velocity_x, Velocity_y, Velocity_z, GroupID FROM RefL0100N1504_Subhalo WHERE "
        con = sql.connect("kbx631", password = "******")
	indices = [41, '27', '28', 42, '27', '28', 70, '27', '28', 71, '27', '28', 111, '26', '28', 116, '26', '27', 118, '26', '28', 128, '26', '28', 129, '26', '26', 131, '26', '28', 136, '26', '28', 150, '26', '28', 163, '26', '28', 178, '26', '28', 230, '25', '27', 231, '25', '28', 237, '25', '27', 239, '25', '28', 241, '25', '27', 244, '25', '28', 269, '25', '27', 287, '25', '28', 288, '25', '28', 294, '25', '28', 298, '25', '28', 305, '25', '28', 320, '25', '28', 325, '25', '27', 328, '25', '28', 330, '25', '28', 356, '25', '26', 364, '24', '28', 367, '24', '26', 371, '24', '25', 377, '24', '27', 387, '24', '28', 389, '24', '28', 398, '24', '28', 399, '24', '27', 401, '24', '26', 402, '24', '28', 405, '24', '25', 406, '24', '27', 424, '24', '25', 429, '24', '26', 430, '24', '27', 431, '24', '28', 440, '24', '27', 442, '24', '25', 446, '24', '27', 449, '24', '28', 461, '24', '28', 464, '24', '26', 472, '24', '28', 479, '24', '28', 484, '24', '27', 485, '24', '27', 486, '24', '28', 508, '23', '28', 509, '23', '24', 511, '23', '26', 512, '23', '26', 521, '23', '28', 524, '23', '28', 529, '23', '28', 530, '23', '25', 532, '23', '26', 545, '23', '26', 546, '23', '26', 548, '23', '26', 551, '23', '27', 554, '23', '26', 555, '23', '25', 556, '23', '26', 561, '23', '26', 562, '23', '27', 563, '23', '27', 567, '23', '27', 571, '23', '25', 573, '23', '28', 574, '23', '28', 577, '23', '25', 581, '23', '25', 582, '23', '25', 587, '23', '25', 605, '23', '28', 607, '23', '26', 614, '23', '26', 619, '23', '26', 623, '23', '27', 624, '23', '27', 631, '23', '27', 633, '23', '26', 638, '23', '27', 640, '23', '25', 641, '23', '27', 643, '23', '27', 654, '23', '25', 681, '22', '28', 686, '22', '25', 688, '22', '28', 691, '22', '25', 695, '22', '27', 696, '22', '26', 701, '22', '26', 702, '22', '26', 706, '22', '25', 709, '22', '24', 716, '22', '25', 720, '22', '25', 722, '22', '25', 724, '22', '25', 729, '22', '25', 731, '22', '25', 732, '22', '25', 733, '22', '27', 737, '22', '25', 739, '22', '28', 740, '22', '26', 743, '22', '28', 744, '22', '27', 745, '22', '26', 746, '22', '28', 747, '22', '23', 751, '22', '25', 753, '22', '26', 754, '22', '26', 755, '22', '23', 757, '22', '28', 760, '22', '25', 763, '22', '25', 764, '22', '28', 766, '22', '27', 768, '22', '27', 770, '22', '27', 775, '22', '27', 785, '22', '27', 786, '22', '26', 793, '22', '28', 814, '22', '27', 816, '22', '28', 822, '22', '24', 826, '22', '27', 836, '22', '28', 838, '22', '26', 888, '21', '25', 889, '21', '26', 890, '21', '25', 893, '21', '24', 894, '21', '25', 898, '21', '23', 899, '21', '23', 900, '21', '27', 914, '21', '27', 916, '21', '23', 917, '21', '23', 918, '21', '28', 922, '21', '25', 924, '21', '24', 928, '21', '25', 934, '21', '25', 940, '21', '23', 941, '21', '25', 942, '21', '27', 952, '21', '23', 954, '21', '26', 956, '21', '26', 958, '21', '24', 960, '21', '26', 964, '21', '26', 965, '21', '23', 971, '21', '24', 973, '21', '27', 975, '21', '27', 979, '21', '26', 981, '21', '25', 982, '21', '24', 986, '21', '24', 990, '21', '26', 997, '21', '24', 1000, '21', '27', 1011, '21', '23', 1014, '21', '28', 1018, '21', '24', 1053, '20', '27', 1058, '20', '24', 1059, '20', '27', 1065, '20', '26', 1071, '20', '27', 1077, '20', '23', 1078, '20', '22', 1079, '20', '24', 1081, '20', '23', 1082, '20', '25', 1083, '20', '28', 1089, '20', '24', 1090, '20', '27', 1092, '20', '27', 1093, '20', '23', 1101, '20', '26', 1102, '20', '21', 1107, '20', '26', 1108, '20', '21', 1111, '20', '23', 1112, '20', '26', 1117, '20', '27', 1118, '20', '22', 1120, '20', '21', 1127, '20', '25', 1129, '20', '26', 1135, '20', '23', 1136, '20', '24', 1150, '20', '24', 1151, '20', '23', 1182, '19', '22', 1184, '19', '28', 1189, '19', '24', 1193, '19', '21', 1194, '19', '24', 1196, '19', '22', 1197, '19', '24', 1198, '19', '21', 1200, '19', '27', 1204, '19', '25', 1205, '19', '24', 1206, '19', '26', 1208, '19', '24', 1210, '19', '25', 1211, '19', '21', 1212, '19', '21', 1216, '19', '21', 1219, '19', '21', 1221, '19', '24', 1222, '19', '20', 1224, '19', '23', 1225, '19', '26', 1227, '19', '21', 1229, '19', '21', 1230, '19', '26', 1231, '19', '26', 1232, '19', '21', 1233, '19', '24', 1235, '19', '23', 1240, '19', '23', 1241, '19', '23', 1244, '19', '26', 1245, '19', '25', 1248, '19', '24', 1249, '19', '21', 1253, '19', '23', 1254, '19', '24', 1261, '19', '26', 1263, '19', '24', 1265, '19', '24', 1273, '19', '24']
        for i in range(0,len(indices),3):
	        myQuery = realstring[indices[i]]
                myData = sql.execute_query(con, myQuery)
                print myQuery
		#for j in range(len(myData)):
               		#print myData[j][13]
		string2 = "SELECT GroupMass, GroupCentreOfPotential_x, GroupCentreOfPotential_y, GroupCentreOfPotential_z, Group_R_Crit2500 FROM RefL0100N1504_FOF WHERE GroupID = %i" %myData[0][13]
		myData2 =  sql.execute_query(con, string2)
		myData2 = np.array([myData2])
		#print myData2[0][0]
		centre = np.array([myData2[0][1],myData2[0][2],myData2[0][3]])
		Findmass(myData[0][0], myData[0][1],  centre,i)
	fil.close()
Beispiel #16
0
    # in apertures etc. See again Stu's database reference paper for SQL examples of how to do that.

    query = '''SELECT        
                     gal.GroupNumber as gn, 
                     gal.SubGroupNumber as sgn, 
     	             gal.Redshift as z,
                     gal.MassType_Star as stellarmass   
                FROM        
                     %s_SubHalo as ref,      -- You pick your z=0 descendant galaxy from the ref table
                     %s_Subhalo as gal       -- Then the gal table holds its progenitors  
          
                WHERE        
                     ref.GroupNumber = %i         -- identify your halo
                  	 and ref.SubGroupNumber = 0   -- pick the central 
                  	 and ref.SnapNum = 28         -- at z=0
                  	 and gal.GalaxyID between ref.GalaxyID and ref.TopLeafID -- this line picks out the tree  
                        
                ORDER BY        
                     gal.Redshift asc''' % (simlabel, simlabel,
                                            groupnumbers[g])

    data = sql.execute_query(con, query)

    # The output is a dictionary which can be addressed using the names you gave in 'SELECT'

    redshift = data['z']
    stellarmass = data['stellarmass']

    print redshift
    print stellarmass
Beispiel #17
0
    string = "SELECT GroupNumber, SubGroupNumber, CentreOfPotential_x, CentreOfPotential_y ,CentreOfPotential_z, GalaxyID FROM RefL0100N1504_Subhalo WHERE "
    realstring = []
    projection = []
    for line in data:
        words = line.split()
        if words[0] == "#column":
            continue
        for i in range(3, len(words)):
            #for i in range(5,len(words)):
            projection.append(words[2])
            if words[i] != "-1":
                string += "GalaxyID = %s or " % (words[i])
            if words[i] == "-1":
                string = string[:-3]
                break
        realstring.append(string)
        string = "SELECT GroupNumber, SubGroupNumber, CentreOfPotential_x, CentreOfPotential_y ,CentreOfPotential_z, GalaxyID FROM RefL0100N1504_Subhalo WHERE "
    con = sql.connect("kbx631", password="******")
    indices = [11, 13, 19, 24, 49, 64, 69]
    #indices = [42]
    n = 4
    for i in range(3, len(indices)):
        myQuery = realstring[indices[i]]
        print myQuery
        myData = sql.execute_query(con, myQuery)
        centre = np.array([myData[0][2], myData[0][3], myData[0][4]])
        PhaseDiagram_ReadEagle(myData[0][0], myData[0][1], centre)
        n += 1
#	xp = PhaseDiagram_ReadEagle(941030,0, centrep)
#	y = PhaseDiagram_ReadEagle(1846,2, centre2)
Beispiel #18
0
def searchgals(xmin, xmax, ymin, ymax, zmin, zmax, strict=False):

    mySim = ('RefL0100N1504', 100.)
    con = sql.connect("dlokhorst", password="******")

    myQuery = "SELECT " + "SH.GalaxyID, \
                SH.StarFormationRate as SFR, \
                SH.CentreOfPotential_x, \
                SH.CentreOfPotential_y, \
                SH.CentreOfPotential_z, \
                SH.SubGroupNumber, \
                SH.MassType_Star, \
                SH.HalfMassProjRad_Gas, \
                SH.HalfMassProjRad_Star \
            FROM \
                %s_SubHalo as SH \
            WHERE \
                SH.SnapNum = 28 and \
                SH.CentreOfPotential_x >= %s and \
                SH.CentreOfPotential_x <= %s and \
                SH.CentreOfPotential_y >= %s and \
                SH.CentreOfPotential_y <= %s and \
                SH.CentreOfPotential_z >= %s and \
                SH.CentreOfPotential_z <= %s and \
                SH.MassType_Star > 0 " % ('RefL0100N1504', xmin, xmax, ymin,
                                          ymax, zmin, zmax)

    if strict:
        myQuery = "SELECT " + "SH.GalaxyID, \
                    SH.StarFormationRate as SFR, \
                    SH.CentreOfPotential_x, \
                    SH.CentreOfPotential_y, \
                    SH.CentreOfPotential_z, \
                    SH.SubGroupNumber, \
                    SH.MassType_Star, \
                    SH.HalfMassProjRad_Gas, \
                    SH.HalfMassProjRad_Star \
                FROM \
                    %s_SubHalo as SH \
                WHERE \
                    SH.SnapNum = 28 and \
                    SH.CentreOfPotential_x >= %s and \
                    SH.CentreOfPotential_x <= %s and \
                    SH.CentreOfPotential_y >= %s and \
                    SH.CentreOfPotential_y <= %s and \
                    SH.CentreOfPotential_z >= %s and \
                    SH.CentreOfPotential_z <= %s and \
                    SH.MassType_Star > 0 and \
                    SH.StarFormationRate > 0.00001 " % (
            'RefL0100N1504', xmin, xmax, ymin, ymax, zmin, zmax)

        #  and \
        #  SH.StarFormationRate > 0.00001

    if verbose:
        print myQuery

    myData = sql.execute_query(con, myQuery)

    xgal = myData['CentreOfPotential_x'][:]  # cMpc
    ygal = myData['CentreOfPotential_y'][:]  # cMpc
    #z = myData['CentreOfMass_z'][:]
    mgal = myData['MassType_Star'][:]  # M_solar
    rhgas = myData['HalfMassProjRad_Gas'][:]  # pkpc
    rhstar = myData['HalfMassProjRad_Star'][:]  # pkpc

    return xgal, ygal, mgal, rhgas, rhstar