Debian Codegraph @

src/moon.rs

raw symbols SCIP
1 /*! Lunar Dynamics
2
3 Contains one main type, `Moon`, with methods for:
4
5 * Location
6 * Distance
7 * Illuminated Fraction
8 * Phase Angle
9 * Angular Diameter
10 * Magnitude
11 * Parallax
12 */
13 use crate::{
14 coord::{self, Coord},
15 sol, time,
16 };
17
18 /// Structure for the moons orbital properties at an epoch.
19 ///
20 /// There's only one moon, but having the data and routines all in one type is cleaner.
21 /// The data contained in this is floating point instead of the Time/Coordinate types provided because static
22 /// Time/Angle/Coordinate data using those types is unpleasant to construct. And because the code I've written before
23 /// works with floats.
24 #[derive(Copy, Clone)]
25 pub struct Moon {
26 /// The epoch on which this data is based off
27 pub epoch: f64,
28 /// Mean Longitude
29 pub l0: f64,
30 /// Mean Longitude of the node
31 pub n0: f64,
32 /// Inclination
33 pub i: time::Angle,
34 /// Eccentricity
35 pub e: f64,
36 /// Semi-major axis
37 pub a: f64,
38 /// Angular size at 1AU
39 pub theta0: time::Angle,
40 /// Parallax at 1AU
41 pub pi0: time::Angle,
42 }
43
44 /// The moon at epoch January 1980 0.0
45 pub const MOON: Moon = Moon {
46 epoch: 2444238.5, // January 1980 0.0
47 l0: 64.975464,
48 n0: 151.950429,
49 i: time::Angle::from_degrees(5.145396),
50 e: 0.054900,
51 a: 0.002569562, // AU
52 theta0: time::Angle::from_degrees(0.0013312900722),
53 pi0: time::Angle::from_degrees(0.0024428825934),
54 };
55
56 impl Moon {
57 /// Gets a ton of information about the moon that is used by other functions
58 ///
59 /// From moontool.c by John Walker
60 fn mooninfo(self, d: time::Date) -> (time::Angle, coord::Coord, f64) {
61 /* Calculation of the Sun's position */
62 let day = d.julian() - self.epoch; /* Date within epoch */
63 let m = time::Angle::from_degrees(((360.0 / 365.2422) * day) + 278.833540 - 282.596403); /* Convert from perigee co-ordinates to epoch 1980.0 */
64 let lambdasun = sol::SUN.location(d).ecliptic(d).0;
65
66 // Moon's mean longitude
67 let ml = time::Angle::from_degrees(13.1763966 * day + self.l0);
68
69 // Moon's mean anomaly
70 let mm = time::Angle::from_degrees(ml.degrees() - 0.1114041 * day - 349.383063); /* 349: Mean longitude of the perigee at the epoch */
71
72 // Evection
73 let ev = 1.2739 * ((ml - lambdasun) * 2.0 - mm).sin();
74
75 // Annual equation
76 let ae = 0.1858 * m.sin();
77
78 // Corrected anomaly
79 let mmp = time::Angle::from_degrees(mm.degrees() + ev - ae - (0.37 * m.sin()));
80
81 // Correction for the equation of the centre
82 let mec = time::Angle::from_degrees(6.2886 * mmp.sin());
83
84 // Corrected longitude
85 let lp = time::Angle::from_degrees(
86 ml.degrees() + ev + (6.2886 * mmp.sin()) - ae + (0.214 * (mmp * 2.0).sin()),
87 );
88
89 // True longitude
90 let lpp = time::Angle::from_degrees(lp.degrees() + (1.3166 * (lp - lambdasun).sin()));
91
92 // Corrected longitude of the node
93 let np = time::Angle::from_degrees(
94 time::Angle::from_degrees(self.n0 - 0.0529539 * day).degrees() - 0.16 * m.sin(),
95 );
96
97 let lambdamoon =
98 time::Angle::from_radians(((lpp - np).sin() * self.i.cos()).atan2((lpp - np).cos()))
99 + np;
100 let betamoon = time::Angle::asin((lpp - np).sin() * self.i.sin());
101
102 let dist = (self.a * (1.0 - self.e * self.e)) / (1.0 + self.e * (mmp + mec).cos());
103
104 (
105 // Age of the Moon in degrees
106 lpp - lambdasun,
107 // Coordinates of the moon
108 coord::Coord::from_ecliptic(lambdamoon, betamoon, d),
109 // Distance from earth
110 dist,
111 )
112 }
113
114 /// Gets the cartesian coordinates of the moon in AU
115 pub fn locationcart(self, d: time::Date) -> (f64, f64, f64) {
116 let (_, crd, dist) = self.mooninfo(d);
117 Coord::cartesian(crd, dist)
118 }
119
120 /// Returns age of phase in Days
121 pub fn phaseage(self, d: time::Date) -> f64 {
122 29.53058868 * self.mooninfo(d).0.turns()
123 }
124
125 /// Returns the illuminated fraction of the Moons surface
126 pub fn illumfrac(self, d: time::Date) -> f64 {
127 (1.0 - self.mooninfo(d).0.cos()) / 2.0
128 }
129
130 /// Returns the phase angle of the moon
131 pub fn phaseangle(self, d: time::Date) -> time::Angle {
132 self.mooninfo(d).0
133 }
134 /// Returns the coordinates of the moon
135 ///
136 /// This code has a low accuracy of around 5 degrees
137 pub fn location(self, d: time::Date) -> coord::Coord {
138 self.mooninfo(d).1
139 }
140
141 /// Returns the distance to the moon in AU
142 pub fn distance(self, d: time::Date) -> f64 {
143 self.mooninfo(d).2
144 }
145
146 /// Returns angular diameter of the planet at current time
147 pub fn angdia(self, d: time::Date) -> time::Angle {
148 self.theta0 / self.distance(d)
149 }
150
151 /// Calculates the moons horizontal parallax
152 pub fn parallax(self, d: time::Date) -> time::Angle {
153 self.pi0 / self.distance(d)
154 }
155
156 /// Magnitude of the moon
157 pub fn magnitude(self, d: time::Date) -> f64 {
158 5.0 * (self.distance(d) / self.illumfrac(d).sqrt()).log10() + 0.21
159 }
160 }
161
162 #[cfg(test)]
163 mod tests {
164 use super::*;
165
166 #[test]
167 fn test_moonlocation() {
168 assert_eq!(
169 MOON.location(time::Date::from_julian(2460748.554861)),
170 coord::Coord::from_equatorial(
171 time::Angle::from_degminsec(172, 11, 15.7),
172 time::Angle::from_degminsec(3, 59, 15.2)
173 )
174 );
175 }
176
177 #[test]
178 fn test_moonphase() {
179 assert_eq!(
180 MOON.illumfrac(time::Date::from_calendar(
181 2025,
182 03,
183 29,
184 time::Angle::default()
185 )),
186 0.002799062630499616
187 );
188 assert_eq!(
189 MOON.illumfrac(time::Date::from_calendar(
190 2025,
191 04,
192 09,
193 time::Angle::default()
194 )),
195 0.8694887493109439
196 );
197 assert_eq!(
198 MOON.magnitude(time::Date::from_calendar(
199 2024,
200 12,
201 25,
202 time::Angle::default()
203 )),
204 -11.366493493867907
205 );
206 }
207
208 #[test]
209 fn test_moondist() {
210 assert_eq!(
211 MOON.distance(time::Date::from_julian(2460748.467894)),
212 0.0026765709280575905
213 );
214 assert_eq!(
215 MOON.angdia(time::Date::from_julian(2460748.467894)),
216 time::Angle::from_degrees(0.499999999)
217 );
218 }
219 }