function badbinstruct = rzap_badbins(AA, pflags, figpos)
% rzap_badbins(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 = 'abadbin.asc';   	% writes here by default

config.flagged = pflags;

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

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

if (isfield(data, buttonstring))
   fprintf('rzapping 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,...
              'badrect', AA.badrect,...
              'mabs', AA.mabs,...
              'badprof', AA.badprof);

axes(cfg.cur_ax);


disp('drag a rectangle to select bins for rzapping\n')
waitforbuttonpress
p1 = get(gca, 'currentpoint');
final_rect = rbbox;
p2 = get(gca, 'currentpoint');


p1x = p1(1,1); p1y = p1(1,2);
p2x = p2(1,1); p2y = p2(1,2);


startbin = max(   ceil(min(p1y, p2y)),    1);
endbin   = min(   floor(max(p1y, p2y)),  numbins);


tr = [min(p1x, p2x)    max(p1x, p2x) ];  %time range


%%---------------------------------

te = data.dday;                       % ensemble times
[nsm, nc]=size(te);
[ntr, nn]=size(tr);	

% create a logical mask, the size of t, where value is set
%  to 1 for those times which fall within the timeranges

onesec = 1/86400;

%since we clicked on a pcolor plot, the timestamp for a given stripe 
%  is the timestamp BEFORE the ginput value.  Find those ensemble times:
i_use = find( (te>= tr(1)) & (te<= tr(2))  );

[ne,ns]=size(i_use);

tbad=te(i_use);
badbins = [startbin:endbin];



%%create an ASCII file in the format as same as badbin.asc
%% formula is yyyy/mm/dd hh:mm:ss blk prf N bin1 bin2 bin3 binN



%tbad=tbad-onesec;		% subtract one second to ensure our times are 
%
%date_str=to_date(yr,tbad,'s');	% convert to string format
%
%
%
%writethese = repmat([length(badbins) badbins], length(tbad), 1);
%
%fmt_str = ['\n%s   0   0  ', repmat([' %d '], 1, length(badbins)+1)];
%FID=fopen(outfile, 'a');
%for ii=1:length(tbad)
%   fprintf(FID, fmt_str, date_str(ii,:), writethese(ii, :));
%	end
%fclose(FID);
%
%fprintf('wrote to %s, bins %d-%d for %4.3f-%4.3f\n', ...
%        outfile, startbin, endbin, tr(1), tr(2))
%


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






%for output
badbinstruct.dday = tbad;
badbinstruct.bins = badbins;