function eval_calibration_oxy(cruiseid)
% COMPARE_OXY -  Diagnostics plots to compare CTD and samples dissolved oxygen values.
%
% CTD Calibration toolbox.
%
% USAGE:
% compare_oxy(cruiseid);
%
%INPUT: cruiseid: Cruise identification name.
%   
% OUTPUT: A series of plots to compare CTD and bottle samples dissolved oxygen values 
%          
% 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
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%		* Initial version.

close all;
% 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']);
db_file = [cruiseid '_db.mat'];
if exist(db_file)==2
ld_file=(['load ',db_file,' oxy_match_cal 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_cal');
 if isempty(n)==1
 error(['oxy_match_cal not found. Run make_cal_sumfile.m ']);
 return;
 end
 
 
disp('****************************************** ')
disp('FINAL COMPARISONS')
disp('****************************************** ')
disp('The data found in oxy_match_cal goes from') 
disp(['the station ',num2str(min(oxy_match_cal.stnnbr)),' to the station ',num2str(max(oxy_match_cal.stnnbr)),'.'] ) 
disp('****************************************** ')
%
staa=input('Input first station: ');
stab=input('Input last station: ');
s_index=find( oxy_match_cal.stnnbr>= staa & oxy_match_cal.stnnbr  <= stab& oxy_match_cal.quality1==2 & oxy_match_cal.quality2==2);
%%%%%%%%
disp(' 1 = use the primary sensor (DEFAULT)' )
disp(' 2 = use the secondary sensor' )
pri_sec=input('Do the calibration was applied to the primary or to the secondary sensor: ');
while (isempty(pri_sec)==1 | pri_sec<=0 | pri_sec>2)
	disp('Please answer 0 (zero) for primary or  1(one) for secondary ')
	pri_sec=input('Do the calibration was applied to the primary or to the secondary sensor ');
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
%
% disp('***************************************************** ')
% YN=input('Do you want to remove any niskin bottle? y/n ','s');
% bad_niskin=1;
% if strcmpi(YN,'y'),
%     while bad_niskin~=0,
%     bad_niskin=input(['Enter the niskin number to remove or ', ...
% 	'0 to skip: ']);  
%     bad_index=find(oxy_match_cal.niskinbtlpos==bad_niskin);
%     oxy_match_cal.quality1(bad_index)=1;
%     end;
% end
%%%%%%%
s_index=find( oxy_match_cal.stnnbr>= staa & oxy_match_cal.stnnbr  <= stab  & oxy_match_cal.quality1==2);% & oxy_match_cal.quality2==2   );
disp(['Number of good data points found: ',num2str(length(s_index))])
aux_gd=find( oxy_match_cal.stnnbr>= staa & oxy_match_cal.stnnbr  <= stab);
per_gd=(length(s_index)/length(aux_gd))*100;
disp(['This represents ', num2str(per_gd),'% of the data']); 
%
 station_num=oxy_match_cal.stnnbr(s_index);
 bottle_oxy=oxy_match_cal.btloxy(s_index);
 %bottle_oxy_ori=oxy_match_cal.btloxy(s_index);
 %bottle_oxy=ox_units(bottle_oxy_ori,oxy_match_cal.ctdsal(s_index),sw_ptmp(oxy_match_cal.ctdsal(s_index),oxy_match_cal.ctdtmp(s_index),oxy_match_cal.ctdprs(s_index),0),'mll2mmk');
 ctd_oxy_1=oxy_match_cal.ctdoxyumolkg(s_index);
 niskin_num=oxy_match_cal.niskinbtlpos(s_index);
 sample_bottle_num=oxy_match_cal.sampnbr(s_index);
 ctd_pressure=oxy_match_cal.ctdprs(s_index);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            Start Ploting                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clean_idx=find(oxy_match_cal.quality1==2 & oxy_match_cal.quality2==2);

