function varargout = getp(varargin)
% ------------------------------------------------------------------
% getp - Read log pings from a binary log file
%
% SYNOPSIS:
%
%   [err, data] = getp(start, npings, ncols, bin_file, type, host, noc)
%
% PARAMETERS:
%
%      start
%         First ping to read (default: 1) If this value is negative, the
%         information message is disabled.
%
%      npings
%         Number of pings to read (default NPINGS)
%
%      ncols
%         Number of columns in file (default: NCOL).
%
%      bin_file
%         Binary log file to read pings from (default: BINFILE)
%
%      type
%         Type of data to read (default: TYPE).
%         WARNING: this is a string, not a number (see Matlab help on
%                  `fread()' for possible values)
%
%      host
%         Character descriptor for machine format to use on opening the
%         binary file (default: HOST).
%
%      noc
%         If zero, the file does not start with a control value used to
%         select automatically the opening parameter host (default: 1).
%
% DESCRIPTION
%
%   Read pings from a binary log file. If `nlines' is positive, output a
%   message each time `nlines' of pings have been read. Data are send to
%   `tonan()' for bad data flagging.
%
% RETURN
%
%   err
%      Error status:
%
%         -1 : end of file
%          0 : no errors
%          1 : errors while reading
%
%   data
%      Array MxN of data read, where M is the number of columns and N the
%      number of pings (default: DATA).
%
% ------------------------------------------------------------------
%Time:<Last updated on 98/10/06 at 13:31:23 by veins@TORSK>
%File:<c:/codas3/gfi/matlab/matlab50/scan_bin/getp.m>

scanglob;

%--- Default values:
startp   = 1;
npings   = NPINGS;
ncols    = NVARS;
bin_file = BINFILE;
type     = TYPE;
host     = HOST;
noc      = 1;
msg      = 1;

%--- Input arguments:
if (nargin > 0) & ~isempty(varargin{1}),
  startp = varargin{1};
end
if (nargin > 1) & ~isempty(varargin{2}),
  npings = varargin{2};
end
if (nargin > 2) & ~isempty(varargin{3}),
  ncols = varargin{3};
end
if (nargin > 3) & ~isempty(varargin{4}),
  bin_file = varargin{4};
end
if (nargin > 4) & ~isempty(varargin{5}),
  type = varargin{6};
end
if (nargin > 5) & ~isempty(varargin{6}),
  host = varargin{7};
end
if (nargin > 6) & ~isempty(varargin{7}),
  noc = varargin{7};
end
if startp < 0,
  startp = abs(startp);
  msg = 0;
end

%--- Initializations:
err  = 0;
data = [];
fid = fopen(bin_file, 'rb', host);
offset = (startp - 1) * ncols * sizeof(type);
if noc, 
  offset = offset + noc*sizeof(type);
end
err = fseek(fid, offset, 'bof');
if err,
  fclose(fid);
  return;
end

%--- Read the pings:
if msg,
  fprintf('getp - Getting %-d pings from ping %-d ... ', ...
	 npings, startp);
end
ntot = ncols * npings;
data = fread(fid, ntot, type);
if feof(fid),
  err = -1;
end
fclose(fid);
data = data';
data = tonan(data);

%--- Actual number of pings:
npings = length(data)/ncols;
if msg,
  fprintf('done (%-d pings)\n', npings);
end

%--- Reshape data array:
DATA = reshape(data, ncols, npings);

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

return;


