-
Notifications
You must be signed in to change notification settings - Fork 58
/
xyztool
executable file
·78 lines (67 loc) · 2.09 KB
/
xyztool
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
#!/usr/bin/env python
# Manipulate coordinate files in xyz format.
import sys
import operator
class Atom:
def __init__(self, name, x, y, z):
self.name = name
self.x = float(x)
self.y = float(y)
self.z = float(z)
def __repr__(self):
return repr((self.x, self.y, self.z))
def __str__(self):
return "{0:<4s} {1:11.6f} {2:11.6f} {3:11.6f}".format(self.name,
self.x, self.y, self.z)
def main():
if len(sys.argv) < 2:
print('Manipulate xyz file')
print('usage: xyztool command file.xyz')
print('commands:')
print(' sort {xyz|zxy|...}')
print(' swap {xy|xz|yz}')
print(' move {all|n1:n2} {x|y|z} dist/A')
sys.exit(1)
cmd = sys.argv[1]
file = sys.argv[-1]
atom = []
with open(file, 'r') as f:
tok = f.readline().strip().split()
natom = int(tok[0])
title = f.readline().strip()
for i in range(natom):
tok = f.readline().strip().split()
atom.append(Atom(tok[0], tok[1], tok[2], tok[3]))
if cmd.startswith('so'):
key1, key2, key3 = sys.argv[2]
atom.sort(key = operator.attrgetter(key1, key2, key3))
elif cmd.startswith('sw'):
key1, key2 = sys.argv[2]
for at in atom:
val1 = getattr(at, key1)
val2 = getattr(at, key2)
setattr(at, key1, val2)
setattr(at, key2, val1)
elif cmd.startswith('m'):
index = sys.argv[2]
if index.startswith('a'):
start = 0
stop = len(atom) - 1
else:
tok = index.split(':')
start, stop = [ int(t) - 1 for t in tok ]
key = sys.argv[3]
d = float(sys.argv[4])
for index, at in enumerate(atom):
if index >= start and index <= stop:
val = getattr(at, key)
setattr(at, key, val + d)
else:
print('bad command')
sys.exit(1)
print(natom)
print(title)
for at in atom:
print(at)
if __name__ == "__main__":
main()