%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Load Science Payload Results into Matlab and synchronize
%          start times.
% AUTHOR:  Nicole Tervalon
% DATE:    01/07/12
% NOTES:   See the companion shell script cvt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% clear dataSim dataNav dataDC opts;

if ( exist('startTime') == 0 )
   startTime = 0;
end

%
% Get dataNav
%
%if ( exist('dataNav') == 0 )
    disp (' Loading Navigation Data, matrix = dataNav '),
   if ( exist('dataN') == 2 )
      dataN
   else
      disp (' Navigation Data Not Present ')
      return
   end
%end

if dataNav(1,1) > startTime
   startTime = dataNav(1,1);
   dataNav(:,1) = dataNav(:,1) - startTime;
   dataNav = NegTrunc(dataNav);
end

%
% Get and synchronize IPS data
%
  if ( exist('dataIPS4') == 2 )
    dataIPS4
    if( exist('ips4log')==1 & length(ips4log) > 20 )
      disp ' Loading IPS data '
      if (ips4log(1,1) - 100) > startTime
	startTime = ips4log(1,1)
      end
      ips4log(:,1) = ips4log(:,1) - startTime;
      ips4log = NegTrunc(ips4log);
    end
  elseif ( exist('dataIPS') == 2 )
    dataIPS
    if( exist('ips4log')==1 & length(ips4log) > 20 )
      disp ' Loading IPS data '
      if (ips4log(1,1) - 100) > startTime
	startTime = ips4log(1,1)
      end
      ips4log(:,1) = ips4log(:,1) - startTime;
      ips4log = NegTrunc(ips4log);
    end
  else
    disp (' No IPS data available. ')
  end  

%
% Get and synchronize Primary SBE 25 boardset data
%
  if ( exist('dataCTD1') == 2 )
    % For backward compatibility.
    disp (' Loading CTD data, matrix = seabirdctdlog')
    dataCTD1
    if (exist('seabirdctdlog') == 1 )
      if (seabirdctdlog(1,1) - 100) > startTime
          startTime = seabirdctdlog(1,1);
	  flow1=9;
      end
      seabirdctdlog(:,1) = seabirdctdlog(:,1) - startTime;
      ctdlog = NegTrunc(seabirdctdlog);
      time1 = ctdlog(:,time);
      temp1 = ctdlog(:,temperature);
      cond1 = ctdlog(:,conductivity);
      flowsensor1 = ctdlog(:,flow1);
      humidity = ctdlog(:,humidity);
      obs = ctdlog(:,OBS);
      oxygen = ctdlog(:,dissolvedO2);
    end
 elseif ( exist('dataCTD') == 2 )
      disp (' Loading primary SBE 25 boardset data ')
      dataCTD
    if (exist('ctdDriverctdlog') == 1 )
      if (ctdDriverctdlog(1,1) - 100) > startTime
          startTime = ctdDriverctdlog(1,1);
      end
      humidity = 8;
      flow1=9;
      ctdDriverctdlog(:,1) = ctdDriverctdlog(:,1) - startTime;
      ctdlog = NegTrunc(ctdDriverctdlog);
      time1 = ctdlog(:,time);
      temp1 = ctdlog(:,temperature);
      cond1 = ctdlog(:,conductivity);
      flowsensor1 = ctdlog(:,flow1);
      humidity = ctdlog(:,humidity);
      obs = ctdlog(:,OBS);
      oxygen = ctdlog(:,dissolvedO2);
    elseif (exist('ctdDriver') == 1 )
      if (ctdDriver(1,1) - 100) > startTime
          startTime = ctdDriver(1,1)
      end
      ctdDriver(:,1) = ctdDriver(:,1) - startTime;
      ctdlog = NegTrunc(ctdDriver);
      time1 = ctdlog(:,time);
      temp1 = ctdlog(:,temperature);
      cond1 = ctdlog(:,conductivity);
      flowsensor1 = ctdlog(:,flow1);
      humidity = ctdlog(:,humidity);
      obs = ctdlog(:,OBS);
      oxygen = ctdlog(:,dissolvedO2);
    end
  else
      %Skip it!
      disp(' Primary SBE 25 boardset data not found ')
  end

%
% Get and synchronize Secondary SBE 25 boardset data
%
  if ( exist('dataCTD2') == 2 )
      disp (' Loading secondary SBE 25 boardset data ')
      dataCTD2
    if (exist('ctdDriver2ctdlog') == 1 )
      if (ctdDriver2ctdlog(1,1) - 100) > startTime
          startTime = ctdDriver2ctdlog(1,1)
      end
      ctdDriver2ctdlog(:,1) = ctdDriver2ctdlog(:,1) - startTime;
      ctdlog2 = NegTrunc(ctdDriver2ctdlog);
      time2 = ctdlog2(:,time);
      temp2 = ctdlog2(:,temperature);
      cond2 = ctdlog2(:,conductivity);
      flowsensor2 = ctdlog2(:,flow2);
    elseif (exist('ctdDriver2') == 1 )
      if (ctdDriver2(1,1) - 100) > startTime
          startTime = ctdDriver2(1,1)
      end
      ctdDriver2(:,1) = ctdDriver2(:,1) - startTime;
      ctdlog2 = NegTrunc(ctdDriver2);
      time2 = ctdlog2(:,time);
      temp2 = ctdlog2(:,temperature);
      cond2 = ctdlog2(:,conductivity);
      flowsensor2 = ctdlog2(:,flow2);
    end
  else
      %Skip it!
      disp(' Secondary SBE 25 boardset data not found ')
  end

%
% Get and synchronize ISUS data
%

if ( exist('dataIsus') == 2 )
   disp (' Loading ISUS data, matrix = isuslog ')
   dataISUS
   % truncate ISUS data if it started recording before the navigation data did
   isuslog(:,1) = isuslog(:,1) - startTime;
   isuslog = NegTrunc(isuslog);
else
   disp(' No ISUS data found ')
end
 
  
%
% Get and synchronize psa916log
%
  if ( exist('dataPSA') == 2 )
    disp ' Loading PSA916 data, matrix = psa916 '
    dataPSA
    psa916(:,1) = psa916(:,1) - startTime;
    psa916 = NegTrunc(psa916);
  else
    disp (' No PSA916 data available. ')  
  end
  
%
% Get and synchronize Tritech log
%
  if ( exist('datapa200LR') == 2 )
    disp ' Loading Tritech data '
    datapa200LR
    if( exist('PA200LR') == 1 & length(PA200LR) > 20 )
      PA200LR(:,time) = PA200LR(:,time) - startTime;
      PA200LR = NegTrunc(PA200LR);
      rangetritech = PA200LR(:,range);
    end
  else
    disp (' No Tritech data available. ')  
  end
  
  
  disp ' Data Loading Complete.'
  return







