#!/usr/bin/env python
from optparse import OptionParser

from ASE.IO.xyz import WriteXYZ
from ASE.Trajectories.NetCDFTrajectory import NetCDFTrajectory
from Dacapo import Dacapo
import tempfile

parser = OptionParser(usage='%prog [-r R1 R2 R3] nc trajectory',
                      version='%prog 0.1')
parser.add_option('-r', '--repeat', type='int', nargs=3,
                  help='Repeat R1, R2, R3 times along the three axes',
                  metavar='R1 R2 R3')

options, args = parser.parse_args()

if len(args) != 2:
    parser.print_help()
    raise SystemExit

print 'Creating trajectory file:' ,args[1]

atoms = Dacapo.ReadAtoms(args[0],index=0) 
atoms.SetCalculator(None)
trajectory = NetCDFTrajectory(args[1],atoms)
trajectory.Update()

frame = 0 
error = False
while not error: 
    frame +=1
    print 'reading frame ',frame
    try: 
        atoms1 = Dacapo.ReadAtoms(args[0],index=frame) 
        atoms.SetCartesianPositions(atoms1.GetCartesianPositions())
        print atoms
        trajectory.Update()
    except IndexError: 
        error = True

trajectory.Close()    

