%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Load dataNav and dataK for various sets of missions.  Load the Usbl
%          data, if present.  Do some processing to prepare it for plotting.
% AUTHOR:  Rob McEwen
% DATE:    03/3/14
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  
% Declare and initialize a struct that holds various sets of paths for the
% various mission sets.
%
  clear pathNames;
%
% 10 Oct, 283.  First outing at 79 Degrees north.  One 4 minute mission, but it
% has both an "align" and a "surface" file.
%
  pathIndx = 1;  
  pathNames(pathIndx).missions = {...
      'j:/Oct01_Healy/INS/align20011010a/latest',
      'j:/Oct01_Healy/INS/align20011010b/latest',
      'j:/Oct01_Healy/auvdata/2001.283.00',
      'j:/Oct01_Healy/INS/surface20011010a/latest',
      []};
  pathNames(pathIndx).winfrog = 'j:/Oct01_Healy/WinFrog';
  pathNames(pathIndx).winfrogFileName = 'October102001.DAT';
  pathNames(pathIndx).tracking = 'water';
  pathNames(pathIndx).plotusbl = 'none';
  pathNames(pathIndx).EA = pi/4;
%
% 13 Oct, 286.  First set of long missions; no ice in sight.
%
  pathIndx = pathIndx+1;
  pathNames(pathIndx).missions = {...
      'j:/Oct01_Healy/auvdata/2001.286.00',
      'j:/Oct01_Healy/auvdata/2001.286.01',
      'j:/Oct01_Healy/auvdata/2001.286.02',
      'j:/Oct01_Healy/auvdata/2001.286.03',
      'j:/Oct01_Healy/auvdata/2001.286.04',
      'j:/Oct01_Healy/INS/hangar20011013a/latest',
%     'j:/Oct01_Healy/INS/align20011013a/latest',  %Something is wrong here
      'j:/Oct01_Healy/INS/align20011013b/latest',
      []};
  pathNames(pathIndx).winfrog = 'j:/Oct01_Healy/WinFrog';
  pathNames(pathIndx).winfrogFileName = 'October132001.DAT';
  pathNames(pathIndx).tracking = 'ice';  %Kft up, no ice, but bottom-trk data??
  pathNames(pathIndx).plotusbl = 'none';
  pathNames(pathIndx).EA = pi/4;
%
% 17 Oct, 290.  Second under-ice run set, the day after the first one where we
% had the Ips turned off.
%
  pathIndx = pathIndx+1;
  pathNames(pathIndx).missions = {...
%     'j:/Oct01_Healy/auvdata/2001.290.00',      %Deck, no Gps
%     'j:/Oct01_Healy/auvdata/2001.290.01',      %Deck, no Gps
%     'j:/Oct01_Healy/auvdata/2001.290.02',      %Deck, no Gps
%     'j:/Oct01_Healy/auvdata/2001.290.03',      %Deck, no Gps
      'j:/Oct01_Healy/auvdata/2001.290.04',      %It never dove
      'j:/Oct01_Healy/auvdata/2001.290.06',
      'j:/Oct01_Healy/auvdata/2001.290.08',
      'j:/Oct01_Healy/INS/surface20011017a/latest',  %This is before the above
     []};
  pathNames(pathIndx).winfrog = 'j:/Oct01_Healy/WinFrog';
  pathNames(pathIndx).winfrogFileName = 'October172001.DAT';
  pathNames(pathIndx).tracking = 'ice';
  pathNames(pathIndx).plotusbl = 'none';
  pathNames(pathIndx).pcodeGll = 'j:/Oct01_Healy/SCSData/pcode-gll';
  pathNames(pathIndx).pcodeGllName = 'pcode-gll_20011015-065529.raw';
