function bmask1 = bottommask(bmask, b, frac, extra)
% function bmask1 = bottommask(bmask, b, frac, extra)
%
% bmask1 is a mask with 1 at and below bottom interference
% depth, and zero elsewhere.
% bmask and b are the output of bigtarget.m.
% frac is the unaffected fraction of the vertical distance to
% the bottom; it is 0.85 for 30-degree beams.  If we had good
% pitch and roll, the correct number could be calculated for
% each beam and ping; but no provision is made for this now.
% extra is an added number of bins to lop off--typically 1 to
% account for the blanking.
%
% Vectorization tricks make the code a bit obscure.
%
% EF 2000/09/26
%

[nd, np] = size(b);

indices = (1:nd).';
indices = indices(:, ones(np, 1));

bmax = max(b .* bmask);
bmax = bmax(ones(nd, 1), :);
bmaxmask = cumsum((b == bmax) & bmask); % looking down each column, it
                              % will be zero until the first bmax value
bmaxmask = (bmaxmask > 0);
b_ind = indices;
b_ind(~bmaxmask) = NaN;
b_ind = min(b_ind);

b_ind = floor(frac * b_ind - extra);

b_ind = b_ind(ones(nd, 1), :);

bmask1 = (indices >= b_ind);  % 1 below the adjusted depth of the bottom


