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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use crate::macros as pa_m;
use crate::planetdata as pa_pd;
use crate::util as pa_u;

/// Calculate approximate position of a planet.
///
/// ## Arguments
/// * `lct_hour` -- Local civil time, in hours.
/// * `lct_min` -- Local civil time, in minutes.
/// * `lct_sec` -- Local civil time, in seconds.
/// * `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.
/// * `planet_name` -- Name of planet, e.g., "Jupiter".
///
/// ## Returns
/// * `planet_ra_hour` -- Right ascension of planet (hour part)
/// * `planet_ra_min` -- Right ascension of planet (minutes part)
/// * `planet_ra_sec` -- Right ascension of planet (seconds part)
/// * `planet_dec_deg` -- Declination of planet (degrees part)
/// * `planet_dec_min` -- Declination of planet (minutes part)
/// * `planet_dec_sec` -- Declination of planet (seconds part)
pub fn approximate_position_of_planet(
    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,
    planet_name: String,
) -> (f64, f64, f64, f64, f64, f64) {
    let daylight_saving = if is_daylight_saving == true { 1 } else { 0 };

    let (planet_info, _planet_info_status) = pa_pd::get_planet_info_vector(planet_name);

    let planet_tp_from_table = planet_info.tp;
    let planet_long_from_table = planet_info.long;
    let planet_peri_from_table = planet_info.peri;
    let planet_ecc_from_table = planet_info.ecc;
    let planet_axis_from_table = planet_info.axis;
    let planet_incl_from_table = planet_info.incl;
    let planet_node_from_table = planet_info.node;

    let gdate_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 gdate_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 gdate_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 d_days = pa_m::cd_jd(gdate_day + (ut_hours / 24.0), gdate_month, gdate_year)
        - pa_m::cd_jd(0.0, 1, 2010);
    let np_deg1 = 360.0 * d_days / (365.242191 * planet_tp_from_table);
    let np_deg2 = np_deg1 - 360.0 * (np_deg1 / 360.0).floor();
    let mp_deg = np_deg2 + planet_long_from_table - planet_peri_from_table;
    let lp_deg1 = np_deg2
        + (360.0 * planet_ecc_from_table * mp_deg.to_radians().sin() / std::f64::consts::PI)
        + planet_long_from_table;
    let lp_deg2 = lp_deg1 - 360.0 * (lp_deg1 / 360.0).floor();
    let planet_true_anomaly_deg = lp_deg2 - planet_peri_from_table;
    let r_au = planet_axis_from_table * (1.0 - num::pow(planet_ecc_from_table, 2))
        / (1.0 + planet_ecc_from_table * planet_true_anomaly_deg.to_radians().cos());

    let (earth_info, _earth_info_status) = pa_pd::get_planet_info_vector("Earth".to_string());

    let earth_tp_from_table = earth_info.tp;
    let earth_long_from_table = earth_info.long;
    let earth_peri_from_table = earth_info.peri;
    let earth_ecc_from_table = earth_info.ecc;
    let earth_axis_from_table = earth_info.axis;

    let ne_deg1 = 360.0 * d_days / (365.242191 * earth_tp_from_table);
    let ne_deg2 = ne_deg1 - 360.0 * (ne_deg1 / 360.0).floor();
    let me_deg = ne_deg2 + earth_long_from_table - earth_peri_from_table;
    let le_deg1 = ne_deg2
        + earth_long_from_table
        + 360.0 * earth_ecc_from_table * me_deg.to_radians().sin() / std::f64::consts::PI;
    let le_deg2 = le_deg1 - 360.0 * (le_deg1 / 360.0).floor();
    let earth_true_anomaly_deg = le_deg2 - earth_peri_from_table;
    let r_au2 = earth_axis_from_table * (1.0 - num::pow(earth_ecc_from_table, 2))
        / (1.0 + earth_ecc_from_table * earth_true_anomaly_deg.to_radians().cos());
    let lp_node_rad = (lp_deg2 - planet_node_from_table).to_radians();
    let psi_rad = ((lp_node_rad).sin() * planet_incl_from_table.to_radians().sin()).asin();
    let y = lp_node_rad.sin() * planet_incl_from_table.to_radians().cos();
    let x = lp_node_rad.cos();
    let ld_deg = pa_m::degrees(y.atan2(x)) + planet_node_from_table;
    let rd_au = r_au * psi_rad.cos();
    let le_ld_rad = (le_deg2 - ld_deg).to_radians();
    let atan2_type_1 = (rd_au * le_ld_rad.sin()).atan2(r_au2 - rd_au * le_ld_rad.cos());
    let atan2_type_2 = (r_au2 * (-le_ld_rad).sin()).atan2(rd_au - r_au2 * le_ld_rad.cos());
    let a_rad = if rd_au < 1.0 {
        atan2_type_1
    } else {
        atan2_type_2
    };
    let lamda_deg1 = if rd_au < 1.0 {
        180.0 + le_deg2 + pa_m::degrees(a_rad)
    } else {
        pa_m::degrees(a_rad) + ld_deg
    };
    let lamda_deg2 = lamda_deg1 - 360.0 * (lamda_deg1 / 360.0).floor();
    let beta_deg = pa_m::degrees(
        (rd_au * psi_rad.tan() * ((lamda_deg2 - ld_deg).to_radians()).sin()
            / (r_au2 * (-le_ld_rad).sin()))
        .atan(),
    );
    let ra_hours = pa_m::dd_dh(pa_m::ec_ra(
        lamda_deg2,
        0.0,
        0.0,
        beta_deg,
        0.0,
        0.0,
        gdate_day,
        gdate_month,
        gdate_year,
    ));
    let dec_deg = pa_m::ec_dec(
        lamda_deg2,
        0.0,
        0.0,
        beta_deg,
        0.0,
        0.0,
        gdate_day,
        gdate_month,
        gdate_year,
    );

    let planet_ra_hour = pa_m::dh_hour(ra_hours);
    let planet_ra_min = pa_m::dh_min(ra_hours);
    let planet_ra_sec = pa_m::dh_sec(ra_hours);
    let planet_dec_deg = pa_m::dd_deg(dec_deg);
    let planet_dec_min = pa_m::dd_min(dec_deg);
    let planet_dec_sec = pa_m::dd_sec(dec_deg);

    return (
        planet_ra_hour as f64,
        planet_ra_min as f64,
        planet_ra_sec,
        planet_dec_deg,
        planet_dec_min,
        planet_dec_sec,
    );
}

