#!/usr/bin/python


from matplotlib import pyplot as plt
import numpy as np

####measurements taken of non linear amplification and de amplification for different green powers
dark_level = np.array([25.83]);  # dark level on OPO IR trans PD
unamplified_max = np.array([190.03]); # IR transmission with green blocked, slowly scanning OPO 
fiber_transmission = 0.158;
fiber_launch_powers = np.array([18, 8, 14.4, 15.9, 16.9])* fiber_transmission; #mW green power according to fiber launch PD, mulitplied by 15.8% to account for fiber transmission
OPO_trans_norm = np.array([0.97, 0.42, 0.74, 0.82, 0.88]);
max_IR = np.array([365.29, 283.095, 329.14, 340, 348.74]);
min_IR = np.array([121.6, 138.75, 126.826, 124.6, 123.22]);

amplification =  (max_IR - dark_level) / (unamplified_max -dark_level);
deamplification = (min_IR -dark_level) / (unamplified_max - dark_level);

x_from_amp = 1-1/np.sqrt(amplification);
thresh_from_amp = fiber_launch_powers / x_from_amp**2; 
x_from_deamp = 1/np.sqrt(deamplification)-1;
thresh_from_deamp = fiber_launch_powers / x_from_deamp**2; 

x_from_maxMin = (np.sqrt((max_IR - dark_level)/(min_IR - dark_level)) - 1) / (1+ np.sqrt((max_IR - dark_level)/(min_IR - dark_level)))
thresh_from_maxMin = fiber_launch_powers / x_from_maxMin**2; 

#plot expected expression
powers = np.linspace(0.2,22, 100) * fiber_transmission; # array of fiber launch powers
p_threshold = 31; #gues at threshold, could do monte carlo instead.  This is the power at the fiber launch PD, which is about a factor of 4 above what is injected into OPO
x = np.sqrt(powers/p_threshold);
amp_expected = 1/(1-x)**2;
deamp_expected = 1/(1+x)**2;

allfigures = []
fig,ax = plt.subplots()
#ax = fig.subplot(111)
ax.scatter(fiber_launch_powers, amplification, color = 'black', label ='Measured amplificiation')
ax.scatter(fiber_launch_powers, deamplification, color = 'green', label = 'Measured deamplificiation')
ax.plot(powers, amp_expected, color = 'red', label = 'amplification expression (30.5 mW threshold)')
ax.plot(powers, deamp_expected, color = 'blue', label = 'deamplification expression')
ax.legend()
#plt.show()
ax.set_xlabel('OPO incident power (mW)')
ax.set_ylabel('seed amplificiation')
allfigures.append(fig)
fig.savefig('threshold_measurement_May29_2019.pdf',bbox_inches='tight')

fig = plt.figure()
s1 = plt.subplot(211)
s1.scatter(fiber_launch_powers, x_from_amp, label = 'x infered from amplification')
s1.scatter(fiber_launch_powers, x_from_deamp, label = 'x infered from deamplification')
s1.scatter(fiber_launch_powers, x_from_maxMin, label = 'x infered from amp over deamp')
s1.legend()
s1.set_ylabel('x = sqrt(P/Pthresh)',fontsize=8)

s2 = plt.subplot(212)
s2.scatter(fiber_launch_powers, thresh_from_amp, label = 'threshold power infered from amplificiation')
s2.scatter(fiber_launch_powers, thresh_from_deamp, label = 'threshold power infered from deamplificiation')
s2.scatter(fiber_launch_powers, thresh_from_maxMin, label = 'threshold power infered from amp over deamp')
s2.legend()
s2.set_ylabel('threshold incident power (mW)',fontsize=8)
s2.set_xlabel('OPO incident power (mW)', fontsize=8)
plt.show()
fig.savefig('threshold_vs_power_May29_2019.pdf',bbox_inches='tight', format = 'pdf')

