%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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
% CHANGE LOG: 
%       1. May 2005, rsm.  Added code to read the .logs directly into
%          Matlab, if they're present.
%       2. 25May05 Rewritten as loops with indirect references to the file
%          names. 
%       3. 26May 05 Added the ability to detect and read the Netcdf versions.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  clear dataSim dataNav dataDC opts ldnames;
%
% Cell array of names.  The ascii files are (or used to be) generated by the
% shell script "cvt".
%
%              log file name        ascii file name     Matlab array name
  ldnames = { 'navigation',        'dataN',            'dataNav';
              'dynamicControl',    'dataD',            'dataDC';
	      'simulation',        'dataS',            'dataSim';
	      'parosci',           'dataPS',           'parosci';
	      'psa916log',         'dataPSA',          'psa916log';
	      'echoSounder',       'echoSounderAscii', 'dataES';
	      'camera',            'cameraAscii',      'dataCam';
	      'terrainAid',        'terrainAidAscii',  'dataTA';
	      'terrainNav',        'terrainNavAscii',  'dataTN';
	      'PA200LRlog',        'dataPA200LR',      'PA200LR';
	      'crossbow',          'dataCrossB',       'dataCB';
	      'gps',               'dataGPS',          'gps';
	      'tailCone',          'dataTailCone',     'dataTC';
	      'PS8000',            'dataPS8',          'dataPS8000';
	      'metrabytelog',      'dataMBL',          'metrabyte';
	      'dvl',               'dataDVL',          'dataDvl';
	      'dvlSide',           'dataDVLSIDE',      'dataDvlSide';
	      'Env',               'dataE',            'dataEnv';
	      'isuslog',           'dataIsus',         'isuslog';
	      'fuelcell',          'dataFuelCell',     'dataFC';
	      'octans',            'dataOct',          'dataO';
	      'bluefinBattlog',    'dataBFL',          'bluefinBattLog';
          'kearfott',          'dataKft',          'dataK';
	      'ctdDriver',         'dataCTD',          'ctdDriverctdlog';
	      'ctdDriver2',        'dataCTD2',         'ctdDriver2ctdlog';
	      'biolume',           'dataBiolume',      'biolume';
	      'sonardyne',         'dataUsbl',         'usbllog';
	      'Gulper',            'dataGulper',       'gulper';
	      'hydroscatlog',      'dataHS2',          'hydroscatlog';
	      'AdaptiveSampler',   'NslashA',          'asp';
	      'SysDataCpuPrio',    'NslashA',          'cpuprio';
	      'SysDataMemPrio',    'NslashA',          'memprio';
  	      'idt',               'NslashA',          'idt';
  	      'idt_fwd',           'NslashA',          'idt_fwd';
  	      'WaypointWall',      'NslashA',          'wpw';
  	      'WaypointBottom',    'NslashA',          'wpb';
  	      'side_idt',          'NslashA',          'side_idt';
  	      'deltatlog_fwd',     'NslashA',          'idt_fwd';
  	      'deltatlog',         'NslashA',          'idt';
  	      'epiBattLog',        'NslashA',          'epi';
            };
