Using pNbody with scripts

In addition to using pNbody in the python interpreter, it is very useful to use pNbody in python scripts. Usually a python script begin by the line #!/usr/bin/env python and must be executable:

chmod a+x file.py

The following example (slice.py), we show how to write a script that opens a gadget file, select gas particles and cut a thin slice

\[-1000<y<1000\]

The new files are saved using the extension .slice.

#!/usr/bin/env python3

import sys
from pNbody import *

files = sys.argv[1:]

for file in files:
  print("slicing",file)
  nb = Nbody(file,ftype='gadget')
  nb = nb.select('gas')
  nb = nb.selectc((np.fabs(nb.pos[:,1])<1000))
  nb.rename(file+'.slice')
  nb.write()

In your pnbody_example directory, you can run this script with the command:

./scripts/slice.py gadget_z*0.dat

or:

python ./scripts/slice.py gadget_z*0.dat