-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold2new.py
executable file
·158 lines (143 loc) · 4.91 KB
/
old2new.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
# =========
# MODULES
# =========
import os # OS interface: os.getcwd(), os.chdir('dir'), os.system('mkdir dir')
import sys # System-specific functions: sys.argv(), sys.exit(), sys.stderr.write()
import glob # Unix pathname expansion: glob.glob('*.txt')
import re # Regex
import argparse # commandline argument parsers
import math # C library float functions
import subprocess # Spawn process: subprocess.run('ls', stdout=subprocess.PIPE)
import numpy # Scientific computing
import typing # Support for type hints
# ==============
# PROGRAM DATA
# ==============
PROGNAME = os.path.basename(sys.argv[0])
USER = os.getenv("USER")
HOME = os.getenv("HOME")
SHELL = os.getenv("SHELL")
# ==========
# DEFAULTS
# ==========
# =================
# BASIC FUNCTIONS
# =================
def errore(message=None):
"""Error function"""
if message is not None:
print(f"ERROR: {str(message)}")
sys.exit(1)
# =================
# PARSING OPTIONS
# =================
def parseopt():
"""Parse options"""
# Create parser
parser = argparse.ArgumentParser(prog=PROGNAME, description="Command-line option parser")
# Mandatory arguments
parser.add_argument("opt1", help="First mandatory argument")
# Optional arguments
parser.add_argument("-v", "--iprint", dest="iprt", action="count", default=0, help="Set printing level")
opts = parser.parse_args()
# Check options
return opts
# ================
# WORK FUNCTIONS
# ================
def filparse(input_file):
newlines = []
skip = 0
imol = 1
wrtcoords = True
rmend = False
dofq = False
with open(input_file, "r") as file_obj:
lines = file_obj.readlines()
for nline, line in enumerate(lines):
if skip > 0:
skip = skip - 1
continue
if "ENDENGINE" in line.upper():
skip = 0
imol = 1
wrtcoords = True
rmend = False
dofq = False
newlines.append(line)
continue
if "FQQM" in line.upper():
dofq = True
newlines.append(line.replace("FQQM", "QMFQ"))
if line[0] != "#":
rmend = True
continue
if "SCREEN" in line.upper() and dofq:
newlines.append(line.upper().replace("SCREEN", "Kernel"))
continue
if "TOTALCHARGE" in line.upper() and dofq:
newlines.append(line.upper().replace("TOTALCHARGE", "MolCharge"))
continue
if "END" in line.upper() and rmend and dofq:
rmend = False
continue
if "FQPAR" in line.upper():
if "ALPHA" in lines[nline + 4]:
skip = 10
else:
skip = 8
newlines.append(" AtomType\n")
newlines.append(" Symbol O\n")
if "ALPHA" in lines[nline + 4]:
newlines.append(" Chi 0.2908429850\n")
newlines.append(" Eta 0.5625181140\n")
newlines.append(" Alpha 2.2187983720\n")
else:
newlines.append(" Chi 0.189194\n")
newlines.append(" Eta 0.523700\n")
newlines.append(" SubEnd\n")
newlines.append(" AtomType\n")
newlines.append(" Symbol H\n")
if "ALPHA" in lines[nline + 4]:
newlines.append(" Chi 0.1675711970\n")
newlines.append(" Eta 0.6093265770\n")
newlines.append(" Alpha 1.1906416090\n")
else:
newlines.append(" Chi 0.012767\n")
newlines.append(" Eta 0.537512\n")
newlines.append(" SubEnd\n")
continue
if "GROUP" in line and dofq:
skip = 5
if wrtcoords:
newlines.append(f" Coords\n")
wrtcoords = False
newlines.append(f"{lines[nline+2].rstrip()} {imol}\n")
newlines.append(f"{lines[nline+3].rstrip()} {imol}\n")
newlines.append(f"{lines[nline+4].rstrip()} {imol}\n")
imol = imol + 1
elif not wrtcoords:
newlines.append(f" SubEnd\n")
newlines.append(line)
wrtcoords = True
else:
newlines.append(line)
print("".join(newlines))
# filout = 'out'
# with open(filout, 'w') as file_obj:
# for line in newlines:
# file_obj.write(line)
# ==============
# MAIN PROGRAM
# ==============
def main():
# PARSE OPTIONS
opts = parseopt()
filparse(opts.opt1)
sys.exit()
# ===========
# MAIN CALL
# ===========
if __name__ == "__main__":
main()