#!/usr/bin/env python
###############################################################################
#
# Convert DFTB+ gen format to XYZ
#
###############################################################################
import sys
import optparse
import numpy as np
from dptools.gen import Gen
from dptools.xyz import XYZ

USAGE = """usage: %prog [options] INPUT

Converts the given INPUT file in DFTB+ GEN format to XYZ. Per default, if the
filename INPUT is of the form PREFIX.gen the result is stored in PREFIX.xyz,
otherwise in INPUT.xyz. You can additionally store lattice vectors of the GEN
file in a separate file"""

def main():
    parser = optparse.OptionParser(usage=USAGE)
    parser.add_option("-l", "--lattice-file", action="store", dest="lattfile",
                      help="store lattice vectors in an external file")
    parser.add_option("-o", "--output", action="store", dest="output",
                      help="override the name of the output file (use '-' for "
                      "standard output)")
    parser.add_option("-c", "--comment", action="store", dest="comment",
                      default="", help="comment for the second line of the "
                      "xyz-file")
    (options, args) = parser.parse_args()


    if len(args) != 1:
        parser.error("You must specify exactly one argument (input file).")
    infile = args[0]

    gen = Gen.fromfile(infile)
    xyz = XYZ(gen.geometry, options.comment)
    if options.output:
        if options.output == "-":
            outfile = sys.stdout
        else:
            outfile = options.output
    else:
        if infile.endswith(".gen"):
            outfile = infile[:-4] + ".xyz"
        else:
            outfile = infile + ".xyz"
    xyz.tofile(outfile)

    if gen.geometry.periodic and options.lattfile:
        fp = open(options.lattfile, "w")
        for vec in gen.geometry.latvecs:
            fp.write("{0:18.10E} {1:18.10E} {2:18.10E}\n".format(*vec))
        fp.close()


if __name__ == "__main__":
    main()