%
% 24 Oct, 297.  Three open-water yoyo runs down by Svalbard:
%
  pathIndx = pathIndx+1;
  pathNames(pathIndx).missions = {...
      'j:/Oct01_Healy/auvdata/2001.297.03',
      'j:/Oct01_Healy/auvdata/2001.297.04',
      'j:/Oct01_Healy/auvdata/2001.297.05',
      'j:/Oct01_Healy/INS/surface20011024a/latest',
      []};
  pathNames(pathIndx).winfrog = 'j:/Oct01_Healy/WinFrog';
  pathNames(pathIndx).winfrogFileName = 'October242001.DAT';
  pathNames(pathIndx).tracking = 'water';
  pathNames(pathIndx).plotusbl = 'none';
%
% 25 Oct, 298.  Four long under-ice triangles, on the last day of AUV
% operations: 
% 
  pathIndx = pathIndx+1;
  pathNames(pathIndx).missions = {...
      'j:/Oct01_Healy/auvdata/2001.298.00',
      'j:/Oct01_Healy/auvdata/2001.298.03',
      'j:/Oct01_Healy/auvdata/2001.298.05',
      'j:/Oct01_Healy/auvdata/2001.298.06',
      'j:/Oct01_Healy/INS/surface20011025a/latest',
      []};
  pathNames(pathIndx).winfrog = 'j:/Oct01_Healy/WinFrog';
  pathNames(pathIndx).winfrogFileName = 'October252001.DAT';
  pathNames(pathIndx).tracking = 'water';
  pathNames(pathIndx).plotusbl = 'all';
  pathNames(pathIndx).EA = pi/4;
%
% Names for each mission set.  This is like an enumerated type.  The order of
% these names MUST correspond to the above struct.
%
  lbls={...
%
% 10 Oct, 283.  First outing at 79 Degrees north.  One 4 minute mission, but it
% has both an "align" and a "surface" file.  This data was lost.
%
  'FirstPNIndx',
%
% 13 Oct, 286.  First set of long missions; no ice in sight.  Looks like we
% were in zone 34X in this set.  Kearfott up.  Very little good dvl data.
% Before Han's EA fix.
%
  'NoIcePNIndx',
%
% 17 Oct, 290.  Second under-ice run set, the day after the first one where we
% had the Ips turned off.  The Kearfott data is really messed up. The Kearfott
% never acquired GPS in the post-recovery log.  The Dvl water-tracking is shut
% off.  Kearfott up.
%
  'SecondIcePNIndx',      
%
% 24 Oct, 297.  Three open-water yoyo runs down by Svalbard.  Kearfott down.
%
  'ThreeYoyosPNIndx', 
%
% 25 Oct, 298.  Four long under-ice triangles, on the last day of AUV
% operations.  Kearfott down.
% 
  'IceTrianglesPNIndx'};      
%
% 
  i = 1;
  while( i <= length(lbls) ),
    eval([ char(lbls(i))  ' = '  num2str(i) ';']);
    i = i + 1;
  end
%  
% Now select the Mission set:   zzz
%
  pathIndx = SecondIcePNIndx;
  pathIndx = NoIcePNIndx;
  pathIndx = IceTrianglesPNIndx;
  pathIndx = ThreeYoyosPNIndx;
