
function zap(blkprfbin)

% use to mark/unmark selected bins in various profiles
% blkprfbin is a 3-column array: column 1 is block no.
%                                column 2 is profile no.
%                                column 3 is bin no.

%editglob
global EDIT_MODE CURRX NBINS DISP BADBIN BADBIN_BAK IBLKPRF
global LISTED LASTCOMM LASTPLOT

if ~EDIT_MODE
  error('You are not in edit mode.  Set EDIT_MODE = 1 to enable.')
end

if nargin < 1
  if isempty(CURRX)
    error('You must plot a variable first before invoking this function without args')
  end
  disp('Use the mouse to click on the bad bins')
  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 + DISP;
  i = find(isnan(r));
  r(i) = 1e38 * ones(size(i));

  BADBIN_BAK = BADBIN;
  LISTED = 0;
  for i = 1:length(x)
    [m imin] = min(abs(r(y(i),:) - x(i)));
    iv = (imin - 1) * NBINS + y(i);
    if BADBIN(iv) == 0
      BADBIN(iv) = -2;   % user-added
    end
  end
else
  BADBIN_BAK = BADBIN;
  LISTED = 0;
  for i = 1:length(blkprfbin(:,1))
    ibad = find(IBLKPRF(:,1) == blkprfbin(i,1) & IBLKPRF(:,2) == blkprfbin(i,2));
    if ~isempty(ibad)
      iv = (ibad - 1) * NBINS + blkprfbin(i,3);
      if BADBIN(iv) == 0
        BADBIN(iv) = -2;   % user-added flag
      end
    else
      disp(['No such block-profile in workspace: ' int2str(blkprfbin(i,1)) ' ' int2str(blkprfbin(i,2))])
    end
  end
end

LASTCOMM = 'badbin';
eval(LASTPLOT)

