function [r, bt_bmask, bt_wmask, hiamp] = prepedit(instclass, r, editcfg,...
                                                  showverbose, editname);
% [r, bt_bmask, bt_wmask, hiamp] = prepedit(instclass, r, editcfg, ...
%                            showverbose, editname );
% r is raw data from read
% showverbose is 0 or 1 (show bottomtrack masks)
% editcfg is output from get_pedefaults;  containing
%          editcfg.ampfilt
%          editcfg.bigstarget_threshold
%          editcfg.bigstarget_offset
%          editcfg.bigstarget_nside
% editname is optional; assumed to be 'all' or 'ampbt'.  
%           override with 'amp' or 'bt' (to specify one only)
% r is returned with cleaned_amp as a field
%  flags returned (1 == flagged; 0 == unflagged)
% bt_mask is the bottom mask from bottom track  
% hiamp is the mask of high amplitudes (acoustic interference)
% bt_wmask is the bottom mask from bigstarget (after removing high amplitudes)

if nargin == 3
   showverbose = 0;
   editname = 'all'
elseif nargin == 4
   editname = 'all'
end

if ~ismember (editname, {'all', 'ampbt', 'amp', 'bt'})
   fprintf('must choose to do "all", "ampbt", "amp", "bt"\n')
   fprintf('you chose %s\n', editname)
   r=[];
   bt_bmask = [];
   bt_wmask = [];
   hiamp = [];
   return
end

%% bottomtrack bottom mask
bt_bmask = zeros(size(squeeze(r.amp(:,1,:))));
%% watertrack identification of bottom
bt_wmask = bt_bmask;
%% hiamp
hiamp =  bt_bmask;

if isa(instclass, 'nb')
   use_bt = 1;
else
   use_bt = 0;
end


%% look for bottomtrack or max amp bin
% Calculate other masks and make the bad pieces integer 1
if ismember(editname, {'all', 'ampbt', 'bt'}) & (r.config.bt_was_on  | use_bt)
   %% Ocean surveyor uses 0 to denote a bad bottom track value.  
   %% Replace it with a big number (out of instrument's range)
   r.bt.range(r.bt.range == 0) = 10000;
   r.bt.range(find(isnan(r.bt.range))) = 10000;
   btd = min(r.bt.range);
   dd = r.depth - r.config.tr_pressure;
   bt_bmask = mask_below(dd, btd, r.config.beamangle);
end


%try to find the bottom, (try to avoid deleting shallow profiles)
if showverbose
   f1; clf
end


for ii = 1:4
   if ismember(editname, {'all', 'ampbt', 'amp'})
       %now look for bottom mask
       % need to remove spikes due to interference.
       [cleaned_amp, nmask] =  clean_amp(squeeze(r.amp(:,ii,:)),...
                                       'osbug',isa(instclass,'os'),...
                                       'ampfilt', editcfg.ampfilt,...
                                       'nedit_threshold', 0);
       r.cleaned_amp(:,ii,:) = cleaned_amp;
    else
       cleaned_amp = squeeze(r.amp(:,ii,:));
       r.cleaned_amp(:,ii,:) = cleaned_amp;
       nmask = zeros(size(cleaned_amp));
    end


   if ismember(editname, {'all', 'ampbt', 'bt'})    

      [bm1, b] = bigstarget(cleaned_amp,...
                         'thresh', editcfg.bigstarget_thresh, ...
                         'offset', editcfg.bigstarget_offset, ...
                         'nside',  editcfg.bigstarget_nside, ...
                         'nmask', nmask);
   
       bbtmp =  bottommask(bm1, b, cos(pi*r.config.beamangle/180) , 1);

       bt_wmask(find(bbtmp)) = 1;
   end


   if ismember(editname, {'all', 'ampbt', 'amp'})
      hiamp(find(nmask)) == 1;
   end

   
   if (showverbose)
      titlestr = sprintf('beam %d',ii);
      plotbot(ii, r, bbtmp, titlestr);
   end
end      

r.bmask =  illogical(bt_bmask | bt_wmask | hiamp);

%------------------------------
function mask = mask_below(depth, bottom, beamangle)
% depth is bin depth in velocity profile
% bottom is the identified max amp bin from bottom tracking
np = length(bottom);
nd = length(depth);

% make depth slightly deeper to ensure we're cutting off enough
depth = depth + .5*diff(depth([1 2])); %assumes regularly-space depths
depth = depth(:, ones(np, 1));


% include 15% for 30deg beam 
bottom = cos(pi*beamangle/180)*bottom;
         %repmat(bottom,nd,1)
bottom = bottom(ones(nd, 1), :);

mask = zeros(nd,np);
mask(depth > bottom) = 1;



