Difference between revisions of "Mod:Hunt Research Group/chelpg extract"

From wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 51: Line 51:
  
 
==Python Script to Extract ESP Charges from Either opt or SP Calculations==
 
==Python Script to Extract ESP Charges from Either opt or SP Calculations==
 +
* Copy the script into a file named "extract_esp.py"
 +
* To execute the script, type "python extract_esp.py file_name (without extension.
 +
*This will display the charges in the terminal and create a file named "file_name.csv" simultaneously.
 +
*Only the last set of ESP charges is printed into the file.
  
 +
<pre>
  
 
#!/usr/bin/env python3
 
#!/usr/bin/env python3
 
#
 
#
# extract_chelpg.py
+
# extract_esp.py
 
#
 
#
 
# python script to extract and print ESP charges from a NBO=ChelpG, dipole, atomdipole job data from a gaussian log file
 
# python script to extract and print ESP charges from a NBO=ChelpG, dipole, atomdipole job data from a gaussian log file
Line 64: Line 69:
 
dir=os.getcwd()
 
dir=os.getcwd()
 
if len(sys.argv) == 1:
 
if len(sys.argv) == 1:
   print('to run the script please type: python extract_ESP.py input_file_name \(without extension\)')
+
   print('to run the script please type: python extract_esp.py input_file_name \(without extension\)')
 
   sys.exit()   
 
   sys.exit()   
 
else:  
 
else:  
Line 170: Line 175:
 
c.close()
 
c.close()
 
#
 
#
 +
 +
 +
</pre>
 +
 +
*Sample com file to run and test the code: [[File:Test water.log]]

Latest revision as of 01:19, 12 January 2024

Before running the code please ensure you have XlsxWriter python module and python version later than 3.5.


Python code to extract charge values from CHELPG calculation log file: File:Chelpg.txt

Here is an example log file to test the code: File:Test chelpg.log

Here is the correct excel file the code should generate: File:Test chelpg.xlsx


Python code to extract charge values from NBO calculation log file: File:Nbo charge script.txt

Here is an example log file to test the code: File:Test nbo.log

Here is the correct excel file the code should generate: File:Test nbo.xlsx


Instructions to run the python code from the same directory:

1. Download the code, change the extension to .py and place the code at the same directory as the .log file from CHELPG or NBO calculation you want to extract the charge values

2. Type either

python chelpg.py

or

python nbo_charge_script.py

to run the code

3. The code will ask

Enter filename:

type the name of the .log file exactly as it is, e.g.

test_chelpg.log

4. If the code has run successfully, it should generate an excel file with the title same as the log file and return

Generated your_file_name.xlsx file 

Instructions to run the python code with alias:

If you need to do CHELPG or NBO analysis on many log files, it may be more convenient to create an alias rather than copying the code to the directory each time you need to run the code. Here are the instructions:

1. Download the code, change the extension to .py and place the code at the desired directory. I would recommend creating a folder dedicated to python scripts at your home directory.

2. Go to your home directory and type:

vim .bash_profile

3. Enter following texts to .bash_profile file where the alias are:

alias nbo_charge="python ~/path/to/directory/where/you/placed/python/scripts/nbo_charge_script.py"
alias chelpg_charge="python ~/path/to/directory/where/you/placed/python/scripts/chelp.py"

4. Now go to the directory where you have the .log files you want to extract charge values from NBO or CHELPG calculation. You can now run the code by simply typing the commands below to the terminal:

nbo_charge

or

chelpg_charge

Python Script to Extract ESP Charges from Either opt or SP Calculations

  • Copy the script into a file named "extract_esp.py"
  • To execute the script, type "python extract_esp.py file_name (without extension.
  • This will display the charges in the terminal and create a file named "file_name.csv" simultaneously.
  • Only the last set of ESP charges is printed into the file.

#!/usr/bin/env python3
#
# extract_esp.py
#
# python script to extract and print ESP charges from a NBO=ChelpG, dipole, atomdipole job data from a gaussian log file
# to run the script type python extract_ESP.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_esp.py input_file_name \(without extension\)')
  sys.exit()  
else: 
  base=str(sys.argv[1])
  file=base+'.log'
  out=base+'.csv'
  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
#
col_num=[]
col_atom=[]
col_charge=[]
col_pd1=[]
col_pd2=[]
col_pd3=[]
rep=0
go=False

# open file and read lines one by one in read-only mode
f=open(file,"r")

#reading lines from the file one by one until the end of the file is reached.
line = f.readline()
while line :
  line = f.readline()

# check for the presence of ESP text and sets go as True if the text appears
# print and write fit error to out file
  if 'Charges from ESP fit' in line:
    go=True
    s='Charges from ESP fit found: '
    if rep == 0 : print('{0:}{1:}'.format(s,line[22:-1]))
    c=open(out,"w")
    c.write('{0:}{1:} \n'.format(s,line[22:-1]))   
# if rep == 0 : print('{0:}'.format(s))
    n=1
    rep=rep+1
    count=0
# close if
#
# inside the first while loop, if go is on or True, the script extracts ESP and stores in the defined variables
# only extract if you have not got to a line containing '   Tot  ' and 'Sum of ESP charges'
  while go:
    n = n+1
    m = 0
    line =f.readline()
    if '   Tot  ' in line : m=-1
    if 'Sum of ESP charges' in line : m=-1 
    if n > 3 and m == 0 :
      count=count+1
      #unpack the values from splitting the line into 6 variables
      values=line.rstrip().split()
      # appends the value, a1 into col_atom list after converting it to a string
      if len(values) == 6:
        a1, b1, c1, d1, e1, f1 = values
        col_num.append(int(a1))
        col_atom.append(str(b1))
        col_charge.append(float(c1))
        col_pd1.append(float(d1))
        col_pd2.append(float(e1))
        col_pd3.append(float(f1))
      if len(values) == 3:
        g1, h1, i1 = values
        col_num.append(int(g1))
        col_atom.append(str(h1))
        col_charge.append(float(i1))
    if m == -1 : go=False
# close while
#close while

#opens the output file in write mode, we can now write to the .csv file
c=open(out,"w")
# number of atoms
c.write('{0:} \n'.format(count))
s='comment'
c.write('{0:} \n'.format(s))
#

n=0  
#adjusts the value of n to start from a position that corresponds to the beginning of the last set of ESP data.
if rep != 1 :
  n=len(col_num)-count
  count=count*rep
  s=' sets of charges found only the last is saved'
  print('{0:}{1:}'.format(rep,s))
# endif

#Write data to csv file#
header= ["No.", "Atom", "ESP Charges"]
c.write(' '.join(header) + '\n') 
print(' '.join(header)) 
while n < count:
  c.write('{0:3d}  {1: >3s}  {2: >09.6f} \n'.format(col_num[n],col_atom[n],col_charge[n]))
  print('{0:3d}  {1: >3s}  {2: >09.6f}'.format(col_num[n],col_atom[n],col_charge[n]))
  n=n + 1
#close while

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