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
/*
Copyright (c) 2015, 2016 Saurav Sachidanand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//! Time of rise, transit and set for a celestial body

use angle;
use coords;
use interpol;

/// Represents a celestial body in transit
pub enum TransitBody {
    /// A star or a planet
    StarOrPlanet,
    /// The Sun
    Sun,
    /// The Moon
    Moon
}

/// Represents a transit type
pub enum TransitType {
    /// Rise
    Rise,
    /// Transit at zenith
    Transit,
    /// Set
    Set
}

/**
Computes the time of transit for a celestial body

# Returns

* `(hour, min, sec)`: Time of transit on the day of interest, in UTC

# Arguments

* `transit_type`  : A `TransitType`
* `transit_body`  : The `TransitBody`
* `geograph_point`: Geographic point of the observer *| in radians*

Let `JD` be the Julian (Ephemeris) day of interest,

* `eq_point1`: Equatorial point of the transit body on `JD - 1` *| in radians*
* `eq_point2`: Equatorial point of the transit body on `JD` *| in radians*
* `eq_point3`: Equatorial point of the transit body on `JD + 1` *| in radians*
* `apprnt_greenwhich_sidr`: Apparent sidereal time at Greenwhich on `JD` *| in radians*
* `delta_t`: ΔT for `JD` (Julian day)
* `moon_eq_hz_parallax`: Equatorial horizontal parallax of the Moon on `JD`
                             *| in radians*. *Pass a meaningfull value here only when*
                             `TransitBody::Moon` *is passed for* `transit_body`.

**/
pub fn time (

    transit_type           : &TransitType,
    transit_body           : &TransitBody,
    geograph_point         : &coords::GeographPoint,
    eq_point1              : &coords::EqPoint,
    eq_point2              : &coords::EqPoint,
    eq_point3              : &coords::EqPoint,
    apprnt_greenwhich_sidr : f64,
    delta_t                : f64,
    moon_eq_hz_parallax    : f64

) -> (i64, i64, f64) {

    let h0 = match transit_body {
        &TransitBody::StarOrPlanet => -0.5667_f64.to_radians(),
        &TransitBody::Sun          => -0.8333_f64.to_radians(),
        &TransitBody::Moon         =>  0.7275 * moon_eq_hz_parallax
                                     - 0.5667_f64.to_radians(),
    };

    let mut H0 = (
        (h0.sin() - geograph_point.lat.sin() * eq_point2.dec.sin()) /
        (geograph_point.lat.cos() * eq_point2.dec.cos())
    ).acos();
    H0 = angle::limit_to_two_PI(H0);

    let mut m = m(
        &transit_type, H0, eq_point2.asc, geograph_point.long,
        apprnt_greenwhich_sidr);
    let theta0 = apprnt_greenwhich_sidr + m*360.985647_f64.to_radians();

    let d = m + delta_t/86400.0;

    let asc = interpol::three_values(eq_point1.asc, eq_point2.asc, eq_point3.asc, d);

    let dec = match transit_type {
        &TransitType::Transit => 0.0,

        &TransitType::Rise    => interpol::three_values(
                                    eq_point1.dec, eq_point2.dec,
                                    eq_point3.dec, d),

        &TransitType::Set     => interpol::three_values(
                                    eq_point1.dec, eq_point2.dec,
                                    eq_point3.dec, d)
    };

    let mut H = coords::hr_angl_frm_observer_long
        (theta0, geograph_point.long, asc).to_degrees();
    H = angle::limit_to_360(H);
    if H > 180.0 { H -= 360.0; }
    H = H.to_radians();

    let h = match transit_type {
        &TransitType::Transit => 0.0,
        &TransitType::Rise    => coords::alt_frm_eq(H, dec, geograph_point.lat),
        &TransitType::Set     => coords::alt_frm_eq(H, dec, geograph_point.lat)
    };

    m += match transit_type {
        &TransitType::Transit => -H / angle::TWO_PI,
        &TransitType::Rise    => (h - h0) / (angle::TWO_PI * dec.cos() * geograph_point.lat.cos() * H.sin()),
        &TransitType::Set     => (h - h0) / (angle::TWO_PI * dec.cos() * geograph_point.lat.cos() * H.sin())
    };

    let h = 24.0 * m;
    let hour = h as i64;
    let m = (h - (hour as f64)) * 60.0;
    let minute = m as i64;
    let second = (m - (minute as f64)) * 60.0;

    (hour, minute, second)

}

#[inline]
fn m (

    transit_type : &TransitType,
    H0           : f64,
    asc          : f64,
    L            : f64,
    Theta0       : f64

) -> f64 {

    let mut m = (asc + L - Theta0)/angle::TWO_PI;
    let p = H0/angle::TWO_PI;

    m += match transit_type {
        &TransitType::Transit => 0.0,
        &TransitType::Rise    => -p,
        &TransitType::Set     => p
    };

    if      m < 0.0 { m += 1.0 }
    else if m > 1.0 { m -= 1.0 }

    m

}