function [sfh,fh]=compare_cond(cruiseid,staa,stab)
% COMPARE_COND -  diagnostics plots to compare conductivity values 
%	between CTD and bottle samples.
%
% CTD Calibration toolbox.
%
% USAGE:
% [sfh,fh]=compare_cond(cruiseid,staa,stab);
%	staa, stab are optional. 
%	sfh, fh are handles to the figures produced. sfh are the multiplot 
%		'final' figures, fh are the 13 or so other figures produced.
%		We probably want to print the sfh figures using 
%		print_compare_cond.m
%
% INPUT
%	cruiseid	cruise identification name.
%	staa,stab 	from station, to station
%	
% OUTPUT
%	cond_match	update combined bottle and ctd salinity information
%	
%	plots to compare conductivity values between CTD and bottle samples
%          
% DESCRIPTION: This function uses the summary conductivity calibration file
% created by  make_cond_sumfile and does a series of diagnostics plots to
% compare conductivity values between CTD and bottle sample to evaluate the
% quality of the data that we are collecting, as well, any potential problems
% with niskin/sample bottles and/or sensors.

% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
%
% 
% VERSION: 
%	$Revision: 1.13 $
%	$Date: 2008/03/14 17:51:34 $
%	$Id: compare_cond.m,v 1.13 2008/03/14 17:51:34 duncombe Exp duncombe $
%
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%		* Initial version.
%  2007/12/12 CMDR
%  Get cruiseid from a file containing the identifier in the data
%  directory.
%	* test for secondary sensors present
% 2007/12/16 CMDR
% 	* put staa and stab in the input variables so that it
%	doesn't keep asking for stations when you're doing testing.
%  2008/01/01 CMDR
% 	* make input lines easier to read (aesthetic adjustment!)
%	* return filehandles from make_plots to the calling function
%	* return filehandles from save_plots to the calling function
%  2008/01/09 CMDR
%	* arrange the order of the output arguments and add comments
%  2008/01/14 CMDR
%	* set more off to avoid endless screeds of output being paused 
%	each 22 lines
%  2008/02/04
%	* dependencies
%  2008/03/11 CMDR
%	* provide defaults if staa or stab isempty.
% 
% DEPENDENCIES
%   * getcruiseid - read the cruise_id from a file
%   * group_name - get the preference group name for a given cruiseid.
%   * make_plots - draws plots.
%   * save_plots - saves plots.
%
close all
%%%%%%%%%%% CMDR read cruiseid if we can
if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), cruiseid=getcruiseid; end;
%%%%%%%%%%%
%%%
% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% cruise_dir is the main calibration data directory 
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
	ld_file=(['load ',db_file,' cond_match']);
	eval(ld_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_sum.m ']);
 return;
 end

%%%%%%%%  CMDR  turn off more if it is on
morestate=get(0,'more');
more off;
%%%%%%%%

disp('****************************************** ')
disp('PRELIMINARY COMPARISONS')
disp('****************************************** ')
disp('The data found in cond_match goes from') 
disp(['the station ',num2str(min(cond_match.stnnbr)),' to the station ', ...
		num2str(max(cond_match.stnnbr)),'.'] ) 
disp('****************************************** ')
%
%%%%%%%%%%%% CMDR
% staa=input('Input first station: ');
% stab=input('Input last station: ');
if ~exist('staa'), staa=input('Input first station: '); end;
if ~exist('stab'), stab=input('Input last station: '); end;
if isempty(staa), staa=min([cond_match.stnnbr]); end;
if isempty(stab), stab=max([cond_match.stnnbr]); end;
%%%%%%%%%%%%

%%%%%%%%
%
disp('You have an option to color code the output selecting a depth')
disp('This depth will be also used in the criteria to quality control the data' )
col_code=input('Do you want select a depth? (0=no 1=yes): ');
while (isempty(col_code)==1 | col_code<0 | col_code>1)
	disp('Please answer 0 (zero) for no or  1(one) for yes')
	col_code=input('Do you want select a depth? (0=no 1=yes): ');
end
if col_code==0
	hcolor=0;
end

if col_code==1
	hcolor=input('Enter the depth for color coding the output: ');
end

quality_flag=input('Do you want to see data after the first qc? (0=no 1=yes): ');

while (isempty(quality_flag)==1 | quality_flag<0 | quality_flag>1)
	disp('Please answer 0 (zero) for no or  1(one) for yes')
	quality_flag=input('Do you want to see data after the first qc? (0=no 1=yes): ');
end
%%%%%%% This looks like it has to do with printing out information
%%%%%%% depending on the while loop above allowing you to to see the data
%%%%%%% after the first qc.
if quality_flag==1
s_index=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab & ...
	cond_match.quality2==2 & cond_match.quality1==2 );
