Exemple #1
0
        public void TestSolarTimes_NearEquator()
        {
            // set up
            double latitude  = -8.506810; // Ubud, Bali, Indonesia
            double longitude = 115.262482;

            var date = new DateTimeOffset(2020, 7, 1, 12, 0, 0, TimeSpan.FromHours(8));

            // run
            var times = SunCalc.GetTimes(date, latitude, longitude);

            // check
            // 2020-07-01: 06:33 - 18:11
            Assert.IsTrue(times.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times.Sunrise.Value, 6, 33), "sunrise time must match");

            Assert.IsTrue(times.Sunset.HasValue, "sunset must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times.Sunset.Value, 18, 11), "sunset time must match");

            TimeSpan sunriseDuration = times.SunriseSunsetTimes[SunTimeType.SunriseEnd] - times.Sunrise.Value;
            TimeSpan sunsetDuration  = times.Sunset.Value - times.SunriseSunsetTimes[SunTimeType.SunsetStart];

            Assert.AreEqual(2.3, sunriseDuration.TotalMinutes, 0.1, "sunrise duration must be 2.3 minutes");
            Assert.AreEqual(2.3, sunsetDuration.TotalMinutes, 0.1, "sunset duration must be 2.3 minutes");
        }
Exemple #2
0
        public void TestSolarTimes_PositiveLatLong()
        {
            // set up
            double latitude  = 48.137222; // munich
            double longitude = 11.575556;

            var date1 = new DateTimeOffset(2020, 7, 31, 12, 0, 0, TimeSpan.FromHours(2.0)); // MESZ
            var date2 = new DateTimeOffset(2020, 12, 5, 12, 0, 0, TimeSpan.FromHours(1.0)); // MEZ

            // run
            var times1 = SunCalc.GetTimes(date1, latitude, longitude);
            var times2 = SunCalc.GetTimes(date2, latitude, longitude);

            // check
            // 2020-07-31: 05:48 - 20:50
            Assert.IsTrue(times1.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times1.Sunrise.Value, 5, 48), "sunrise time must match");

            Assert.IsTrue(times1.Sunset.HasValue, "sunset must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times1.Sunset.Value, 20, 50), "sunset time must match");

            // 2020-12-05: 07:48 - 16:20
            Assert.IsTrue(times2.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times2.Sunrise.Value, 7, 48), "sunrise time must match");

            Assert.IsTrue(times2.Sunset.HasValue, "sunset must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times2.Sunset.Value, 16, 20), "sunset time must match");
        }
        public void GetTimes_returns_null_if_event_does_not_occur()
        {
            var times = SunCalc.GetTimes(DateTime.Parse("2020-05-21 12:00:00"), 51.9947, -1.4819);

            times.Night.Should().BeNull();
            times.NightEnd.Should().BeNull();
        }
