-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmetalParser.py
More file actions
100 lines (86 loc) · 4.78 KB
/
metalParser.py
File metadata and controls
100 lines (86 loc) · 4.78 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
import os, math
import urllib
import numpy as np
# Script to download refractive index + absorption data from refractiveindex.info
# for a variety of metals, and convert to javascript code
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def frange(x, y, jump):
while x <= y:
yield x
x += jump
def download(metalname, url, Nresample, lmin, lmax):
response = urllib.urlopen(url)
table = response.read()
lines = table.split('\n')
n = []
k = []
for l in lines:
d = l.split()
if len(d)>1:
x = d[0]
y = d[1]
if not is_number(x): dataset=y
else:
sample = (x, y)
if dataset=='n': n.append(sample)
elif dataset=='k': k.append(sample)
else: print "don't know dataset '%s'" % dataset; quit()
if len(n) != len(k):
print 'bad parsing, mismatched lengths: %d n, %d k' % (len(n), len(k))
else:
# resample array into 64 uniformly spaced samples
w_lst = []
n_lst = []
k_lst = []
for l in range(0, len(n)):
n_sample = n[l]
k_sample = k[l]
if n_sample[0] != k_sample[0]:
print 'n, k sample wavelength inconsistency! %f %f' % (n_sample[0], k_sample[0]); quit()
w_lst.append(1000.0*float(n_sample[0]))
n_lst.append(float(n_sample[1]))
k_lst.append(float(k_sample[1]))
dnm = (lmax-lmin)/float(Nresample-1)
wavelengths_nm = list(frange(lmin, lmax, dnm))
n_interp = np.interp(wavelengths_nm, w_lst, n_lst)
k_interp = np.interp(wavelengths_nm, w_lst, k_lst)
assert len(wavelengths_nm) == Nresample
assert len(n_interp) == Nresample
assert len(k_interp) == Nresample
w_str = ",".join(map(str, wavelengths_nm))
n_str = ",".join(map(str, n_interp))
k_str = ",".join(map(str, k_interp))
# wavelength_nm: new Float32Array([%s]),
code = '''function tabulated_%s() { // %s
return { n: new Float32Array([%s]),
k: new Float32Array([%s]) } }
''' % (metalname, "%s samples of n, k between %fnm and %fnm"%(Nresample, lmin, lmax), n_str, k_str)
return code
print download("aluminium", "https://refractiveindex.info/tmp/main/Ag/Babar.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("brass", "https://refractiveindex.info/tmp/other/alloys/Cu-Zn/Querry-Cu70Zn30.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("calcium", "https://refractiveindex.info/tmp/main/Ca/Rodriguez-de%20Marcos.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("chromium", "https://refractiveindex.info/tmp/main/Cr/Johnson.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("cobalt", "https://refractiveindex.info/tmp/main/Co/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("copper", "https://refractiveindex.info/tmp/main/Cu/Babar.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("gold", "https://refractiveindex.info/tmp/main/Au/Johnson.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("iridium", "https://refractiveindex.info/tmp/main/Ir/Windt.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("iron", "https://refractiveindex.info/tmp/main/Fe/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("lead", "https://refractiveindex.info/tmp/main/Pb/Ordal.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("mercury", "https://refractiveindex.info/tmp/main/Hg/Inagaki.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("molybdenum", "https://refractiveindex.info/tmp/main/Mo/Ordal.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("nickel", "https://refractiveindex.info/tmp/main/Ni/Ordal.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("palladium", "https://refractiveindex.info/tmp/main/Pd/Johnson.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("platinum", "https://refractiveindex.info/tmp/main/Pt/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("silicon", "https://refractiveindex.info/tmp/main/Si/Aspnes.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("silver", "https://refractiveindex.info/tmp/main/Ag/Johnson.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("tantalum", "https://refractiveindex.info/tmp/main/Ta/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("titanium", "https://refractiveindex.info/tmp/main/Ti/Johnson.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("tungsten", "https://refractiveindex.info/tmp/main/W/Ordal.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("vanadium", "https://refractiveindex.info/tmp/main/V/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("zinc", "https://refractiveindex.info/tmp/main/Zn/Werner.txt", Nresample=64, lmin=390.0, lmax=750.0)
print download("zirconium", "https://refractiveindex.info/tmp/main/Zr/Querry.txt", Nresample=64, lmin=390.0, lmax=750.0)