%pylab inline
import requests
import json
# This is our base url
base = "http://bluetides-api.psc.edu/"
response = requests.get(base)
print(response.url)
print(response.json())
Now we want to look at the available snapshots and their basic information:
pig_url = base + "pig/"
response = requests.get(pig_url)
pig_list = json.loads(response.text)['LIST']
print('******************************PIG INFO*****************************')
for i in range(len(pig_list)):
print('ID: ', pig_list[i]['id'],\
' FileName: ', pig_list[i]['name'], \
' Num halos: ', pig_list[i]['num_halos'], \
' Redshift: %.2f'%(pig_list[i]['time']))
Before reading the data, let's first look at available particle types and fields within this snapshot
ptype_url = pig_url + '251'
requests.get(ptype_url).json()
We see that there are 4 types of particles(subdirectories) within snapshot 251, let's examine 'gas' first:
field_url = ptype_url + '/gas'
requests.get(field_url).json()
Now we get the available fields(subdirectories) of gas particles. Take a look at the position of the first halo:
# this is a function for getting requested field as a numpy array
import time
def get_group_data(group_url):
t1 = time.time()
response = requests.get(group_url)
key = list(response.json().keys())[0]
response_arr = json.loads(response.json()[key])
response_arr = np.array(response_arr)
t2 = time.time()
print('Time to fetch from '+group_url+': %.2f s'%( t2-t1))
return response_arr
# url for positions of gas particles within Halo#1 in Snapshot#251
url = field_url + "/Position/1"
gas_pos = get_group_data(url)
plt.scatter(gas_pos[:,0],gas_pos[:,1],s=0.001)
plt.title('Projected Gas Position')
plt.show()
Now let's add other particles to the plot:
ptype = ['dm','gas','star','bh']
fig,ax = plt.subplots(figsize=(8,6))
for p in ptype:
data_url = pig_url + '251/' + p + '/Position/1'
pos = get_group_data(data_url)
if p=='bh':
ax.scatter(pos[:,0],pos[:,1],s=80,marker='.',label=p)
else:
ax.scatter(pos[:,0],pos[:,1],s=0.01,marker='.',label=p)
plt.title('Particles in Snapshot 251 Halo 1')
leg = ax.legend()
for ll in leg.legendHandles:
ll._sizes = [50]
plt.show()
def mass_func(m,Lbox,lim_min,lim_max,nbin):
mbin = np.logspace(lim_min,lim_max,nbin)
binmid=np.log10(mbin)[:-1]+np.diff(np.log10(mbin))/2
mhis = np.histogram(m,mbin)
mask = mhis[0]>0
Volumndlog = np.diff(np.log10(mbin))*(Lbox/hh)**3
yy = mhis[0]/Volumndlog
err = yy[mask]/np.sqrt(mhis[0][mask])
y1 = np.log10(yy[mask]+err)
y2 = yy[mask]-err
y2[y2<=0] = 1e-50
return (binmid[mask]),np.log10(yy[mask]), y1, np.log10(y2)
fof_url = pig_url + "251/fofgroup"
requests.get(fof_url).json()
gas_mass_url = fof_url + '/MassByType/all'
response = requests.get(gas_mass_url).json()
response
Now let's see how we can search for particular halos by their BH mass
This query returns the list of halo ids in which the total BH mass is between $10^8$ and $10^9$ $M_\odot$
t1 = time.time()
# this is the url to get haloid of halos whose total bhmass is between 1e8 and 1e9 Msun
response = requests.get(pig_url + "251/search_id/bh/MassByType",params={"min_range":1e-2,"max_range":1e-1})
t2 = time.time()
print('Time to fetch from '+response.url+': %.2f s'%( t2-t1))
key = list(response.json().keys())[0]
haloid_list = json.loads(response.json()[key])[0]
print(haloid_list)
With the list above, we can then look at different kinds of data from those halos with BH mass between $10^8$ and $10^9$ $M_\odot$
For example, let's fetch the accretion rate of all BHs within those halos
# url for positions of gas particles within Halo#1 in Snapshot#251
url = [pig_url + "251/bh/BlackholeAccretionRate/"+str(i+1) for i in haloid_list]
mdot = [get_group_data(u) for u in url]
or we can directly fetch data with a given criterion
t1 = time.time()
response = requests.get(pig_url + "251/search/bh/BlackholeMass/gas_mass",params={"min_range":1e-2,"max_range":2e-2})
t2 = time.time()
print('Time to fetch from '+response.url+': %.2f s'%( t2-t1))
response