function theResult  = correct_autosal_drift(nominal_std,infile,outfile)
% CORRECT_AUTOSAL_DRIFT - correct for autosal drift with pre/post standards 
% 
% CTD Calibration toolbox
% 
% INPUT: 
%   nominal_cond_standard_water: scalar representing the nominal value for
%		standard water twice conductivity ratio.  This number changes
% 		for each batch of standard seawater.
%  salts: structure obtained as output from match_autosal_raw_dat or a
%		similar program.
%  outfile: path and name of the output file which will contain the 
%		corrected averaged bottle sample salinity.
%
% OUTPUT:
%   theResult: the data structure of corrected values.  This is
%		essentially a duplicate of the information in the outfile.
%
% DESCRIPTION:	
% 	This function is specific to the autosal configuration found on the
%	Ron Brown during the 2004 field season.  Modifications may be
%	necessary, but please rename accordingly.
%
% SEE ALSO:
%		read_autosal_raw		
% 		read_autosal_dat
%

% function theResult  = correct_autosal_drift_ab05(nominal_std,infile,outfile)

% 
% VERSION: 
%	$Revision: 1.6 $
%	$Date: 2008/02/18 19:31:48 $
%	$Id: correct_autosal_drift.m,v 1.6 2008/02/18 19:31:48 duncombe Exp duncombe $
%
% CHANGELOG: 
%   08-Jul-2004, Version 1.0
%		* Initial version.
%
%  2008/01/16 CMDR
%	* improve readability
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

error(nargchk(3,3,nargin))
warning off MATLAB:divideByZero
ldfile=['salfile=load(''',infile,''');'];
eval(ldfile);
salts2cor.station_id=salfile(:,1);
salts2cor.niskin_bottle=salfile(:,2);
salts2cor.samp_nbr=salfile(:,3);
salts2cor.tank_temp=salfile(:,10);
salts2cor.year=salfile(:,6);
salts2cor.month=salfile(:,5);
salts2cor.day=salfile(:,4);
salts2cor.hours=salfile(:,7);
salts2cor.minutes=salfile(:,8);
salts2cor.seconds=salfile(:,9);
salts2cor.cond_ratio=salfile(:,11)*2;
date_cond = datenum(salts2cor.year,salts2cor.month,salts2cor.day,salts2cor.hours,salts2cor.minutes,salts2cor.seconds);
%date_cond_stds = datenum(, , ,standards.hours,standards.minutes,standards.seconds);
%date_cond=datenum(aux_out(:,7),aux_out(:,8),aux_out(:,9),aux_out(:,10),aux_out(:,11),aux_out(:,12));

% Figure out where the standards start and stop relative to the bottle samples
standard1_idx=find(salts2cor.station_id==1000);
standard2_idx=find(salts2cor.station_id==1001);
figure
subplot(2,1,1) 
plot(date_cond(standard1_idx(1),1),mean(salts2cor.cond_ratio(standard1_idx)),'ok')
hold on
plot(date_cond(standard2_idx(length(standard2_idx)),1),mean(salts2cor.cond_ratio(standard2_idx)),'ok')
datetick('x','HH:MM')
x1=date_cond(standard1_idx(1),1);
x2=date_cond(standard2_idx(length(standard2_idx)),1)
y1=mean(salts2cor.cond_ratio(standard1_idx));
y2=mean(salts2cor.cond_ratio(standard2_idx));
beta=nominal_std-y1;
if isempty(y2) | isempty(x2)
	alfa = 0;
	warning('Autosal analysis does not contain a post calibration for drift correction.  No drift correction applied.')
else
	alfa=(y2-y1)/(x2-x1);
end
adjust0=(alfa*(date_cond(standard1_idx(1),1)-x1))+y1;
adjust=(alfa*(date_cond(standard2_idx(length(standard2_idx)),1)-x1))+y1;
subplot(2,1,2)
plot(date_cond(standard1_idx(1),1),(mean(salts2cor.cond_ratio(standard1_idx))-1.999)*100000,'ok')
hold on
plot(date_cond(standard2_idx(length(standard2_idx)),1),(mean(salts2cor.cond_ratio(standard2_idx))-1.999)*100000,'ok')
plot(x1,(y1-1.999)*100000,'*k')
plot(x2,(y2-1.999)*100000,'*k')
plot([x1 x2],[(adjust0-1.999)*100000 (adjust-1.999)*100000],'k--')
datetick('x','HH:MM')
title(['Standard alfa = ',num2str(alfa),  'beta = ',num2str(beta)])
saida(1,1)=alfa;
saida(1,2)=beta;
coeff=saida;
t0=date_cond(standard1_idx(1),1);
aux_date=date_cond-t0;
figure
for j=1:length(aux_date)
  correction=(coeff(1)*aux_date(j))+coeff(2)
  plot(aux_date(j),correction,'ok')
  datetick('x','HH:MM')
  hold on
  cond_ratio2_correct(j,1)=salts2cor.cond_ratio(j)+correction;
  plot(aux_date(j),salts2cor.cond_ratio(j)-cond_ratio2_correct(j,1),'or')
end
out_idx_cor=find(salts2cor.samp_nbr(:)<1000);
output_correct=[salts2cor.station_id(out_idx_cor),salts2cor.niskin_bottle(out_idx_cor) ,salts2cor.samp_nbr(out_idx_cor), cond_ratio2_correct(out_idx_cor,1),...
        sw_sals(cond_ratio2_correct(out_idx_cor,1)/2,salts2cor.tank_temp(out_idx_cor))];
fid=fopen(outfile,'w');
fprintf(fid,'%Station\t Niskin\t Sample_Bottle\t 2*Cond. Ratio Corrected Salinity\n');
fprintf(fid,'%d\t %d\t %d\t %1.5f %2f5\n',output_correct');
fclose(fid);

theResult.station=output_correct(:,1);
theResult.sample_bottle=output_correct(:,3);
theResult.cond_ratio=output_correct(:,4);
theResult.tank_temp=salts2cor.tank_temp(out_idx_cor);
theResult.niskin_bottle=salts2cor.niskin_bottle(out_idx_cor);
theResult.year=salts2cor.year(out_idx_cor);
theResult.month=salts2cor.month(out_idx_cor);
theResult.day=salts2cor.day(out_idx_cor);
theResult.hours=salts2cor.hours(out_idx_cor);
theResult.minutes=salts2cor.minutes(out_idx_cor);
theResult.seconds=salts2cor.seconds(out_idx_cor);
theResult.salinity=output_correct(:,5);
return

