Jump to content

Mod:Hunt Research Group/MO energies

From ChemWiki

python script to extract MO energies

#!/opt/local/bin/python3
#
# get_MOenergy.py
#
# python3 script to 
#    extract key data from gaussian log files for input into 
#    excell spreadsheets for our database
#
# to run the script type
#    python3 get_MOenergy.py file.log
#
import sys
import os
#
# check to see if the user has used the .log extenion or not
# find returns -1 if the sub-string is not found, or the index of where the sub-string starts
# we cut out the .log part if .log is found
infile=str(sys.argv[1])
i_infile=infile.find('.log')
if (i_infile == -1):
  base=infile
else:
  base=infile[:i_infile]
#endif
log_file=base+'.log'
f = open(log_file,'r')

s='input file is '
print('{0:}{1:}'.format(s,log_file))

# set some basic parameters
MOenergy = []
MOevenergy = []
MOnumber = []
MOenergy_search_string = 'Orbital energies and kinetic energies (alpha):'
MOenergy_end_string = 'Total kinetic energy from orbitals='
go=False

# read the file
count=0
line=f.readline()
#print('{0:}'.format(line))
while line:
  line=f.readline()

# find where energies are
  if MOenergy_search_string in line:
#    print ('found txt')
    line=f.readline()
    go=True
# endif

# if MO energies are found
  while go:
    line=f.readline()
#    print('{0:}'.format(line))
    if MOenergy_end_string in line:
#      print('stop')
      go=False   
    else:
      tmp=line.rstrip().split()
      MOnumber.append(int(tmp[0]))         
      MOenergy.append(float(tmp[2]))         
#      print('{0:} {1:3d} {2:09.6f}'.format('step: ',MOnumber[count],MOenergy[count]))
      count=count+1
#   endif
# endif
#endwhile


s='total no MOs is '
print('{0:}{1:} \n'.format(s,count))

s='MO energy in au and eV'
print('{0:}'.format(s))

x=0
while x < count :
  temp=MOenergy[x] * 27.2113 
  MOevenergy.append(temp)
#  print('{0:3d},{1:9.6f},{2:9.6f}'.format(MOnumber[x],MOenergy[x],MOevenergy[x]))
  x=x+1
#endwhile

#s='\n suitable for copying to excel:'
#print('{0:} \n'.format(s))

x=0
while x < count :
  print(' {0:3d}, {1:9.6f}, {2:9.6f}'.format(MOnumber[x],MOenergy[x],MOevenergy[x]))
  x=x+1
#endwhile

# finish up
f.close()
sys.exit()