function fh=save_plots(bottle_var,ctd_var1,ctd_var2,ctd_sta,ctd_prs, ...
	nis,smp_btl,hcolor,variable,unit)
% SAVE_PLOTS - saves plots

% this function had no comments!

% 
% VERSION: 
%	$Revision: 1.6 $
%	$Date: 2008/02/18 19:47:26 $
%	$Id: save_plots.m,v 1.6 2008/02/18 19:47:26 duncombe Exp duncombe $
%
% CHANGELOG:
% 	2007/12/17 CMDR 
% 	* excessive use of the gtext function is driving me insane. test and
%	skip these lines
% 	* secondary sensors are missing. test and deal.
%	* return figure handles

CMDR_insanity_check=0;
fcnt=0;

deep_idx=find(ctd_prs>=hcolor);
set(0,'DefaultAxesFontSize',8)
set(0,'DefaultTextFontSize',8)

fcnt=fcnt+1;
fh(fcnt)=makefig;

makeaxis(8.0,1.0,3,2.5);
plot(bottle_var,ctd_var1,'g+')
hold on
plot(bottle_var(deep_idx),ctd_var1(deep_idx),'b+')
grid on;
xlabel(['Bottle ',variable,' (',unit,')'])
ylabel(['CTD ',variable,' (',unit,')'])
title(['Primary Sensor -Uncalibrated Data'])