/// Calculate precise position of a planet.
///
/// ## 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.
/// * `planet_name` -- Name of planet, e.g., "Jupiter".
///
/// ## Returns
/// * `planet_ra_hour` -- Right ascension of planet (hour part)
/// * `planet_ra_min` -- Right ascension of planet (minutes part)
/// * `planet_ra_sec` -- Right ascension of planet (seconds part)
/// * `planet_dec_deg` -- Declination of planet (degrees part)
/// * `planet_dec_min` -- Declination of planet (minutes part)
/// * `planet_dec_sec` -- Declination of planet (seconds part)
pub fn precise_position_of_planet(
    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,
    planet_name: String,
) -> (f64, f64, f64, f64, f64, f64) {
    let daylight_saving = if is_daylight_saving == true { 1 } else { 0 };

    let _gdate_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 _gdate_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 _gdate_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 (
        planet_ecl_long_deg,
        planet_ecl_lat_deg,
        _planet_distance_au,
        _planet_h_long1,
        _planet_h_long2,
        _planet_h_lat,
        _planet_r_vect,
    ) = pa_m::planet_coordinates(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
        planet_name,
    );

    let planet_ra_hours = pa_m::dd_dh(pa_m::ec_ra(
        planet_ecl_long_deg,
        0.0,
        0.0,
        planet_ecl_lat_deg,
        0.0,
        0.0,
        local_date_day,
        local_date_month,
        local_date_year,
    ));
    let planet_dec_deg1 = pa_m::ec_dec(
        planet_ecl_long_deg,
        0.0,
        0.0,
        planet_ecl_lat_deg,
        0.0,
        0.0,
        local_date_day,
        local_date_month,
        local_date_year,
    );

    let planet_ra_hour = pa_m::dh_hour(planet_ra_hours);
    let planet_ra_min = pa_m::dh_min(planet_ra_hours);
    let planet_ra_sec = pa_m::dh_sec(planet_ra_hours);
    let planet_dec_deg = pa_m::dd_deg(planet_dec_deg1);
    let planet_dec_min = pa_m::dd_min(planet_dec_deg1);
    let planet_dec_sec = pa_m::dd_sec(planet_dec_deg1);

    return (
        planet_ra_hour as f64,
        planet_ra_min as f64,
        planet_ra_sec,
        planet_dec_deg,
        planet_dec_min,
        planet_dec_sec,
    );
}

