function [dat,dat_load] = load_dat(cruiseid)
% LOAD_DAT - load information in *.dat files into cruise database
% 
% CTD Calibration toolbox.
%
% INPUT
%	cruiseid	cruise identification name. 
%	
% OUTPUT
%	dat		downcast dat (raw) data from SBE .DAT files
%	dat_load	parsed data from .DAT files
%	
%USAGE -	
%	load_dat(cruiseid)
%	[dat,dat_load]=load_dat(cruiseid)
%
%EXPLANATION -	
%
%SEE ALSO -	
%

%AUTHORS - 	MATLAB code by c.m.duncombe rae
%
%CREATED -	2008/03/21
%
% VERSION:
%	$Revision: 1.4 $
%	$Date: 2008/04/27 17:48:59 $
%	$Id: load_dat.m,v 1.4 2008/04/27 17:48:59 duncombe Exp duncombe $
%
%CHANGELOG -	
%
%

% DEPENDENT ON 
%	READ_SBEDAT
%	PARSE_SBEDAT
%

if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), 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_dat_dir')
        cnv_dir=getpref(group,'cal_dat_dir');
else
	error(['The cal_dat_dir preference was not set for cruise: ' ...
                cruiseid '.  Run register_cruise.m']);
end;

% cruise_dir is the calibration data directory 
if ispref(group,'cal_dat_dir')
	cruise_dir=getpref(group,'cal_dat_dir');
else
	error(['The cal_dat_dir preference was not set for cruise: ' ...
		 cruiseid '.  Run register_cruise.m']);
end;

lc_cnv = dir(fullfile(cnv_dir,'*.dat')); 
uc_cnv = dir(fullfile(cnv_dir,'*.DAT'));
if size(lc_cnv)==size(uc_cnv),
    cnv_names = lc_cnv;
else
    if ~isempty(lc_cnv) & ~isempty(uc_cnv)
	error(['The *.dat 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
	error(['No *.dat files were found in ', cnv_dir]);
    end;
end;

fprintf(1,['Loading the .DAT files\n'])
% The files are there, retrieve the data.
for i=1:length(cnv_names)
    dat(i)=read_sbedat(fullfile(cnv_dir,cnv_names(i).name));
    disp([fullfile(cnv_dir,cnv_names(i).name),' loaded'])
% this is a mess. dat should have the coefficients from the con file already,
% we should not have to go calculating the confilename here.
%
    datfilename=fullfile(cnv_dir,cnv_names(i).name);
    [pathstr,name,ext,versn]=fileparts(datfilename);
    confilename=fullfile(pathstr,[name,'.con',versn]);
        if exist(confilename)~=2,
            confilename=fullfile(pathstr,[name,'.CON',versn]);
            if exist(confilename)~=2,
                disp([confilename ' is not found. ' mfilename ' will ' ...
			'return dat_load(' num2str(i) ') empty.']);
        	dat_load(i).srcfilename=dat(i).srcfilename;
	    end;
        end;
    	if exist(confilename)==2,
		dat_load(i) = parse_sbedat(dat(i),confilename);
	end;
end;

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

for i=1:length(dat),
	dat(i).cruiseid=cruiseid;
	dat_load(i).cruiseid=cruiseid;
end;


return;

