
function bottom(blkprfbin)

% use to mark/unmark entire profile
% 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 BOTTOM BOTTOM_BAK LISTED IBLKPRF
global DEPTH LASTCOMM LASTPLOT MAB

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 max amp bin of bottom-contaminated profiles')
  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));

  BOTTOM_BAK = BOTTOM;
  LISTED = 0;
  for i = 1:length(x)
    [m imin] = min(abs(r(y(i),:) - x(i)));
    BOTTOM(imin) = -y(i);          % use '-' to indicate user-added
  end
else
  BOTTOM_BAK = BOTTOM;
  LISTED = 0;
  for i = 1:length(blkprfbin(:,1))
    ibad = find(IBLKPRF(:,1) == blkprfbin(i,1) & IBLKPRF(:,2) == blkprfbin(i,2));
    if ~isempty(ibad)
      BOTTOM(ibad) = -blkprfbin(i,3);
    else
      disp(['No such block-profile in workspace: ' int2str(blkprfbin(i,1)) ' ' int2str(blkprfbin(i,2))])
    end
  end
end

i_already = find(abs(BOTTOM) == MAB');
BOTTOM(i_already) = BOTTOM_BAK(i_already);

ibot = find(BOTTOM <= NBINS);
Blk_Prf_Bin_Z_Gap = [IBLKPRF(ibot,1)'; IBLKPRF(ibot,2)'; abs(BOTTOM(ibot)); ...
                     DEPTH(NBINS * (ibot-1) + abs(BOTTOM(ibot))); diff([0 ibot])-1]

LASTCOMM = 'bottom';
eval(LASTPLOT)

