%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Load DorOdyssey Simulation Results into Matlab and synchronize
%          start times.
% AUTHOR:  Rob McEwen
% DATE:    99/12/20
% NOTES:   See the companion shell script cvt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  clear dataSim dataNav dataDC opts;
%
% Get dataNav
%
  if ( exist('dataN')   == 2 )
    disp ' Loading Navigation Data. ',
    dataN
  else
    disp ' Navigation Data Not Present.'
    return
  end
%
% Get dataDC
%
  if ( exist('dataD')   == 2 )
    disp ' Loading Dynamic Control Data. ',
    dataD
  else
    disp ' Dynamic Control Data Not Present.'
    return
  end
%
% Find latest start time.
%
  startTime = 0;
  if dataNav(1,1) > startTime; startTime = dataNav(1,1);end
  if dataDC(1,1)  > startTime; startTime = dataDC(1,1);end
%
% Reset time in dataNav and dataDC
%
  dataNav(:,1) = dataNav(:,1) - startTime;
  dataDC(:,1)  = dataDC(:,1)  - startTime;
  dataNav = NegTrunc(dataNav);
  dataDC  = NegTrunc(dataDC);
%
% Get and synchronize dataS.
%
  if ( exist('dataS') == 2 )
    disp ' Loading Simulation Data '
    dataS
    % Comment the below out if NoAutoTimeStamp is specified in SimulationLog.cc.
    dataSim(:,1) = dataSim(:,1) - startTime;
  end
%
% Get and synchronize ctdlog
%
if 0
  if ( exist('dataCTD') == 2 )
    disp ' Loading CTD data '
    dataCTD
    if (exist('ctdlog') == 1 )
      ctdlog(:,1) = ctdlog(:,1) - startTime;
      ctdlog = NegTrunc(ctdlog);
    elseif (exist('ctd') == 1 )
      %
      % For backward compatibility.
      ctd(:,1) = ctd(:,1) - startTime;
      ctd = NegTrunc(ctd);
      ctdlog = ctd;
    else
      %Skip it!
      disp(' CTD data not found on stack.')
    end

  end
end

%
% Get and synchronize parosci
%
  if ( exist('dataPS') == 2 )
    disp ' Loading PS data '
    dataPS
    parosci(:,1) = parosci(:,1) - startTime;
    parosci = NegTrunc(parosci);
  end
%
% Get and synchronize psa916log
%
  if ( exist('dataPSA') == 2 )
    disp ' Loading PSA data '
    dataPSA
    if( exist('psa916')==1 )  psa916log = psa916; end
    psa916log(:,1) = psa916log(:,1) - startTime;
    psa916log = NegTrunc(psa916log);
  end
%
% Get and synchronize kvhInclin
%
  if ( exist('dataINC') == 2 )
    disp ' Loading INC data '
    dataINC
    kvhInclin(:,1) = kvhInclin(:,1) - startTime;
    kvhInclin = NegTrunc(kvhInclin);
  end
%
% Get and synchronize kvhCompass
%
  if ( exist('dataCOM') == 2 )
    disp ' Loading COM data '
    dataCOM
    kvhCompass(:,1) = kvhCompass(:,1) - startTime;
    kvhCompass = NegTrunc(kvhCompass);
  end
%
% Get and synchronize Crossbow
%
  clear dataCB
  if ( exist('dataCrossB') == 2 )
    disp ' Loading Crossbow data '
    dataCrossB
    dataCB(:,1) = dataCB(:,1) - startTime;
    dataCB = NegTrunc(dataCB);
  elseif ( exist('dataCB') == 2 )
    %
    %For backward compatibility.  This was changed because it's confusing to
    %have a file whose name is the same as the array it contains.
    disp ' Loading Crossbow data '
    dataCB;
    dataCB(:,1) = dataCB(:,1) - startTime;
    dataCB = NegTrunc(dataCB);
  end
%
% Get and synchronize gps
%
  if ( exist('dataGPS') == 2 )
    disp ' Loading GPS data '
    dataGPS
    gps(:,1) = gps(:,1) - startTime;
    gps = NegTrunc(gps);
  end
