function varargout  = load_bl(cruiseid)
% load_btl: 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.
%

% TODO: Make this remember the root project directory somehow.
cruise_dir=cruiseid;
bl_dir=fullfile(cruise_dir,filesep,'raw_data');
if exist(bl_dir)==0
    disp('Folder structure nonexistent')
    disp('Run create_cruise_dir')
    return
end    
sd=['cd ',bl_dir];
eval(sd)
lc_bl = dir('*.bl'); 
uc_bl = dir('*.BL');

if size(lc_bl)==size(uc_bl)
    bl_names = uc_bl;
else
   if ~isempty(lc_bl) & ~isempty(uc_bl)
	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
	error(['No *.bl files were found in ', bottle_dir]);
   end
end
for i=1:length(bl_names)
	bl(i)=bl2mat(bl_names(i).name);
end
cd ..
fname=[cruise_dir,'_db.mat'];
if exist(fname)==2
    savefile=['save ',fname,' bl -append'];
    eval(savefile);
else
   savefile=['save ',fname,' bl -mat'];
   eval(savefile);
end
if nargout==0
    return
else
    varargout{1}=bl;
    return
end