if pri_sec==2
bottle_oxy_raw=oxy_match_cal.btloxy(clean_idx);
%ctd_oxy_raw=oxy_match_cal.ctdoxy2(clean_idx);
%bottle_oxy_raw=ox_units(oxy_match_cal.btloxy(clean_idx),oxy_match_cal.ctdsal2(clean_idx),sw_ptmp(oxy_match_cal.ctdsal2(clean_idx),oxy_match_cal.ctdtmp2(clean_idx),oxy_match_cal.ctdprs(clean_idx),0),'mll2mmk');
ctd_oxy_raw=ox_units(oxy_match_cal.ctdoxy(clean_idx),oxy_match_cal.ctdsal(clean_idx),sw_ptmp(oxy_match_cal.ctdsal(clean_idx),oxy_match_cal.ctdtmp(clean_idx),oxy_match_cal.ctdprs(clean_idx),0),'mll2mmk');
else
bottle_oxy_raw=oxy_match_cal.btloxy(clean_idx);
%ctd_oxy_raw=oxy_match_cal.ctdoxy1(clean_idx);    
%bottle_oxy_raw=ox_units(oxy_match_cal.btloxy(clean_idx),oxy_match_cal.ctdsal1(clean_idx),sw_ptmp(oxy_match_cal.ctdsal1(clean_idx),oxy_match_cal.ctdtmp1(clean_idx),oxy_match_cal.ctdprs(clean_idx),0),'mll2mmk');
ctd_oxy_raw=ox_units(oxy_match_cal.ctdoxy(clean_idx),oxy_match_cal.ctdsal(clean_idx),sw_ptmp(oxy_match_cal.ctdsal(clean_idx),oxy_match_cal.ctdtmp(clean_idx),oxy_match_cal.ctdprs(clean_idx),0),'mll2mmk');
end;

if pri_sec==2
[quality]=make_plots_report(bottle_oxy_raw,ctd_oxy_raw,oxy_match_cal.stnnbr(clean_idx),oxy_match_cal.ctdprs(clean_idx),oxy_match_cal.niskinbtlpos(clean_idx),oxy_match_cal.sampnbr(clean_idx),hcolor,'Uncalibrated','Secondary','Oxygen','umol/kg');
else
[quality]=make_plots_report(bottle_oxy_raw,ctd_oxy_raw,oxy_match_cal.stnnbr(clean_idx),oxy_match_cal.ctdprs(clean_idx),oxy_match_cal.niskinbtlpos(clean_idx),oxy_match_cal.sampnbr(clean_idx),hcolor,'Uncalibrated','Primary','Oxygen','umol/kg');    
end;

if pri_sec==2
[quality]=make_plots_report(bottle_oxy,ctd_oxy_1,station_num,ctd_pressure,niskin_num,sample_bottle_num,hcolor,'Calibrated','Secondary','Oxygen','umol/kg');    
else
[quality]=make_plots_report(bottle_oxy,ctd_oxy_1,station_num,ctd_pressure,niskin_num,sample_bottle_num,hcolor,'Calibrated','Primary','Oxygen','umol/kg');    
end;  

if pri_sec==2
%Report Plot
report_plots(bottle_oxy_raw,ctd_oxy_raw,oxy_match_cal.stnnbr(clean_idx),oxy_match_cal.ctdprs(clean_idx),oxy_match_cal.niskinbtlpos(clean_idx),oxy_match_cal.sampnbr(clean_idx),hcolor,'Uncalibrated','Secondary','Oxygen','umol/kg');
%Report Plot
report_plots(bottle_oxy,ctd_oxy_1,station_num,ctd_pressure,niskin_num,sample_bottle_num,hcolor,'Calibrated','Secondary','Oxygen','umol/kg');
else
%Report Plot
report_plots(bottle_oxy_raw,ctd_oxy_raw,oxy_match_cal.stnnbr(clean_idx),oxy_match_cal.ctdprs(clean_idx),oxy_match_cal.niskinbtlpos(clean_idx),oxy_match_cal.sampnbr(clean_idx),hcolor,'Uncalibrated','Primary','Oxygen','umol/kg');
%Report Plot
report_plots(bottle_oxy,ctd_oxy_1,station_num,ctd_pressure,niskin_num,sample_bottle_num,hcolor,'Calibrated','Primary','Oxygen','umol/kg');    
end

diff_pct=abs((bottle_oxy_raw-ctd_oxy_raw)./bottle_oxy_raw);
aux=find(diff_pct<=0.01);
length(aux)/length(diff_pct)
deep_idx=find(ctd_pressure>500);
diff_pct=abs((bottle_oxy_raw(deep_idx)-ctd_oxy_raw(deep_idx))./bottle_oxy_raw(deep_idx));
aux=find(diff_pct<=0.01);
length(aux)/length(diff_pct)

diff_pct=abs((bottle_oxy-ctd_oxy_1)./bottle_oxy);
aux=find(diff_pct<=0.01);
length(aux)/length(diff_pct)
deep_idx=find(ctd_pressure>500);
diff_pct=abs((bottle_oxy(deep_idx)-ctd_oxy_1(deep_idx))./bottle_oxy(deep_idx));
aux=find(diff_pct<=0.01);
length(aux)/length(diff_pct)
%Save the oxygen summary structure.
%%%
return
  