%
% Get and synchronize tailcone
%
  if ( exist('dataTailCone') == 2 )
    disp ' Loading tailcone data '
    dataTailCone
    dataTC(:,1) = dataTC(:,1) - startTime;
    dataTC = NegTrunc(dataTC);
  end
%
% Get and synchronize lbl
%
  if ( exist('dataPS8') == 2 )
    disp ' Loading lbl PS8000 data.  Arrayname is ''dataPS8000''. '
    dataPS8
    dataPS8000(:,1) = dataPS8000(:,1) - startTime;
    dataPS8000 = NegTrunc(dataPS8000);
  end
%
% Get and synchronize metrabyte
%
  v2a = 2.24;                 %Volts to amperes conversion factor;
                              %Nicole 00/6.  Moved to MetraByte.cc in July.

  if ( exist('dataMBL') == 2 )
    disp ' Loading metrabyte data.  Arrayname is ''metrabyte'' '
    dataMBL
    metrabyte(:,1) = metrabyte(:,1) - startTime;
    metrabyte = NegTrunc(metrabyte);
    metrabyte(:,current) = metrabyte(:,current);
  end
%
% Get and synchronize dataDvl
%
  if ( exist('dataDVL') == 2 )
    disp ' Loading DVL data.  Arrayname is ''dataDvl'' '
    dataDVL
    dataDvl(:,1) = dataDvl(:,1) - startTime;
    dataDvl = NegTrunc(dataDvl);
  end

%
% Nicole's CTD processing code.
% 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 )
      fprintf('  Matrix = %s\n','ctdlog');
      if (seabirdctdlog(1,1) - 100) > startTime
          startTime = seabirdctdlog(1,1);
	  flow1=9;
      end
      seabirdctdlog(:,1) = seabirdctdlog(:,1) - startTime;
      ctdlog = NegTrunc(seabirdctdlog);
    end
 elseif ( exist('dataCTD') == 2 )
      disp (' Loading primary SBE 25 boardset data ')
      dataCTD
    if (exist('ctdDriverctdlog') == 1 )
      fprintf('  Matrix = %s\n','ctdlog');
      if (ctdDriverctdlog(1,1) - 100) > startTime
          startTime = ctdDriverctdlog(1,1);
      end
    % name appropriate data columns to make up for using old driver
      humidity = 8;
      flow1=9;
      dissolvedO2=11;
      OBS=10;
      ctdDriverctdlog(:,1) = ctdDriverctdlog(:,1) - startTime;
      ctdlog = NegTrunc(ctdDriverctdlog);
    elseif (exist('ctdDriver') == 1 )
      fprintf('  Matrix = %s\n','ctdlog');
      if (ctdDriver(1,1) - 100) > startTime
          startTime = ctdDriver(1,1)
      end
      ctdDriver(:,1) = ctdDriver(:,1) - startTime;
      ctdlog = NegTrunc(ctdDriver);
    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
   % For backward compatibility.
    if (exist('ctdDriver2ctdlog') == 1 )
      fprintf('  Matrix = %s\n','ctdlog2');
      if (ctdDriver2ctdlog(1,1) - 100) > startTime
          startTime = ctdDriver2ctdlog(1,1)
      end
      ctdDriver2ctdlog(:,1) = ctdDriver2ctdlog(:,1) - startTime;
      ctdlog2 = NegTrunc(ctdDriver2ctdlog);
   % For backward compatibility.
    elseif (exist('ctdDriver2') == 1 )
      fprintf('  Matrix = %s\n','ctdlog2');
      if (ctdDriver2(1,1) - 100) > startTime
          startTime = ctdDriver2(1,1)
      end
      ctdDriver2(:,1) = ctdDriver2(:,1) - startTime;
      ctdlog2 = NegTrunc(ctdDriver2);
    elseif (exist('seabirdctdlog') == 1 )
      fprintf('  Matrix = %s\n','ctdlog2');
      if (seabirdctdlog(1,1) - 100) > startTime
          startTime = seabirdctdlog(1,1);
      end
	  flow2=9;
      seabirdctdlog(:,1) = seabirdctdlog(:,1) - startTime;
      ctdlog2 = NegTrunc(seabirdctdlog);
    else
      fprintf('Appropriate secondary matlab file not found\n')
    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
   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 dataFC
