summaryrefslogtreecommitdiff
path: root/sim/src/mergemmpxyz.py
blob: 59ff18f8b7d99f9dc9abeb1e8ea0acf024326229 (plain)
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
#!/usr/bin/python
# Copyright 2006 Nanorex, Inc.  See LICENSE file for details.

# usage:
#
# mergemmpxyz.py file.mmp file.xyz > merged.mmp
#

import sys
import re

if len(sys.argv) != 3:
    print >>sys.stderr, "usage: mergemmpxyz.py file.mmp file.xyz > merged.mmp"
    sys.exit(1)

atomLinePattern = re.compile(r"^(atom.*\(.*\).*\().*(\).*)$")
xyzLinePattern = re.compile(r"^\S+\s+([-.0-9]+)\s+([-.0-9]+)\s+([-.0-9]+)\s*$")

mmpInputFileName = sys.argv[1]
xyzInputFileName = sys.argv[2]

mmpInputFile = file(mmpInputFileName, "r")
xyzInputFile = file(xyzInputFileName, "r")
# skip the first two lines of xyz file
xyzInput = xyzInputFile.readline() # number of atoms
xyzInput = xyzInputFile.readline() # RMS=0.12345

mmpInput = mmpInputFile.readline()
while mmpInput:
    mmpInput = mmpInput[:-1] # strip trailing newline
    m = atomLinePattern.match(mmpInput)
    if m:
        xyzInput = xyzInputFile.readline()
        if xyzInput:
            m2 = xyzLinePattern.match(xyzInput)
            if m2:
                x = int(float(m2.group(1)) * 1000.0)
                y = int(float(m2.group(2)) * 1000.0)
                z = int(float(m2.group(3)) * 1000.0)
                mmpInput = "%s%d, %d, %d%s" % (m.group(1), x, y, z, m.group(2))
            else:
                print >>stderr, "xyz format error: " + xyzInput,
                sys.exit(1)
        else:
            print >>stderr, "not enough lines in xyz file"
            sys.exit(1)
    print mmpInput
    mmpInput = mmpInputFile.readline()
mmpInputFile.close()
xyzInputFile.close()