function tbad = cl_badtimes(data, config, pflags, figpos, yr)
% cl_badtimes(data, config, pflags, figwidgh, yr)
%
% This program does not stand alone: it is a callback for gautoedit 
%
% data should have data.dday, data.bins, data.[fn] ('fn' is field name)
% config is from agetmat(prefix)
% yr is yearbase
% click on the endpoints and  it adds an entry to badtimes.asc 
%     with bad profile times



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

config.flagged = pflags;


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

set(buttonhandle, 'value', 1);

fighandle = findobj('Tag','gautoedit_fig');
AA = get(fighandle,'userdata');

if (isfield(data, buttonstring))
   fprintf('selecting bad time range from data field %s\n', buttonstring)
   fn = buttonstring;
else
   fprintf('select a field from the popup menu to use for profile deletion\n')
end


badtimefig = 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];

if (strmatch(fn, 'pg'))
   cfg = aplotit(data, config, 'cur_ax', ax1, 'fn', fn,...
                 'badrect', AA.badrect, ...
                 'badpoly', AA.badpoly, ...
                 'badprof', AA.badprof,...
                 'mabs', AA.mabs );
else
cfg = aplotit(data, config, 'cur_ax', ax1, 'fn', fn,...
                 'var_range', [AA.uvmin AA.uvmax],...
                 'badrect', AA.badrect, ...
                 'badpoly', AA.badpoly, ...
                 'badprof', AA.badprof,...
                 'mabs', AA.mabs );
end   
   
   
   
   

axes(cfg.cur_ax)



ButtonName  = questdlg('Choose time selection type',...
                       'time selection type',...
                       'select time range',...
                       'click on bad profiles (hit <CR> to end)',...
                       'select time range');


if (~isemptys(findstr('range', ButtonName)))
   
   tr = ginput(2);
   tr = tr(:,1)';
   
   %------------------------ start copy from mkbadtimes --------------------
   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);
   
else % profiles
   
   fprintf(' click on bad profiles until finished, hit <CR> to end\n')
   [chosen_dd, junk] = ginput;
   tbad = NaN*chosen_dd;
   
   for ii=1:length(chosen_dd)
      tbad(ii) = data.dday(max(find(data.dday <= chosen_dd(ii))));
   end
end
   
set(badtimefig, 'position',  [figpos(1:3) round(figpos(4)/4)]);
close(badtimefig);



