%% This script does not work yet. There are many edge cases to deal with.
% sometime BOG has two 0 casts. Need more test data to figure this out.
% then add this within the Automate data loop
nprofiles = length(RSKprofiles.profiles.upcast.tstart); % n profiles
% for profile = 1:nprofiles
for profile = 5
    castidx = getdataindex(RSKprofiles, profile, 'up');
    maxprofiledepth = max(RSKprofiles.data(castidx).values(:,10)); % Depth
    if maxprofiledepth < 50 % Incomplete cast. Skip to next cast
        continue
    end

    BOGDepth = 0:5:500;
    RSKdepth = RSKprofiles.data(profile).values(:,10); % Depth
    RSKtemperature = RSKprofiles.data(profile).values(:,2); % Temperature
    RSKSalinity = RSKprofiles.data(profile).values(:,11); % Salinity
    RSKDoxy = RSKprofiles.data(profile).values(:,5); % Doxy
    RSKChlorophyll_a1 = RSKprofiles.data(profile).values(:,7); % Chla1
    RSKChlorophyll_a2 = RSKprofiles.data(profile).values(:,8); % Chla2
    RSKBackscatter = RSKprofiles.data(profile).values(:,6); % Backscatter
    
    variableNames = {'DEPTH','RSKDepth','Temperature', 'Salinity', 'Doxy', ...
                     'Chlorophyll_a1', 'Chlorophyll_a2', 'Backscatter'};
    RSK_BOG_Grid = table('Size', [length(BOGDepth), length(variableNames)], ...
                         'VariableTypes', repmat({'double'}, 1, length(variableNames)), ...
                         'VariableNames', variableNames);

    RSK_BOG_Grid.DEPTH = BOGDepth';
    
    for i = 1:length(BOGDepth) 
        targetValue = BOGDepth(i);
        if i == 1
            targetValue = .25; % 0 on the RSK is an air sample
        end
        [mindiff, idx] = min(abs(RSKdepth - targetValue));
        if mindiff > .5
            RSK_BOG_Grid.RSKDepth(i) = NaN;
            RSK_BOG_Grid.Temperature(i) = NaN;
            RSK_BOG_Grid.Salinity(i) = NaN;
            RSK_BOG_Grid.Doxy(i) = NaN;
            RSK_BOG_Grid.Chlorophyll_a1(i) = NaN;
            RSK_BOG_Grid.Chlorophyll_a2(i) = NaN;
            RSK_BOG_Grid.Backscatter(i) = NaN;
        else
            RSK_BOG_Grid.RSKDepth(i) = RSKdepth(idx);
            RSK_BOG_Grid.Temperature(i) = RSKtemperature(idx);
            RSK_BOG_Grid.Salinity(i) = RSKSalinity(idx);
            RSK_BOG_Grid.Doxy(i) = RSKDoxy(idx);
            RSK_BOG_Grid.Chlorophyll_a1(i) = RSKChlorophyll_a1(idx);
            RSK_BOG_Grid.Chlorophyll_a2(i) = RSKChlorophyll_a2(idx);
            RSK_BOG_Grid.Backscatter(i) = RSKBackscatter(idx);
        end
    end
    RSKprofiledatetime = datetime(...
        datevec(RSKprofiles.profiles.downcast.tstart(profile)),...
        'TimeZone','UTC'); % Extract the downcast tstart
    [MatchingCastIndexBOG, mintimeBOG] = matchingCastIndices(BOG.DATE_TIME, RSKprofiledatetime); % What is BOG vs BOG2
    [MatchingCastIndexBOG2, mintimeBOG2] = matchingCastIndices(BOG2.DATE_TIME, RSKprofiledatetime); % Can set up BOG SQL to replace this step
    B = BOG(MatchingCastIndexBOG,:);
    CombinedTable = join(B,RSK_BOG_Grid,'Keys', {'DEPTH'});
    variableNames = {'DEPTH','DepthDiff','Temperature', 'Salinity', 'Doxy', ...
                     'Chlorophyll_a1', 'Chlorophyll_a2', 'Backscatter'};
    ResidualTable = table('Size', [height(B), length(variableNames)], ...
                         'VariableTypes', repmat({'double'}, 1, length(variableNames)), ...
                         'VariableNames', variableNames);
    ResidualTable.DEPTH = CombinedTable.DEPTH;
    ResidualTable.DepthDiff = CombinedTable.DEPTH - CombinedTable.RSKDepth;
    ResidualTable.Temperature = CombinedTable.tc - CombinedTable.Temperature;
    ResidualTable.Salinity = CombinedTable.psal - CombinedTable.Salinity;
    ResidualTable.Doxy = CombinedTable.doxy - CombinedTable.Doxy;
    % ResidualTable.Chlorophyll_a1 = NaN; % need this data
    % ResidualTable.Chlorophyll_a2 = NaN; % need this data
    % ResidualTable.Backscatter = NaN; % need this data

    figure
cla; clf;
TL = tiledlayout(1,7);
title(TL,'Residuals (BOG - RBR)')

% Common plot settings
dotSize = 10;
dotColor = 'r';

% Plot 1: Depth
nexttile
scatter(ResidualTable.DepthDiff, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.DepthDiff)) max(abs(ResidualTable.DepthDiff))])
title('Depth')

% Plot 2: Temperature
nexttile
scatter(ResidualTable.Temperature, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Temperature)) max(abs(ResidualTable.Temperature))])
yticklabels([]) % Remove y-axis tick labels
title('Temperature')

% Plot 3: Salinity
nexttile
scatter(ResidualTable.Salinity, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Salinity)) max(abs(ResidualTable.Salinity))])
yticklabels([])
title('Salinity')

% Plot 4: Doxy
nexttile
scatter(ResidualTable.Doxy, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Doxy)) max(abs(ResidualTable.Doxy))])
yticklabels([])
title('Doxy')

% Plot 5: Chlorophyll a1
nexttile
scatter(ResidualTable.Chlorophyll_a1, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Chlorophyll_a1)) max(abs(ResidualTable.Chlorophyll_a1))])
yticklabels([])
title('Chla a1')

% Plot 6: Chlorophyll a2
nexttile
scatter(ResidualTable.Chlorophyll_a2, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Chlorophyll_a2)) max(abs(ResidualTable.Chlorophyll_a2))])
yticklabels([])
title('Chla 2')

% Plot 7: Backscatter
nexttile
scatter(ResidualTable.Backscatter, ResidualTable.DEPTH, dotSize, 'filled', 'MarkerFaceColor', dotColor)
hold on
grid on
xline(0, 'k--')
set(gca, 'YDir', 'reverse')
xlim([-max(abs(ResidualTable.Backscatter)) max(abs(ResidualTable.Backscatter))])
yticklabels([])
title('Backscatter')

end
%%