%	      
% Read in the data.  Include a few special cases in oldasciiname where the .m
% file name is the same as the array name, for backward compatibility.  
localpath = pwd;
for( ii=1:length(ldnames) )
  logname      = [char(ldnames(ii,1)) '.log'];
  ncname       = [char(ldnames(ii,1)) '.nc'];
  asciiname    = [char(ldnames(ii,2)) '.m'];
  %  
  % Matlab now is case insensitive for functions or scripts.  So, it can't
  % tell the difference between the data file PS8000.m and the plot script
  % ps8000.m in the review directory (see below).  So, add the whole path:
  oldasciiname = [ localpath '/' char(ldnames(ii,1)) '.m']; 
  if( exist(logname) == 2 )
    filedata = dir(logname);
    if( filedata.bytes )
      %
      % First check for the native binary file.  Read it in, if it's there.
      fprintf('Loading binary file %s.\n',logname);
      clear byteEnd byteStart data fid i nBytes nCol nRecords recordLength s time
      readlog;                  %beware that readlog changes variables like "i"
      ldnames(ii,3) = cellstr(mnemonic);   %readlog creates  "mnemonic"
    end
  elseif(  exist( ncname )  == 2 )
    %
    % Next check for a Net Cdf file.  Read it in, if it's there.
    fprintf('Loading Netcdf file %s.\n',ncname);
    clear nc;
    nc = netcdf(ncname);
    ncvars = var(nc);
    %
    % Put variable names on the Matlab stack:
    nCol     = length(ncvars);
    clear nRecords;
    for( i=1:nCol )
      eval([ char(name(ncvars{i}))  ' = '  num2str(i) ';']);
      nRecords(i) = length(nc{name(ncvars{i})});
    end
    %
    % Verify that each variable has the same number of records:
    if( ~isempty(find(nRecords~=nRecords(1))) )
      fprintf('loadData: Error reading %s.  Unequal dimensions.', ncname);
      continue;
    else
      %
      % Make it a scalar, since all dims are equal.
      nRecords = nRecords(1);
    end
    %
    % Pre-allocate memory:
    eval([ char(ldnames(ii,3)) ...
	  '(1:nRecords,1:nCol) = ones(nRecords,nCol) * NaN;']);
    %
    % Load the data into the array:
    for( i=1:nCol )
      eval([ char(ldnames(ii,3)) '(:,' num2str(i) ') = '  ...
	    'nc{name(ncvars{' num2str(i) '})}(:);']);
    end
  elseif(  exist( asciiname )  == 2 )
    %
    % Now look for an Ascii .m version, and read it in:
    fprintf('Loading ascii file %s.\n', asciiname );
    eval( asciiname(1:end-2) );    %Remove the .m extension
    %
    % Put in code to parse out the array name, such as "dataN", and record it
    % in ldnames(i,3).  For now, rely on the hardcoded third column of the
    % ldnames array. 
    %
  elseif(  exist( oldasciiname )  == 2 )
    %
    % This is for backward compatibility with very old stuff.
    %
    eval( [ 'clear ' char(ldnames(ii,1)) ] )
    fprintf('Loading ascii file %s.\n', oldasciiname );
    eval( char(ldnames(ii,1)) );
  end
  %
  % Special case code for ctd1 and 2.  Rename the folloing column indicies to
  % prevent them from being overwritten by the biolume and the hydroscat:
  if( strcmp( ldnames(ii,1), 'ctdDriver' ) & exist(char(ldnames(ii,3))) == 1) 
    ctdTempCol1 = temperature;
    ctdCondCol1 = conductivity;
  elseif( strcmp( ldnames(ii,1), 'ctdDriver2' ) & ...
	exist(char(ldnames(ii,3))) == 1) 
    ctdTempCol2 = temperature;
    ctdCondCol2 = conductivity;
  end
end
%
% Require both nav and control data to be present:
if( exist('dataNav') ~= 1 )
  fprintf('Navigation data is not present.  Abort.\n');
  return
end
	      
if( exist('dataDC') ~=1 ) 
  fprintf('Dynamic Control data is not present.  Abort.\n');
  return
end

  whos data* gps usbllog
%
% Find latest start time in the nav or control data.
%
startTime = 0;
if dataNav(1,1) > startTime; startTime = dataNav(1,1);end
if dataDC(1,1)  > startTime; startTime = dataDC(1,1);end

if( exist('relStartTime') )
    startTime = startTime + relStartTime;
    fprintf('Jumping start time to %.2f seconds relative; %.2f absolute.\n', ...
            relStartTime, startTime);
else
    relStartTime = 0;
end

if( exist('relStopTime') )
    if( relStopTime - relStartTime < 0 )
        fprintf('relStopTime must be > relStartTime.\n');
        return;
    end
    stopTime = startTime + relStopTime;
    fprintf('Truncating all data after %.2f seconds relative, %.2f absolute.\n', ...
            relStopTime, stopTime);
end

