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]
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
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)
import numpy as np import eagleSqlTools as sql ############################################################################################ simlabel = 'RefL0100N1504' # Pick your simulation - this has to be in the same format as the name in the database groupnumbers = [1000, 1100, 1200] # Your groupnumbers here! ############################################################################################ # Create the connection to the SQL database using your login credentials print 'Connecting to EAGLE database...' con = sql.connect("your_database_username", password="******") print 'Connected.' print 'Executing queries...' # Loop over your group numbers for g in range(len(groupnumbers)): # This query will get you quantities for the MAIN BRANCH progenitors of your z=0 central galaxy # You can get any quantities from the Subhalo table (see Stu McAlpine's 2016 reference paper) from 'gal' # for galaxies in the tree. # If you want the whole tree and not just the main branch, replace 'TopLeafID' with 'LastProgID' # in the last WHERE statement. # You can connect the Subhalo table to the Aperture table to get star formation rates, stellar masses # 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,
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
sim_box = int(argv[8]) # ============ # sim = int(argv[3]) # set simulation here # ============ if sim_box == 100: sim = sim100 box_size = 100 host_mass = 1E13 else: sim = sim25 box_size = 25 host_mass = 1E12 con = sql.connect('twh176', password='******') gid = int(argv[1]) fof = int(argv[2]) sub = int(argv[3]) dx = float(argv[4]) dy = float(argv[5]) dz = float(argv[6]) sat_index = int(argv[7]) # select satellite here sat_flag = int(argv[9]) host_index = gid # print(sim, gid,dx,dy,dz,box_size,sat_index) # satellite selection around a host at z = 0. Hosts close to the wall of the box are handled as well normal_sat_query = 'SELECT \ S.GalaxyID as Sgid, \ H.GalaxyID as Hgid, \
import eagleSqlTools as sql import numpy as np import matplotlib.pyplot as plt # Array of chosen simulations. Entries refer to the simulation name and comoving box length. mySims = np.array([('RefL0100N1504', 100.), ('AGNdT9L0050N0752', 50.), ('RecalL0025N0752', 25.)]) # This uses the eagleSqlTools module to connect to the database with your username and password. # If the password is not given, the module will prompt for it. con = sql.connect("<username>", password="******") for sim_name, sim_size in mySims: print sim_name # Construct and execute query for each simulation. This query returns the number of galaxies # for a given 30 pkpc aperture stellar mass bin (centered with 0.2 dex width). myQuery = "SELECT \ 0.1+floor(log10(AP.Mass_Star)/0.2)*0.2 as mass, \ count(*) as num \ FROM \ %s_SubHalo as SH, \ %s_Aperture as AP \ WHERE \ SH.GalaxyID = AP.GalaxyID and \ AP.ApertureSize = 30 and \ AP.Mass_Star > 1e8 and \ SH.SnapNum = 27 \ GROUP BY \ 0.1+floor(log10(AP.Mass_Star)/0.2)*0.2 \
#redshift_labels = ['0.0'] #redshift_labels = ['0.5','2.0','3.0'] galtype = 'all' # 'dwarf', 'Lstar', 'group' or 'all' particle_types = ['0', '4'] ################################################################## outfile = '/data5/arijdav1/merger_data/radial_profiling/mergertrees_' + simulation + '_' + run + '.pkl' if not os.path.exists(outfile) or 'requery' in argv: # Look up which galaxies to get using the merger trees print 'Connecting to EAGLE database...' con = sql.connect("jdavies", password="******") print 'Connected.' print 'Executing queries...' # Define mass bins, descending in mass so that we roughly ascend in group number mass_bins = np.log10(np.logspace(11.75, 13., 60))[::-1] for m in range(len(mass_bins) - 1): myQuery = '''-- Select the quantities we want SELECT DES.GroupNumber as GroupNumber, PROG.SnapNum as prog_SnapNum, PROG.GroupNumber as prog_GroupNumber, FOF.Group_M_Crit200 as M200
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
import mypy import pylab import numpy as np import eagleSqlTools as sql import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from astropy import wcs, cosmology, constants, units cosmo = cosmology.FlatLambdaCDM(H0=70, Om0=0.3) sim = ['RefL0100N1504', 100.] connection = sql.connect("atomczak", password="******") ### storing basic meta data of EAGLE snapshots class snapshot: def __init__(self, id, expansionFactor, lookbackTime, redshift): self.id = id self.expansionFactor = expansionFactor self.lookbackTime = lookbackTime self.redshift = redshift self.z = redshift snapshots_info = [[0, 0.05, 13.62, 20.00], [1, 0.06, 13.53, 15.13], [2, 0.09, 13.32, 9.99], [3, 0.10, 13.25, 8.99], [4, 0.11, 13.16, 8.07], [5, 0.12, 13.04, 7.05], [6, 0.14, 12.86, 5.97], [7, 0.15, 12.75, 5.49], [8, 0.17, 12.63, 5.04], [9, 0.18, 12.46, 4.49], [10, 0.20, 12.25, 3.98], [11, 0.22, 12.01, 3.53], [12, 0.25, 11.66, 3.02], [13, 0.29, 11.16, 2.48],
import eagleSqlTools as sql import numpy as np import os username = os.environ['EAGLE_USERNAME'] password = os.environ['EAGLE_PASSWORD'] con = sql.connect(username, password=password) model_dict = {'REFERENCE': 'Ref', 'RECAL': 'Recal', 'AGNdT9': 'AGNdT9'} class SnapShotInfo: """ simple class to contain information from the database about snapshots history: written - Mackereth (UoB) - 05/02/2020 """ 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']