makeaxis(8.0,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
	plot(bottle_var,ctd_var2,'g+')
	hold on
	plot(bottle_var(deep_idx),ctd_var2(deep_idx),'b+')
end;
	grid on;
	xlabel(['Bottle ',variable,' (',unit,')'])
	ylabel(['CTD ',variable,' (',unit,')'])
	title(['Secondary Sensor -Uncalibrated Data'])
makeaxis(4.5,1.0,3,2.5);
linregctd(bottle_var,ctd_var1,variable,unit)
if CMDR_insanity_check, gtext('All') ; end;
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
makeaxis(4.5,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
	linregctd(bottle_var,ctd_var2,variable,unit)
end;
if CMDR_insanity_check, gtext('All') ; end;
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;
makeaxis(1.0,1.0,3,2.5);
linregctd(bottle_var(deep_idx),ctd_var1(deep_idx),variable,unit)
if CMDR_insanity_check, gtext(['Below ',num2str(hcolor),' dbar']) ; end;
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
makeaxis(1.0,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
	linregctd(bottle_var(deep_idx),ctd_var2(deep_idx),variable,unit)
end;
if CMDR_insanity_check, gtext(['Below ',num2str(hcolor),' dbar']) ; end;
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;

fcnt=fcnt+1;
fh(fcnt)=makefig;

makeaxis(8.0,1.0,3,2.5);
plot(bottle_var,ctd_var1,'g+')
hold on
plot(bottle_var(deep_idx),ctd_var1(deep_idx),'b+')
text(bottle_var,ctd_var1,int2str(ctd_sta))
grid on;
xlabel(['Bottle ',variable,' (',unit,')'])
ylabel(['CTD ',variable,' (',unit,')'])
title(['Primary Sensor -Uncalibrated Data - Station Number'])

makeaxis(8.0,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
	plot(bottle_var,ctd_var2,'g+'); 
	hold on
	plot(bottle_var(deep_idx),ctd_var2(deep_idx),'b+')
	text(bottle_var,ctd_var2,int2str(ctd_sta))
end;
grid on;
xlabel(['Bottle ',variable,' (',unit,')'])
ylabel(['CTD ',variable,' (',unit,')'])
title(['Secondary Sensor -Uncalibrated Data - Station Number'])

makeaxis(4.5,1.0,3,2.5);
plot(ctd_sta,bottle_var-ctd_var1,'g+')
hold on
plot(ctd_sta(deep_idx),bottle_var(deep_idx)-ctd_var1(deep_idx),'b+')
sta_unique=unique(ctd_sta);
for i=1:1:length(sta_unique)
    aux=find(ctd_sta==sta_unique(i));
    plot(sta_unique(i),median(bottle_var(aux)-ctd_var1(aux)),'m^', ...
	'MarkerFaceColor','m')
     aux=find(ctd_sta==sta_unique(i) & ctd_prs>=hcolor);
    plot(sta_unique(i),median(bottle_var(aux)-ctd_var1(aux)),'rs', ...
	'MarkerFaceColor','r')
end
grid on;
xlabel('Station number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
diff_median=median(bottle_var-ctd_var1);
diff_stddev=std(bottle_var-ctd_var1);
title(['Primary Sensor - Median Diff: ',num2str(diff_median),' Std Dev: ', ...
	num2str(diff_stddev)])  

makeaxis(4.5,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
	plot(ctd_sta,bottle_var-ctd_var2,'g+')
	hold on
	plot(ctd_sta(deep_idx),bottle_var(deep_idx)-ctd_var2(deep_idx),'b+')
	sta_unique=unique(ctd_sta);
	for i=1:1:length(sta_unique)
    		aux=find(ctd_sta==sta_unique(i));
    		plot(sta_unique(i),median(bottle_var(aux)-ctd_var2(aux)), ...
	'm^','MarkerFaceColor','m')
     		aux=find(ctd_sta==sta_unique(i) & ctd_prs>=hcolor);
    		plot(sta_unique(i),median(bottle_var(aux)-ctd_var2(aux)), ...
	'rs','MarkerFaceColor','r')
	end
end;
grid on;
xlabel('Station number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
diff_median=median(bottle_var-ctd_var2);
diff_stddev=std(bottle_var-ctd_var2);
else
diff_median=NaN;
diff_stddev=NaN;
end; 
%%%%%%%% end CMDR 
title(['Secondary Sensor - Median Diff: ',num2str(diff_median),' Std Dev: ' ...
	,num2str(diff_stddev)]) 

makeaxis(1.0,1.0,3,2.5);
plot(nis,bottle_var-ctd_var1,'g+')
grid on;
hold on
plot(nis(deep_idx),bottle_var(deep_idx)-ctd_var1(deep_idx),'b+')
xlabel('Niskin number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title(['Primary Sensor -Uncalibrated Data'])
 
makeaxis(1.0,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
plot(nis,bottle_var-ctd_var2,'g+')
hold on
plot(nis(deep_idx),bottle_var(deep_idx)-ctd_var2(deep_idx),'b+')
end;
%%%%%%%% end CMDR 
grid on;
xlabel('Niskin number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title(['Secondary Sensor -Uncalibrated Data'])

fcnt=fcnt+1;
fh(fcnt)=makefig;

makeaxis(8.0,1.0,3,2.5);
plot(smp_btl,bottle_var-ctd_var1,'g+')
grid on;
hold on
plot(smp_btl(deep_idx),bottle_var(deep_idx)-ctd_var1(deep_idx),'b+')
xlabel('Sample Bottle number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title(['Primary Sensor -Uncalibrated Data'])

makeaxis(8.0,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
plot(smp_btl,bottle_var-ctd_var2,'g+')
hold on
plot(smp_btl(deep_idx),bottle_var(deep_idx)-ctd_var2(deep_idx),'b+')
end;
grid on;
xlabel('Sample Bottle number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title(['Secondary Sensor -Uncalibrated Data'])

makeaxis(4.5,1.0,3,2.5);
plot(ctd_prs,bottle_var-ctd_var1,'g+')
grid on;
hold on
plot(ctd_prs(deep_idx),bottle_var(deep_idx)-ctd_var1(deep_idx),'b+')
xlabel('Pressure (dbar)')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
diff_median=median(bottle_var(deep_idx)-ctd_var1(deep_idx));
diff_stddev=std(bottle_var(deep_idx)-ctd_var1(deep_idx));
title(['Primary Sensor - < 1000 dbar - Diff: ',num2str(diff_median),' +-', ...
	num2str(diff_stddev)])  

makeaxis(4.5,5.0,3,2.5);
%%%%%%%% CMDR 
if ~isempty(ctd_var2),
plot(ctd_prs,bottle_var-ctd_var2,'g+')
hold on
plot(ctd_prs(deep_idx),bottle_var(deep_idx)-ctd_var2(deep_idx),'b+')
end;
%%%%%%%%
grid on;
xlabel('Pressure (dbar)')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
diff_median=median(bottle_var(deep_idx)-ctd_var2(deep_idx));
diff_stddev=std(bottle_var(deep_idx)-ctd_var2(deep_idx));
else
diff_median=NaN;
diff_stddev=NaN;
end;
%%%%%%%
title(['Secondary Sensor - < 1000 dbar - Diff: ',num2str(diff_median), ...
	' +-',num2str(diff_stddev)])  
makeaxis(1.0,1.0,3,2.5);
name=['BTL - CTD (',unit,')'];
[params1, outside_values] = boxparams(bottle_var-ctd_var1);
boxplotter1(params1, outside_values,name);
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
title(['Box Plot of BTL- CTD ', variable,' - Uncalibrated Data'])

makeaxis(1.0,5.0,3,2.5);
name=['BTL - CTD (',unit,')'];
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
[params2, outside_values] = boxparams(bottle_var-ctd_var2);
boxplotter1(params2, outside_values,name);
end;
%%%%%%% 
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;
title(['Box Plot of BTL - CTD ', variable,' - Uncalibrated Data'])

fcnt=fcnt+1;
fh(fcnt)=makefig;

makeaxis(8.0,1.0,3,2.5);
hlim=params1(5);
llim=params1(1);
name=['BTL - CTD (',unit,')'];
normalqqplot(bottle_var-ctd_var1,name,hlim,llim)
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
grid off
title(['QQ Plot of BTL - CTD ',variable, ' - Uncalibrated Data'])

makeaxis(8.0,5.0,3,2.5);
name=['BTL - CTD (',unit,')'];
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
	hlim=params2(5);
	llim=params2(1);
	normalqqplot(bottle_var-ctd_var2,name,hlim,llim)
end;
%%%%%%%% 
grid off
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;
title(['QQ Plot of BTL - CTD ',variable, ' - Uncalibrated Data'])

makeaxis(4.5,1.0,3,2.5);
name=['BTL - CTD (',unit,')'];
[params1, outside_values] = boxparams_deep( ...
	bottle_var(deep_idx)-ctd_var1(deep_idx));
boxplotter1(params1, outside_values,name);
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
title(['Box Plot of BTL - CTD ', variable,' - Uncalibrated Data - Below ', ...
	num2str(hcolor),' dbar'])

makeaxis(4.5,5.0,3,2.5);
name=['BTL - CTD (',unit,')'];
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
[params2, outside_values] = boxparams_deep( ...
	bottle_var(deep_idx)-ctd_var2(deep_idx));
boxplotter1(params2, outside_values,name);
end;
%%%%%%%% 
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;
title(['Box Plot of BTL - CTD ', variable,' - Uncalibrated Data - Below ', ...
	num2str(hcolor),' dbar'])

makeaxis(1.0,1.0,3,2.5);
hlim=params1(5);
llim=params1(1);
name=['BTL - CTD (',unit,')'];
normalqqplot(bottle_var(deep_idx)-ctd_var1(deep_idx),name,hlim,llim)
if CMDR_insanity_check, gtext(['Primary Sensor']) ; end;
grid off
title(['QQ Plot of BTL - CTD ',variable, ' - Uncalibrated Data - Below ', ...
	num2str(hcolor),' dbar'])

makeaxis(1.0,5.0,3,2.5);
name=['BTL - CTD (',unit,')'];
%%%%%%%% CMDR 
if ~isempty(ctd_var2), 
	hlim=params2(5);
	llim=params2(1);
	normalqqplot(bottle_var(deep_idx)-ctd_var2(deep_idx),name,hlim,llim)
end;
grid off
if CMDR_insanity_check, gtext(['Secondary Sensor']) ; end;
title(['QQ Plot of BTL - CTD ',variable, ' - Uncalibrated Data - Below ', ...
	num2str(hcolor),' dbar'])

set(0,'DefaultAxesFontSize',10)
set(0,'DefaultTextFontSize',10)

return;