Exemple #4
0
        public void TestSolarTimes_AllDayUp_AllDayDown()
        {
            // set up
            double latitude  = -70.674444; // Neumayer-Station III
            double longitude = -8.274167;

            var date1 = new DateTimeOffset(2020, 6, 21, 12, 0, 0, TimeSpan.Zero); // UTC
            var date2 = new DateTimeOffset(2020, 12, 21, 12, 0, 0, TimeSpan.Zero);

            // run
            var times1 = SunCalc.GetTimes(date1, latitude, longitude);
            var times2 = SunCalc.GetTimes(date2, latitude, longitude);

            // check
            // midwinter
            Assert.IsFalse(times1.Sunrise.HasValue, "sunrise must not have been set");
            Assert.IsFalse(times1.Sunset.HasValue, "sunset must not have been set");

            // midsummer
            Assert.IsFalse(times2.Sunrise.HasValue, "sunrise must not have been set");
            Assert.IsFalse(times2.Sunset.HasValue, "sunset must not have been set");

            // the difference between midwinter and midsummer is that midwinter has no Night times set
            Assert.IsTrue(times1.SunriseSunsetTimes.ContainsKey(SunTimeType.Night), "midwinter has a Night time set");
            Assert.IsFalse(times2.SunriseSunsetTimes.ContainsKey(SunTimeType.Night), "midsummer has no Night time set");
        }
        public void Get_Sun_Phases_Adjusts_Sun_Phases_When_Additionally_Given_The_Observer_Height()
        {
            //Arrange
            var heightTestData = new List <SunPhase>
            {
                new SunPhase(SunPhaseName.SolarNoon, new DateTime(2013, 3, 5, 10, 10, 57, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Nadir, new DateTime(2013, 3, 4, 22, 10, 57, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Sunrise, new DateTime(2013, 3, 5, 4, 25, 7, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Sunset, new DateTime(2013, 3, 5, 15, 56, 46, DateTimeKind.Utc))
            };

            var date   = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);
            var lat    = 50.5;
            var lng    = 30.5;
            var height = 2000;

            //Act
            var sunPhases = SunCalc.GetSunPhases(date, lat, lng, height).ToList();

            //Assert
            foreach (var testSunPhase in heightTestData)
            {
                var sunPhaseValue = sunPhases.First(x => x.Name.Value == testSunPhase.Name.Value);

                var testDataPhaseTime = testSunPhase.PhaseTime.ToString("yyyy-MM-dd hh:mm:ss");
                var sunPhaseTime      = sunPhaseValue.PhaseTime.ToString("yyyy-MM-dd hh:mm:ss");
                Assert.Equal(testDataPhaseTime, sunPhaseTime);
            }
        }
        public void GetTimes_adjusts_sun_phases_when_additionally_given_the_observer_height()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng, _height);

            times.SolarNoon.ToDateString().Should().Be("2013-03-05T10:10:57Z");
            times.Nadir.ToDateString().Should().Be("2013-03-04T22:10:57Z");
            times.Sunrise.ToDateString().Should().Be("2013-03-05T04:25:07Z");
            times.Sunset.ToDateString().Should().Be("2013-03-05T15:56:46Z");
        }
    public CelestialPositionResult CalculatePosition(System.DateTime date, double latitude, double longitude, double alture = 0)
    {
        var res = SunCalc.getMoonPosition(date, latitude, longitude);
        CelestialPositionResult result = new CelestialPositionResult();

        result.altitude    = res.altitude;
        result.azimuth     = res.azimuth + Math.PI;
        result.parallactic = res.parallacticAngle;
        result.distance    = res.distance;
        result.radius      = CelestialScale.MoonRadiusKm;
        return(result);
    }
        public void Get_Sun_Phases_Works_At_North_Pole()
        {
            //Arrange
            var date = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);
            var lat  = 90;
            var lng  = 135;

            //Act
            var sunPhases = SunCalc.GetSunPhases(date, lat, lng).ToList();

            //Assert
            Assert.Equal(2, sunPhases.Count);
        }
        public void Get_Moon_Illumination_Returns_Fraction_And_Angle_Of_Moons_Illuminated_Limb_And_Phase()
        {
            //Arrange
            var date = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);

            //Act
            var moonIllum = SunCalc.GetMoonIllumination(date);

            //Assert
            Assert.Equal(0.4848068202456373, moonIllum.Fraction, 15);
            Assert.Equal(0.7548368838538762, moonIllum.Phase, 15);
            Assert.Equal(1.6732942678578346, moonIllum.Angle, 15);
        }
        public void Get_Sun_Position_Returns_Azimuth_And_Altitude_For_The_Given_Time_And_Location()
        {
            //Arrange
            var date = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);
            var lat  = 50.5;
            var lng  = 30.5;

            //Act
            var sunPosition = SunCalc.GetSunPosition(date, lat, lng);

            //Assert
            Assert.Equal(-2.5003175907168385, sunPosition.Azimuth, 15);
            Assert.Equal(-0.7000406838781611, sunPosition.Altitude, 15);
        }
