function shipVelocity( hMainFig )
%
% Compute the ship velocity in knots
% Used to test bad Position
%
% At present 

% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

% Get tsg fieldnames
% ------------------
tsgNames     = fieldnames(tsg);
nbFieldNames = length( tsgNames );

% Test for bad velocity.
% Suppress the bad records
% ------------------------
nBadVelocity   = 0;
indBadVelocity = 1;
while ~isempty( indBadVelocity )

  % Record number
  % -------------
  tsgLength = length(tsg.DAYD);
  
  % Spherical earth distance in km - Velocity in knots
  % --------------------------------------------------
  range    = m_lldist(tsg.LONX,tsg.LATX);
  ind      = size(tsg.DAYD);
  velocity = zeros(size(ind));
  for i = 1:tsgLength-1
    velocity(i) = range(i) / ((tsg.DAYD(i+1)-tsg.DAYD(i)) * 24 * 1.854);
  end
  velocity = [velocity';0];

  % Find velocity > 50 knots
  % ------------------------
  indBadVelocity = find( velocity > 50 );
  
  if ~isempty( indBadVelocity )
    
    % Keep only the first indice. One bad position lead to 2 bad velocities
    % ---------------------------------------------------------------------
    indBadVelocity = indBadVelocity(1) + 1;

    % Store the number of bad Velocity
    % --------------------------------
    nBadVelocity = nBadVelocity + 1;

    % Delete bad record
    % -----------------
    for i = 1 : nbFieldNames

      para = char( tsgNames{i} );

      % Find array of length DAYD in the TSG structure
      % ----------------------------------------------
      if length( tsg.(para) ) == tsgLength
        tsg.(para)(indBadVelocity,:) = [];
      end
    end
  end
  
end

% Keep the number of data not in increasing date
% ----------------------------------------------
tsg.report.badvelocity = nBadVelocity;

% Keep ship velocity from positions if sog not available
% ------------------------------------------------------
if isempty(tsg.SPDC)
  tsg.SPDC = velocity;
end

% Save tsg structure
% ------------------
setappdata( hMainFig, 'tsg_data', tsg);

end
