
%%
st1=2;
st2=2;
boxy=ox_units(oxy_match.btloxy,oxy_match.ctdoxy2,...
        sw_ptmp(oxy_match.ctdtmp2,oxy_match.ctdsal2,oxy_match.ctdprs,0),'mmk2mll');
 
% loading the data
    ii=find(oxy_match.stnnbr>=st1 & oxy_match.stnnbr<=st2  & ...
		oxy_match.quality1==2 & oxy_match.quality2==2);
  % common variables
    stn=oxy_match.stnnbr(ii);
    nis=oxy_match.niskinbtlpos(ii);
    o2_bot_num=oxy_match.sampnbr(ii);
    cprs=oxy_match.ctdprs(ii);
    %Convert boxy from umol to mll
    boxy_ori=oxy_match.btloxy(ii);
    %ptmp=sw_ptmp(oxy_match.ctdtmp2,oxy_match.ctdsal2,oxy_match.ctdprs,0);
    boxy=ox_units(oxy_match.btloxy(ii),oxy_match.ctdoxy2(ii),...
        sw_ptmp(oxy_match.ctdtmp2(ii),oxy_match.ctdsal2(ii),oxy_match.ctdprs(ii),0),'mmk2mll');
 
    if 1,
  % primary sensors
     ctem1=oxy_match.ctdtmp1(ii);
     csal1=oxy_match.ctdsal1(ii);
     coxv1=oxy_match.ctdoxv1(ii);
     coxy1=oxy_match.ctdoxy1(ii);
     ccon1=oxy_match.ctdcon1(ii);
    end;
     
      % secondary sensors
%%%%%%%%%%% CMDR test for second sensors. Set flags about the 2nd sensors
% ctem2
     if ~isnan(oxy_match.ctdtmp2), ctem2=oxy_match.ctdtmp2(ii);
		else ctem2=NaN; t2flag=0; end;
% csal2
     if ~isnan('oxy_match.ctdsal2'), csal2=oxy_match.ctdsal2(ii);
		else csal2=NaN; s2flag=0; end;
% coxv2
     if  ~isnan('oxy_match.ctdoxv2'), coxv2=oxy_match.ctdoxv2(ii);
		else  coxv2=NaN; o2vflag=0; end;
% coxy2
     if ~isnan(oxy_match.ctdoxy2), coxy2=oxy_match.ctdoxy2(ii);
		else  coxy2=NaN; o2cflag=0; end;
% ccon2
     if ~isnan(oxy_match.ctdcon2), ccon2=oxy_match.ctdcon2(ii);
		else  ccon2=NaN; c2flag=0; end;
%%%%%%%%%
    diff_temp=ctem1-ctem2;
    dt_max=mean(diff_temp)+3*std(diff_temp);
    dt_min=mean(diff_temp)-3*std(diff_temp);
    good_data=find(diff_temp>=dt_min & diff_temp<=dt_max);
    if 0,
     disp(' 0 = use the primary sensor (DEFAULT)' )
    disp(' 1 = use the secondary sensor' )
    pri_sec=input('Do you want to use the primary or the secondary sensor: ');
    while (isempty(pri_sec)==1 | pri_sec<0 | pri_sec>1),
	 disp('Please answer 0 (zero) for no or 1 (one) for yes ');
	 pri_sec=input(['Do you want to use the primary or ' ...
		'the secondary sensor: ']);
    end;
    end;
    %if pri_sec == 1,
    %Secondary oxygen sensor 1266 ab1202 post cal
    fprintf('Secondary sensor cal coeff\t')
    Soc = 0.5174
    Voffset = -0.5395
    tau20 = 1.40
    A = -2.7060e-3
    B = 1.03201e-4
    C = -2.0782e-6
    E = 0.036
    
    if 0,
    %Primary oxygen sensor 0730 ab1202 post cal
    fprintf('Primary sensor cal coeff\t')    
    Soc = 0.4993
    Voffset = -0.5074
    Tau20 = 1.36
    A = -3.0102e-3
    B = 9.4219e-5
    C = -1.7332e-6
    E = 0.036
    end;

    
    stn=stn(good_data);
    nis=nis(good_data);
    o2_bot_num=o2_bot_num(good_data);
    cprs=cprs(good_data);
    boxy=boxy(good_data);
    boxy_ori=boxy_ori(good_data);
    if 0,
    %if pri_sec==0
     ctem=ctem1(good_data);
     csal=csal1(good_data);
     coxv=coxv1(good_data);
     coxy=coxy1(good_data);
     ccon=ccon1(good_data);
    sensor='Primary';
    end;
     ctem=ctem2(good_data);
     csal=csal2(good_data);
     coxv=coxv2(good_data);
     coxy=coxy2(good_data);
     ccon=ccon2(good_data);
    sensor='Secondary';
    
    
    jj_nan=find(isnan(boxy)==0);
stn=stn(jj_nan);
nis=nis(jj_nan);
o2_bot_num=o2_bot_num(jj_nan);
cprs=cprs(jj_nan);
ctem=ctem(jj_nan);
ccon=ccon(jj_nan);
csal=csal(jj_nan);
coxv=coxv(jj_nan);
coxy=coxy(jj_nan);
boxy=boxy(jj_nan);
%%%%%%
%Calculating the oxygen saturation

%%%%%%%%%%% CMDR use seawater O2 saturation function
[msize,nsize] = size(boxy);
oxy=zeros(msize,nsize);
oxsat=zeros(msize,nsize);
%
oxsat=sw_satO2(ctem2,csal2);

disp('Possible equations to fit: ');
disp('  0 = None - no fit for the station number (DEFAULT)');
disp('  1 = First Order:  linear fit [p1*sta] ');
method=input('What kind of fit for the station number: ');
if (isempty(method)==1),
    method=0;
end;
if (method==0),
[new_oxy,Soc,Voffset,A,B,C,E]=...
    	oxy_fit_new_equation(coxv2,oxsat,ctem2,cprs,stn,boxy,Soc,Voffset,A,B,C,E);
end
if (method==1)
p1=0;
    [new_oxy,Soc,Voffset,A,B,C,E,p1]=...
    	oxy_fit_new_equation_linear(coxv,oxsat,ctem,cprs,stn,boxy,Soc,Voffset,A,B,C,E,p1);
end

coxymm_new=ox_units(new_oxy,csal2,sw_ptmp(ctem2,csal2,cprs,0),'mll2mmk');

f1
plot(cnv_dlp(2).oxmmsec,cnv_dlp(2).pressure,'b')
revyaxis
hold on
jj=find(oxy_match.stnnbr==2);
plot(oxy_match.btloxy(jj),oxy_match.ctdprs(jj),'bx')
ii=find(stn==2);
plot(boxymm(ii),cprs(ii),'r')
plot(ox_units(boxy(ii),csal(ii),sw_ptmp(ctem(ii),csal(ii),cprs(ii),0),'mll2mmk'),...
    cprs(ii),'xr')
legend('Sec dlp','bottle','new oxy','new btl?')

