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

Packages/MoonriseKit/Sources/SkyEngine/Geomagnetic.swift

24 lines · 1039 bytes

3 symbols in this file
 1import Foundation
 2
 3/// Geomagnetic latitude from a centred-dipole model of Earth's field.
 4public enum Geomagnetic {
 5    /// North geomagnetic pole, IGRF-14 for 2025 (approximately 80.8° N, 72.8° W).
 6    public static let poleLatitude = 80.8
 7    public static let poleLongitude = -72.8
 8
 9    /// Geomagnetic latitude in degrees for a geographic position.
10    public static func latitude(_ observer: Observer) -> Double {
11        let r = Double.pi / 180
12        let s = sin(observer.latitude * r) * sin(poleLatitude * r)
13            + cos(observer.latitude * r) * cos(poleLatitude * r) * cos((observer.longitude - poleLongitude) * r)
14        return asin(max(-1, min(1, s))) / r
15    }
16
17    /// The backend's 5° band, named by its equatorward edge: 50 covers 50–55° N, -55 covers 50–55° S.
18    /// Must match `bandFor` in server/src/aurora.ts.
19    public static func band(_ observer: Observer) -> Int {
20        let m = latitude(observer)
21        let b = Int((abs(m) / 5).rounded(.down)) * 5
22        return m < 0 ? -(b + 5) : b
23    }
24}