Example #1
0
        /// <summary>
        /// Calculates phases of the sun for a single day and latitude/longitude
        /// and optionally the observer height (in meters) relative to the horizon
        /// </summary>
        /// <param name="date"></param>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static IEnumerable <SunPhase> GetSunPhases(DateTimeOffset date, double lat, double lng, double height = 0)
        {
            var lw  = Constants.Rad * -lng;
            var phi = Constants.Rad * lat;

            var dh = SunTime.GetObserverAngle(height);

            var d = date.ToDays();

            var n  = SunTime.GetJulianCycle(d, lw);
            var ds = SunTime.GetApproxTransit(0, lw, n);

            var m   = Sun.GetMeanAnomaly(ds);
            var l   = Sun.GetEclipticLongitude(m);
            var dec = Position.GetDeclination(l, 0);

            var jnoon     = SunTime.GetSolarTransitJ(ds, m, l);
            var solarNoon = jnoon.FromJulian();
            var nadir     = (jnoon - 0.5).FromJulian();

            var sunPhaseCol = new List <SunPhase>
            {
                new SunPhase(SunPhaseName.SolarNoon, solarNoon),
                new SunPhase(SunPhaseName.Nadir, nadir)
            };

            foreach (var sunPhase in SunPhaseAngle.List)
            {
                var h0   = (sunPhase.Angle + dh) * Constants.Rad;
                var jset = SunTime.GetSetJ(h0, lw, phi, dec, n, m, l);

                if (double.IsNaN(jset) || double.IsInfinity(jset))
                {
                    continue;
                }

                var jrise = jnoon - (jset - jnoon);
                sunPhaseCol.Add(new SunPhase(sunPhase.RiseName, jrise.FromJulian()));
                sunPhaseCol.Add(new SunPhase(sunPhase.SetName, jset.FromJulian()));
            }

            return(sunPhaseCol);
        }
Example #2
0
        static void Main(string[] args)
        {
            SunTime sun = new SunTime();

            sun.Latitude   = 55.567231;
            sun.Longtitude = 9.754025;


            Console.WriteLine("Requesting API");
            Console.WriteLine($"\nStatus: {sun.ApiStatus}");

            SunTimeData data = sun.SunTimeData;

            Console.WriteLine($"Sunrise: {sun.Sunrise}\nSunset: {sun.Sunset}\nSolar noon: {data.SolarNoon}\n" +
                              $"Day length: {data.DayLength}\nCivil twilight: {data.CivilTwilightBegin} --> {data.CivilTwilightEnd}\n" +
                              $"Nautical twilight: {data.NauticalTwilightBegin} --> {data.NauticalTwilightEnd}\n" +
                              $"Astronomical twilight: {data.AstronomicalTwilightBegin} --> {data.AstronomicalTwilightEnd}");

            Console.ReadKey();
        }
Example #3
0
 // adds a custom time to the times config
 public static void AddTime(SunTime sunTime)
 {
     SunTimes.Add(sunTime);
 }