function [out1,out2] = scircleg(varargin)

%SCIRCLEG:  Display of small circles defined via a mouse input
%
%  SCIRCLEG(n) will compute and display n small circles on the
%  current map axes.  The small circles are defined by
%  a mouse click on the center of the circle and then by
%  a mouse click on the circle, thereby defining its radius.
%
%  SCIRCLEG(n,npts) uses the inputs npts to define the number
%  of points per circle computed for each small circle.
%
%  SCIRCLEG(n,'LineSpec') and SCIRCLEG(n,npts,'LineSpec') uses
%  any valid LineSpec string to display the small circles.
%
%  SCIRCLEG(n,'PropertyName',PropertyValue,...) and
%  SCIRCLEG(n,npts,'PropertyName',PropertyValue,...) uses the
%  specified line properties to display the small circles.
%
%  SCIRCLEG('track',...) uses the track string to define
%  the distance calculation for the circle radius.  If
%  'track' = 'gc', then great circle distance is used as
%  the circle radius.  If 'track' = 'rh', then rhumb line
%  distance is used as the circle radius.  If omitted,
%  'track' = 'gc' is assumed.
%
%  h = SCIRCLEG(...) returns the handles of the circles drawn.
%
%  [lat,lon] = SCIRCLEG(...) returns the latitude and longitude
%  matrices corresponding to the circles drawn.
%
%  See also SCIRCLE1, SCIRCLE2, TRACKG

%  Copyright 1996-1998 by Systems Planning and Analysis, Inc. and The MathWorks, Inc.
% $Revision: 1.7 $
%  Written by:  E. Byrns, E. Brown




if ~ismap;   error('Not a map axes');  end

str   = [];     ncirc = [];      npts  = [];

if nargin ~= 0 & isstr(varargin{1})
   str = varargin{1};     varargin(1) = [];

   if length(varargin) == 1 & ~isstr(varargin{1})
          ncirc = varargin{1};    varargin(1) = [];   npts  = [];
   elseif length(varargin) == 1 & isstr(varargin{1})
          ncirc = [];      npts  = [];
   elseif length(varargin) >= 2 & isstr(varargin{2})
          ncirc = varargin{1};   varargin(1) = [];
   elseif length(varargin) >= 2 & ~isstr(varargin{2})
          ncirc = varargin{1};   npts = varargin{2};   varargin(1:2) = [];
   end

elseif nargin ~= 0 & ~isstr(varargin{1})
   str = [];
   ncirc = varargin{1};    varargin(1) = [];

   if length(varargin) >= 1 & ~isstr(varargin{1})
          npts  = varargin{1};   varargin(1) = [];
   end
end

%  Test for a valid number of circles

if isempty(ncirc)
    ncirc = 1;
elseif isstr(ncirc)
    error('Number of circles not specified.')
elseif max(size(ncirc)) > 1
    error('Number of small circles must be a scalar')
end

%  Test the track string

if isempty(str)
    str = 'gc';
else
    validstr = strvcat('gc','rh');
	indx     = strmatch(lower(str),validstr);
	if length(indx) ~= 1;       error('Unrecognized track string')
          else;                 str = deblank(validstr(indx,:));
    end
end

%  Set the tag string

switch str
    case 'gc',   tagstr = 'Small Circle';
	case 'rh',   tagstr = 'Rhumb Line Circle';
end

%  Pre-allocate some memory space

lat1(ncirc,1) = 0;     lon1(ncirc,1) = 0;
lat2(ncirc,1) = 0;     lon2(ncirc,1) = 0;

%  Get the circle definition.
%  Enforce that ncirc must be real (avoids messy warning message
%  with colon operator)

for i = 1:real(ncirc)
       disp(['Circle ',num2str(i),':  Click on center and perimeter'])

       [latpts,lonpts] = inputm(2);
	   if isempty(latpts);   warning('Wrong view for SCIRCLEG');  return;  end

	   lat1(i) = latpts(1);      lon1(i) = lonpts(1);
	   lat2(i) = latpts(2);      lon2(i) = lonpts(2);
end

%  Get geoid and unit parameters

geoid = getm(gca,'Geoid');
units = getm(gca,'AngleUnits');

%  Compute circles

[latc,lonc] = scircle2(str,lat1,lon1,lat2,lon2,geoid,units,npts);

%  Plot the circles

if ~isempty(varargin)
%  VARARGIN may contain a LineSpec string so it must come before
%  the property 'Tag'.  This has the drawback that any
%  user 'TAG' passed in will be overwritten by tagstr.  However,
%  this is a small price to pay for potentially complex parsing if
%  VARARGIN contains the property 'Tag'.

    hndl = plotm(latc,lonc,varargin{:},'Tag',tagstr);
else
    hndl = plotm(latc,lonc,'Tag',tagstr);
end

%  Set output arguments

if nargout == 1
     out1 = hndl;
elseif nargout == 2
     out1 = latc;    out2 = lonc;
end
