-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNaif.swift
363 lines (343 loc) · 9.29 KB
/
Naif.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// Naif.swift
// Orbits
//
// Created by Ben Lu on 1/29/17.
// Copyright © 2017 Ben Lu. All rights reserved.
//
import Foundation
// https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/naif_ids.html#NAIF%20Object%20ID%20numbers
public enum Naif: Comparable, Hashable, ExpressibleByIntegerLiteral, CustomStringConvertible {
public enum MajorBody: Int {
case mercury = 199
case venus = 299
case earth = 399
case mars = 499
case jupiter = 599
case saturn = 699
case uranus = 799
case neptune = 899
case pluto = 999
public var moons: [Moon] {
return (1...98).compactMap { Moon(rawValue: $0) }
}
}
public enum Moon: Int {
case luna = 301
case phobos = 401
case deimos = 402
case io = 501
case europa = 502
case ganymede = 503
case callisto = 504
case amalthea = 505
case himalia = 506
case elara = 507
case pasiphae = 508
case sinope = 509
case lysithea = 510
case carme = 511
case ananke = 512
case leda = 513
case thebe = 514
case adrastea = 515
case metis = 516
case callirrhoe = 517
case themisto = 518
case magaclite = 519
case taygete = 520
case chaldene = 521
case harpalyke = 522
case kalyke = 523
case iocaste = 524
case erinome = 525
case isonoe = 526
case praxidike = 527
case autonoe = 528
case thyone = 529
case hermippe = 530
case aitne = 531
case eurydome = 532
case euanthe = 533
case euporie = 534
case orthosie = 535
case sponde = 536
case kale = 537
case pasithee = 538
case hegemone = 539
case mneme = 540
case aoede = 541
case thelxinoe = 542
case arche = 543
case kallichore = 544
case helike = 545
case carpo = 546
case eukelade = 547
case cyllene = 548
case kore = 549
case herse = 550
case mimas = 601
case enceladus = 602
case tethys = 603
case dione = 604
case rhea = 605
case titan = 606
case hyperion = 607
case iapetus = 608
case phoebe = 609
case janus = 610
case epimetheus = 611
case helene = 612
case telesto = 613
case calypso = 614
case atlas = 615
case prometheus = 616
case pandora = 617
case pan = 618
case ymir = 619
case paaliaq = 620
case tarvos = 621
case ijiraq = 622
case suttungr = 623
case kiviuq = 624
case mundilfari = 625
case albiorix = 626
case skathi = 627
case erriapus = 628
case siarnaq = 629
case thrymr = 630
case narvi = 631
case methone = 632
case pallene = 633
case polydeuces = 634
case daphnis = 635
case aegir = 636
case bebhionn = 637
case bergelmir = 638
case bestla = 639
case farbauti = 640
case fenrir = 641
case fornjot = 642
case hati = 643
case hyrrokkin = 644
case kari = 645
case loge = 646
case skoll = 647
case surtur = 648
case anthe = 649
case jarnsaxa = 650
case greip = 651
case tarqeq = 652
case aegaeon = 653
case ariel = 701
case umbriel = 702
case titania = 703
case oberon = 704
case miranda = 705
case cordelia = 706
case ophelia = 707
case bianca = 708
case cressida = 709
case desdemona = 710
case juliet = 711
case portia = 712
case rosalind = 713
case belinda = 714
case puck = 715
case caliban = 716
case sycorax = 717
case prospero = 718
case setebos = 719
case stephano = 720
case trinculo = 721
case francisco = 722
case margaret = 723
case ferdinand = 724
case perdita = 725
case mab = 726
case cupid = 727
case triton = 801
case nereid = 802
case naiad = 803
case thalassa = 804
case despina = 805
case galatea = 806
case larissa = 807
case proteus = 808
case halimede = 809
case psamathe = 810
case sao = 811
case laomedeia = 812
case neso = 813
case charon = 901
case nix = 902
case hydra = 903
case kerberos = 904
case styx = 905
public var primary: MajorBody {
return MajorBody(rawValue: self.rawValue / 100 * 100 + 99)!
}
}
case majorBody(MajorBody)
case moon(Moon)
case sun
case custom(Int)
static let planets: [Naif] = {
let planets: [MajorBody] = [.mercury, .venus, .earth, .mars, .jupiter, .saturn, .uranus, .neptune, .pluto]
return planets.map { .majorBody($0) }
}()
public var description: String {
switch self {
case let .majorBody(mb):
return String(describing: mb).capitalized
case .sun:
return "Sun"
case let .moon(m):
if m == .luna {
return "Moon"
}
return String(describing: m).capitalized
default:
fatalError()
}
}
public var rawValue: Int {
switch self {
case let .majorBody(mb):
return mb.rawValue
case .sun:
return 10
case let .moon(m):
return m.rawValue
case let .custom(customId):
return customId
}
}
public var hashValue: Int {
return rawValue
}
public var primary: Naif? {
switch self {
case .sun:
return nil
case .majorBody:
return .sun
case .moon(let m):
return .majorBody(m.primary)
case .custom:
return nil
}
}
public var moons: [Naif] {
switch self {
case .sun:
return Naif.planets
case .majorBody(let mb):
return mb.moons.map { .moon($0) }
default:
return []
}
}
public init(integerLiteral value: Int) {
self.init(naifId: value)
}
public init(naifId: Int) {
if naifId == 10 {
self = .sun
} else if naifId % 100 == 99 {
if let mb = Naif.MajorBody(rawValue: naifId) {
self = .majorBody(mb)
} else {
fatalError("major planet naif id not found")
}
} else if naifId / 100 < 10 {
if let m = Naif.Moon(rawValue: naifId) {
self = .moon(m)
} else {
fatalError("moon naif id not found")
}
} else {
self = .custom(naifId)
}
}
/// Returns whether the current celestial body is the primary of the other
///
/// - Parameter otherNaif: Naif code of other celestial body
/// - Returns: Whether the current celestial body is the primary of the other
public func isPrimary(of otherNaif: Naif) -> Bool {
switch self {
case .moon:
return false
case .sun:
if case .majorBody = otherNaif {
return true
} else {
return false
}
case .majorBody(let mb):
if case let .moon(m) = otherNaif {
return mb.rawValue / 100 == m.rawValue / 100
} else {
return false
}
case .custom:
return false
}
}
public func isSatellite(of otherNaif: Naif) -> Bool {
switch self {
case .moon(let m):
if case let .majorBody(mb) = otherNaif {
return mb.rawValue / 100 == m.rawValue / 100
}
return false
case .majorBody:
if case .sun = otherNaif {
return true
} else {
return false
}
case .sun:
return false
case .custom:
return false
}
}
public static func <(lhs: Naif, rhs: Naif) -> Bool {
if lhs.rawValue == rhs.rawValue {
return false
}
// 10 < x99 < x01-x89
if lhs.rawValue == 10 {
return true
} else if rhs.rawValue == 10 {
return false
}
if lhs.rawValue / 100 == rhs.rawValue / 100 {
if lhs.rawValue % 100 == 99 {
return true
} else if rhs.rawValue % 100 == 99 {
return false
}
return lhs.rawValue < rhs.rawValue
} else {
// 1xx < 2xx < 3xx
return lhs.rawValue < rhs.rawValue
}
}
public static func ==(lhs: Naif, rhs: Naif) -> Bool {
return lhs.rawValue == rhs.rawValue
}
}
extension Naif {
var parents: Set<Naif> {
var results = Set<Naif>()
results.insert(self)
var current: Naif? = self
while let p = current?.primary {
results.insert(p)
current = current?.primary
}
return results
}
}