import numpy as np
from string import Template
import subprocess as sp
import re

zs=np.linspace(0.3, 1.8, 15)
energy_match=re.compile('Total (SCF|DFT).* (?P<en>[^ ]*)$')

with open('h2.templ') as infile:
    templ=infile.read()

templ=Template(templ)

for z in zs:
    nw=templ.substitute(dict(z=z))
    nw_name='h2-%.1f.nw'%z
    out_name='h2-%.1f.out'%z
    with open(nw_name,'w') as outfile:
        outfile.write(nw)
    with open(out_name,'w') as outfile:
        sp.call(["nwchem", nw_name], stdout=outfile)
    for line in open(out_name,'r'):
        match=re.search(energy_match, line)
        if match:
            en=match.group('en')
            print("{:.1f}  {:s}".format(z,en))




