This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
408 lines (336 loc) · 12.4 KB
/
functions.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#%%
import math
from decimal import Decimal
from radis import calc_spectrum, Spectrum
# NOTE for graphing
import numpy as np
import matplotlib.pyplot as plt
# function presents error and usage to user, then quits
def __error(error_text):
print(error_text)
print(
" usage: python3 generate_spectra.py <source (t or g)> <min-wavenumber (cm^-1)> <max-wavenumber (cm^-1)> <beamsplitter AR_ZnSe or AR_CaF2> <cell_window KBr or CaF2 or ZnSe> <detector MCT or InSb>"
)
quit()
def __loadData(s):
data = {}
for key, val in zip(s[0], s[1]):
data[float(key)] = float(val)
return data
def __KBr(data):
if data == None:
return False
for x in data:
datapoint = (0.92267) / (1 + (25.66477 / (x / 1000)) ** -12.35159) ** 0.17344
data[x] = datapoint * data[x]
return data
def __CaF2(data):
if data == None:
return False
for x in data:
datapoint = (0.93091) / (1 + (11.12929 / (x / 1000)) ** -12.43933) ** 4.32574
data[x] = datapoint * data[x]
return data
def __ZnSe(data):
if data == None:
return False
for x in data:
x_um = x / 1000
datapoint = (0.71015) / (
(1 + (20.99353 / x_um) ** -19.31355) ** 1.44348
) + -0.13265 / (2.25051 * math.sqrt(math.pi / (4 * math.log(2)))) * math.exp(
-4 * math.log(2) * ((x_um - 16.75) ** 2) / (2.25051**2)
)
data[x] = datapoint * data[x]
return data
def __sapphire(data):
if data == None:
return False
for x in data:
# Gets accurate graph with numpy float128 but throws runtime overflow error
# datapoint = Decimal(0.78928) / Decimal(1 + (11.9544 / (x / 1000)) ** -12.07226 ) ** (Decimal(6903.57039))
datapoint = np.float128(0.78928) / np.float128(
1 + (11.9544 / (x / 1000)) ** -12.07226
) ** (np.float128(6903.57039))
dp2 = datapoint * np.float128(data[x])
data[x] = dp2
return data
def __AR_ZnSe(data):
if data == None:
return False
for x in data:
x_um = x / 1000
datapoint = (
(0.82609) / ((1 + ((34.63971 / x_um) ** -8.56269)) ** 186.34792)
+ -0.47
/ (0.55 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 1.47) ** 2) / (0.55**2))
+ -0.03456
/ (0.4 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 2.88) ** 2) / (0.4**2))
+ -0.009
/ (0.3 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 6.16) ** 2) / (0.3**2))
+ -0.09
/ (1 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 16.2) ** 2) / (1**2))
+ -0.08
/ (1 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 17.4) ** 2) / (1**2))
+ 1.12
/ (8 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 9.5) ** 2) / (8**2))
+ 0.11546
/ (2 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 4.9) ** 2) / (2**2))
+ 0.21751
/ (2 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 2.6) ** 2) / (2**2))
+ -0.05
/ (0.07 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 0.8) ** 2) / (0.07**2))
)
data[x] = datapoint * data[x]
return data
def __AR_CaF2(data):
if data == None:
return False
for x in data:
x_um = x / 1000
datapoint = (
(0.9795) / ((1 + ((18.77617 / x_um) ** -6.94246)) ** 91.98745)
+ -0.06
/ (0.08 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 0.76) ** 2) / (0.08**2))
+ -0.06
/ (0.2 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * (x_um - 1.06) ** 2 / 0.20**2)
+ -0.6
/ (3.0 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 4.85) ** 2) / (3.0**2))
+ -0.35
/ (1.0 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 9.40) ** 2) / (1.00**2))
+ 0.05
/ (0.8 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 2.60) ** 2) / (0.8**2))
+ 0.04
/ (0.5 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 7.75) ** 2) / (0.50**2))
+ -0.01
/ (0.6 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 6.55) ** 2) / (0.6**2))
+ 0.01
/ (0.5 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 1.82) ** 2) / (0.5**2))
)
data[x] = datapoint * data[x]
return data
def __InSb(data):
if data == None:
return False
for x in data:
x_um = x / 1000
datapoint = 1.97163e11 * (1 / (1 + math.exp(-(x_um - 5.3939) / 1.6624))) * (
1 - 1 / (1 + math.exp(-(x_um - 5.3939) / 0.11925))
) + (3.3e10) / (2.44977 * math.sqrt(math.pi / (4 * math.log(2)))) * math.exp(
-4 * math.log(2) * ((x_um - 5) ** 2) / (2.44977**2)
)
# add noise to InSb
data[x] = (datapoint + np.random.normal(0, 200000000)) * data[x]
return data
def __MCT(data):
if data == None:
return False
for x in data:
x_um = x / 1000
datapoint = (
(1.98748 * (10**9))
+ (2.10252 * (10**10))
* (1 / (1 + math.exp(-(x_um - 20.15819) / 5.73688)))
* (1 - 1 / (1 + math.exp(-(x_um - 20.15819) / 1.11659)))
+ (1.3 * (10**9))
/ (2 * math.sqrt(math.pi / (4 * math.log(2))))
* math.exp(-4 * math.log(2) * ((x_um - 18.6) ** 2) / (2**2))
)
# add noise to MCT
data[x] = (datapoint + np.random.normal(0, 20000000)) * data[x]
return data
def __sPlanck(spectrum, temp):
H = 6.62606957e-34
C = 2.99792458e8
K_B = 1.3806488e-23
if spectrum == None:
return False
for x in spectrum:
x2 = x * (10**-9)
p = ((0.2 * H * (C**2)) / ((x2**4) * x)) * (
1 / (math.exp((H * C) / (x2 * K_B * temp)) - 1)
)
spectrum[x] = spectrum[x] * p
return spectrum
def graph(spectrum):
xs = []
ys = []
for key in spectrum:
xs.append(float(key))
ys.append(float(spectrum[key]))
plt.plot(np.array(xs), np.array(ys), "blue")
plt.show()
def check_params(source, min_wavenum, max_wavenum, beamsplitter, cell_window, detector):
# check if source is correct (t or g)
if (source != "t") and (source != "g"):
__error(" source needs to be <t> or <g>. provided source: %s" % (source))
# set source_temp based on source input
if source == "g":
source_temp = 1700
elif source == "t":
source_temp = 3100
else:
__error(" source needs to be <t> or <g>. provided source: %s" % (source))
# check if wavenumbers are correct
if min_wavenum < 400:
__error(
" min wavenumber is out of range (400 - 12500). provided min: %s"
% (min_wavenum)
)
elif min_wavenum > 12500:
__error(
" max wavenumber is out of range (400 - 12500). provided max: %s"
% (max_wavenum)
)
elif min_wavenum > max_wavenum:
__error(
" min wavenumber is greater than max wavenumber. provided min: %s provided max: %s"
% (min_wavenum, max_wavenum)
)
elif min_wavenum == max_wavenum:
__error(
" min wavenumber is equivalent to max wavenumber. provided min: %s provided max: %s"
% (min_wavenum, max_wavenum)
)
# check if beamsplitter is vaild
if beamsplitter != "AR_ZnSe" and beamsplitter != "AR_CaF2":
__error(
" given beamsplitter option is not a vaild option\n Valid Options: AR_ZnSe AR_CaF2"
)
# check if cell_window is vaild
if cell_window != "CaF2" and cell_window != "ZnSe":
__error(
" given cell window option is not a vaild option\n Valid Options: CaF2 ZnSe"
)
# check if detector is vaild
if detector != "MCT" and detector != "InSb":
__error(
" given detector option is not a vaild option\n Valid Options: MCTs InSb"
)
# output verified params to console as self-check before calc_spectrum()
print(
"source: %s source_temp: %s min_wavenum: %s max_wavenum: %s beamspliter: %s cell_window: %s detector: %s"
% (
source,
source_temp,
min_wavenum,
max_wavenum,
beamsplitter,
cell_window,
detector,
)
)
return source_temp
#%%
if __name__ == "__main__":
# NOTE for graphing
source = "t"
min_wavenum = 1900
max_wavenum = 2300
beamsplitter = "AR_ZnSe"
cell_window = "CaF2"
detector = "MCT"
# check params
source_temp = check_params(
source,
min_wavenum,
max_wavenum,
beamsplitter,
cell_window,
detector,
)
print(source_temp)
# ----- a.) transmission spectrum of gas sample -----
# https://radis.readthedocs.io/en/latest/source/radis.lbl.calc.html#radis.lbl.calc.calc_spectrum
s = calc_spectrum(
min_wavenum,
max_wavenum,
molecule="CO",
isotope="1,2,3",
pressure=0.01, # bar
Tgas=294.15, # K
path_length=10, # cm
wstep=0.5, # cm^-1
verbose=False, # hides HITRAN output
databank="hitran",
warnings={"AccuracyError": "ignore"},
)
# generate spectrum with noise
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.operations.html#radis.spectrum.operations.add_array
noise = __loadData(s.get("transmittance_noslit", wunit="nm", Iunit="default"))
for x in noise:
noise[x] = noise[x] + np.random.normal(0, 1)
spectrum = __loadData(s.get("transmittance_noslit", wunit="nm", Iunit="default"))
# ----- b.) blackbody spectrum of source -----
spectrum = __sPlanck(spectrum, source_temp)
# ----- c.) transmission spectrum of windows/beamsplitter -----
# Beamsplitter
if beamsplitter == "AR_ZnSe":
spectrum = __AR_ZnSe(spectrum)
elif beamsplitter == "AR_CaF2":
spectrum = __AR_CaF2(spectrum)
# Cell Windows
if cell_window == "CaF2":
spectrum = __CaF2(spectrum)
spectrum = __CaF2(spectrum)
elif cell_window == "ZnSe":
spectrum = __ZnSe(spectrum)
spectrum = __ZnSe(spectrum)
# ----- d.) detector response spectrum -----
if detector == "MCT":
spectrum = __ZnSe(spectrum)
spectrum = __MCT(spectrum)
elif detector == "InSb":
spectrum = __sapphire(spectrum)
spectrum = __InSb(spectrum)
graph(spectrum)
# Turns the dictionary back into a radis Spectrum object
waverange = []
vector = []
for x in spectrum:
waverange.append(x)
vector.append(spectrum[x])
# TODO --> does not work, potential fix: https://github.com/radis/radis/pull/499
# processed_spec = Spectrum.from_array(
# np.array(waverange),
# np.array(vector),
# "transmittance_noslit",
# wunit="nm",
# Iunit="nm",
# )
# # find peaks in spectrum
# # https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.to_specutils
# spec_u = processed_spec.to_specutils(
# "transmittance_noslit", wunit="nm", Iunit="default"
# )
# TODO --> potential working solution (does not throw error in dev branch as of 8/12/2022):
# processed_spec = Spectrum.from_array(
# np.array(waverange),
# np.array(vector),
# "transmittance_noslit",
# wunit="nm",
# Iunit="",
# )
# # find peaks in spectrum
# # https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.to_specutils
# spec_u = processed_spec.to_specutils(
# "transmittance_noslit", wunit="nm_vac", Iunit="default"
# )
#%%