function [quality,fh]=make_plots(bottle_var,ctd_var,ctd_sta,ctd_prs, ...
	nis,hcolor,sensor,variable,unit)
% MAKE_PLOTS - draws plots

% this function had no comments!

% 
% VERSION: 
%	$Revision: 1.6 $
%	$Date: 2008/02/18 19:36:17 $
%	$Id: make_plots.m,v 1.6 2008/02/18 19:36:17 duncombe Exp duncombe $
%
% CHANGELOG: 
%	2007/12/17 CMDR 
%	* excessive use of the gtext function is driving me insane. test
%	and skip these lines
%	* spelling error in output corrected
%	* return file handles of the plots we create here
%

CMDR_insanity_check=0;
fig_cnt=0;

deep_idx=find(ctd_prs>=hcolor);
quality=ones(length(ctd_var),1)*2;


%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
subplot(3,2,1)
%%%%%%%%%
data='All';
linregctd(bottle_var,ctd_var,variable,unit,sensor,data)
if CMDR_insanity_check, gtext('All') ; end;
if CMDR_insanity_check, gtext([sensor,' Sensor']) ; end;

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,2)
%%%%%%%%%
data=['Below ',num2str(hcolor),' dbar'];
linregctd(bottle_var(deep_idx),ctd_var(deep_idx),variable,unit,sensor,data)
title(['Least-squares fit of Y on X with maximum 95% CIs below ',num2str(hcolor),' dbar'])
if CMDR_insanity_check, gtext(['Below ',num2str(hcolor),' dbar']) ; end;
if CMDR_insanity_check, gtext([sensor,' Sensor']) ; end;

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,3)
%%%%%%%%%
name=['Bottle - CTD ', variable,' (',unit,')'];
[params, outside_values] = boxparams(bottle_var-ctd_var);
boxplotter1(params, outside_values,name);
if CMDR_insanity_check, gtext([sensor,' Sensor']) ; end;
title(['Box Plot of Bottle - CTD ', variable,' - Uncalibrated Data'])

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,4)
%%%%%%%%%
hlim=params(5);
llim=params(1);
name=['Quantiles of Bottle - CTD ',variable];
normalqqplot(bottle_var-ctd_var,name,hlim,llim)
grid off
if CMDR_insanity_check, gtext([sensor,' Sensor']) ; end;
title(['QQ Plot of Bottle - CTD ',variable, ' - Uncalibrated Data'])

%%% Marking the data for further quality control
aux=find(bottle_var-ctd_var >=params(5)| bottle_var-ctd_var <=params(1));
quality(aux)=3;
%

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,5)
%%%%%%%%%
name=['Bottle - CTD ', variable,' (',unit,')'];
[params, outside_values] = boxparams_deep(bottle_var(deep_idx)-ctd_var(deep_idx));
boxplotter1(params, outside_values,name);
text(0,1.5,[sensor,' Sensor'])
title(['Box Plot of Bottle - CTD ', variable,' - Uncalibrated Data - Below ',num2str(hcolor),' dbar'])

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,6)
%%%%%%%%%
hlim=params(5);
llim=params(1);
name=['Quantiles of Bottle  - CTD ',variable];
normalqqplot(bottle_var(deep_idx)-ctd_var(deep_idx),name,hlim,llim)
grid off
text(0.5,0,[sensor,' Sensor'])
title(['QQ Plot of Bottle - CTD ',variable, ' - Uncalibrated Data - Below ',num2str(hcolor),' dbar'])

%%% Marking the data for further quality control
aux1=find(bottle_var(deep_idx)-ctd_var(deep_idx)>= params(5)|...
    bottle_var(deep_idx)-ctd_var(deep_idx)<=params(1));
quality(deep_idx(aux1))=3;
%%%
bad_idx=find(quality==3);

%**************************************************************************
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
subplot(3,2,1)
%%%%%%%%%
plot(bottle_var,ctd_var,'g+')
hold on
plot(bottle_var(deep_idx),ctd_var(deep_idx),'b+')
grid on;
xlabel(['Bottle ',variable,' (',unit,')'])
ylabel(['CTD ',variable,' (',unit,')'])
title([sensor,' Sensor - Uncalibrated Data'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,2)
%%%%%%%%%
plot(bottle_var,ctd_var,'g+')
hold on
plot(bottle_var(deep_idx),ctd_var(deep_idx),'b+')
text(bottle_var,ctd_var,int2str(ctd_sta))
grid on;
xlabel(['Bottle ',variable,' (',unit,')'])
ylabel(['CTD ',variable,' (',unit,')'])
title([sensor,' Sensor - Uncalibrated Data - Station Number'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,3)
%%%%%%%%%
plot(nis,bottle_var-ctd_var,'g+')
grid on;
hold on
plot(nis(deep_idx),bottle_var(deep_idx)-ctd_var(deep_idx),'b+')
xlabel('Niskin number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title([sensor,' Sensor -Uncalibrated Data'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,4)
%%%%%%%%%
plot(ctd_sta,bottle_var-ctd_var,'g+')
hold on
plot(ctd_sta(deep_idx),bottle_var(deep_idx)-ctd_var(deep_idx),'b+')
plot(ctd_sta(bad_idx),bottle_var(bad_idx)-ctd_var(bad_idx),'ks','MarkerFaceColor','k')
xlabel('Station number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title('Data point points flagged as bad (black squares)')
legend(['All ',variable],['Deep ',variable])

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fig_cnt=fig_cnt+1;
%fh(fig_cnt)=figure;
subplot(3,2,5)
%%%%%%%%%
plot(nis,bottle_var-ctd_var,'g+')
grid on;
hold on
plot(nis(deep_idx),bottle_var(deep_idx)-ctd_var(deep_idx),'b+')
xlabel('Niskin number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title([sensor,' Sensor -Uncalibrated Data'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
font(14)
%%%%%%%%%
plot(ctd_sta,bottle_var-ctd_var,'g+')
hold on
plot(ctd_sta(deep_idx),bottle_var(deep_idx)-ctd_var(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_var(aux)),'m^','MarkerFaceColor','m')
     aux=find(ctd_sta==sta_unique(i) & ctd_prs>=hcolor);
    plot(sta_unique(i),median(bottle_var(aux)-ctd_var(aux)),'rs','MarkerFaceColor','r')
end
grid on;
xlabel('Station number','fonts',16)
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'],'fonts',16)
diff_median=median(bottle_var-ctd_var);
diff_stddev=std(bottle_var-ctd_var);
title([sensor,' Sensor - Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  
legend(['All ',variable],['Deep ',variable],['Median ',variable],['Median Deep ', variable],'Location','Northwest')
%print('-depsc',['figures/',sensor,variable,'stt']) 
saveas(gcf,['figures/',sensor,variable,'stt.fig'])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
font(14)
%%%%%%%%%
plot(ctd_prs,bottle_var-ctd_var,'g+')
grid on;
hold on
plot(ctd_prs(deep_idx),bottle_var(deep_idx)-ctd_var(deep_idx),'b+')
xlabel('Pressure (dbar)','fonts',16)
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'],'fonts',16)
diff_median=median(bottle_var(deep_idx)-ctd_var(deep_idx));
diff_stddev=std(bottle_var(deep_idx)-ctd_var(deep_idx));
title([sensor,' Sensor - >',num2str(hcolor),' dbar - Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)],'fonts',16)  
legend(['All ',variable],['Deep ',variable],'Location','Northwest')
%print('-depsc',['figures/',sensor,variable,'prs'])
saveas(gcf,['figures/',sensor,variable,'prs.fig'])
