function cond_cal_stats(cruiseid)
% cond_cal_stats -  Statistics about the condcutivity calibration to be used on the Data Report.
%
% CTD Calibration toolbox.
%
% USAGE:
% cond_cal_stats(cruiseid);
%
%INPUT: cruiseid: Cruise identification name.
%   
% OUTPUT: Statistics about the condcutivity calibration to be used on the Data Report..
%          
% DESCRIPTION: This function uses the summary conductivity calibration file created by  
% make_cond_sumfile....
%
% 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.

% 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_cal 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_cal');
 if isempty(n)==1
 error(['cond_match_cal not found. Run make_cal_sumfile.m ']);
 return;
 end
disp('****************************************** ')
disp('PRELIMINARY COMPARISONS')
disp('****************************************** ')
disp('The data found in cond_match goes from') 
disp(['the station ',num2str(min(cond_match_cal.stnnbr)),' to the station ',num2str(max(cond_match_cal.stnnbr)),'.'] ) 
disp('****************************************** ')
%
staa=input('Input first station: ');
stab=input('Input last station: ');

%%%%%%%%
%
disp(' 0 = use the primary sensor (DEFAULT)' )
disp(' 1 = 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>1)
	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
%s_index=find( cond_match_cal.stnnbr>= staa & cond_match_cal.stnnbr  <= stab   & cond_match_cal.quality1==2 & cond_match_cal.quality2==2);
s_index=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab);
disp('**************************************************************************************')
disp('First the Raw Values...')
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']); 
%
 station_num_raw=cond_match.stnnbr(s_index);
 bottle_sal_raw=cond_match.btlsal(s_index);
 if pri_sec==1
 ctd_sal_raw=cond_match.ctdsal2(s_index);
 else
 ctd_sal_raw=cond_match.ctdsal1(s_index);    
 end
 ctd_pressure_raw=cond_match.ctdprs(s_index);
aux_500=find(ctd_pressure_raw>=500);
aux_3000=find(ctd_pressure_raw>=3000);
disp(['average residual ',num2str(mean(bottle_sal_raw-ctd_sal_raw)),' sd ',num2str(std(bottle_sal_raw-ctd_sal_raw))])
disp(['average residual (below 500) ',num2str(mean(bottle_sal_raw(aux_500)-ctd_sal_raw(aux_500))),' sd ',num2str(std(bottle_sal_raw(aux_500)-ctd_sal_raw(aux_500)))])
disp(['average residual (below 3000)',num2str(mean(bottle_sal_raw(aux_3000)-ctd_sal_raw(aux_3000))),' sd ',num2str(std(bottle_sal_raw(aux_3000)-ctd_sal_raw(aux_3000)))])
 
s_index=find( cond_match.stnnbr>= staa & cond_match.stnnbr  <= stab   & cond_match.quality1==2 & cond_match.quality2==2);
disp('**************************************************************************************')
disp('Now the QC Values...')
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']); 
%
 station_num_raw=cond_match.stnnbr(s_index);
 bottle_sal_raw=cond_match.btlsal(s_index);
 if pri_sec==1
 ctd_sal_raw=cond_match.ctdsal2(s_index);
 else
 ctd_sal_raw=cond_match.ctdsal1(s_index);    
 end
 ctd_pressure_raw=cond_match.ctdprs(s_index);
aux_500=find(ctd_pressure_raw>=500);
aux_3000=find(ctd_pressure_raw>=3000);
disp(['average residual ',num2str(mean(bottle_sal_raw-ctd_sal_raw)),' sd ',num2str(std(bottle_sal_raw-ctd_sal_raw))])
disp(['average residual (below 500) ',num2str(mean(bottle_sal_raw(aux_500)-ctd_sal_raw(aux_500))),' sd ',num2str(std(bottle_sal_raw(aux_500)-ctd_sal_raw(aux_500)))])
disp(['average residual (below 3000)',num2str(mean(bottle_sal_raw(aux_3000)-ctd_sal_raw(aux_3000))),' sd ',num2str(std(bottle_sal_raw(aux_3000)-ctd_sal_raw(aux_3000)))])

disp('**************************************************************************************')
disp('And  finally the Calibrated Values...')
disp(['Number of good data points found: ',num2str(length(s_index))])
aux_gd=find( cond_match_cal.stnnbr>= staa & cond_match_cal.stnnbr  <= stab);
per_gd=(length(s_index)/length(aux_gd))*100;
disp(['This represents ', num2str(per_gd),'% of the data']); 
%
 station_num=cond_match_cal.stnnbr(s_index);
 bottle_sal=cond_match_cal.btlsal(s_index);
 ctd_sal_1=cond_match_cal.ctdsal(s_index);
 niskin_num=cond_match_cal.niskinbtlpos(s_index);
 sample_bottle_num=cond_match_cal.sampnbr(s_index);
 ctd_pressure=cond_match_cal.ctdprs(s_index);

 aux_500=find(ctd_pressure>=500);
aux_3000=find(ctd_pressure>=3000);
disp(['average residual ',num2str(mean(bottle_sal-ctd_sal_1)),' sd ',num2str(std(bottle_sal-ctd_sal_1))])
disp(['average residual (below 500) ',num2str(mean(bottle_sal(aux_500)-ctd_sal_1(aux_500))),' sd ',num2str(std(bottle_sal(aux_500)-ctd_sal_1(aux_500)))])
disp(['average residual (below 3000)',num2str(mean(bottle_sal(aux_3000)-ctd_sal_1(aux_3000))),' sd ',num2str(std(bottle_sal(aux_3000)-ctd_sal_1(aux_3000)))])


%%%%%%%%%%%%%%%
%Start Ploting%
%%%%%%%%%%%%%%%
return