function varargout  = load_raw(cruiseid)
% LOAD_RAW - load information in *.cnv files into cruise database
%
% CTD Calibration toolbox.
%
% load_raw, modified directly from load_cnv, reads the data contained in the
% full SeaBird ASCII .CNV files for a given cruise, including both up and down
% casts.  The data is then saved in the cruise database file for later use.
% The cruise database file is stored in the cruise directory as defined in
% register_cruise.m.  See the User's Guide for more information.
%
% INPUT
%	cruiseid	cruise identification name. 
%
% OUTPUT
%	raw		raw data from SBE .CNV files
%	raw_load	parsed data from .CNV files
%
% USAGE: 
%	load_raw(cruiseid) 
%                     
%	[raw,raw_load]=load_raw(cruiseid)
%
% eg. raw = load_raw('ab06')
%
% SEE ALSO:
%		load_cnv, register_cruise, and read_sbecnv			
%

% AUTHORS:
%		Carlos Fonseca 
%      UM/CIMAS
%      Derrick Snowden
%      NOAA/AOML/PhOD
%      Tue May 11 11:23:37 EDT 2004
% 
% VERSION: 
%	$Revision: 1.7 $
%	$Date: 2008/04/28 15:24:23 $
%	$Id: load_raw.m,v 1.7 2008/04/28 15:24:23 duncombe Exp duncombe $
%
% 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 MB
%       * changed some error messages.  
%       * automatically loads downcast profiles which are assumed to start
%       with the letter "d".
%       * automatically convert to cnv "standard" processing format
%   2007/11/28 CMDR
%	* read cruise id from the file in the current directory
%	(see also load_btl and load_bl)
%  2008/02/15 CMDR
%	* some minor commenting and reformatting, no program changes
%  2008/02/17 CMDR
%	* if the _db file did not exist before, the save of cnv_load would
%	 have overwritten the save of cnv on the previous line. Fixed.
%  2008/02/20 CMDR
%	* adapt to read full cast data, by specifying the full-cast-data file
%	and changing variable names cnv and cnv_load to raw and raw_load.
%	* include splitting the data into up and down casts by
%	calling split_cnv_load
%  2008/03/14 CMDR
%	* add cruiseid to the raw and raw_load structures
%  2008/03/27 CMDR
%	* fixed the assumption that the dir command would provide a nicely
%	sorted array of station numbers. Now sort station numbers and loop 
%	through the station number index.
%  2008/04/21
%	* detect use of Beckman oxygen sensor and call the appropriate
%	function to load the data
% 	* do without the popup error boxes. Edit out the comments if they are
% 	required later.
%  2008/04/28
%	* set more off
%

% DEPENDENCIES
%	* GETCRUISEID - read the cruise_id from a file
%	* GROUP_NAME - get the preference group name for a given cruiseid.
%	* PARSE_CNV_DATA_ALL - parse data columns read in from raw CNV files .
%	* READ_SBECNV - reads the SeaBird BINARY or ASCII *.cnv file format
%	* SPLIT_CNV_LOAD - splits data into upcast and downcast

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

% 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
	% I can do without the popup box. If you like the popup then 
	% uncomment the next statement
    	% 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;

% set more off so the listing doesn't jam
morestate=get(0,'more');
more off;

% cruise_dir is the calibration data directory 
if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
    % I can do without the popup box. If you like the popup then 
    % uncomment the next statement
    % 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;

%% CMDR get the filename prefix
% file_prefix is the file name prefix
if ispref(group,'file_name_prefix'),
        file_prefix=getpref(group,'file_name_prefix')
else
    errordlg(['The file_name_prefix preference was not set for cruise: ' ...
                cruiseid '.  Run register_cruise.m']);
    error(['The file_name_prefix 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,[ file_prefix '*.cnv']));
uc_cnv = dir(fullfile(cnv_dir,[ file_prefix '*.CNV']));

if size(lc_cnv)==size(uc_cnv) & ~isempty(lc_cnv),
    cnv_names = lc_cnv;
else
    if ~isempty(lc_cnv) & ~isempty(uc_cnv)
	% do without the popupbox
        % 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
	% I can do without the popup box.
	% uncomment this line if you like a popup box
        % errordlg(['No *.cnv files were found in ', cnv_dir]);
	disp(['Looking for filenames with ' file_prefix ]);
	error(['No *.cnv files were found in ', cnv_dir]);
    end;
end;
fprintf(1,['Loading the .CNV files\n'])
% The files are there, retrieve the data.
for i=1:length(cnv_names),
	disp([ 'Processing ' cnv_names(i).name]);
    raw(i)=read_sbecnv(fullfile(cnv_dir,cnv_names(i).name));
    raw(i).cruiseid=cruiseid;
    disp([fullfile(cnv_dir,cnv_names(i).name),' loaded'])
    if isfield(raw(i),'fullnames'), 
	I=regexpi(raw(i).fullnames,'beckman');
	if any(vertcat(I{:})),
    		raw_load(i) = parse_cnv_data_all_beckman(raw(i));
	else
    		raw_load(i) = parse_cnv_data_all(raw(i));
	end;
    end;
end;

if exist('raw_load')~=1,
	error(['No data loaded from ' num2str(length(cnv_names)) ' files']);
	return;
end;
raw_load=split_cnv_load(raw_load);

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

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

if nargout==0
	assignin('caller','ans',raw);
elseif nargout==1
    varargout{1}=raw;
else
    varargout{1}=raw;
    varargout{2}=raw_load;
end;

more(morestate);

return;

