import giepy as gp import giechem as gc def read_xyz_file(filename): # # Reads an .xyz file with the format: # #_atoms # ignored line # a1 X Y Z # a2 X Y Z # a3 X Y Z # ... # an X Y Z # # where #_atoms = an integer indicating the number of atoms in the molecule # a1...an are element symbols or atomic numbers # X Y Z are the XYZ coordinates of the atom # input = gp.RichFile(filename) input_lines = input.next_split_line(splitter=None) tokens = input_lines.next() try: numatms = int(tokens[0]) except ValueError: print "Error reading line 1 of .xyz file" return False tokens = input_lines.next() molecule = gc.Molecule(name=tokens[0]) try: while True: tokens = input_lines.next() molecule.add_atom(id=tokens[0], coords=[ x for x in tokens[1:]]) except StopIteration: pass if numatms != molecule.num_atoms(): print "Incorrect number of atoms in .xyz file" return False return molecule if __name__ == '__main__': import sys molecule = read_xyz_file(sys.argv[1])