-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrayleigh.py
174 lines (141 loc) · 6.3 KB
/
rayleigh.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
'''
rayleigh.py - Rayleigh scattering
Description:
Calculation of the scattering by very small particles (compared to the wavelength).
Also known as Rayleigh scattering.
The scattering intensity is proportional to 1/wavelength^4.
It is scaled so that the scattering factor for 555.0 nm is 1.0.
This is the basic physical reason that the sky is blue.
Functions:
rayleigh_scattering (wl_nm) -
Get the Rayleigh scattering factor for the wavelength.
Scattering is proportional to 1/wavelength^4.
The scattering is scaled so that the factor for wl_nm = 555.0 is 1.0.
rayleigh_scattering_spectrum () -
Get the Rayleigh scattering spectrum (independent of illuminant), as a numpy array.
rayleigh_illuminated_spectrum (illuminant) -
Get the spectrum when illuminated by the specified illuminant.
rayleigh_illuminated_color (illuminant) -
Get the xyz color when illuminated by the specified illuminant.
Plots:
rayleigh_patch_plot (named_illuminant_list, title, filename) -
Make a patch plot of the Rayleigh scattering color for each illuminant.
rayleigh_color_vs_illuminant_temperature_plot (T_list, title, filename) -
Make a plot of the Rayleigh scattered color vs. temperature of blackbody illuminant.
rayleigh_spectrum_plot (illuminant, title, filename) -
Plot the spectrum of Rayleigh scattering of the specified illuminant.
References:
H.C. van de Hulst, Light Scattering by Small Particles,
Dover Publications, New York, 1981. ISBN 0-486-64228-3.
License:
Copyright (C) 2008 Mark Kness
Author - Mark Kness - [email protected]
This file is part of ColorPy.
ColorPy is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
ColorPy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ColorPy. If not, see <http://www.gnu.org/licenses/>.
'''
import math
import numpy, pylab
from . import colormodels
from . import ciexyz
from . import illuminants
from . import blackbody
from . import plots
def rayleigh_scattering (wl_nm):
'''Get the Rayleigh scattering factor for the wavelength.
Scattering is proportional to 1/wavelength^4.
The scattering is scaled so that the factor for wl_nm = 555.0 is 1.0.'''
wl_0_nm = 555.0
wl_rel = wl_nm / wl_0_nm
rayleigh_factor = math.pow (wl_rel, -4)
return rayleigh_factor
def rayleigh_scattering_spectrum ():
'''Get the Rayleigh scattering spectrum (independent of illuminant), as a numpy array.'''
spectrum = ciexyz.empty_spectrum()
(num_rows, num_cols) = spectrum.shape
for i in range(0, num_rows):
spectrum [i][1] = rayleigh_scattering (spectrum [i][0])
return spectrum
def rayleigh_illuminated_spectrum (illuminant):
'''Get the spectrum when illuminated by the specified illuminant.'''
spectrum = rayleigh_scattering_spectrum()
(num_wl, num_col) = spectrum.shape
for i in range(0, num_wl):
spectrum [i][1] *= illuminant [i][1]
return spectrum
def rayleigh_illuminated_color (illuminant):
'''Get the xyz color when illuminated by the specified illuminant.'''
spectrum = rayleigh_illuminated_spectrum (illuminant)
xyz = ciexyz.xyz_from_spectrum (spectrum)
return xyz
#
# Figures
#
def rayleigh_patch_plot (named_illuminant_list, title, filename):
'''Make a patch plot of the Rayleigh scattering color for each illuminant.'''
xyz_colors = []
color_names = []
for (illuminant, name) in named_illuminant_list:
xyz = rayleigh_illuminated_color (illuminant)
xyz_colors.append (xyz)
color_names.append (name)
plots.xyz_patch_plot (xyz_colors, color_names, title, filename)
def rayleigh_color_vs_illuminant_temperature_plot (T_list, title, filename):
'''Make a plot of the Rayleigh scattered color vs. temperature of blackbody illuminant.'''
num_T = len (T_list)
rgb_list = numpy.empty ((num_T, 3))
for i in range(0, num_T):
T_i = T_list [i]
illuminant = illuminants.get_blackbody_illuminant (T_i)
xyz = rayleigh_illuminated_color (illuminant)
rgb_list [i] = colormodels.rgb_from_xyz (xyz)
plots.color_vs_param_plot (
T_list,
rgb_list,
title,
filename,
tight = True,
plotfunc = pylab.plot,
xlabel = r'Illuminant Temperature (K)',
ylabel = r'RGB Color')
def rayleigh_spectrum_plot (illuminant, title, filename):
'''Plot the spectrum of Rayleigh scattering of the specified illuminant.'''
spectrum = rayleigh_illuminated_spectrum (illuminant)
plots.spectrum_plot (
spectrum,
title,
filename,
xlabel = 'Wavelength (nm)',
ylabel = 'Intensity ($W/m^2$)')
def figures ():
'''Draw some plots of Rayleigh scattering.'''
# patch plots for some illuminants
rayleigh_patch_plot (
[(illuminants.get_blackbody_illuminant (blackbody.SUN_TEMPERATURE), 'Sun')],
'Rayleigh Scattering by the Sun', 'Rayleigh-PatchSun')
rayleigh_patch_plot (
[(illuminants.get_illuminant_D65 (), 'D65'),
(illuminants.get_blackbody_illuminant (2000.0), '2000 K'),
(illuminants.get_blackbody_illuminant (3500.0), '3500 K'),
(illuminants.get_blackbody_illuminant (blackbody.SUN_TEMPERATURE), 'Sun'),
(illuminants.get_blackbody_illuminant (6500.0), '6500 K'),
(illuminants.get_blackbody_illuminant (15000.0), '15000 K')],
'Rayleigh Scattering by Various Illuminants', 'Rayleigh-PatchVarious')
# color vs illuminant temperature
T_list = range (1200, 16000, 50) # must be integers for range()
rayleigh_color_vs_illuminant_temperature_plot (T_list, 'Rayleigh Scattering Sky Colors', 'Rayleigh-SkyColors')
# spectra for several illuminants
T_list = [2000.0, 3000.0, blackbody.SUN_TEMPERATURE, 6500.0, 11000.0, 15000.0]
for T in T_list:
rayleigh_spectrum_plot (
illuminants.get_blackbody_illuminant (T),
'Rayleigh Scattering\nIlluminant %g K' % (T),
'Rayleigh-Spectrum-%gK' % (T))