% Tahoe data (29 May 2003)
% Process raw merger file (*.mer)
% apply AC9 field offset, TS corr, lag, scattering.
% compute bbt from ECOVSF with attenuation correction
% type: dis-dissolved, tot-totals

%function[]=Bisc_iop_proc(path,type)
clear all


path='../iop_files_b';

[file_list,n_file]=list_file(path);

fprintf('\r\n %d file(s) selected \r\n',n_file-2)

for i=3:n_file
   close all;
   file_name_current_in=file_list(i,:);
   
   % get file name, remove empty characters
   fname_length=size(file_name_current_in,2);
   j=0;
   while file_name_current_in(1,fname_length-j)==' '
      file_name_current_in=file_name_current_in(1,1:fname_length-j-1);
   	j=j+1;   
   end
   fname_length=size(file_name_current_in,2);
      
   % load selected file
   disp('loading input file...')
   disp(file_name_current_in);
   [hdr_in,data_in]=rdctd([path,'\',file_name_current_in]);
   
   %find variables that need to be corrected
   hdr_in=lower(hdr_in);
   ind_time=strmatch('time(s)',hdr_in);
   ind_depth=strmatch('pressure(db)',hdr_in);
   ind_apg412=strmatch('apg412',hdr_in);
   ind_apg676=strmatch('apg676',hdr_in);
   ind_bp440=strmatch('bp440',hdr_in);
   ind_ag412=strmatch('ag412',hdr_in);
   ind_press=strmatch('pressure(db)',hdr_in);
   ind_temp=strmatch('temperature(c)',hdr_in);
   ind_sal=strmatch('salinity(psu)',hdr_in);
   ind_con=strmatch('conductivity(ms)',hdr_in);
   ind_LSS=strmatch('lss(v)',hdr_in);
   ind_chl=strmatch('chl(v)',hdr_in);
   ind_cdm=strmatch('cdom(v)',hdr_in);
   ind_eco=strmatch('bb470eco',hdr_in);
   
   if (isempty(ind_apg412)+isempty(ind_ag412)==2)
       type='oth';
   elseif (isempty(ind_ag412)+isempty(ind_ag412)==2)
       type='tot';
   else
       type='dis';
   end
    
   
   %bin all data by pressure
   [dat,prr2]=binpre(data_in,data_in(ind_depth,:),floor(min(data_in(ind_depth,:))),floor(max(data_in(ind_depth,:))),1,2);
   
   dat(ind_depth,:)=prr2;
   %derived product if files with measured totals:
   if type=='tot' 
       a_chl=(dat(ind_apg676,:)-(26/65)*dat(ind_apg676-1,:))/0.014;
       bb_ratio=(dat(ind_eco,:)-2.3884e-004)./(0.5*(dat(ind_bp440,:)+dat(ind_bp440+1,:)));
   end
   if type=='dis'
       wlac9=[412 440 488 510 532 555 660 676];
       m=length(dat(ind_cdm,:));
       options(1)=0;options(2)=1e-6;
       for i=1:m
           outag(i,:)=fmins('least_squares_ag',[dat(ind_ag412+1,i),0.014],options,[],dat(ind_ag412:ind_ag412+7,i)',wlac9);   
       end
       S_a_g(i)=outag(i,2);
   end
       
          
   %regrouping ctd and ac9 data and building new header
    hdr_out=hdr_in;
	if type=='tot'
	   hdr_out=str2mat(hdr_out,'a_chl');
	   hdr_out=str2mat(hdr_out,'bb_ratio');
   end

	if type=='dis'
	hdr_out=str2mat(hdr_out,'S_a_g');
   end   
   
    
   if type=='tot' 
      data_out=[dat;a_chl;bb_ratio;];
   end
   
   if type=='dis'
      data_out=[dat;S_a_g];
   end   
   
   if type=='oth'
      data_out=[dat];
   end   
      
   file_name_current_out=[file_name_current_in(1:fname_length-4),'.bin'];
   fprintf('\nwriting to file: %s\n',file_name_current_out);
   wrtmerg(file_name_current_out,data_out,hdr_out);
	fprintf('done\n');
   clear bp
	end

   fprintf('\nALL DONE\n');  

   