Commit 6626a227c5
Unsigned
Layout: unified · split
App/Sources/SunScreen.swift +23
| @@ -13,6 +13,8 @@ struct SunScreen: View { | |||
| 13 | @State private var picture: Picture? | 13 | @State private var picture: Picture? |
| 14 | @State private var imageFailed = false | 14 | @State private var imageFailed = false |
| 15 | @State private var solarWind: SolarWind? | 15 | @State private var solarWind: SolarWind? |
| 16 | @State private var regions: [SolarRegion] = [] | ||
| 17 | @AppStorage("sunRegionLabels") private var regionLabels = true | ||
| 16 | @Environment(\.dynamicTypeSize) private var typeSize | 18 | @Environment(\.dynamicTypeSize) private var typeSize |
| 17 | 19 | ||
| 18 | private var kp: [KpBar] { data?.kp ?? [] } | 20 | private var kp: [KpBar] { data?.kp ?? [] } |
| @@ -48,6 +50,9 @@ struct SunScreen: View { | |||
| 48 | } | 50 | } |
| 49 | .pickerStyle(.segmented) | 51 | .pickerStyle(.segmented) |
| 50 | .padding(.top, 12) | 52 | .padding(.top, 12) |
| 53 | if !regions.isEmpty { | ||
| 54 | Toggle("Region numbers", isOn: $regionLabels).font(.footnote).tint(accent).padding(.top, 8) | ||
| 55 | } | ||
| 51 | Text(wavelength.caption).font(.footnote).dimmed(0.75).padding(.top, 8) | 56 | Text(wavelength.caption).font(.footnote).dimmed(0.75).padding(.top, 8) |
| 52 | Text(SolarImage.credit).font(.caption2).dimmed(0.5).padding(.top, 4) | 57 | Text(SolarImage.credit).font(.caption2).dimmed(0.5).padding(.top, 4) |
| 53 | 58 | ||
| @@ -78,6 +83,7 @@ struct SunScreen: View { | |||
| 78 | private func load(force: Bool) async { | 83 | private func load(force: Bool) async { |
| 79 | if data == nil { await show(SunFeeds.lastSaved()) } | 84 | if data == nil { await show(SunFeeds.lastSaved()) } |
| 80 | if let apiBase { solarWind = (try? await SolarWind.fetch(base: apiBase)) ?? solarWind } | 85 | if let apiBase { solarWind = (try? await SolarWind.fetch(base: apiBase)) ?? solarWind } |
| 86 | regions = (try? await SolarRegion.fetch()) ?? regions | ||
| 81 | if force { imageID = .now } | 87 | if force { imageID = .now } |
| 82 | let result = await SunFeeds.load(force: force) | 88 | let result = await SunFeeds.load(force: force) |
| 83 | await show(result?.snapshot) | 89 | await show(result?.snapshot) |
| @@ -172,6 +178,7 @@ struct SunScreen: View { | |||
| 172 | Group { | 178 | Group { |
| 173 | if let picture { | 179 | if let picture { |
| 174 | Image(uiImage: picture.image).resizable().scaledToFit() | 180 | Image(uiImage: picture.image).resizable().scaledToFit() |
| 181 | .overlay { if regionLabels { regionOverlay(at: picture.frame ?? picture.saved ?? picture.loaded) } } | ||
| 175 | } else if imageFailed { | 182 | } else if imageFailed { |
| 176 | Color.black.overlay(Text("Image unavailable").font(.footnote).dimmed(0.6)) | 183 | Color.black.overlay(Text("Image unavailable").font(.footnote).dimmed(0.6)) |
| 177 | } else { | 184 | } else { |
| @@ -189,6 +196,22 @@ struct SunScreen: View { | |||
| 189 | } | 196 | } |
| 190 | } | 197 | } |
| 191 | 198 | ||
| 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 | |||
| 192 | /// Three columns, stacked at accessibility text sizes. | 215 | /// Three columns, stacked at accessibility text sizes. |
| 193 | private func scalesStrip(_ s: NOAAScales) -> some View { | 216 | private func scalesStrip(_ s: NOAAScales) -> some View { |
| 194 | let stacked = typeSize.isAccessibilitySize | 217 | let stacked = typeSize.isAccessibilitySize |
PLAN.org +3 −1
| @@ -518,7 +518,9 @@ Worker; DSN Now freshness; NASA/ESA image licences; 26 LoC plates bundled. | |||
| 518 | Bz. Done: 5-minute means over 6 h in =/v1/today=; Sun tab panel and Bz | 518 | Bz. Done: 5-minute means over 6 h in =/v1/today=; Sun tab panel and Bz |
| 519 | chart; alerts to the Kp 6 bands when the last 30 min average Bz ≤ −10 nT | 519 | chart; alerts to the Kp 6 bands when the last 30 min average Bz ≤ −10 nT |
| 520 | and speed ≥ 450 km/s (a rule of thumb, not a Kp model). | 520 | 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). | ||
| 522 | - Sun path, golden and blue hour, day length. | 524 | - Sun path, golden and blue hour, day length. |
| 523 | - CCOR-2 coronagraph loop, WSA-Enlil forecast line, solar cycle progress. | 525 | - CCOR-2 coronagraph loop, WSA-Enlil forecast line, solar cycle progress. |
| 524 | 526 | ||
Packages/MoonriseKit/Sources/Feeds/SolarRegions.swift added +59
| @@ -0,0 +1,59 @@ | |||
| 1 | import Foundation | ||
| 2 | |||
| 3 | /// NOAA-numbered active regions from SWPC's daily report (json/solar_regions.json), placed on the disk now. | ||
| 4 | public 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 { | |||
| 38 | public static let credit = "Courtesy of NASA/SDO and the AIA, EVE, and HMI science teams." | 38 | public static let credit = "Courtesy of NASA/SDO and the AIA, EVE, and HMI science teams." |
| 39 | 39 | ||
| 40 | /// Disk radius as a fraction of the frame's width. | 40 | /// 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 } |
| 42 | 42 | ||
| 43 | /// Whether a frame shows the whole disk. SDO publishes AIA frames that are black apart from the label along the | 43 | /// Whether a frame shows the whole disk. SDO publishes AIA frames that are black apart from the label along the |
| 44 | /// bottom, and some with part of the disk behind a curved dark edge. On a 64 px thumbnail, the share of the disk | 44 | /// 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 { | |||
| 123 | let o = try JSONDecoder().decode(Ovation.self, from: data) | 123 | let o = try JSONDecoder().decode(Ovation.self, from: data) |
| 124 | #expect(o.cells == [Ovation.Cell(lon: 0, lat: 65, probability: 12), Ovation.Cell(lon: 359, lat: -60, probability: 3)]) | 124 | #expect(o.cells == [Ovation.Cell(lon: 0, lat: 65, probability: 12), Ovation.Cell(lon: 359, lat: -60, probability: 3)]) |
| 125 | } | 125 | } |
| 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 | } | ||