function [sfh,fh]=compare_oxy(cruiseid,staa,stab)
% COMPARE_OXY - plots to compare CTD and sample oxygen values
%
% CTD Calibration toolbox.
%
% USAGE:
% [sfh,fh]=compare_oxy(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_oxy.m
%
% INPUT
%	cruiseid	cruise identification name.
%   
% OUTPUT
%	oxy_match	update combined bottle and ctd oxygen information
%	
%	plots to compare dissolved oxygen values of CTD and bottle samples 
%          
% DESCRIPTION: This function uses the summary dissolved oxygen calibration file created by  
% make_oxy_sum and does a series of diagnostics plots to compare CTD and
% bottle samples dissolved oxygen values 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.15 $
%	$Date: 2008/03/11 20:44:10 $
%	$Id: compare_oxy.m,v 1.15 2008/03/11 20:44:10 duncombe Exp duncombe $
%
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%	* Initial version.
%  2007/12/17 - CMDR
%	* read cruise id from the file in the current directory
%	* test for secondary sensors present
% 	* 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
%	* ctd_oxy_1 carries NaN baggage introduced by an interp1 step in
%	make_oxy_sum_file.m. Toss these NaNs.
%  2008/01/29 CMDR
%	* set more off to avoid endless screeds of output being paused 
%	each 22 lines
%  2008/02/18 CMDR
%	* comments and style
%  2008/03/11 CMDR
%	* provide default if staa or stab are empty
%	
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,' oxy_match']);
eval(ld_file)
else
	error(['The cruise data base file was not found on the path.  ' ...
		db_file]);
end
n=who('oxy_match');
 if isempty(n)==1
 error(['oxy_match not found. Run make_oxy_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 oxy_match goes from') 
disp(['the station ',num2str(min(oxy_match.stnnbr)),' to the station ', ...
		num2str(max(oxy_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([oxy_match.stnnbr]); end;
if isempty(stab), stab=max([oxy_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
%%%%%%%
if quality_flag==1
  	s_index=find(	oxy_match.stnnbr >= staa &	 ...
			oxy_match.stnnbr <= stab &	 ...
			oxy_match.quality1 == 2  &	 ...
			oxy_match.quality2 == 2           );
	disp(['Number of good data points found: ',num2str(length(s_index))])
	aux_gd=find( oxy_match.stnnbr>= staa & oxy_match.stnnbr  <= stab);
	per_gd=(length(s_index)/length(aux_gd))*100;
	disp(['This represents ', num2str(per_gd),'% of the data']); 
else
 s_index=find( oxy_match.stnnbr>= staa & oxy_match.stnnbr  <= stab & oxy_match.quality1==2);
 disp(['Number of good data points found: ',num2str(length(s_index))])
 aux_gd=find( oxy_match.stnnbr>= staa & oxy_match.stnnbr  <= stab);
 per_gd=(length(s_index)/length(aux_gd))*100;
 disp(['This represents ', num2str(per_gd),'% of the data']); 
end

%
%%%%%%%%%% CMDR
%% ctd_oxy_1 carries NaN baggage introduced by an interp1 step in
%% make_oxy_sum_file.m. Toss these NaNs.
%% 
II=find(isnan(oxy_match.ctdoxy1(s_index))); s_index(II)=[];
%
 station_num=oxy_match.stnnbr(s_index);
 %disp('change depending on if you convert')
 %pause
 bottle_oxy=oxy_match.btloxy(s_index);
 %ctd_oxy_1=oxy_match.ctdoxy1(s_index);
 ctd_oxy_1=ox_units(oxy_match.ctdoxy1(s_index),oxy_match.ctdsal1(s_index), ...
    sw_ptmp(oxy_match.ctdsal1(s_index),oxy_match.ctdtmp1(s_index),oxy_match.ctdprs(s_index),0),'mll2mmk');
%%%%%%%%%% CMDR test for sec sensors
 if ~isnan(oxy_match.ctdoxy2), 
     %ctd_oxy_2=oxy_match.ctdoxy2(s_index);
     ctd_oxy_2=ox_units(oxy_match.ctdoxy2(s_index),oxy_match.ctdsal2(s_index), ...
    sw_ptmp(oxy_match.ctdsal2(s_index),oxy_match.ctdtmp2(s_index),oxy_match.ctdprs(s_index),0),'mll2mmk');
 end;
 niskin_num=oxy_match.niskinbtlpos(s_index);
 sample_bottle_num=oxy_match.sampnbr(s_index);
 ctd_pressure=oxy_match.ctdprs(s_index);
%
%%%%%%%%%%%%%%%%%
% Start Plotting %
%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%
% Primary Sensor %
%%%%%%%%%%%%%%%%%%
[quality,fh1]=make_plots_jay(bottle_oxy,ctd_oxy_1,station_num,ctd_pressure, ...
	niskin_num,hcolor,'Primary','Oxygen','{\mu}mol/kg');
if quality_flag==0
oxy_match.quality1(s_index)=quality;
end
%%%%%%%%%%%%%%%%%%%%
% Secondary Sensor %
%%%%%%%%%%%%%%%%%%%%
%%%%%%%% CMDR test for second sensor
if exist('ctd_oxy_2'),
	[quality,fh2]=make_plots_jay(bottle_oxy,ctd_oxy_2,station_num, ...
		ctd_pressure,niskin_num,hcolor, ...
		'Secondary','Oxygen','{\mu}mol/kg');
	if quality_flag==0
		oxy_match.quality2(s_index)=quality;
	end
else 
	fh2=[]; ctd_oxy_2=[];
end;
%%%%%%%%% CMDR
fh=[fh1,fh2]; % concatenate file handles
%%%%%%%%%%%%%%%%
%Saving figures
if 0,
sfh=save_plots(bottle_oxy,ctd_oxy_1,ctd_oxy_2,station_num,ctd_pressure, ...
	niskin_num,sample_bottle_num,hcolor,'Oxygen','{\mu}mol/kg');
end;
%%%%%%%%%%%
%Save the oxygen summary structure.
%%%
if quality_flag==0
 save(db_file,'oxy_match','-append');
end

if nargout==0
	assignin('caller','ans',oxy_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 oxy_match.
%%%%%%%%%		For now just return the figure handles, and worry 
%%%%%%%%%		about it later.
	varargout{1}=oxy_match;
end;

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

return;
 