%  
% Synchronize the start times in all of the data files.  Discard data that
% begins before the start time.  
%
% This does NOT grid or interpolate the data.  The stringtokenizer is to take
% the struct name and not the data member.  exist('es.data') is 0, while
% exist('es') is 1.
try
for( i=1:length(ldnames) )
  arrayName  = char(ldnames(i,3));
  structName = stringtokenizer( char(ldnames(i,3)), 1, '.' );
  if( exist( structName )  == 1 )
    fprintf('Synchronizing %s.\n',  structName);
    eval( [arrayName '(:,1) = ' arrayName '(:,1) - startTime;'] );
    eval( [arrayName ' = NegTrunc(' arrayName ');'] );
    if( exist('relStopTime') )
        eval( [arrayName ' = PosTrunc(' arrayName ', relStopTime-relStartTime);'] );
    end
  end
end
catch
    fprintf('Problem with %s. Perhaps it''s not present?\n',char(ldnames(i,1)));
end


% eval( ['whos ', arrayName])
  whos data* gps usbllog

% 
% Miscellaneous:
if( exist('PA200LR') == 1 )  pa200range = range; end
%
% This will find the index in ldnames
% find(strcmp(ldnames(:,1),'dvl'))
if( exist('ctdDriverctdlog') == 1 )   ctdlog  = ctdDriverctdlog; end
if( exist('ctdDriver2ctdlog') == 1 )  ctdlog2 = ctdDriver2ctdlog; end
if( exist('ctdlog') == 1 | exist('ctdlog2') == 1 )
  humidityCol = 8;
  flow1Col=9;
  OBSCol=10;
  dissolvedO2Col=11;
end
%
% Extract and synchronize waypoints:
%
% The following code replaces the script extractwp, which used to be
% contained in cvt.   It reqires Cygwin.
%
% 5 Mar 2013 The following code was replaced again.
%
if( exist('waypoints.mat') == 2)
    load('waypoints.mat');
    fprintf('Loaded waypoints.mat.\n');
else
    if( exist('missionFileName') ~=1 )
        missionFileName=[];
        fprintf('Will attempt to extract waypoints from a .pp file.\n');
    else
        fprintf('Will attempt to extract waypoints from file %s.\n', missionFileName);
    end

    dataWP=corridor(missionFileName);
    
    if( isempty(dataWP) ) 
        if( exist('syslog') == 2 )
            if( localpath(2) == ':' )
                fprintf('%s\n', 'Extracting waypoint data from syslog.');
                tmpPath = '';
                if( strcmp( computer, 'PCWIN' ) ) tmpPath = 'c:/cygwin';end
                err = system([ 'grep wplog syslog > ' tmpPath '/tmp/temp.txt'] );
                if( err ) 
                    fprintf('%s\n', ...
                            'Error grepping syslog. Perhaps cygwin isn''t available.');
                else
                    clear dataWP;
                    dataWPstr = char(textread([ tmpPath '/tmp/temp.txt' ],...
                                              '%s','delimiter','\n'));
                    %system('rm /tmp/temp.txt');
                end  %if( err ) 
            else
                fprintf('You must cd to a mapped drive, such as z:, to extract waypoint data.\n')
                fprintf('Extraction won''t work with a UNC pathname like //sandpiper/altex/etc.\n')
            end

            if( exist('dataWPstr') == 1 )
                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
            end
        else
            fprintf('Syslog not found; can''t attempt waypoint extraction.\n');
        end
    end
end
    
if( exist('dataWP') == 1 & ~isempty(dataWP) )
    save('waypoints.mat','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
  
%
% Nice try, but it's not really feasible.  There are too many problems with
% the various types of pathnames, and different syntaxes for running scripts
% between Unix and Windows.
%
% Extract waypoints from the syslog. First find the path to the "review"
% directory:
pp = path;
delim = ':';
dirDelim = '/';
if( strcmp(computer,'PCWIN') ) 
  delim = ';';
  dirDelim = '\';
end
[ss,rr] = strtok(pp,delim);
while( isempty(strfind(ss,'review')) & ~isempty(rr) ) 
  [ss,rr] = strtok(rr,delim);
end
%
% Build up the full pathname.
exwpPath=[ss dirDelim 'extractwp'];
%
% Execute the script that is in the "review" directory.
system(exwpPath);
  
