public static double DistanceFromGreatArc(double Alpha1, double Delta1, double Alpha2, double Delta2, double Alpha3, double Delta3)
        {
            Delta1 = AASCoordinateTransformation.DegreesToRadians(Delta1);
            Delta2 = AASCoordinateTransformation.DegreesToRadians(Delta2);
            Delta3 = AASCoordinateTransformation.DegreesToRadians(Delta3);

            Alpha1 = AASCoordinateTransformation.HoursToRadians(Alpha1);
            Alpha2 = AASCoordinateTransformation.HoursToRadians(Alpha2);
            Alpha3 = AASCoordinateTransformation.HoursToRadians(Alpha3);

            double X1 = Math.Cos(Delta1) * Math.Cos(Alpha1);
            double X2 = Math.Cos(Delta2) * Math.Cos(Alpha2);

            double Y1 = Math.Cos(Delta1) * Math.Sin(Alpha1);
            double Y2 = Math.Cos(Delta2) * Math.Sin(Alpha2);

            double Z1 = Math.Sin(Delta1);
            double Z2 = Math.Sin(Delta2);

            double A = Y1 * Z2 - Z1 * Y2;
            double B = Z1 * X2 - X1 * Z2;
            double C = X1 * Y2 - Y1 * X2;

            double m = Math.Tan(Alpha3);
            double n = Math.Tan(Delta3) / Math.Cos(Alpha3);

            double value = Math.Asin((A + B * m + C * n) / (Math.Sqrt(A * A + B * B + C * C) * Math.Sqrt(1 + m * m + n * n)));

            value = AASCoordinateTransformation.RadiansToDegrees(value);
            if (value < 0)
            {
                value = Math.Abs(value);
            }

            return(value);
        }
        public static CAAPhysicalMarsDetails Calculate(double JD, bool bHighPrecision)
        {
            //What will be the return value
            CAAPhysicalMarsDetails details = new CAAPhysicalMarsDetails();

            //Step 1
            double T          = (JD - 2451545) / 36525;
            double Lambda0    = 352.9065 + 1.17330 * T;
            double Lambda0rad = AASCoordinateTransformation.DegreesToRadians(Lambda0);
            double Beta0      = 63.2818 - 0.00394 * T;
            double Beta0rad   = AASCoordinateTransformation.DegreesToRadians(Beta0);

            //Step 2
            double l0    = AASEarth.EclipticLongitude(JD, bHighPrecision);
            double l0rad = AASCoordinateTransformation.DegreesToRadians(l0);
            double b0    = AASEarth.EclipticLatitude(JD, bHighPrecision);
            double b0rad = AASCoordinateTransformation.DegreesToRadians(b0);
            double R     = AASEarth.RadiusVector(JD, bHighPrecision);

            double PreviousLightTravelTime = 0;
            double LightTravelTime         = 0;
            double x        = 0;
            double y        = 0;
            double z        = 0;
            bool   bIterate = true;
            double DELTA    = 0;
            double l        = 0;
            double lrad     = 0;
            double b        = 0;
            double r        = 0;

            while (bIterate)
            {
                double JD2 = JD - LightTravelTime;

                //Step 3
                l    = AASMars.EclipticLongitude(JD2, bHighPrecision);
                lrad = AASCoordinateTransformation.DegreesToRadians(l);
                b    = AASMars.EclipticLatitude(JD2, bHighPrecision);
                double brad = AASCoordinateTransformation.DegreesToRadians(b);
                r = AASMars.RadiusVector(JD2, bHighPrecision);

                //Step 4
                x               = r * Math.Cos(brad) * Math.Cos(lrad) - R * Math.Cos(l0rad);
                y               = r * Math.Cos(brad) * Math.Sin(lrad) - R * Math.Sin(l0rad);
                z               = r * Math.Sin(brad) - R * Math.Sin(b0rad);
                DELTA           = Math.Sqrt(x * x + y * y + z * z);
                LightTravelTime = AASElliptical.DistanceToLightTime(DELTA);

                //Prepare for the next loop around
                bIterate = (Math.Abs(LightTravelTime - PreviousLightTravelTime) > 2E-6); //2E-6 correponds to 0.17 of a second
                if (bIterate)
                {
                    PreviousLightTravelTime = LightTravelTime;
                }
            }

            //Step 5
            double lambdarad = Math.Atan2(y, x);
            double lambda    = AASCoordinateTransformation.RadiansToDegrees(lambdarad);
            double betarad   = Math.Atan2(z, Math.Sqrt(x * x + y * y));
            double beta      = AASCoordinateTransformation.RadiansToDegrees(betarad);

            //Step 6
            details.DE = AASCoordinateTransformation.RadiansToDegrees(Math.Asin(-Math.Sin(Beta0rad) * Math.Sin(betarad) - Math.Cos(Beta0rad) * Math.Cos(betarad) * Math.Cos(Lambda0rad - lambdarad)));

            //Step 7
            double N    = 49.5581 + 0.7721 * T;
            double Nrad = AASCoordinateTransformation.DegreesToRadians(N);

            double ldash    = l - 0.00697 / r;
            double ldashrad = AASCoordinateTransformation.DegreesToRadians(ldash);
            double bdash    = b - 0.000225 * (Math.Cos(lrad - Nrad) / r);
            double bdashrad = AASCoordinateTransformation.DegreesToRadians(bdash);

            //Step 8
            details.DS = AASCoordinateTransformation.RadiansToDegrees(Math.Asin(-Math.Sin(Beta0rad) * Math.Sin(bdashrad) - Math.Cos(Beta0rad) * Math.Cos(bdashrad) * Math.Cos(Lambda0rad - ldashrad)));

            //Step 9
            double W = AASCoordinateTransformation.MapTo0To360Range(11.504 + 350.89200025 * (JD - LightTravelTime - 2433282.5));

            //Step 10
            double          e0             = AASNutation.MeanObliquityOfEcliptic(JD);
            double          e0rad          = AASCoordinateTransformation.DegreesToRadians(e0);
            AAS2DCoordinate PoleEquatorial = AASCoordinateTransformation.Ecliptic2Equatorial(Lambda0, Beta0, e0);
            double          alpha0rad      = AASCoordinateTransformation.HoursToRadians(PoleEquatorial.X);
            double          delta0rad      = AASCoordinateTransformation.DegreesToRadians(PoleEquatorial.Y);

            //Step 11
            double u        = y * Math.Cos(e0rad) - z * Math.Sin(e0rad);
            double v        = y * Math.Sin(e0rad) + z * Math.Cos(e0rad);
            double alpharad = Math.Atan2(u, x);
            double alpha    = AASCoordinateTransformation.RadiansToHours(alpharad);
            double deltarad = Math.Atan2(v, Math.Sqrt(x * x + u * u));
            double delta    = AASCoordinateTransformation.RadiansToDegrees(deltarad);
            double xi       = Math.Atan2(Math.Sin(delta0rad) * Math.Cos(deltarad) * Math.Cos(alpha0rad - alpharad) - Math.Sin(deltarad) * Math.Cos(delta0rad), Math.Cos(deltarad) * Math.Sin(alpha0rad - alpharad));

            //Step 12
            details.w = AASCoordinateTransformation.MapTo0To360Range(W - AASCoordinateTransformation.RadiansToDegrees(xi));

            //Step 13
            double NutationInLongitude = AASNutation.NutationInLongitude(JD);
            double NutationInObliquity = AASNutation.NutationInObliquity(JD);

            //Step 14
            lambda += 0.005693 * Math.Cos(l0rad - lambdarad) / Math.Cos(betarad);
            beta   += 0.005693 * Math.Sin(l0rad - lambdarad) * Math.Sin(betarad);

            //Step 15
            Lambda0 += NutationInLongitude / 3600;
            lambda  += NutationInLongitude / 3600;
            e0      += NutationInObliquity / 3600;

            //Step 16
            AAS2DCoordinate ApparentPoleEquatorial = AASCoordinateTransformation.Ecliptic2Equatorial(Lambda0, Beta0, e0);
            double          alpha0dash             = AASCoordinateTransformation.HoursToRadians(ApparentPoleEquatorial.X);
            double          delta0dash             = AASCoordinateTransformation.DegreesToRadians(ApparentPoleEquatorial.Y);
            AAS2DCoordinate ApparentMars           = AASCoordinateTransformation.Ecliptic2Equatorial(lambda, beta, e0);
            double          alphadash = AASCoordinateTransformation.HoursToRadians(ApparentMars.X);
            double          deltadash = AASCoordinateTransformation.DegreesToRadians(ApparentMars.Y);

            //Step 17
            details.P = AASCoordinateTransformation.MapTo0To360Range(AASCoordinateTransformation.RadiansToDegrees(Math.Atan2(Math.Cos(delta0dash) * Math.Sin(alpha0dash - alphadash), Math.Sin(delta0dash) * Math.Cos(deltadash) - Math.Cos(delta0dash) * Math.Sin(deltadash) * Math.Cos(alpha0dash - alphadash))));

            //Step 18
            double          SunLambda     = AASSun.GeometricEclipticLongitude(JD, bHighPrecision);
            double          SunBeta       = AASSun.GeometricEclipticLatitude(JD, bHighPrecision);
            AAS2DCoordinate SunEquatorial = AASCoordinateTransformation.Ecliptic2Equatorial(SunLambda, SunBeta, e0);

            details.X = AASMoonIlluminatedFraction.PositionAngle(SunEquatorial.X, SunEquatorial.Y, alpha, delta);

            //Step 19
            details.d = 9.36 / DELTA;
            details.k = AASIlluminatedFraction.IlluminatedFraction(r, R, DELTA);
            details.q = (1 - details.k) * details.d;

            return(details);
        }
        public static AASSaturnRingDetails Calculate(double JD, bool bHighPrecision)
        {
            //What will be the return value
            AASSaturnRingDetails details = new AASSaturnRingDetails();

            double T  = (JD - 2451545) / 36525;
            double T2 = T * T;

            //Step 1. Calculate the inclination of the plane of the ring and the longitude of the ascending node referred to the ecliptic and mean equinox of the date
            double i        = 28.075216 - 0.012998 * T + 0.000004 * T2;
            double irad     = AASCoordinateTransformation.DegreesToRadians(i);
            double omega    = 169.508470 + 1.394681 * T + 0.000412 * T2;
            double omegarad = AASCoordinateTransformation.DegreesToRadians(omega);

            //Step 2. Calculate the heliocentric longitude, latitude and radius vector of the Earth in the FK5 system
            double l0 = AASEarth.EclipticLongitude(JD, bHighPrecision);
            double b0 = AASEarth.EclipticLatitude(JD, bHighPrecision);

            l0 += AASFK5.CorrectionInLongitude(l0, b0, JD);
            double l0rad = AASCoordinateTransformation.DegreesToRadians(l0);

            b0 += AASFK5.CorrectionInLatitude(l0, JD);
            double b0rad = AASCoordinateTransformation.DegreesToRadians(b0);
            double R     = AASEarth.RadiusVector(JD, bHighPrecision);

            //Step 3. Calculate the corresponding coordinates l,b,r for Saturn but for the instance t-lightraveltime
            double DELTA = 9;
            double PreviousEarthLightTravelTime = 0;
            double EarthLightTravelTime         = AASElliptical.DistanceToLightTime(DELTA);
            double JD1      = JD - EarthLightTravelTime;
            bool   bIterate = true;
            double x        = 0;
            double y        = 0;
            double z        = 0;
            double l        = 0;
            double b        = 0;
            double r        = 0;

            while (bIterate)
            {
                //Calculate the position of Saturn
                l  = AASSaturn.EclipticLongitude(JD1, bHighPrecision);
                b  = AASSaturn.EclipticLatitude(JD1, bHighPrecision);
                l += AASFK5.CorrectionInLongitude(l, b, JD1);
                b += AASFK5.CorrectionInLatitude(l, JD1);

                double lrad = AASCoordinateTransformation.DegreesToRadians(l);
                double brad = AASCoordinateTransformation.DegreesToRadians(b);
                r = AASSaturn.RadiusVector(JD1, bHighPrecision);

                //Step 4
                x     = r * Math.Cos(brad) * Math.Cos(lrad) - R * Math.Cos(l0rad);
                y     = r * Math.Cos(brad) * Math.Sin(lrad) - R * Math.Sin(l0rad);
                z     = r * Math.Sin(brad) - R * Math.Sin(b0rad);
                DELTA = Math.Sqrt(x * x + y * y + z * z);
                EarthLightTravelTime = AASElliptical.DistanceToLightTime(DELTA);

                //Prepare for the next loop around
                bIterate = (Math.Abs(EarthLightTravelTime - PreviousEarthLightTravelTime) > 2e-6); //2e-6 corresponds to 0.17 of a second
                if (bIterate)
                {
                    JD1 = JD - EarthLightTravelTime;
                    PreviousEarthLightTravelTime = EarthLightTravelTime;
                }
            }

            //Step 5. Calculate Saturn's geocentric Longitude and Latitude
            double lambda = Math.Atan2(y, x);
            double beta   = Math.Atan2(z, Math.Sqrt(x * x + y * y));

            //Step 6. Calculate B, a and b
            details.B = Math.Asin(Math.Sin(irad) * Math.Cos(beta) * Math.Sin(lambda - omegarad) - Math.Cos(irad) * Math.Sin(beta));
            details.a = 375.35 / DELTA;
            details.b = details.a * Math.Sin(Math.Abs(details.B));
            details.B = AASCoordinateTransformation.RadiansToDegrees(details.B);

            //Step 7. Calculate the longitude of the ascending node of Saturn's orbit
            double N        = 113.6655 + 0.8771 * T;
            double Nrad     = AASCoordinateTransformation.DegreesToRadians(N);
            double ldash    = l - 0.01759 / r;
            double ldashrad = AASCoordinateTransformation.DegreesToRadians(ldash);
            double bdash    = b - 0.000764 * Math.Cos(ldashrad - Nrad) / r;
            double bdashrad = AASCoordinateTransformation.DegreesToRadians(bdash);

            //Step 8. Calculate Bdash
            details.Bdash = AASCoordinateTransformation.RadiansToDegrees(Math.Asin(Math.Sin(irad) * Math.Cos(bdashrad) * Math.Sin(ldashrad - omegarad) - Math.Cos(irad) * Math.Sin(bdashrad)));

            //Step 9. Calculate DeltaU
            details.U1     = AASCoordinateTransformation.MapTo0To360Range(AASCoordinateTransformation.RadiansToDegrees(Math.Atan2(Math.Sin(irad) * Math.Sin(bdashrad) + Math.Cos(irad) * Math.Cos(bdashrad) * Math.Sin(ldashrad - omegarad), Math.Cos(bdashrad) * Math.Cos(ldashrad - omegarad))));
            details.U2     = AASCoordinateTransformation.MapTo0To360Range(AASCoordinateTransformation.RadiansToDegrees(Math.Atan2(Math.Sin(irad) * Math.Sin(beta) + Math.Cos(irad) * Math.Cos(beta) * Math.Sin(lambda - omegarad), Math.Cos(beta) * Math.Cos(lambda - omegarad))));
            details.DeltaU = Math.Abs(details.U1 - details.U2);
            if (details.DeltaU > 180)
            {
                details.DeltaU = 360 - details.DeltaU;
            }

            //Step 10. Calculate the Nutations
            double Obliquity           = AASNutation.TrueObliquityOfEcliptic(JD);
            double NutationInLongitude = AASNutation.NutationInLongitude(JD);

            //Step 11. Calculate the Ecliptical longitude and latitude of the northern pole of the ring plane
            double lambda0 = omega - 90;
            double beta0   = 90 - i;

            //Step 12. Correct lambda and beta for the aberration of Saturn
            lambda += AASCoordinateTransformation.DegreesToRadians(0.005693 * Math.Cos(l0rad - lambda) / Math.Cos(beta));
            beta   += AASCoordinateTransformation.DegreesToRadians(0.005693 * Math.Sin(l0rad - lambda) * Math.Sin(beta));

            //Step 13. Add nutation in longitude to lambda0 and lambda
            lambda   = AASCoordinateTransformation.RadiansToDegrees(lambda);
            lambda  += NutationInLongitude / 3600;
            lambda   = AASCoordinateTransformation.MapTo0To360Range(lambda);
            lambda0 += NutationInLongitude / 3600;
            lambda0  = AASCoordinateTransformation.MapTo0To360Range(lambda0);

            //Step 14. Convert to equatorial coordinates
            beta = AASCoordinateTransformation.RadiansToDegrees(beta);
            AAS2DCoordinate GeocentricEclipticSaturn = AASCoordinateTransformation.Ecliptic2Equatorial(lambda, beta, Obliquity);
            double          alpha = AASCoordinateTransformation.HoursToRadians(GeocentricEclipticSaturn.X);
            double          delta = AASCoordinateTransformation.DegreesToRadians(GeocentricEclipticSaturn.Y);
            AAS2DCoordinate GeocentricEclipticNorthPole = AASCoordinateTransformation.Ecliptic2Equatorial(lambda0, beta0, Obliquity);
            double          alpha0 = AASCoordinateTransformation.HoursToRadians(GeocentricEclipticNorthPole.X);
            double          delta0 = AASCoordinateTransformation.DegreesToRadians(GeocentricEclipticNorthPole.Y);

            //Step 15. Calculate the Position angle
            details.P = AASCoordinateTransformation.RadiansToDegrees(Math.Atan2(Math.Cos(delta0) * Math.Sin(alpha0 - alpha), Math.Sin(delta0) * Math.Cos(delta) - Math.Cos(delta0) * Math.Sin(delta) * Math.Cos(alpha0 - alpha)));

            return(details);
        }