%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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 ( 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
%
% 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
%
% 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 dataK.m
%
  if 1
  if ( exist('dataKft') == 2 )
    disp ' Loading Kearfott data.  Arrayname is ''dataKft'' '
    dataKft
    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;
    tusbl = datenum(usblTime)*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




