import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

def width_to_time(wid):
	return 1/(3e8*(1/(1550-wid/2)-1/(1550+wid/2))*1e9)*1e9

print(width_to_time(10**-7))

#power in photons/s/mode, spec_width in nm
labels=['Arc lamp\n(filtered)','Sunlight (filtered)', 'Hg lamp', 'Subthreshold laser diode', 'SLD', 'SLD (filtered)', '2-emitter']
power=[30*10**-9/1e-19,200000,115000,32573355000000,23e-3/(10**-19),23e-3/10/(10**-19),2.6E16]
spec_width=[4,0.0026, 0.0023, 0.0000385, 50, 0.5, 0.00014]
print(power)

labels1=['MS','RGG']
power1=[1000000,200000]
coh_time=[10000, 1000]

fontsize=20
tick_fontsize=20

#fig,ax=plt.subplots(1,1,figsize=(15,4))
#fig.subplots_adjust(left=0.115, bottom=0.2, right=0.96, top=0.96)
fig,ax=plt.subplots(1,1,figsize=(9,9))
fig.subplots_adjust(left=0.13, bottom=0.1, right=0.862, top=0.91)

for i in range(len(labels)):
	ax.scatter(spec_width[i],power[i],c='r', marker='o',s=50)

#plt.text(spec_width[0],power[0]*0.3,labels[0],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[0]*2,power[0]*10,labels[0],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[1],power[1]*3,labels[1],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[2],power[2]*0.3,labels[2],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
#plt.text(spec_width[3]*3,power[3],labels[3],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[3],power[3]*3,labels[3],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[4]*0.3,power[4],labels[4],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[5]*0.035,power[5],labels[5],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spec_width[6]*0.08,power[6],labels[6],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)

#ax.axhspan(10e2, 10e12, color='purple', alpha=0.3)
ax.fill_between((10**-7.5,10e1),((10**-6)/(10**-19)*(10**-7.5),(10**-6)/(10**-19)*(10e1)),color='purple', alpha=0.3)
print((10**-6)/(10**-19)*(10**-7.5))
print((10**-6)/(10**-19)*(10e1))
plt.text(1.5, 10**7,'Detector timing resolution',horizontalalignment='center',verticalalignment='center',fontsize=fontsize-2,rotation=90,rotation_mode='anchor')
plt.text(10**-3.2, 10**9.4,'Amplifier spontaneous emission power',rotation=33.45,horizontalalignment='center',verticalalignment='center',fontsize=fontsize-2)

ax.set_xlim(10**-7.5,10e1)
ax.set_ylim(1e3,1e18)
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xlabel('Spectral width (nm)',fontsize=fontsize)
ax.set_ylabel(r'Photoevents/s per spatial mode (s$^{-1}$)',fontsize=fontsize)
ax.tick_params(axis='both', labelsize=fontsize)
ax2=ax.twiny()
ax2.scatter(coh_time[0],power1[0],c='r', marker='o',s=50)
ax2.scatter(coh_time[1],power1[1],c='r', marker='o',s=50)
ax2.axvspan(10*10**-3, 10**-5, color='green', alpha=0.3)
ax3=ax.twinx()
ax2.text(coh_time[0],power1[0]*0.3,labels1[0],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
ax2.text(coh_time[1],power1[1]*0.3,labels1[1],horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
ax2.tick_params(axis='both', labelsize=fontsize)
ax2.set_xlim(width_to_time(10**-7.5),width_to_time(100))
ax2.set_xlabel('Coherence time (ns)', fontsize=fontsize)
ax2.set_xscale('log')
ax3.set_ylim(1e3*10**-19*1e3,1e18*10**-19*1e3)
ax3.tick_params(axis='both',labelsize=fontsize)
ax3.set_ylabel('Power (mW)', fontsize=fontsize)
ax3.set_yscale('log')

plt.show()

"""
#ax.scatter(thermal_input,thermal_output,c='r',marker='o',s=100)
ax.scatter(thermal_input[0],thermal_output[0],c='r',marker='o',s=100)
ax.scatter(thermal_input[1],thermal_output[1],c='r',marker='o',s=100)
ax.scatter(thermal_input[2],thermal_output[2],c='r',marker='o',s=100)
ax.scatter(thermal_input[3],thermal_output[3],c='r',marker='o',s=100)
#ax.scatter(thermal_input[5],thermal_output[5],c='r',marker='o',s=100)
ax.scatter(spdc_input,spdc_output,c='b',marker='s',s=100)
ax.axline((0,0),(1,1),c='k',linestyle='--')
ax.axline((1,1e-10),slope=1,c='k',linestyle='--')
ax.axline((1,0.4),slope=1,c='k',linestyle='--')
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_yticks((1e-16,1e-14,1e-12,1e-10,1e-8,1e-6,1e-4,1e-2,1e0,1e2),(r'$10^{-16}$',r'$10^{-14}$',r'$10^{-12}$',r'$10^{-10}$',r'$10^{-8}$',r'$10^{-6}$',r'$10^{-4}$',r'$10^{-2}$',r'$10^{0}$',r'$10^{2}$'),fontsize=tick_fontsize)
ax.set_xticks((1e-5,1e-4,1e-3,1e-2,1e-1,1e0),(r'$10^{-5}$',r'$10^{-4}$',r'$10^{-3}$',r'$10^{-2}$',r'$10^{-1}$',r'$10^{0}$'),fontsize=tick_fontsize)
ax.xaxis.set_tick_params(which='minor', bottom=False)
ax.set_xlabel('Seed laser input power (W)',fontsize=fontsize)
ax.set_ylabel('Photon-bunched light\noutput power (W)',fontsize=fontsize)
plt.text(thermal_input[0], thermal_output[0]*3, thermal_labels[0], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(thermal_input[1], thermal_output[1]*0.2, thermal_labels[1], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(thermal_input[2], thermal_output[2]*0.2, thermal_labels[2], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(thermal_input[3], thermal_output[3]*4, thermal_labels[3], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
#plt.text(thermal_input[3]*1.4, thermal_output[3], thermal_labels[3], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
#plt.text(thermal_input[4], thermal_output[4]*0.001, thermal_labels[4], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
#plt.text(thermal_input[5], thermal_output[5]*0.2, thermal_labels[5], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spdc_input[0], spdc_output[0]*3, spdc_labels[0], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spdc_input[1], spdc_output[1]*3, spdc_labels[1], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spdc_input[2], spdc_output[2]*0.2, spdc_labels[2], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(spdc_input[3], spdc_output[3]*0.2, spdc_labels[3], horizontalalignment='center',verticalalignment='center',fontsize=fontsize)
plt.text(10**(-3), 10**(-12.5), r'10$^{-8}$% efficiency', horizontalalignment='center',verticalalignment='center',fontsize=fontsize, rotation=13.3, rotation_mode='anchor')
plt.text(10**(-4), 10**(-5), '40% efficiency', horizontalalignment='center',verticalalignment='center',fontsize=fontsize, rotation=13.3, rotation_mode='anchor')
plt.text(10**(-4), 10**(-3.6), '100% efficiency', horizontalalignment='center',verticalalignment='center',fontsize=fontsize, rotation=13.3, rotation_mode='anchor')
plt.show()
"""