%
% Clear some stuff, and begin loading the big enchilada:
%
  dataNavBig =[]; dataKBig = []; Mstart=[]; Mend = [];
  poleDown = []; Zone=[]; MstartIndx = []; MendIndx=[];

  firstMission = 1;  % For loading Nav and Dvl data
  i=1;
  while( ~isempty(pathNames(pathIndx).missions{i}) )
    eval(['cd ', pathNames(pathIndx).missions{i}]) 
    pwd
    %
    % Load Nav and Dvl data:
    %
    if( exist('dataN') == 2 )
      disp(sprintf(' Loading %s Nav data.\n',...
	  pathNames(pathIndx).missions{i}));
      dataN
      disp(sprintf(' Done.\n '));
      %
      loadDvl = 1;
      if( loadDvl )
	disp(sprintf(' Loading %s Dvl data.\n',...
	    pathNames(pathIndx).missions{i}));
	dataDVL
	disp(sprintf(' Done.\n '));
      end
      %
      % Assume that if nav data exists, the pole was down and Winfrog/usbl was
      % operating.
      %
      poleDown(i,1) = 1;
      %
      % Assume the mission pathnames in pathNames().missions are chronological,
      % and that the first file was an in-water mission.  Pre and post dive deck
      % data is appended.
      %
      if( firstMission )  %And dataN was there
	firstMission = 0;
	startTime = dataNav(1,1);
	dataNavSize = size(dataNav);
	ncols = dataNavSize(2);
	dataNav(:,1) = dataNav(:,1) - startTime;
	dataNavBig = dataNav;
	disp(sprintf('dataNavBig is %d x %d\n',size(dataNavBig)))
	%
	if( loadDvl & exist('dataDvl') )
	  %
	  % Problem: The first directory may not contain Dvl data.
	  %
	  dataDvl(:,1) = dataDvl(:,1) - startTime;
	  dataDvlBig = dataDvl;
	  disp(sprintf('dataDvlBig is %d x %d\n',size(dataDvlBig)))
	  [nrowsDvl ncolsDvl]=size(dataDvl);
	end
      else
	dataNav(:,1) = dataNav(:,1) - startTime;
	dataNavBig = [dataNavBig;NaN*ones(2,ncols);dataNav];
	disp(sprintf('dataNavBig is %d x %d\n',size(dataNavBig)))
	%
	if( loadDvl & exist('dataDvl') )
	  dataDvl(:,1) = dataDvl(:,1) - startTime;
	  dataDvlBig = [dataDvlBig;NaN*ones(2,ncolsDvl);dataDvl];
	  disp(sprintf('dataDvlBig is %d x %d\n',size(dataDvlBig)))
	end  % if( loadDvl & exist('dataDvl') )
      end % if( firstMission)
    else % if( exist('dataN') == 2 )
      %
      % Assume that if nav data doesn't exist, the pole wasn't down.
      %
      poleDown(i) = 0;
    end
    %
    % Load Kearfott data:
    %
    if( exist('dataKft') == 2 )
      disp(sprintf(' Loading %s Kearfott data.\n',...
	  pathNames(pathIndx).missions{i}));
      dataKft
      disp(sprintf(' Done.\n '));
      dataK(:,1) = dataK(:,1) - startTime;
      NegTrunc(dataK);
      if( i==1 ) 
	dataKSize = size(dataK);
	ncolsK = dataKSize(2);
	Mstart = 0;
	Mend   = dataK(end,1);
	MstartIndx = 1;
	MendIndx = length(dataK(:,1));
	dataKBig = dataK;
      else
	Mstart = [ Mstart; dataK(1,  1) ];
	Mend   = [ Mend;   dataK(end,1) ];
	%
	% length(dataKBig(:,1)) is the end of the big array.  +3 skips past the
	% two NaN's and points at the first element of the next segment.
	%
	MstartIndx = [MstartIndx; length(dataKBig(:,1))+3];
	dataKBig   = [dataKBig;NaN*ones(2,ncolsK);dataK];
	MendIndx   = [MendIndx; length(dataKBig(:,1))];
      end
      %
      % This assumes that i==1 is the first in-water mission.
      %
      Zone = [Zone; utmzone(dataK(1,[mLatK mLonK])/d2r)];
    else
      if( i == 1)
	%
	% If the Kearfott data isn't present in the first directory, declare
	% an error and terminate.
	%
	disp(sprintf(' Error.  Kearfott data is not present in %s.\n',...
	    pathNames(pathIndx).missions{i}));
	return
      end
    end
    i=i+1;
  end
  %
  % The Kearfott data in dataKEnd will be converted to UTM and plotted on the
  % UTM grid.
  %
  dataKEnd = dataK;
  %
  % Stick the "big" data back into the original arrays so that the plotting
  % routines will take it.
  %
  dataNav = dataNavBig;
  clear dataNavBig;
      
  dataK   = dataKBig;
  clear dataKBig;
  
  if( loadDvl )
    dataDvl = dataDvlBig;
    clear dataDvlBig;
  end
  %
  % Set up the format string to read in the October 2001 Healy Winfrog data.
  % See the Winfrog manual, Appendix B-45 "Data Files".
  %
  %           1     2          3    4     5           6          7
  %           Fix   date/time  lat  long  Height,msl  Northing   Easting
  wfFormat = '%*d   %s         %*s  %*s   %*d         %f         %f ';
  %
  %                     8     9      10     11       12   13    14   15
  %                     Depth Off,m  Along  Heading  Cmg  Speed QC   Offset
  wfFormat = [wfFormat,'%*f   %*f    %*f    %f       %*f  %*f   %*f  %*s '];
  %
  %                     16  17      18  19  20  21  22
  %                     km? Layback ?   ?   ?   ?   Vessel name
  wfFormat = [wfFormat,'%*f %*f     %*f %*f %*f %*f %*s '];
  %
  %                     23  24  25  26        27       
  %                     lat lon ?   Northing  Easting  
  wfFormat = [wfFormat,'%*s %*s %*f %f        %f '];
  %
  %                     28  29  30   31       32   33   34   35
  %                     ?   ?   ?    Heading  ?    ?    ?    NONE
  wfFormat = [wfFormat,'%*f %*f %*f  %*f      %*f  %*f  %*f  %*s '];
  %
  %                     36  37  38  39  40  41   42
  %                     ?   ?   ?   ?   ?   ?    AUV
  wfFormat = [wfFormat,'%*f %*f %*f %*f %*f %*f %*s '];
  %
  % Read in the Winfrog data:
  %
  if( ~isempty(pathNames(pathIndx).winfrog) )
    eval(['cd ', pathNames(pathIndx).winfrog]) 
    disp(sprintf(' Loading %s Winfrog data.\n',...
	pathNames(pathIndx).winfrogFileName));
    [usblTime wfShipN wfShipE wfShipH wfAuvN wfAuvE] = ...
	textread( pathNames(pathIndx).winfrogFileName, wfFormat, ...
	'delimiter',',' ); 
    usbl = [wfShipN wfShipE wfShipH wfAuvN wfAuvE];
    clear wfShipN wfShipE wfShipH wfAuvN wfAuvE;
    shipN = 1;
    shipE = 2;
    shipH = 3;
    vehN  = 4;  
    vehE  = 5;
