Jump to content

Talk:Mod:Hunt Research Group/extract single geom

From ChemWiki

python script to extract single structure from Standard Orientaion

  • copy the code into a script called "extract_structure.py"
  • to execute the script type "python extract_structure.py file_name (without extension)
  • this will print to terminal and create a file file_name.xyz

•the last structure is saved

#
# extract_structure.py
#
# python script to extract and print single standard orintation from a gaussian log file
# to run the script type python extract_structure.py input_file_name (without extension)
#
import sys
import os
dir=os.getcwd()
#
if len(sys.argv) == 1:
  print('to run the script please type: python extract_structure.py input_file_name \(without extension\)')
  sys.exit()
else:
  base=str(sys.argv[1])
  file=base+'.log'
  out=base+'.xyz'
  s='directory is '
  print('{0:}{1:}'.format(s,dir))
  s='input file is '
  print('{0:}{1:}'.format(s,file))
  s='output file is '
  print('{0:}{1:}'.format(s,out))
#close if
#
# set some defaults
col_num=[]
col_atom=[]
col_type=[]
col_x=[]
col_y=[]
col_z=[]
rep=0
go=False

# open file and read lines one by one
f=open(file,"r")

line =f.readline()
while line :
  line =f.readline()

#==extract GEOMETRY ==
# check for standard orientation text set go if the text appears
  if 'Standard orientation:' in line:
    go=True
    s='Structure in standard orientation found '
    if rep == 0 : print('{0:}'.format(s))
    n=1
    rep=rep+1
    count=0
# close if
#
# check for input orientation text set go if the text appears
  if 'Input orientation:' in line:
    go=True
    s='Input orientation: found '
    if rep == 0 : print('{0:}'.format(s))
    n=1 
    rep=rep+1
    count=0
# close if
#
# if go is on then print out the structure
# only print if you have not got to rot const line
# don't print if its one of the separation lines
  while go:
    n = n+1
    m = 0
    line =f.readline()
    if 'Rotational constants (GHZ):' in line: m=-1    
    if '---------' in line: m=1
    if n > 4 and m == 0 :
      count=count+1
      a1,b1,c1,d1,e1,f1=line.rstrip().split()
      col_num.append(int(a1))
      col_atom.append(int(b1))
      col_type.append(int(c1))
      col_x.append(float(d1))
      col_y.append(float(e1))
      col_z.append(float(f1))
    if m == -1 : go=False
# close while
#close while

#now write to the .xyz file
#we may have muliple geometries print only the last
c=open(out,"w")
s='number of atoms '
print('{0:}{1:}'.format(s,count))
c.write('{0:}{1:} \n'.format(s,count))
s='atomic no, xyz coords'
print('{0:}'.format(s))
c.write('{0:} \n'.format(s))
#
n=0
if rep != 1 :
  n=len(col_num)-count
  count=count*rep
  s=' structures found only the last is saved'
  print('{0:}{1:}'.format(rep,s))
# endif
while n < count:
  c.write('{0:>4d}  {1: 09.6f}  {2: 09.6f}  {3: 09.6f} \n'.format(col_atom[n],col_x[n],col_y[n],col_z[n]))
  print('{0:>4d} {1:>4d}  {2: 09.6f}  {3: 09.6f}  {4: 09.6f}'.format(col_num[n],col_atom[n],col_x[n],col_y[n],col_z[n]))
  n=n + 1
#close while

# close all open files
f.close()
c.close()
#