%pylab inline
import emcee
import triangle
import pandas as pd
import seaborn as sns
from astroML.decorators import pickle_results

sns.set_context("paper", font_scale=4.0, rc={"lines.linewidth": 2.5})
sns.set(style="ticks")

"""
Explanation: This IPython Notebook is for showing spectra of sample VG09-12, on- and off- the mesh region with 49+/-6 nm gap.  This version is updated with new data from February 2015.
The filename of the figure is [TBD].pdf.
Author: Michael Gully-Santiago, gully@astro.as.utexas.edu
Date: February 9, 2015
End of explanation
"""


from etalon import *
np.random.seed(78704)

"""
Explanation: Import all the local models, saved locally as etalon.py.  See the paper for derivations of these equations.
End of explanation
"""


df = pd.read_csv('../data/VG09_12_gap_20150206.csv', index_col=0)
df.head()

"""
Explanation: Read in the data.  We want "VG09-12" from February 2015
End of explanation
"""


# Introduce the Real data, decimate the data.
x = df.index.values
N = len(x)
# Define T_DSP for the model
T_DSP = T_gap_Si(x, 0.0)
n1 = sellmeier_Si(x)

# Define uncertainty
yerr = 0.0002*np.ones(N)
yerr[(x > 1350) & (x < 1420)] = 0.0005 #higher noise in this region
iid_cov = np.diag(yerr ** 2)

# Select the spectrum of interest
# Normalize the spectrum by measured DSP Si wafer.
y = df['VG09-12_1_14'].values

"""
Explanation: The step interval is 2.0 mm.  There are 26 steps.
End of explanation
"""


plt.plot(df.index, df['VG09-12_1_4'], label='On-mesh- 4 mm')
plt.plot(df.index, df['VG09-12_1_6'], label='On-mesh- 6 mm')
plt.plot(df.index, df['VG09-12_1_8'], label='On-mesh- 8 mm')
plt.plot(df.index, df['VG09-12_1_10'], label='On-mesh- 10 mm')
plt.plot(df.index, df['VG09-12_1_12'], label='On-mesh- 12 mm')
plt.plot(df.index, df['VG09-12_1_14'], label='On-mesh- 14 mm')
plt.plot(df.index, df['VG09-12_1_16'], 'k.', label='Mesh Boundary')
plt.plot(df.index, df['VG09-12_1_18'], 'b-.', label='Off-mesh- 18 mm')

fit1 = T_gap_Si_withFF_fast(x, 43, 0.5, n1)/T_DSP
fit2 = T_gap_Si_withFF_fast(x, 55, 0.5, n1)/T_DSP
fit2_label = 'Model with $d_M={:.0f}\pm{:.0f}$ nm, $\epsilon={:.0f}$'.format(49, 6, 0)
plt.fill_between(x, fit1, fit2, alpha=0.6, color=sns.xkcd_rgb["green apple"])
plt.plot([-10, -9], [-10, -9],"-", alpha=0.85, color=sns.xkcd_rgb["green apple"], label=fit2_label)

plt.plot([-1000, 9000], [1,1], 'k--')
plt.legend(loc='best')
plt.ylim(0.85, 1.05)
plt.xlim(1250, 1780)
plt.ylabel("$T_{gap}$")
plt.xlabel("$\lambda$ (nm)");
plt.savefig("VG0912_STA_scan.pdf",  bbox_inches='tight')

sns.set_context("paper", font_scale=1.5, rc={"lines.linewidth": 2.0})

plt.step(df.index, df['VG09-12_1_4'], label='On-mesh- 4 mm')
plt.step(df.index, df['VG09-12_1_6'], label='On-mesh- 6 mm')
plt.step(df.index, df['VG09-12_1_8'], label='On-mesh- 8 mm')
plt.step(df.index, df['VG09-12_1_10'], label='On-mesh- 10 mm')
plt.step(df.index, df['VG09-12_1_12'], label='On-mesh- 12 mm')
plt.step(df.index, df['VG09-12_1_14'], label='On-mesh- 14 mm')
#plt.plot(df.index, df['VG09-12_1_16'], 'k.', label='Mesh Boundary')
plt.plot(df.index, df['VG09-12_1_18'], 'b.', label='Off-mesh- 18 mm')

fit1 = T_gap_Si_withFF_fast(x, 43, 0.3, n1)/T_DSP
fit2 = T_gap_Si_withFF_fast(x, 55, 0.75, n1)/T_DSP
fit2_label = 'Model with $d_M={:.0f}\pm{:.0f}$ nm, ${:.1f}<f<{:.2f}$'.format(49, 6, 0.30, 0.75)
plt.fill_between(x, fit1, fit2, alpha=0.6, color='#cccccc')
plt.plot([-10, -9], [-10, -9],"-", alpha=0.0, color='#cccccc', label=fit2_label)

plt.plot([-1000, 9000], [1,1], 'k--')
plt.legend(loc='best')
# Complete hack to get a shaded band in the legend.
plt.fill_between([1390, 1440], [0.825, 0.825], [0.845,0.845], alpha=0.6, color='#cccccc')
plt.ylim(0.82, 1.02)
plt.xlim(1250, 1780)
plt.ylabel("$T_{gap}$")
plt.xlabel("$\lambda$ (nm)");
plt.savefig("../figs/VG0912_STA_scan_coarse.pdf",  bbox_inches='tight')

"""
Explanation: Plot the data.  This is the original plot.
End of explanation
"""


