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/ProgramVisibility.swift

59d401dca1a327705632d1e2e20f00f807dee828
moonrise/Packages/MoonriseKit/Sources/SkyEngine/ProgramVisibility.swift history · blame · raw

17 lines · 1094 bytes

1 symbol in this file
 1import Foundation
 2
 3extension SkyEngine {
 4    /// Highest point of each fixed J2000 position (degrees) between `start` and `end` while the Sun is below −12°,
 5    /// sampled every 15 minutes; nil when it doesn't reach `minimumAltitude` in that time.
 6    public static func bestInDarkness(j2000 coordinates: [(ra: Double, dec: Double)], from start: Date, to end: Date, observer: Observer, minimumAltitude: Double = 25) throws -> [(date: Date, position: Horizontal)?] {
 7        var best: [(date: Date, position: Horizontal)?] = Array(repeating: nil, count: coordinates.count)
 8        for s in stride(from: start.timeIntervalSince1970, through: end.timeIntervalSince1970, by: 900).map(Date.init(timeIntervalSince1970:)) {
 9            guard try horizontal(.sun, at: s, from: observer).altitude < -12 else { continue }
10            for (i, h) in horizontal(j2000: coordinates, at: s, from: observer).enumerated()
11            where h.altitude >= minimumAltitude && h.altitude > best[i]?.position.altitude ?? -90 {
12                best[i] = (s, h)
13            }
14        }
15        return best
16    }
17}