%% load profiles
mission = '24403401';
prof = 535;
radius = 5;
months = 1;
years = 2;
mindepth = 500;
FilterRadius = 1;
FilterMonths = 1;
FilterYears = 1;
FilterMinDepth = 1;

filename = fullfile('C:', 'Users', 'bwerb', 'Documents',...
            'SprayDashBoard', 'GlideAppPackage', 'TestProjectLibrary',...
            char(mission), 'sat_files', [char(mission), '_sat.mat']);
load(filename,"s")
load("GliderIndex_v1.mat","Glidex")
Spray2Plot = filterSpray(Glidex, s, mission, prof, radius, months,...
    years, mindepth, FilterRadius, FilterMonths, FilterYears, FilterMinDepth);

%%
tic
[DepIDs, ii] = unique(Spray2Plot.DepID,'first');
files = Spray2Plot.("file location")(ii);
sc = struct;
sc.pH25atm = NaN(3000, length(Spray2Plot.DepProfile));
sc.tc = NaN(3000, length(Spray2Plot.DepProfile));
sc.doxy = NaN(3000, length(Spray2Plot.DepProfile));
sc.psal = NaN(3000, length(Spray2Plot.DepProfile));
sc.pres = NaN(3000, length(Spray2Plot.DepProfile));
sc.lat = NaN(1, length(Spray2Plot.DepProfile));
sc.lon = NaN(1, length(Spray2Plot.DepProfile));
startindex = 1;
for i = 1:length(DepIDs)
    sprayfile = load(files(i), "s");
    iiprofs = Spray2Plot.DepID == DepIDs(i);
    profs = Spray2Plot.DepProfile(iiprofs);
    len = length(profs);
    h = height(sprayfile.s.pH25atm);
    endindex = startindex + len - 1;
    sc.pH25atm(1:h, startindex:endindex) = sprayfile.s.pH25atm(:, profs);
    sc.tc(1:h, startindex:endindex) = sprayfile.s.tc(:, profs);
    sc.psal(1:h, startindex:endindex) = sprayfile.s.tc(:, profs);
    sc.doxy(1:h, startindex:endindex) = sprayfile.s.tc(:, profs);
    sc.pres(1:h, startindex:endindex) = sprayfile.s.pres(:, profs);
    sc.lat(1, startindex:endindex) = sprayfile.s.lat(:, profs);
    sc.lon(1,startindex:endindex) = sprayfile.s.lon(:, profs);
    startindex = endindex + 1;
end
sc.pH25atm(sc.pH25atm == -999) = NaN;
sc.tc(sc.tc == -999) = NaN;
sc.psal(sc.psal == -999) = NaN;
sc.doxy(sc.doxy == -999) = NaN;
sc.pres(sc.pres == -999) = NaN;

me = toc
% need to index out the bad profs
% 
%% gpt

tic
% Unique IDs and file locations
[DepIDs, ii] = unique(Spray2Plot.DepID, 'first');
files = Spray2Plot.("file location")(ii);

% Predefine struct fields
numProfiles = length(Spray2Plot.DepProfile);
sc.pH25atm = NaN(3000, numProfiles);
sc.tc = NaN(3000, numProfiles);
sc.doxy = NaN(3000, numProfiles);
sc.psal = NaN(3000, numProfiles);
sc.pres = NaN(3000, numProfiles);
sc.lat = NaN(1, numProfiles);
sc.lon = NaN(1, numProfiles);

startindex = 1;
for i = 1:length(DepIDs)
    sprayfile = load(files(i), "s");
    iiprofs = Spray2Plot.DepID == DepIDs(i);
    profs = Spray2Plot.DepProfile(iiprofs);
    len = length(profs);
    h = height(sprayfile.s.pH25atm);
    endindex = startindex + len - 1;
    
    % Temporary variables for struct fields
    s = sprayfile.s;
    
    sc.pH25atm(1:h, startindex:endindex) = s.pH25atm(:, profs);
    sc.tc(1:h, startindex:endindex) = s.tc(:, profs);
    sc.psal(1:h, startindex:endindex) = s.psal(:, profs);  % Corrected from s.tc
    sc.doxy(1:h, startindex:endindex) = s.doxy(:, profs);  % Corrected from s.tc
    sc.pres(1:h, startindex:endindex) = s.pres(:, profs);
    sc.lat(1, startindex:endindex) = s.lat(:, profs);
    sc.lon(1, startindex:endindex) = s.lon(:, profs);
    
    startindex = endindex + 1;
end

% Replace -999 with NaN in one go
sc.pH25atm(sc.pH25atm == -999) = NaN;
sc.tc(sc.tc == -999) = NaN;
sc.psal(sc.psal == -999) = NaN;
sc.doxy(sc.doxy == -999) = NaN;
sc.pres(sc.pres == -999) = NaN;

gpt = toc





%%
[B] = loadBOG();
prof = 535;
radius = 5;
months = 1;
years = 5;
mindepth = 500;
FilterRadius = 1;
FilterMonths = 1;
FilterYears = 1;
FilterMinDepth = 1;
BOG2Plot = filterBOG(B, s, prof, radius, months, years, FilterRadius, FilterMonths, FilterYears);
%%
figure
clf
plot(pH25atm,pres,'Color','blue',LineWidth=.3)
hold on
plot(BOG2Plot.pH25atm,BOG2Plot.pres,'o','Color','green')
plot(s.pH25atm(:,prof),s.pres(:,prof),'Color','red',LineWidth=3)
set(gca,'YDir','reverse')
legend('spraydata','BOG data','current spray profile')