%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function drawplots
%%%%%%%%%%%%%%%%%%%%

%%%%% GO OVER THIS AND MAKE SURE THE VARIABLES ARE CORRECT.  HE DIDN'T SEEM
%%%%% TO HAVE CHANGED THE NAMES SO NEED TO MAKE SURE WE ARE ACTUALLY
%%%%% WORKING WITH OXYGEN DATA AND SALINITY. ALSO ADD IN SECONDARY OXYGEN
%%%%% AND TEMPERATURE
function [figTO,figOP,figDeln]=oxycheckplots(ctddat,botmatch,idx_sta)

%col='bgrcmkbgrcmk';
figTO=bigplot;
figOP=bigplot;
figDeln=bigplot;


for istn=1:length({ctddat.station}),
	Ibot=find(ctddat(istn).station == botmatch.stnnbr & botmatch.quality1==2);
    %Bottle data
	Ob=botmatch.btloxy(Ibot);
    Tb=botmatch.ctdtmp1(Ibot);
	Pb=botmatch.ctdprs(Ibot);
    %Sensor data
	%O=ox_units(ctddat(istn).oxmllpri);
    %O2=ox_units(ctddat(istn).oxmllsec);
    O=ctddat(istn).oxmmpri;
    O2=ctddat(istn).oxmmsec;
	T=ctddat(istn).temppri;
    T2=ctddat(istn).tempsec;
	P=ctddat(istn).pressure;
    
    % T-O plot
	figure(figTO);
		plot(O,T); 
		hold on;
        plot(O2,T2,'k');
		plot(Ob,Tb,'rx');       
        title(['TO Plot: Station ' num2str(ctddat(istn).station)]);
		xlabel('Oxygen');
		ylabel('Temperature');
        legend('Primary','Secondary','Bottle','Location','NorthWest');
        
    % O-P plot    
	figure(figOP);
		plot(O,P);
		set(gca,'ydir','reverse');
		hold on;
        plot(O2,P,'k');
		plot(Ob,Pb,'rx');
		title(['Oxygen Profile: Station ' num2str(ctddat(istn).station)]);
		xlabel('Oxygen');
		ylabel('Pressure');
        legend('Primary','Secondary','Bottle','Location','NorthWest');
end;
    aux=find(botmatch.quality1(idx_sta)==2);
    %Oc=botmatch.ctdoxy1(idx_sta(aux);
	Oc=ox_units(botmatch.ctdoxy1(idx_sta(aux)),botmatch.ctdsal1(idx_sta(aux)), ...
        sw_ptmp(botmatch.ctdoxy1(idx_sta(aux)),botmatch.ctdtmp1(idx_sta(aux)),botmatch.ctdprs(idx_sta(aux)),0),'mll2mmk');
    %Oc2=botmatch.ctdoxy2(idx_sta(aux));
    Oc2=ox_units(botmatch.ctdoxy2(idx_sta(aux)),botmatch.ctdsal2(idx_sta(aux)), ...
        sw_ptmp(botmatch.ctdoxy2(idx_sta(aux)),botmatch.ctdtmp2(idx_sta(aux)),botmatch.ctdprs(idx_sta(aux)),0),'mll2mmk');
	Ob=botmatch.btloxy(idx_sta(aux));
	sta=botmatch.stnnbr(idx_sta(aux));
	Delta=Ob-Oc;
    Delta2=Ob-Oc2;
    d_idx=find(botmatch.ctdprs(idx_sta(aux))>1000);
    [params,extremes]=boxparams(Delta(~isnan(Delta)));
    [params2,extremes2]=boxparams(Delta2(~isnan(Delta2)));
    ii=find(Delta>=params(5)| Delta<=params(1));
    jj=find(Delta2>=params2(5)| Delta2<=params2(1));
	figure(figDeln);
    
    % Sensor-Bottle difference plot
    figure(figDeln);
		nO=Delta;
        nO2=Delta2;
		I=find(abs(nO)>0.02);
        J=find(abs(nO2)>0.02);
		plot(sta,nO,'+');
		hold on;
        plot(sta,nO2,'g+');
        plot(sta(d_idx),nO(d_idx),'r+');
        plot(sta(d_idx),nO2(d_idx),'c+');
        plot(sta(ii),nO(ii),'ro');
        plot(sta(jj),nO2(jj),'ro');
		title('Bottle and CTD difference');
		ylabel('difference (ml/l)');
        ylabel('difference (mM/Kg)');
        legend('Primary','Secondary','Primary (<1000m)','Secondary (<1000m)')

return;