Exemple #11
0
    // Update is called once per frame
    void Update()
    {
        double latitude  = Input.location.lastData.latitude;        //-17.3663289;
        double longitude = Input.location.lastData.longitude;       //-66.1758675;

        var moonPosition     = SunCalc.getMoonPosition(DateTime.UtcNow, latitude, longitude);
        var moonIllumination = SunCalc.getMoonIllumination(DateTime.UtcNow);
        var phase            = MoonPhase.GetMoonPhase((float)moonIllumination.phase);

        phaseText.text    = phase.Name;
        distanceText.text = moonPosition.distance + " Km.";

        //Texture2D texture = Resources.Load<Texture2D>(phase.ImagePath);
    }
        public void Get_Moon_Position_Returns_Azimuth_Altitude_Distance_And_ParallacticAngle_For_The_Given_Time_And_Location()
        {
            //Arrange
            var date = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);
            var lat  = 50.5;
            var lng  = 30.5;

            //Act
            var sunPosition = SunCalc.GetMoonPosition(date, lat, lng);

            //Assert
            Assert.Equal(-0.9783999522438226, sunPosition.Azimuth, 15);
            Assert.Equal(0.0145514822438922, sunPosition.Altitude, 15);
            Assert.Equal(364121.37256256194, sunPosition.Distance, 15);
            Assert.Equal(-0.59832117604234014, sunPosition.ParallacticAngle, 15);
        }
Exemple #13
0
        public void TestSolarTimes_HeightAboveHorizon()
        {
            // set up
            double latitude  = 48.137222;
            double longitude = 11.575556;
            var    date      = new DateTimeOffset(2020, 7, 31, 12, 0, 0, TimeSpan.FromHours(2.0));

            // run
            var times1 = SunCalc.GetTimes(date, latitude, longitude);
            var times2 = SunCalc.GetTimes(date, latitude, longitude, 100.0);

            // check
            TimeSpan deltaSunrise = times2.Sunrise.Value - times1.Sunrise.Value;
            TimeSpan deltaSunset  = times2.Sunset.Value - times1.Sunset.Value;

            Assert.AreEqual(-2.3, deltaSunrise.TotalMinutes, 0.1, "sunrise 100m above must be 2 minutes earlier");
            Assert.AreEqual(2.3, deltaSunset.TotalMinutes, 0.1, "sunset 100m above must be 2 minutes later");
        }
Exemple #14
0
        public void TestSolarTimes_DayWithLastSunrise()
        {
            // set up
            double latitude  = 79.988889; // Eureka, Nunavut, Canada
            double longitude = -85.940833;

            var date = new DateTimeOffset(2020, 4, 12, 12, 0, 0, TimeSpan.FromHours(-5));

            // run
            var times = SunCalc.GetTimes(date, latitude, longitude);

            // check
            // 2020-04-12: 01:41 - no sunset
            Assert.IsTrue(times.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times.Sunrise.Value, 1, 41), "sunrise time must match");

            //// strange, timeanddate has no sunset value; might be a bug?
            ////Assert.IsFalse(times.Sunset.HasValue, "sunset must have been set");
        }
Exemple #15
0
        public void TestSolarTimes_SunsetOnTheNextDay()
        {
            // set up
            double latitude  = 64.475528; // somewhere in iceland
            double longitude = -20.253140;

            var date = new DateTimeOffset(2020, 6, 21, 12, 0, 0, TimeSpan.Zero); // GMT

            // run
            var times = SunCalc.GetTimes(date, latitude, longitude);

            // check
            // 2020-07-31: 02:39 - 00:05 (+1 day)
            Assert.IsTrue(times.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times.Sunrise.Value, 2, 39), "sunrise time must match");

            Assert.IsTrue(times.Sunset.HasValue, "sunset must have been set");
            Assert.AreEqual(date.DayOfYear + 1, times.Sunset.Value.DayOfYear, "sunset must be one day ahead");
            Assert.IsTrue(CheckMatchingTimeOfDay(times.Sunset.Value, 0, 5), "sunset time must match");
        }
Exemple #16
0
    public CelestialPositionResult CalculatePosition(System.DateTime date, double latitude, double longitude, double alture = 0)
    {
        var res = SunCalc.getPosition(date, latitude, longitude);
        CelestialPositionResult result = new CelestialPositionResult();

        result.altitude    = res.altitude;
        result.azimuth     = res.azimuth + Math.PI;
        result.parallactic = double.NaN;
        result.distance    = CelestialScale.SunAproxDistance;
        result.radius      = CelestialScale.SunRadiusKm;
        if (res.altitude < 0)
        {
            status = "Below the horizon";
        }
        else
        {
            status = "Over the horizon";
        }
        return(result);
    }