%   dataUsblTime
%   dataUsbl
    clear tusbl;
    disp(sprintf(' Done loading Winfrog data.\n '));
    cvtwf  %This call parses the time vector
  else
    disp(sprintf(' Winfrog data not found.\n'));
  end
  
  loadPcodeGll=0;
  %         date   time  $GPGLL  Lat  N    Long  E/W  UTC
  gllFmt = '%s     %s    %*s     %f   %*s  %f    %s   %*f'
  if(loadPcodeGll)
  if(~isempty(pathNames(pathIndx).pcodeGll) )
    eval(['cd ', pathNames(pathIndx).pcodeGll]) 
    %
    % Read in the raw data:
    %
    disp(sprintf(' Loading %s pcodeGll data.\n',...
	pathNames(pathIndx).pcodeGllName));
    [pcodeGllDate pcodeGllTime pcodeGllLatAscii pcodeGllLongAscii ...
	gpcodeGllEW] = ...
        textread( pathNames(pathIndx).pcodeGllName, gllFmt, ...
	'delimiter',',' ); 
    disp(sprintf(' Done loading Pcode Gll data.\n '));
    %
    % Convert SCS UTC time stamp to Unix time - seconds since 
    % 1 Jan 1970, midnight.
    %
    disp(sprintf(' Begin Pcode time conversion.  Please wait...\n'))
    tPcodeGll = (   datenum([pcodeGllDate pcodeGllTime]) ...
	          - datenum('1-Jan-1970 00:00:00')       )*24*3600;
    disp(sprintf(' End Pcode time conversion\n'))
    %
    % Convert the NMEA Latitude convention ddmm.mmmm N to radians, as well as the
    % Longitude convention dddmm.mmmm E/W
    %
    %latmat = sscanf('
    %pcodeGllLat = 
  else
    disp(sprintf(' Pcode Gll data not found.\n'));
  end
  end
  
  
  
  
  return

%
  if exist('dataNav') ~= 1
    cd j:/Oct01_Healy/auvdata/2001.297.03
    disp(sprintf(' Loading 297.03 Nav data.  Arrayname is ''dataNav''.\n '));
    pwd
    dataN
    disp(sprintf(' Done.\n '));
    startTime = dataNav(1,1);
    dataNav(:,1) = dataNav(:,1) - startTime;
    disp(sprintf(' Loading 297.03 Kft data.  Arrayname is ''dataK''.\n '));
    dataKft
    disp(sprintf(' Done.\n '));
    dataK(:,1) = dataK(:,1) - startTime;
    NegTrunc(dataK);
  end

  if exist('usblTime') ~= 1
    cd k:\ArcticNav\2001.297.03
    disp(sprintf(' Loading Usbl data.\n '));
    pwd
    dataUsblTime
    dataUsbl
    disp(sprintf(' Done loading Usbl data.\n '));
    cvtwf
  end
  tmin = startTime - tusbl(1);
  cvtwf

  dataNavSize = size(dataNav);
  ncols = dataNavSize(2);
  dataKSize = size(dataK);
  ncolsK = dataKSize(2);

  dataNavBig = dataNav;
  dataKBig = dataK;
  cd j:/Oct01_Healy/auvdata/2001.297.04
  disp(sprintf(' Loading 297.04 Nav data.  Arrayname is ''dataNav''.\n '));
  pwd
  dataN
  disp(sprintf(' Done.\n '));
  dataNav(:,1) = dataNav(:,1) - startTime;
  dataNavBig = [dataNavBig;NaN*ones(2,ncols);dataNav];
  disp(sprintf(' Loading 297.04 Kearfott data.  Arrayname is ''dataK''.\n '));
  dataKft
  disp(sprintf(' Done.\n '));
  dataK(:,1) = dataK(:,1) - startTime;
% dataKBig = [dataKBig;dataK];
  dataKBig = [dataKBig;NaN*ones(2,ncolsK);dataK];

  cd j:/Oct01_Healy/auvdata/2001.297.05
  disp(sprintf(' Loading 297.05 Nav data.  Arrayname is ''dataNav''.\n '));
  pwd
  dataN
  disp(sprintf(' Done.\n '));
  dataNav(:,1) = dataNav(:,1) - startTime;
  dataNavBig = [dataNavBig;NaN*ones(2,ncols);dataNav];
  disp(sprintf(' Loading 297.05 Kearfott data.  Arrayname is ''dataK''.\n '));
  dataKft
  disp(sprintf(' Done.\n '));
  dataK(:,1) = dataK(:,1) - startTime;
% dataKBig = [dataKBig;dataK];
  dataKBig = [dataKBig;NaN*ones(2,ncolsK);dataK];

  dataNav = dataNavBig;
  clear dataNavBig;
%
% Load the alignment and post-recovery Kearfott data:
%
  cd j:/Oct01_Healy/INS/surface20011024a/latest
  disp(...
  sprintf(' Loading 297.End Kearfott data.  Arrayname is ''dataKEnd''.\n '));
  pwd
  dataKftEnd
  disp(sprintf(' Done.\n '));
  dataKEnd(:,1) = dataKEnd(:,1) - startTime;
% dataKBig = [dataKBig;dataKEnd];
  dataKBig = [dataKBig;NaN*ones(2,ncolsK);dataKEnd];

  dataK   = dataKBig;
  clear dataKBig;
  
  
  cd k:\ArcticNav\2001.297.03
  
  return

