#!/usr/bin/env python
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

mpl.rc("text", usetex=True)
fig = plt.figure(figsize=(3.69,3.))
#plt.rcParams.update({'font.size': 10})

#Ei, average displacement in X until moment of reaction (r=1.0) (angstrom), standard error (angstrom)
NN_533_D2 = np.array(
[[0.0073, 16.7, 0.3],
[0.0304, 14.6, 0.4],
[0.1436, 2.7, 0.1]]
)

NN_322_D2 = np.array(
[[0.0073, 19.2, 0.4],
[0.0304, 14.8, 0.5],
[0.1436, 2.2, 0.1]]
)

NN_755_D2 = np.array(
[[0.0073, 22.8, 0.4],
[0.0304, 14.8, 0.6],
[0.1436, 1.7, 0.1]]
)

NN_433_D2 = np.array(
[[0.0073, 23.9, 0.1],
[0.0304, 14.9, 0.2],
[0.1436, 1.7, 0.0]]
)

NN_977_D2 = np.array(
[[0.0073, 25.2, 0.5],
[0.0304, 16.1, 0.7],
[0.1436, 1.7, 0.1]]
)

NN_544_D2 = np.array(
[[0.0073, 27.0, 0.5],
[0.0304, 10.2, 0.7],
[0.1436, 1.5, 0.1]]
)

NN_1199_D2 = np.array(
[[0.0073, 27.6, 0.6],
[0.0304, 14.5, 0.8],
[0.1436, 1.3, 0.1]]
)

NN_655_D2 = np.array(
[[0.0073, 28.2, 0.6],
[0.0304, 14.7, 0.8],
[0.1436, 1.4, 0.1]]
)

NN_131111_D2 = np.array(
[[0.0073, 29.1, 0.6],
[0.0304, 14.1, 0.8],
[0.1436, 1.1, 0.1]]
)

NN_766_D2 = np.array(
[[0.0073, 30.5, 0.7],
[0.0304, 12.3, 0.7],
[0.1436, 1.1, 0.1]]
)

NN_151313_D2 = np.array(
[[0.0073, 30.9, 0.7],
[0.0304, 12.1, 0.7],
[0.1436, 1.1, 0.1]]
)

NN_877_D2 = np.array(
[[0.0073, 29.1, 0.5],
[0.0304, 13.8, 0.8],
[0.1436, 1.1, 0.1]]
)

NN_171515_D2 = np.array(
[[0.0073, 30.8, 0.7],
[0.0304, 12.1, 0.8],
[0.1436, 0.9, 0.1]]
)

# In terms of unit cell vector parallel to the terrace and in nm^-1
# Please check the non-rectangular unit cells, there might be an error (or not)
NN_step_density = {
'211': 1.33925980477299,
'533': 1.10245499657604,
'322': 0.867222846526904,
'755': 0.717738278424414,
'433': 0.609594900345529,
'977': 0.531226638707765,
'544': 0.469525140800388,
'1199': 0.42152023770247,
'655': 0.381732224349042,
'131111': 0.349345915692593,
'766': 0.321582789240287,
'151313': 0.298239412749505,
'877': 0.277790623773609,
'171515': 0.260184400328028,
'111': 0.
}

# Lengths are taken from facet_angle.xlsx
NN_terrace_length = {
'211': 5.005684868,
'533': 6.766517938,
'322': 9.475127493,
'755': 11.69103357,
'433': 14.3231367,
'977': 16.6150325,
'544': 19.21121386,
'1199': 21.53507859,
'655': 24.10409104,
'131111': 26.44955954,
'766': 28.99933585,
'151313': 31.36404051,
'877': 33.89876552,
'171515': 36.27414259
}

