function varargout  = load_bl(cruiseid)
% load_bl: Read  and save the SeaBird ASCII .BL file format
%
% CTD Calibration toolbox.
%
% INPUT: cruiseid: Cruise identification name. 
%
% USAGE: 
% load_bl(cruiseid): read the SeaBird ASCII .BL files and save in
%                     the database file of the cruise.This file will
%                     be named "cruiseid"_db.mat 
%                     
% bl=load_bl(cruiseid) : Additionally, it will save the  .BL 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_bl (load_bl): Removed dependence on directory structure in favor of 
% 			register_cruise
%		* load_bl (load_bl): Fixed some logic errors that missed empty directories.  
%			e.g. size(dir()) doesn't work.
%   31-Mar-2007 M.Baringer
%       * fixed message box being too small (a bit of a kludge)
%


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

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

% bl_dir is the calibration data directory 
if ispref(group,'cal_bl_dir')
	bl_dir=getpref(group,'cal_bl_dir');
else
    errordlg(['The cal_bl_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
    error(['The cal_bl_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 bl files are in the given directory.
% TODO: Make use of the upper case/lower case preference.
lc_bl = dir(fullfile(bl_dir,'*.bl')); 
uc_bl = dir(fullfile(bl_dir,'*.BL'));

if isempty(lc_bl) & isempty(uc_bl)
    errordlg(['No *.bl or *.BL files were found in ', bl_dir]);
	error(['No *.bl or *.BL files were found in ', bl_dir]);
end
if isequal(lc_bl,uc_bl)
    bl_names = lc_bl;
else
    if ~isempty(lc_bl) & ~isempty(uc_bl)
        	errordlg(['The *.bl files in ', bl_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
		error(['The *.bl files in ', bl_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
    elseif isempty(lc_bl) & ~isempty(uc_bl)
		bl_names = uc_bl;
    elseif ~isempty(lc_bl) & isempty(uc_bl)
		bl_names = lc_bl;
    else
        errordlg(['No *.bl or *.BL files were found in ', bl_dir]);
		error(['No *.bl or *.BL files were found in ', bl_dir]);
    end
end

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

% The files are there, retrieve the data.
for i=1:length(bl_names)
	tmp = read_sbebl(fullfile(bl_dir,bl_names(i).name));
	if ~isempty(tmp)
		bl(i)=tmp;
	else
		%bl(i)=
	end
end

% Should never get here but make sure.
if ~exist('bl','var') | isempty(bl)
	error('The bl output structure does not exist or is empty.')
end

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

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

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


