function [btl_struct]=read_sbebtl(btl_file,varargin);
% READ_SBEBTL - read SeaBird ASCII .BTL files
%
% Usage:   [btl_struct]=read_sbebtl(btl_file);
%
% Input:  
% btl_file = path/and/name of .BTL file  
%		(e.g. 'cast002.btl', '/data/cruises/oc365/seabird/cast002.btl')
% time_offset = optional offset to be added to the System UpLoad time
%		if the data is logged in a time zone other than UTC.  If it is 
%		not passed in the calling syntax, it is assumed to be zero.
%          
% Output:
% btl_struct with fields:
% lon = longitude in decimal degrees, West negative
% lat = latitude in decimal degrees, North positive
% gtime = Gregorian time vector in UTC
% data = matrix containing all the columns of data in the .BTL file
% names = cell of string matrix containing the names of the columns
% sensors = string matrix containing the names of the sensors
%
%  NOTE: The format of lon,lat and time in the header of the .BTL
%        file may vary with CTD setup.  
%	For example,  .BTL files collected on 
%        the R/V Oceanus, the lat, lon & time info look like this:
%
%    * System UpLoad Time = Mar 30 1998 18:48:42
%    * NMEA Latitude = 42 32.15 N
%    * NMEA Longitude = 069 28.69 W
%    * NMEA UTC (Time) = 23:50:36
%
%  Modify the lat,lon and date string handling if your .BTL files are 
%  different. Also, There are several possibilities for the way time is 
%  stored.  Modify the time and gtime calculations based on whether or not 
%  the data as logged in local time or gmt/utc. 
%
%  I do not store anything about the standard deviations.  If you feel like 
%  tackling that I'd love to see what you come up with.
%
% BUGS: 
%  1. There is a problem with the interpretation of time.  Currently the System
%  UpLoad date is used and the NMEA UTC time, if it exists.  If the NMEA time
%  is greater than the time zone difference between system upload time and utc,
%  then the date may be wrong.
%
%  2. I have by no means encountered every possible variation in the way rossum
%  stores data.  If something is wrong with the output check first that the
%  variables stored in your btl file are all accounted for below.  I use only
%  three cruises on three different ships as templates for creating this
%  program.  
%
%  3. Does not account for the fact that a quantity can be output in several
%  different units.  i.e. if conductivity is output in both S/m and mS/cm then
%  the data in the output  for conductivity will be the last conductivity
%  colummn encountered.:
%

%  AUTHOR: 
% 18 Oct 2001 Derrick Snowden (Derrick.Snowden@noaa.gov)
%    based on cnv2mat by Rich Signell.  
%  4-8-98  Rich Signell (rsignell@usgs.gov)  
%     incorporates ideas from code by Derek Fong & Peter Brickley
%
% 
% VERSION: 
%	$Revision: 1.7 $
%	$Date: 2008/03/24 18:25:41 $
%
% CHANGELOG: 
% 2007/12/20 - CMDR
%	* Fixed the code removing redundant/empty name and data 
% 	columns 
%  2008/03/14 CMDR
%	* add fieldname cruiseid to the btl structure
%  2008/03/24 CMDR
%	* write out a message for bad or missing position information
%	* test for broken NMEA time string

% TODO: Handle units better.  The variables are renamed and the units 
% information is lost. 

if nargin == 2
	time_offset = varargin{1};
else
	time_offset = 0;
end

% Open the .btl file as read-only text
%
fid=fopen(btl_file,'rt');
if fid < 0 
    disp(['WARNING: read_sbebtl could not open ',btl_file]);
    btl=[];
    return
end
% 
% Read the header.
% Start reading header lines of .BTL 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.
%
str='*START*';

%% set some defaults
lat=[]; lon=[];



