Commit 775d8d5d61
Unsigned
Layout: unified · split
App/Sources/SunScreen.swift +1
| @@ -56,6 +56,7 @@ struct SunScreen: View { | ||
| 56 | 56 | Text(wavelength.caption).font(.footnote).dimmed(0.75).padding(.top, 8) |
| 57 | 57 | Text(SolarImage.credit).font(.caption2).dimmed(0.5).padding(.top, 4) |
| 58 | 58 | |
| 59 | section("The Sun here today", note: "When the Sun rises and sets where you are, and the soft light of the golden and blue hours.") { SunToday(ink: ink, accent: accent) } | |
| 59 | 60 | section("Aurora tonight", note: "NOAA's forecast of where the aurora may be overhead in the next hour; brighter green is more likely. From a few hundred kilometres further from the pole it can still be seen low on the horizon.") { auroraMap } |
| 60 | 61 | if let solarWind { |
| 61 | 62 | section("Solar wind", note: "Measured 1.5 million km sunward of Earth, 30 to 60 minutes before it arrives. When Bz, the north–south part of its magnetic field, turns strongly negative (south), aurora often follows.") { solarWindPanel(solarWind) } |
App/Sources/SunToday.swift added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | import Charts | |
| 2 | import SkyEngine | |
| 3 | import SwiftUI | |
| 4 | ||
| 5 | /// The Sun at the Sky tab's place today: rise and set, day length, noon, golden and blue hours, and its path. | |
| 6 | struct SunToday: View { | |
| 7 | var ink: Color | |
| 8 | var accent: Color | |
| 9 | @State private var today: SunDay? | |
| 10 | @State private var yesterday: SunDay? | |
| 11 | ||
| 12 | private let golden = Color(red: 0.95, green: 0.68, blue: 0.25) | |
| 13 | private let blue = Color(red: 0.35, green: 0.5, blue: 0.95) | |
| 14 | ||
| 15 | var body: some View { | |
| 16 | VStack(alignment: .leading, spacing: 10) { | |
| 17 | if let d = today { | |
| 18 | chart(d) | |
| 19 | Grid(alignment: .leading, horizontalSpacing: 16, verticalSpacing: 6) { | |
| 20 | row("Sunrise", time(d.sunrise)) | |
| 21 | row("Sunset", time(d.sunset)) | |
| 22 | row("Day length", length(d)) | |
| 23 | row("Solar noon", d.noon.map { "\(time($0)), \(Int((d.noonAltitude ?? 0).rounded()))° up" } ?? "–") | |
| 24 | row("Golden hour", [d.morningGolden, d.eveningGolden].compactMap { $0 }.map(span).joined(separator: ", ").nonEmpty ?? "–") | |
| 25 | row("Blue hour", [d.morningBlue, d.eveningBlue].compactMap { $0 }.map(span).joined(separator: ", ").nonEmpty ?? "–") | |
| 26 | } | |
| 27 | .font(.subheadline.monospacedDigit()) | |
| 28 | Text("For \(SkyModel.shared.placeName), in this device's time zone. Golden hour: the Sun's centre between 6° above and 4° below the horizon; blue hour: 4° to 6° below.") | |
| 29 | .font(.caption2).dimmed(0.6) | |
| 30 | } else { | |
| 31 | Text("Loading").font(.footnote).dimmed(0.6) | |
| 32 | } | |
| 33 | } | |
| 34 | .task(id: SkyModel.shared.observer) { | |
| 35 | let o = SkyModel.shared.observer, start = Calendar.current.startOfDay(for: .now) | |
| 36 | let before = Calendar.current.date(byAdding: .day, value: -1, to: start) ?? start | |
| 37 | (today, yesterday) = await Task.detached { | |
| 38 | (try? SunDay(start: start, observer: o), try? SunDay(start: before, observer: o)) | |
| 39 | }.value | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | private func chart(_ d: SunDay) -> some View { | |
| 44 | Chart { | |
| 45 | RectangleMark(yStart: .value("", -6), yEnd: .value("", -4)).foregroundStyle(blue.opacity(0.25)) | |
| 46 | RectangleMark(yStart: .value("", -4), yEnd: .value("", 6)).foregroundStyle(golden.opacity(0.2)) | |
| 47 | RuleMark(y: .value("Horizon", 0)).foregroundStyle(ink.opacity(0.4)) | |
| 48 | ForEach(d.path, id: \.date) { p in | |
| 49 | LineMark(x: .value("Time", p.date), y: .value("Altitude", p.altitude)).foregroundStyle(ink) | |
| 50 | } | |
| 51 | RuleMark(x: .value("Now", Date.now)).foregroundStyle(accent) | |
| 52 | } | |
| 53 | .chartYScale(domain: -90...90) | |
| 54 | .chartYAxis { AxisMarks(values: [-60, -30, 0, 30, 60]) { v in AxisGridLine().foregroundStyle(ink.opacity(0.12)); AxisValueLabel("\(v.as(Int.self) ?? 0)°").foregroundStyle(ink.opacity(0.6)) } } | |
| 55 | .chartXAxis { AxisMarks(values: .stride(by: .hour, count: 6)) { _ in AxisValueLabel(format: .dateTime.hour()).foregroundStyle(ink.opacity(0.6)) } } | |
| 56 | .frame(height: 140) | |
| 57 | .accessibilityLabel("The Sun's altitude through today") | |
| 58 | } | |
| 59 | ||
| 60 | private func row(_ label: String, _ value: String) -> some View { | |
| 61 | GridRow { Text(label).dimmed(0.7); Text(value) } | |
| 62 | } | |
| 63 | ||
| 64 | private func time(_ d: Date?) -> String { d?.formatted(date: .omitted, time: .shortened) ?? "–" } | |
| 65 | ||
| 66 | private func span(_ i: DateInterval) -> String { "\(time(i.start))–\(time(i.end))" } | |
| 67 | ||
| 68 | private func length(_ d: SunDay) -> String { | |
| 69 | guard let l = d.dayLength else { return (d.path.first?.altitude ?? 0) > 0 ? "24 h (the Sun does not set)" : "None (the Sun does not rise)" } | |
| 70 | let h = Int(l) / 3600, m = Int(l) % 3600 / 60 | |
| 71 | guard let y = yesterday?.dayLength else { return "\(h) h \(m) min" } | |
| 72 | let change = Int((l - y).rounded()) | |
| 73 | return "\(h) h \(m) min, \(change >= 0 ? "+" : "−")\(abs(change) / 60) min \(abs(change) % 60) s on yesterday" | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | private extension String { | |
| 78 | var nonEmpty: String? { isEmpty ? nil : self } | |
| 79 | } | |
PLAN.org +3 −1
| @@ -521,7 +521,9 @@ Worker; DSN Now freshness; NASA/ESA image licences; 26 LoC plates bundled. | ||
| 521 | 521 | - Active-region labels on the SDO image. Done: SWPC =solar_regions.json=, turned at 13.2°/day |
| 522 | 522 | from 24:00 UTC on =observed_date= and projected with B0; checked on an HMI |
| 523 | 523 | image (4560, 4557 on their spots). |
| 524 | - Sun path, golden and blue hour, day length. | |
| 524 | - Sun path, golden and blue hour, day length. Done: =SunDay= (SkyEngine) and a Sun tab | |
| 525 | section with the altitude curve, rise, set, noon, day length against | |
| 526 | yesterday, golden (−4° to +6°) and blue (−6° to −4°) hours. | |
| 525 | 527 | - CCOR-2 coronagraph loop, WSA-Enlil forecast line, solar cycle progress. |
| 526 | 528 | |
| 527 | 529 | ** v5.2: Sky |
Packages/MoonriseKit/Sources/SkyEngine/SunDay.swift added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | import CAstronomy | |
| 2 | import Foundation | |
| 3 | ||
| 4 | /// The Sun over one local day: rise, set, noon, and the golden and blue hours (the Sun's centre between −4° and +6°, | |
| 5 | /// and between −6° and −4°). Times are nil where the Sun does not cross that altitude, as in polar day or night. | |
| 6 | public struct SunDay: Sendable { | |
| 7 | public var start: Date | |
| 8 | public var sunrise: Date? | |
| 9 | public var sunset: Date? | |
| 10 | public var noon: Date? | |
| 11 | public var noonAltitude: Double? | |
| 12 | public var morningBlue: DateInterval? | |
| 13 | public var morningGolden: DateInterval? | |
| 14 | public var eveningGolden: DateInterval? | |
| 15 | public var eveningBlue: DateInterval? | |
| 16 | /// Altitude every 10 minutes from `start` for 24 hours. | |
| 17 | public var path: [(date: Date, altitude: Double)] | |
| 18 | ||
| 19 | public var dayLength: TimeInterval? { | |
| 20 | guard let r = sunrise, let s = sunset, s > r else { return nil } | |
| 21 | return s.timeIntervalSince(r) | |
| 22 | } | |
| 23 | ||
| 24 | /// `start` is the local midnight the day begins at. | |
| 25 | public init(start: Date, observer: Observer) throws { | |
| 26 | self.start = start | |
| 27 | let rs = SkyEngine.riseSet(.sun, after: start, from: observer, limitDays: 1) | |
| 28 | sunrise = rs.rise | |
| 29 | sunset = rs.set | |
| 30 | func cross(_ alt: Double, rising: Bool, after d: Date) -> Date? { | |
| 31 | SkyEngine.searchAltitude(.sun, altitude: alt, rising: rising, after: d, from: observer, limitDays: 1) | |
| 32 | .flatMap { $0 < start.addingTimeInterval(86_400) ? $0 : nil } | |
| 33 | } | |
| 34 | func span(_ a: Date?, _ b: Date?) -> DateInterval? { | |
| 35 | guard let a, let b, b > a else { return nil } | |
| 36 | return DateInterval(start: a, end: b) | |
| 37 | } | |
| 38 | let m6 = cross(-6, rising: true, after: start), m4 = cross(-4, rising: true, after: start), p6 = cross(6, rising: true, after: start) | |
| 39 | morningBlue = span(m6, m4) | |
| 40 | morningGolden = span(m4, p6) | |
| 41 | let e6 = cross(6, rising: false, after: start), e4 = cross(-4, rising: false, after: start), e6b = cross(-6, rising: false, after: start) | |
| 42 | eveningGolden = span(e6, e4) | |
| 43 | eveningBlue = span(e4, e6b) | |
| 44 | ||
| 45 | SkyEngine.lock.lock() | |
| 46 | let h = Astronomy_SearchHourAngleEx(BODY_SUN, observer.astro, 0, SkyEngine.time(start), +1) | |
| 47 | SkyEngine.lock.unlock() | |
| 48 | if h.status == ASTRO_SUCCESS, SkyEngine.date(h.time) < start.addingTimeInterval(86_400) { | |
| 49 | noon = SkyEngine.date(h.time) | |
| 50 | noonAltitude = h.hor.altitude | |
| 51 | } | |
| 52 | path = try (0...144).map { i in | |
| 53 | let d = start.addingTimeInterval(Double(i) * 600) | |
| 54 | return (d, try SkyEngine.horizontal(.sun, at: d, from: observer).altitude) | |
| 55 | } | |
| 56 | } | |
| 57 | } | |
Packages/MoonriseKit/Tests/SkyEngineTests/SunDayTests.swift added +28
| @@ -0,0 +1,28 @@ | ||
| 1 | import Foundation | |
| 2 | import Testing | |
| 3 | @testable import SkyEngine | |
| 4 | ||
| 5 | private let greenwich = Observer(latitude: 51.4779, longitude: -0.0015) | |
| 6 | ||
| 7 | @Test func sunDayAtGreenwichInSummerAndWinter() throws { | |
| 8 | let june = try SunDay(start: try Date("2026-06-21T00:00:00Z", strategy: .iso8601), observer: greenwich) | |
| 9 | let summer: TimeInterval = 16 * 3600 + 38 * 60, winter: TimeInterval = 7 * 3600 + 50 * 60 | |
| 10 | let juneLength = try #require(june.dayLength) | |
| 11 | #expect(abs(juneLength - summer) < 300) | |
| 12 | #expect(abs(try #require(june.noonAltitude) - 62) < 0.5) | |
| 13 | let golden = try #require(june.eveningGolden), blue = try #require(june.eveningBlue) | |
| 14 | #expect(golden.end == blue.start) | |
| 15 | #expect(try #require(june.sunset) > golden.start && june.sunset! < golden.end) | |
| 16 | let december = try SunDay(start: try Date("2026-12-21T00:00:00Z", strategy: .iso8601), observer: greenwich) | |
| 17 | let decemberLength = try #require(december.dayLength) | |
| 18 | #expect(abs(decemberLength - winter) < 300) | |
| 19 | #expect(june.path.count == 145) | |
| 20 | } | |
| 21 | ||
| 22 | @Test func polarNightHasNoSunrise() throws { | |
| 23 | let svalbard = Observer(latitude: 78.22, longitude: 15.65) | |
| 24 | let d = try SunDay(start: try Date("2026-12-21T00:00:00Z", strategy: .iso8601), observer: svalbard) | |
| 25 | #expect(d.sunrise == nil && d.sunset == nil && d.dayLength == nil) | |
| 26 | #expect(d.morningGolden == nil && d.eveningGolden == nil) | |
| 27 | #expect(d.path.allSatisfy { $0.altitude < 0 }) | |
| 28 | } | |