function bottomstruct = polybottom(AA, pflags, figpos)
% polybottom(AA, pflags, figwidgh)
%
% This program does not stand alone: it is a callback for gautoedit 
%
% AA.data should have data.dday, data.bins, data.[fn] ('fn' is field name)
% AA.config is from agetmat(prefix)
% AA.config.yearbase is yearbase
% drag a rectangle to rzap bad bins
% AA.badprof (see aplotit)
% AA.badrect (see aplotit)

data = AA.data;
config = AA.plotconfig;
yr = AA.config.yearbase;


prefix = 'a_';  %for gautoedit
outfile = 'abottom.asc';   	% writes here by default

config.flagged = pflags;

numbins = length(data.pg(:,1));

%%% specific to gautoedit
buttonhandle = findobj('Tag','polybottomButton');
buttonstrings = get(buttonhandle, 'string');
buttonval = get(buttonhandle, 'value');
buttonstring = buttonstrings{buttonval};
set(buttonhandle, 'value', 1);

if (isfield(data, buttonstring))
   fprintf('polygon zapping bad bins from data field %s\n', buttonstring)
   fn = buttonstring;
else
   fprintf('select a field from the popup menu to use for rzap\n')
end

zapfig = figure;
set(gcf, 'menubar','none','toolbar','none', 'pos', ...
         [figpos(1:3) round(figpos(4)/4)])
whitebg(gcf,'w');
new_pos = [.08 NaN .92 NaN];   %(new xstart and new width)
ax1 = axes;
pos = get(ax1, 'pos');

set(ax1, 'pos', [new_pos(1)  pos(2) new_pos(3) pos(4)])


config.xlim = [config.start_dd config.start_dd+config.ddrange];
cfg = aplotit(data, config, 'cur_ax', ax1, 'fn', fn,...
              'flagged',0*data.pflag,...
              'badrect', AA.badrect,...
              'mabs', AA.mabs,...
              'badprof', AA.badprof);


axes(cfg.cur_ax);


disp('click to outline the bottom; doubleclick to finish')

clear gcbo

polyh = drawppolygon(cfg.cur_ax,'Color','k','LineWidth',2);

polydday = get(polyh, 'xdata');
polybins  = get(polyh, 'ydata');


%%% now make these be max amp bin, not bad bins
polydday = [polydday(1) ; polydday(:); polydday(end); polydday(1)];
polybins = [data.bins(end) ; polybins(:); data.bins(end); data.bins(end)];
[ddayM, binM] = meshgrid(data.dday, data.bins);
alldday = ddayM(:);
allbins = binM(:);
ini = inpolygon(alldday, allbins, polydday, polybins); %index "inside" polygon


%mask is all zeros to start
bmask = zeros(size(ddayM));
% mask is 1 if inside polygon
bmask(ini) = 1;
% mask is 1 if below shallowest point in polygon
bmask(cumsum(bmask) >= 1) = 1;
% maxampbin will be a matrix of ones where bottom was located
b = (cumsum(bmask) == 1);
% "b" float (as opposed to logical)
bf = b+0.0;
bf(b==0) = NaN;


% ibot is index for bottom;
[maxamp, maxampbin] = max(bf);
ibot = find(~isnan(maxamp));


%time = to_date(config.yearbase, ddayM(1,ibot), 'n');
%fid = fopen(outfile, 'a');
%for ii = 1:length(ibot)
%   fprintf(fid, ...
%           '  0  %3.0f %3.0f %2.0f/%02.0f/%02.0f  %02.0f:%02.0f:%02.0f',  ...
%           [0 0], time(ii,1:6));
%   fprintf(fid, ' A    32767 %5.0f %3.0f\n', ...
%           maxampbin(ibot(ii)), maxampbin(ibot(ii)));
%end
%fclose(fid);


set(zapfig, 'position',  [figpos(1:3) round(figpos(4)/4)]);
close(zapfig)


%for output


bottomstruct.dday = data.dday(ibot)';
bottomstruct.bins = maxampbin(ibot)';




