function eval_calibration(cruiseid,beta,oxy_coeff1,oxy_coeff2)
% EVAL_CALIBRATION - Apply oxygen and conductivity calibration and evaluate the quality of the fit.
%
% CTD Calibration toolbox.
%
% USAGE:
% cond_match=apply_calibration(cruiseid,coeff);
%
% INPUT: cruiseid: Cruise identification name.
%        coeff: matrix with coefficients from conductivity calibration
%   
% OUTPUT: .... 
%          
% DESCRIPTION: ...  
% 
% AUTHORS:
%			Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
%
% CHANGELOG: 
%   07-Oct-2004, Version 1.0
%		* Initial version. 
%
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
flag_message1=0;
flag_message2=0;
flag_conv1=0;
flag_conv2=0;
%
% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% cruise_dir is the main calibration data directory 

if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
	error(['The cal_data_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end

% Load data from the cruise database
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
	load(db_file);
else
	error(['The cruise data base file was not found on the path.  ' db_file]);
end
disp('**************************************************************************************')
disp('This is the program that applies the calibration coefficients to the .cnv delooped data.')
disp('**************************************************************************************')

disp('**************************************************************************************')
disp(' 1 = use the primary sensor (DEFAULT)' )
disp(' 2 = use the secondary sensor' )
disp('First for the conductivity sensor:')
pri_sec_c=input('Do you want apply the calibration to the primary or to the secondary sensor: ');
while (isempty(pri_sec_c)==1 | pri_sec_c<=0 | pri_sec_c>2)
	disp('Please answer 0 (zero) for no or  1(one) for yes ')
	pri_sec_c=input('Do you want to use the primary or the secondary sensor: ');
end
disp('Now, for the oxygen sensor:')
pri_sec_o=input('Do you want apply the calibration to the primary or to the secondary sensor: ');
while (isempty(pri_sec_o)==1 | pri_sec_o<=0 | pri_sec_o>2)
	disp('Please answer 0 (zero) for no or  1(one) for yes ')
	pri_sec_o=input('Do you want to use the primary or the secondary sensor: ');
end
disp('***************************************************************************')
staa=input('Input first station: ');
stab=input('Input last station: ');
%%%%%%%%%%%%%%%%%%%%
%max_cnv=max(size(cnv));
%for i=1:max_cnv;
for i=staa:1:stab;
    sta=i;
    disp(['Applying the calibrations to the station ',num2str(i)])     
    aux=find(cond_match.stnnbr==i & cond_match.quality1);% == 2 & cond_match.quality2 == 2);
    aux1=find(oxy_match.stnnbr==i & oxy_match.quality1);% == 2 & oxy_match.quality2 == 2);
   
    press_aux=cond_match.ctdprs(aux);
    
    if isempty(aux)==0     
        if pri_sec_c==1
            cond_aux=cond_match.ctdcon1(aux);
            temp_aux=cond_match.ctdtmp1(aux);
        else
            cond_aux=cond_match.ctdcon2(aux);
            temp_aux=cond_match.ctdtmp2(aux);
            %oxyvolt_aux=cnv_dlp(i).oxvsec;
            %oxyvolt_aux=smooth(press_aux,oxyvolt_aux, 0.02,'loess');
        end;
    end;
    
    if isempty(aux1)==0
        if pri_sec_o==1
            oxyvolt_aux=oxy_match.ctdoxv1(aux1);         
            cond_oxy=oxy_match.ctdcon1(aux1);
            temp_oxy=oxy_match.ctdtmp1(aux1);
            press_oxy=oxy_match.ctdprs(aux1);        
        else
            oxyvolt_aux=oxy_match.ctdoxv2(aux1);
            cond_oxy=oxy_match.ctdcon2(aux1);
            temp_oxy=oxy_match.ctdtmp2(aux1);
            press_oxy=oxy_match.ctdprs(aux1);             
        end;
        sal_oxy=sw_salt(cond_oxy/sw_c3515,temp_oxy,press_oxy);  
    end;
   
  %value for the offset
  if isempty(aux)==0
  condc=(beta(1)*cond_aux)+(beta(2)*repmat(sta,length(cond_aux),1))+beta(3)+(beta(4)*press_aux);
  sal_aux=sw_salt(condc/sw_c3515,temp_aux,press_aux); 
  end 
   % correcting the oxygen (oxy fit 3.01 std dev)
   % calculating oxygen saturation
   if isempty(aux1)==0
