-
Notifications
You must be signed in to change notification settings - Fork 3
/
cfour-xyz-extractor.py
executable file
·73 lines (60 loc) · 1.97 KB
/
cfour-xyz-extractor.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
#!/usr/bin/python3
# extracts xyz file from cfour2.0 output file
# author: D.B. Magers
import sys
import argparse
# argument parser
description = 'Extracts xyz data from cfour2.0 output file'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-i', '--input', help='Set the name of the input file. (Default: output.dat)', default='output.dat')
parser.add_argument('-o', '--output', default='geo_bohr.xyz', help='Set the name of the output file. (Default: geo_bohr.xyz)')
parser.add_argument('-f', '--first', action='store_true', help='Pull first xyz instead of last')
args = vars(parser.parse_args())
# read file
f = open(args['input'], "r")
outputLines = f.readlines()
f.close()
outputLines = [x.strip() for x in outputLines]
# initialize print lines
printLines = []
# find first xyz
if args['first']:
for i, line in enumerate(outputLines):
if 'Coordinates (in bohr)' in line:
for line2 in outputLines[i+3:]:
if '----' in line2:
break
printLines.append(line2)
break
# find last xyz
else:
for i, line in reversed(list(enumerate(outputLines))):
if 'Coordinates (in bohr' in line:
for line2 in outputLines[i+3:]:
if '----' in line2:
break
printLines.append(line2)
break
# clean up extracted xyz
for i,line in enumerate(printLines):
split_line = line.split()
split_line.pop(1)
for j,item in enumerate(split_line[1:]):
if not item.startswith('-'):
item = ' '+item
split_line[j+1] = item
line = ' '.join(split_line)
printLines[i] = line
# determine number of atoms
num_atoms = str(len(printLines))
# add header
printLines.insert(0,'[bohr]')
printLines.insert(0,num_atoms)
# print
for item in printLines:
print(item)
# write file
with open('geo_bohr.xyz','w') as f_out:
for item in printLines:
f_out.write('%s\n' % item)
f_out.close()