function [ctd,fluor]=readBottleFile(btlFile)
% btlFile = 'tn376-002-01.btl';
cast = btlFile(7:9);
bottleFolder = 'z:/ctd_processed/tn376_sts_1_25_with_corrected_1_3_4/bottle_files/';
chlFolder = 'z:/balch lab/chl data/';
chlFile = ['tn376_chl_stn' cast '_data-figs.xls'];

chlPart1 = xlsread([chlFolder chlFile],'entry');
chlPart2 = xlsread([chlFolder chlFile],'results');
niskin = chlPart1(:,4);
chlDeep = chlPart2(:,18);
chlSurf = chlPart2(:,21);

fid = fopen([bottleFolder btlFile],'rt');
foundData = 0;
arrayLine = 0;
while ~feof(fid)
    line = fgetl(fid);
    if line(1) == ' '
        if foundData
%             disp(line)
            values = strsplit(strtrim(line));
            values(end) = [];
            year = values{4};
            month = values{2};
            day = values{3};
            line = fgetl(fid);   % read line with time
            times = strsplit(strtrim(line));
            time = times{1};
            firetime = [day '-' month '-' year ' ' time];
            firetime = num2str(datenum(firetime));
            values{2} = firetime;
            values(3:4) = [];
            values = str2double(values);
            fgetl(fid);         % skip a line
            fgetl(fid);         % skip another line
            whichNiskin = (niskin == values(1));
            switch  sum(whichNiskin)
                case 1
                    values(size(dataArray,2)) = chlDeep(whichNiskin);
                case 3
                    values(size(dataArray,2)) = chlSurf(9);
            end
            arrayLine = arrayLine + 1;
            for ix = 1:numel(values)
                dataArray(arrayLine,ix) = values(ix); %#ok<*AGROW>
            end
        else
            foundData = 1;
%             disp(line)
            fields = strsplit(strtrim(line));
            for ix = 1:numel(fields)
                thisField = matlab.lang.makeValidName(fields{ix});
                s.(thisField) = 0; %#ok<*STRNU>
            end
            fgetl(fid);
            dataArray = nan(24,numel(fields)+1);
        end
    end
end

fluor = dataArray(:,33);    %y
ctd = dataArray(:,26);      %x

% linear regression

x = [ones(size(ctd)) ctd];
y = fluor;

[coeff,~,~,~,stats] = regress(y,x);
fitline = ['y = ' num2str(coeff(2)) ' * CTDFL + ' num2str(coeff(1)) '; r^2 = ' num2str(stats(1))];
disp (fitline)
lincoeff = coeff;
% log xform regression

x = [ones(size(ctd)) log(ctd)];
y = fluor;

[coeff,~,~,~,stats] = regress(y,x);
fitline = ['y = ' num2str(coeff(2)) ' * ln(CTDFL) + ' num2str(coeff(1)) '; r^2 = ' num2str(stats(1))];
disp(fitline)
logcoeff = coeff;

figure
plot(ctd,fluor,'k*')
hold on

xhat = linspace(min(ctd),max(ctd));

yhatlin = lincoeff(2).*xhat + lincoeff(1);
plot(xhat,yhatlin,'r');

yhatlog = logcoeff(2).*log(xhat) + logcoeff(1);
plot(xhat,yhatlog,'g');

xlims = xlim;
xlims(1) = min(xlims(1),0);
xlim(xlims);
title (btlFile)
xlabel ('CTD Fluorometer')
ylabel ('Chl.a')
hold off

fclose(fid);
