14 KiB
14 KiB
In [ ]:
import requests
response = requests.get('http://xkcd.com/353/')In [ ]:
print(response.text[:300]) # only print the first 300 charactersIn [ ]:
r2 = requests.get('http://wwwstaff.ari.uni-heidelberg.de/rschmidt/pycourse/ice_data/20060315.npy')In [ ]:
r2.headersIn [ ]:
r2.text[:200]In [ ]:
r2.content[:200]In [ ]:
with open('20060315.npy', 'wb') as f:
f.write(r2.content)In [ ]:
import numpy as np
data = np.load('20060315.npy')In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(12,12))
plt.imshow(data, origin='lower')In [ ]:
headers={"api-key":"2566d8dd2bcf9aefbb3d8b01080a877b"} # 2566... = key for Uni HD python courseIn [ ]:
import requests
r = requests.get('http://www.illustris-project.org/api/', headers=headers)
print (r.text[:400])In [ ]:
import json
data = json.loads(r.text)In [ ]:
dataIn [30]:
id = 75 # choose your ID of the halo 75 or 1030 are defaults
redshift = 0.0 # choose a redshift, will be converted to snapshot automatically.
params = {'dm':'Coordinates'}
url = "http://www.illustris-project.org/api/Illustris-1/snapshots/z=" + str(redshift) + "/subhalos/" + str(id)
# read the parameters of the subhalo in the json format
subhalo = requests.get(url, headers=headers).json()
# read the cutout of the subhalo into memory
cutout = requests.get(url+"/cutout.hdf5", headers=headers, params=params)
# extract the filename from the header information and build the output name
filename= cutout.headers['content-disposition'].split("filename=")[1]
outname=filename.split(".hdf5")[0]+"_z="+str(redshift)+".hdf5"
# save the cutout to disk, use 'outname'
with open(outname, 'wb') as f:
f.write(cutout.content)In [ ]:
for key in sorted(subhalo):
print (key,subhalo[key])In [ ]:
subhalo['len']In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
print (outname)In [ ]:
import h5py
with h5py.File(outname) as f:
# we subtract the centre of the halo (extracted from the dictionary information in subhalo)
# from the particle coordinates
dx = f['PartType1']['Coordinates'][:,0] - subhalo['pos_x']
dy = f['PartType1']['Coordinates'][:,1] - subhalo['pos_y']
dz = f['PartType1']['Coordinates'][:,2] - subhalo['pos_z']
In [ ]:
def get(url, params=None):
# make HTTP GET request to path
headers = {"api-key":"2566d8dd2bcf9aefbb3d8b01080a877b"}
r = requests.get(url, params=params, headers=headers)
# raise exception if response code is not HTTP SUCCESS (200)
r.raise_for_status()
if r.headers['content-type'] == 'application/json':
return r.json() # parse json responses automatically
if 'content-disposition' in r.headers:
filename = r.headers['content-disposition'].split("filename=")[1]
with open(filename, 'wb') as f:
f.write(r.content)
return filename # return the filename string
return rIn [ ]:
id = 75
snapshot = 135
params = {'dm':'Coordinates'}
url = "http://www.illustris-project.org/api/Illustris-1/snapshots/" + str(snapshot) + "/subhalos/" + str(id)
sub = get(url) # get json response of subhalo properties
saved_filename = get(url + "/cutout.hdf5",params) # get and save HDF5 cutout file
with h5py.File(saved_filename) as f: # read coordinates
dx = f['PartType1']['Coordinates'][:,0] - sub['pos_x']
dy = f['PartType1']['Coordinates'][:,1] - sub['pos_y']
dz = f['PartType1']['Coordinates'][:,2] - sub['pos_z']