

clear
% close all


% load MMS data
trex = readtable('MMS_NaOH_stdAdd_20180530.txt');

MMS.Dark = trex{:,6}';
MMS.I_blank = trex{1,10:end}';
MMS.I = trex{2:end,10:end}';
MMS.I_D = MMS.I - MMS.Dark(2:end);
MMS.A = log10((MMS.I_blank - MMS.Dark(1))./(MMS.I_D));

% calculate lamda for MMS

% UVVIS1. Pretty sure this is the correct UVVIS 
C0 = +2.455646892E+02;
C1 = +2.136483472E+00;
C2 = +1.005266419E-04;
C3 = -7.432017061E-07;
C4 = +0.0;

% % UVVIS2
% C0 = +2.457748093E+02;
% C1 = +2.136883848E+00;
% C2 = +8.514133181E-05;
% C3 = -7.207947759E-07;
% C4 = +0.0;
X = [1:256]'; 
MMS.Lambda = C0 + C1.*X + C2.*X.^2 + C3.*X.^3;

% correct for baseline
ibase = inrange(MMS.Lambda, [750 760]);
MMS.Acorr = MMS.A - mean(MMS.A(ibase,:));



clearvars -except MMS


% load Agilent data
trex = readtable('Agilent_NaOH_stdAdd_20180530.txt');
Ag.A = trex{:,201:601}';
Ag.Lambda = [400:800]';
ibase = inrange(Ag.Lambda, [750 760]);
Ag.Acorr = Ag.A - mean(Ag.A(ibase,:));

clear trex

% interpolate MMS onto 1nm grid
% MMS.Ainterp = interp1(MMS.Lambda, MMS.Acorr, 


% interpolate MMS onto grid
lambda_off = -1; % offset to apply to MMS lambda
MMS.Aint1 = interp1(MMS.Lambda, MMS.Acorr, Ag.Lambda + lambda_off, 'linear');
MMS.Aintspline = interp1(MMS.Lambda, MMS.Acorr, Ag.Lambda + lambda_off, 'spline');
MMS.Aintspline(Ag.Lambda > 780,:) = NaN;
MMS.Ainterp_resid = MMS.Aint1 - MMS.Aintspline; % residual between linear fit and spline fit. Max residual was < 1 mA, so probably not significant... 

% Calculate

figure
hold on; box on;
plot(Ag.Lambda, MMS.Aintspline - Ag.Acorr);






