disp(['Number of good data points found: ',num2str(length(s_index))])
aux_gd=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab & cond_match.quality1 ~= 9);
per_gd=(length(s_index)/length(aux_gd))*100;
disp(['This represents ', num2str(per_gd),'% of the data']); 
else
s_index=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab & cond_match.quality1==2);
disp(['Number of good data points found: ',num2str(length(s_index))])
aux_gd=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab);
per_gd=(length(s_index)/length(aux_gd))*100;
disp(['This represents ', num2str(per_gd),'% of the data. ']); 
end

%
 station_num=cond_match.stnnbr(s_index);
 bottle_sal=cond_match.btlsal(s_index);
 ctd_sal_1=cond_match.ctdsal1(s_index);
%%%%%%%%%% CMDR test for second sensors
 %if ~isnan(cond_match.ctdsal2), 
 if isfield(cond_match,'ctdsal2'),
	ctd_sal_2=cond_match.ctdsal2(s_index); 
 else 
	ctd_sal_2=[]; 
 end;
%%%%%%%%%%
 niskin_num=cond_match.niskinbtlpos(s_index);
 sample_bottle_num=cond_match.sampnbr(s_index);
 ctd_pressure=cond_match.ctdprs(s_index);
%
%%%%%%%%%%%%%%%
%Start plotting%
%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%
% Primary Sensor %
%%%%%%%%%%%%%%%%%%%%
[quality,fh1]=make_plots_jay(bottle_sal,ctd_sal_1,station_num,ctd_pressure, ...
	niskin_num,hcolor,'Primary','Salinity','psu');
if quality_flag==0
	cond_match.quality1(s_index)=quality;
end;
%%%%%%%%%%%%%%%%%%%%
% Secondary Sensor %
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% CMDR test for second sensors 
if ~isempty(ctd_sal_2),
	[quality,fh2]=make_plots_jay(bottle_sal,ctd_sal_2,station_num, ...
		ctd_pressure,niskin_num,hcolor, ...
		'Secondary','Salinity','psu');
	if quality_flag==0
		cond_match.quality1(s_index)=quality;
	end;
else 
	fh2=[]; ctd_sal_2=[]; 
end;
%%%%%%%%% CMDR
fh=[fh1,fh2]; % concatenate file handles
%%%%%%%%%%%%%%%%
%Saving figures
if 0,
sfh=save_plots(bottle_sal,ctd_sal_1,ctd_sal_2,station_num,ctd_pressure, ...
		niskin_num,sample_bottle_num,hcolor,'Salinity','psu');
end;
%%%%%%%%%%%
%Save the conductivity summary structure.
%%%
if quality_flag==0
 save(db_file,'cond_match','-append');
end
if nargout==0
	assignin('caller','ans',cond_match);
else
%%%%%%%%% CMDR comment: you define varargout, but you haven't put 
%%%%%%%%%		varargout in your output variable list???
%%%%%%%%%		This needs a bit of work to make it consistent.
%%%%%%%%%		Have to test what kind of output is expected, then
%%%%%%%%%		return either the figure handles or the new cond_match
%%%%%%%%%		For now just return the figure handles, and worry 
%%%%%%%%%		about it later.
	varargout{1}=cond_match;
end;

%%%%%%%%  CMDR  return more to the original state
more(morestate);
%%%%%%%%

return;

