function sc = SpraySearch(Spray2Plot)
% SpraySearch takes in profiles from Spray2Plot and outputs a struct 'sc',
% spray compare, that has the data from the profiles. tc, pH25atm, psal,
% doxy.
    % 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;
end
