Skip to content

Instantly share code, notes, and snippets.

@rokroskar
Created April 30, 2014 10:14
Show Gist options
  • Save rokroskar/9e869ec7b50f56e0c040 to your computer and use it in GitHub Desktop.
Save rokroskar/9e869ec7b50f56e0c040 to your computer and use it in GitHub Desktop.
ramses tform derived quantity using part2birth
import pynbody
part2birth = '/home/itp/roskar/ramses/galaxy_formation/part2birth'
@pynbody.ramses.RamsesSnap.derived_quantity
def tform(self) :
"""Generate a tform array that is compatible with pynbody unit system
and will allow one to get the ages of stars.
make sure the 'part2birth' above corresponds to the location of the executable on your machine
"""
from scipy.io.numpyio import fread
top = self
while hasattr(top,'base') : top = self.base
ncpu = top._info['ncpu']
nstar = len(top.s)
top.s['tform'] = -1.0
done = 0
for i in range(ncpu) :
try :
f = open('%s/birth/birth_%s.out%05d'%(top.filename[:-12],top._timestep_id,i+1))
except IOError :
import os
os.system("cd %s; mkdir birth; %s -inp output_%s; cd .."%(top.filename[:-12],part2birth,top._timestep_id))
f = open('%s/birth/birth_%s.out%05d'%(top.filename[:-12],top._timestep_id,i+1))
n = fread(f,1,'i')
n /= 8
ages = fread(f,n,'d')
new = np.where(ages > 0)[0]
top.s['tform'][done:done+len(new)] = ages[new]
done += len(new)
f.close()
top.s['tform'].units = 'Gyr'
return self.s['tform']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment