-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_uv_plot.py
More file actions
179 lines (137 loc) · 5.5 KB
/
create_uv_plot.py
File metadata and controls
179 lines (137 loc) · 5.5 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script that extracts excitations from TDDFT calculations and plots the corresponding UV spectra.
Last Update 2016-09-20 by Emmanuel Nicolas
email emmanuel.nicolas-at-cea.fr
Requires Python3 to be installed.
"""
import argparse
import shlex
import sys
import os
import cclib
def main():
"""
Do all the work, extracting data, computing spectra and exporting.
Retrieves names of files to extract, eventual parameters, starts
extraction, exports the results in txt files.
"""
# Get parameters from command line
list_of_raw_files, *plotting_parameters = get_options()
# Parse all files
filenames, parsed_files = parse_all_files(list_of_raw_files)
# For every file, run the routine.
for i, file in enumerate(parsed_files):
# Print filename and log information
print(80 * "=")
print("=== " + filenames[i])
# Extract transitions
transitions = get_transitions(file)
print("Extracted {0} transitions.".format(len(transitions)))
# Format transitions and save them in file
formatted_transitions = format_transitions(transitions)
write_transitions(formatted_transitions, filenames[i])
# Setup gnuplot script
gnuplot_script = write_gnuplot(transitions,
plotting_parameters,
filenames[i])
print("")
# Run gnuplot
os.system('gnuplot {0}'.format(shlex.quote(gnuplot_script)))
print("All files have been generated.\n")
def get_options():
"""Check command line options and accordingly set computation parameters."""
parser = argparse.ArgumentParser(description=help_description(),
epilog=help_epilog())
parser.formatter_class = argparse.RawDescriptionHelpFormatter
parser.add_argument('-s', '--sigma', type=float, dest='sigma',
default='0.4',
help="Sigma value for Gaussian broadening")
parser.add_argument('-w', '--window', type=str, dest='window',
default='200:800', help="Limits for the wavelength")
parser.add_argument('outFiles', type=str, nargs='+',
help='The calculations to plot')
try:
args = parser.parse_args()
except argparse.ArgumentError as error:
print(str(error)) # Print something like "option -a not recognized"
sys.exit(2)
# Test values for validity
return ([os.path.basename(x) for x in args.outFiles], args.sigma,
args.window)
def parse_all_files(files):
"""Parse all files in list of files."""
opened_files = [cclib.io.ccopen(f) for f in files]
print(files)
filenames = [f.filename for f in opened_files]
parsed_files = [f.parse() for f in opened_files]
return (filenames, parsed_files)
def get_transitions(file):
"""
Extract transitions from logfiles.
Returns a list of lists, containing all [transition, oscillator
strength] couples, in cm-1 and unitless respectively.
"""
energies = file.etenergies
oscillator_strengths = file.etoscs
transitions = zip(energies, oscillator_strengths)
return list(transitions)
def format_transitions(transitions):
"""
Reformat transitions into a usable list.
Use them as:
353.02 0.2667
294.12 0.0000
In nm an unitless respectively.
"""
# Convert cm-1 in nm
transitions_converted = [[round(10E6 / x[0], 2), round(x[1], 4)]
for x in transitions]
# Format the list
transitions_formatted = ['{:.2f}'.format(x[0]).rjust(15) +
'{:.4f}'.format(x[1]).rjust(15)
for x in transitions_converted]
return transitions_formatted
def write_gnuplot(transitions, parameters, filename):
"""Write gnuplot files."""
print(parameters)
# Process filename
basename = os.path.splitext(filename)[0]
# Initialize script
gnuplot_script = []
gnuplot_script.append("set terminal png\n")
gnuplot_script.append("set encoding utf8\n")
gnuplot_script.append('set output "{0}.png"\n'.format(basename))
gnuplot_script.append("\n")
# Style parameters
gnuplot_script.append("set xrange [{0}]\n".format(parameters[1]))
gnuplot_script.append("set yrange [0:1]\n")
gnuplot_script.append("set style line 1 linecolor rgb '#0060ad' "
"linetype 1 linewidth 2\n")
# Add useful functions
gnuplot_script.append("Band(nu,nu_i,sigma,osc_str) = 1.3062974 * 10**8")
gnuplot_script.append(" * osc_str / sigma * exp( -( (nu - nu_i)/sigma) )")
gnuplot_script.append("")
# Write to file
gnuplot_name = basename + ".plt"
with open(gnuplot_name, mode='w') as gnuplot_file:
# Write script to file
gnuplot_file.writelines(gnuplot_script)
# return gnuplot filename
return gnuplot_name
def write_transitions(transitions, filename):
"""Write a file containing the formatted transisions."""
dat_name = os.path.splitext(filename)[0] + ".dat"
with open(dat_name, mode='w', newline="\n") as dat_file:
# Write transitions to file
dat_file.write("Wavelength (nm) Osc Strength\n")
dat_file.write('\n'.join(transitions))
def help_description():
"""Return description of program for help message."""
return ""
def help_epilog():
"""Return additionnal help message."""
return ""
if __name__ == '__main__':
main()