Debian Codegraph @

src/probe.rs

raw symbols SCIP
1 /*! Segmented Solar Dynamics and handling of planets orbits
2
3 This function has one main type, [`SegmentedPlanet`] With methods for:
4
5 * Cartesian Coordinates
6 * Distance from earth
7
8 Orbital property and correction numbers from <https://ssd.jpl.nasa.gov/planets/approx_pos.html>
9 and JPL Horizons <https://ssd.jpl.nasa.gov/horizons/>
10 */
11
12 use crate::{coord, sol::EARTH, time};
13
14 /// Generalized Planet Structure containing keplerian orbital properties and corrections.
15 ///
16 /// Ephemeris for planets uses Keplerian motion with correction for perturbations of other planets
17 /// Error is at most 10' for most use, well within range of wanted accuracy.
18 #[derive(Clone, Debug, PartialEq)]
19 pub struct SegmentedPlanet {
20 /// Planet Name
21 pub name: &'static str,
22 /// Semi-Major Axis (AU)
23 pub a: f64,
24 /// Eccentricity
25 pub e: f64,
26 /// Inclination (Degrees)
27 pub i: f64,
28 /// Longitude of the Periapsis (Degrees)
29 pub w: f64,
30 /// Longitude of the ascending node (Degrees)
31 pub o: f64,
32 /// Mean longitude (Degrees)
33 pub l: f64,
34 /// Correction rates of the mean longitude per Julian Century (degrees)
35 pub l_delta_century: f64,
36 /// Epoch of the Mean Longitude
37 pub l_epoch: time::Date,
38 }
39 impl SegmentedPlanet {
40 /// Returns the location of the planets as rectangular coordinates as relative to the Sun, in AU
41 ///
42 /// From <https://ssd.jpl.nasa.gov/planets/approx_pos.html>
43 pub fn locationcart(&self, d: time::Date) -> (f64, f64, f64) {
44 let t = (d.julian() - self.l_epoch.julian()) / 36525.0;
45 let a = self.a;
46 let e = self.e;
47 let i = time::Angle::from_degrees(self.i);
48 let o = time::Angle::from_degrees(self.o);
49 let w = time::Angle::from_degrees(self.w);
50 let l = time::Angle::from_degrees(self.l + (self.l_delta_century * t));
51 let ww = w - o;
52 let mut m = (l - w).degrees();
53 m = time::Angle::from_degrees(m).to_latitude().degrees();
54
55 fn kepler(m: f64, e: f64, ee: f64) -> f64 {
56 let dm = m - (ee - e.to_degrees() * (ee.to_radians().sin()));
57 dm / (1.0 - e * (ee.to_radians()).cos())
58 }
59 let mut ee = m + 57.29578 * e * (m.to_radians().sin());
60 let mut de: f64 = 1.0;
61 while de.abs() > 1e-7 {
62 de = kepler(m, e, ee);
63 ee += de;
64 }
65
66 if e < 1.0 {
67 let v = 2.0
68 * ((1.0 + e).sqrt() * (ee.to_radians() / 2.0).sin())
69 .atan2((1.0 - e).sqrt() * (ee.to_radians() / 2.0).cos());
70 eprintln!("{}", l.degrees());
71 let xp = a * ((ee.to_radians()).cos() - e);
72 let yp = a * (1.0 - e * e).sqrt() * (ee.to_radians().sin());
73
74 let xecl = (ww.cos() * o.cos() - ww.sin() * o.sin() * i.cos()) * xp
75 + (-ww.sin() * o.cos() - ww.cos() * o.sin() * i.cos()) * yp;
76 let yecl = (ww.cos() * o.sin() + ww.sin() * o.cos() * i.cos()) * xp
77 + (-ww.sin() * o.sin() + ww.cos() * o.cos() * i.cos()) * yp;
78 let zecl = (ww.sin() * i.sin()) * xp + (ww.cos() * i.sin()) * yp;
79
80 let eps = 23.43928_f64.to_radians();
81 let tx = xecl;
82 let ty = eps.cos() * yecl - eps.sin() * zecl;
83 let tz = eps.sin() * yecl + eps.cos() * zecl;
84
85 return (tx, ty, tz);
86 } else {
87 todo!();
88 }
89 }
90
91 /// Returns coordinates as subtracted from the earths coordinates
92 pub fn location(&self, d: time::Date) -> coord::Coord {
93 let c = self.locationcart(d);
94 let e = EARTH.locationcart(d);
95
96 coord::Coord::from_cartesian(c.0 - e.0, c.1 - e.1, c.2 - e.2)
97 }
98
99 /// Returns distance in AU
100 pub fn distance(&self, d: time::Date) -> f64 {
101 let c = self.locationcart(d);
102 let e = EARTH.locationcart(d);
103 let (tx, ty, tz) = (c.0 - e.0, c.1 - e.1, c.2 - e.2);
104
105 (tx * tx + ty * ty + tz * tz).sqrt()
106 }
107
108 fn sun_distance(&self, d: time::Date) -> f64 {
109 let (tx, ty, tz) = self.locationcart(d);
110 (tx * tx + ty * ty + tz * tz).sqrt()
111 }
112 }
113
114 /// Voyager 2 Test Object
115 pub const VOYAGER2TEST: SegmentedPlanet = SegmentedPlanet {
116 name: "Voyager 2 Test of 1983-11-30",
117 a: -3.922739981,
118 e: 3.44861399869959,
119 i: 2.66291222081523,
120 w: 77.45779628,
121 o: 48.33076593,
122 l: 298.15,
123 l_delta_century: 55390.810728252,
124 l_epoch: time::Date::from_julian(2445668.5),
125 };
126 /// Mars
127 pub const MARS: SegmentedPlanet = SegmentedPlanet {
128 name: "Mars Control Test Object",
129 a: 1.52371034,
130 e: 0.09339410,
131 i: 1.84969142,
132 l: -4.55343205,
133 w: -23.94362959,
134 o: 49.55953891,
135 l_delta_century: 19140.30268499,
136 l_epoch: time::Date::from_julian(2451545.0),
137 };
138 /// Parker Solar Probe Test Object
139 pub const PARKERTEST: SegmentedPlanet = SegmentedPlanet {
140 name: "Parker Solar Probe Test of 2025-09-09",
141 a: 0.3884911788,
142 e: 0.881936788230005,
143 i: 3.39525331656616,
144 w: 68.4610434250691,
145 o: 76.6442910488463,
146 l: 477.32,
147 l_delta_century: 148669.7918,
148 l_epoch: time::Date::from_julian(2460928.5),
149 };
150 /// Halleys Commet
151 pub const HALLEY: SegmentedPlanet = SegmentedPlanet {
152 name: "Halley's Commet",
153 a: 17.8591256074516,
154 e: 0.967894644637869,
155 i: 162.151115110664,
156 o: 59.5723978313194,
157 w: 112.470381868335,
158 l: 360.1,
159 l_delta_century: 365.25,
160 l_epoch: time::Date::from_julian(2460928.5),
161 };
162 /// Mars, Again
163 pub const SUPERSURE: SegmentedPlanet = SegmentedPlanet {
164 name: "Mars Again",
165 a: 1.52362660139838,
166 e: 0.0934994380486591,
167 i: 1.84752995631806,
168 o: 49.483749371897,
169 w: 286.651495403059,
170 l: 592.71,
171 l_delta_century: 19141.02,
172 l_epoch: time::Date::from_julian(2460927.5),
173 };
174
175 #[cfg(test)]
176 mod tests {
177 use super::*;
178
179 #[test]
180 fn test_voy2831130() {
181 return;
182 let x = SUPERSURE.location(time::Date::from_calendar(
183 2025,
184 9,
185 9,
186 time::Angle::from_degrees(0.0),
187 ));
188 eprintln!(
189 "{}, {}",
190 x.equatorial().0.decimal(),
191 x.equatorial().1.to_latitude().degrees()
192 );
193 todo!();
194 }
195 }