function DRILL=data_drill(DATA,kdim_avg,iiavg,vdims,ii_peak,iich,tsize,iifig)
%
% Allows user to choose points on averaged surface, drill down into local maxima, and view which
% surfaces contributed to the local maxima.
%
% Call DRILL=data_drill(DATA,kdim_avg,iiavg,vdims,ii_peak,iich,tsize,iifig);
%
% DATA      = data structure of unaveraged/uncollapsed data;
% kdim_avg  = dimensions(s) over which to collapse data to get to 2-D image;
% iiavg     = collapsing operation to perform (1=avg,2=int,3=max,4=med) ([]=avg);
% vdims     = [ydim,xdim], y,x dimensions to view collapsed image;
% ii_peak   = 1 to find local maximum by interpolation (OK for CFARs), 0 to use point clicked on (best for DTRs);
% iich      = 1 to allow user to change size/vdims,clim in view5 when choosing peaks;
% tsize     = [width,height] to view image;
% iifig     = 1 to view DRILL at end;
%
%
% 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.
view_global;
%
if nargin<4,
    str=sprintf('    %s   ',DATA.labels{1:DATA.ndim}); disp(['Axis labels: ',str]);
    str=['Axis limits: '];
    for jdim=1:DATA.ndim, str=[str,'   ',sprintf('[%1.2f %1.2f]',DATA.scales{jdim}([1,end]))]; end
    disp(str)
    str=sprintf('       %i       ',DATA.dims); disp(['Axis lengths: ',str]);
end
if nargin<2,
    kdim_avg=   str2num(input(['                                Enter dimension number(s) over which to collapse: '],'s'));
end
if nargin<3,
    iiavg=      str2num(input(['                Enter operation(s) to perform (1=avg,2=int,3=max,4=med) ([]=avg): '],'s'));
    if isempty(iiavg), iiavg=1; end
end
if nargin<4,
    vdims=      str2num(input(['               Enter dimensions with which to view image (ydim,xdim):([]=default) '],'s'));
end
if nargin<5,
    ii_peak=    str2num(input(['                   Interpolate to find peak (1) or use exact point clicked on (0) '],'s'));
end
if nargin<6 | isempty(iich), iich=1; end
if nargin<7 | isempty(tsize), tsize=[5 2]; end
if nargin<8 | isempty(iifig), iifig=1; end
%
AVG=data_avg(DATA,kdim_avg,[],iiavg,0,1);
if isempty(vdims), vdims=find(AVG.dims>1); end
AVG.vdims=[vdims(:)' kdim_avg(:)']; end
AVG.size=tsize;
view5(AVG,[],iich);
%
[peak_vals,peak_xy,hpeak]=peak_label([],[],[],[],[],[],ii_peak);
npeak=size(peak_xy,2);
if npeak==0, disp('No peaks chosen - returning ...'); DRILL=[]; return; end
%
ydim=V.vdims(1); xdim=V.vdims(2);
%
DRILL=structure_copy(DATA,{'data'});
DRILL.dims(xdim)=npeak; DRILL.dims(ydim)=1;
DRILL.scales{xdim}=[1:npeak]; DRILL.scales{ydim}=0;
if isfield(DRILL,'scales2'), DRILL.scales2{xdim}=[1:npeak]; DRILL.scales2{ydim}=0; end
DRILL.data=zeros(DRILL.dims);
%
for jdim=1:7, KD{jdim}=[1]; end
for jdim=1:DATA.ndim, KD{jdim}=[1:DATA.dims(jdim)]; end
%
for jpeak=1:npeak,
    [dum,kx]=min(abs(DATA.scales{xdim} - peak_xy(1,jpeak)));
    [dum,ky]=min(abs(DATA.scales{ydim} - peak_xy(2,jpeak)));
    KD{xdim}=kx; KD{ydim}=ky;
    DRILL.scales{xdim}(jpeak)=peak_xy(1,jpeak);
    if isfield(DRILL,'scales2'), DRILL.scales2{xdim}(jpeak)=peak_xy(1,jpeak); end
    xx=DATA.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7});
    KD{xdim}=jpeak; KD{ydim}=1;
    DRILL.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7})=xx; 
end
cmax=max(DRILL.data(:)); cmin=min(DRILL.data(:));
DRILL.clim=[cmin,cmax];
%
if iifig, view5(DRILL); end
