%% Lecture fichier btl issus de Seasoft 
% Picheral 2015/03

function [data]=btl2mat_outpace_nmea(btl_dir,btl_file)

disp('---------------------------------------------------');
file = btl_file;
ind = findstr(file,'.');
filename = file(1:ind-1);
disp(filename);

fid = fopen([btl_dir,'\',file],'rt');
%
% Read the header.
% Start reading header lines of .CNV file,
% Stop at line that starts with '*END*'
%
% Pull out NMEA lat & lon along the way and look
% at the '# name' fields to see how many variables we have.
%
data_tot = [];
data = [];

while feof(fid)==0;
    str=fgetl(fid);
    %-----------------------------------
    %
    %    Read the NMEA latitude string.  This may vary with CTD setup.
    %
    if (strncmp(str,'* NMEA Lat',10))
        is=findstr(str,'=');
        isub=is+1:length(str);
        dm=sscanf(str(isub),'%f',2);
        if(findstr(str(isub),'N'));
            lat=dm(1)+dm(2)/60;
        else
            lat=-(dm(1)+dm(2)/60);
        end
        %-------------------------------
        %
        %    Read the NMEA longitude string.  This may vary with CTD setup.
        %
    elseif (strncmp(str,'* NMEA Lon',10))
        is=findstr(str,'=');
        isub=is+1:length(str);
        dm=sscanf(str(isub),'%f',2);
        if(findstr(str(isub),'E'));
            lon=dm(1)+dm(2)/60;
        else
            lon=-(dm(1)+dm(2)/60);
        end
        %------------------------
        %
        %    Read the 'System upload time' to get the date.
        %           This may vary with CTD setup.
        %
    elseif (strncmp(str,'* NMEA UTC (Time)',15))
        is=findstr(str,'=');
        %    pick apart date string and reassemble in DATEFORM type 0 form
        datstr=[str(is+6:is+7) '-' str(is+2:is+4) '-' str(is+9:is+12)];
        datstr=[datstr ' ' str(is+14:is+21)];
        %    convert datstr to Julian time, add 5 hours to convert from EST to GMT
        n=datenum(datstr);
        gtime=datevec(n);
        %------------------------------
%% ------------- BOTTLE DATA -----------------------------
   elseif (strncmp(str,'    Bottle',10)) 
        % ------------ NAMES -------------------------------------
        b = str;
        index = 1;
        names = {};
        while ~isempty(b);      
            [names{index} b] = strtok(b,' ');
            index = index+1;
        end
        names = names([1 3:end-1]);
        index = index-3;
        % ------------ DATA -------------------------------------
    elseif (strncmp(str,'     ',5) && ~strncmp(str,'         ',9))   
       
        b = str;
        data_bottle = [];
        [data_val b] = strtok(b,' ');
        data_bottle(1) = str2num(data_val);
        [data_val b] = strtok(b,' ');
        [data_val b] = strtok(b,' ');
        [data_val b] = strtok(b,' ');
        for i = 2:index;
            [data_val b] = strtok(b,' ');
            data_bottle = [data_bottle str2num(data_val)];
        end
        data_tot = [data_tot;data_bottle];
        
    end
end
disp('---------------------------------------------------');
fclose(fid);

data.filename = filename;
data.latitude = lat;
data.longitude = lon;
data.time = gtime;
data.datenum = n;
data.names = names';
data.data = data_tot;
