-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoritaSFG.py
executable file
·247 lines (179 loc) · 6.71 KB
/
MoritaSFG.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import csv
import numpy
import sys
import os
from scipy import *
import matplotlib.text
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
NAMES = {'Na2SO4':r'Na$_2$SO$_4$', 'NaCl':r'NaCl', 'NaNO3':r'NaNO$_3$'}
class SFGData:
def __init__(self,file):
self.filename = file
self.systemName = file.split('/')[-2]
self.data = loadtxt(file)
self.freq = self.data[:,0] # Frequency for each data point
self.real = list(self.data[:,1]) # The real part
self.imag = list(self.data[:,2]) # The imaginary part
# The complex representation of the data
self.comp = map(lambda r,i: complex(r,i), self.real, self.imag)
# chi is the magnitude-squared of the complex lineshape
self.chi = map(lambda c: real(c*c.conjugate()), self.comp)
def Filename(self):
return self.filename
def SystemName(self):
return self.systemName
def Freq(self):
return self.freq
def Real(self):
return self.real
def Imag(self):
return self.imag
def Complex(self):
return self.comp
def Chi(self):
return self.chi
# returns the chi lineshape scaled by some value
def ScaleChi_LowFreqs(self,scale):
return map(lambda x: x * scale, self.chi)
def NormalizeChiToMaximum(self):
# find the max point in the curve
max_peak = max(self.chi)
return self.ScaleChi_LowFreqs(1.0/max_peak)
def NormalizeChiToArea(self):
# find the area of the curve:
area = numpy.trapz(self.chi, self.x, dx=0.1)
return self.ScaleChi_LowFreqs(1.0/area)
class MoritaSFG:
def __init__(self,files):
self.files = files
# file i/o to get the data
self.h2o = SFGData(files[0])
self.sfgdata = [SFGData(f) for f in self.files[1:]]
self.InitPlot()
#titles = [r'CCl$_4$-NaCl', r'CCl$_4$-NaNO$_3$', r'CCl$_4$-Na$_2$SO$_4$', r'Salt Comparison']
for dat,ax in zip(self.sfgdata,self.axs):
self.fig.sca(ax)
self.PlotChi(dat,ax)
self.SetTicksAndLabels(dat,ax)
self.ShowLegend(ax)
plt.xlim(2800,3800)
#plt.ylim(-2.3,1.2)
plt.show()
def InitPlot(self):
# Set up the plot parameters (labels, size, limits, etc)
self.fig = plt.figure(num=1, facecolor='w', edgecolor='w', frameon=True)
self.axs = []
for i in range(len(self.sfgdata)):
self.axs.append(self.fig.add_subplot(2,2,i+1))
plt.title(NAMES[self.sfgdata[i].SystemName()], size=30)
self.axs[i].set_yticklabels(())
#self.axs[i].set_ylabel(r'$\left|\chi^{(2)}\right|^2 (a.u.)$', size=35)
self.axs[i].set_ylabel(r'$\chi^{(2)} (a.u.)$', size=35)
return
'''
extra_axes = True
if extra_axes == True:
ax1 = self.fig.add_subplot(3,1,1)
else:
ax1 = self.fig.add_subplot(1,1,1)
ax1.set_title('SFG Spectrum', size='x-large')
if extra_axes == True:
ax2 = self.fig.add_subplot(3,1,2)
ax2.set_ylabel(r'Re $\chi^{(2)}$', size='xx-large')
ax3 = self.fig.add_subplot(3,1,3)
ax3.set_ylabel(r'Im $\chi^{(2)}$', size='xx-large')
'''
'''
for ax in self.fig.get_axes():
#ax.set_yticklabels([])
#ax.axhline(xmin=2800.0/4000.0, xmax=3800.0/4000.0, color='k', linestyle='-')
ax.yaxis.set_major_locator(MaxNLocator(1))
ax.yaxis.grid(True)
ax.xaxis.grid(False)
'''
# Plot out all the real and imaginary parts
def PlotRealImag(self,dat,ax):
ax.plot(dat.Freq(), dat.Real(), 'b-', linewidth=2, label='Real')
ax.plot(dat.Freq(), dat.Imag(), 'g-', linewidth=2, label='Imaginary')
def ScaleChi_LowFreqs(self,dat,scale,cut_freq):
cut_ind = list(dat.Freq()).index(cut_freq)
re = dat.Real()
# cut the imaginary part into two and scale the lower one
imag = dat.Imag()
low_imag = imag[:cut_ind]
low_imag = map(lambda im: im*scale, low_imag) # scale the complex part somehow
high_imag = imag[cut_ind:]
new_imag = low_imag + high_imag
comp = map(lambda r,i: complex(r,i), re, new_imag)
chi = map(lambda c: real(c*c.conjugate()), comp)
return chi
def ScaleChi(self,dat,scale):
re = dat.Real()
# cut the imaginary part into two and scale the lower one
imag = dat.Imag()
new_imag = map(lambda im: im*scale, imag) # scale the complex part somehow
comp = map(lambda r,i: complex(r,i), re, new_imag)
chi = map(lambda c: real(c*c.conjugate()), comp)
return (re,new_imag,chi)
def NormalizeListToMax(self,dat):
max_peak = max(dat)
return map(lambda x: x/max_peak, dat)
def MakeFreqList(self,min,max,numbins):
df = (max-min)/float(numbins)
return [min + x*df for x in range(numbins)]
def MoveXAxis(self,x,shift):
return map(lambda y: y+shift, x)
def StretchFreq(self,freq,new_min,cut_freq):
cut_freq_id = list(freq).index(cut_freq)
low = list(freq[:cut_freq_id])
high = list(freq[cut_freq_id:])
new_low = self.MakeFreqList(new_min, cut_freq, len(low))
return new_low + high
# plot the chi-squared lineshape
def PlotChi(self,dat,ax):
CUTFREQ = {'NaCl':3600.0, 'NaNO3':3600.0, 'Na2SO4':3630.0}
LOWFREQ = {'NaCl':1500.0, 'NaNO3':2000.0, 'Na2SO4':1000.0}
SCALE = {'NaCl':1.4, 'NaNO3':1.5, 'Na2SO4':0.7}
COLOR = {'NaCl':'g-', 'NaNO3':'r-', 'Na2SO4':'purple'}
cut_freq = CUTFREQ[dat.SystemName()]
low_freq = LOWFREQ[dat.SystemName()]
scale = SCALE[dat.SystemName()]
line_color = COLOR[dat.SystemName()]
# scale the chi
h2o_chi = self.NormalizeListToMax(self.h2o.Chi())
h2o_real = self.h2o.Real()
h2o_imag = self.h2o.Imag()
real = self.ScaleChi (dat, scale)[0]
imag = self.ScaleChi (dat, scale)[1]
chi = self.NormalizeListToMax(self.ScaleChi (dat, scale)[2])
# scale the frequencies
dat_freq = self.StretchFreq (dat.Freq(), low_freq, cut_freq)
if dat.SystemName() == 'NaNO3':
dat_freq = self.MoveXAxis(dat_freq,37)
#dat_freq = dat.Freq()
h2o_freq = self.StretchFreq (self.h2o.Freq(), 2000, 3600.0)
#h2o_freq = self.h2o.Freq()
#ax.plot(dat.Freq(), dat.Chi(), 'b:', linewidth=4, label=dat.SystemName()) #label=r'$|\chi|^{2}$')
ax.plot(dat_freq, real, 'r-', linewidth=4, label=r'Re $\chi^{(2)}$')
ax.plot(dat_freq, imag, 'b-', linewidth=4, label=r'Im $\chi^{(2)}$')
#ax.plot(h2o_freq, h2o_real, 'k:', linewidth=4, label=r'H$_2$O')
def SetTicksAndLabels(self,dat,ax):
#axs[i].set_yticklabels([])
labels = ax.get_xticklabels() + ax.get_yticklabels()
for label in labels:
label.set_size('x-large')
def ShowLegend(self,ax):
# set some legend properties. All the code below is optional. The
# defaults are usually sensible but if you need more control, this
# shows you how
leg = plt.legend(loc='best', shadow=True, fancybox=True)
# the matplotlib.patches.Rectangle instance surrounding the legend
frame = leg.get_frame()
frame.set_facecolor('1.00') # set the frame face color to light gray
# matplotlib.text.Text instances
for t in leg.get_texts():
t.set_fontsize('x-large') # the legend text fontsize
# matplotlib.lines.Line2D instances
for l in leg.get_lines():
l.set_linewidth(4.0) # the legend line width