Skip to content

Instantly share code, notes, and snippets.

@jzuhone
Created May 28, 2019 19:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzuhone/ef08134ab51c611858f83d5bd4626214 to your computer and use it in GitHub Desktop.
Save jzuhone/ef08134ab51c611858f83d5bd4626214 to your computer and use it in GitHub Desktop.
import requests
import h5py
import yt
print(yt.__version__)
# First download a test halo from the TNG project
baseUrl = 'http://www.tng-project.org/api/'
def get(path, params=None):
# make HTTP GET request to path
headers = {"api-key":"0c3c5772a765e2aa23e32cc537b6c315"}
r = requests.get(path, 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 r
id = 100
url = "http://www.tng-project.org/api/Illustris-1/snapshots/z=0/halos/" + str(id) + "/cutout.hdf5"
saved_filename = get(url, params={"gas":"all"})
print(saved_filename)
# Now we have to open up the file we downloaded and fix the header
# in the file to include info yt needs to read this as an Arepo file
f = h5py.File(saved_filename, "r+")
f["Header"].attrs["NumPart_Total"] = np.array(f["Header"].attrs["NumPart_ThisFile"])
f.create_group("Config")
f["/Config"].attrs["VORONOI"] = 1
f.flush()
f.close()
# Now you can open the file with yt
ds = yt.load(saved_filename)
print(type(ds))
slc = yt.SlicePlot(ds, "z", "density", width=(1.0, "Mpc"), center="max")
slc.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment