% 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='.\merged_files';

[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_a412=strmatch('a412',hdr_in);
   ind_c412=strmatch('c412',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('eco_100',hdr_in);
   
   
   %pressure tare = P with top of the cage at surface + 90 cm to the ac-9 intake.
   data_in(ind_press,:)= data_in(ind_press,:)+1.75+0.9; 
   
   %ac9 cal correction
   fprintf('\n');
   disp('Doing AC9 field cal correction...')
   data_in=apply_cal_offset_to_data_array(hdr_in,data_in,'Tahoe_274cal.txt','');
   fprintf('done\r\n');
   
   %ac9 lag (flow rate + 3.2 due to wetproce.exe)
   %disp('Doing AC9 lag correction...')
   %[lag_one,lag_two]=getlag_from_flowrate(hdr_in,data_in,0.4);
   %fprintf('AC9 lag is %f \n',lag_one);
   %[data_in,new_n_sample]=correct_lag_on_data_array2(data_in,lag_one+3.2,ind_a412,ind_a412+8);
   %fprintf('done\r\n');
   
   %ac9 TS correction Tcal=25.5
   disp('Doing AC9 TS correction...')
   disp('using cal temp 25.5C')
   a_data=data_in(ind_a412:ind_a412+8,:);
   c_data=data_in(ind_c412:ind_c412+8,:);
   [TS_corr_a_data,TS_corr_c_data]=tscorr(data_in(ind_temp,:),data_in(ind_sal,:),a_data,c_data,25.5);
   a715_ts=TS_corr_a_data(end,:);
   c715_ts=TS_corr_c_data(end,:);
   fprintf('done\r\n');
   
   %determine if dissolved or particulate
   if median(c_data(8,:))<0.04
      type='dis' % type: dis-dissolved, tot-totals
  else
      type='tot';
  end

   
   %Scattering correction on apg if total
   if type=='tot'
   disp('Doing AC9 scattering correction...')   
   [apg,reff]=ascatcr3(TS_corr_a_data,TS_corr_c_data,3);
   fprintf('done\r\n');
   end
   
  
   % cpg
   cpg=TS_corr_c_data(1:end-1,:);
   % bp calculation if total
   if type=='tot'
   for i=1:8
   bp(i,:)=TS_corr_c_data(i,:)-apg(i,:);
	end
	end   
   
   % ag and cg
   if type=='dis'
      ag=data_in(ind_a412:ind_a412+7,:);
      cg=data_in(ind_c412:ind_c412+7,:);
   end
   
   %ctd data
   ctd_data=[data_in(ind_time,:);data_in(ind_press,:);data_in(ind_temp,:);data_in(ind_sal,:);data_in(ind_con,:);data_in(ind_chl,:);data_in(ind_cdm,:)];
   
   %LSS data (voltage and NTUs)
   LSS_data=[data_in(ind_LSS,:);data_in(ind_LSS,:)*1.358-0.017];
        
   %regrouping ctd and ac9 data and building new header
    hdr_out='Time(s)';
	hdr_out=str2mat(hdr_out,'Pressure(db)');
	hdr_out=str2mat(hdr_out,'Temperature(C)');
	hdr_out=str2mat(hdr_out,'Salinity(PSU)');
	hdr_out=str2mat(hdr_out,'Conductivity(ms)');
	hdr_out=str2mat(hdr_out,'Chl(V)');
	hdr_out=str2mat(hdr_out,'CDOM(V)');
	hdr_out=str2mat(hdr_out,'LSS(V)');
	hdr_out=str2mat(hdr_out,'LSS(NTU)');

	if type=='tot'
	hdr_out=str2mat(hdr_out,'apg412');
	hdr_out=str2mat(hdr_out,'apg440');
	hdr_out=str2mat(hdr_out,'apg488');
	hdr_out=str2mat(hdr_out,'apg510');	
	hdr_out=str2mat(hdr_out,'apg532');
	hdr_out=str2mat(hdr_out,'apg555');
	hdr_out=str2mat(hdr_out,'apg650');
   hdr_out=str2mat(hdr_out,'apg676');
   hdr_out=str2mat(hdr_out,'bp412');
	hdr_out=str2mat(hdr_out,'bp440');
	hdr_out=str2mat(hdr_out,'bp488');
	hdr_out=str2mat(hdr_out,'bp510');
	hdr_out=str2mat(hdr_out,'bp532');
	hdr_out=str2mat(hdr_out,'bp555');
	hdr_out=str2mat(hdr_out,'bp650');
	hdr_out=str2mat(hdr_out,'bp676');
	hdr_out=str2mat(hdr_out,'cpg412');
	hdr_out=str2mat(hdr_out,'cpg440');
	hdr_out=str2mat(hdr_out,'cpg488');
	hdr_out=str2mat(hdr_out,'cpg510');
	hdr_out=str2mat(hdr_out,'cpg532');
	hdr_out=str2mat(hdr_out,'cpg555');
	hdr_out=str2mat(hdr_out,'cpg650');
   hdr_out=str2mat(hdr_out,'cpg676');
   hdr_out=str2mat(hdr_out,'reff');
   hdr_out=str2mat(hdr_out,'a715_TS');
	hdr_out=str2mat(hdr_out,'c715_TS');
	end

	if type=='dis'
	hdr_out=str2mat(hdr_out,'ag412');
	hdr_out=str2mat(hdr_out,'ag440');
	hdr_out=str2mat(hdr_out,'ag488');
	hdr_out=str2mat(hdr_out,'ag510');
	hdr_out=str2mat(hdr_out,'ag532');
	hdr_out=str2mat(hdr_out,'ag555');
	hdr_out=str2mat(hdr_out,'ag650');
   hdr_out=str2mat(hdr_out,'ag676');
   hdr_out=str2mat(hdr_out,'cg412');
	hdr_out=str2mat(hdr_out,'cg440');
	hdr_out=str2mat(hdr_out,'cg488');
	hdr_out=str2mat(hdr_out,'cg510');
	hdr_out=str2mat(hdr_out,'cg532');
	hdr_out=str2mat(hdr_out,'cg555');
	hdr_out=str2mat(hdr_out,'cg650');
    hdr_out=str2mat(hdr_out,'cg676');
    hdr_out=str2mat(hdr_out,'a715_TS');
	hdr_out=str2mat(hdr_out,'c715_TS');
   end   
   
   hdr_tmp=hdr_out;  
 
   if type=='dis' 
      data_out=[ctd_data;LSS_data;ag;cg;a715_ts;c715_ts];
   end
   
   if type=='tot'
      data_out=[ctd_data;LSS_data;apg;bp;cpg;reff;a715_ts;c715_ts];
   end   
      
   hdr_tmp=lower(hdr_tmp);
   ind_time=strmatch('time',hdr_tmp);
   ind_sal=strmatch('sal',hdr_tmp);
   ind_apg=strmatch('apg412',hdr_tmp);
   ind_ag=strmatch('ag412',hdr_tmp);
    
   % Compute bb from blue(470) ECOVSF
   if isempty(ind_eco)==0
	   fprintf('ECOVSF correction in progress...\n');
       if type=='dis' 
  	      apg_470_data=(data_out(ind_ag+1,:)+data_out(ind_ag+1,:))/2;
       end
       if type=='tot'
          apg_470_data=(data_out(ind_apg+1,:)+data_out(ind_apg+1,:))/2;
       end
        [bbeco_470_data]=get_bb_from_ecovsf(470,apg_470_data',transpose(data_in(ind_eco:ind_eco+2,:)));
        bbeco_470_data=bbeco_470_data';
     	fprintf('done\n');
   end
     
   % Save data
   
   if isempty(ind_eco)==0
      hdr_out=str2mat(hdr_out,'bb470eco');
      data_out=[data_out;bbeco_470_data];
   end
   
   file_name_current_out=[file_name_current_in(1:fname_length-4),'.iop'];
   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');  
     
     
     
   
   
   
   