
function flagprf(flag, blkprf)

% use to mark/unmark entire profile
% blkprf is a 2-column array: column 1 is block no.
%                             column 2 is profile no.

%editglob
global CURRX NBINS DISP BADPRF BADPRF_BAK IBLKPRF LASTCOMM LASTPLOT


if nargin < 2
  if isempty(CURRX)
    error('You must plot a variable first before running badprf without args')
  end
  disp('Use the mouse to click on each of the bad profiles in the current window')
  disp('Press return key when done')
  [x y] = ginput;

  y = round(abs(y));               % bump to the nearest bin
  i = find(y < 1);
  y(i) = ones(size(i));
  i = find(y > NBINS);
  y(i) = NBINS * ones(size(i));

  r = CURRX(y,:) + DISP(y,:);
  i = find(isnan(r));
  r(i) = 1e38 * ones(size(i));

  BADPRF_BAK = BADPRF;
  for i = 1:length(x)
    [m imin] = min(abs(r(i,:) - x(i)));
    if BADPRF(imin) ~= 1   % don't disturb what's already flagged in database
      BADPRF(imin) = flag;
    end
  end
else
  BADPRF_BAK = BADPRF;
  for i = 1:length(blkprf(:,1))
    ibad = find(IBLKPRF(:,1) == blkprf(i,1) & IBLKPRF(:,2) == blkprf(i,2));
    if ~isempty(ibad)
      BADPRF(ibad) = flag;
    else
      disp(['No such block-profile in workspace: ' int2str(blkprf(i,1)) ' ' int2str(blkprf(i,2))])
    end
  end
end

i = find(BADPRF ~= BADPRF_BAK);
Blk_Prf = [IBLKPRF(i,1) IBLKPRF(i,2)]

LASTCOMM = 'badprf';
eval(LASTPLOT)

