function sinphi=sin_space(xy_rec,xy_tgt,LOB,iis2p);
%
% Call sinphi=sin_space(xy_rec,xy_tgt,LOB,1);
% xy_rec	= [x,y] position of receiver (npt by 2);
% xy_tgt	= [x,y] position of target (npt by 2);
% LOB		= receiver LOB in degrees true;
% sinphi	= bearing of target in sin space relative to receiver LOB;
%
% OR:
%
% Call sinphi=sin_space(dummy,phi,LOB,2);
% dummy		= dummy;
% phi		= bearing of target from receiver;
% LOB		= receiver LOB in degrees true;
% sinphi	= bearing of target in sin space relative to receiver LOB;
%
% OR:
%
% Call brg_tgt=sin_space(dummy,sinphi_tgt,LOB,0);
% dummy			= dummy;
% sinphi_tgt	= bearing of target in sin space re. LOB;
% LOB			= receiver LOB in degrees true;
% brg_tgt		= bearing of target from receiver in degrees true;
%
if nargin<4, iis2p=1; end
%
if iis2p==1,
	npt=size(xy_rec,1);
% Compute sinphi of TGT relative to REC having line of bearing LOB:
	dxy_rt=xy_tgt - xy_rec;		% dxy of TGT relative to REC;
	dxy_rt=dxy_rt./repmat(sqrt(sum(dxy_rt.^2,2)),[1,2]);
	dxy_lob=[sin(LOB*pi/180),cos(LOB*pi/180)];
	sinphi=sum(dxy_rt.*repmat(dxy_lob,[npt,1]),2);
	dxy_bs=[-dxy_lob(:,2),dxy_lob(:,1)];
	cosphi=sum(dxy_rt.*repmat(dxy_bs,[npt,1]),2);
	iiback_beam=find(cosphi<0);
	sinphi(iiback_beam)=2-sinphi(iiback_beam);
elseif iis2p==2,
	phi=xy_tgt; npt=length(phi);
	xy_rec0=repmat([0.,0.],[npt,1]);
	xy_tgt0=[sin(phi(:)*pi/180) cos(phi(:)*pi/180)];
% Compute sinphi of TGT relative to REC having line of bearing LOB:
	dxy_rt=xy_tgt0 - xy_rec0;		% dxy of TGT relative to REC;
	dxy_rt=dxy_rt./repmat(sqrt(sum(dxy_rt.^2,2)),[1,2]);
	dxy_lob=[sin(LOB*pi/180),cos(LOB*pi/180)];
	sinphi=sum(dxy_rt.*repmat(dxy_lob,[npt,1]),2);
	dxy_bs=[-dxy_lob(:,2),dxy_lob(:,1)];
	cosphi=sum(dxy_rt.*repmat(dxy_bs,[npt,1]),2);
	iiback_beam=find(cosphi<0);
	sinphi(iiback_beam)=2-sinphi(iiback_beam);
else
% Compute true bearing of TGT relative to REC given sinphi_tgt in xy_tgt and
% receiver LOB:
	sinphi_tgt=xy_tgt;
	iiback_beam=find(sinphi_tgt>1);
	sinphi_tgt(iiback_beam)=2-sinphi_tgt(iiback_beam);
	brg=asin(sinphi_tgt);
	brg(iiback_beam)=pi - brg(iiback_beam);
	brg_tgt=brg*180/pi - (90-LOB);
	sinphi=brg_tgt;
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.
