-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency.py
145 lines (104 loc) · 4.79 KB
/
frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# created by Shaun D Ramsey - 2021
# version 06.11.21 the output filename can be tied to the input file name
# version 05.13.21 includes parsing of geom data and output to file
# licensed under the MIT license - an example of which is: https://opensource.org/licenses/MIT
# Copyright 2021 Shaun D Ramsey
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import re
import datetime
from logger import log, log_close, log_setup, LogSingleton
DEFAULT_FILENAME_EXTENSION = ".csv"
GUI = False
program_name = "Frequency/Intensity CSV"
def main_program():
log_setup("frequency", 1)
log(' **************************************** ')
log(f" Welcome to {program_name}")
log(' To use this program use: ')
log(' ****frequency [optfile.log] -o <outputfile.txt>')
log(' [] is required <> is optional ', 1)
log(' Current DEBUG/VERBOSITY LEVEL: ' + str(LogSingleton().VERBOSITY), 2)
if len(sys.argv) < 2:
log(" [*] Error, expected at least one arguments", 0)
sys.exit(1)
log(' **************************************** ', 1)
output_file = sys.argv[1]
x = output_file.rfind(".")
output_file = output_file[:x] + DEFAULT_FILENAME_EXTENSION
if len(sys.argv) == 4:
output_file = sys.argv[3]
if output_file == sys.argv[1]: #a little sanity check
output_file = "(output)" + output_file
log( f" [BGN] OUTPUT TO \"{output_file}\" BEGINS ", 1)
input_file = sys.argv[1]
if GUI:
input_file = inputFilename.get()
output_file = outputFilename.get()
f = open(input_file, "r")
of = open(output_file, "w")
freq_list = []
int_list = []
lines = f.readlines()
for line in lines:
if line[0:15] == " Frequencies --":
number_line = re.sub('\s+', ' ', line[15:])
number_line = number_line.strip()
freqs = number_line.split(" ")
freq_list.extend(freqs)
if line[0:15] == " IR Inten --":
number_line = re.sub('\s+', ' ', line[15:])
number_line = number_line.strip()
ints = number_line.split(" ")
int_list.extend(ints)
log(f" {freq_list} total length={len(freq_list)} ", 8)
log(f" {int_list} total length={len(int_list)}", 8)
log(f"Frequencies , Intensities", 3)
of.write(f"Frequencies , Intensities\n")
for i in range(len(freq_list)):
log(f"{freq_list[i]:<14} , {int_list[i]:<14}", 3)
of.write(f"{freq_list[i]:<14} , {int_list[i]:<14}\n")
of.close()
log( f" [END] OUTPUT TO \"{output_file}\" COMPLETE ", 1)
log( f" [END] Closing \"{program_name}\".", 1)
log(' **************************************** ', 1)
log_close()
if GUI:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.geometry("400x250")
root.title("Frequency Correlation")
inputFilename = tk.StringVar()
inputFilename.set("Freq_ex1.log")
if len(sys.argv) > 1:
inputFilename.set(sys.argv[1])
outputEntry = tk.Button(textvariable=inputFilename, command=lambda: inputFilename.set(filedialog.askopenfilename()))
outputEntry.grid(row=1, column=2)
outputLabel = tk.Label(text="Input File")
outputLabel.grid(row=1,column=1)
outputFilename = tk.StringVar()
outputFilename.set("freq.csv")
if len(sys.argv) > 2:
outputFilename.set(sys.argv[3])
outputEntry = tk.Entry(textvariable=outputFilename)
outputEntry.grid(row=2, column=2)
outputLabel = tk.Label(text="Output File")
outputLabel.grid(row=2,column=1)
runButton = tk.Button(text="Create", command=main_program)
runButton.grid(row=3,column=1)
root.mainloop()
else:
main_program()