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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::cometdata as pa_c;
use crate::macros as pa_m;
use crate::util as pa_u;

/// Calculate position of an elliptical comet.
///
/// ## Arguments
/// * `lct_hour` -- Local civil time, hour part.
/// * `lct_min` -- Local civil time, minutes part.
/// * `lct_sec` -- Local civil time, seconds part.
/// * `is_daylight_saving` -- Is daylight savings in effect?
/// * `zone_correction_hours` -- Time zone correction, in hours.
/// * `local_date_day` -- Local date, day part.
/// * `local_date_month` -- Local date, month part.
/// * `local_date_year` -- Local date, year part.
/// * `comet_name` -- Name of comet, e.g., "Halley".
///
/// ## Returns
/// * `comet_ra_hour` -- Right ascension of comet (hour part)
/// * `comet_ra_min` -- Right ascension of comet (minutes part)
/// * `comet_dec_deg` -- Declination of comet (degrees part)
/// * `comet_dec_min` -- Declination of comet (minutes part)
/// * `comet_dist_earth` -- Comet's distance from Earth (AU)
pub fn position_of_elliptical_comet(
    lct_hour: f64,
    lct_min: f64,
    lct_sec: f64,
    is_daylight_saving: bool,
    zone_correction_hours: i32,
    local_date_day: f64,
    local_date_month: u32,
    local_date_year: u32,
    comet_name: String,
) -> (f64, f64, f64, f64, f64) {
    let daylight_saving = if is_daylight_saving == true { 1 } else { 0 };

    let greenwich_date_day = pa_m::lct_gday(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );
    let greenwich_date_month = pa_m::lct_gmonth(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );
    let greenwich_date_year = pa_m::lct_gyear(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );

    let (comet_info, _comet_info_status) = pa_c::get_comet_info_elliptical_vector(comet_name);

    let time_since_epoch_years = (pa_m::cd_jd(
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    ) - pa_m::cd_jd(0.0, 1, greenwich_date_year))
        / 365.242191
        + greenwich_date_year as f64
        - comet_info.epoch;
    let mc_deg = 360.0 * time_since_epoch_years / comet_info.period;
    let mc_rad = (mc_deg - 360.0 * (mc_deg / 360.0).floor()).to_radians();
    let eccentricity = comet_info.ecc;
    let true_anomaly_deg = pa_m::degrees(pa_m::true_anomaly(mc_rad, eccentricity));
    let lc_deg = true_anomaly_deg + comet_info.peri;
    let r_au = comet_info.axis * (1.0 - eccentricity * eccentricity)
        / (1.0 + eccentricity * ((true_anomaly_deg).to_radians()).cos());
    let lc_node_rad = (lc_deg - comet_info.node).to_radians();
    let psi_rad = ((lc_node_rad).sin() * ((comet_info.incl).to_radians()).sin()).asin();

    let y = (lc_node_rad).sin() * ((comet_info.incl).to_radians()).cos();
    let x = (lc_node_rad).cos();

    let ld_deg = pa_m::degrees(y.atan2(x)) + comet_info.node;
    let rd_au = r_au * (psi_rad).cos();

    let earth_longitude_le_deg = pa_m::sun_long(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    ) + 180.0;
    let earth_radius_vector_au = pa_m::sun_dist(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );

    let le_ld_rad = (earth_longitude_le_deg - ld_deg).to_radians();
    let a_rad = if rd_au < earth_radius_vector_au {
        (rd_au * (le_ld_rad).sin()).atan2(earth_radius_vector_au - rd_au * (le_ld_rad).cos())
    } else {
        (earth_radius_vector_au * (-le_ld_rad).sin())
            .atan2(rd_au - earth_radius_vector_au * (le_ld_rad).cos())
    };

    let comet_long_deg1 = if rd_au < earth_radius_vector_au {
        180.0 + earth_longitude_le_deg + pa_m::degrees(a_rad)
    } else {
        pa_m::degrees(a_rad) + ld_deg
    };
    let comet_long_deg = comet_long_deg1 - 360.0 * (comet_long_deg1 / 360.0).floor();
    let comet_lat_deg = pa_m::degrees(
        (rd_au * (psi_rad).tan() * ((comet_long_deg1 - ld_deg).to_radians()).sin()
            / (earth_radius_vector_au * (-le_ld_rad).sin()))
        .atan(),
    );
    let comet_ra_hours1 = pa_m::dd_dh(pa_m::ec_ra(
        comet_long_deg,
        0.0,
        0.0,
        comet_lat_deg,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    ));
    let comet_dec_deg1 = pa_m::ec_dec(
        comet_long_deg,
        0.0,
        0.0,
        comet_lat_deg,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    );
    let comet_distance_au = (num::pow(earth_radius_vector_au, 2) + num::pow(r_au, 2)
        - 2.0
            * earth_radius_vector_au
            * r_au
            * ((lc_deg - earth_longitude_le_deg).to_radians()).cos()
            * (psi_rad).cos())
    .sqrt();

    let comet_ra_hour = pa_m::dh_hour(comet_ra_hours1 + 0.008333);
    let comet_ra_min = pa_m::dh_min(comet_ra_hours1 + 0.008333);
    let comet_dec_deg = pa_m::dd_deg(comet_dec_deg1 + 0.008333);
    let comet_dec_min = pa_m::dd_min(comet_dec_deg1 + 0.008333);
    let comet_dist_earth = pa_u::round_f64(comet_distance_au, 2);

    return (
        comet_ra_hour as f64,
        comet_ra_min as f64,
        comet_dec_deg,
        comet_dec_min,
        comet_dist_earth,
    );
}

