clear data;
close all;
tableHeader = {'CastNum', 'DateNum', 'CTDPress', 'CTDTemp', 'CTDSal', 'InternalTemp', ...
          'InternalHumidity', 'SystemPowerV', 'SystemPowerI', ...
          'BackupBattV', 'Vrs', 'stdVrs', 'Vk', 'stdVk', 'Ik', 'Ib', 'pHTotal'};

data = array2table(merge_pH);
data.Properties.VariableNames = tableHeader;

DateTime = datetime(data.DateNum, 'ConvertFrom', 'datenum');
data = addvars(data,DateTime,'Before', 1);

numProfiles = max(data.CastNum);

figLeft = 0.01;
leftIncr = 0.25;
figBottom = 0.51;
figWidth = 0.24;
figHeight = 0.4;
fig = figure;
fig.Units = 'normalized';
fig.Position = [figLeft figBottom figWidth figHeight];
ax = gca;
ax.YDir = 'reverse';
hold on;
for i=1:numProfiles
    index = find(data.CastNum == i);
    plot(data.pHTotal(index), data.CTDPress(index))
    label(i) = "Profile " + compose("%2d", i);
end
legend(label)
title('pH vs Depth');
xlabel('pH');
ylabel('Depth (dBar)');
hold off
grid on

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
for i=1:numProfiles
    index = find(data.CastNum == i);
    pH = [NaN; data.pHTotal(index)];
    depth = [NaN; data.CTDPress(index)];
    dateNum = data.DateNum(index(1));
    hold on
    waterfall(pH', dateNum, depth')
    datetick('y');
end
ax = gca;
ax.ZDir = 'reverse';
view(3);
hidden off;
grid on
title('pH vs Depth');
xlabel('pH');
ylabel('Date');
zlabel('Depth (dBar)');
hold off
zlabel = 'Depth (dBar)';

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    VrsMean(i) = mean(data.Vrs(index));
    VrsRange(i) = max(data.Vrs(index)) - min(data.Vrs(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    VrsMeanUpper200m(i) = mean(data.Vrs(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    VrsMeanLower200m(i) = mean(data.Vrs(index));
end
yyaxis left
plot(DateTime, VrsMean, DateTime, VrsMeanUpper200m, DateTime, VrsMeanLower200m)
yyaxis right
xlabel('Date');
ylabel('Vrs');
plot(DateTime, VrsRange)
legend('Vrs Mean', 'Vrs Mean Upper 200m', 'Vrs Mean Lower 200m', 'Vrs Range')
title('Vrs vs Time');
xlabel('Date');
ylabel('Vrs Range');

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    stdVrsMean(i) = mean(data.stdVrs(index));
    stdVrsRange(i) = max(data.stdVrs(index)) - min(data.stdVrs(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    stdVrsMeanUpper200m(i) = mean(data.stdVrs(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    stdVrsMeanLower200m(i) = mean(data.stdVrs(index));
end
yyaxis left
plot(DateTime, stdVrsMean, DateTime, stdVrsMeanUpper200m, DateTime, stdVrsMeanLower200m)
xlabel('Date');
ylabel('Vrs Stdev Range');
yyaxis right
plot(DateTime, stdVrsRange)
legend('stdVrs Mean', 'stdVrs Mean Upper 200m', 'stdVrs Mean Lower 200m', 'stdVrs Range')
title('Vrs Std Dev vs Time');
xlabel('Date');
ylabel('Vrs Stdev Range');

fig = figure;
fig.Units = 'normalized';
figLeft = 0.01;
figBottom = 0.01;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    VkMean(i) = mean(data.Vk(index));
    VkRange(i) = max(data.Vk(index)) - min(data.Vk(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    VkMeanUpper200m(i) = mean(data.Vk(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    VkMeanLower200m(i) = mean(data.Vk(index));
end
yyaxis left
plot(DateTime, VkMean, DateTime, VkMeanUpper200m, DateTime, VkMeanLower200m)
xlabel('Date');
ylabel('Vk');
yyaxis right
plot(DateTime, VkRange)
legend('Vk Mean', 'Vk Mean Upper 200m', 'Vk Mean Lower 200m', 'Vk Range')
title('Vk vs Time');
xlabel('Date');
ylabel('Vk Range');

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    stdVkMean(i) = mean(data.stdVk(index));
    stdVkRange(i) = max(data.stdVk(index)) - min(data.stdVk(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    stdVkMeanUpper200m(i) = mean(data.stdVk(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    stdVkMeanLower200m(i) = mean(data.stdVk(index));
end
yyaxis left
plot(DateTime, stdVkMean, DateTime, stdVkMeanUpper200m, DateTime, stdVkMeanLower200m)
xlabel('Date');
ylabel('Vk Std Dev');
yyaxis right
plot(DateTime, stdVkRange)
legend('stdVk Mean', 'stdVk Mean Upper 200m', 'stdVk Mean Lower 200m', 'stdVk Range')
title('Vk Std Dev vs Time');
xlabel('Date');
ylabel('Vk Std Dev Range');

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    IkMean(i) = mean(data.Ik(index));
    IkRange(i) = max(data.Ik(index)) - min(data.Ik(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    IkMeanUpper200m(i) = mean(data.Ik(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    IkMeanLower200m(i) = mean(data.Ik(index));
end
yyaxis left
plot(DateTime, IkMean, DateTime, IkMeanUpper200m, DateTime, IkMeanLower200m)
xlabel('Date');
ylabel('Ik');
yyaxis right
plot(DateTime, IkRange)
legend('Ik Mean', 'Ik Mean Upper 200m', 'Ik Mean Lower 200m', 'Ik Range')
title('Ik vs Time');
xlabel('Date');
ylabel('Ik Range');

fig = figure;
fig.Units = 'normalized';
figLeft = figLeft + leftIncr;
fig.Position = [figLeft figBottom figWidth figHeight];
clear DateTime
for i=1:numProfiles
    index = find(data.CastNum == i);
    DateTime(i) = data.DateTime(index(1));
    IbMean(i) = mean(data.Ib(index));
    IbRange(i) = max(data.Ib(index)) - min(data.Ib(index));
    index = find((data.CastNum == i) & (data.CTDPress <= 200.0));
    IbMeanUpper200m(i) = mean(data.Ib(index));
    index = find((data.CastNum == i) & (data.CTDPress > 200.0));
    IbMeanLower200m(i) = mean(data.Ib(index));
end
yyaxis left
plot(DateTime, IbMean, DateTime, IbMeanUpper200m, DateTime, IbMeanLower200m)
xlabel('Date');
ylabel('Ib');
yyaxis right
plot(DateTime, IbRange)
legend('Ib Mean', 'Ib Mean Upper 200m', 'Ib Mean Lower 200m', 'Ib Range')
title('Ib vs Time');
xlabel('Date');
ylabel('Ib Range');

