# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import tmm
from tmm import coh_tmm ,unpolarized_RT 
import numpy as np
from scipy.interpolate import interp1d
import matplotlib . pyplot as plt
import tmm.examples
import tmm.tests


try:
    import colorpy.illuminants
    import colorpy.colormodels
    from tmm import color
    colors_were_imported = True
except ImportError :
    colors_were_imported = False

# 
# tmm.examples.sample1()
# tmm.examples.sample2()
# tmm.examples.sample3()
# tmm.examples.sample4()
# tmm.examples.sample6()
# tmm.tests.run_all()


degree = np.pi /180

# list of layer thicknesses in nm
d_list = [np.inf ,100 ,300 , np.inf]
# list of refractive indices
n_list = [1 ,2.2 ,3.3+0.3j ,1]

# list of wavenumbers to plot in nm ^ -1
lambd=np.linspace(1/0.01 , 1/0.001 , num =400)
ks= np.linspace (0.0001 ,.01 , num =400)
# initialize lists of y- values to plot
Rnorm =[] # for normal incidence
R45 =[] # for 45 degree incidence


for k in lambd:
# For normal incidence , s and p polarizations are identical .
# I arbitrarily decided to use 's '.
    Rnorm . append ( coh_tmm ('s',n_list , d_list , 0, k)['R']) # for polarized or unpolarized light
# 's' is polarization , could be 'p'
# n_list and d_list are material indices and thicknesses in the order in which light passes through them
# 0 is incidence angle in radian : here , normal indidence
# 1/k is the wavelength to plot
# REMEMBER : KEEP LENGTH UNITS CONSISTANT
# ['R '] selects the reflected power in a dictionnary ( among r,t,R,T ...)
    R45. append ( unpolarized_RT (n_list , d_list , 45* degree ,k)['R'])
# same for 45 degree incident unpolarized light

kcm = ks * 1e7 #ks in cm ^ -1 rather than nm ^ -1
plt. figure ()
plt. grid()
plt. plot (lambd ,Rnorm ,'blue',lambd ,R45 ,'purple')
plt. xlabel ('Wavelength ( nm)')
plt. ylabel ('Fraction   reflected ')
plt. title ('Reflection of unpolarized light at 0 and 45 degres');


# Interface semi infini air-verre incidence vraiable
nAngle=100
lbd=660
RdeThetas=[]
RdeThetap=[]
TdeThetas=[]
TdeThetap=[]
d_list = [np.inf , np.inf]
n_list = [1,1.5]
lambd=np.linspace(1/0.01 , 1/0.001 , num =400)
listeTheta=np.linspace(0,np.pi/2,nAngle)
for angle in listeTheta:
    RdeThetas.append( coh_tmm ('s',n_list , d_list, angle,lbd)['R'])
    RdeThetap.append( coh_tmm ('p',n_list , d_list, angle,lbd)['R'])
    TdeThetas.append( coh_tmm ('s',n_list , d_list, angle,lbd)['T'])
    TdeThetap.append( coh_tmm ('p',n_list , d_list, angle,lbd)['T'])
    
plt. figure ()
plt. grid()
plt. plot (listeTheta*180/np.pi ,RdeThetas,label="R_TE",color="blue",ls='solid')
plt. plot (listeTheta*180/np.pi ,RdeThetap,label="R_TM",color="red",ls='solid')
plt. plot (listeTheta*180/np.pi ,TdeThetas,label="T_TE",color="blue",ls='dashed')
plt. plot (listeTheta*180/np.pi ,TdeThetap,label="T_TM",color="red",ls='dashed')

plt.xlabel('Angle incident $\\theta$ (°)')
plt.ylabel('Coefficient de Fresnel en intensité')
plt.legend()

# Air verre air incidence normale et 45° avec variation lambda 
nbLambda=100
Listelbd=np.linspace(100,1000,nbLambda)
RdeLambdasNorm=[]
RdeLambdas45=[]
TdeLambdasNorm=[]
TdeLambdas45=[]
d_list = [np.inf ,500, np.inf]
n_list = [1,1.5,1]

for lbd in Listelbd:
    RdeLambdasNorm.append( coh_tmm ('s',n_list , d_list, 0,lbd)['R'])
    RdeLambdas45.append( coh_tmm ('s',n_list , d_list, 45,lbd)['R'])
    TdeLambdasNorm.append( coh_tmm ('s',n_list , d_list, 0,lbd)['T'])
    TdeLambdas45.append( coh_tmm ('s',n_list , d_list, 45,lbd)['T'])
    
plt. figure ()
plt. grid()
plt. plot (Listelbd,RdeLambdasNorm,label="R 0 deg",color="blue",ls='solid')
plt. plot (Listelbd,TdeLambdasNorm,label="T 0 deg",color="red",ls='solid')
plt. plot (Listelbd ,RdeLambdas45,label="R 45 deg",color="blue",ls='dashed')
plt. plot (Listelbd ,TdeLambdas45,label="T 45 deg",color="red",ls='dashed')

plt.xlabel("Longueur d'onde (nm) ")
plt.ylabel('Coefficient de Fresnel en intensité')
plt.legend()

# Air absorbeur air pour impact de im(n) sur R et T
nImN=100
lbd=500
ListeIm=np.linspace(0,0.003,nImN)
RdeLambdasNorm=[]
TdeLambdasNorm=[]
RdeLambdas45=[]
TdeLambdas45=[]
d_list = [np.inf ,50000, np.inf]
n_list = [1,1.5,1]

for im in ListeIm:
    n_list[1]=1.5+im*1j
    RdeLambdasNorm.append( coh_tmm ('s',n_list , d_list, 0,lbd)['R'])
    RdeLambdas45.append( coh_tmm ('s',n_list , d_list, 45,lbd)['R'])
    TdeLambdasNorm.append( coh_tmm ('s',n_list , d_list, 0,lbd)['T'])
    TdeLambdas45.append( coh_tmm ('s',n_list , d_list, 45,lbd)['T'])
    
plt. figure ()
plt. grid()
plt. plot (ListeIm,RdeLambdasNorm,label="R 0 deg",color="blue",ls='solid')
plt. plot (ListeIm,TdeLambdasNorm,label="T 0 deg",color="red",ls='solid')
plt. plot (ListeIm ,RdeLambdas45,label="R 45 deg",color="blue",ls='dashed')
plt. plot (ListeIm ,TdeLambdas45,label="T 45 deg",color="red",ls='dashed')

plt.xlabel("Im(n)  ")
plt.ylabel('Coefficient de Fresnel en intensité')
plt.legend()