/// Calculate position of a parabolic comet.
///
/// ## Arguments
/// * `lct_hour` -- Local civil time, hour part.
/// * `lct_min` -- Local civil time, minutes part.
/// * `lct_sec` -- Local civil time, seconds part.
/// * `is_daylight_saving` -- Is daylight savings in effect?
/// * `zone_correction_hours` -- Time zone correction, in hours.
/// * `local_date_day` -- Local date, day part.
/// * `local_date_month` -- Local date, month part.
/// * `local_date_year` -- Local date, year part.
/// * `comet_name` -- Name of comet, e.g., "Kohler".
///
/// ## Returns
/// * `comet_ra_hour` -- Right ascension of comet (hour part)
/// * `comet_ra_min` -- Right ascension of comet (minutes part)
/// * `comet_ra_sec` -- Right ascension of comet (seconds part)
/// * `comet_dec_deg` -- Declination of comet (degrees part)
/// * `comet_dec_min` -- Declination of comet (minutes part)
/// * `comet_dec_sec` -- Declination of comet (seconds part)
/// * `comet_dist_earth` -- Comet's distance from Earth (AU)
pub fn position_of_parabolic_comet(
    lct_hour: f64,
    lct_min: f64,
    lct_sec: f64,
    is_daylight_saving: bool,
    zone_correction_hours: i32,
    local_date_day: f64,
    local_date_month: u32,
    local_date_year: u32,
    comet_name: String,
) -> (f64, f64, f64, f64, f64, f64, f64) {
    let daylight_saving = if is_daylight_saving == true { 1 } else { 0 };

    let greenwich_date_day = pa_m::lct_gday(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );
    let greenwich_date_month = pa_m::lct_gmonth(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );
    let greenwich_date_year = pa_m::lct_gyear(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );

    let _ut_hours = pa_m::lct_ut(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
    );

    let (comet_info, _comet_info_status) = pa_c::get_comet_info_parabolic_vector(comet_name);

    let perihelion_epoch_day = comet_info.epoch_peri_day;
    let perihelion_epoch_month = comet_info.epoch_peri_month;
    let perihelion_epoch_year = comet_info.epoch_peri_year;
    let q_au = comet_info.peri_dist;
    let inclination_deg = comet_info.incl;
    let perihelion_deg = comet_info.arg_peri;
    let node_deg = comet_info.node;

    let (comet_long_deg, comet_lat_deg, comet_dist_au) = pa_m::p_comet_long_lat_dist(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
        perihelion_epoch_day,
        perihelion_epoch_month,
        perihelion_epoch_year,
        q_au,
        inclination_deg,
        perihelion_deg,
        node_deg,
    );

    let comet_ra_hours = pa_m::dd_dh(pa_m::ec_ra(
        comet_long_deg,
        0.0,
        0.0,
        comet_lat_deg,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    ));
    let comet_dec_deg1 = pa_m::ec_dec(
        comet_long_deg,
        0.0,
        0.0,
        comet_lat_deg,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    );

    let comet_ra_hour = pa_m::dh_hour(comet_ra_hours);
    let comet_ra_min = pa_m::dh_min(comet_ra_hours);
    let comet_ra_sec = pa_m::dh_sec(comet_ra_hours);
    let comet_dec_deg = pa_m::dd_deg(comet_dec_deg1);
    let comet_dec_min = pa_m::dd_min(comet_dec_deg1);
    let comet_dec_sec = pa_m::dd_sec(comet_dec_deg1);
    let comet_dist_earth = pa_u::round_f64(comet_dist_au, 2);

    return (
        comet_ra_hour as f64,
        comet_ra_min as f64,
        comet_ra_sec,
        comet_dec_deg,
        comet_dec_min,
        comet_dec_sec,
        comet_dist_earth,
    );
}