Talk:Mod:Hunt Research Group/extract ESP charges
Appearance
python script to extract ESP ie CHelpG charges
- copy the code into a script called "extract_charges.py"
- to execute the script type "python extract_charges.py file_name (without extension)
- this will print to terminal the charges and create a file file_name.chg
- if there are multiple sets of charges only the last set are printed to the file
#!/usr/bin/python # extract_charges.py # # python script to extract and print ESP and NBO charges from a gaussian log file # to run the script type python extract_charges.py file_name (without extension) # charges will be printed and sent to file_name.chg # import sys import os dir=os.getcwd() # if len(sys.argv) == 1: print('to run the script please type: python extract_charges.py file_name (without extension)') sys.exit() else: base=str(sys.argv[1]) if base.endswith('log'): file=base base=base[0:-4] else: file=base+'.log' # endif out=base+'.chg' 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 go=False totalN=0 totalE=0 total=0 esp=0 nbo=0 col_Enum=[] col_Eatom=[] col_Eesp=[] col_Nnum=[] col_Natom=[] col_Nnbo=[] # open file and read lines one by one f=open(file,"r") c=open(out,"w") line =f.readline() while line: # read a line from the file line =f.readline() #==ESP charges== # check for key text for ESP and set go 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 ' print('{0:}{1:}'.format(s,line[22:-1])) c.write('{0:}{1:} \n'.format(s,line[22:-1])) esp=esp+1 n=0 m=0 # close if # if go is on then read through the next section and print out the charges # put the numbers in an array for atom number, atom type and esp value # but also check if the charges are finished and exit if they are # if see sum of ESP then stop while go: n = n+1 line =f.readline() if 'Sum of ESP charges' in line: m=1 if n > 2 and m == 0 : a1,b1,c1=line.rstrip().split() col_Enum.append(int(a1)) col_Eatom.append(b1) col_Eesp.append(float(c1)) print('{0:>3s} {1:>3s} {2:>10s}'.format(a1,b1,c1)) # close if if m == 1 : go=False totalE=n-3 # close if # close while # # now keep going to search for NBO charges #==NBO charges== # check for key text set go if the text appears # print and write key lines to out file if 'Summary of Natural Population Analysis:' in line: go=True s='Charges from NBO ' print('{0:}'.format(s)) p=0 n=0 nbo=nbo+1 # close if # if go is on then print out the charges # but also check if the charges are finished and exit if they are while go: n = n+1 line =f.readline() if ' ======================' in line: p=1 if n > 5 and p == 0 : a1,b1,c1,d1,e1,f1,g1=line.rstrip().split() col_Natom.append(a1) col_Nnum.append(int(b1)) col_Nnbo.append(float(c1)) print('{0:>3s} {1:>3s} {2:>10s}'.format(a1,b1,c1)) if p == 1 : go=False totalN=n-6 # close if # close while #now write to the .chg file #formating decimal, total length=8 with 6 after decimal point leading zero to add space for plus #formating the ^ means place in middle #formating the > means right justify # # we could have only ESP or only NBO # also if there has been more than one set of charges advance counter to last set of charges if totalE > 0 and totalN == 0 : n=0 total=totalE if esp != 0 : n=len(col_Enum)-total total=total*esp s='only printing last set of charges' # c.write('{0:} \n'.format(s)) print('{0:}'.format(s)) # endif # print 0 into the NBO charges x=0 while x < total : col_Nnbo.append(0.0) x=x+1 # close while c.write('{0:>4s}{1:>4s} {2:^9s} \n'.format('no.','atm','ESP')) while n < total : c.write('{0:>4d}{1:>4s} {2: 08.6f} \n'.format(col_Enum[n],col_Eatom[n],col_Eesp[n])) n=n + 1 # close while #close if #endif if totalE == 0 and totalN > 0 : n=0 total=totalN if nbo != 0 : n=len(col_Nnum)-total total=total*nbo s=' sets of charges only printing last set of charges' c.write('{0:}{0:} \n'.format(nbo,s)) print('{0:}'.format(s)) # endif x=0 while x < total : col_Enum[x]=col_Nnum[x] col_Eatom[x]=col_Natom[x] col_Eesp.append(0.0) x=x+1 # close while c.write('{0:>4s}{1:>4s} {2:^9s} \n'.format('no.','atm','NBO')) while n < total : c.write('{0:>4d}{1:>4s} {2: 08.6f} \n'.format(col_Enum[n],col_Eatom[n],col_Nnbo[n])) n=n + 1 # close while #close if if totalN > 0 and totalE > 0 : n=0 total=totalE if esp != 0 : n=len(col_Enum)-total total=total*esp s=' sets of charges only writing last set of charges' print('{0:}{1:}'.format(nbo,s)) c.write('{0:}{1:} \n'.format(nbo,s)) # endif # # now write out # c.write('{0:>4s}{1:>4s} {2:^9s} {3:^9s} \n'.format('no.','atm','ESP','NBO')) while n < total : c.write('{0:>4d}{1:>4s} {2: 08.6f} {3: 08.6f} \n'.format(col_Enum[n],col_Eatom[n],col_Eesp[n],col_Nnbo[n])) n=n + 1 # close while #close if # close all open files c.close() f.close() #