krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Commit 6626a227c5

6626a227c5fd543a82d214fbd642a36d504fb7eb

parent: a40dab3f37

Unsigned

cmc <hello@cleberg.net> · 2026-10-22 03:46 UTC

Sun: active-region numbers on the SDO image (!117)

- `SolarRegion` (Feeds): latest report from `json/solar_regions.json` (fetched with the Sun tab), longitude east-positive converted to west; position on the disk at a time: rotated 13.2°/day, projected with B0 (Meeus ch. 29, low precision), nil behind the limb.
- Reference time: positions taken as of 24:00 UTC on `observed_date`. At 00:00 the labels sat about 13° west of their spots on today's HMI image; at 24:00, 4560 (N13W28) and 4557 (N07W55) fall on their groups.
- Sun tab: region numbers over the image at the frame's time, scaled by the wavelength's disk radius; "Region numbers" switch (`sunRegionLabels`, on). `SolarImage.diskRadius` is now public.
- Tests: B0 through the year; decoding, rotation and limb. Feeds tests pass; iOS builds.

Layout: unified · split

App/Sources/SunScreen.swift +23
@@ -13,6 +13,8 @@ struct SunScreen: View {
1313 @State private var picture: Picture?
1414 @State private var imageFailed = false
1515 @State private var solarWind: SolarWind?
16 @State private var regions: [SolarRegion] = []
17 @AppStorage("sunRegionLabels") private var regionLabels = true
1618 @Environment(\.dynamicTypeSize) private var typeSize
1719
1820 private var kp: [KpBar] { data?.kp ?? [] }
@@ -48,6 +50,9 @@ struct SunScreen: View {
4850 }
4951 .pickerStyle(.segmented)
5052 .padding(.top, 12)
53 if !regions.isEmpty {
54 Toggle("Region numbers", isOn: $regionLabels).font(.footnote).tint(accent).padding(.top, 8)
55 }
5156 Text(wavelength.caption).font(.footnote).dimmed(0.75).padding(.top, 8)
5257 Text(SolarImage.credit).font(.caption2).dimmed(0.5).padding(.top, 4)
5358
@@ -78,6 +83,7 @@ struct SunScreen: View {
7883 private func load(force: Bool) async {
7984 if data == nil { await show(SunFeeds.lastSaved()) }
8085 if let apiBase { solarWind = (try? await SolarWind.fetch(base: apiBase)) ?? solarWind }
86 regions = (try? await SolarRegion.fetch()) ?? regions
8187 if force { imageID = .now }
8288 let result = await SunFeeds.load(force: force)
8389 await show(result?.snapshot)
@@ -172,6 +178,7 @@ struct SunScreen: View {
172178 Group {
173179 if let picture {
174180 Image(uiImage: picture.image).resizable().scaledToFit()
181 .overlay { if regionLabels { regionOverlay(at: picture.frame ?? picture.saved ?? picture.loaded) } }
175182 } else if imageFailed {
176183 Color.black.overlay(Text("Image unavailable").font(.footnote).dimmed(0.6))
177184 } else {
@@ -189,6 +196,22 @@ struct SunScreen: View {
189196 }
190197 }
191198
199 /// NOAA region numbers where each region is at `date`, from SWPC's daily report turned with the Sun.
200 private func regionOverlay(at date: Date) -> some View {
201 GeometryReader { g in
202 let r = wavelength.diskRadius * g.size.width
203 ForEach(regions) { region in
204 if let p = region.disk(at: date) {
205 Text(String(region.number))
206 .font(.system(size: 10, weight: .medium)).monospacedDigit()
207 .foregroundStyle(.white).shadow(color: .black, radius: 2)
208 .position(x: g.size.width / 2 + p.x * r, y: g.size.height / 2 - p.y * r - 10)
209 }
210 }
211 }
212 .accessibilityHidden(true)
213 }
214
192215 /// Three columns, stacked at accessibility text sizes.
193216 private func scalesStrip(_ s: NOAAScales) -> some View {
194217 let stacked = typeSize.isAccessibilitySize
PLAN.org +3 −1
@@ -518,7 +518,9 @@ Worker; DSN Now freshness; NASA/ESA image licences; 26 LoC plates bundled.
518518 Bz. Done: 5-minute means over 6 h in =/v1/today=; Sun tab panel and Bz
519519 chart; alerts to the Kp 6 bands when the last 30 min average Bz ≤ −10 nT
520520 and speed ≥ 450 km/s (a rule of thumb, not a Kp model).
521- Active-region labels on the SDO image.
521- Active-region labels on the SDO image. Done: SWPC =solar_regions.json=, turned at 13.2°/day
522 from 24:00 UTC on =observed_date= and projected with B0; checked on an HMI
523 image (4560, 4557 on their spots).
522524- Sun path, golden and blue hour, day length.
523525- CCOR-2 coronagraph loop, WSA-Enlil forecast line, solar cycle progress.
524526
Packages/MoonriseKit/Sources/Feeds/SolarRegions.swift added +59
@@ -0,0 +1,59 @@
1import Foundation
2
3/// NOAA-numbered active regions from SWPC's daily report (json/solar_regions.json), placed on the disk now.
4public struct SolarRegion: Sendable, Equatable, Identifiable {
5 public var number: Int
6 /// Heliographic latitude, degrees, north positive.
7 public var latitude: Double
8 /// Degrees from the central meridian at `observed`, west positive.
9 public var west: Double
10 public var observed: Date
11 /// Millionths of the hemisphere; nil for plage without spots.
12 public var area: Int?
13 public var id: Int { number }
14
15 public static let url = URL(string: "https://services.swpc.noaa.gov/json/solar_regions.json")!
16
17 /// Regions in the latest report. SWPC gives longitude east positive. Positions are as of the end of
18 /// `observed_date` (24:00 UTC): taken so, they match SDO images; taken at its start, they sit about 13° too far west.
19 public static func decode(_ data: Data) throws -> [SolarRegion] {
20 struct Row: Decodable { var region: Int; var observed_date: String; var latitude: Double?; var longitude: Double?; var area: Int? }
21 let rows = try JSONDecoder().decode([Row].self, from: data)
22 guard let latest = rows.map(\.observed_date).max() else { return [] }
23 return rows.compactMap { r in
24 guard r.observed_date == latest, let lat = r.latitude, let lon = r.longitude,
25 let start = try? Date(r.observed_date + "T00:00:00Z", strategy: .iso8601) else { return nil }
26 let day = start.addingTimeInterval(86_400)
27 return SolarRegion(number: r.region, latitude: lat, west: -lon, observed: day, area: r.area)
28 }
29 }
30
31 public static func fetch(session: URLSession = .shared) async throws -> [SolarRegion] {
32 try decode(await session.data(from: url).0)
33 }
34
35 /// The Sun turns about 13.2° a day as seen from Earth (synodic, at low latitude).
36 static let degreesPerDay = 13.2
37
38 /// Position on the disk at `date` in disk radii, x to solar west (right), y to solar north (up);
39 /// nil once rotated behind the limb.
40 public func disk(at date: Date) -> (x: Double, y: Double)? {
41 let r = Double.pi / 180
42 let l = (west + date.timeIntervalSince(observed) / 86_400 * Self.degreesPerDay) * r
43 let b = latitude * r, b0 = Self.b0(at: date) * r
44 guard sin(b) * sin(b0) + cos(b) * cos(l) * cos(b0) > 0 else { return nil }
45 return (cos(b) * sin(l), sin(b) * cos(b0) - cos(b) * cos(l) * sin(b0))
46 }
47
48 /// Heliographic latitude of the disk centre (B0), degrees: from the Sun's ecliptic longitude and the solar
49 /// equator's inclination (7.25°) and node (Meeus, chapter 29, low precision).
50 static func b0(at date: Date) -> Double {
51 let r = Double.pi / 180
52 let jd = date.timeIntervalSince1970 / 86_400 + 2_440_587.5
53 let d = jd - 2_451_545
54 let g = (357.529 + 0.98560028 * d) * r
55 let lambda = (280.459 + 0.98564736 * d) * r + (1.915 * sin(g) + 0.020 * sin(2 * g)) * r
56 let node = (73.6667 + 1.3958333 * (jd - 2_396_758) / 36_525) * r
57 return asin(sin(lambda - node) * sin(7.25 * r)) / r
58 }
59}
Packages/MoonriseKit/Sources/Feeds/Sun.swift +1 −1
@@ -38,7 +38,7 @@ public enum SolarImage: String, CaseIterable, Sendable, Identifiable {
3838 public static let credit = "Courtesy of NASA/SDO and the AIA, EVE, and HMI science teams."
3939
4040 /// Disk radius as a fraction of the frame's width.
41 var diskRadius: Double { self == .visible ? 0.45 : 0.39 }
41 public var diskRadius: Double { self == .visible ? 0.45 : 0.39 }
4242
4343 /// Whether a frame shows the whole disk. SDO publishes AIA frames that are black apart from the label along the
4444 /// bottom, and some with part of the disk behind a curved dark edge. On a 64 px thumbnail, the share of the disk
Packages/MoonriseKit/Tests/FeedsTests/SunTests.swift +19
@@ -123,3 +123,22 @@ func solarFrameUsable(name: String, image: SolarImage, usable: Bool) throws {
123123 let o = try JSONDecoder().decode(Ovation.self, from: data)
124124 #expect(o.cells == [Ovation.Cell(lon: 0, lat: 65, probability: 12), Ovation.Cell(lon: 359, lat: -60, probability: 3)])
125125}
126
127@Test func solarB0FollowsTheYear() {
128 // B0 is about 0° in early June and December, +7.25° in early September, −7.25° in early March.
129 let at = { (s: String) in SolarRegion.b0(at: try! Date(s, strategy: .iso8601)) }
130 #expect(abs(at("2026-06-06T00:00:00Z")) < 0.5)
131 #expect(abs(at("2026-09-07T00:00:00Z") - 7.25) < 0.3)
132 #expect(abs(at("2026-03-06T00:00:00Z") + 7.25) < 0.3)
133}
134
135@Test func regionsRotateWestAndLeaveTheDisk() throws {
136 let data = Data(#"[{"region":4560,"observed_date":"2026-10-21","latitude":13,"longitude":-28,"area":150},{"region":4551,"observed_date":"2026-10-21","latitude":-16,"longitude":-85,"area":340},{"region":4500,"observed_date":"2026-10-20","latitude":0,"longitude":0,"area":10}]"#.utf8)
137 let regions = try SolarRegion.decode(data)
138 #expect(regions.map(\.number) == [4560, 4551])
139 let r = regions[0], noon = try Date("2026-10-22T12:00:00Z", strategy: .iso8601)
140 let p = try #require(r.disk(at: noon))
141 #expect(p.x > sin(28 * Double.pi / 180) * 0.9)
142 #expect(p.y > 0)
143 #expect(regions[1].disk(at: try Date("2026-10-23T12:00:00Z", strategy: .iso8601)) == nil)
144}