%%%%%%%%%%% CMDR use seawater O2 saturation function
	oxsat=sw_satO2(temp_oxy,sal_oxy);
%%%%%%%%%%%
   % calibration coefficients for the oxygen
  % calibration coefficients for the oxygen
    if sta <=24,
         % calibration coefficients for station 1 - 24 the oxygen
         Soc=oxy_coeff1(1);
         Voffset=oxy_coeff1(2);
         A=oxy_coeff1(3);
         B=oxy_coeff1(4);
         C=oxy_coeff1(5);
         E=oxy_coeff1(6);
         p1=oxy_coeff1(7);
         stn=repmat(sta,length(oxyvolt_aux),1);
     end;
     if sta >=25,
         % calibration coefficients for stations 25 - end the oxygen
         Soc=oxy_coeff2(1);
         Voffset=oxy_coeff2(2);
         A=oxy_coeff2(3);
         B=oxy_coeff2(4);
         C=oxy_coeff2(5);
         E=oxy_coeff2(6);
         p1=oxy_coeff2(7);
         stn=repmat(sta,length(oxyvolt_aux),1);
     end;
  new_oxy=((Soc.*(oxyvolt_aux+Voffset))+(p1.*stn)).*...
	(1.0+(A.*temp_oxy)+(B.*temp_oxy.^2)+(C.*temp_oxy.^3)).*oxsat.*exp((E.*press_oxy)./(temp_oxy+273.15));
  end
  % saving the variables cond
  
  if isempty(aux)==0
  cond_match_cal.stnnbr(aux,1)=cond_match.stnnbr(aux); 
  cond_match_cal.niskinbtlpos(aux,1)=cond_match.niskinbtlpos(aux);
  cond_match_cal.sampnbr(aux,1)=cond_match.sampnbr(aux);
  cond_match_cal.btlsal(aux,1)=cond_match.btlsal(aux);
  cond_match_cal.quality1(aux,1)=cond_match.quality1(aux);
  cond_match_cal.quality2(aux,1)=cond_match.quality2(aux);
  cond_match_cal.ctdprs(aux,1)=press_aux;
  cond_match_cal.ctdtmp(aux,1)=temp_aux;
  cond_match_cal.ctdcon(aux,1)=condc;
  cond_match_cal.ctdsal(aux,1)=sal_aux;
  end
  % saving the variables oxy
  if isempty(aux1)==0
  oxy_match_cal.stnnbr(aux1,1)=oxy_match.stnnbr(aux1);
  oxy_match_cal.niskinbtlpos(aux1,1)=oxy_match.niskinbtlpos(aux1);
  oxy_match_cal.sampnbr(aux1,1)=oxy_match.sampnbr(aux1);
  oxy_match_cal.btloxy(aux1,1)=oxy_match.btloxy(aux1);
  oxy_match_cal.quality1(aux1,1)=oxy_match.quality1(aux1);
  oxy_match_cal.quality2(aux1,1)=oxy_match.quality2(aux1);
  oxy_match_cal.ctdprs(aux1,1)=press_oxy;
  oxy_match_cal.ctdtmp(aux1,1)=temp_oxy;
  oxy_match_cal.ctdcon(aux1,1)=cond_oxy;
  oxy_match_cal.ctdsal(aux1,1)=sal_oxy; 
  oxy_match_cal.ctdoxy(aux1,1)=new_oxy; 
   % converting the oxygen to micromol/kg
  oxy_aux=ox_units(new_oxy,sal_oxy,sw_ptmp(sal_oxy,temp_oxy,press_oxy,0),'mll2mmk');
  oxy_match_cal.ctdoxyumolkg(aux1,1)=oxy_aux; 
  end
 
%  
end
% Save the calibrated data structure.
save(db_file,'oxy_match_cal','-append');
save(db_file,'cond_match_cal','-append');
return