Exemple #17
0
        public void TestSolarTimes_BeforeAndAfterTimezoneChange()
        {
            // set up
            double latitude  = 48.137222; // munich
            double longitude = 11.575556;

            var date1 = new DateTimeOffset(2020, 10, 24, 12, 0, 0, TimeSpan.FromHours(2.0)); // MESZ
            var date2 = new DateTimeOffset(2020, 10, 25, 12, 0, 0, TimeSpan.FromHours(1.0)); // MEZ

            // run
            var times1 = SunCalc.GetTimes(date1, latitude, longitude);
            var times2 = SunCalc.GetTimes(date2, latitude, longitude);

            // check
            // 2020-10-24: 07:47 - 18:07
            Assert.IsTrue(times1.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times1.Sunrise.Value, 7, 47), "sunrise time must match");

            Assert.IsTrue(times1.Sunset.HasValue, "sunset must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times1.Sunset.Value, 18, 7), "sunset time must match");

            // 2020-10-25: 06:48 - 17:06
            Assert.IsTrue(times2.Sunrise.HasValue, "sunrise must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times2.Sunrise.Value, 6, 48), "sunrise time must match");

            Assert.IsTrue(times2.Sunset.HasValue, "sunset must have been set");
            Assert.IsTrue(CheckMatchingTimeOfDay(times2.Sunset.Value, 17, 06), "sunset time must match");

            // difference must be (about) one hour backwards
            Assert.AreEqual(
                -1.0,
                (times2.Sunrise.Value.TimeOfDay - times1.Sunrise.Value.TimeOfDay).TotalHours,
                0.1,
                "sunrise time zone difference must be 1h");

            Assert.AreEqual(
                -1.0,
                (times2.Sunset.Value.TimeOfDay - times1.Sunset.Value.TimeOfDay).TotalHours,
                0.1,
                "sunset time zone difference must be 1h");
        }
        /// <summary>
        /// Wrapper for SunCalc.GetSunPhases(), returning all SunPhases in Local time and
        /// implementing workaround for bug mentioned here: https://github.com/mourner/suncalc/issues/11#issue-22960903
        /// </summary>
        /// <param name="date">the day to get sun phases for, represented in Local time</param>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <param name="height"></param>
        /// <returns>all SunPhases for the given day, represented in Local time</returns>
        public static IEnumerable <SunPhase> GetSunPhases(DateTime date, double lat, double lng, double height = 0)
        {
            // Report possible error
            if (date.Kind != DateTimeKind.Local)
            {
                Console.Error.WriteLine("SunCalcHelper.GetSunPhases(): date should be in Local time");
            }

            DateTime noon = new DateTime(date.Year, date.Month, date.Day, 12, 0, 0); // noon of the given DateTime's day
            //return SunCalc.GetSunPhases(noon, lat, lng, height); // note: uncommenting this should make tests fail

            // Convert all SunPhases returned by SunCalc to Local time...
            var             phases = SunCalc.GetSunPhases(noon, lat, lng, height);
            List <SunPhase> result = new List <SunPhase>();

            foreach (SunPhase phase in phases)
            {
                result.Add(new SunPhase(phase.Name, phase.PhaseTime.ToLocalTime()));
            }
            return(result);
        }
        public void Get_Moon_Times_Returns_MoonRise_And_Set_Times()
        {
            //Arrange
            var date = new DateTime(2013, 3, 4, 0, 0, 0, DateTimeKind.Utc);
            var lat  = 50.5;
            var lng  = 30.5;

            //Act
            var moonPhase = SunCalc.GetMoonPhase(date, lat, lng);

            //Assert
            Assert.NotNull(moonPhase.Rise);
            Assert.NotNull(moonPhase.Set);
            var rise = moonPhase.Rise.Value.ToString("yyyy-MM-dd hh:mm:ss");
            var set  = moonPhase.Set.Value.ToString("yyyy-MM-dd hh:mm:ss");

            Assert.Equal("2013-03-04 11:54:29", rise);
            Assert.Equal("2013-03-04 07:47:58", set);
            Assert.False(moonPhase.AlwaysDown);
            Assert.False(moonPhase.AlwaysUp);
        }
        public void Get_Sun_Phases_Returns_Sun_Phases_For_The_Given_Date_And_Location()
        {
            //Arrange
            var testData = new List <SunPhase>
            {
                new SunPhase(SunPhaseName.SolarNoon, new DateTime(2013, 3, 5, 10, 10, 57, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Nadir, new DateTime(2013, 3, 4, 22, 10, 57, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Sunrise, new DateTime(2013, 3, 5, 4, 34, 56, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Sunset, new DateTime(2013, 3, 5, 15, 46, 57, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.SunriseEnd, new DateTime(2013, 3, 5, 4, 38, 19, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.SunsetStart, new DateTime(2013, 3, 5, 15, 43, 34, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Dawn, new DateTime(2013, 3, 5, 4, 2, 17, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Dusk, new DateTime(2013, 3, 5, 16, 19, 36, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.NauticalDawn, new DateTime(2013, 3, 5, 3, 24, 31, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.NauticalDusk, new DateTime(2013, 3, 5, 16, 57, 22, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.NightEnd, new DateTime(2013, 3, 5, 2, 46, 17, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.Night, new DateTime(2013, 3, 5, 17, 35, 36, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.GoldenHourEnd, new DateTime(2013, 3, 5, 5, 19, 01, DateTimeKind.Utc)),
                new SunPhase(SunPhaseName.GoldenHour, new DateTime(2013, 3, 5, 15, 2, 52, DateTimeKind.Utc)),
            };

            var date = new DateTime(2013, 3, 5, 0, 0, 0, DateTimeKind.Utc);
            var lat  = 50.5;
            var lng  = 30.5;

            //Act
            var sunPhases = SunCalc.GetSunPhases(date, lat, lng).ToList();

            //Assert
            foreach (var testSunPhase in testData)
            {
                var sunPhaseValue = sunPhases.First(x => x.Name.Value == testSunPhase.Name.Value);

                var testDataPhaseTime = testSunPhase.PhaseTime.ToString("yyyy-MM-dd hh:mm:ss");
                var sunPhaseTime      = sunPhaseValue.PhaseTime.ToString("yyyy-MM-dd hh:mm:ss");
                Assert.Equal(testDataPhaseTime, sunPhaseTime);
            }
        }
        public void GetTimes_returns_NauticalDawn_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.NauticalDawn.ToDateString().Should().Be("2013-03-05T03:24:31Z");
        }
        public void GetTimes_returns_Dusk_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.Dusk.ToDateString().Should().Be("2013-03-05T16:19:36Z");
        }
        public void GetTimes_returns_SunsetStart_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.SunsetStart.ToDateString().Should().Be("2013-03-05T15:43:34Z");
        }
        public void GetTimes_returns_SunriseEnd_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.SunriseEnd.ToDateString().Should().Be("2013-03-05T04:38:19Z");
        }
        public void GetTimes_returns_Nadir_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.Nadir.ToDateString().Should().Be("2013-03-04T22:10:57Z");
        }
        public void GetTimes_returns_SolarNoon_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.SolarNoon.ToDateString().Should().Be("2013-03-05T10:10:57Z");
        }
        public void GetPosition_returns_altitude_for_the_given_time_and_location()
        {
            var sunPosition = SunCalc.GetPosition(_date, _lat, _lng);

            sunPosition.Altitude.Should().BeApproximately(-0.7000406838781611, 1e-15);
        }
        public void GetPosition_returns_azimuth_for_the_given_time_and_location()
        {
            var sunPosition = SunCalc.GetPosition(_date, _lat, _lng);

            sunPosition.Azimuth.Should().BeApproximately(-2.5003175907168385, 1e-15);
        }
        public void GetTimes_returns_GoldenHour_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.GoldenHour.ToDateString().Should().Be("2013-03-05T15:02:52Z");
        }
        public void GetTimes_returns_NightEnd_for_the_given_date_and_location()
        {
            var times = SunCalc.GetTimes(_date, _lat, _lng);

            times.NightEnd.ToDateString().Should().Be("2013-03-05T02:46:17Z");
        }