% This may change in different configurations.  I suppose it depends on your 
% shipboard processing scripts/procedures.  look through your files for 
% something that denotes the end of the header but not the line containing
% the names ofthe columns.
% while (~strncmp(str,'*END*',5));
choose_nmea = 0;
% We assume that each line of the header information is preceded by a * or a #
while (strncmp(str,'#',1) | strncmp(str,'*',1));
    str=remove233(fgetl(fid)); 
   % 
   %A16S
   %Reading Station number and cast number
   % ----------------------------------------------------
    % if (strncmp(str,'* FileName',10))
    %   is=findstr(str,'.');
    %   isub=is-4:1:is-2;
    %   sta=sscanf(str(isub),'%d',3);
    %   isub2=is-1;
    %   cast=sscanf(str(isub2),'%d',1);  
    % ----------------------------------------------------
    %%AB0505
    %   if (strncmp(str,'* FileName',10))
    %   is=findstr(str,'.');
    %   isub=is-3:1:is-1;
    %   sta=sscanf(str(isub),'%d',3);
    %   cast=1;  
    % ----------------------------------------------------
      %%AB0206
       if (strncmp(str,'* FileName',10))
       is=findstr(str,'.');
       isub=is-2:1:is-1;
       sta=sscanf(str(isub),'%d',2);
       cast=1;  
    % ----------------------------------------------------
    %-----------------------------------
    %
    %    Read the NMEA latitude string.  This may vary with CTD setup.
    %
    %if (strncmp(str,'* NMEA Lat',10))
   elseif (strncmp(str,'* NMEA Lat',10))
        choose_nmea = 1;
        is=findstr(str,'=');
        isub=is+1:length(str);
        dm=sscanf(str(isub),'%f',2);
        if(findstr(str(isub),'S'));
            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))
        choose_nmea = 1;
        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 NON NMEA latitude string.  This may vary with CTD setup.
        %
    elseif (strncmp(str,'** Lat',6))
        if choose_nmea == 0
            is=findstr(str,':');
            isub=is+1:length(str);
            dm=sscanf(str(isub),'%f',2);
            if(findstr(str(isub),'S'));
                if length(dm) == 2
                    lat=-(dm(1)+dm(2)/60);
                else
                    lat = -(dm(1)); % assume they have used decimal degrees
                end
            else 
                if length(dm) == 2
                    lat=(dm(1)+dm(2)/60);
                else
                    lat = (dm(1)); % assume they have used decimal degrees
                end
                
            end
        end
        %-------------------------------
        %
        %    Read the NON NMEA longitude string.  This may vary with CTD setup.
        %
    elseif (strncmp(str,'** Lon',6))
        if choose_nmea == 0
            is=findstr(str,':');
            isub=is+1:length(str);
            dm=sscanf(str(isub),'%f',2);
            if(findstr(str(isub),'E'));
                 if length(dm) == 2
                    lon =dm(1)+dm(2)/60;
                else
                    lon = dm(1); % assume they have used decimal degrees
                end
            else 
                if length(dm) == 2
                    lon=-(dm(1)+dm(2)/60);
                else
                    lon = -(dm(1)); % assume they have used decimal degrees
                end
                
           end
        end
        %------------------------
        %
        %    Read the 'System upload time' to get the date.
        %           This may vary with CTD setup.
        %
        %
    elseif (strncmp(str,'* System UpLoad',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 time_offset if System time is logged in local time.
        n=datenum(datstr)+time_offset/24;
        gtime=datevec(n);
        %----------------------------
        %
        %    Read the NMEA TIME string.  This may vary with CTD setup.
        %
        %      replace the System upload time with the NMEA time
    elseif (strncmp(str,'* NMEA UTC',10))
        is=findstr(str,':');
	if ~isempty(is),
        	isub=is(1)-2:length(str);
        	gtime([4:6])=sscanf(str(isub),'%2d:%2d:%2d');
	end;
        %------------------------------
        %
        %    Read the variable names & units into a cell array
        %
    elseif (strncmp(str,'# name',6))  
        var=sscanf(str(7:10),'%d',1);
        var=var+1;  % .BTL file counts from 0, Matlab counts from 1
        %      stuff variable names into cell array
        names{var}=str;
        %------------------------------
        %
        %    Read the sensor names into a cell array
        %
    elseif (strncmp(str,'# sensor',8))  
        sens=sscanf(str(10:11),'%d',1);
        sens=sens+1;  % .BTL file counts from 0, Matlab counts from 1
        %      stuff sensor names into cell array
        sensors{sens}=str;
        %
        %  pick up bad flag value
    elseif (strncmp(str,'# bad_flag',10))  
        isub=13:length(str);
        bad_flag=sscanf(str(isub),'%g',1);
    end
end
%==============================================
%
%  Done reading header.  Now read the data!
%
%nvars=var;  %number of variables

%==============================================
%
%  Start off with the column headings. which should occupy two lines.
%
%col_headings = remove233(fgetl(fid));
col_headings = str;
% CMDR note: break up string into header labels
column_names1 = strread(col_headings,'%s','delimiter',' ');
col_headings = remove233(fgetl(fid));
column_names2 = strread(col_headings,'%s','delimiter',' ');
%
% warn the user if there is not a Nbf column
Nbf_flag=strmatch('Nbf',column_names1)|strmatch('Nbf',column_names2);
%
%if ~strmatch('Nbf',column_names1) | ~strmatch('Nbf',column_names2)
%    if isempty(Nbf_flag)
%    msg = sprintf('\n');
%    msg = [msg sprintf([' WARNING read_sbebtl: There is no Nbf column in file:  ',btl_file,' \n'])];
%    msg = [msg sprintf(['  Rerun rossum and configure to print number of bottles fired.\n'])];
%    msg = [msg sprintf(['  Trying to continue anyway...\n'])];
%    msg = [msg sprintf('\n')];
%    disp(msg);
%end
%==============
% Going to need to do some looping but I don't know exactly how yet.
% Maybe mark the spot here then count lines and come back here?
%
count = 1;
while ~feof(fid)
    str = remove233(fgetl(fid));
    avgcell{count} = strread(str,'%s' ,'delimiter',' ');
    % remove the string '(avg)'
    tmp = avgcell{count};
    tmp{end} = [];
    avgcell{count} = tmp;
    str = remove233(fgetl(fid));
    stdcell{count} = strread(str,'%s' ,'delimiter',' ');
    % remove the string '(avg)'
    tmp = stdcell{count};
    tmp{end} = [];
    stdcell{count} = tmp;
    count = count+1;
end

%  I'm going to exclude the std information for now DPS 19Oct2001
% Assume that the date string will occupy 3 columns
% Try to find the indices of the major known fields
offset = 0;
column_count = 0;
for i = 1:length(column_names1)
    if strncmp('Bottle',column_names1{i},6); 
        column_count = column_count + 1;
        btl_pos_idx = column_count + offset; 
    end;
    if strncmp('Date',column_names1{i},4); 
        column_count = column_count + 1;
        date_idx = column_count; 
        offset = 2;
    end;
    if strncmp('Density00',column_names1{i},9); 
        column_count = column_count + 1;
        pri_pri_dens_idx = column_count + offset; 
    end;
    if strncmp('Density11',column_names1{i},9); 
        column_count = column_count + 1;
        sec_sec_dens_idx = column_count + offset; 
    end;
    if strncmp('Density01',column_names1{i},9); 
        column_count = column_count + 1;
        pri_sec_dens_idx = column_count + offset; 
    end;
    if strncmp('Density10',column_names1{i},9); 
        column_count = column_count + 1;
        sec_pri_dens_idx = column_count + offset; 
    end;
    if strncmp('Sigma-',column_names1{i},6); 
        %Parse the sigma into individual ascii representation
        %in order to compare non standard characters.
        % integer code 233 represents the e' in the column Skip it.
        sig = column_names1{i};
        if strncmp('00',sig(end-1:end),2)
            column_count = column_count + 1;
            pri_pri_sig_idx = column_count + offset;
        elseif strncmp('11',sig(end-1:end),2)
            column_count = column_count + 1;
            sec_sec_sig_idx = column_count + offset;
        elseif strncmp('10',sig(end-1:end),2)
            column_count = column_count + 1;
            sec_pri_sig_idx = column_count + offset;
        elseif strncmp('01',sig(end-1:end),2)
            column_count = column_count + 1;
            pri_sec_sig_idx = column_count + offset; 
        end
    end;
    
    if strncmp('pr',lower(column_names1{i}),2); 
        column_count = column_count + 1;
        pr_idx = column_count + offset; 
    end;
    if strncmp('T068',column_names1{i},4); 
        column_count = column_count + 1;
        pri_t68_idx = column_count + offset; 
    end;
    if strncmp('T168',column_names1{i},4); 
        column_count = column_count + 1;
        sec_t68_idx = column_count + offset; 
    end;
    if strncmp('T090',column_names1{i},4); 
        column_count = column_count + 1;
        pri_t90_idx = column_count + offset; 
    end;
    if strncmp('T190',column_names1{i},4); 
        column_count = column_count + 1;
        sec_t90_idx = column_count + offset; 
    end;
    if strncmp('Sal00',column_names1{i},5); 
        column_count = column_count + 1;
        pri_pri_sal_idx = column_count + offset; 
    end;
    if strncmp('Sal11',column_names1{i},5); 
        column_count = column_count + 1;
        sec_sec_sal_idx = column_count + offset; 
    end;
    if strncmp('Sal01',column_names1{i},5); 
        column_count = column_count + 1;
        pri_sec_sal_idx = column_count + offset; 
    end;
    if strncmp('Sal10',column_names1{i},5); 
        column_count = column_count + 1;
        sec_pri_sal_idx = column_count + offset; 
    end;
    if strncmp('C0S/m',column_names1{i},5); 
        column_count = column_count + 1;
        pri_con_idx = column_count + offset; 
    end;
    if strncmp('C1S/m',column_names1{i},5); 
        column_count = column_count + 1;
        sec_con_idx = column_count + offset; 
    end;
    if strncmp('C0mS/cm',column_names1{i},5); 
        column_count = column_count + 1;
        pri_con_idx = column_count + offset; 
    end;
    if strncmp('C1mS/cm',column_names1{i},5); 
        column_count = column_count + 1;
        sec_con_idx = column_count + offset; 
    end;
    if strncmp('OxC',column_names1{i},3); 
        column_count = column_count + 1;
        pri_oxc_idx = column_count + offset; 
    end;
    if strncmp('OxsC',column_names1{i},4); 
        column_count = column_count + 1;
        sec_oxc_idx = column_count + offset; 
    end;
    if strncmp('OxT',column_names1{i},3); 
        column_count = column_count + 1;
        pri_oxt_idx = column_count + offset; 
    end;
    if strncmp('OxsT',column_names1{i},4); 
        column_count = column_count + 1;
        sec_oxt_idx = column_count + offset; 
    end;
    if strncmp('OxMm/Kg',column_names1{i},7); 
        column_count = column_count + 1;
        pri_do2_mmkg_idx = column_count + offset; 
    end;
    if strncmp('OxsMm/Kg',column_names1{i},8); 
        column_count = column_count + 1;
        sec_do2_mmkg_idx = column_count + offset; 
    end;
    if strncmp('Sbeox0Mm/Kg',column_names1{i},11); 
        column_count = column_count + 1;
        pri_sbedo2_mmkg_idx = column_count + offset; 
    end;
    if strncmp('Sbeox1Mm/Kg',column_names1{i},11); 
        column_count = column_count + 1;
        sec_sbedo2_mmkg_idx = column_count + offset; 
    end;
    if strncmp('OxdOC/dT',column_names1{i},8); 
        column_count = column_count + 1;
        pri_odocdt_idx = column_count + offset; 
    end;
    if strncmp('OxsdOC/dT',column_names1{i},9); 
        column_count = column_count + 1;
        sec_odocdt_idx = column_count + offset; 
    end;
    if strncmp('Sbeox0ML/L',column_names1{i},10); 
        column_count = column_count + 1;
        pri_sbedo2_mll_idx = column_count + offset; 
    end;
    if strncmp('Sbeox1ML/L',column_names1{i},10); 
        column_count = column_count + 1;
        sec_sbedo2_mll_idx = column_count + offset; 
    end;
    if strncmp('OxML/L',column_names1{i},6); 
        column_count = column_count + 1;
        pri_do2_mll_idx = column_count + offset; 
    end;
    if strncmp('OxsML/L',column_names1{i},7); 
        column_count = column_count + 1;
        sec_do2_mll_idx = column_count + offset; 
    end;
    if strncmp('Nbf',column_names1{i},3); 
        column_count = column_count + 1;
        btl_fired_idx = column_count + offset; 
    end;
 % CAF
       if strncmp('Potemp090C',column_names1{i},10); 
        column_count = column_count + 1;
        pot_temp_pri_idx = column_count + offset; 
       end;
       if strncmp('Potemp190C',column_names1{i},10); 
        column_count = column_count + 1;
        pot_temp_sec_idx = column_count + offset; 
       end;
       if strncmp('Sbeox0PS',column_names1{i},8); 
        column_count = column_count + 1;
        sbeox_pct_sat_pri_idx = column_count + offset; 
       end;
       if strncmp('Sbeox1PS',column_names1{i},8); 
        column_count = column_count + 1;
        sbeox_pct_sat_sec_idx = column_count + offset; 
       end;
       if strncmp('Sbeox0dOC/dT',column_names1{i},12); 
        column_count = column_count + 1;
        pri_sbedOC_dt_idx = column_count + offset; 
       end; 
       if strncmp('Sbeox1dOC/dT',column_names1{i},12); 
        column_count = column_count + 1;
        sec_sbedOC_dt_idx = column_count + offset; 
       end;
       if strncmp('Sbeox0V',column_names1{i},7); 
        column_count = column_count + 1;
        pri_sbeox_volt_idx = column_count + offset; 
       end;
       if strncmp('Sbeox1V',column_names1{i},7); 
        column_count = column_count + 1;
        sec_sbeox_volt_idx = column_count + offset; 
       end;
       if strncmp('Scan',column_names1{i},4); 
        column_count = column_count + 1;
        scan_idx = column_count + offset; 
       end;
       if strncmp('OxPS',column_names1{i},4); 
        column_count = column_count + 1;
        pct_sat_idx = column_count + offset; 
       end;
        if strncmp('OxsPS',column_names1{i},5); 
        column_count = column_count + 1;
        pct_sat_sec_idx = column_count + offset; 
       end;
       % Added on the Cruise A16S
       % case this program
       if strncmp('TimeS',column_names1{i},5); 
        column_count = column_count + 1;
        time_cast_idx = column_count + offset; 
       end;
       if strncmp('V4',column_names1{i},2); 
        column_count = column_count + 1;
        volt_4_idx = column_count + offset; 
       end;
        if strncmp('V5',column_names1{i},2); 
        column_count = column_count + 1;
        volt_5_idx = column_count + offset; 
       end;
        if strncmp('V6',column_names1{i},2); 
        column_count = column_count + 1;
        volt_6_idx = column_count + offset; 
       end;
       % Added on the Cruise AB0505
      %%%%%%%%%%%%%%%
        if strncmp('DepSM',column_names1{i},5); 
        column_count = column_count + 1;
        depth_sw_idx = column_count + offset; 
       end;
       %%%%%%Added on Nov 2005
       if strncmp('AltM',column_names1{i},4); 
        column_count = column_count + 1;
        altimeter_idx = column_count + offset; 
       end;
        if strncmp('TimeJ',column_names1{i},5); 
        column_count = column_count + 1;
        julian_time_idx = column_count + offset; 
       end;
       
             %CAF
    % repeat the above construct as you discover more parameters which rossum outputs.
end
column_count2 = 0;
for i = 1:length(column_names2);
    if strncmp('Position',column_names2{i},8)
        %  This is redundant compared with Bottle above
    elseif strncmp('Time',column_names2{i},4)
        column_count2 = column_count2 + 1;
        time_idx = column_count2;
    end
end

% Begin to assemble the output matrix/structure.
num_rows = length(avgcell);
if ~isempty(date_idx)
    % date is parsed as a 3 column vector but we will combine into one.
    offset = 2;
else
    offset = 0;
end
data = NaN.* ones(num_rows,(column_count-offset));
for i = 1:num_rows
    if exist('btl_pos_idx','var') == 1
        data(i,btl_pos_idx) = str2num(avgcell{i}{btl_pos_idx});
        names{btl_pos_idx} = 'bottle_ros_position';
    end
    if exist('date_idx','var') == 1
        mnth = avgcell{i}{date_idx};
        day = avgcell{i}{date_idx + 1};
        yr = avgcell{i}{date_idx+2};
        if exist('time_idx','var') == 1
            timestr = stdcell{i}{time_idx};
            % dateform=0 == 'dd-mmm-yyyy HH:MM:SS'
            datestr = sprintf('%s',[day,'-',mnth,'-',yr,' ',timestr]);
        else
            datestr = sprintf('%s',[day,'-',mnth,'-',yr]);
        end
        data(i,date_idx) = datenum(datestr);
        names{date_idx} = 'date';
    end;
    if exist('pri_pri_dens_idx','var') == 1
        data(i,pri_pri_dens_idx) = str2num(avgcell{i}{pri_pri_dens_idx});
        names{pri_pri_dens_idx} = 'density_pri_pri';
    end;
    if exist('sec_sec_dens_idx','var') == 1
        data(i,sec_sec_dens_idx) = str2num(avgcell{i}{sec_sec_dens_idx});
        names{sec_sec_dens_idx} = 'density_sec_sec';
    end;
    if exist('pri_sec_dens_idx','var') == 1
        data(i,pri_sec_dens_idx) = str2num(avgcell{i}{pri_sec_dens_idx});
        names{pri_sec_dens_idx} = 'density_pri_sec';
    end;
    if exist('sec_pri_dens_idx','var') == 1
        data(i,sec_pri_dens_idx) = str2num(avgcell{i}{sec_pri_dens_idx});
        names{sec_pri_dens_idx} = 'density_sec_pri';
    end;
    if exist('pri_pri_sig_idx','var') == 1
        data(i,pri_pri_sig_idx) = str2num(avgcell{i}{pri_pri_sig_idx});
        names{pri_pri_sig_idx} = 'sigma_pri_pri';
    end;
    if exist('sec_sec_sig_idx','var') == 1
        data(i,sec_sec_sig_idx) = str2num(avgcell{i}{sec_sec_sig_idx});
        names{sec_sec_sig_idx} = 'sigma_sec_sec';
    end;
    if exist('pri_sec_sig_idx','var') == 1
        data(i,pri_sec_sig_idx) = str2num(avgcell{i}{pri_sec_sig_idx});
        names{pri_sec_sig_idx} = 'sigma_pri_sec';
    end;
    if exist('sec_pri_sig_idx','var') == 1
        data(i,sec_pri_sig_idx) = str2num(avgcell{i}{sec_pri_sig_idx});
        names{sec_pri_sig_idx} = 'sigma_sec_pri';
    end;
    if exist('pr_idx','var') == 1
        data(i,pr_idx) = str2num(avgcell{i}{pr_idx});
        names{pr_idx}='pressure';
    end;
    if exist('pri_t68_idx','var') == 1
        data(i,pri_t68_idx) = str2num(avgcell{i}{pri_t68_idx});
        names{pri_t68_idx} = 't68_pri';
    end;
    if exist('sec_t68_idx','var') == 1
        data(i,sec_t68_idx) = str2num(avgcell{i}{sec_t68_idx});
        names{sec_t68_idx} = 't68_sec';
    end;
    if exist('pri_t90_idx','var') == 1 
        data(i,pri_t90_idx) = str2num(avgcell{i}{pri_t90_idx});
        names{pri_t90_idx} = 't90_pri';
    end;
    if exist('sec_t90_idx','var') == 1
        data(i,sec_t90_idx) = str2num(avgcell{i}{sec_t90_idx});
        names{sec_t90_idx} = 't90_sec' ;
    end;
    if exist('pri_pri_sal_idx','var') == 1
        data(i,pri_pri_sal_idx) = str2num(avgcell{i}{pri_pri_sal_idx});
        names{pri_pri_sal_idx} = 'sal_pri_pri';
    end;
    if exist('sec_sec_sal_idx','var') == 1
        data(i,sec_sec_sal_idx) = str2num(avgcell{i}{sec_sec_sal_idx});
        names{sec_sec_sal_idx} = 'sal_sec_sec';
    end;
    if exist('pri_sec_sal_idx','var') == 1 
        data(i,pri_sec_sal_idx) = str2num(avgcell{i}{pri_sec_sal_idx});
        names{pri_sec_sal_idx} = 'sal_pri_sec';
    end;
    if exist('sec_pri_sal_idx','var') == 1
        data(i,sec_pri_sal_idx) = str2num(avgcell{i}{sec_pri_sal_idx});
        names{sec_pri_sal_idx} = 'sal_sec_pri';        
    end;
    if exist('pri_con_idx','var') == 1
        data(i,pri_con_idx) = str2num(avgcell{i}{pri_con_idx});
        names{pri_con_idx} = 'con_pri';                
    end;
    if exist('sec_con_idx','var') == 1
        data(i,sec_con_idx) = str2num(avgcell{i}{sec_con_idx});
        names{sec_con_idx} = 'con_sec';                
    end;
    if exist('pri_oxc_idx','var') == 1 
        data(i,pri_oxc_idx) = str2num(avgcell{i}{pri_oxc_idx});
        names{pri_oxc_idx} = 'oxc_pri';                
    end;
    if exist('sec_oxc_idx','var') == 1
        data(i,sec_oxc_idx) = str2num(avgcell{i}{sec_oxc_idx});
        names{sec_oxc_idx} = 'oxc_sec';                
    end;
    if exist('pri_oxt_idx','var') == 1
        data(i,pri_oxt_idx) = str2num(avgcell{i}{pri_oxt_idx});
        names{pri_oxt_idx} = 'oxt_pri';                
    end;
    if exist('sec_oxt_idx','var') == 1
        data(i,sec_oxt_idx) = str2num(avgcell{i}{sec_oxt_idx});
        names{sec_oxt_idx} = 'oxt_sec';                
    end;
    if exist('pri_do2_mmkg_idx','var') == 1 
        data(i,pri_do2_mmkg_idx) = str2num(avgcell{i}{pri_do2_mmkg_idx});
        names{pri_do2_mmkg_idx} = 'do2_pri';                
    end;
    if exist('sec_do2_mmkg_idx','var') == 1 
        data(i,sec_do2_mmkg_idx) = str2num(avgcell{i}{sec_do2_mmkg_idx});
        names{sec_do2_mmkg_idx} = 'do2_sec';                
    end;
    if exist('pri_do2_mll_idx','var') == 1
        data(i,pri_do2_mll_idx) = str2num(avgcell{i}{pri_do2_mll_idx});
        names{pri_do2_mll_idx} = 'do2_pri';                       
    end;
    if exist('sec_do2_mll_idx','var') == 1
        data(i,sec_do2_mll_idx) = str2num(avgcell{i}{sec_do2_mll_idx});
        names{sec_do2_mll_idx} = 'do2_sec';                               
    end;
    if exist('btl_fired_idx','var') == 1
        data(i,btl_fired_idx) = str2num(avgcell{i}{btl_fired_idx});
        names{btl_fired_idx} = 'bottle_fired_count';                                       
    end;
    if exist('scan_idx','var') == 1
        data(i,scan_idx) = str2num(avgcell{i}{scan_idx});
        names{scan_idx} = 'scan';                                       
    end;
    if exist('pri_odocdt_idx','var') == 1
        data(i,pri_odocdt_idx) = str2num(avgcell{i}{pri_odocdt_idx});
        names{pri_odocdt_idx} = 'odocdt_pri';                                       
    end;
    if exist('sec_odocdt_idx','var') == 1
        data(i,sec_odocdt_idx) = str2num(avgcell{i}{sec_odocdt_idx});
        names{sec_odocdt_idx} = 'odocdt_sec';                                       
    end;
    if exist('pri_sbedo2_mll_idx','var') == 1
        data(i,pri_sbedo2_mll_idx) = str2num(avgcell{i}{pri_sbedo2_mll_idx});
        names{pri_sbedo2_mll_idx} = 'sbedo2_mll_pri';                       
    end;
    if exist('sec_sbedo2_mll_idx','var') == 1
        data(i,sec_sbedo2_mll_idx) = str2num(avgcell{i}{sec_sbedo2_mll_idx});
        names{sec_sbedo2_mll_idx} = 'sbedo2_mll_sec';                               
    end;
    if exist('pri_sbedo2_mmkg_idx','var') == 1
        data(i,pri_sbedo2_mmkg_idx) = str2num(avgcell{i}{pri_sbedo2_mmkg_idx});
        names{pri_sbedo2_mmkg_idx} = 'sbedo2_mmkg_pri';                       
    end;
    if exist('sec_sbedo2_mmkg_idx','var') == 1
        data(i,sec_sbedo2_mmkg_idx) = str2num(avgcell{i}{sec_sbedo2_mmkg_idx});
        names{sec_sbedo2_mmkg_idx} = 'sbedo2_mmkg_sec';                               
    end;
   %CAF
   if exist('pot_temp_pri_idx','var') == 1
        data(i,pot_temp_pri_idx) = str2num(avgcell{i}{pot_temp_pri_idx});
        names{pot_temp_pri_idx} = 'pot_temp_pri';                               
    end;
    if exist('pot_temp_sec_idx','var') == 1
        data(i,pot_temp_sec_idx) = str2num(avgcell{i}{pot_temp_sec_idx});
        names{pot_temp_sec_idx} = 'pot_temp_sec';                               
    end;
    if exist('sbeox_pct_sat_pri_idx','var') == 1
        data(i,sbeox_pct_sat_pri_idx) = str2num(avgcell{i}{sbeox_pct_sat_pri_idx});
        names{sbeox_pct_sat_pri_idx} = 'sbeox_pct_sat_pri';                               
    end;
    if exist('sbeox_pct_sat_sec_idx','var') == 1
        data(i,sbeox_pct_sat_sec_idx) = str2num(avgcell{i}{sbeox_pct_sat_sec_idx});
        names{sbeox_pct_sat_sec_idx} = 'sbeox_pct_sat_sec';                               
    end;
    if exist('pri_sbedOC_dt_idx','var') == 1
        data(i,pri_sbedOC_dt_idx) = str2num(avgcell{i}{pri_sbedOC_dt_idx});
        names{pri_sbedOC_dt_idx} = 'sbeox_dOCdt_pri';                               
    end;
    if exist('sec_sbedOC_dt_idx','var') == 1
        data(i,sec_sbedOC_dt_idx) = str2num(avgcell{i}{sec_sbedOC_dt_idx});
        names{sec_sbedOC_dt_idx} = 'sbeox_dOCdt_sec';                               
    end;
    if exist('pri_sbeox_volt_idx','var') == 1
        data(i,pri_sbeox_volt_idx) = str2num(avgcell{i}{pri_sbeox_volt_idx});
        names{pri_sbeox_volt_idx} = 'sbeox_volt_pri';                               
    end;
    if exist('sec_sbeox_volt_idx','var') == 1
        data(i,sec_sbeox_volt_idx) = str2num(avgcell{i}{sec_sbeox_volt_idx});
        names{sec_sbeox_volt_idx} = 'sbeox_volt_sec';                               
    end;
     if exist('pct_sat_idx','var') == 1
        data(i,pct_sat_idx) = str2num(avgcell{i}{pct_sat_idx});
        names{pct_sat_idx} = 'pct_sat';                               
    end;
     if exist('pct_sat_sec_idx','var') == 1
        data(i,pct_sat_sec_idx) = str2num(avgcell{i}{pct_sat_sec_idx});
        names{pct_sat_sec_idx} = 'pct_sat_sec';                               
    end;
    % Added on the Cruise A16S
     if exist('time_cast_idx','var') == 1
        data(i,time_cast_idx) = str2num(avgcell{i}{time_cast_idx});
        names{time_cast_idx} = 'time_sec';                               
    end;
      if exist('volt_4_idx','var') == 1
        data(i,volt_4_idx) = str2num(avgcell{i}{volt_4_idx});
        names{volt_4_idx} = 'volt_4';                               
    end;
     if exist('volt_5_idx','var') == 1
        data(i,volt_5_idx) = str2num(avgcell{i}{volt_5_idx});
        names{volt_5_idx} = 'volt_5';                               
    end;
    if exist('volt_6_idx','var') == 1
        data(i,volt_6_idx) = str2num(avgcell{i}{volt_6_idx});
        names{volt_6_idx} = 'volt_6';                               
    end;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Added on the cruise AB0505
    if exist('depth_sw_idx','var') == 1
        data(i,depth_sw_idx) = str2num(avgcell{i}{depth_sw_idx});
        names{depth_sw_idx} = 'depth_sw';                               
    end;
    %
    %%%%%%%Added on Nov 2005
    if exist('altimeter_idx','var') == 1
        data(i,altimeter_idx) = str2num(avgcell{i}{altimeter_idx});
        names{altimeter_idx} = 'altimeter';                               
    end;
     if exist('julian_time_idx','var') == 1
        data(i,julian_time_idx) = str2num(avgcell{i}{julian_time_idx});
        names{julian_time_idx} = 'julian_time';                               
    end;
    %CAF
  % repeat the above construct as you discover more parameters which rossum outputs.
end

%%%%%%%%%% CMDR - the commented out code removed two specific data columns and 
%%%%%%%%%%        then deleted all empty cells from the names array. In effect 
%%%%%%%%%%        if there are additional empty name cells these columns are 
%%%%%%%%%%        not removed from the data array. The code is fixed below. Use
%%%%%%%%%%        cellfun to find empty name cells. Remove them, and the 
%%%%%%%%%%        corresponding columns of data. Ignore the date_idx thing. 
%%%%%%%%%% 
%%%%%%%%%% % Remove the empty cells from the names cell array
%%%%%%%%%% % and the data matrix if the date_idx exists.
%%%%%%%%%% offset = 0;
%%%%%%%%%% if exist('date_idx','var') == 1
%%%%%%%%%%     data(:,[date_idx+1:date_idx+2]) = [];
%%%%%%%%%%     count = 1;
%%%%%%%%%%     for i = 1:(length(names))
%%%%%%%%%%         if ~isempty(names{i})
%%%%%%%%%%             tmp{count} = names{i};
%%%%%%%%%%             count = count + 1;
%%%%%%%%%%         end
%%%%%%%%%%     end
%%%%%%%%%%     names = tmp;
%%%%%%%%%% end
% Remove the empty cells from the names cell array
% and the data matrix 
i=find(cellfun('isempty',names));
names(i)=[];
data(:,i)=[];
%%%%%%%%%% CMDR end of edit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Flag bad values with nan
%
if exist('bad_flag','var') == 1
    ind=find(data==bad_flag);
    data(ind)=data(ind)*nan;
end
btl_struct.cruiseid = [];
btl_struct.station=sta;
btl_struct.cast = cast;
if isempty(lat), 
	error([' No position found. Edit header with "** latitude" entry.']);
else
	btl_struct.lat = lat;
end;
if isempty(lon), 
	error([' No position found. Edit header with "** longitude" entry.']);
else
	btl_struct.lon = lon;
end;
btl_struct.gtime = gtime;
%btl_struct.sensors = char(sensors');%removed for New SBEpocessing header
%2/28/2012
btl_struct.tcor=0; % flag indicating if the correction for the temp. sensors was applied
btl_struct.pcor=0; % flag indicating if the correction for the pressure sensor was applied
% remove the spaces in the names cell
for i = 1:length(names)
    str = names{i};
    idxspc = isspace(str);
    str(idxspc) = [];
    names{i} = str;
end
btl_struct.names = names;
btl_struct.data = data;
disp([btl_file,' loaded'])

return;  % End of read_sbebtl

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function outStr = remove233(inStr);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% remove the ascii character 233 and replace with a space.
% ascii 233 is e-grave ??? 
%
strDbl = double(inStr);
idxStr = find(strDbl==233);
if ~isempty(idxStr)
	strDbl(idxStr) = [];
	outStr = char(strDbl);
else
	outStr = inStr;
end

return;

