%input: mfet data, spec pH data, CTD data
% 1) specfile and beamsfile (.txt)
% 2) spec pH_tot -> pH_insitu
% 3) sync datetimes
% 4) k0 for each pH_insitu value
% 5) mean k0
% 6) pH calc for all BEAMS vrse
%output: averaged k0 and std
clear all; close all;
%% data names
specfile = 'INPUTFILENAMEHERE.TXT'; % like '20240509_T51_pH_k0_spec.txt'
beamsfile = 'INPUTFILENAMEHERE.TXT'; % like 'M3240529.txt'
%% load data
% CTD = readCTD('2024-05.csv'); % if statement here: 
% if ctd is present on the board then use that data. else pull the latest data from mlml ctd and use that instead.
Spec = readSpec(specfile);
GetMfetData(beamsfile);
BEAMS = data;
BEAMS.("MM/DD/YYYY HH:MM:SS").TimeZone = 'UTC'; % annoying naming situation
BEAMS.("MM/DD/YYYY HH:MM:SS").Second = 0;
%% k2
k2_T51 = -0.0010626;
%% Calc insitu pH
trex = CO2SYS(2270, Spec.pH_tot,...
    1, 3, Spec.Salinity,...
    20, Spec.Temp_used, 0, 0, 0, 0, 1, 10, 1); % used or samp?
pH_insitu = trex(:,18); % Grab insitu pH
Spec.pH_insitu = pH_insitu;

%% Intersecting times with spec pH

[~, iBEAMS, ~] = intersect(BEAMS.("MM/DD/YYYY HH:MM:SS"),Spec.DateTime);

BEAMS_intersect = BEAMS(iBEAMS,:);
%% Calc k0 (one value for each spec pH sample)
%
k0 = nan(height(Spec.DateTime),1);
for i = 1:height(Spec.DateTime)
    k0(i) = calc_k0_ext(BEAMS_intersect.Vrse(i), BEAMS_intersect.TC_MCat(i),...
        BEAMS_intersect.pSal(i), Spec.pH_insitu(i), k2_T51);
end
%% Stats

k0_std = std(k0);
k0_mean = mean(k0);

%% Calculate pH
[pHext_tot, pHext_free] = calc_dfet_pHext_k0(BEAMS.Vrse,...
    BEAMS.TC_MCat, BEAMS.pSal, k0_mean, k2_T51);

%% plot!
figure(1)
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),pHext_tot)
hold on
plot(Spec.DateTime,Spec.pH_insitu,'.')
legend('MBARI MFET','Spec pH Insitu')
%% more plot
figure(2)
tl = tiledlayout(6,1);

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Vrse)
ylabel('Vrse')
hold on
yyaxis right
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Vk)
ylabel('Vk')

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Vrse_std)
ylabel('Vrse std')
hold on
yyaxis right
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Vk_std)
ylabel('Vk std')

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.TC_cont)
ylabel('Board Temp')
hold on
yyaxis right
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Humidity)
ylabel('Humidity')

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Ik)
ylabel('Ik')
hold on
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.Ib)
ylabel('Ib')

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.VbatMain)
ylabel('main batt voltage')

nexttile
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),BEAMS.VbiasPos)
ylabel('bias batt pos')
%%
[pHext_tot_compare, ~] = calc_dfet_pHext_k0(BEAMS_intersect.Vrse,...
    BEAMS_intersect.TC_MCat, BEAMS_intersect.pSal, k0_mean, k2_T51);
%%
figure(3)
plot(BEAMS.("MM/DD/YYYY HH:MM:SS"),pHext_tot)
legend('BEAMS')
%%
figure(4)
tiledlayout('flow')

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Vrse, '.')
ylabel('Vrse', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Vk, '.')
ylabel('Vk', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('Vrse and Vk')
hold off

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.TC_opt, '.')
ylabel('TC_opt', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.TC_MCat, '.')
ylabel('TC_MCat', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('TC_opt and TC_MCat')
hold off

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.pSal, '.')
ylabel('pSal', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.O2satper, '.')
ylabel('O2satper', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('pSal and O2satper')
hold off

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Ik, '.')
ylabel('Ik', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Ib, '.')
ylabel('Ib', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('Ik and Ib')
hold off

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.VbatMain, '.')
ylabel('VbatMain', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Humidity, '.')
ylabel('Humidity', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('VbatMain and Humidity')
hold off

nexttile
yyaxis left
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Vrse_std, '.')
ylabel('Vrse_std', 'Color', 'b')
ax = gca;
ax.YAxis(1).Color = 'b';
hold on
yyaxis right
scatter(BEAMS.("MM/DD/YYYY HH:MM:SS"), BEAMS.Vk_std, '.')
ylabel('Vk_std', 'Color', 'r')
ax.YAxis(2).Color = 'r';
grid on
title('Vrse_std and Vk_std')
hold off
