function [out1,out2] = trackg(varargin)

%TRACKG  Display of great circles and rhumb lines defined via a mouse input
%
%  TRACKG(n) will compute and display n great circles on the
%  current map axes.  The great circles are defined by
%  a mouse click on the starting and ending point of the track.
%
%  TRACKG(n,npts) uses the inputs npts to define the number
%  of points per circle computed for each great circle.
%
%  TRACKG(n,'LineSpec') and TRACKG(n,npts,'LineSpec') uses
%  any valid LineSpec string to display the great circles.
%
%  TRACKG(n,'PropertyName',PropertyValue,...) and
%  TRACKG(n,npts,'PropertyName',PropertyValue,...) uses the
%  specified line properties to display the great circles.
%
%  TRACKG('track',...) uses the track string to define
%  the track drawn.  If 'track' = 'gc', then great circles
%  are drawn.  If 'track' = 'rh', then rhumb lines are drawn.
%  If omitted, 'track' = 'gc' is assumed.
%
%  h = TRACKG(...) returns the handles of the tracks drawn.
%
%  [lat,lon] = TRACKG(...) returns the latitude and longitude
%  matrices corresponding to the tracks drawn.
%
%  See also TRACK1, TRACK2, SCIRCLEG

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



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

str   = [];     ntrak = [];      npts  = [];

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

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

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

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

%  Test for a valid number of tracks

if isempty(ntrak)
    ntrak = 1;
elseif max(size(ntrak)) ~= 1
    error('Number of tracks 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 = 'GreatCircleArc';
	case 'rh',   tagstr = 'RhumbLineSegment';
end

%  Pre-allocate some memory space

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

%  Get the track definition
%  Enforce that ntrak must be real (avoids messy warning message
%  with colon operator)

for i = 1:real(ntrak)
       disp(['Track',num2str(i),':  Click on starting and ending points'])

       [latpts,lonpts] = inputm(2);
	   if isempty(latpts);   warning('Wrong view for TRACKG');  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 tracks

[lattrk,lontrk] = track2(str,lat1,lon1,lat2,lon2,geoid,units,npts);

%  Plot the tracks

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(lattrk,lontrk,varargin{:},'Tag',tagstr);
else
    hndl = plotm(lattrk,lontrk,'Tag',tagstr);
end

%  Set output arguments

if nargout == 1
     out1 = hndl;
elseif nargout == 2
     out1 = lattrk;    out2 = lontrk;
end