function [peak_vals,peak_xy,hpeak]=peak_label_grid(figno,dotsize,nsig,tcol,ptsize,dmin);
%
% Call [peak_vals,peak_xy,hpeak]=peak_label(figno,dmin,tcol,ptsize,dotsize,nsig);
%
% nsig =     the number of digits after the decimal to keep when printing out;
%
%
% 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
%
xax=[];
figure(figno);
if isempty(tcol), map=get(figno,'Colormap'); tcol=1 - map(1,:); end
child1=get(figno,'Children'); zoom on;
nimage=0;
for jch=1:length(child1),
    if strcmp(get(child1(jch),'Type'),'axes') & ~strcmp(get(child1(jch),'Tag'),'Colorbar'),
        child2=get(child1(jch),'Children');
        for jch2=1:length(child2);
            if strcmp(get(child2(jch2),'Type'),'image'),
                nimage=nimage + 1;
                xax{nimage}=get(child2(jch2),'Xdata'); yax{nimage}=get(child2(jch2),'Ydata'); 
                data{nimage}=get(child2(jch2),'Cdata');
                [ny{nimage},nx{nimage}]=size(data{nimage});
                handles(nimage)=child1(jch);
            end
        end
    end
end
%%nimage, handles,
if isempty(xax), disp('Did not find image in current figure.'); return; end
%
jimage=1;
ii_neighbors_offset=[-ny{jimage}-1 -ny{jimage}+1 -1 1 ny{jimage}-1 ny{jimage}+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{jimage})]; jy_opts=[0 length(yax{jimage}) 1];
%
nsig_pow=10^nsig;
npix_max0=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); %%disp([x0,y0,button]),
    if isempty(x0), break; end
    if button<=3, jimage=find(gca==handles); end
    if button==2 | button==3, npix_max=-1; else, npix_max=npix_max0; end
    [dum,jx0]=min(abs(xax{jimage}-x0)); 
    [dum,jy0]=min(abs(yax{jimage}-y0));
%
    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';
    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
    iifound=0; dval_peak=-Inf;
    for jcirc=0:npix_max,
        jx_min=max(jx0-jcirc,2); jx_max=min(jx0+jcirc,nx{jimage}-1);
        jy_min=max(jy0-jcirc,2); jy_max=min(jy0+jcirc,ny{jimage}-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{jimage}(jy,jx); idx0=(jx-1)*ny{jimage} + jy;
                    dval_neighbors=max(data{jimage}(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
    if iifound,
        npeak=npeak + 1;
        peak_vals(npeak)=dval_peak;
        peak_xy(:,npeak)=[xax{jimage}(jx_peak);yax{jimage}(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{jimage}(jx0)+dxx,yax{jimage}(jy0),str,'Color',tcol2,...
            'FontName','Arial','FontSize',ptsize, ...
			'FontWeight','Bold','HorizontalAl',hal,'VerticalAl',val);
        hpeak(npeak)=h; hold on; 
        if dotsize>0, plot_cfill(peak_xy(1,npeak),peak_xy(2,npeak),'o',tcol2,dotsize); end
    end
end
zoom on;