/// Calculate several visual aspects of a planet.
///
/// ## 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.
/// * `planet_name` -- Name of planet, e.g., "Jupiter".
///
/// ## Returns
/// * `distance_au` -- Planet's distance from Earth, in AU.
/// * `ang_dia_arcsec` -- Angular diameter of the planet, in arcseconds.
/// * `phase` -- Illuminated fraction of the planet.
/// * `light_time_hour` -- Light travel time from planet to Earth, hour part.
/// * `light_time_minutes` -- Light travel time from planet to Earth, minutes part.
/// * `light_time_seconds` -- Light travel time from planet to Earth, seconds part.
/// * `pos_angle_bright_limb_deg` -- Position-angle of the bright limb.
/// * `approximate_magnitude` -- Apparent brightness of the planet.
pub fn visual_aspects_of_a_planet(
    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,
    planet_name: String,
) -> (f64, 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 (
        planet_ecl_long_deg,
        planet_ecl_lat_deg,
        planet_dist_au,
        planet_h_long1,
        _temp3,
        _temp4,
        planet_r_vect,
    ) = pa_m::planet_coordinates(
        lct_hour,
        lct_min,
        lct_sec,
        daylight_saving,
        zone_correction_hours,
        local_date_day,
        local_date_month,
        local_date_year,
        planet_name.to_string(),
    );

    let planet_ra_rad = (pa_m::ec_ra(
        planet_ecl_long_deg,
        0.0,
        0.0,
        planet_ecl_lat_deg,
        0.0,
        0.0,
        local_date_day,
        local_date_month,
        local_date_year,
    ))
    .to_radians();
    let planet_dec_rad = (pa_m::ec_dec(
        planet_ecl_long_deg,
        0.0,
        0.0,
        planet_ecl_lat_deg,
        0.0,
        0.0,
        local_date_day,
        local_date_month,
        local_date_year,
    ))
    .to_radians();

    let light_travel_time_hours = planet_dist_au * 0.1386;
    let (planet_info, _planet_info_status) = pa_pd::get_planet_info_vector(planet_name.to_string());
    let angular_diameter_arcsec = planet_info.theta0 / planet_dist_au;
    let phase1 = 0.5 * (1.0 + ((planet_ecl_long_deg - planet_h_long1).to_radians()).cos());

    let sun_ecl_long_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,
    );
    let sun_ra_rad = (pa_m::ec_ra(
        sun_ecl_long_deg,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    ))
    .to_radians();
    let sun_dec_rad = (pa_m::ec_dec(
        sun_ecl_long_deg,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        greenwich_date_day,
        greenwich_date_month,
        greenwich_date_year,
    ))
    .to_radians();

    let y = (sun_dec_rad).cos() * (sun_ra_rad - planet_ra_rad).sin();
    let x = (planet_dec_rad).cos() * (sun_dec_rad).sin()
        - (planet_dec_rad).sin() * (sun_dec_rad).cos() * (sun_ra_rad - planet_ra_rad).cos();

    let chi_deg = pa_m::degrees(y.atan2(x));
    let radius_vector_au = planet_r_vect;
    let approximate_magnitude1 =
        5.0 * (radius_vector_au * planet_dist_au / (phase1).sqrt()).log10() + planet_info.v0;

    let distance_au = pa_u::round_f64(planet_dist_au, 5);
    let ang_dia_arcsec = pa_u::round_f64(angular_diameter_arcsec, 1);
    let phase = pa_u::round_f64(phase1, 2);
    let light_time_hour = pa_m::dh_hour(light_travel_time_hours);
    let light_time_minutes = pa_m::dh_min(light_travel_time_hours);
    let light_time_seconds = pa_m::dh_sec(light_travel_time_hours);
    let pos_angle_bright_limb_deg = pa_u::round_f64(chi_deg, 1);
    let approximate_magnitude = pa_u::round_f64(approximate_magnitude1, 1);

    return (
        distance_au,
        ang_dia_arcsec,
        phase,
        light_time_hour as f64,
        light_time_minutes as f64,
        light_time_seconds,
        pos_angle_bright_limb_deg,
        approximate_magnitude,
    );
}