function varargout  = load_btl(cruiseid)
% LOAD_BTL - read and save the SeaBird ASCII .BTL file format
%
% CTD Calibration toolbox.
%
% INPUT
% 	cruiseid	Cruise identification name. 
%
% OUTPUT
%	btl		bottle sample data from SBE .BTL file
%
% 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
% 
% VERSION: 
%	$Revision: 1.9 $
%	$Date: 2008/03/14 17:29:32 $
%	$Id: load_btl.m,v 1.9 2008/03/14 17:29:32 duncombe Exp duncombe $
%
% 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
% 2007/11/27 CMDR
% 	* There is a whole directory structure set up for a cruise, and the
% 	calibration process starts with cd'ing to that directory structure.
% 	Therefore it seems counter productive to have to essentially enter the
% 	cruiseid by hand each time.  Create a cruise_id file, which has the
% 	cruise id in it, and use that. Create a function getcruiseid which
% 	searches for this file, reads it if it exists and otherwise proceeds as
% 	previously done, ie to fail with error.  I attempted to do this by
% 	having the cruise id be determined by group_name function but that
% 	requires too many other changes have to be made. 
%  2008/02/17 CMDR
% 	* comments and consistency
%  2008/02/22 CMDR
%	* string repair (fix previous edit)
%  2008/03/14 CMDR 
%	* add fieldname cruiseid to the btl structure
% 

% Check for correct number of inputs.
% error(nargchk(1,1,nargin))
if nargin==0, cruiseid=getcruiseid; end;

% 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,['Loading the .BTL files\n'])
% The files are there, retrieve the data.
for i=1:length(btl_names)
	btl(i)=read_sbebtl(fullfile(btl_dir,btl_names(i).name));
	btl(i).cruiseid=cruiseid;
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,['Loaded .BTL files from station ',num2str(min(station_list)), ...
	' to ',num2str(max(station_list)) '\n']);

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

return;