%
  if ( exist('dataFuelCell') == 2 )
    disp ' Loading fuel cell data.  Arrayname is ''dataFC'' '
    dataFuelCell
    dataFC(:,1) = dataFC(:,1) - startTime;
    dataFC = NegTrunc(dataFC);
  end
%
% Get and synchronize dataO
%
  if ( exist('dataOct') == 2 )
    disp ' Loading Octans data.  Arrayname is ''dataO'' '
    dataOct
    dataO(:,1) = dataO(:,1) - startTime;
    dataO = NegTrunc(dataO);
  end
%
% Get and synchronize Bluefin battery data
%
  if ( exist('dataBFL') == 2 )
    disp ' Loading Bluefin battery data.  Arrayname is ''bluefinBattLog'' '
    dataBFL
    bluefinBattLog(:,1) = bluefinBattLog(:,1) - startTime;
    bluefinBattLog = NegTrunc(bluefinBattLog);
  end
%
% Get and synchronize dataK.m
%
  if 1
  if ( exist('dataKft') == 2 )
    disp ' Loading Kearfott data.  Arrayname is ''dataK'' '
    dataKft
    dataK(:,1) = dataK(:,1) - startTime;
    dataK = NegTrunc(dataK);
  end
  if ( exist('dataK') == 2 )
    disp ' Loading Kearfott data.  Arrayname is ''dataK'' '
    dataK
    dataK(:,1) = dataK(:,1) - startTime;
    dataK = NegTrunc(dataK);
  end
  end
%
% Get and synchronize waypoints
%
  if ( exist('waypoint') == 2 )
    disp ' Loading waypoint data.  Arrayname is ''dataWP'' '
    waypoint
    nlines = 4;

    dim    = length(dataWPstr(:,1));

    if ( mod(dim,nlines) ~= 0 )

      dataWP(1,1) = cutnum(dataWPstr(1,:),6);
      dataWP(1,2) = cutnum(dataWPstr(2,:),6);
      dataWP(1,3) = cutnum(dataWPstr(2,:),8);

      for i = 1:dim/nlines
        j = (i-1)*nlines;
        inext = i+1;
        dataWP(inext,1) = cutnum(dataWPstr(j+4,:),5);
        dataWP(inext,2) = cutnum(dataWPstr(j+3,:),5);
        dataWP(inext,3) = cutnum(dataWPstr(j+3,:),7);
      end
    else
      disp(' Error processing dataWPstr.  Data is missing.');
    end

    %dataWP(:,1) = dataWP(:,1) - startTime;
    %dataWP = NegTrunc(dataWP);
  end
%
% Get USBL data.
%
  if (   (exist('dataUsbl') == 2)   &   (exist('dataUsblTime') == 2)  )
    disp ' Loading USBL data.  Arraynames are ''usbl'' and ''tusbl'''
    dataUsbl;
    dataUsblTime;
    % Synchronize tusbl with the qnx vehicle clock:
    tusbl = (datenum(usblTime) - datenum('1-Jan-1970 00:00:00'))*24*3600;
    %tusbl(:,1) = tusbl(:,1) - startTime;
    %tusbl = NegTrunc(tusbl);
  end

  disp ' Data Loading Complete.'
  return



if ( exist('dataPS')  == 2 ), disp ' Loading ParoSci Data. ',     dataPS, end
if ( exist('dataPSA') == 2 ), disp ' Loading PSA916 Data. ',      dataPSA, end
if ( exist('dataGPS') == 2 ), disp ' Loading GPS Data. ',         dataINC, end
if ( exist('dataINC') == 2 ), disp ' Loading KVH Inclo Data. ',   dataINC, end
if ( exist('dataCOM') == 2 ), disp ' Loading KVH Compass Data. ', dataCOM, end




