function [blkprf, sel_dday] = cl_blkprf(fieldname, prefix)
% [blkprf, sel_dday] = cl_blkprf(fieldname, str);
% graphical tool to identify block and profile
% input:
%  fieldname could be 'amp', 'pg', 'u_meas', ...
%
% output: 
%  nlkprf is block and profile of selected dday
%  sel_dday is selected decimal day (and bin number)
%
%  writes formatted date and decimal day to stdout
%
%  action: click on locations where you want to know the block and profile
%         hit <CR> (while the figure is active) when through
%
% ASSUMES you are in a directory where getmat output exists (such as 
%       during a standard shipboard profile  editing session
%
% normally reads bare getmat output (w.mat, u.mat,...)
% second argument is an optional prefix
%
% example: 
% [blkprf, sel_dday] = cl_blkprf('pg');
% [blkprf, sel_dday] = cl_blkprf('uabs_sm', 'a_');


%broke this
% If you are doing 'autoedit' and have run aflagit_setup, a second argument
%       of 'gautoedit' will turn on flagging if it is on in autoedit
%


if ~exist('prefix')
   prefix = '';
end


%Jules 2000/11/13

if (nargin == 0)
   fieldname = 'pg';
   disp('assuming fieldname pg')
   help cl_blkprf
end



%make the figure size match gautoedit plot window if possible
fighandle = findobj('Tag','gautoedit_fig');
if (~isemptys(fighandle))
   figpos = get(501, 'position');
   
   blkprffig = figure;
   set(gcf, 'menubar','none','toolbar','none', 'pos', ...
            [figpos(1:3) round(figpos(4)/4)])
   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)])
else
   blkprffig = figure;
end

whitebg(blkprffig,'w')


[data, config] = agetmat(prefix);


%hide flagged data if plotting velocity
if (strcmp(fieldname, 'uabs_sm') | ...
    strcmp(fieldname, 'vabs_sm') | ...
    strcmp(fieldname, 'fabs_sm'))
   cfg = aplotit(data, config, 'cur_ax', ax1, 'fn', fieldname, ...
                 'flagged', data.pflag);
else
   cfg = aplotit(data, config, 'cur_ax', ax1, 'fn', fieldname);
end   

axes(cfg.cur_ax)
disp('select points on the current plot to get block and profile')
sel_dday = ginput;
blkprf = zeros(size(sel_dday));


if (~isemptys(sel_dday))
   sel_dday(:,2) = round(sel_dday(:,2));  % get whole numbers for bin
   
   for ddi = 1:length(sel_dday(:,1))   %decimal day counter
      [junk, bpi] = min(abs(data.dday - sel_dday(ddi,1)));
      blkprf(ddi,:) = data.iblkprf(bpi,:);
      cc = data.time(bpi,:);
      fprintf(' %s  (dday %10.7f)      bin %d\n',...
              to_date(cc(1),to_day(cc(1),cc),'s'),...
              data.dday(bpi), sel_dday(ddi,2))
   end
end

   

