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
extern crate time;

use std::ops::Rem;
use math::*;
use julian::*;
use delta_time::*;

#[repr(usize)]
#[derive(Clone, Copy)]
enum MoonPhase {
    NewMoon,
    FirstQuarterMoon,
    FullMoon,
    LastQuarterMoon
}

// From "Astronomical Algorithms"
// By Jean Meeus
fn get_time_of(phase: MoonPhase, lunation_number: f64) -> i64 {
    let k = lunation_number;
    let t = k / 1236.85;

    // FIXME: Use that?
    // k + 0.25 => first quarter
    // k + 0.50 => full moon
    // k + 0.75 => last quarter

    let e = 1.0 - 0.002_516 * t - 0.000_007_4 * t.powi(2);

    // Sun's mean anomaly at time JDE
    let s =  2.5534
           + 29.105_356_7  * k
           -  0.000_001_4  * t.powi(2) 
           -  0.000_000_11 * t.powi(3);

    // Moon's mean anomaly
    let m = 201.5643
           + 385.816_935_28  * k
           +   0.010_758_2   * t.powi(2)
           +   0.000_012_38  * t.powi(3)
           -   0.000_000_058 * t.powi(4);

    // Moon's argument of latitude
    let f = 160.7108
          + 390.670_502_84  * k
          -   0.001_611_8   * t.powi(2)
          -   0.000_002_27  * t.powi(3)
          +   0.000_000_011 * t.powi(4);

    // Longitude of the ascending node of the lunar orbit
    let o = 124.7746
          - 1.563_755_88 * k
          + 0.002_0672   * t.powi(2)
          + 0.000_002_15 * t.powi(3);

    let e = (e.rem(360.0) + 360.0).rem(360.0);
    let s = (s.rem(360.0) + 360.0).rem(360.0);
    let m = (m.rem(360.0) + 360.0).rem(360.0);
    let f = (f.rem(360.0) + 360.0).rem(360.0);
    let o = (o.rem(360.0) + 360.0).rem(360.0);

    let jde = 2_451_550.097_660
            +        29.530_588_861     * k
            +         0.000_154_370     * t.powi(2)
            -         0.000_000_150     * t.powi(3)
            +         0.000_000_000_730 * t.powi(4);

    // Correction to be added to JDE

    // [New Moon, First Quarter, Full Moon, Last Quarter]
    let num_cors = vec![
        [-0.40720, -0.62801, -0.40614, -0.62801],
        [ 0.17241,  0.17172,  0.17302,  0.17172],
        [ 0.01608, -0.01183,  0.01614, -0.01183],
        [ 0.01039,  0.00862,  0.01043,  0.00862],
        [ 0.00739,  0.00804,  0.00734,  0.00804],
        [-0.00514,  0.00454, -0.00515,  0.00454],
        [ 0.00208,  0.00204,  0.00209,  0.00204],
        [-0.00111, -0.00180, -0.00111, -0.00180],
        [-0.00057, -0.00070, -0.00057, -0.00070],
        [ 0.00056, -0.00040,  0.00056, -0.00040],
        [-0.00042, -0.00034, -0.00042, -0.00034],
        [ 0.00042,  0.00032,  0.00042,  0.00032],
        [ 0.00038,  0.00032,  0.00038,  0.00032],
        [-0.00024, -0.00028, -0.00024, -0.00028],
        [-0.00017,  0.00027, -0.00017,  0.00027],
        [-0.00007, -0.00017, -0.00007, -0.00017],
        [ 0.00004, -0.00005,  0.00004, -0.00005],
        [ 0.00004,  0.00004,  0.00004,  0.00004],
        [ 0.00003, -0.00004,  0.00003, -0.00004],
        [ 0.00003,  0.00004,  0.00003,  0.00004],
        [-0.00003,  0.00003, -0.00003,  0.00003],
        [ 0.00003,  0.00003,  0.00003,  0.00003],
        [-0.00002,  0.00002, -0.00002,  0.00002],
        [-0.00002,  0.00002, -0.00002,  0.00002],
        [ 0.00002, -0.00002,  0.00002, -0.00002]
    ];

    // Multiply each previous terms by E to a given power
    // [new moon, first quarter, full moon, last quarter]
    let pow_cors = vec![
        [0, 0, 0, 0],
        [1, 1, 1, 1],
        [0, 1, 0, 1],
        [0, 0, 0, 0],
        [1, 0, 1, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [1, 0, 1, 0],
        [0, 1, 0, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 2, 1, 2],
        [0, 1, 0, 1],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ];

    // Sum the following terms multiplied a number of times
    // given in the next table, and multiply the sinus of the
    // result by the previously obtained number.
    let terms = [s, m, f, o];

    // [new and full moon, first and last quarter]
    let mul_cors = vec![
        [[ 0.0,  1.0,  0.0,  0.0], [ 0.0,  1.0,  0.0,  0.0]],
        [[ 1.0,  0.0,  0.0,  0.0], [ 1.0,  0.0,  0.0,  0.0]],
        [[ 0.0,  2.0,  0.0,  0.0], [ 1.0,  1.0,  0.0,  0.0]],
        [[ 0.0,  0.0,  2.0,  0.0], [ 0.0,  2.0,  0.0,  0.0]],
        [[-1.0,  1.0,  0.0,  0.0], [ 0.0,  0.0,  2.0,  0.0]],
        [[ 1.0,  1.0,  0.0,  0.0], [-1.0,  1.0,  0.0,  0.0]],
        [[ 2.0,  0.0,  0.0,  0.0], [ 2.0,  0.0,  0.0,  0.0]],
        [[ 0.0,  1.0, -2.0,  0.0], [ 0.0,  1.0, -2.0,  0.0]],
        [[ 0.0,  1.0,  2.0,  0.0], [ 0.0,  1.0,  2.0,  0.0]],
        [[ 1.0,  2.0,  0.0,  0.0], [ 0.0,  3.0,  0.0,  0.0]],
        [[ 0.0,  3.0,  0.0,  0.0], [-1.0,  2.0,  0.0,  0.0]],
        [[ 1.0,  0.0,  2.0,  0.0], [ 1.0,  0.0,  2.0,  0.0]],
        [[ 1.0,  0.0, -2.0,  0.0], [ 1.0,  0.0, -2.0,  0.0]],
        [[-1.0,  2.0,  0.0,  0.0], [ 2.0,  1.0,  0.0,  0.0]],
        [[ 0.0,  0.0,  0.0,  1.0], [ 1.0,  2.0,  0.0,  0.0]],
        [[ 2.0,  1.0,  0.0,  0.0], [ 0.0,  0.0,  0.0,  1.0]],
        [[ 0.0,  2.0, -2.0,  0.0], [-1.0,  1.0, -2.0,  0.0]],
        [[ 3.0,  0.0,  0.0,  0.0], [ 0.0,  2.0,  2.0,  0.0]],
        [[ 1.0,  1.0, -2.0,  0.0], [ 1.0,  1.0,  2.0,  0.0]],
        [[ 0.0,  2.0,  2.0,  0.0], [-2.0,  1.0,  0.0,  0.0]],
        [[ 1.0,  1.0,  2.0,  0.0], [ 1.0,  1.0, -2.0,  0.0]],
        [[-1.0,  1.0,  2.0,  0.0], [ 3.0,  0.0,  0.0,  0.0]],
        [[-1.0,  1.0, -2.0,  0.0], [ 0.0,  2.0, -2.0,  0.0]],
        [[ 1.0,  3.0,  0.0,  0.0], [-1.0,  1.0,  2.0,  0.0]],
        [[ 0.0,  4.0,  0.0,  0.0], [ 1.0,  3.0,  0.0,  0.0]]
    ];

    let j = phase as usize;
    let cor = (0..25).fold(0.0, |acc, i| {
        let sin_cor = (0..4).fold(0.0, |sa, si| {
            sa + mul_cors[i][j % 2][si] * terms[si]
        });

        acc + num_cors[i][j] * e.powi(pow_cors[i][j]) * sin_deg(sin_cor)
    });

    // Additional corrections for quarters
    let w = 0.00306
          - 0.00038 * e * cos_deg(s)
          + 0.00026 *     cos_deg(m)
          - 0.00002 *     cos_deg(m - s)
          + 0.00002 *     cos_deg(m + s)
          + 0.00002 *     cos_deg(2.0 * f);

    let cor = match phase {
        MoonPhase::FirstQuarterMoon => cor + w,
        MoonPhase::LastQuarterMoon  => cor - w,
        _                           => cor
    };

    // Additional corrections for all phases
    let add = 0.0
            + 0.000_325 * sin_deg(299.77 +  0.107_408 * k - 0.009_173 * t.powi(2))
            + 0.000_165 * sin_deg(251.88 +  0.016_321 * k)
            + 0.000_164 * sin_deg(251.83 + 26.651_886 * k)
            + 0.000_126 * sin_deg(349.42 + 36.412_478 * k)
            + 0.000_110 * sin_deg( 84.66 + 18.206_239 * k)
            + 0.000_062 * sin_deg(141.74 + 53.303_771 * k)
            + 0.000_060 * sin_deg(207.14 +  2.453_732 * k)
            + 0.000_056 * sin_deg(154.84 +  7.306_860 * k)
            + 0.000_047 * sin_deg( 34.52 + 27.261_239 * k)
            + 0.000_042 * sin_deg(207.19 +  0.121_824 * k)
            + 0.000_040 * sin_deg(291.34 +  1.844_379 * k)
            + 0.000_037 * sin_deg(161.72 + 24.198_154 * k)
            + 0.000_035 * sin_deg(239.56 + 25.513_099 * k)
            + 0.000_023 * sin_deg(331.55 +  3.592_518 * k);

    let jde = jde + cor + add;

    terrestrial_to_universal_time(julian_to_unix(jde))
}

pub fn get_new_moon(lunation_number: f64) -> i64 {
    get_time_of(MoonPhase::NewMoon, lunation_number)
}

pub fn get_first_quarter_moon(lunation_number: f64) -> i64 {
    get_time_of(MoonPhase::FirstQuarterMoon, lunation_number)
}

pub fn get_full_moon(lunation_number: f64) -> i64 {
    get_time_of(MoonPhase::FullMoon, lunation_number)
}

pub fn get_last_quarter_moon(lunation_number: f64) -> i64 {
    get_time_of(MoonPhase::LastQuarterMoon, lunation_number)
}

/// Computes the Lunation Number since the first new moon of 2000
pub fn get_lunation_number(timestamp: i64) -> f64 {
    ((unix_to_year(timestamp) - 2000.0) * 12.3685).floor()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_lunation_number_test() {
        assert_eq!(-283.0, get_lunation_number(225085072));
    }

    #[test]
    fn get_new_moon_test() {
        // Example 49.a from "Astronomical Algoritms"
        // New Moon: 1977-02-18 03:37:42 TD
        let lunation_number = -283.0;
        let t = terrestrial_to_universal_time(225085062);
        assert_eq!(t, get_new_moon(lunation_number));
    }

    #[test]
    fn get_last_quarter_moon_test() {
        // Example 49.b from "Astronomical Algoritms"
        // Last Quarter Moon: 2044-01-21 23:48:17 TD
        let lunation_number = 544.75;
        let t = terrestrial_to_universal_time(2337032897);
        assert_eq!(t, get_last_quarter_moon(lunation_number));
    }
}