function f = limflag(d, vars, dmin, dmax, dlim, dind)
% ------------------------------------------------------------------
% Generate a flag array corresponding to limit tests.
%
% SYNOPSIS:
%
%   f = limflag(d, vars, min, max, lim, ind)
%
% PARAMETERS:
% 
%   d
%      Array (MxN) of M variables over N pings.
%
%   vars
%      Column array of variable numbers present in the data array.
%      The order must correspond to the latter.
%
%   min, max, lim, ind
%      These parameters have the same meaning as their counterparts
%      described in function `statp'.
%
% RETURNS:
%
%   f
%      Array of flags corresponding to limit testing on the data. It
%      has the dimensions (IxN), where I is the length of index vector 
%      `ind'.
%
%      Flags may have the following values:
%
%         -1 : data is less than min (used only tests within intervals)
%          0 : data passes the limit test
%         +1 : data is larger than max, in case of tests within intervals,
%              or data does not pass the limit test, in case of tests
%              outside intervals.
% ------------------------------------------------------------------
%Time:<Last updated on 98/09/08 at 15:58:48 by jaccard@pc34.gfi.uib.no>
%File:</home1/jaccard/data1/codas3/gfi/matlab/matlab50/scan/limflag.m>

nv = min([ length(dmin) length(dmax) length(dlim) length(dind) size(d,1) ]);
np = size(d,2);

f = zeros(size(d,1), np);

for i=1:nv,
  
  j = find(vars(i) == dind);
  if dlim(j) == 1,
	 f(i,:) = (d(i,:) < dmin(j)) * (-1);
	 f(i,:) = f(i,:) + (d(i,:) > dmax(j));
	 %DEBUG:
	 %fprintf('%d -- %f %f %f -- %d\n', i, d(i,1), dmin(j), dmax(j), f(i,1));
  elseif dlim(j) == -1,
	 f(i,:) = ( (d(i,:) < dmax(j)) & (d(i,:) > dmin(j)) );
	 %DEBUG:
	 %fprintf('%d -- %f %f %f -- %d\n', i, d(i,1), dmin(j), dmax(j), f(i,1));
  end

end


return;

