function bbp_spectra=bb9cor_BOSS(beta117_data,apg_data,sal_data,lambdas)

% function bbp_data=bb9cor_BOSS(beta117_data,sal_data,lambdas)
% Correct bb9 data according to "E. Boss Method" as follow:
% for each wavelength:
% bbp=1.1(Beta(117deg)-Beta_salt_water(117deg))*exp(La)
% data arrays format is (line,sample)
%
% inputs:- beta140_data(1 to 9,1 to n_sample) : beta(140) data from bb9
%         - a_data : total absorption from ac-9
%		  - sal_data(1 to n_sample) : salinity data in PSU
%        - lambdas(1 to 9) : wavelength channels ex:[407 439 485 507 527 594 651 715 878]  
% (order of wavelengths channel must be the same is beta117_data and lambdas)
%
% output: - new_bb_data(1 to 9,1 to n_sample)
%
% Emmanuel Boss UMaine 2012

%apply cals
n_sample=length(sal_data);
wl=[407 439 485 507 527 594 651 715 878];
slope=[3.456 2.005 1.868 1.563 1.554 1.161 1.01 0.8359 0.6604]*10^-5;
darks=[50 56 53 57 54 55 52 53];
beta117_data=(beta117_data-ones(1,n_sample)*darks).*(ones(1,n_sample)*slopes);

%init array
betasw_117=struct(['ch_' num2str(lambdas(1))],0,['ch_' num2str(lambdas(2))],0,...
           ['ch_' num2str(lambdas(3))],0,['ch_' num2str(lambdas(4))]...
           ,0,['ch_' num2str(lambdas(5))],0,['ch_' num2str(lambdas(6))],0,['ch_' num2str(lambdas(7))],0,['ch_' num2str(lambdas(8))],0,['ch_' num2str(lambdas(9))],0);
bbsw=struct(['ch_' num2str(lambdas(1))],0,['ch_' num2str(lambdas(2))],0,...
           ['ch_' num2str(lambdas(3))],0,['ch_' num2str(lambdas(4))]...
           ,0,['ch_' num2str(lambdas(5))],0,['ch_' num2str(lambdas(6))],0,['ch_' num2str(lambdas(7))],0,['ch_' num2str(lambdas(8))],0,['ch_' num2str(lambdas(9))],0);
        
n_sample=size(sal_data,2);

%compute beta(140) salt water at specifies wavelengths
%compute bb salt water at specifies wavelengths
%compute new bb
for i=1:9
  for j=1:n_sample 
   eval(['betasw_117.ch_' num2str(lambdas(i)) '=1.38*(lambdas(i)/500)^(-4.32)*(1+0.3*sal_data(j)/37)*((1+(cos((12/18)*pi))^2)*(1-0.09)/(1+0.09))*10^(-4);']);
%   eval(['bbsw.ch_' num2str(lambdas(i)) '=1.38*(lambdas(i)/500)^(-4.32)*(1+0.3*sal_data(j)/37)*2*pi*(1+(1-0.09)/(3*(1+0.09)))*10^(-4);']);
   eval(['bbp_data(i,j)=1.1*(beta117_data(i,j)-betasw_140.ch_' num2str(lambdas(i)) ').*exp(0.0391*apg_data) ';'] );
	end
end

return