def sticking_array_NN( idx ):
	NN = []
	NN.append( [NN_step_density['533'], NN_533_D2[idx,1], NN_533_D2[idx,2] ])
	NN.append( [NN_step_density['322'], NN_322_D2[idx,1], NN_322_D2[idx,2] ])
	NN.append( [NN_step_density['755'], NN_755_D2[idx,1], NN_755_D2[idx,2] ])
	NN.append( [NN_step_density['433'], NN_433_D2[idx,1], NN_433_D2[idx,2] ])
	NN.append( [NN_step_density['977'], NN_977_D2[idx,1], NN_977_D2[idx,2] ])
	NN.append( [NN_step_density['544'], NN_544_D2[idx,1], NN_544_D2[idx,2] ])
	NN.append( [NN_step_density['1199'], NN_1199_D2[idx,1], NN_1199_D2[idx,2] ])
	NN.append( [NN_step_density['655'], NN_655_D2[idx,1], NN_655_D2[idx,2] ])
	NN.append( [NN_step_density['131111'], NN_131111_D2[idx,1], NN_131111_D2[idx,2] ])
	NN.append( [NN_step_density['766'], NN_766_D2[idx,1], NN_766_D2[idx,2] ])
	NN.append( [NN_step_density['151313'], NN_151313_D2[idx,1], NN_151313_D2[idx,2] ])
	NN.append( [NN_step_density['877'], NN_877_D2[idx,1], NN_877_D2[idx,2] ])
	NN.append( [NN_step_density['171515'], NN_171515_D2[idx,1], NN_171515_D2[idx,2] ])
	NN = np.array(NN)

	return NN

NN_007 = sticking_array_NN( 0 )
NN_029 = sticking_array_NN( 1 )
NN_139 = sticking_array_NN( 2 )

plt.errorbar( NN_007[:,0], NN_007[:,1], yerr=NN_007[:,2], capsize=4, marker='o', color='C00', label=r'$E_\textrm{i}=0.7$ kJ/mol' )
plt.errorbar( NN_029[:,0], NN_029[:,1], yerr=NN_029[:,2], capsize=4, marker='o', color='C01', label=r'$E_\textrm{i}=2.9$ kJ/mol' )
plt.errorbar( NN_139[:,0], NN_139[:,1], yerr=NN_139[:,2], capsize=4, marker='o', color='C02', label=r'$E_\textrm{i}=13.9$ kJ/mol' )

stat = []
stat.append( [NN_step_density['211'], NN_terrace_length['211']] )
stat.append( [NN_step_density['533'], NN_terrace_length['533']] )
stat.append( [NN_step_density['322'], NN_terrace_length['322']] )
stat.append( [NN_step_density['755'], NN_terrace_length['755']] )
stat.append( [NN_step_density['433'], NN_terrace_length['433']] )
stat.append( [NN_step_density['977'], NN_terrace_length['977']] )
stat.append( [NN_step_density['544'], NN_terrace_length['544']] )
stat.append( [NN_step_density['1199'], NN_terrace_length['1199']] )
stat.append( [NN_step_density['655'], NN_terrace_length['655']] )
stat.append( [NN_step_density['131111'], NN_terrace_length['131111']] )
stat.append( [NN_step_density['766'], NN_terrace_length['766']] )
stat.append( [NN_step_density['151313'], NN_terrace_length['151313']] )
stat.append( [NN_step_density['877'], NN_terrace_length['877']] )
stat.append( [NN_step_density['171515'], NN_terrace_length['171515']] )
stat = np.array(stat)

plt.plot(stat[:,0], stat[:,1], c='k', ls='--')

plt.legend(loc='best', numpoints=1, frameon=True)
plt.xlim(0., 1.2)
plt.ylim(0.0, 40.)
#plt.ylim(1e-7, 1.)
#plt.yscale('log')
plt.tick_params(length=6, width=1, direction='in', top=True, right=True)
plt.ylabel(r'Average displacement in $X$ (\r{A})')
plt.xlabel(r'Step density (nm$^{-1}$)')

plt.tight_layout()
plt.savefig('Xdisplacement.pdf')
#plt.show()
