Jump to content

Mod:Hunt Research Group:frequency spectrum script

From ChemWiki

This script takes a .csv file containing frequency and intensity data, and generates a file with x-y co-ordinates of the frency spectrum, for ir and raman.

# how to run the script
# run using python3, has only been tested on python3.6
# freq_data should be a csv file, 1st line is "Frequency,IR,Raman," then each line is: frequency (in wavenumbers), relative ir intensity, relative raman intensity.
# freq_data.csv can be generated from a gaussian frequency .log file, using the script gaussian_input.py
# output_file is where the final spectrum x,y values will be printed to in csv format. Use a csv extension to make it easier to open in excel.
# another file called detail_file.csv will be created, containing the individual spectrum of each peak.
# line_width is the full width half maximum of the peaks in the generated spectrum.
# wave_numbers is the range of frequency values to include, in wavenumbers, from 0.
# default is ir spectrum, to change to raman spectrum check line 44.

from sys import argv
script, freq_data, output_file = argv

wave_numbers = 4001
line_width = 1

# read in freq_data file

freq_data_handle = open(freq_data, "r")

# make each line in the file into a tuple: line_tuple
# add each tuple to freq_data_list

freq_data_list = []

for line in freq_data_handle:
    line_tuple = line.split(",")
    line_tuple = line_tuple[0:3]
    freq_data_list.append(line_tuple)

# remove data header line from freq_data_list, and close the freq_data file.

freq_data_list = freq_data_list[1:]
freq_data_handle.close()

# create master list, this will be a list of lists of the y values, one list of y values is created for each peak.

master_list = []

# calculating a y_list for each peak, and adding the y_lists to master_list

for tuple in freq_data_list:
    frequency = tuple[0]
    frequency = float(frequency)
# if you want raman instead of ir then change the tuple index to 2.
    inten = tuple[1]
    inten = float(inten)
    x = 1
    y_list = []

    while x < wave_numbers:
        y = inten*(1/(3.14*2*line_width))*((2*line_width)**2/((x-frequency)**2+(2*line_width)**2))
        y_list = y_list + [y]
        x = x + 1
    master_list.append(y_list)

# adding each y_list into one total list, y_total_list

total_y_list = []
counter = 0
while counter < wave_numbers - 1:
    total_y = 0
    for list in master_list:
        total_y = total_y + list[counter]

    total_y = format(total_y, '.4f')
    total_y_list = total_y_list + [total_y]
    counter = counter + 1

# writing the x,y file

i_file = open(output_file, 'w')

x = 1
for element in total_y_list:
    i_file.write(str(x) + ", " + str(element) + "\n")
    x = x + 1

#######################################################


# making the x-list

x_list = []
x = 1
while x < wave_numbers:
    x_list = x_list + [x]
    x = x + 1

# inserting the x-list to the master_list to get the x_master_list, a list of lists. The first list is the x-values, the subsequent lists are the y-values for each separate peak.

x_master_list = []
x_master_list.append(x_list)

for list in master_list:
    x_master_list.append(list)

# writing to detail_file to get separate y co-ordinates for each peak.

d_file = open("detail_file.csv", 'w')

x = 0
while x < wave_numbers - 1:
    line_string = ""
    for list in x_master_list:
        y_value = format(list[x], '.4f')
        line_string = line_string + y_value + ","
    line_string = line_string + "\n"
    d_file.write(line_string)
    x = x + 1

# closing files.

d_file.close()
i_file.close()
freq_data_handle.close()


This script generates a .csv input file for the script above from a gaussian frequency .log file.


from sys import argv
script, g_file_name, spectrum_input_file = argv

# opening and setting up files

g_file = open(g_file_name, 'r')
i_file = open(spectrum_input_file, 'w')

i_file.write("Frequency,IR,Raman,\n")

# creating lists for the data

freqlist = []
irlist = []
ramanlist = []

# finding the frequencies and adding them to freqlist

for line in g_file:
    if "Frequencies" in line:
        freqlist.extend(line.split())
        freqlist.remove("Frequencies")
        freqlist.remove("--")

g_file.seek(0)

# finding the ir intensities and adding them to irlist

for line in g_file:
    if "IR Inten" in line:
        irlist.extend(line.split())
        irlist.remove("IR")
        irlist.remove("Inten")
        irlist.remove("--")

g_file.seek(0)

# finding the raman intensities and adding them to ramanlist
# if you have raman data in your log file then uncomment this line and use the second data = zip line.

#for line in g_file:
#   if "Raman Activ" in line:
#       ramanlist.extend(line.split())
#       ramanlist.remove("Raman")
#       ramanlist.remove("Activ")
#       ramanlist.remove("--")

# zipping the lists into a list of lists, each lists contains frequency, ir_inten, raman_inten

data = zip(freqlist,irlist)
#data = zip(freqlist,irlist,ramanlist)

# writing the data to the spectrum_input_file

for sublist in data:
    for item in sublist:
        i_file.write(item + ',')
    i_file.write('\n')

# closing files

g_file.close()
i_file.close()