function [quality,fh]=make_plots_report(bottle_var,ctd_var,ctd_sta,ctd_prs, ...
	nis,smp_btl,hcolor,datatype,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);
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' ',sensor,' Sensor'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
data='All';
linregctd(bottle_var,ctd_var,variable,unit,sensor,data)
title([datatype, ' - ' ,'Least-squares fit of Y on X with maximum 95% CIs below '])

%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;
%%%%%%%%%
data=['Below ', num2str(hcolor),' dbar'];
linregctd(bottle_var(deep_idx),ctd_var(deep_idx),variable,unit,sensor,data)
title([datatype, ' - ' ,'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;
%%%%%%%%%
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([datatype,' ',sensor,' Sensor - ','Station Number'])
legend(['All ',variable],['Deep ',variable],'Location','Northwest')

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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)),'k^','MarkerFaceColor','k')
     aux=find(ctd_sta==sta_unique(i) & ctd_prs>=hcolor);
    plot(sta_unique(i),median(bottle_var(aux)-ctd_var(aux)),'ks','MarkerFaceColor','k')
end
grid on;
xlabel('Station number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
diff_median=nanmedian(bottle_var-ctd_var);
diff_stddev=nanstd(bottle_var-ctd_var);
title([datatype,' ',sensor,' Sensor - Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)])  
 
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' ',sensor,' Sensor'])
 
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
plot(smp_btl,bottle_var-ctd_var,'g+')
grid on;
hold on
plot(smp_btl(deep_idx),bottle_var(deep_idx)-ctd_var(deep_idx),'b+')
xlabel('Sample Bottle number')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
title([datatype,' ',sensor,' Sensor'])
  
%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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)')
ylabel(['Bottle ',variable,' - CTD ', variable,' (',unit,')'])
diff_median=nanmedian(bottle_var(deep_idx)-ctd_var(deep_idx));
diff_stddev=nanstd(bottle_var(deep_idx)-ctd_var(deep_idx));
title([datatype,' ',sensor,' Sensor - Below ',num2str(hcolor),' dbar - Median Diff: ',num2str(diff_median),' Std Dev: ',num2str(diff_stddev)])  

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' Data - ''Box Plot of Bottle - CTD ', variable])

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' Data - QQ Plot of Bottle - CTD ',variable])

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

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' Data - Box Plot of Bottle - CTD ', variable,' - Below ',num2str(hcolor),' dbar'])

%%%%%%%%% CMDR figure handles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_cnt=fig_cnt+1;
fh(fig_cnt)=figure;
%%%%%%%%%
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([datatype,' Data - QQ Plot of Bottle - CTD ',variable, ' - 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))=0;
%%%
bad_idx=find(quality==0);



