function [buoy_ll,brg_plane,buoy_sep_nmi,handles]=adar_buoy_loc(S,nbuoy,rng_lims,phi_rot,col);
%
% Call [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc(S,nbuoy,rng_lims,col);
%
% Possible depths: 65, 300, 525 ft.
%
% Example call:
% [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[4,4],[1 10; 1 12]);
%
% 401-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[4,4],[9 2; -3 6]);
%
% 402-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[4,4],[13 4; -2 7]);
%
% 403-2:
% [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[3,5],[0 6; -2 10]);
%
% 404-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[4 4],[16 4; -4 9]);
%
% 405-2:
% [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B_plan','MSS_Y_plan'},[2 7],[5 5; -12 8]);
%
% Revised based on actual:
% 401-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B','MSS_Y'},[4,4],[12 -3; -6 6],[0 40]);
%
% 402-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B','MSS_Y'},[4,4],[15 3; -4 8],[-5 0]);
%
% 403-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B','MSS_Y'},[3,5],[8 -4; -2 13],[25 10]);
%
% 404-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B','MSS_Y'},[4 4],[18 4; -3 7],[10 10]);
%
% 405-2:
% mydelete(handles); [buoy_ll,brg,buoy_sep_nmi,handles]=adar_buoy_loc({'MSS_B','MSS_Y'},[2 6],[5 10; -14 10],[15 -20]);
%
%
% 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<5, col='mgbmgbmgb'; end
%
adi_global
gui_global
%
figure(GBOX.figno); hold on;
handles=[];

%set(gcf,'Name','Click on Midpoint of Sub Track');
%[xt,yt] = ginput(1);
nsrc=length(S);
[nr,nc]=size(phi_rot);
if nr~=1 | nc~=nsrc, error('phi_rot must by 1 by nsrc'); end
[nr,nc]=size(rng_lims);
if nr~=nsrc | nc~=2, error('rng_lims must be nsrc by 2'); end
%
xt=mean(TGT.txyvp(:,2)); yt=mean(TGT.txyvp(:,3));
h=plot_cfill(xt,yt,'o','k',6); handles=[handles,h];

lat0=mean(BATHY.lat([1,end])); long0=mean(BATHY.long([1,end]));
x0=mean(BATHY.xax([1,end])); y0=mean(BATHY.yax([1,end]));
%
for jsrc=1:nsrc,
    if nbuoy(jsrc)>0,
        eval(['load ',GUI.nav_dir,S{jsrc},'_nav.mat'])
        xs(jsrc)=NAV.xkm(1); ys(jsrc)=NAV.ykm(1);
        h=plot_cfill(xs(jsrc),ys(jsrc),'o','c',7); handles=[handles,h];
        [rng(jsrc),brg(jsrc)]=xy2rb(xs(jsrc),ys(jsrc),xt,yt);   % rng in km
        disp([brg(jsrc),phi_rot(jsrc),jsrc])
	%
        [xt_ext,yt_ext]=rb2xy(xs(jsrc),ys(jsrc),2.5*rng(jsrc),brg(jsrc));
        h=plot_cfill([xs(jsrc),xt_ext],[ys(jsrc),yt_ext],'none','k',5,'-',1.5); handles=[handles,h];
	%
        rng_buoy=linspace(rng_lims(jsrc,1),rng_lims(jsrc,2),nbuoy(jsrc))'*1.852;    % nmi to km
        buoy_sep_nmi(jsrc)=abs(diff(rng_buoy([1,end]))/(nbuoy(jsrc)-1))/1.852;      % km to nmi for output
        
        brg_plane(jsrc)=brg(jsrc) + phi_rot(jsrc);
        if rng_lims(jsrc,1)>rng_lims(jsrc,2), brg_plane(jsrc)=brg_plane(jsrc) - 180; end
        if brg_plane(jsrc)<0, brg_plane(jsrc)=brg_plane(jsrc) + 360; end
	%
        brg_buoy=repmat(brg(jsrc) + phi_rot(jsrc),[nbuoy(jsrc),1]);
        [xb,yb]=rb2xy(xt,yt,rng_buoy,brg_buoy);
        [lat,long]=xy2ll(lat0,long0,x0,y0,xb,yb,0);
        buoy_ll{jsrc}=[lat,long];
        h=plot_cfill(xb,yb,'o',col(jsrc),5); handles=[handles,h];
    else
        buoy_sep_nmi(jsrc)=0; brg_plane(jsrc)=0;
        buoy_ll{jsrc}=[];
    end
end
%
disp(['Target NAV File = ',TGT.NAV.nav_file])
disp(' ')
for jsrc=1:nsrc,
    disp(['Source NAV File = ',S{jsrc}]);
    disp('Lat-Longs of Buoys')
    disp(buoy_ll{jsrc});
    disp('Heading of Plane (deg true)')
    disp(brg_plane(jsrc));
    disp('Buoy Separation (nm)');
    disp(buoy_sep_nmi(jsrc))
    disp(' ');
end
