-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlight_sources.py
98 lines (88 loc) · 3.36 KB
/
light_sources.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
from os.path import join as joinpath
import csv
from numpy import interp as interpolate
from devsim import DS_DATADIR
class AM0(object):
"""docstring for GenericLightSource"""
def __init__(self, lambda_min=280, lambda_max=4000, samples=None):
self.lambda_min = lambda_min
self.lambda_max = lambda_max
self._wavelength = []
self._irradiance = []
self._photon_flux = []
self._acc_photon_flux = []
data_file = joinpath(DS_DATADIR, 'AM0.csv')
with open(data_file, newline='') as csvfile:
reader = csv.DictReader(
csvfile, fieldnames=('wavelength', 'irradiance', 'photon_flux')
)
prev_flux = 0
for row in reader:
wl = float(row['wavelength'])
if lambda_min <= wl <= lambda_max:
self._wavelength.append(wl)
self._irradiance.append(float(row['irradiance']))
self._photon_flux.append(float(row['photon_flux']) - prev_flux)
self._acc_photon_flux.append(float(row['photon_flux']))
prev_flux = float(row['photon_flux'])
if samples is not None:
# Probably not the best way? We'll see
interval = len(self._wavelength) // samples
self._wavelength = self._wavelength[::interval]
self._irradiance = self._irradiance[::interval]
self._acc_photon_flux = self._acc_photon_flux[::interval]
if len(self._wavelength) == samples + 1:
self._wavelength.pop()
self._irradiance.pop()
self._acc_photon_flux.pop()
def __len__(self):
return len(self._wavelength)
def __iter__(self):
return self._wavelength.__iter__()
def __next__(self):
return self._wavelength.__next__()
def irradiance(self, wavelength):
"""
Returns spectral irradiance for the given wavelength
Units: W/(m–2⋅nm–1)
"""
if wavelength < self.lambda_min or wavelength > self.lambda_max:
return 0.0
if wavelength in self._wavelength:
ix = self._wavelength.index(wavelength)
return self._irradiance[ix]
return interpolate(
wavelength,
self._wavelength,
self._irradiance
)
def photon_flux(self, wavelength):
"""
Returns the photon flux for the given wavelength
Units: cm-2 * s-1
"""
if wavelength < self.lambda_min or wavelength > self.lambda_max:
return 0.0
if wavelength in self._wavelength:
ix = self._wavelength.index(wavelength)
return self._photon_flux[ix]
return interpolate(
wavelength,
self._wavelength,
self._photon_flux
)
def acc_photon_flux(self, wavelength):
"""
Returns the cumulative photon flux for the given wavelength
Units: cm-2 * s-1
"""
if wavelength < self.lambda_min or wavelength > self.lambda_max:
return 0.0
if wavelength in self._wavelength:
ix = self._wavelength.index(wavelength)
return self._acc_photon_flux[ix]
return interpolate(
wavelength,
self._wavelength,
self._acc_photon_flux
)