function fh=checking(cruiseid,parameter)
%CHECKING - compares downcast, upcast, calibrated and bottle salt and oxygen
% 
% CTD Calibration toolbox.
%
%USAGE -	
%	fh=checking(cruiseid,parameter)
%	parameter	all, salt, cond, oxy
%	fh 		figure handles
%
%INPUT
%
%OUTPUT
%
%SEE ALSO -	
%

%PROGRAM - 	MATLAB code by c.m.duncombe rae
%
%CREATED 2008/02/20
% 
%VERSION
%	$Revision: 1.6 $
%	$Date: 2008/04/29 23:26:10 $
%	$Id: checking.m,v 1.6 2008/04/29 23:26:10 duncombe Exp duncombe $
%
%CHANGELOG
%  2008/02/22 CMDR
% 	* add parameter conductivity to possible checks
% 	* check for no bottles on station
%	* change default parameter option to 'all'
%  2008/03/29 CMDR
%	* substitute cnv_dlp for cnv_cal if we haven't done the 
%	calibration step yet.
%

if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), cruiseid=getcruiseid; end;
group = group_name(cruiseid);

dosalt=0;
dooxy=0;
docond=0;

if ~exist('parameter'), parameter=[]; end;
if isempty(parameter), parameter='all'; end;

if parameter(1)=='a',
	dosalt=1;
	dooxy=1;
	docond=1;
elseif parameter(1)=='o',
	dooxy=1;
elseif parameter(1)=='c',
	docond=1;
elseif parameter(1)=='s',
	dosalt=1;
end;

if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
	error(['The cal_data_dir preference was not set for cruise: ' ...
		cruiseid '.  Run register_cruise.m']);
end;

% Load data from the cruise database
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
	load(db_file);
else
	error(['The cruise data base file was not found on the path.  ' ...
		db_file]);
end;

n=who('cond_match');
if isempty(n)==1
	error(['cond_match not found. Run make_cond_sumfile.m ']);
	return;
end;

n=who('raw_load');
if isempty(n)==1
	error(['raw_load not found. Run load_raw.m ']);
	return;
end;

dvar='cnv\_cal';
n=who('cnv_cal');
if isempty(n),
	n=who('cnv_dlp');
	if isempty(n),
		error(['cnv_cal not found, cnv_dlp not found. ' ...
			'Run at least deloop.m first.']);
		return;
	else
		disp(['cnv_cal not found, using cnv_dlp instead']);
		cnv_cal=cnv_dlp;
		for i=1:length(cnv_cal), cnv_cal(i).salt=cnv_dlp(i).salpri; end;
		dvar='cnv\_dlp';
	end;
end;

fcnt=0;
legS={ 'raw down', 'raw up', dvar, 'btlsal'};
legO={ 'raw down', 'raw up', dvar, 'btloxy'};
legC={ 'raw down', 'raw up', dvar, 'btlcon1'};

for i=1:length(cnv_cal),

% 
% Salinity
%
if dosalt,
   Ibtl=find(cnv_cal(i).station==cond_match.stnnbr);
   fcnt=fcnt+1;
   fh(fcnt)=figure;
      % assign a curve handle to each plot
      ch(1)=plot(raw_load(i).dn.pressure, raw_load(i).dn.salpri,'r'); 
      hold on;
      ch(2)=plot(raw_load(i).up.pressure, raw_load(i).up.salpri,'m'); 
      ch(3)=plot(cnv_cal(i).pressure,cnv_cal(i).salt,'g');
      if ~isempty(Ibtl),
         ch(4)=plot(cond_match.ctdprs(Ibtl),cond_match.btlsal(Ibtl),'x');
      end;
	title([ cruiseid ' Station ' num2str(cnv_cal(i).station) ]);
	xlabel('Pressure');
	ylabel('Salinity');
      legend(ch,legS,'location','Best');
	zoom on;
end; % if dosalt,

% 
% Oxygen
%
if dooxy,
   Ibtl=find(cnv_cal(i).station==oxy_match.stnnbr);
   fcnt=fcnt+1;
   fh(fcnt)=figure;
      % assign a curve handle to each plot
	if isfield(raw_load,'oxvpri'),
      ch(1)=plot(raw_load(i).dn.pressure, raw_load(i).dn.oxvpri,'r'); 
	elseif isfield(raw_load,'oxcpri'),
      ch(1)=plot(raw_load(i).dn.pressure, raw_load(i).dn.oxcpri,'r'); 
	end;
      hold on;
	if isfield(raw_load,'oxvpri'),
      ch(2)=plot(raw_load(i).up.pressure, raw_load(i).up.oxvpri,'m'); 
	elseif isfield(raw_load,'oxcpri'),
      ch(2)=plot(raw_load(i).up.pressure, raw_load(i).up.oxcpri,'m'); 
	end;
      ch(3)=plot(cnv_cal(i).pressure,cnv_cal(i).oxymll,'g');
      if ~isempty(Ibtl),
         ch(4)=plot(oxy_match.ctdprs(Ibtl),oxy_match.btloxy(Ibtl),'x');
      end;
	title([ cruiseid ' Station ' num2str(cnv_cal(i).station) ]);
	xlabel('Pressure');
	ylabel('Oxygen');
      legend(ch,legO,'location','Best');
	zoom on;
end; % if dooxy,

% 
% Conductivity
%
if docond,
   Ibtl=find(cnv_cal(i).station==cond_match.stnnbr);
   fcnt=fcnt+1;
   fh(fcnt)=figure;
      % assign a curve handle to each plot
      ch(1)=plot(raw_load(i).dn.pressure, raw_load(i).dn.condpri.*10,'r'); 
      hold on;
      ch(2)=plot(raw_load(i).up.pressure, raw_load(i).up.condpri.*10,'m'); 
      ch(3)=plot(cnv_cal(i).pressure,cnv_cal(i).cond,'g');
      if ~isempty(Ibtl),
         ch(4)=plot(cond_match.ctdprs(Ibtl),cond_match.btlcon1(Ibtl),'x');
      end;
	title([ cruiseid ' Station ' num2str(cnv_cal(i).station) ]);
	xlabel('Pressure');
	ylabel('Conductivity');
      legend(ch,legC,'location','Best');
	zoom on;
end; % if docond,

end; % for i=1:length(cnv_cal),

return;

