function [out1,out2,savepts] = goode(mstruct,in1,in2,object,direction,savepts)

%GOODE  Goode Homolosine Pseudocylindrical Projection
%
%  This is an equal area projection.  Scale is true along all parallels
%  and the central meridian between 40 deg 44 min N and S, and is
%  constant along any parallel and between any pair of parallels
%  equidistant from the Equator for all latitudes.  Its distortion
%  is identical to that of the Sinusoidal projection between 40 deg
%  44 min N and S, and to that of the Mollweide projection elsewhere.
%  This projection is not conformal or equidistant.
%
%  This projection was developed by J. Paul Goode in 1916.  It is
%  sometimes called simply the Homolosine projection, and it is
%  usually in an interrupted form.  It is a merging of the
%  Sinusoidal and Mollweide projections.
%
%  This projection is available in an uninterrupted form only.

%  Copyright 1996-2000 Systems Planning and Analysis, Inc. and The MathWorks, Inc.
% $Revision: 1.8 $
%  Written by:  E. Byrns, E. Brown, W. Stumpf


if nargin == 1                  %  Set the default structure entries
	  mstruct.trimlat = angledim([ -90  90],'degrees',mstruct.angleunits);
      mstruct.trimlon = angledim([-180 180],'degrees',mstruct.angleunits);
	  mstruct.mapparallels = 0;
      mstruct.nparallels   = 0;
	  mstruct.fixedorient  = [];
	  out1 = mstruct;          return
elseif nargin ~= 5 & nargin ~= 6
      error('Incorrect number of arguments')
end


units  = mstruct.angleunits;
aspect = mstruct.aspect;
radius = rsphere('authalic',mstruct.geoid);
origin  = angledim(mstruct.origin,units,'radians');
trimlat = angledim(mstruct.flatlimit,units,'radians');
trimlon = angledim(mstruct.flonlimit,units,'radians');
scalefactor = mstruct.scalefactor;
falseeasting = mstruct.falseeasting;
falsenorthing = mstruct.falsenorthing;

%  Adjust the origin latitude to the auxiliary sphere

origin(1) = geod2aut(origin(1),mstruct.geoid,'radians');
trimlat   = geod2aut(trimlat  ,mstruct.geoid,'radians');

switch direction
case 'forward'

     lat  = angledim(in1,units,'radians');
     lat  = geod2aut(lat,mstruct.geoid,'radians');
     long = angledim(in2,units,'radians');

     [lat,long] = rotatem(lat,long,origin,direction);   %  Rotate to new origin
     [lat,long,clipped] = clipdata(lat,long,object);    %  Clip at the date line
     [lat,long,trimmed] = trimdata(lat,trimlat,long,trimlon,object);

%  Construct the structure of altered points

     savepts.trimmed = trimmed;
     savepts.clipped = clipped;

%  Back off of the +/- 90 degree points.  This allows
%  the differentiation of longitudes at the poles of the transformed
%  coordinate system.

     epsilon = epsm('radians');
     indx = find(abs(pi/2 - abs(lat)) <= epsilon);
     if ~isempty(indx)
	      lat(indx) = (pi/2 - epsilon)*sign(lat(indx));
     end

%  Pick up NaN place holders.

	 x = long;  y = lat;

%  Compute the indices for each component of the projection

     breakpt = dms2rad(40, 44,11.8);
     indx1 = find(abs(lat) >= breakpt);
	 indx2 = find(abs(lat) < breakpt);

%  Mollweide portion of the projection

     convergence = epsm('radians');     maxsteps = 100;
     steps = 1;                         thetanew = lat(indx1);
	 converged = 0;

     while ~converged & steps <= maxsteps
          steps = steps + 1;
          thetaold = thetanew;
		  deltheta = -(thetaold + sin(thetaold) - pi*sin(lat(indx1))) ./ ...
		              (1 + cos(thetaold));
          if max(abs(deltheta(:))) <= convergence;    converged = 1;
		      else;                  thetanew = thetaold + deltheta;
	      end
     end
     thetanew = thetanew / 2;

     x(indx1) = sqrt(8) * radius * long(indx1) .* cos(thetanew) / pi;
     y(indx1) = radius*(sqrt(2) *sin(thetanew) - 0.0528035274542*sign(lat(indx1)) );

%  Sinusoid portion of the projection (adjust scale to match mollweide)

     x(indx2) = radius * long(indx2) .* cos(lat(indx2));
	 y(indx2) = radius * lat(indx2);

%  Apply scale factor, false easting, northing

	x = x*scalefactor+falseeasting;
	y = y*scalefactor+falsenorthing;

%  Set the output variables

     switch  aspect
	    case 'normal',         out1 = x;      out2 = y;
	    case 'transverse',	   out1 = y;      out2 = -x;
        otherwise,             error('Unrecognized aspect string')
     end


case 'inverse'

     switch  aspect
	    case 'normal',         x = in1;    y = in2;
	    case 'transverse',	   x = -in2;   y = in1;
        otherwise,             error('Unrecognized aspect string')
     end

%  Apply scale factor, false easting, northing

	x = (x-falseeasting)/scalefactor;
	y = (y-falsenorthing)/scalefactor;

%  Pick up NaN place holders.

	 long = x;  lat = y;

%  Compute the indices for each component of the projection

     breakpt = radius*dms2rad(40, 44, 11.8); % was previously dms2rad(4444);
     indx1 = find(abs(y) >= breakpt);
	 indx2 = find(abs(y) < breakpt);

%  Mollweide portion of the projection

     theta = asin( (y(indx1)/radius + 0.0528035274542*sign(y(indx1)))/sqrt(2) );
	 lat(indx1)  = asin((2*theta +sin(2*theta))/pi);
	 long(indx1) = pi*x(indx1) ./ (sqrt(8)*radius*cos(theta));

%  Sinusoid portion of the projection

     lat(indx2)  = y(indx2) / radius;
     long(indx2) = x(indx2) ./ (radius * cos(lat(indx2)) );

%  Undo trims and clips

     [lat,long] = undotrim(lat,long,savepts.trimmed,object);
     [lat,long] = undoclip(lat,long,savepts.clipped,object);

%  Rotate to Greenwich and transform to desired units

     [lat,long] = rotatem(lat,long,origin,direction);
     lat        = aut2geod(lat,mstruct.geoid,'radians');

     out1 = angledim(lat, 'radians', units);
     out2 = angledim(long,'radians', units);

otherwise
     error('Unrecognized direction string')
end

%  Some operations on NaNs produce NaN + NaNi.  However operations
%  outside the map may product complex results and we don't want
%  to destroy this indicator.

indx = find(isnan(out1) | isnan(out2));
out1(indx) = NaN;   out2(indx) = NaN;

