function [lim, varargout] = statlim(mode, lim, varargin)
%  1.  NAME
%
%  statlim - Set or return a string with the current limits
%
%  2.  SYNOPSIS
%
%  [lim, str] = statlim(mode, lim, vars, stat )
%
%  3.  DESCRIPTION
%
%  Set, update or display the current limits used for statistics. The
%  limit array is described in stat(5m).
%
%  4.  PARAMETERS
%
%     mode
%        Mode for updating limit array. If mode is -1,the limits for the
%        variables specified in vars are set to [-Inf,+Inf,0]. If mode is
%        0,the limits for the variables specified in vars are set to
%        [min,max,1],where min and max are taken from the statistics
%        array stat. If mode is +1,the limits are set to the specified
%        values listed in vars,in which case vars is an array with the
%        following columns:
%
%        1. variable number
%
%        2. minimal value
%
%        3. maximal value
%
%        4. limit mode
%
%     lim
%        Limit array to be updated or displayed.
%
%     vars optional
%        If mode is -1 or 0,vars is a vector with the line indexes to the
%        variables whose limits should be updated. If mode is +1,it is an
%        array as described above. If empty,the limit array is returned
%        as is together with the limit string.
%
%     stat
%        This parameter is required if mode is 0.
%
%  5.  RETURNS
%
%     lim
%        The updated limit array
%
%     str optional
%        A formatted string specifying updated limits.
%
%  6.  SEE ALSO
%
%  flagdata(3m)stat(5m)statcalc(3m)statinit(3m)statlim(3m)stat_str(3m)statupd(3m)
%
%  7.  AUTHORS
%
%  Pierre Jaccard, Geophysical Institute, University of Bergen, 1999
%

%--- Defaults:
inf_reset  = 0;
stat_reset = 0;
new_lim    = [];
nvars = size(lim, 1);
vars = [];
stat = [];
ii = 1:nvars;
for i=ii,
  vars(i) = i;
  stat(i,:) = [ -Inf, +Inf ];
end

%--- Indexes:
min_ = 1; max_ = 2; mode_ = 3;

%--- Arguments:
i0 = 2;
i = nargin - i0;
if i > 0,
  vars = varargin{1};
end
if i > 1,
  stat = varargin{2};
end

%--- Initialize the new limits:
if mode == -1,
  ii=1:length(vars);
  for i=ii,
	 new_lim(i,:) = [ vars(i), -Inf, +Inf, 0];
  end
elseif mode == 0,
  ii=1:length(vars);
  for i=ii,
	 new_lim(i,:) = [ ...
			 vars(i), stat(vars(i), min_), stat(vars(i), max_), 1 ];
  end
elseif mode == 1,
  if size(vars, 2) == 4,
	 new_lim = vars;
  end
else
  error([ 'Undefined mode for updating limits' ]);
end

%--- Update limits:
if ~isempty(new_lim),
  ii = 1:size(new_lim,1);
  for i=ii,
	 lim(new_lim(i,1),:) = [ new_lim(i,2:4) ];
  end
end

%--- Create the limit string:
str = sprintf(' SPECIFIED LIMITS:\n\n');
str = sprintf('%s VAR...  MIN............  MAX............  RANGE\n', str);
ii = 1:nvars;
for i=ii,
  switch(lim(i,mode_))
	 case -1,
		s = 'outside';
	 case 0,
		s = 'disabled';
	 case +1,
		s = 'inside';
  end
  str = sprintf('%s %6d  %15.8f  %15.8f  %s\n', ...
		str, i, lim(i, min_), lim(i, max_), s);
end
str = sprintf('%s\n', str);

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

return;
