getatoms

#!/usr/bin/env python
import os
import tempfile
from optparse import OptionParser
 
from Dacapo import Dacapo
from ASE.Trajectories.NetCDFTrajectory import NetCDFTrajectory
from ASE.IO.xyz import WriteXYZ
 
cmd = OptionParser(usage = '%prog [-r R1 R2 R3] input_nc_file output_xyz_file')
cmd.add_option('-r', '--repeat', type = 'int', nargs = 3,
               help = 'Repeat R1, R2, R3 times along the three axes',
               metavar = 'R1 R2 R3')
 
(opt, argv) = cmd.parse_args()
 
if len(argv) != 2:
    cmd.print_help()
    raise SystemExit
 
ncfile = argv[0]
xyzfile = argv[1]
 
try:
  model = Dacapo.ReadAtoms(ncfile, index = 1)
  trajectory = True
except IndexError:
  trajectory = False
 
if not trajectory:
  model = Dacapo.ReadAtoms(ncfile)
  WriteXYZ(xyzfile, atoms = model, repeat = opt.repeat, id = ncfile)
else:
  model = Dacapo.ReadAtoms(ncfile, index = 0) 
  model.SetCalculator(None)
  tmpfile = tempfile.mktemp()
  xyzpath = NetCDFTrajectory(tmpfile, model)
  xyzpath.Update()
 
  frame = 0 
  error = False
  while not error: 
    frame += 1
    try: 
        atoms = Dacapo.ReadAtoms(ncfile, index = frame) 
        model.SetCartesianPositions(atoms.GetCartesianPositions())
        xyzpath.Update()
    except IndexError: 
        error = True
 
  xyzpath.Close()    
  xyzpath = NetCDFTrajectory(tmpfile)
  WriteXYZ(xyzfile, trajectory = xyzpath, repeat = opt.repeat, id = ncfile)
  os.remove(tmpfile)

Note: Since we are driving some system having only python 2.2, we do not use the function, mkstemp().

Usage

$ getatoms
usage: getatoms [-r R1 R2 R3] input_nc_file output_xyz_file
 
options:
  -h, --help            show this help message and exit
  -rR1 R2 R3, --repeat=R1 R2 R3
                        Repeat R1, R2, R3 times along the three axes
$