# Created 2016, Zack Gainsforth
from __future__ import division
import matplotlib
#matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
def CBEDThicknessMeasure(lam, d0, r0, dr):
# CBEDThicknessMeasure(lam, d0, r0, dr]
#
# lam = wavelength of electron beam.
# 200 keV = 0.025079
# 300 keV = 0.019687
#
# d0 = d-spacing for the reflection. In the case of not quite 2-beam
# conditions, assume the center is the dark band in the 000 spot and then
# measure the d-spacing to the center of the bright band anywhere on the
# image.
#
# r0 = distance on the image in pixels or microns or mm or whatever
# arbitrary measure.
#
# dr = distance from the center of the reflection bright band to the
# minimum in the same units as r0. This is a vector for if 5 elements,
# then 5 minima are recorded.
#
# Example:
# lam = 0.025301 # A
# d0 = 1.3577 # A
# r0 = 355
# dr[1] = 20.18
# dr[2] = 37.18
# dr[3] = 50.92
# dr[4] = 63.74
# dr[5] = 77.55
# dr[6] = 90.29
# dr[7] = 104.12
# dr[8] = 117.91
# r0 = 507
# dr[1] = 32
# dr[2] = 53
# dr[3] = 73
# lam = 0.019687 # A
# d0 = 0.7557 # A
# r0 = 20.2
# dr[1] = 1.66
# dr[2] = 3.23
# dr[3] = 4.72
s = np.zeros(len(dr))
for i in range(len(dr)):
s[i] = lam*dr[i] / (d0**2 * r0)
plt.figure()
maxy = 0
for i in range(10):
n = np.array(range(len(dr))) + 1 + i
x = (1/n)**2
y = (s/n)**2
# n = i:1:i+length[dr]-1
# x = [1./n].^2
# y = [s./n].^2
plt.plot(x,y, 'b')
P = np.polyfit(x, y, 1)
plt.plot(x, P[1] + P[0]*x, 'r')
#plot(x, P[2] + P[1]*x, 'r')
print 'n=' + str(n[0]) + ' -> t = ' + str(np.sqrt(1/P[1])) + ' Angstroms, Xi = ' + str(np.sqrt(abs(1/P[0]))) + ' Angstroms.'
if max(y) > maxy:
maxy = max(y)
plt.xlabel('$1/n_i^2$')
plt.ylabel('$(s_i/n_i)^2$')
plt.show()
#axis([0 1 0 maxy])
return
# lam = 0.019687 # A
# d0 = 1.7 # A
# r0 = 6
# dr = np.array([ 0.367, 0.762, 1.129, 1.581])
#
# lam = 0.019687 # A
# d0 = 1.7 # A
# r0 = 5.89
# dr = np.array([ 0.282, 0.875, 1.214, 1.689, 2.117])-0.282
lam = 0.019687 # A
d0 = 0.66 # A
r0 = 15.17
dr = np.array([ 0.226, 0.706, 1.073])
# lam = 0.019687 # A
# d0 = 0.70 # A
# r0 = 14.27
# dr = np.array([ 0.226, 0.565, 0.988])
CBEDThicknessMeasure(lam, d0, r0, dr)