[k0, Eo_ext_insitu] = calc_k0_ext(app.meanvrse, app.meanCTD_temp, app.meanCTD_psal+.1566, app.spec_pH_insitu, -1.00E-03)
[k0, Eo_ext_insitu] = calc_k0_ext(-0.88279144, 18.43508, 33.68184, app.spec_pH_insitu,-1.00E-03)

%

trex = CO2SYSv3(2270, app.spec_pH, 1, 3, app.meanCTD_psal, app.spec_Temp_used, app.meanCTD_temp, 0, 0, 0, 0, 0, 0, 1, 10, 1, 1, 1);
% Spec pH input for next step
app.spec_pH_insitu = trex(:,20);

%


[file, path] = uigetfile('*.txt', 'Select a Text File');
if isequal(file, 0)
    return; % User canceled file selection
end

% Read the text file
app.GliderfullPath = fullfile(path, file);

try
    [~, selectedLines] = indexLinesWithPattern(app.GliderfullPath, 'Z_1>');
    data = cellstr(selectedLines);
    data = data(~contains(data,'SBE'));
    
    for i = 1:height(data)
        data(i,1:3) = strsplit(char(data(i)),'  ');
    end
    
    data = str2double(data);
    mn = mean(data);
    
    app.meanCTD_temp = mn(1,2);
    app.meanCTD_psal = mn(1,3);
    
    
    [~, selectedLines] = indexLinesWithPattern(app.GliderfullPath, 'A_1>');
    vrsedata = cellstr(selectedLines);
    vrsedata = vrsedata(contains(vrsedata,'ch = '));
    
    for i = 1:height(vrsedata)
        vrsedata(i,1:9) = strsplit(char(vrsedata(i)),'\t');
    end
    vrse = str2double(vrsedata(:,4)); % 4 is vrse, 5 is vrse std. date, time imports as datetime (1 col not 2)
    app.meanvrse = mean(vrse);
    vk = str2double(vrsedata(:,6)); % 6 is vk, 7 is vk std. date, time imports as datetime (1 col not 2)
    app.meanvk = mean(vk);
    
    % If successful, set button to green and display success message
    app.GliderCTDandpHButton.BackgroundColor = [0, 1, 0]; % Green
    app.gliderlabel.Value = {'Data loaded successfully',file,...
        ['mean vrse: ',num2str(app.meanvrse)],...
        ['mean CTD temperature: ',num2str(app.meanCTD_temp)]...
        ['mean CTD salinity: ',num2str(app.meanCTD_psal)]};
catch ME
    % If there's an error, set button to red and display error message
    app.GliderCTDandpHButton.BackgroundColor = [1, 0, 0]; % Red
    app.gliderlabel.Value = sprintf('Error loading data: %s', ME.message);
end

%

% Load the text file
[file, path] = uigetfile('*.txt', 'Select a Text File');
if isequal(file, 0)
    return; % User canceled file selection
end

% Read the text file
app.SpecfullPath = fullfile(path, file);

try
    % Attempt to detect import options
    opts = detectImportOptions(app.SpecfullPath);
    opts = setvaropts(opts, 'DateTime', 'InputFormat', 'MM/dd/uuuu hh:mm aa');
    opts.VariableNamesLine = 2;
    opts.DataLines = [3, inf];

    % Attempt to read the table
    specpH = readtable(app.SpecfullPath, opts);

    % Attempt to remove junk comments
    specpH = specpH(~contains(specpH.Comments, 'junk'), :);
    specpH = specpH(1,:); % only use first sample
    % Attempt to extract pH and Temperature used
    app.spec_pH = specpH.pH_tot;
    app.spec_Temp_used = specpH.Temp_used;
    
    % If successful, set button to green and display success message
    app.SpecpHButton.BackgroundColor = [0, 1, 0]; % Green
    app.speclabel.Value = {'Data loaded successfully',file,...
        ['spec pH: ', num2str(app.spec_pH)],...
        ['spec temperature used: ', num2str(app.spec_Temp_used)]};
catch ME
    % If there's an error, set button to red and display error message
    app.SpecpHButton.BackgroundColor = [1, 0, 0]; % Red
    app.speclabel.Value = sprintf('Error loading data: %s', ME.message);
end