function varargout  = load_new_btl(cruiseid)
% load_btl : Read  and save the SeaBird ASCII .BTL file format
%
% CTD Calibration toolbox.
%
% INPUT: cruiseid: Cruise identification name. 
%
% USAGE: 
% load_btl(cruiseid): read the SeaBird ASCII .BTL files and save in
%                     the database file of the cruise.This file will
%                     be named "cruiseid"_db.mat 
%                     
% btl=load_btl(cruiseid) : Additionally, it will save the  .BTL files 
%                          in the workspace
%
% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
%
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%		* Initial version.
%   01-Oct-2004  Derrick Snowden 
%		* load_btl (load_btl): Removed dependence on directory structure in favor of 
% 			register_cruise
%   31-Mar-2007 M.Baringer
%       * fixed message box being too small (a bit of a kludge)
%   Feb 2012 M. Baringer
%       * reads new bottle files
%       * got rid of message box
%   
%
% Original program returned a variable structure that looks like this:
%  btl
%    station
%    cast
%    lat
%    lon
%    gtime
%    sensors
%    tcor
%    pcor
%    names
%    data
%
% data and names are in the following order/columns:
%    'bottle_ros_position'
%    'date'
%    'pot_temp_pri'
%    'pot_temp_sec'
%    'sal_pri_pri'
%    'sal_sec_sec'
%    'sbedo2_mll_pri'
%    'sbedo2_mll_sec'
%    'scan'
%    'time_sec'
%    'depth_sw'
%    'pressure'
%    't90_pri'
%    't90_sec'
%    'con_pri'
%    'con_sec'
%    'sbeox_volt_pri'
%    'sbeox_volt_sec'
%    'altimeter'
%


% Check for correct number of inputs.
error(nargchk(1,1,nargin))

% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% btl_dir is the calibration data directory 
if ispref(group,'cal_btl_dir')
	btl_dir=getpref(group,'cal_btl_dir');
else
    errordlg(['The cal_btl_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
	error(['The cal_btl_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end

% cruise_dir is the calibration data directory 
if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
    errordlg(['The cal_data_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
	error(['The cal_data_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end

% Check that the btl files are in the given directory.
% TODO: Make use of the upper case/lower case preference.
lc_btl = dir(fullfile(btl_dir,'*.btl')); 
uc_btl = dir(fullfile(btl_dir,'*.BTL'));
if size(lc_btl)==size(uc_btl)
    btl_names = lc_btl;
else
    if ~isempty(lc_btl) & ~isempty(uc_btl)
        errordlg(['The *.btl files in ', btl_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
		error(['The *.btl files in ', btl_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
    elseif isempty(lc_btl) & ~isempty(uc_btl)
		btl_names = uc_btl;
    elseif ~isempty(lc_btl) & isempty(uc_btl)
		btl_names = lc_btl;
    else
        errordlg(['No *.btl files were found in ', btl_dir]);
		error(['No *.btl files were found in ', btl_dir]);
    end
end

fprintf(1,['\n Loading the *.BTL files\n \n'])

% The files are there, retrieve the data.
for II = 1 : length(btl_names)
%	btl(II)=read_sbebtl(fullfile(btl_dir,btl_names(i).name));
	btl(II)=sbebtl_rd2(fullfile(btl_dir,btl_names(II).name), 'NMEA');
    temp_sta = str2num(btl_names(II).name(findstr('_',btl_names(II).name)+1:findstr('.',btl_names(II).name)-1));
    if exist('temp_sta')
        btl(II).station = temp_sta;
    else
        btl(II).station = 999.
    end
end

% Open the output file
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
	save(db_file,'btl','-append');
else
	save(db_file,'btl');
end

station_list=unique(vertcat(btl.station));
fprintf(1,['\n Loaded *.BTL files from station ',num2str(min(station_list)),' to ',num2str(max(station_list)) '\n \n'])

if nargout==0
	assignin('caller','ans',btl);
    return
else
    varargout{1}=btl;
    return
end
%

