fileloadpath = ['\\atlas.shore.mbari.org\projectlibrary\' ...
    '901805_Coastal_Biogeochemical_Sensing\Spray_Data\25202901\' ...
    '\QC\25202901_Calibrated.mat'];
filesavepath = 'C:\Users\bwerb\Documents\dep_25202901\QCPlots_QC_ProfandDiag\QC';
load(fileloadpath,'s');
n = length(s.sdn);
xvars = {'tc','psal','opt','pHin','doxy','par'};
yvar = {'pres'};
xlabels = {'Temperature (^oC)','Salinity (PSU)',...
    'Fluoresence', 'pHin', 'Doxy (\mumol/kg)', 'PAR'};
xlims = [3.5 14; 32.5 34.5; 0 2; 7.5 8; 0 300; 0 100];

for profile = 1:n      
    figure(Visible="off")
    clf
    tl = tiledlayout(2,3);
    title(tl,['QC plots 25202901: ', num2str(profile)])

    % upcastidx = diff(s.pres(:,profile));
    % upcastidx = [upcastidx; 1]; %#ok<AGROW>
    % % upcastidx = upcastidx < 0; % downcast
    % upcastidx = upcastidx > 0 & s.pres(:,profile) > 0; % think this is upcast. Need a way to remove weird last data points

    
    for i = 1:6
        nexttile(tl)
        plot(s.(xvars{i})(:,profile),s.(yvar{1})(:,profile),'.')
        % plot(s.(yvar{1})(upcastidx,profile),s.(xvars{i})(upcastidx,profile),'.')
        xlabel(xlabels{i})
        ylabel('Pressure (Dbar)')
        grid on
        set(gca,'Ydir','reverse')
        % ylim(ylims(i,:))
        xlim(xlims(i,:))
        % hold on
        % yyaxis right
        % plot(s.(yvar{1})(upcastidx,profile),s.Ik(upcastidx,profile),'.')
        % ylabel('Ik (nA)')
        disp(['generating figure ', num2str(profile), '/', num2str(n)])
    end
    % Create the filename for saving the figure
    filename = fullfile(filesavepath, sprintf('QCPlot_profile_upcast_%d.png', profile));
    
    % Save the figure as a PNG file
    saveas(gcf, filename)
end