function btl_data = readbtl(filename)
% READBTL Read Sea-Bird CTD bottle (.btl) file
%   btl_data = readbtl(filename) reads a .btl file and returns a table
%   containing the bottle data with columns for all measurements.
%
%   Input:
%       filename - path to .btl file (string or char)
%
%   Output:
%       btl_data - table with bottle measurements including Bottle number,
%                  salinity, temperature, oxygen, etc., plus Date and Time

    % Read the file
    fid = fopen(filename, 'r');
    if fid == -1
        error('Could not open file: %s', filename);
    end
    fileContent = textscan(fid, '%s', 'Delimiter', '\n', 'WhiteSpace', '');
    fclose(fid);
    lines = fileContent{1};
    
    % Find the header line that starts with "Bottle"
    headerIdx = 0;
    for i = 1:length(lines)
        if contains(lines{i}, 'Bottle') && contains(lines{i}, 'Date') && contains(lines{i}, 'Sal00')
            headerIdx = i;
            break;
        end
    end
    
    if headerIdx == 0
        error('Could not find header line in file: %s', filename);
    end
    
    % Extract header (spans two lines: column names and "Position Time")
    headerLine1 = strtrim(lines{headerIdx});
    headerLine2 = strtrim(lines{headerIdx + 1});
    
    % Find where data starts (skip the Position/Time subheader)
    dataStartIdx = headerIdx + 2;
    
    % Read data lines (look for lines with "(avg)")
    dataLines = {};
    for i = dataStartIdx:length(lines)
        line = strtrim(lines{i});
        if isempty(line)
            continue;
        end
        % Look for lines containing "(avg)" which are data lines
        if contains(line, '(avg)')
            dataLines{end+1} = line;
        end
    end
    
    % Parse the data
    nRows = length(dataLines);
    nCols = 30; % Total number of columns based on header
    data = nan(nRows, nCols);
    dates = cell(nRows, 1);
    times = cell(nRows, 1);
    
    for i = 1:nRows
        % Split by whitespace
        tokens = strsplit(dataLines{i});
        
        % Parse: Bottle, Date (3 tokens), Time, then numeric values
        bottle = str2double(tokens{1});
        dates{i} = strjoin(tokens(2:4), ' '); % "Oct 24 2025" or any date
        times{i} = tokens{5};
        
        % Extract numeric values (skip last token which is "(avg)")
        numericStart = 6;
        numericEnd = length(tokens) - 1;
        numericVals = str2double(tokens(numericStart:numericEnd));
        
        % Store data
        data(i, 1) = bottle;
        data(i, 2:length(numericVals)+1) = numericVals;
    end
    
    % Create column names
    colNames = {'Bottle', 'Sal00', 'Sal11', 'SigmaE00', 'SigmaE11', 'TimeJ', ...
                'PrDM', 'DepSM', 'C0S_m', 'C1S_m', 'T090C', 'T190C', ...
                'Sbeox0PS', 'Sbeox0ML_L', 'Xmiss', 'Bat', 'FlECO_AFL', ...
                'Par', 'Par1', 'Sbeox0V', 'V0', 'V1', 'V2', 'V3', ...
                'V4', 'V5', 'V6', 'V7', 'Scan'};
    
    % Create table
    btl_data = array2table(data(:, 1:length(colNames)), 'VariableNames', colNames);
    btl_data.Date = dates;
    btl_data.Time = times;
    
end