function varargout = statp(varargin)
% ------------------------------------------------------------------
% Calculate statistics on pings. Result is strored in global
% variable `STAT', displayed in window and written to the output
% file.
%
% SYNOPSIS:
%
%   n = statp
%
%      Calculate statistics on current ping global variables.
%
% RETURNS
%
%   n
%      Number of pings read during the statstics
%
% ------------------------------------------------------------------
%Time:<Last updated on 98/10/07 at 17:34:18 by veins@TORSK>
%File:<c:/codas3/gfi/matlab/matlab50/scan_bin/statp.m>

scanglob;

%--- Initialize statistics and data array:
STAT = statinit(NVARS);
DATA = ones(NVARS, NPINGS) * NaN;


%--- Loop through pings:
err = 0;
start = 1;
ntot  = 0;
while err == 0,
  %--- Get ping data:
  [err, DATA] = getp(start);
  %--- Flag data:
  [DATA, flags] = flagdata(DATA, LIM, '-hdg', HDG);
  %--- Update statistics
  [STAT, HDG] = statupd(DATA, STAT, '-hdg', HDG);
  %--- Update number of pings:
  ntot = ntot + size(DATA,2);
  %--- Update start ping:
  start = start + NPINGS;
end

%--- Check errors:
if err > 0,
  error([ 'Problems while reading pings from file ' BINFILE ]);
end

%--- Calculate statistics:
STAT = statcalc(STAT, '-hdg', HDG);

%--- Build the stat string:
str = sprintf('\n RESULTS FROM STATP:\n\n');
str = sprintf('%s \t BINFILE: %s\n\n', str, BINFILE);
STATSTR = stat_str(STAT, LIM, HDR, str);
fprintf('%s', STATSTR);

%--- Output arguments:
if nargout > 0,
  varargout{1} = ntot;
end

return;


  








