function varargout  = load_cnv(cruiseid)
% load_cnv : Load the information in the *.cnv files into the cruise database
%
% CTD Calibration toolbox.
%
% load_cnv reads the data contained in the  SeaBird ASCII .CNV files 
% for a given cruise.  The data is then saved in the cruise database file 
% for later use.  The cruise database file is stored in the cruise directory 
% as definied in register_cruise.m.  See the User's Guide for more information.
%
% INPUT: 
%	cruiseid: Cruise identification name. 
%
% OUTPUT: 
%	cnv_struct: vector of structures each containing the data in 
% 			one *.cnv file.
%
% USAGE: 
% load_cnv(cruiseid) 
%                     
% cnv=load_cnv(cruiseid)
%
% E.g. cnv = load_cnv('ab06')
%
% SEE ALSO:
%		register_cruise, and read_sbecnv			
% 
%

% 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_cnv (load_cnv): Removed dependence on directory structure in favor of 
% 			register_cruise
%   23-Mar-2006 M. Baringer
%       * changed some error messages.  
%       * automatically loads downcast profiles which are assumed to start
%       with the letter "d".
%       * automatically convert to cnv "standard" processing format
%   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);

% cnv_dir is the calibration data directory 
if ispref(group,'cal_cnv_dir')
	cnv_dir=getpref(group,'cal_cnv_dir');
else
    errordlg(['The cal_cnv_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
	error(['The cal_cnv_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 cnv files are in the given directory.
% TODO: Make use of the upper case/lower case preference.
lc_cnv = dir(fullfile(cnv_dir,'d*.cnv')); 
uc_cnv = dir(fullfile(cnv_dir,'d*.CNV'));
if size(lc_cnv)==size(uc_cnv)
    cnv_names = lc_cnv;
else
    if ~isempty(lc_cnv) & ~isempty(uc_cnv)
        errordlg(['The *.cnv files in ', cnv_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
		error(['The *.cnv files in ', cnv_dir...
		,' are named in both upper and lower case.  Pick one and rename before running ',mfilename]);
    elseif isempty(lc_cnv) & ~isempty(uc_cnv)
		cnv_names = uc_cnv;
    elseif ~isempty(lc_cnv) & isempty(uc_cnv)
		cnv_names = lc_cnv;
    else
        errordlg(['No *.cnv files were found in ', cnv_dir]);
		error(['No *.cnv files were found in ', cnv_dir]);
    end
end

fprintf(1,['Loading the .CNV files\n'])
sprintf(['Loading the .CNV files\n'])

% The files are there, retrieve the data.
for i=1:length(cnv_names)
	cnv(i)=read_sbecnv(fullfile(cnv_dir,cnv_names(i).name));
    disp([fullfile(cnv_dir,cnv_names(i).name),' loaded'])
    cnv_load(i) = parse_cnv_data_all(cnv(i));
end

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



%% Open the output file
%db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
%if exist(db_file)==2
%	save(db_file,'cnv','-append');
%	save(db_file,'cnv_load','-append');
%else
%	save(db_file,'cnv');
%	save(db_file,'cnv_load');
%end
%
if nargout==0
	assignin('caller','ans',cnv);
    return
elseif nargout==1
    varargout{1}=cnv;
    return
else
    varargout{1}=cnv;
    varargout{2}=cnv_load;
    return
end

return
