function plot_sensors_diff_beckman(cruiseid)
% PLOT_SENSORS_DIFF_BECKMAN - diagnostics plots to compare sensor differences
%
% CTD Calibration toolbox.
%
% USAGE
% 	plot_sensors_diff_beckman(cruiseid);
%
% INPUT
% 	cruiseid	cruise identification name
%   
% OUTPUT
%	A series of plots to compare differences between CTD sensors.
%          
% 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
%         Wed Apr 25 11:23:37 EDT 2007
%
%
% VERSION: 
%	$Revision: 1.3 $
%	$Date: 2008/04/09 17:22:56 $
% 
% CHANGELOG: 
%	* Initial version.
%  2008/04/09 CMDR
%	* incorporate changes from plot_sensors_diff.m
%

% Retrieve the necessary preferences for this cruise

%%%

%%%
%%%%%%%%%%% CMDR read cruiseid if we can
if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), cruiseid=getcruiseid; end;
%%%%%%%%%%%
% 
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,' btl']);
	eval(ld_file)
else
	error(['The cruise data base file was not found on the path.  ' ...
		db_file]);
end;

n=who('btl');

if isempty(n)==1,
  error(['btl not found. Run load_btl.m ']);
  return;
end;
 
 % Flags for the temperature scale conversion (t90 to t68)
flag_conv1=0;
flag_conv2=0;
flag_message1=0;
flag_message2=0;
%
 z=1;
 for i=1:length(btl)
   	for j=1:length(btl(i).names);
		if strncmp(btl(i).names(j),'pressure',8);
			pr_idx=j;
		end
        if strncmp(btl(i).names(j),'sbedo2_mll_pri',14);
        ctd_oxy1_idx=j;
        end
        if strncmp(btl(i).names(j),'sbedo2_mll_sec',14);
        ctd_oxy2_idx=j;
        end
        %%%%% YSI Beckman - AB01
        if strncmp(btl(i).names(j),'do2_pri',7),
        	ctd_oxy1_idx=j;
        end;
        if strncmp(btl(i).names(j),'do2_sec',7),
        	ctd_oxy2_idx=j;
        end;
		if strncmp(btl(i).names(j),'con_pri',7);
			ctd_c1_idx=j;
		end
		if strncmp(btl(i).names(j),'con_sec',7);
			ctd_c2_idx=j;
		end
		if strncmp(btl(i).names(j),'t90_pri',7);
			ctd_t1_idx=j;
		end
		if strncmp(btl(i).names(j),'t90_sec',7);
			ctd_t2_idx=j;
        end
	    if strncmp(btl(i).names(j),'t68_pri',7);
			ctd_t1_idx=j;
			flag_conv1=1;
			if flag_message1==0
				disp('Primary temperatures are given in T68.')
				disp('This program will convert to T90 to make the necessary calculations')
				flag_message1=1;
			end
		end
		if strncmp(btl(i).names(j),'t68_sec',7);
			ctd_t2_idx=j;
			flag_conv2=1;
			if flag_message2==0
				disp('Secondary temperatures are given in T68.')
				disp('This program will convert to T90 to make the necessary calculations')
				flag_message2=1;
			end  
		end
	 end % j loop
 for k=1:length(btl(i).data(:,1) )   
    ctd_diff.station(z,1)=btl(i).station;
    ctd_diff.latitude(z,1)=btl(i).lat;
    ctd_diff.longitude(z,1)=btl(i).lon;
    ctd_diff.pressure(z,1)=btl(i).data(k,pr_idx);
	ctd_diff.cond1(z,1)=10*btl(i).data(k,ctd_c1_idx);
	ctd_diff.cond2(z,1)=10*btl(i).data(k,ctd_c2_idx);
    ctd_diff.oxy1(z,1)=btl(i).data(k,ctd_oxy1_idx);
    ctd_diff.oxy2(z,1)=btl(i).data(k,ctd_oxy2_idx); 
	ctd_diff.temp1(z,1)=btl(i).data(k,ctd_t1_idx);
    ctd_diff.temp2(z,1)=btl(i).data(k,ctd_t2_idx);  
    if flag_conv1==1;
		ctd_diff.temp1(z,1)=t68tot90(ctd_diff.temp1(z,1));   
	end
	if flag_conv2==1;
		ctd_diff.temp2(z,1)=t68tot90(ctd_diff.temp2(z,1));   
    end 
    z=z+1;
 end % zloop
end %i loop
aux_deep=find(ctd_diff.pressure>500);

% plotting the T diff
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station,(ctd_diff.temp1-ctd_diff.temp2)*1000);
xlabel('station')
ylabel('T1-T2 residuals (m^{o}C)')
title(['Temperature Sensors Residuals \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure,(ctd_diff.temp1-ctd_diff.temp2)*1000);
xlabel('pressure(dbar)')
ylabel('T1-T2 residuals (m^{o}C)')
title('Temperature Sensors Residuals')
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station(aux_deep),(ctd_diff.temp1(aux_deep)-ctd_diff.temp2(aux_deep))*1000);
xlabel('station')
ylabel('T1-T2 residuals (m^{o}C)')
title(['Temperature Sensors Residuals below 500 dbar \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure(aux_deep),(ctd_diff.temp1(aux_deep)-ctd_diff.temp2(aux_deep))*1000);
xlabel('pressure(dbar)')
ylabel('T1-T2 residuals (m^{o}C)')
title('Temperature Sensors Residuals below 500 dbar')

% plotting the C diff
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station,(ctd_diff.cond1-ctd_diff.cond2)*1000);
xlabel('station')
ylabel('C1-C2 residuals (\muSm/cm)')
title(['Conductivity Sensors Residuals \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure,(ctd_diff.cond1-ctd_diff.cond2)*1000);
xlabel('pressure(dbar)')
ylabel('C1-C2 residuals (\muSm/cm)')
title('Conductivity Sensors Residuals')
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station(aux_deep),(ctd_diff.cond1(aux_deep)-ctd_diff.cond2(aux_deep))*1000);
xlabel('station')
ylabel('C1-C2 residuals (\muSm/cm)')
title(['Conductivity Sensors Residuals below 500 dbar \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure(aux_deep),(ctd_diff.cond1(aux_deep)-ctd_diff.cond2(aux_deep))*1000);
xlabel('pressure(dbar)')
ylabel('C1-C2 residuals (\muSm/cm)')
title('Conductivity Sensors Residuals below 500 dbar')

% plotting the O2 diff
figure;
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station,(ctd_diff.oxy1-ctd_diff.oxy2));
xlabel('station')
ylabel('O1-O2 residuals (ml/l)')
title(['Oxygen Sensors Residuals \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure,(ctd_diff.oxy1-ctd_diff.oxy2));
xlabel('pressure(dbar)')
ylabel('O1-O2 residuals (ml/l)')
title('Oxygen Sensors Residuals')
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.station(aux_deep),(ctd_diff.oxy1(aux_deep)-ctd_diff.oxy2(aux_deep)));
xlabel('station')
ylabel('O1-O2 residuals (ml/l)')
title(['Oxygen Sensors Residuals below 500 dbar \DeltaT= ',num2str(t_mean),' +- ',num2str(t_std)])
figure
[t_mean,t_std]=plot_sensors_residuals(ctd_diff.pressure(aux_deep),(ctd_diff.oxy1(aux_deep)-ctd_diff.oxy2(aux_deep)));
xlabel('pressure(dbar)')
ylabel('O1-O2 residuals (ml/l)')
title('Oxygen Sensors Residuals below 500 dbar')

return;

