/
xyzreader.py
84 lines (62 loc) · 2.4 KB
/
xyzreader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.
"""A reader for XYZ (Cartesian coordinate) files."""
from cclib.io import filereader
from cclib.parser.data import ccData
from cclib.parser.utils import PeriodicTable
class XYZ(filereader.Reader):
"""A reader for XYZ (Cartesian coordinate) files."""
def __init__(self, source, *args, **kwargs):
super(XYZ, self).__init__(source, *args, **kwargs)
self.pt = PeriodicTable()
def parse(self):
super(XYZ, self).parse()
self.generate_repr()
return self.data
def generate_repr(self):
"""Convert the raw contents of the source into the internal representation."""
assert hasattr(self, 'filecontents')
it = iter(self.filecontents.splitlines())
# Ordering of lines:
# 1. number of atoms
# 2. comment line
# 3. line of at least 4 columns: 1 is atomic symbol (str), 2-4 are atomic coordinates (float)
# repeat for numver of atoms
# (4. optional blank line)
# repeat for multiple sets of coordinates
all_atomcoords = []
comments = []
while True:
try:
line = next(it)
if line.strip() == '':
line = next(it)
tokens = line.split()
assert len(tokens) >= 1
natom = int(tokens[0])
comments.append(next(it))
lines = []
for _ in range(natom):
line = next(it)
tokens = line.split()
assert len(tokens) >= 4
lines.append(tokens)
assert len(lines) == natom
atomsyms = [line[0] for line in lines]
atomnos = [self.pt.number[atomsym] for atomsym in atomsyms]
atomcoords = [line[1:4] for line in lines]
# Everything beyond the fourth column is ignored.
all_atomcoords.append(atomcoords)
except StopIteration:
break
attributes = {
'natom': natom,
'atomnos': atomnos,
'atomcoords': all_atomcoords,
'metadata': {"comments": comments},
}
self.data = ccData(attributes)