Files
Chronos/tests/Titano.Core.Tests/Math/SolarPositionTests.cs
T
Claude 8c2692073c feat(math): add NOAA solar position and day phases
Meeus/NOAA equations for elevation (with refraction) and azimuth,
iteratively refined sunrise, sunset and civil/nautical/astronomical
twilight per local solar day, and DayPhase classification.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:31:06 +00:00

105 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using Titano.Core.Mathematics;
using Titano.Testing;
namespace Titano.Core.Tests.Mathematics;
/// <summary>
/// Reference values are NOAA Solar Calculator results (spreadsheet equations as implemented by the
/// <c>astral</c> package, cross-checked against PyEphem/VSOP87 to within 35 s), UTC, for the local solar day.
/// </summary>
public sealed class SolarPositionTests
{
private const double OneMinute = 60;
[Theory]
[Case("Greenwich summer", 51.4769, -0.0005, "2024-06-21", "03:43:13", "20:20:36", "02:54:34", "21:09:14", "01:40:14", "22:23:33", null, null)]
[Case("Greenwich winter", 51.4769, -0.0005, "2024-12-21", "08:03:42", "15:52:56", "07:22:37", "16:34:01", "06:39:40", "17:16:58", "05:58:57", "17:57:41")]
[Case("New York equinox", 40.7128, -74.0060, "2024-03-20", "10:58:42", "23:08:28", "10:30:54", "23:36:20", "09:59:06", "+00:08:13", "09:26:36", "+00:40:50")]
[Case("Sydney winter", -33.8688, 151.2093, "2024-06-21", "-21:00:18", "06:53:41", "-20:32:02", "07:21:57", "-20:00:56", "07:53:03", "-19:30:30", "08:23:28")]
[Case("Quito equinox", -0.1807, -78.4678, "2024-09-22", "11:03:17", "23:09:26", "10:42:14", "23:30:29", "10:18:21", "23:54:22", "09:54:23", "+00:18:19")]
[Case("Tromso March", 69.6492, 18.9553, "2024-03-01", "06:08:32", "15:46:01", "05:06:36", "16:48:12", "03:57:47", "17:57:27", "02:44:21", "19:11:43")]
// The sun grazes −6° here, so tiny ephemeris differences move the crossing by minutes: astral's single-pass
// NOAA value is 77 s early, while the iterated NOAA solution and PyEphem agree on 08:31:31 / 12:53:21.
[Case("Tromso polar night", 69.6492, 18.9553, "2024-12-21", null, null, "08:31:31", "12:53:21", "06:46:35", "14:38:21", "05:28:22", "15:56:34")]
[Case("Tromso midnight sun", 69.6492, 18.9553, "2024-06-21", null, null, null, null, null, null, null, null)]
[Case("Golden NREL SPA site", 39.742476, -105.1786, "2003-10-17", "13:12:57", "+00:18:36", "12:45:10", "+00:46:21", "12:13:59", "+01:17:30", "11:42:47", "+01:48:39")]
public void EventsMatchNoaaWithinOneMinute(string place, double latitude, double longitude, string date,
string? sunrise, string? sunset, string? civilDawn, string? civilDusk, string? nauticalDawn, string? nauticalDusk, string? astroDawn, string? astroDusk)
{
var day = DateOnly.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var events = SolarPosition.Events(day, latitude, longitude);
Check(place + " sunrise", day, sunrise, events.Sunrise);
Check(place + " sunset", day, sunset, events.Sunset);
Check(place + " civil dawn", day, civilDawn, events.CivilDawn);
Check(place + " civil dusk", day, civilDusk, events.CivilDusk);
Check(place + " nautical dawn", day, nauticalDawn, events.NauticalDawn);
Check(place + " nautical dusk", day, nauticalDusk, events.NauticalDusk);
Check(place + " astronomical dawn", day, astroDawn, events.AstronomicalDawn);
Check(place + " astronomical dusk", day, astroDusk, events.AstronomicalDusk);
}
[Theory]
[Case(51.4769, -0.0005, "2024-06-21", "12:01:49")]
[Case(-33.8688, 151.2093, "2024-06-21", "01:56:58")]
[Case(39.742476, -105.1786, "2003-10-17", "18:46:14")]
public void SolarNoonMatchesNoaa(double latitude, double longitude, string date, string noon)
{
_ = latitude;
var day = DateOnly.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
Check("noon", day, noon, SolarPosition.SolarNoon(day, longitude));
}
[Theory]
[Case(39.742476, -105.1786, "2003-10-17T19:30:30", 39.8914, 39.8721, 194.3426)]
[Case(51.4769, -0.0005, "2024-06-21T12:00:00", 61.9665, 61.9579, 179.0598)]
[Case(-33.8688, 151.2093, "2024-01-15T06:30:00", 30.7712, 30.7442, 263.8208)]
[Case(35.6762, 139.6503, "2024-09-01T21:00:00", 8.4899, 8.3861, 86.3181)]
[Case(64.1466, -21.9426, "2024-12-01T15:00:00", 1.9248, 1.6140, 203.7679)]
public void PositionMatchesNoaa(double latitude, double longitude, string utc, double elevation, double geometric, double azimuth)
{
var time = DateTime.SpecifyKind(DateTime.ParseExact(utc, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture), DateTimeKind.Utc);
var sun = SolarPosition.Compute(time, latitude, longitude);
Assert.Approximately(geometric, sun.GeometricElevation, 0.01);
Assert.Approximately(elevation, sun.Elevation, 0.01);
Assert.Approximately(azimuth, sun.Azimuth, 0.01);
}
[Theory]
[Case(10.0, DayPhase.Day)]
[Case(3.0, DayPhase.GoldenHour)]
[Case(-0.5, DayPhase.GoldenHour)]
[Case(-3.0, DayPhase.CivilTwilight)]
[Case(-8.0, DayPhase.NauticalTwilight)]
[Case(-15.0, DayPhase.AstronomicalTwilight)]
[Case(-30.0, DayPhase.Night)]
public void ClassifiesElevation(double elevation, DayPhase expected) => Assert.Equal(expected, SolarPosition.Classify(elevation));
[Test]
public void PhaseFollowsTheEvents()
{
var day = new DateOnly(2024, 12, 21);
var events = SolarPosition.Events(day, 51.4769, -0.0005);
Assert.Equal(DayPhase.Day, SolarPosition.Phase(events.SolarNoon, 51.4769, -0.0005));
Assert.Equal(DayPhase.GoldenHour, SolarPosition.Phase(events.Sunset!.Value.AddMinutes(-5), 51.4769, -0.0005));
Assert.Equal(DayPhase.CivilTwilight, SolarPosition.Phase(events.Sunset!.Value.AddMinutes(5), 51.4769, -0.0005));
Assert.Equal(DayPhase.Night, SolarPosition.Phase(events.AstronomicalDusk!.Value.AddMinutes(5), 51.4769, -0.0005));
Assert.Throws<ArgumentOutOfRangeException>(() => SolarPosition.Compute(DateTime.UtcNow, 91, 0));
}
/// <summary>Expected "HH:mm:ss" on <paramref name="day"/>; a leading '-' or '+' means the previous or next UTC day.</summary>
private static void Check(string label, DateOnly day, string? expected, DateTime? actual)
{
if (expected is null)
{
Assert.Null(actual);
return;
}
var offsetDays = expected[0] switch { '-' => -1, '+' => 1, _ => 0 };
var clock = TimeOnly.ParseExact(expected.TrimStart('+', '-'), "HH:mm:ss", CultureInfo.InvariantCulture);
var reference = day.AddDays(offsetDays).ToDateTime(clock, DateTimeKind.Utc);
var value = Assert.NotNull(actual, label);
Assert.Approximately(0, (value - reference).TotalSeconds, OneMinute, label);
}
}