function duplicate_stats(cruiseid)
% duplicate_stats -  Statistics about the duplicates collected.
%
% CTD Calibration toolbox.
%
% USAGE:
%  duplicate_stats(cruiseid);
%
%INPUT: cruiseid: Cruise identification name.
%   
% OUTPUT: Statistics about the duplicates collected...
%          
% 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 oxy_match oxy_match_cal']);
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

disp('**************************************************************************************')
disp('******Looking for duplicates*******')
disp('**************************************************************************************')

% first salinity
k=1;
for i=staa:1:stab;
aux_sta=find(cond_match.stnnbr==i & cond_match.quality1==2 &  cond_match.quality2==2);
 for j=1:24
     aux=find(cond_match.niskinbtlpos(aux_sta)==j);
     if length(aux)>1;
     sta(k,1)=i;
     niskin(k,1)=j;
     botsal1(k,1)=cond_match.btlsal(aux_sta(aux(1)));
     botsal2(k,1)=cond_match.btlsal(aux_sta(aux(2)));
     pressure(k,1)=cond_match.ctdprs(aux_sta(aux(2)));
     k=k+1;
     end
 end  
end
fid=fopen(['duplicates_salt.tab'],'w');
bp=[sprintf('%s\t','Station','Niskin','Salinity1','Salinity2','Differences')];
fprintf(fid,bp);
fprintf(fid,'\n');
fprintf(fid,'%2i\t%2i\t%8.3f\t%8.3f\t%8.3f\n',[sta niskin botsal1 botsal2 botsal1-botsal2]');
fclose(fid)

disp('****************************************** ')
disp('Sality Duplicates') 
disp(['Total : ',num2str(length(sta)),'  Pct of total samples: ',num2str(length(sta)/length(cond_match.stnnbr))])
disp('****************************************** ')
%%%%%%%%%%%%%%%
%Start Ploting%
%%%%%%%%%%%%%%%
figure
plot(sta,botsal1-botsal2,'k*')
xlabel('station')
ylabel('Salinity differences')
title(['Mean Difference: ', num2str(mean(botsal1-botsal2)),'  Std. Dev.: ', num2str(std(botsal1-botsal2))])

figure
plot(niskin,botsal1-botsal2,'k*')
xlabel('niskin bottle')
ylabel('Salinity differences')
title(['Mean Difference: ', num2str(mean(botsal1-botsal2)),'  Std. Dev.: ', num2str(std(botsal1-botsal2))])

figure
plot(pressure,botsal1-botsal2,'k*')
xlabel('pressure (dbar)')
ylabel('Salinity differences')
title(['Mean Difference: ', num2str(mean(botsal1-botsal2)),'  Std. Dev.: ', num2str(std(botsal1-botsal2))])


clear sta niskin pressure
% now oxygen
k=1;
for i=staa:1:stab;
aux_sta=find(oxy_match.stnnbr==i & oxy_match.quality1==2 &  oxy_match.quality2==2);
 for j=1:24
     aux=find(oxy_match.niskinbtlpos(aux_sta)==j);
     if length(aux)>1;
     sta(k,1)=i;
     niskin(k,1)=j;
     botoxy1(k,1)=ox_units(oxy_match.btloxy(aux_sta(aux(1))),oxy_match.ctdsal1(aux_sta(aux(1))),...
      sw_ptmp(oxy_match.ctdsal1(aux_sta(aux(1))),oxy_match.ctdtmp1(aux_sta(aux(1))),oxy_match.ctdprs(aux_sta(aux(1))),0),'mll2mmk');
     botoxy2(k,1)=ox_units(oxy_match.btloxy(aux_sta(aux(2))),oxy_match.ctdsal1(aux_sta(aux(2))),...
      sw_ptmp(oxy_match.ctdsal1(aux_sta(aux(2))),oxy_match.ctdtmp1(aux_sta(aux(2))),oxy_match.ctdprs(aux_sta(aux(2))),0),'mll2mmk');
     pressure(k,1)=oxy_match.ctdprs(aux_sta(aux(1)));
  k=k+1;
     end
 end  
end
fid=fopen(['duplicates_oxy.tab'],'w');
bp=[sprintf('%s\t','Station','Niskin','Oxygen1','Oxygen2','Differences')];
fprintf(fid,bp);
fprintf(fid,'\n');
fprintf(fid,'%2i\t%2i\t%3.1f\t%3.1f\t%3.1f\n',[sta niskin botoxy1 botoxy2 botoxy1-botoxy2]');
fclose(fid)

disp('****************************************** ')
disp('Oxygen Duplicates') 
disp(['Total : ',num2str(length(sta)),'  Pct of total samples: ',num2str(length(sta)/length(oxy_match.stnnbr))])
disp('****************************************** ')
%%%%%%%%%%%%%%%
%Start Ploting%
%%%%%%%%%%%%%%%
figure
plot(sta,botoxy1-botoxy2,'k*')
xlabel('station')
ylabel('Dissolved Oxygen differences (\mumol/kg)')
title(['Mean Difference: ', num2str(mean(botoxy1-botoxy2)),'  Std. Dev.: ', num2str(std(botoxy1-botoxy2))])

figure
plot(niskin,botoxy1-botoxy2,'k*')
xlabel('niskin')
ylabel('Dissolved Oxygen differences (\mumol/kg)')
title(['Mean Difference: ', num2str(mean(botoxy1-botoxy2)),'  Std. Dev.: ', num2str(std(botoxy1-botoxy2))])

figure
plot(pressure,botoxy1-botoxy2,'k*')
xlabel('pressure(dbar)')
ylabel('Dissolved Oxygen differences (\mumol/kg)')
title(['Mean Difference: ', num2str(mean(botoxy1-botoxy2)),'  Std. Dev.: ', num2str(std(botoxy1-botoxy2))])







return