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
/*
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.
*/

//! The Sun

use angle;
use time;
use std;
use planet;
use coords;

/**
Computes the Sun's equatorial semidiameter

# Arguments

* `sun_earth_dist`: Sun-Earth distance *| in AU*
**/
#[inline]
pub fn semidiameter(sun_earth_dist: f64) -> f64 {

    angle::deg_frm_dms(0, 0, 959.63) / sun_earth_dist

}

/**
Computes the Sun's geocentric ecliptic position, referred to the mean
equinox of the date

# Returns

`(sun_ecl_point, sun_earth_dist)`

* `sun_ecl_point` : Ecliptic point of the Sun *| in radians*
* `sun_earth_dist`: Sun-Earth distance *| in AU*

# Arguments

* `JD`: Julian (Ephemeris) day
**/
pub fn geocent_ecl_pos(JD: f64) -> (coords::EclPoint, f64) {

    let (L, B, R) = planet::heliocent_coords(&planet::Planet::Earth, JD);

    let ecl_point = coords::EclPoint {
        long: angle::limit_to_two_PI(L + std::f64::consts::PI),
        lat:  angle::limit_to_two_PI(-B)
    };

    (ecl_point, R)

}

/**
Computes the Sun's geocentric ecliptic coordinates converted to the
FK5 system

# Returns

`(ecl_long_FK5, ecl_lat_FK5)`

* `ecl_long_FK5`: Ecliptic longitude of the Sun *| in radians*,
                  converted to the FK5 system
* `ecl_lat_FK5` : Ecliptic latitude of the Sun *| in radians*,
                  converted to the FK5 system

# Arguments

* `JD`      : Julian (Ephemeris) day
* `ecl_long`: Ecliptic longitude of the Sun on `JD`
              *| in radians*, referred to the mean equinox
              of the date
* `ecl_lat` : Ecliptic latitude of the Sun `JD`
              *| in radians*, referred to the mean equinox
              of the date
**/
pub fn ecl_coords_to_FK5(JD: f64, ecl_long: f64, ecl_lat: f64) -> (f64, f64) {

    let ecl_long_FK5 =
        ecl_long
      - angle::deg_frm_dms(0, 0, 0.09033).to_radians();

     let JC = time::julian_cent(JD);
     let lambda1 =
         ecl_long
       - JC * (1.397 + JC*0.00031).to_radians();

    let ecl_lat_FK5 =
        ecl_lat
      + angle::deg_frm_dms(0, 0, 0.03916).to_radians() * (
          lambda1.cos() - lambda1.sin()
        );

    (ecl_long_FK5, ecl_lat_FK5)

}

/**
Computes the Sun's geocentric rectangular coordinates, referred to
the mean equinox of the date

# Returns

`(x, y z)`

* `x`: The X coordinate *| in AU*
* `y`: The Y coordinate *| in AU*
* `z`: The Z coordinate *| in AU*

* The positive x-axis is directed towards the Earth's vernal equinox
(0 degrees longitude)
* The positive y-axis lies in the plane of the Earth's equator and is
directed towards 90 degrees longitude
* The positive z-axis is directed towards the Earth's northern
celestial pole

# Arguments

* `sun_geo_long`: The Sun's geometric longitude *| in radians*,
                      *without* corrections for nutation and abberation
* `sun_geo_lat` : The Sun's geometric latitude *| in radians*,
                     *without* corrections for nutation and abberation
* `sun_rad_vec` : The Sun's geometric radius vector *| in AU*
* `mn_oblq`     : Mean obliquity of the ecliptic
**/
pub fn geocent_rect_coords (

    sun_geo_long : f64,
    sun_geo_lat  : f64,
    sun_rad_vec  : f64,
    mn_oblq      : f64

) -> (f64, f64, f64) {

    let x = sun_rad_vec * sun_geo_lat.cos() * sun_geo_long.cos();

    let y = sun_rad_vec * (
        sun_geo_lat.cos() * sun_geo_long.sin() * mn_oblq.cos()
      - sun_geo_lat.sin() * mn_oblq.sin()
    );

    let z = sun_rad_vec * (
        sun_geo_lat.cos() * sun_geo_long.sin() * mn_oblq.sin()
      + sun_geo_lat.sin() * mn_oblq.cos()
    );

    (x, y, z)

}

/**
Return quantites used in the ephemeris for physical observations of
the Sun

# Returns

`(P, B0, L0)`

* `P` : Position angle of the northern extremity of the axis of
        rotation, measured eastwards from the North point of the
        solar disk *| in radians*
* `B0`: Heliographic latitude of the center of the solar
        disk *| in radians*
* `L0`: Heliographic longitude of the center of the solar
        disk *| in radians*

# Arguments

* `JD`      : Julian (Ephemeris) day
* `app_long`: Apparent longitude of the Sun *| in radians*,
                  including the effect of abberation and *not* that
                  of nutation
* `app_long_with_nut`: Apparent longitude of the Sun *| in radians*,
                  including the effect of abberation *and* that
                  of nutation
* `oblq_eclip`: True obliquity of the ecliptic *| in radians*
**/
pub fn ephemeris (

    JD                : f64,
    app_long          : f64,
    app_long_with_nut : f64,
    oblq_eclip        : f64

) -> (f64, f64, f64) {

    let theta = angle::limit_to_360((JD - 2398220.0) * 360.0/25.38).to_radians();
    let I = 7.25_f64.to_radians();
    let K = (73.6667 + 1.3958333*(JD - 2396758.0)/36525.0).to_radians();

    let z = app_long - K;
    let sin_z = z.sin();
    let cos_z = z.cos();

    let mut x = (-app_long_with_nut.cos() * oblq_eclip.tan()).atan();
    let mut y = (-cos_z * I.tan()).atan();
    x = magnitude_limited_to_less_than_PI(x);
    y = magnitude_limited_to_less_than_PI(y);

    let B_0 = (sin_z * I.sin()).asin();
    let nu = (-sin_z * I.cos()).atan2(-cos_z);
    let L_0 = angle::limit_to_two_PI(nu - theta);

    let P = x + y;

    (P, B_0, L_0)

}

#[inline]
fn magnitude_limited_to_less_than_PI(a: f64) -> f64 {

    let PI_INTO_THREE_BY_TWO = std::f64::consts::PI * 3.0/2.0;

    if a > PI_INTO_THREE_BY_TWO { a - angle::TWO_PI }
    else                        { a }

}

/**
Computes an approximate time for the beginning of a solar synodic
rotation

# Returns

* `JD`: The Julian Day corresponding to the approximate time for
        the beginning of a solar synodic rotation

Between the years 1850 and 2100, `JD` will be in less than 0.002 days
in error.

# Arguments

* `C`: Carrington's synodic rotation number
**/
pub fn synodic_rot(C: i64) -> f64 {

    let M = (281.96 + 26.882476*(C as f64)).to_radians();

    2398140.227 + 27.2752316*(C as f64)
  + 0.1454 * M.sin()
  - 0.0085 * (2.0 * M).sin()
  - 0.0141 * (2.0 * M).cos()

}