CSharp: SunTimeCalculator

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAppPdfDemo
{



    /// <summary>
    /// 
    /// </summary>
    public class SunTimeCalculator
    {


        #region 輔助函數
        /// <summary>
        /// 曆元2000.0,即以2000年第一天開端爲計日起始(天文學以第一天爲0日而非1日)。
        /// 它與UT(就是世界時,格林尼治平均太陽時)1999年末重合。 
        /// </summary>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        private static long Days_since_2000_Jan_0(int y, int m, int d)
        {
            return (367L * (y) - ((7 * ((y) + (((m) + 9) / 12))) / 4) + ((275 * (m)) / 9) + (d) - 730530L);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Revolution(double x)
        {
            return (x - 360.0 * Math.Floor(x * Inv360));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Rev180(double x)
        {
            return (x - 360.0 * Math.Floor(x * Inv360 + 0.5));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private static double GMST0(double d)
        {
            double sidtim0;
            sidtim0 = Revolution((180.0 + 356.0470 + 282.9404) +
                (0.9856002585 + 4.70935E-5) * d);
            return sidtim0;
        }

        /// <summary>
        /// 
        /// </summary>
        private static double Inv360 = 1.0 / 360.0;
        #endregion

        #region 度與弧度轉換系數,爲球面三角計算作準備
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Sind(double x)
        {
            return Math.Sin(x * Degrad);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Cosd(double x)
        {
            return Math.Cos(x * Degrad);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Tand(double x)
        {
            return Math.Tan(x * Degrad);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Atand(double x)
        {
            return Radge * Math.Atan(x);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Asind(double x)
        {
            return Radge * Math.Asin(x);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Acosd(double x)
        {
            return Radge * Math.Acos(x);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="y"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        private static double Atan2d(double y, double x)
        {
            return Radge * Math.Atan2(y, x);

        }
        /// <summary>
        /// 
        /// </summary>
        private static double Radge = 180.0 / Math.PI;
        /// <summary>
        /// 
        /// </summary>
        private static double Degrad = Math.PI / 180.0;

        #endregion

        #region 與日出日落時間相關計算
        /// <summary>
        /// 
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="lon"></param>
        /// <param name="lat"></param>
        /// <param name="altit"></param>
        /// <param name="upper_limb"></param>
        /// <returns></returns>
        private static double DayLen(int year, int month, int day, double lon, double lat,
            double altit, int upper_limb)
        {
            double d,  /* Days since 2000 Jan 0.0 (negative before) */
                obl_ecl,    /* Obliquity (inclination) of Earth's axis */
                //黃赤交角,在2000.0曆元下國際規定爲23度26分21.448秒,但有很小的時間演化。

                sr,         /* Solar distance, astronomical units */
                slon,       /* True solar longitude */
                sin_sdecl,  /* Sine of Sun's declination */
                //太陽赤緯的正弦值。
                cos_sdecl,  /* Cosine of Sun's declination */
                sradius,    /* Sun's apparent radius */
                t;          /* Diurnal arc */

            /* Compute d of 12h local mean solar time */
            d = Days_since_2000_Jan_0(year, month, day) + 0.5 - lon / 360.0;

            /* Compute obliquity of ecliptic (inclination of Earth's axis) */
            obl_ecl = 23.4393 - 3.563E-7 * d;
            //這個黃赤交角時變公式來歷複雜,很大程度是經驗性的,不必追究。

            /* Compute Sun's position */
            slon = 0.0;
            sr = 0.0;
            Sunpos(d, ref slon, ref sr);

            /* Compute sine and cosine of Sun's declination */
            sin_sdecl = Sind(obl_ecl) * Sind(slon);
            cos_sdecl = Math.Sqrt(1.0 - sin_sdecl * sin_sdecl);
            //用球面三角學公式計算太陽赤緯。

            /* Compute the Sun's apparent radius, degrees */
            sradius = 0.2666 / sr;
            //視半徑,同前。

            /* Do correction to upper limb, if necessary */
            if (upper_limb != 0)
                altit -= sradius;

            /* Compute the diurnal arc that the Sun traverses to reach */
            /* the specified altitide altit: */
            //根據設定的地平高度判據計算週日弧長。
            double cost;
            cost = (Sind(altit) - Sind(lat) * sin_sdecl) /
                (Cosd(lat) * cos_sdecl);
            if (cost >= 1.0)
                t = 0.0;                      /* Sun always below altit */
            //極夜。
            else if (cost <= -1.0)
                t = 24.0;                     /* Sun always above altit */
            //極晝。
            else t = (2.0 / 15.0) * Acosd(cost); /* The diurnal arc, hours */
            //週日弧換算成小時計。
            return t;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="lon"></param>
        /// <param name="r"></param>
        private static void Sunpos(double d, ref double lon, ref double r)
        {
            double M,//太陽的平均近點角,從太陽觀察到的地球(=從地球看到太陽的)距近日點(近地點)的角度。
                w, //近日點的平均黃道經度。
                e, //地球橢圓公轉軌道離心率。
                E, //太陽的偏近點角。計算公式見下面。

                x, y,
                v;  //真近點角,太陽在任意時刻的真實近點角。


            M = Revolution(356.0470 + 0.9856002585 * d);//自變量的組成:2000.0時刻太陽黃經爲356.0470度,此後每天約推進一度(360度/365天
            w = 282.9404 + 4.70935E-5 * d;//近日點的平均黃經。

            e = 0.016709 - 1.151E-9 * d;//地球公轉橢圓軌道離心率的時間演化。以上公式和黃赤交角公式一樣,不必深究。

            E = M + e * Radge * Sind(M) * (1.0 + e * Cosd(M));
            x = Cosd(E) - e;
            y = Math.Sqrt(1.0 - e * e) * Sind(E);
            r = Math.Sqrt(x * x + y * y);
            v = Atan2d(y, x);
            lon = v + w;
            if (lon >= 360.0)
                lon -= 360.0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="RA"></param>
        /// <param name="dec"></param>
        /// <param name="r"></param>
        private static void Sun_RA_dec(double d, ref double RA, ref double dec, ref double r)
        {
            double lon, obl_ecl, x, y, z;
            lon = 0.0;

            Sunpos(d, ref lon, ref r);
            //計算太陽的黃道座標。

            x = r * Cosd(lon);
            y = r * Sind(lon);
            //計算太陽的直角座標。

            obl_ecl = 23.4393 - 3.563E-7 * d;
            //黃赤交角,同前。

            z = y * Sind(obl_ecl);
            y = y * Cosd(obl_ecl);
            //把太陽的黃道座標轉換成赤道座標(暫改用直角座標)。

            RA = Atan2d(y, x);
            dec = Atan2d(z, Math.Sqrt(x * x + y * y));
            //最後轉成赤道座標。顯然太陽的位置是由黃道座標方便地直接確定的,但必須轉換到赤
            //道座標裏才能結合地球的自轉確定我們需要的白晝長度。

        }
        /// <summary>
        /// 日出沒時刻計算
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="day">日</param>
        /// <param name="lon"></param>
        /// <param name="lat"></param>
        /// <param name="altit"></param>
        /// <param name="upper_limb"></param>
        /// <param name="trise">日出時刻</param>
        /// <param name="tset">日沒時刻</param>
        /// <returns>太陽有出沒現象,返回0 極晝,返回+1 極夜,返回-1</returns>
        private static int SunRiset(int year, int month, int day, double lon, double lat,
            double altit, int upper_limb, ref double trise, ref double tset)
        {
            double d,  /* Days since 2000 Jan 0.0 (negative before) */
                //以曆元2000.0起算的日數。

                sr,         /* Solar distance, astronomical units */
                //太陽距離,以天文單位計算(約1.5億公里)。      

                sRA,        /* Sun's Right Ascension */
                //同前,太陽赤經。

                sdec,       /* Sun's declination */
                //太陽赤緯。

                sradius,    /* Sun's apparent radius */
                //太陽視半徑,約16分(受日地距離、大氣折射等諸多影響)

                t,          /* Diurnal arc */
                //週日弧,太陽一天在天上走過的弧長。

                tsouth,     /* Time when Sun is at south */
                sidtime;    /* Local sidereal time */
            //當地恆星時,即地球的真實自轉週期。比平均太陽日(日常時間)長3分56秒。      

            int rc = 0; /* Return cde from function - usually 0 */

            /* Compute d of 12h local mean solar time */
            d = Days_since_2000_Jan_0(year, month, day) + 0.5 - lon / 360.0;
            //計算觀測地當日中午時刻對應2000.0起算的日數。

            /* Compute local sideral time of this moment */
            sidtime = Revolution(GMST0(d) + 180.0 + lon);
            //計算同時刻的當地恆星時(以角度爲單位)。以格林尼治爲基準,用經度差校正。

            /* Compute Sun's RA + Decl at this moment */
            sRA = 0.0;
            sdec = 0.0;
            sr = 0.0;
            Sun_RA_dec(d, ref sRA, ref sdec, ref sr);
            //計算同時刻太陽赤經赤緯。

            /* Compute time when Sun is at south - in hours UT */
            tsouth = 12.0 - Rev180(sidtime - sRA) / 15.0;
            //計算太陽日的正午時刻,以世界時(格林尼治平太陽時)的小時計。

            /* Compute the Sun's apparent radius, degrees */
            sradius = 0.2666 / sr;
            //太陽視半徑。0.2666是一天文單位處的太陽視半徑(角度)。

            /* Do correction to upper limb, if necessary */
            if (upper_limb != 0)
                altit -= sradius;
            //如果要用上邊緣,就要扣除一個視半徑。

            /* Compute the diurnal arc that the Sun traverses to reach */
            //計算週日弧。直接利用球面三角公式。如果碰到極晝極夜問題,同前處理。
            /* the specified altitide altit: */

            double cost;
            cost = (Sind(altit) - Sind(lat) * Sind(sdec)) /
                (Cosd(lat) * Cosd(sdec));
            if (cost >= 1.0)
            {
                rc = -1;
                t = 0.0;
            }
            else
            {
                if (cost <= -1.0)
                {
                    rc = +1;
                    t = 12.0;      /* Sun always above altit */
                }
                else
                    t = Acosd(cost) / 15.0;   /* The diurnal arc, hours */
            }


            /* Store rise and set times - in hours UT */
            trise = tsouth - t;
            tset = tsouth + t;

            return rc;
        }
        #endregion

        /// <summary>
        /// 計算日出日沒時間
        /// </summary>
        /// <param name="date"></param>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <returns></returns>
        public static SunTimeResult GetSunTime(DateTime date, double longitude, double latitude)
        {
            double start = 0;
            double end = 0;
            SunRiset(date.Year, date.Month, date.Day, longitude, latitude, -35.0 / 60.0, 1, ref start, ref end);
            DateTime sunrise = ToLocalTime(date, start);
            DateTime sunset = ToLocalTime(date, end);
            return new SunTimeResult(sunrise, sunset);
        }

        #region moon 月亮計算


        private const double dayMs = 86400000;
        private const double J1970 = 2440588;
        private const double J2000 = 2451545;
        private const double PI = Math.PI;
        private const double rad = Math.PI / 180.0;
        private const double e = rad * 23.4397; // obliquity of the Earth


        /// <summary>
        /// 
        /// </summary>
        public class SunTime
        {
            public double Angle { get; set; }
            public string MorningName { get; set; }
            public string EveningName { get; set; }
        }
        /// <summary>
        /// 
        /// </summary>
        public class SunTimeRiseSet : SunTime
        {
            public DateTime RiseTime { get; set; }
            public DateTime SetTime { get; set; }
        }

        /// <summary>
        /// sun times configuration (angle, morning name, evening name)
        /// </summary>

        public static List<SunTime> SunTimes = new List<SunTime>(new SunTime[]
        {
            new SunTime { Angle = -0.833, MorningName = "sunrise", EveningName = "sunset" },
            new SunTime { Angle = -0.3, MorningName = "sunriseEnd", EveningName = "sunsetStart" },
            new SunTime { Angle = -6, MorningName = "dawn", EveningName = "dusk" },
            new SunTime { Angle = -12, MorningName = "nauticalDawn", EveningName = "nauticalDusk" },
            new SunTime { Angle = -18, MorningName = "nightEnd", EveningName = "night" },
            new SunTime { Angle = 6, MorningName = "goldenHourEnd", EveningName = "goldenHour" }
        });

        /// <summary>
        /// adds a custom time to the times config
        /// </summary>
        /// <param name="sunTime"></param>

        public static void AddTime(SunTime sunTime)
        {
            SunTimes.Add(sunTime);
        }
        /// <summary>
        /// 
        /// </summary>
        public class RaDec
        {
            public double ra = 0;
            public double dec = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double ToJulianDate(DateTime dt)
        {
            dt = dt.Kind == DateTimeKind.Local ? dt.ToUniversalTime() : dt;
            return (dt - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / dayMs - 0.5 + J1970;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jd"></param>
        /// <returns></returns>
        public static DateTime FromJulianDate(double jd)
        {
            return double.IsNaN(jd)
                ? DateTime.MinValue
                : new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((jd + 0.5 - J1970) * dayMs);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double JulianDays(DateTime dt)
        {
            dt = dt.Kind == DateTimeKind.Local ? dt.ToUniversalTime() : dt;
            return ToJulianDate(dt) - J2000;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="l"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double RightAscension(double l, double b)
        {
            return Math.Atan2(Math.Sin(l) * Math.Cos(e) - Math.Tan(b) * Math.Sin(e), Math.Cos(l));
        }

        public static double Declination(double l, double b)
        {
            return Math.Asin(Math.Sin(b) * Math.Cos(e) + Math.Cos(b) * Math.Sin(e) * Math.Sin(l));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="H"></param>
        /// <param name="phi"></param>
        /// <param name="dec"></param>
        /// <returns></returns>
        public static double Azimuth(double H, double phi, double dec)
        {
            return Math.Atan2(Math.Sin(H), Math.Cos(H) * Math.Sin(phi) - Math.Tan(dec) * Math.Cos(phi));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="H"></param>
        /// <param name="phi"></param>
        /// <param name="dec"></param>
        /// <returns></returns>
        public static double Altitude(double H, double phi, double dec)
        {
            return Math.Asin(Math.Sin(phi) * Math.Sin(dec) + Math.Cos(phi) * Math.Cos(dec) * Math.Cos(H));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="lw"></param>
        /// <returns></returns>
        public static double SiderealTime(double d, double lw)
        {
            return rad * (280.16 + 360.9856235 * d) - lw;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="h"></param>
        /// <returns></returns>
        public static double AstroRefraction(double h)
        {
            if (h < 0) // the following formula works for positive altitudes only.
            {
                h = 0; // if h = -0.08901179 a div/0 would occur.
            }

            // formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
            // 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
            return 0.0002967 / Math.Tan(h + 0.00312536 / (h + 0.08901179));
        }

        /// <summary>
        /// general sun calculations
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>

        public static double SolarMeanAnomaly(double d)
        {
            return rad * (357.5291 + 0.98560028 * d);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        public static DateTime HoursLater(DateTime dt, double h)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(dt.ValueOf() + h * dayMs / 24);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="M"></param>
        /// <returns></returns>
        public static double EclipticLongitude(double M)
        {
            double C = rad * (1.9148 * Math.Sin(M) + 0.02 * Math.Sin(2 * M) +
                              0.0003 * Math.Sin(3 * M)); // equation of center
            double P = rad * 102.9372; // perihelion of the Earth
            return M + C + P + PI;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static RaDec SunCoords(double d)
        {
            double M = SolarMeanAnomaly(d);
            double L = EclipticLongitude(M);
            return new RaDec { dec = Declination(L, 0), ra = RightAscension(L, 0) };
        }
        /// <summary>
        /// 
        /// </summary>
        public class MoonRaDecDist
        {
            public double ra = 0;
            public double dec = 0;
            public double dist = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static MoonRaDecDist MoonCoords(double d) // geocentric ecliptic coordinates of the moon
        {
            double L = rad * (218.316 + 13.176396 * d); // ecliptic longitude
            double M = rad * (134.963 + 13.064993 * d); // mean anomaly
            double F = rad * (93.272 + 13.229350 * d); // mean distance

            double l = L + rad * 6.289 * Math.Sin(M); // longitude
            double b = rad * 5.128 * Math.Sin(F); // latitude
            double dt = 385001 - 20905 * Math.Cos(M); // distance to the moon in km

            return new MoonRaDecDist { ra = RightAscension(l, b), dec = Declination(l, b), dist = dt };
        }
        /// <summary>
        /// 
        /// </summary>
        public class MoonAzAltDistPa
        {
            public double azimuth = 0;
            public double altitude = 0;
            public double distance = 0;
            public double parallacticAngle = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        public class MoonFracPhaseAngle
        {
            public double fraction = 0;
            public double phase = 0;
            public double angle = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <returns></returns>
        public static MoonAzAltDistPa GetMoonPosition(DateTime dt, double lat, double lng)
        {
            double lw = rad * -lng;
            double phi = rad * lat;
            double d = JulianDays(dt);

            MoonRaDecDist c = MoonCoords(d);
            double H = SiderealTime(d, lw) - c.ra;
            double h = Altitude(H, phi, c.dec);
            // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
            double pa = Math.Atan2(Math.Sin(H), Math.Tan(phi) * Math.Cos(c.dec) - Math.Sin(c.dec) * Math.Cos(H));

            h += AstroRefraction(h); // altitude correction for refraction
            return new MoonAzAltDistPa { azimuth = Azimuth(H, phi, c.dec), altitude = h, distance = c.dist, parallacticAngle = pa };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static MoonFracPhaseAngle GetMoonIllumination(DateTime dt)
        {
            double d = JulianDays(dt);
            RaDec s = SunCoords(d);
            MoonRaDecDist m = MoonCoords(d);
            double sdist = 149598000; // distance from Earth to Sun in km
            double phi = Math.Acos(Math.Sin(s.dec) * Math.Sin(m.dec) +
                                   Math.Cos(s.dec) * Math.Cos(m.dec) * Math.Cos(s.ra - m.ra));
            double inc = Math.Atan2(sdist * Math.Sin(phi), m.dist - sdist * Math.Cos(phi));
            double angle = Math.Atan2(Math.Cos(s.dec) * Math.Sin(s.ra - m.ra), Math.Sin(s.dec) * Math.Cos(m.dec) -
                Math.Cos(s.dec) * Math.Sin(m.dec) * Math.Cos(s.ra - m.ra));
            return new MoonFracPhaseAngle
            {
                fraction = (1 + Math.Cos(inc)) / 2,
                phase = 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / PI,
                angle = angle
            };
        }

            /// <summary>
            /// DateTime.Max = always up, DateTime.Min = always down
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="lat"></param>
            /// <param name="lng"></param>
            /// <param name="risem"></param>
            /// <param name="setm"></param>
            /// <param name="alwaysUp"></param>
            /// <param name="alwaysDown"></param>
            public static void MoonRiset(DateTime dt, double lat, double lng, out DateTime risem, out DateTime setm,
                out bool? alwaysUp, out bool? alwaysDown)
            {
               dt = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, DateTimeKind.Utc);
                DateTime t = dt;

                double hc = 0.133 * rad;
                double h0 = GetMoonPosition(t, lat, lng).altitude - hc;
                double h1, h2, rise = 0, set = 0, a, b, xe, ye = 0, d, x1, x2, dx;
                int roots;

                for (double i = 1.0; i <= 24.0; i += 2.0)
                {
                    h1 = GetMoonPosition(HoursLater(t, i), lat, lng).altitude - hc;
                    h2 = GetMoonPosition(HoursLater(t, i + 1), lat, lng).altitude - hc;

                    a = (h0 + h2) / 2 - h1;
                    b = (h2 - h0) / 2;
                    xe = -b / (2 * a);
                    ye = (a * xe + b) * xe + h1;
                    d = b * b - 4 * a * h1;
                    roots = 0;

                    if (d >= 0)
                    {
                        dx = Math.Sqrt(d) / (Math.Abs(a) * 2);
                        x1 = xe - dx;
                        x2 = xe + dx;
                        if (Math.Abs(x1) <= 1)
                        {
                            roots++;
                        }

                        if (Math.Abs(x2) <= 1)
                        {
                            roots++;
                        }

                        if (x1 < -1)
                        {
                            x1 = x2;
                        }

                        if (roots == 1)
                        {
                            if (h0 < 0)
                            {
                                rise = i + x1;
                            }
                            else
                            {
                                set = i + x1;
                            }
                        }
                        else if (roots == 2)
                        {
                            rise = i + (ye < 0 ? x2 : x1);
                            set = i + (ye < 0 ? x1 : x2);
                        }

                        if (rise > 0 && set > 0)
                        {
                            break;
                        }

                        h0 = h2;
                    }
                }

                risem = DateTime.MinValue;
                setm = DateTime.MinValue;

                if (rise > 0)
                {
                    risem = HoursLater(t, rise);
                }

                if (set > 0)
                {
                    setm = HoursLater(t, set);
                }

                alwaysUp = null;
                alwaysDown = null;

                if (rise < 0 && set < 0)
                {
                    if (ye > 0)
                    {
                        alwaysUp = true;
                        alwaysDown = false;
                        risem = DateTime.MaxValue;
                        setm = DateTime.MaxValue;
                    }
                    else
                    {
                        alwaysDown = true;
                        alwaysUp = false;
                    }
                }           
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="year"></param>
            /// <param name="month"></param>
            /// <param name="day"></param>
            /// <param name="lat"></param>
            /// <param name="lng"></param>
            /// <param name="risem"></param>
            /// <param name="setm"></param>
            /// <param name="alwaysUp"></param>
            /// <param name="alwaysDown"></param>
            public static void MoonRisetInt(int year,int month,int day, double lat, double lng, out DateTime risem, out DateTime setm,
                    out bool? alwaysUp, out bool? alwaysDown)
            {
               DateTime dt = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Utc);

                DateTime t = dt;

                double hc = 0.133 * rad;
                double h0 = GetMoonPosition(t, lat, lng).altitude - hc;
                double h1, h2, rise = 0, set = 0, a, b, xe, ye = 0, d, x1, x2, dx;
                int roots;

                for (double i = 1.0; i <= 24.0; i += 2.0)
                {
                    h1 = GetMoonPosition(HoursLater(t, i), lat, lng).altitude - hc;
                    h2 = GetMoonPosition(HoursLater(t, i + 1), lat, lng).altitude - hc;

                    a = (h0 + h2) / 2 - h1;
                    b = (h2 - h0) / 2;
                    xe = -b / (2 * a);
                    ye = (a * xe + b) * xe + h1;
                    d = b * b - 4 * a * h1;
                    roots = 0;

                    if (d >= 0)
                    {
                        dx = Math.Sqrt(d) / (Math.Abs(a) * 2);
                        x1 = xe - dx;
                        x2 = xe + dx;
                        if (Math.Abs(x1) <= 1)
                        {
                            roots++;
                        }

                        if (Math.Abs(x2) <= 1)
                        {
                            roots++;
                        }

                        if (x1 < -1)
                        {
                            x1 = x2;
                        }

                        if (roots == 1)
                        {
                            if (h0 < 0)
                            {
                                rise = i + x1;
                            }
                            else
                            {
                                set = i + x1;
                            }
                        }
                        else if (roots == 2)
                        {
                            rise = i + (ye < 0 ? x2 : x1);
                            set = i + (ye < 0 ? x1 : x2);
                        }

                        if (rise > 0 && set > 0)
                        {
                            break;
                        }

                        h0 = h2;
                    }
                }

                risem = DateTime.MinValue;
                setm = DateTime.MinValue;

                if (rise > 0)
                {
                    risem = HoursLater(t, rise);
                }

                if (set > 0)
                {
                    setm = HoursLater(t, set);
                }

                alwaysUp = null; //false;//
                alwaysDown = null; //false;//

                if (rise < 0 && set < 0)
                {
                    if (ye > 0)
                    {
                        alwaysUp = true;
                        alwaysDown = false;
                        risem = DateTime.MaxValue;
                        setm = DateTime.MaxValue;
                    }
                    else
                    {
                        alwaysDown = true;
                        alwaysUp = false;
                    }
                }
            }
        #endregion 

        /// <summary>
        /// 計算月亮升起和降落時間
        /// </summary>
        /// <param name="date"></param>
        /// 
        /// 
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <returns></returns>
        public static MoonTimeResult GetMoonTime(DateTime date, double latitude, double longitude)
        {
            double moonrise = 0;
            double moonset = 0;
            DateTime dt = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Utc);
            bool? up = null;
            bool? down = null;
            DateTime rise;
            DateTime set;
            MoonRiset(dt, latitude,longitude,  out rise, out set, out up, out down); //, out up, out down
            DateTime moonriseTime =rise;// ToLocalTime(rise, moonrise);// 
            DateTime moonsetTime = set;//ToLocalTime(set, moonset); // 
            return new MoonTimeResult(moonriseTime, moonsetTime);
        }

        /// <summary>
        /// 私有方法:將 UTC 時間轉換爲本地時間
        /// </summary>
        /// <param name="time"></param>
        /// <param name="utTime"></param>
        /// <returns></returns>
 
        private static DateTime ToLocalTime(DateTime time, double utTime)
        {
            int hour = Convert.ToInt32(Math.Floor(utTime));
            double temp = utTime - hour;
            hour += 8; // 轉換爲東8區北京時間
            temp = temp * 60;
            int minute = Convert.ToInt32(Math.Floor(temp));
            try
            {
                return new DateTime(time.Year, time.Month, time.Day, hour, minute, 0);
            }
            catch
            {
                return new DateTime(time.Year, time.Month, time.Day, 0, 0, 0);
            }
        }




    }


    /// <summary>
    /// 日出日落時間結果類
    /// </summary>
    public class SunTimeResult
    {
        /// <summary>
        /// 
        /// </summary>
        public DateTime SunriseTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime SunsetTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sunrise"></param>
        /// <param name="sunset"></param>
        public SunTimeResult(DateTime sunrise, DateTime sunset)
        {
            SunriseTime = sunrise;
            SunsetTime = sunset;
        }
    }

    /// <summary>
    /// 月亮升起和降落時間結果類
    /// </summary>
    public class MoonTimeResult
    {

        /// <summary>
        /// 
        /// </summary>
        public DateTime MoonriseTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime MoonsetTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="moonrise"></param>
        /// <param name="moonset"></param>
        public MoonTimeResult(DateTime moonrise, DateTime moonset)
        {
            MoonriseTime = moonrise;
            MoonsetTime = moonset;
        }
    }




}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章