function [peak_vals,peak_xy,hpeak]=peak_label(figno,dotsize,nsig,tcol,ptsize,dmin,ii_peak);
%
% Call [peak_vals,peak_xy,hpeak]=peak_label(figno,dotsize,nsig,tcol,ptsize,dmin,ii_peak);
%
% figno     = figure number to work on;
% dotsize   = size of dot to place on peak;
% nsig      = the number of digits after the decimal to keep when printing out;
% tcol      = color of text label;
% ptsize    = point size of text label;
% dmin      = minimum value to count as a peak;
% ii_peak   = 1 to interpolate to peak, 0 to take point clicked on;
%
% See also peak_label_grid;
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if nargin<1 | isempty(figno), figno=gcf; end
if nargin<2 | isempty(dotsize), dotsize=0; end
if nargin<3 | isempty(nsig), nsig=1; end
if nargin<4 | isempty(tcol), tcol='m'; tcol=[]; end     % AUTO METHOD
if nargin<5 | isempty(ptsize), ptsize=10; end
if nargin<6 | isempty(dmin), dmin=-Inf; end
if nargin<7 | isempty(ii_peak), ii_peak=1; end
%
xax=[]; peak_vals=[]; peak_xy=[]; hpeak=[];
figure(figno);
if isempty(tcol), map=get(figno,'Colormap'); tcol=1 - map(1,:); end
child1=get(figno,'Children'); zoom on;
for jch=1:length(child1),
    if strcmp(get(child1(jch),'Type'),'axes'),
        child2=get(child1(jch),'Children');
        for jch2=1:length(child2);
            if strcmp(get(child2(jch2),'Type'),'image'),
                xax=get(child2(jch2),'Xdata'); yax=get(child2(jch2),'Ydata'); 
                data=get(child2(jch2),'Cdata');
                break
            end
        end
    end
end
if isempty(xax), disp('Did not find image in current figure.'); return; end
%
[ny,nx]=size(data);
ii_neighbors_offset=[-ny-1 -ny+1 -1 1 ny-1 ny+1];
%
% Compute dx,dy to offset labels:
ax=axis; delx=diff(ax(1:2)); dely=abs(diff(ax(3:4)));
pos=get(gca,'Position'); asize=pos(3:4);
offset=.032;
dx=offset*delx/asize(1);
dy=offset*dely/asize(2);
jx_opts=[0 1 length(xax)]; jy_opts=[0 length(yax) 1];
%
nsig_pow=10^nsig;
npix_max=3; npeak=0;
disp('Click on points closest to local maxima to be labeled, <CR> to end ...');
disp('   To place label relative to peak, use number pad: 5=center, 7=above&left, 9=above&right, etc.');
while 1==1,
    [x0,y0,button]=ginput(1);
    if isempty(x0), break; end
%
    dxx=0; iilr=1; iitb=1; tcol2=tcol;
    switch button,
    case {49,50,51}, val='Top';
    case {52,53,54,76,108,82,114}, val='Middle';        % Align in middle verticall for left or right of image;
    case {55,56,57}, val='Bottom';
    case {84,116}, val='Bottom'; iitb=2; tcol2='m';
    case {66,98}, val='Top'; iitb=3; tcol2='m';
    otherwise, val='Middle'; button;
    end
    switch button,
    case {49,52,55}, hal='Right'; dxx=-dx;
    case {50,53,56,84,116,66,98}, hal='Center';
    case {51,54,57}, hal='Left'; dxx=dx;
    case {76,108}, hal='Right'; dxx=-dx; iilr=2; tcol2='m';        % "l" or "L" for left of image;
    case {82,114}, hal='Left'; dxx=dx; iilr=3; tcol2='m';          % "r" or "R" for right of image;
    otherwise, hal='Right'; dxx=-dx;
    end
    [dum,jx0]=min(abs(xax-x0)); 
    [dum,jy0]=min(abs(yax-y0));
    if ii_peak,
        iifound=0; dval_peak=-Inf;
        for jcirc=0:npix_max,
            jx_min=max(jx0-jcirc,2); jx_max=min(jx0+jcirc,nx-1);
            jy_min=max(jy0-jcirc,2); jy_max=min(jy0+jcirc,ny-1);
            for jx=jx_min:jx_max,
                for jy=jy_min:jy_max,
                    if jx==jx_min | jx==jx_max | jy==jy_min | jy==jy_max,
                        dval=data(jy,jx); idx0=(jx-1)*ny + jy;
                        dval_neighbors=max(data(idx0+ii_neighbors_offset));
                        if dval>=dval_neighbors & dval>dmin & dval>dval_peak,
                            dval_peak=dval; iifound=1; jx_peak=jx; jy_peak=jy;
                        end
                    end
                end
            end
        end
    else
        iifound=1; dval_peak=data(jy0,jx0); jx_peak=jx0; jy_peak=jy0;
    end
    if iifound,
        npeak=npeak + 1;
        peak_vals(npeak)=dval_peak;
        peak_xy(:,npeak)=[xax(jx_peak); yax(jy_peak)];
        dval2=round(dval_peak*nsig_pow)/nsig_pow; str=num2str(dval2); 
        jx_opts(1)=jx_peak; jx0=jx_opts(iilr);
        jy_opts(1)=jy_peak; jy0=jy_opts(iitb);
        h=text(xax(jx0)+dxx,yax(jy0),str,'Color',tcol2, ...
            'FontName','Arial','FontSize',ptsize, ...
			'FontWeight','Bold','HorizontalAl',hal,'VerticalAl',val);
        hpeak(npeak)=h; hold on; 
        if dotsize>0, plot_cfill(xax(jx_peak),yax(jy_peak),'o',tcol2,dotsize); end
    end
end
zoom on;
