def read_galaxy(self, itype, gn, sgn, centre, load_region_length): """ For a given galaxy (defined by its GroupNumber and SubGroupNumber) extract the Temperature, Density and StarFormationRate of all gas particles using the read_eagle routine. Conversion factors are still loaded directly from the hdf5 files. itype: Particle Type (0 for PartType0 = Baryons) gn: GroupNumber sgn: SubGroupNumber centre: centre of region of particles that we look at load_region_length: cube length of particle region """ data = {} # Initialize read_eagle module. eagle_data = EagleSnapshot(DATADIR + SNAPSHOT) # Put centre into cMpc/h units. centre *= self.h # Select region to load, a 'load_region_length' cMpc/h cube centred on 'centre'. region = np.array([(centre[0] - 0.5 * load_region_length), (centre[0] + 0.5 * load_region_length), (centre[1] - 0.5 * load_region_length), (centre[1] + 0.5 * load_region_length), (centre[2] - 0.5 * load_region_length), (centre[2] + 0.5 * load_region_length)]) eagle_data.select_region(*region) # Load data using read_eagle, load conversion factors manually. f = h5py.File(DATADIR + SNAPSHOT, 'r') for att in [ 'GroupNumber', 'SubGroupNumber', 'Temperature', 'Density', 'StarFormationRate' ]: tmp = eagle_data.read_dataset(itype, att) cgs = f['PartType%i/%s' % (itype, att)].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s' % (itype, att)].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s' % (itype, att)].attrs.get('h-scale-exponent') data[att] = np.multiply(tmp, cgs * self.a**aexp * self.h**hexp, dtype='f8') f.close() print "Size of data:", len(data) # Mask to selected GroupNumber and SubGroupNumber. """ mask = np.logical_and(data['GroupNumber'] == gn, data['SubGroupNumber'] == sgn) for att in data.keys(): data[att] = data[att][mask] """ return data
def read_galaxy(self,gn, sgn, centre,index,load_region_length): print indices[index] indices2 = "%i %s %s " %(indices[index] + 6,indices[index+1],indices[index+2]) fil.write(indices2) data = {} eagle_data = EagleSnapshot('/disks/eagle/L0100N1504/REFERENCE/data/snapshot_027_z000p101/snap_027_z000p101.0.hdf5') centre *= self.h region = np.array([(centre[0]-0.5*load_region_length), (centre[0]+0.5*load_region_length), (centre[1]-0.5*load_region_length), (centre[1]+0.5*load_region_length), (centre[2]-0.5*load_region_length),(centre[2]+0.5*load_region_length)]) eagle_data.select_region(*region) f = h5py.File('/disks/eagle/L0100N1504/REFERENCE/data/snapshot_027_z000p101/snap_027_z000p101.0.hdf5', 'r') constants = f['Constants'] Mpc = constants.attrs['CM_PER_MPC'] totalmass = 0 Rmax = 0 r = 0 Velocity = 0 tmp = eagle_data.read_dataset(0, 'Coordinates') cgs = f['PartType%i/%s'%(0,'Coordinates')].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s'%(0,'Coordinates')].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s'%(0,'Coordinates')].attrs.get('h-scale-exponent') #print cgs, aexp, hexp centre2 = np.multiply(centre, cgs / Mpc * self.a**aexp *self.h**hexp) print self.a, self.h, cgs, aexp, hexp for j in range(len(myData)): Velocity += Vectorlength(myData[j][10], myData[j][11], myData[j][12]) myData[j][2] = np.multiply(myData[j][2], cgs / Mpc * self.a**aexp *self.h**hexp *self.h) myData[j][3] = np.multiply(myData[j][3], cgs / Mpc * self.a**aexp *self.h**hexp *self.h) myData[j][4] = np.multiply(myData[j][4], cgs / Mpc * self.a**aexp *self.h**hexp *self.h) for j in range(len(myData)): totalmass += myData[j][6] if j < len(myData) - 1: for k in range(j+1,len(myData)): #print j,k Rmax = findRmax(myData[j][2], myData[j][3], myData[j][4], myData[k][2], myData[k][3], myData[k][4],Rmax) for i in range(len(myData)): #print i r = findr(centre2[0],centre2[1],centre2[2],myData[i][2],myData[i][3],myData[i][4]) #print Rmax,r,totalmass,myData[i][6],Velocity T = Tfric(Rmax,r,totalmass,myData[i][6], Velocity) print T string = " %s" %(T) fil.write(string) fil.write("\n") f.close()
def read_particles(self): """ Load particles from simulation """ data = {} # Initialize read_eagle module. eagle_data = EagleSnapshot(self._dataloc) for centre in self._centres: # Put centre into cMpc/h units. centre *= self._h print centre # Select region to load, a 'load_region_length' cMpc/h cube centred on 'centre'. region = np.array([(centre[0] - 0.5 * self._region_length), (centre[0] + 0.5 * self._region_length), (centre[1] - 0.5 * self._region_length), (centre[1] + 0.5 * self._region_length), (centre[2] - 0.5 * self._region_length), (centre[2] + 0.5 * self._region_length)]) eagle_data.select_region(*region) # Load data using read_eagle, load conversion factors manually. f = h5py.File(self._dataloc, 'r') for att in self._att: tmp = eagle_data.read_dataset(self._itype, att) cgs = f['PartType%i/%s' % (self._itype, att)].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s' % (self._itype, att)].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s' % (self._itype, att)].attrs.get('h-scale-exponent') data[att] = np.multiply(tmp, cgs * self._a**aexp * self._h**hexp, dtype='f8') f.close() # Mask to selected GroupNumber and SubGroupNumber. """ mask = np.logical_and(data['GroupNumber'] == gn, data['SubGroupNumber'] == sgn) for att in data.keys(): data[att] = data[att][mask] """ self._data = data
def read_subvol(path,ivol,nslice,ptypes=None): file=h5py.File(path,'r') boxsize=file['Header'].attrs['BoxSize'] hfac=file['Header'].attrs['HubbleParam'] afac=file['Header'].attrs['ExpansionFactor'] lims=get_limits(ivol,nslice,boxsize,buffer=0.1) if not ptypes: ptypes={0:['Mass','SubGroupNumber','Temperature','Density','Metallicity'], 4:['Mass','SubGroupNumber','Metallicity']} snapshot=EagleSnapshot(path) snapshot.select_region(*lims) pdata={} for iptype,ptype in enumerate(ptypes): pdata[ptype]=pd.DataFrame(data=snapshot.read_dataset(ptype,'ParticleIDs'),columns=['ParticleIDs']) pdata[ptype].loc[:,[f'Coordinates_{x}' for x in 'xyz']]=snapshot.read_dataset(ptype,'Coordinates') for field in ptypes[ptype]: hexp=file[f'PartType{ptype}/{field}'].attrs['h-scale-exponent'] aexp=file[f'PartType{ptype}/{field}'].attrs['aexp-scale-exponent'] cgs=file[f'PartType{ptype}/{field}'].attrs['CGSConversionFactor'] pdata[ptype][field]=snapshot.read_dataset(ptype,field)*(hfac**hexp)*(afac**aexp)*cgs pdata[ptype].loc[:,'ParticleType']=ptype snapshot.close() #for star particles assign a crazy temp, density, entropy npart_gas=pdata[0].shape[0] npart_star=pdata[4].shape[0] for field in ptypes[0]: if not field in ptypes[4]: pdata[4][field]=np.ones(npart_star)*10**10 #concat all pdata into one df pdata=pd.concat([pdata[ptype] for ptype in pdata],ignore_index=True,) pdata.sort_values(by="ParticleIDs",inplace=True) pdata.reset_index(inplace=True,drop=True) #conversions pdata=convert_pdata(path,pdata) #generate KDtree pdata_kdtree=cKDTree(pdata.loc[:,[f'Coordinates_{x}' for x in 'xyz']].values,boxsize=boxsize) return pdata, pdata_kdtree
def read_galaxy(self, itype, gn, sgn, centre, load_region_length): p, t = defineprojection(projection[indices[n]]) data = {} eagle_data = EagleSnapshot( '/disks/eagle/L0100N1504/REFERENCE/data/snapshot_027_z000p101/snap_027_z000p101.0.hdf5' ) centre *= self.h region = np.array([(centre[0] - 0.5 * load_region_length), (centre[0] + 0.5 * load_region_length), (centre[1] - 0.5 * load_region_length), (centre[1] + 0.5 * load_region_length), (centre[2] - 0.5 * load_region_length), (centre[2] + 0.5 * load_region_length)]) eagle_data.select_region(*region) f = h5py.File( '/disks/eagle/L0100N1504/REFERENCE/data/snapshot_027_z000p101/snap_027_z000p101.0.hdf5', 'r') constants = f['Constants'] Mpc = constants.attrs['CM_PER_MPC'] SMass = constants.attrs["SOLAR_MASS"] for att in ['GroupNumber', 'Coordinates', 'Mass']: if parttype == 1 and att == 'Mass': data[att] = np.ones(len(data['GroupNumber'])) * SMass * 9.7e6 continue tmp = eagle_data.read_dataset(itype, att) cgs = f['PartType%i/%s' % (itype, att)].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s' % (itype, att)].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s' % (itype, att)].attrs.get('h-scale-exponent') data[att] = np.multiply(tmp, cgs * self.a**aexp * self.h**hexp, dtype='f8') if att == 'Coordinates': centre2 = np.multiply(centre, cgs / Mpc * self.a**aexp * self.h**hexp) galx = np.zeros(len(myData)) galy = np.zeros(len(myData)) galz = np.zeros(len(myData)) j = 1 print myData[1][2], cgs, Mpc, self.a, aexp, self.h, hexp print np.multiply(myData[1][2], cgs / Mpc * self.a**aexp * self.h**hexp) for i in range( 1, len(myData)): #Only if first galaxy in list is centre galx[j] = np.multiply( myData[i][2], cgs / Mpc * self.a**aexp * self.h**hexp * self.h) - centre2[0] galy[j] = np.multiply( myData[i][3], cgs / Mpc * self.a**aexp * self.h**hexp * self.h) - centre2[1] galz[j] = np.multiply( myData[i][4], cgs / Mpc * self.a**aexp * self.h**hexp * self.h) - centre2[2] j += 1 print galx, galy, galz x1, y1, x2, y2, x3, y3, l1, l2, l3, l4, l5, l6 = find_galaxy( projection[indices[n]], galx, galy, galz) f.close() mask = (data['GroupNumber'] == gn) for att in data.keys(): data[att] = data[att][mask] for i in range(3): if i == 0: stringtitle = parttype_string + " for p=0 and t=0" + " at snapshot 27 with redshift z = 0.1" x = x1 y = y1 xlabel = l1 ylabel = l2 if i == 1: p = p + 90 stringtitle = parttype_string + " for p=90 and t=0" + " at snapshot 27 with redshift z = 0.1" x = x2 y = y2 xlabel = l3 ylabel = l4 if i == 2: stringtitle = parttype_string + " for p=0 and t=90" + " at snapshot 27 with redshift z = 0.1" t = t + 90 p = p - 90 x = x3 y = y3 xlabel = l5 ylabel = l6 qv_parallel = QuickView(data['Coordinates'] / Mpc, data['Mass'] / SMass, r='infinity', plot=False, p=p, t=t, x=centre2[0], y=centre2[1], z=centre2[2], max_hsml=max_hsml, extent=[-0.5, 0.5, -0.5, 0.5]) plt.imshow(qv_parallel.get_image(), extent=qv_parallel.get_extent(), cmap='viridis', origin='lower') plt.colorbar() color = ['r', 'k', 'm', 'w'] plt.title(stringtitle) plt.xlabel(xlabel) print p, t, x, y plt.ylabel(ylabel) plt.xlim(-0.21, 0.21) plt.ylim(-0.21, 0.21) #plt.clim(5,) #plt.scatter(x, y, facecolors = 'none', color = 'r') marker = ['o', 's', '^', 'D'] for j in range(len(x)): plt.scatter(x[j], y[j], marker=marker[j], facecolors='none', color='r', label=myData[j][5]) plt.legend(bbox_to_anchor=(-0.3, -0.15), loc=3, prop={'size': 8}) plt.show()
def read_particles(self): """ Load particles from simulation """ data = {} # Initialize read_eagle module. eagle_data = EagleSnapshot(self._dataloc) # Select entire cube region eagle_cube_length = eagle_data.boxsize print "EAGLE box size:", eagle_cube_length print "" region = np.array([3 * [0, eagle_cube_length]]).flatten() eagle_data.select_region(*region) # Read all data f = h5py.File(self._dataloc, 'r') # Read coordinates as well, need them to extract the particles in the # volumes of the filaments if not 'Coordinates' in self._att: self._att += ['Coordinates'] # * Step 1 * # Read data from all particles in the simulation at given snapshot for att in self._att: tmp = eagle_data.read_dataset(self._itype, att) cgs = f['PartType%i/%s' % (self._itype, att)].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s' % (self._itype, att)].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s' % (self._itype, att)].attrs.get('h-scale-exponent') if att == 'Coordinates': data[att] = tmp / self._h # Get co-moving coordinates else: data[att] = np.multiply(tmp, cgs * self._a**aexp * self._h**hexp, dtype='f8') f.close() # * Step 2 * # Extract particles that are within the filament region # (= in the volumes of self._volumes) def in_volume(pos): # For each volume around the centres, check if the position is contained # If yes, return True and if it is in no volume return False for vol in self._volumes: if (vol[0] < pos[0] < vol[1]) and (vol[2] < pos[1] < vol[3]) and (vol[4] < pos[2] < vol[5]): return True return False # Apply `in_volume` to every single particle mask = np.apply_along_axis(in_volume, axis=1, arr=data['Coordinates']) # Keep only those particles that are within the filament for key in data.keys(): data[key] = data[key][mask] self._data = data
def read_galaxy(self, itype, gn, sgn, centre, velocity, load_region_length, fileloc): """ For a given galaxy (defined by its GroupNumber and SubGroupNumber) extract the Temperature, Density and StarFormationRate of all gas particles using the read_eagle routine. Conversion factors are still loaded directly from the hdf5 files. """ data = {} # Initialize read_eagle module. eagle_data = EagleSnapshot(fileloc+'snap_028_z000p000.0.hdf5') # Put centre into cMpc/h units. centre *= self.h # Select region to load, a 'load_region_length' cMpc/h cube centred on 'centre'. region = np.array([ (centre[0]-0.5*load_region_length), (centre[0]+0.5*load_region_length), (centre[1]-0.5*load_region_length), (centre[1]+0.5*load_region_length), (centre[2]-0.5*load_region_length), (centre[2]+0.5*load_region_length) ]) eagle_data.select_region(*region) # Load data using read_eagle, load conversion factors manually. f = h5py.File(fileloc+'snap_028_z000p000.0.hdf5', 'r') for att in ['GroupNumber', 'SubGroupNumber', 'StarFormationRate', 'Temperature', 'Density', 'Coordinates', 'Velocity','SmoothedElementAbundance/Oxygen','SmoothedElementAbundance/Hydrogen']: tmp = eagle_data.read_dataset(itype, att) cgs = f['PartType%i/%s'%(itype, att)].attrs.get('CGSConversionFactor') aexp = f['PartType%i/%s'%(itype, att)].attrs.get('aexp-scale-exponent') hexp = f['PartType%i/%s'%(itype, att)].attrs.get('h-scale-exponent') data[att] = np.multiply(tmp, cgs * self.a**aexp * self.h**hexp, dtype='f8') # This puts all attributes into their proper values (not /h) f.close() # Mask to selected GroupNumber and SubGroupNumber. mask = np.logical_and(data['GroupNumber'] == gn, data['SubGroupNumber'] == sgn) for att in data.keys(): data[att] = data[att][mask] # Put centre back into proper units centre /= self.h print 'data[\'SmoothedElementAbundance/Oxygen\']:' print data['SmoothedElementAbundance/Oxygen'] centre_cgs = centre * u.Mpc.to(u.cm) velocity_cgs = velocity * u.km.to(u.cm) data['rel_location'] = np.asarray([(partcoords-centre_cgs) for partcoords in data['Coordinates']]) data['radius'] = np.asarray([float(np.sqrt(np.dot(relloc,relloc))) for relloc in data['rel_location']]) data['rel_velocity'] = np.asarray([(partvelocity-velocity_cgs) for partvelocity in data['Velocity']]) data['rel_speed'] = np.asarray([np.sqrt(np.dot(relvel,relvel)) for relvel in data['rel_velocity']]) data['veldot'] = np.asarray([np.dot(relvel,relloc)/np.sqrt(np.dot(relloc,relloc)) for relvel, relloc in zip(data['rel_velocity'],data['rel_location'])]) data['veldot_norm'] = np.asarray([np.dot(relvel,relloc)/(np.sqrt(np.dot(relloc,relloc))*np.sqrt(np.dot(relvel,relvel))) for relvel, relloc in zip(data['rel_velocity'],data['rel_location'])]) print 'radius' print data['radius']* u.cm.to(u.Mpc) print 'speed' print data['rel_speed']* u.cm.to(u.km) print '' # following adapted from the make maps perrgn script atomw_H = 1.00794005e0 uthing = 1.66053892e-24 data['nH'] = (data['SmoothedElementAbundance/Hydrogen'])*(data['Density'])/(atomw_H*uthing) # DEBUG PLOT # fig, (ax) = plt.subplots(1,1,figsize = (7,7)) # ax.hist(data['radius']* u.cm.to(u.Mpc)) # plt.show() """ for i in range(1): print data['Velocity'][i] * u.cm.to(u.km) print velocity_cgs* u.cm.to(u.km) print (data['Velocity'][i]-velocity_cgs) * u.cm.to(u.km) print "" print data['Coordinates'][i] * u.cm.to(u.Mpc) print centre_cgs * u.cm.to(u.Mpc) print (data['Coordinates'][i]-centre_cgs) * u.cm.to(u.Mpc) print "" print data['rel_location'][i] * u.cm.to(u.Mpc) print data['rel_velocity'][i] * u.cm.to(u.km) print "" print np.dot((data['Velocity'][i]-velocity_cgs),(data['Coordinates'][i]-centre_cgs)) print np.dot(data['rel_velocity'][i],data['rel_location'][i]) print np.sqrt(np.dot(data['rel_velocity'][i],data['rel_velocity'][i])) print np.sqrt(np.dot(data['rel_location'][i],data['rel_location'][i])) print "" print data['veldot'][i] * u.cm.to(u.km) print np.sqrt(np.dot(data['rel_velocity'][i],data['rel_velocity'][i])) * u.cm.to(u.km) print data['veldot_norm'][i] print "" print len(data['GroupNumber']) """ return data