import colour.colorimetry

"""
Explanation: Blackbody
A blackbody or planckian radiator is an ideal thermal radiator that absorbs completely all incident radiation, whatever the wavelength, the direction of incidence or the polarization. <a name="back_reference_1"></a><a href="#reference_1">[1]</a>
A blackbody in thermal equilibrium emits electromagnetic radiation called blackbody radiation.
Planck's Law
The spectral radiance of a blackbody at thermodynamic temperature $T [K]$ in a medium having index of refraction $n$ is given by the Planck's law equation: <a name="back_reference_2"></a><a href="#reference_2">[2]</a>
$$
\begin{equation}
L_{e\lambda}(\lambda,T)=\cfrac{C_1n^{-2}\lambda^{-5}}{\pi}{\Biggl[\exp\biggl(\cfrac{C_2}{n\lambda T}\biggr)-1\Biggr]^{-1}}
\end{equation}
$$
where
$$
\begin{equation}
\begin{aligned}
C_1&=2\pi hc^2\
C_2&=\cfrac{hc}{k}
\end{aligned}
\end{equation}
$$
$h$ is Planck's constant, $c$ is the speed of light in vacuum, $k$ is the Boltzmann constant and $\lambda$ is the wavelength.
As per CIE 015:2004 Colorimetry, 3rd Edition recommendation $C_2$ value when used in colorimetry should be $C_2= 1,4388x10^{-2}mK$ as defined by the International Temperature Scale (ITS-90).
$C_1$ value is given by the Committee on Data for Science and Technology (CODATA) and should be $C_1=3,741771x10^{16}Wm^2$.
In the current CIE 015:2004 Colorimetry, 3rd Edition recommendation, colour temperature and correlated colour temperature are calculated with $n=1$.
Colour implements some blackbody related objects available in colour.colorimetry sub-package:
End of explanation
"""


%matplotlib inline

import colour

colour.utilities.filter_warnings(True, False)

colour.colorimetry.planck_law(500 * 1e-9, 5500)

"""
Explanation: Note: colour.colorimetry package public API is also available from the colour namespace.

The Planck's law is called using either the colour.planck_law or  colour.blackbody_spectral_radiance definitions, they are expecting the wavelength $\lambda$ to be in nanometers and the temperature $T$ to be in degree kelvin:
End of explanation
"""


colour.blackbody_spd(6500, colour.SpectralShape(0, 10000, 10))

"""
Explanation: Generating the spectral power distribution of a blackbody is done using the colour.blackbody_spectral_power_distribution definition:
End of explanation
"""


from colour.plotting import *

colour_plotting_defaults()

# Plotting various *blackbodies* spectral power distributions.
blackbodies_spds = [colour.blackbody_spd(i, colour.SpectralShape(0, 10000, 10)) 
                    for i in range(1000, 15000, 1000)]
multi_spd_plot(blackbodies_spds,
               y_label='W / (sr m$^2$) / m',
               use_spds_colours=True,
               normalise_spds_colours=True,
               legend_location='upper right',
               bounding_box=[0, 1000, 0, 2.25e15])

"""
Explanation: With its temperature lowering, the blackbody peak shifts to longer wavelengths while its intensity also decreases:
End of explanation
"""


blackbody_colours_plot(colour.SpectralShape(500, 12500, 50))

"""
Explanation: Let's plot the blackbody colours from temperature in domain [150, 12500, 50]:
End of explanation
"""


# Comparing theoretical and measured *Sun* spectral distributions.
# Arbitrary ASTM_G_173_ETR scaling factor calculated with
# :def:`colour.spectral_to_XYZ` definition.
ASTM_G_173_spd = ASTM_G_173_ETR.copy() * 1.37905559e+13

blackbody_spd = colour.blackbody_spd(
    5778,
    ASTM_G_173_spd.shape)
blackbody_spd.name = 'The Sun - 5778K'

multi_spd_plot([ASTM_G_173_spd, blackbody_spd],
               y_label='W / (sr m$^2$) / m',
               legend_location='upper right')

"""
Explanation: Stars Colour
Let's compare the extraterrestrial solar spectral irradiance to the blackbody spectral radiance of a thermal radiator with a temperature of 5778 K:
End of explanation
"""


blackbody_spectral_radiance_plot(temperature=3500, blackbody='VY Canis Majoris')

"""
Explanation: As you can see the Sun spectral power distribution is very close to the one from a blackbody at similar temperature $T$.
Calculating theoritical colour of any star is possible, like for instance the VY Canis Majoris red hypergiant in the constellation Canis Major.
End of explanation
"""


blackbody_spectral_radiance_plot(temperature=12130, blackbody='Rigel')

"""
Explanation: Or Rigel the brightest star in the constellation Orion and the seventh brightest star in the night sky.
End of explanation
"""


blackbody_spectral_radiance_plot(temperature=5778, blackbody='The Sun')

"""
Explanation: And finally the Sun, our star:
End of explanation
"""


