-
Notifications
You must be signed in to change notification settings - Fork 1
/
astronomy.c
12925 lines (11357 loc) · 466 KB
/
astronomy.c
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Astronomy Engine for C/C++.
https://github.com/cosinekitty/astronomy
MIT License
Copyright (c) 2019-2023 Don Cross <cosinekitty@gmail.com>
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.
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "astronomy.h"
#ifdef __FAST_MATH__
#error Astronomy Engine does not support "fast math" optimization because it causes incorrect behavior. See: https://github.com/cosinekitty/astronomy/issues/245
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** @cond DOXYGEN_SKIP */
#define PI 3.14159265358979323846
#define PLUTO_NUM_STATES 51
#define PLUTO_TIME_STEP 29200
#define PLUTO_DT 146
#define PLUTO_NSTEPS 201
typedef enum
{
FROM_2000,
INTO_2000
}
precess_dir_t;
typedef struct
{
double x;
double y;
double z;
}
terse_vector_t;
typedef struct
{
double tt; /* J2000 terrestrial time [days] */
terse_vector_t r; /* position [au] */
terse_vector_t v; /* velocity [au/day] */
terse_vector_t a; /* acceleration [au/day^2] */
}
body_grav_calc_t;
typedef struct
{
body_grav_calc_t step[PLUTO_NSTEPS];
}
body_segment_t;
typedef struct
{
double tt; /* Terrestrial Time in J2000 days */
terse_vector_t r; /* position [au] */
terse_vector_t v; /* velocity [au/day] */
}
body_state_t;
typedef struct
{
body_state_t Sun;
body_state_t Jupiter;
body_state_t Saturn;
body_state_t Uranus;
body_state_t Neptune;
}
major_bodies_t;
typedef struct
{
astro_time_t time;
body_state_t gravitators[1 + BODY_SUN];
body_grav_calc_t *bodies;
}
gravsim_endpoint_t;
struct astro_grav_sim_s
{
astro_body_t originBody;
int numBodies;
gravsim_endpoint_t endpoint[2];
gravsim_endpoint_t *prev;
gravsim_endpoint_t *curr;
};
typedef struct
{
double ra;
double dec;
double dist;
}
stardef_t;
/* Mean obliquity of the J2000 ecliptic in radians. */
#define OBLIQ_2000 0.40909260059599012
#define COS_OBLIQ_2000 0.9174821430670688
#define SIN_OBLIQ_2000 0.3977769691083922
/** @endcond */
#define NSTARS 8
static stardef_t StarTable[NSTARS];
#define GetStarPointer(body) (((body) >= BODY_STAR1) && ((body) <= BODY_STAR8) ? &StarTable[(body) - BODY_STAR1] : NULL)
static stardef_t *UserDefinedStar(astro_body_t body)
{
stardef_t *star = GetStarPointer(body);
if (star != NULL && star->dist > 0.0)
return star;
return NULL;
}
/**
* @brief Assign equatorial coordinates to a user-defined star.
*
* Some Astronomy Engine functions allow their `body` parameter to
* be a user-defined fixed point in the sky, loosely called a "star".
* This function assigns a right ascension, declination, and distance
* to one of the eight user-defined stars `BODY_STAR1` .. `BODY_STAR8`.
*
* Stars are not valid until defined. Once defined, they retain their
* definition until re-defined by another call to `Astronomy_DefineStar`.
*
* @param body
* One of the eight user-defined star identifiers: `BODY_STAR1` .. `BODY_STAR8`.
*
* @param ra
* The right ascension to be assigned to the star, expressed in J2000 equatorial coordinates (EQJ).
* The value is in units of sidereal hours, and must be within the half-open range [0, 24).
*
* @param dec
* The declination to be assigned to the star, expressed in J2000 equatorial coordinates (EQJ).
* The value is in units of degrees north (positive) or south (negative) of the J2000 equator,
* and must be within the closed range [-90, +90].
*
* @param distanceLightYears
* The distance between the star and the Sun, expressed in light-years.
* This value is used to calculate the tiny parallax shift as seen by an observer on Earth.
* If you don't know the distance to the star, using a large value like 1000 will generally work well.
* The minimum allowed distance is 1 light-year, which is required to provide certain internal optimizations.
*
* @return
* `ASTRO_SUCCESS` indicates the star has been defined. Any other value indicates an error,
* in which case no change has taken place to any of the star definitions.
*/
astro_status_t Astronomy_DefineStar(astro_body_t body, double ra, double dec, double distanceLightYears)
{
stardef_t *star = GetStarPointer(body);
if (star == NULL)
return ASTRO_INVALID_BODY;
if (!isfinite(ra) || ra < 0.0 || ra >= 24.0)
return ASTRO_INVALID_PARAMETER;
if (!isfinite(dec) || dec < -90.0 || dec > +90.0)
return ASTRO_INVALID_PARAMETER;
if (!isfinite(distanceLightYears) || distanceLightYears < 1.0)
return ASTRO_INVALID_PARAMETER;
star->ra = ra;
star->dec = dec;
star->dist = distanceLightYears * AU_PER_LY;
return ASTRO_SUCCESS;
}
static const terse_vector_t VecZero = { 0.0, 0.0, 0.0 };
static terse_vector_t VecAdd(terse_vector_t a, terse_vector_t b)
{
terse_vector_t c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
return c;
}
static void VecIncr(terse_vector_t *target, terse_vector_t source)
{
target->x += source.x;
target->y += source.y;
target->z += source.z;
}
static void VecDecr(terse_vector_t *target, terse_vector_t source)
{
target->x -= source.x;
target->y -= source.y;
target->z -= source.z;
}
static terse_vector_t VecMul(double s, terse_vector_t v)
{
terse_vector_t p;
p.x = s * v.x;
p.y = s * v.y;
p.z = s * v.z;
return p;
}
static void VecScale(terse_vector_t *target, double scalar)
{
target->x *= scalar;
target->y *= scalar;
target->z *= scalar;
}
static terse_vector_t VecRamp(terse_vector_t a, terse_vector_t b, double ramp)
{
terse_vector_t c;
c.x = (1-ramp)*a.x + ramp*b.x;
c.y = (1-ramp)*a.y + ramp*b.y;
c.z = (1-ramp)*a.z + ramp*b.z;
return c;
}
static terse_vector_t VecMean(terse_vector_t a, terse_vector_t b)
{
terse_vector_t c;
c.x = (a.x + b.x) / 2;
c.y = (a.y + b.y) / 2;
c.z = (a.z + b.z) / 2;
return c;
}
static astro_state_vector_t ExportState(body_state_t terse, astro_time_t time)
{
astro_state_vector_t state;
state.status = ASTRO_SUCCESS;
state.x = terse.r.x;
state.y = terse.r.y;
state.z = terse.r.z;
state.vx = terse.v.x;
state.vy = terse.v.y;
state.vz = terse.v.z;
state.t = time;
return state;
}
static astro_state_vector_t ExportGravCalc(body_grav_calc_t calc, astro_time_t time)
{
astro_state_vector_t state;
state.status = ASTRO_SUCCESS;
state.x = calc.r.x;
state.y = calc.r.y;
state.z = calc.r.z;
state.vx = calc.v.x;
state.vy = calc.v.y;
state.vz = calc.v.z;
state.t = time;
return state;
}
static const double DAYS_PER_TROPICAL_YEAR = 365.24217;
static const double ASEC360 = 1296000.0;
static const double ASEC2RAD = 4.848136811095359935899141e-6;
static const double PI2 = 2.0 * PI;
static const double ARC = 3600.0 * 180.0 / PI; /* arcseconds per radian */
static const double SECONDS_PER_DAY = 24.0 * 3600.0;
static const double SOLAR_DAYS_PER_SIDEREAL_DAY = 0.9972695717592592;
static const double MEAN_SYNODIC_MONTH = 29.530588; /* average number of days for Moon to return to the same phase */
static const double EARTH_ORBITAL_PERIOD = 365.256;
static const double NEPTUNE_ORBITAL_PERIOD = 60189.0;
/*
Degrees of refractive "lift" seen for objects near horizon.
More precisely, the angle below the horizon a point has to be, at sea level,
to appear to be exactly on the horizon.
If the ground plane is higher than sea level, this angle
needs to be corrected for decreased atmospheric density.
*/
static const double REFRACTION_NEAR_HORIZON = 34.0 / 60.0;
#define SUN_RADIUS_AU (SUN_RADIUS_KM / KM_PER_AU)
#define EARTH_MEAN_RADIUS_KM 6371.0 /* mean radius of the Earth's geoid, without atmosphere */
#define EARTH_ATMOSPHERE_KM 88.0 /* effective atmosphere thickness for lunar eclipses. see: https://eclipse.gsfc.nasa.gov/LEcat5/shadow.html */
#define EARTH_ECLIPSE_RADIUS_KM (EARTH_MEAN_RADIUS_KM + EARTH_ATMOSPHERE_KM)
#define EARTH_EQUATORIAL_RADIUS_AU (EARTH_EQUATORIAL_RADIUS_KM / KM_PER_AU)
#define MOON_MEAN_RADIUS_KM 1737.4
#define MOON_EQUATORIAL_RADIUS_AU (MOON_EQUATORIAL_RADIUS_KM / KM_PER_AU)
#define MOON_POLAR_RADIUS_AU (MOON_POLAR_RADIUS_KM / KM_PER_AU)
/* The inclination of the moon's rotation axis to the ecliptic plane, in radians. */
#define MOON_AXIS_INCLINATION_RADIANS (DEG2RAD * 1.543)
static const double ASEC180 = 180.0 * 60.0 * 60.0; /* arcseconds per 180 degrees (or pi radians) */
static const double EARTH_MOON_MASS_RATIO = 81.30056;
/*
Masses of the Sun and planets, used for:
(1) Calculating the Solar System Barycenter
(2) Integrating the movement of Pluto
https://web.archive.org/web/20120220062549/http://iau-comm4.jpl.nasa.gov/de405iom/de405iom.pdf
Page 10 in the above document describes the constants used in the DE405 ephemeris.
The following are GM values (gravity constant * mass) in [au^3 / day^2].
This side-steps issues of not knowing the exact values of G and masses M[i];
the products GM[i] are known extremely accurately.
*/
static const double SUN_GM = 0.2959122082855911e-03;
static const double MERCURY_GM = 0.4912547451450812e-10;
static const double VENUS_GM = 0.7243452486162703e-09;
static const double EARTH_GM = 0.8887692390113509e-09;
static const double MARS_GM = 0.9549535105779258e-10;
static const double JUPITER_GM = 0.2825345909524226e-06;
static const double SATURN_GM = 0.8459715185680659e-07;
static const double URANUS_GM = 0.1292024916781969e-07;
static const double NEPTUNE_GM = 0.1524358900784276e-07;
static const double PLUTO_GM = 0.2188699765425970e-11;
#define MOON_GM (EARTH_GM / EARTH_MOON_MASS_RATIO)
/** @cond DOXYGEN_SKIP */
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
#define AU_PER_PARSEC (ASEC180 / PI) /* exact definition of how many AU = one parsec */
#define Y2000_IN_MJD (T0 - MJD_BASIS)
/** @endcond */
static astro_ecliptic_t RotateEquatorialToEcliptic(const double pos[3], double obliq_radians, astro_time_t time);
static int QuadInterp(
double tm, double dt, double fa, double fm, double fb,
double *t, double *df_dt);
static double LongitudeOffset(double diff)
{
double offset = diff;
while (offset <= -180.0)
offset += 360.0;
while (offset > 180.0)
offset -= 360.0;
return offset;
}
static double NormalizeLongitude(double lon)
{
while (lon < 0.0)
lon += 360.0;
while (lon >= 360.0)
lon -= 360.0;
return lon;
}
/**
* @brief Calculates the length of the given vector.
*
* Calculates the non-negative length of the given vector.
* The length is expressed in the same units as the vector's components,
* usually astronomical units (AU).
*
* @param vector The vector whose length is to be calculated.
* @return The length of the vector.
*/
double Astronomy_VectorLength(astro_vector_t vector)
{
return sqrt(vector.x*vector.x + vector.y*vector.y + vector.z*vector.z);
}
/**
* @brief Finds the name of a celestial body.
* @param body The celestial body whose name is to be found.
* @return The English-language name of the celestial body, or "" if the body is not valid.
*/
const char *Astronomy_BodyName(astro_body_t body)
{
switch (body)
{
case BODY_MERCURY: return "Mercury";
case BODY_VENUS: return "Venus";
case BODY_EARTH: return "Earth";
case BODY_MARS: return "Mars";
case BODY_JUPITER: return "Jupiter";
case BODY_SATURN: return "Saturn";
case BODY_URANUS: return "Uranus";
case BODY_NEPTUNE: return "Neptune";
case BODY_PLUTO: return "Pluto";
case BODY_SUN: return "Sun";
case BODY_MOON: return "Moon";
case BODY_EMB: return "EMB";
case BODY_SSB: return "SSB";
default: return "";
}
}
/**
* @brief Returns the #astro_body_t value corresponding to the given English name.
* @param name One of the following strings: Sun, Moon, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, EMB, SSB.
* @return If `name` is one of the listed strings (case-sensitive), the returned value is the corresponding #astro_body_t value, otherwise it is `BODY_INVALID`.
*/
astro_body_t Astronomy_BodyCode(const char *name)
{
if (name != NULL)
{
if (!strcmp(name, "Mercury")) return BODY_MERCURY;
if (!strcmp(name, "Venus")) return BODY_VENUS;
if (!strcmp(name, "Earth")) return BODY_EARTH;
if (!strcmp(name, "Mars")) return BODY_MARS;
if (!strcmp(name, "Jupiter")) return BODY_JUPITER;
if (!strcmp(name, "Saturn")) return BODY_SATURN;
if (!strcmp(name, "Uranus")) return BODY_URANUS;
if (!strcmp(name, "Neptune")) return BODY_NEPTUNE;
if (!strcmp(name, "Pluto")) return BODY_PLUTO;
if (!strcmp(name, "Sun")) return BODY_SUN;
if (!strcmp(name, "Moon")) return BODY_MOON;
if (!strcmp(name, "EMB")) return BODY_EMB;
if (!strcmp(name, "SSB")) return BODY_SSB;
}
return BODY_INVALID;
}
/**
* @brief Returns 1 for planets that are farther from the Sun than the Earth is, 0 otherwise.
*/
static int IsSuperiorPlanet(astro_body_t body)
{
switch (body)
{
case BODY_MARS:
case BODY_JUPITER:
case BODY_SATURN:
case BODY_URANUS:
case BODY_NEPTUNE:
case BODY_PLUTO:
return 1;
default:
return 0;
}
}
/**
* @brief Returns the average number of days it takes for a planet to orbit the Sun.
* @param body One of the planets: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, or Pluto.
* @return The mean orbital period of the body, or 0.0 if the `body` parameter is not valid.
*/
double Astronomy_PlanetOrbitalPeriod(astro_body_t body)
{
switch (body)
{
case BODY_MERCURY: return 87.969;
case BODY_VENUS: return 224.701;
case BODY_EARTH: return EARTH_ORBITAL_PERIOD;
case BODY_MARS: return 686.980;
case BODY_JUPITER: return 4332.589;
case BODY_SATURN: return 10759.22;
case BODY_URANUS: return 30685.4;
case BODY_NEPTUNE: return NEPTUNE_ORBITAL_PERIOD;
case BODY_PLUTO: return 90560.0;
default: return 0.0; /* invalid body */
}
}
static astro_vector_t VecError(astro_status_t status, astro_time_t time)
{
astro_vector_t vec;
vec.x = vec.y = vec.z = NAN;
vec.t = time;
vec.status = status;
return vec;
}
static astro_state_vector_t StateVecError(astro_status_t status, astro_time_t time)
{
astro_state_vector_t vec;
vec.x = vec.y = vec.z = NAN;
vec.vx = vec.vy = vec.vz = NAN;
vec.t = time;
vec.status = status;
return vec;
}
static astro_spherical_t SphereError(astro_status_t status)
{
astro_spherical_t sphere;
sphere.status = status;
sphere.dist = sphere.lat = sphere.lon = NAN;
return sphere;
}
static astro_time_t TimeError(void)
{
astro_time_t time;
time.tt = time.ut = time.eps = time.psi = time.st = NAN;
return time;
}
static astro_equatorial_t EquError(astro_status_t status)
{
astro_equatorial_t equ;
equ.vec = VecError(status, TimeError());
equ.ra = equ.dec = equ.dist = NAN;
equ.status = status;
return equ;
}
static astro_ecliptic_t EclError(astro_status_t status)
{
astro_ecliptic_t ecl;
ecl.status = status;
ecl.vec = VecError(status, TimeError());
return ecl;
}
static astro_angle_result_t AngleError(astro_status_t status)
{
astro_angle_result_t result;
result.status = status;
result.angle = NAN;
return result;
}
static astro_func_result_t FuncError(astro_status_t status)
{
astro_func_result_t result;
result.status = status;
result.value = NAN;
return result;
}
static astro_rotation_t RotationErr(astro_status_t status)
{
astro_rotation_t rotation;
int i, j;
rotation.status = status;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
rotation.rot[i][j] = NAN;
return rotation;
}
static astro_moon_quarter_t MoonQuarterError(astro_status_t status)
{
astro_moon_quarter_t result;
result.status = status;
result.quarter = -1;
result.time = TimeError();
return result;
}
static astro_elongation_t ElongError(astro_status_t status)
{
astro_elongation_t result;
result.status = status;
result.elongation = NAN;
result.ecliptic_separation = NAN;
result.time = TimeError();
result.visibility = (astro_visibility_t)(-1);
return result;
}
static astro_hour_angle_t HourAngleError(astro_status_t status)
{
astro_hour_angle_t result;
result.status = status;
result.time = TimeError();
result.hor.altitude = result.hor.azimuth = result.hor.dec = result.hor.ra = NAN;
return result;
}
static astro_illum_t IllumError(astro_status_t status)
{
astro_illum_t result;
result.status = status;
result.time = TimeError();
result.mag = NAN;
result.phase_angle = NAN;
result.phase_fraction = NAN;
result.helio_dist = NAN;
result.ring_tilt = NAN;
return result;
}
static astro_apsis_t ApsisError(astro_status_t status)
{
astro_apsis_t result;
result.status = status;
result.time = TimeError();
result.kind = APSIS_INVALID;
result.dist_km = result.dist_au = NAN;
return result;
}
static astro_search_result_t SearchError(astro_status_t status)
{
astro_search_result_t result;
result.time = TimeError();
result.status = status;
return result;
}
static astro_constellation_t ConstelErr(astro_status_t status)
{
astro_constellation_t constel;
constel.status = status;
constel.symbol = constel.name = NULL;
constel.ra_1875 = constel.dec_1875 = NAN;
return constel;
}
static astro_transit_t TransitErr(astro_status_t status)
{
astro_transit_t transit;
transit.status = status;
transit.start = transit.peak = transit.finish = TimeError();
transit.separation = NAN;
return transit;
}
static astro_axis_t AxisErr(astro_status_t status)
{
astro_axis_t axis;
axis.status = status;
axis.ra = axis.dec = NAN;
return axis;
}
static astro_func_result_t SynodicPeriod(astro_body_t body)
{
double Tp; /* planet's orbital period in days */
astro_func_result_t result;
/* The Earth does not have a synodic period as seen from itself. */
if (body == BODY_EARTH)
return FuncError(ASTRO_EARTH_NOT_ALLOWED);
if (body == BODY_MOON)
{
result.status = ASTRO_SUCCESS;
result.value = MEAN_SYNODIC_MONTH;
return result;
}
Tp = Astronomy_PlanetOrbitalPeriod(body);
if (Tp <= 0.0)
return FuncError(ASTRO_INVALID_BODY);
result.status = ASTRO_SUCCESS;
result.value = fabs(EARTH_ORBITAL_PERIOD / (EARTH_ORBITAL_PERIOD/Tp - 1.0));
return result;
}
/**
* @brief Calculates the angle between two vectors.
*
* Given a pair of vectors, this function returns the angle in degrees
* between the two vectors in 3D space.
* The angle is measured in the plane that contains both vectors.
*
* @param a
* The first vector.
*
* @param b
* The second vector.
*
* @returns
* On success, the `status` field holds `ASTRO_SUCCESS` and `angle` holds
* a number of degrees in the range [0, 180].
* If either vector has a zero magnitude or contains NAN (not a number)
* components, the `status` will hold the error code `ASTRO_BAD_VECTOR`.
*/
astro_angle_result_t Astronomy_AngleBetween(astro_vector_t a, astro_vector_t b)
{
double r, dot;
astro_angle_result_t result;
r = Astronomy_VectorLength(a) * Astronomy_VectorLength(b);
if (r < 1.0e-8 || !isfinite(r))
return AngleError(ASTRO_BAD_VECTOR);
dot = (a.x*b.x + a.y*b.y + a.z*b.z) / r;
if (dot <= -1.0)
result.angle = 180.0;
else if (dot >= +1.0)
result.angle = 0.0;
else
result.angle = RAD2DEG * acos(dot);
result.status = ASTRO_SUCCESS;
return result;
}
/**
* @brief The default Delta T function used by Astronomy Engine.
*
* Espenak and Meeus use a series of piecewise polynomials to
* approximate DeltaT of the Earth in their "Five Millennium Canon of Solar Eclipses".
* See: https://eclipse.gsfc.nasa.gov/SEhelp/deltatpoly2004.html
* This is the default Delta T function used by Astronomy Engine.
*
* @param ut
* The floating point number of days since noon UTC on January 1, 2000.
*
* @returns
* The estimated difference TT-UT on the given date, expressed in seconds.
*/
double Astronomy_DeltaT_EspenakMeeus(double ut)
{
double y, u, u2, u3, u4, u5, u6, u7;
/*
Fred Espenak writes about Delta-T generically here:
https://eclipse.gsfc.nasa.gov/SEhelp/deltaT.html
https://eclipse.gsfc.nasa.gov/SEhelp/deltat2004.html
He provides polynomial approximations for distant years here:
https://eclipse.gsfc.nasa.gov/SEhelp/deltatpoly2004.html
They start with a year value 'y' such that y=2000 corresponds
to the UTC Date 15-January-2000. Convert difference in days
to mean tropical years.
*/
y = 2000 + ((ut - 14) / DAYS_PER_TROPICAL_YEAR);
if (y < -500)
{
u = (y - 1820) / 100;
return -20 + (32 * u*u);
}
if (y < 500)
{
u = y / 100;
u2 = u*u; u3 = u*u2; u4 = u2*u2; u5 = u2*u3; u6 = u3*u3;
return 10583.6 - 1014.41*u + 33.78311*u2 - 5.952053*u3 - 0.1798452*u4 + 0.022174192*u5 + 0.0090316521*u6;
}
if (y < 1600)
{
u = (y - 1000) / 100;
u2 = u*u; u3 = u*u2; u4 = u2*u2; u5 = u2*u3; u6 = u3*u3;
return 1574.2 - 556.01*u + 71.23472*u2 + 0.319781*u3 - 0.8503463*u4 - 0.005050998*u5 + 0.0083572073*u6;
}
if (y < 1700)
{
u = y - 1600;
u2 = u*u; u3 = u*u2;
return 120 - 0.9808*u - 0.01532*u2 + u3/7129.0;
}
if (y < 1800)
{
u = y - 1700;
u2 = u*u; u3 = u*u2; u4 = u2*u2;
return 8.83 + 0.1603*u - 0.0059285*u2 + 0.00013336*u3 - u4/1174000;
}
if (y < 1860)
{
u = y - 1800;
u2 = u*u; u3 = u*u2; u4 = u2*u2; u5 = u2*u3; u6 = u3*u3; u7 = u3*u4;
return 13.72 - 0.332447*u + 0.0068612*u2 + 0.0041116*u3 - 0.00037436*u4 + 0.0000121272*u5 - 0.0000001699*u6 + 0.000000000875*u7;
}
if (y < 1900)
{
u = y - 1860;
u2 = u*u; u3 = u*u2; u4 = u2*u2; u5 = u2*u3;
return 7.62 + 0.5737*u - 0.251754*u2 + 0.01680668*u3 - 0.0004473624*u4 + u5/233174;
}
if (y < 1920)
{
u = y - 1900;
u2 = u*u; u3 = u*u2; u4 = u2*u2;
return -2.79 + 1.494119*u - 0.0598939*u2 + 0.0061966*u3 - 0.000197*u4;
}
if (y < 1941)
{
u = y - 1920;
u2 = u*u; u3 = u*u2;
return 21.20 + 0.84493*u - 0.076100*u2 + 0.0020936*u3;
}
if (y < 1961)
{
u = y - 1950;
u2 = u*u; u3 = u*u2;
return 29.07 + 0.407*u - u2/233 + u3/2547;
}
if (y < 1986)
{
u = y - 1975;
u2 = u*u; u3 = u*u2;
return 45.45 + 1.067*u - u2/260 - u3/718;
}
if (y < 2005)
{
u = y - 2000;
u2 = u*u; u3 = u*u2; u4 = u2*u2; u5 = u2*u3;
return 63.86 + 0.3345*u - 0.060374*u2 + 0.0017275*u3 + 0.000651814*u4 + 0.00002373599*u5;
}
if (y < 2050)
{
u = y - 2000;
return 62.92 + 0.32217*u + 0.005589*u*u;
}
if (y < 2150)
{
u = (y-1820)/100;
return -20 + 32*u*u - 0.5628*(2150 - y);
}
/* all years after 2150 */
u = (y - 1820) / 100;
return -20 + (32 * u*u);
}
/**
* @brief A Delta T function that approximates the one used by the JPL Horizons tool.
*
* In order to support unit tests based on data generated by the JPL Horizons online
* tool, I had to reverse engineer their Delta T function by generating a table that
* contained it. The main difference between their tool and the Espenak/Meeus function
* is that they stop extrapolating the Earth's deceleration after the year 2017.
*
* @param ut
* The floating point number of days since noon UTC on January 1, 2000.
*
* @returns
* The estimated difference TT-UT on the given date, expressed in seconds.
*/
double Astronomy_DeltaT_JplHorizons(double ut)
{
if (ut > 17.0 * DAYS_PER_TROPICAL_YEAR)
ut = 17.0 * DAYS_PER_TROPICAL_YEAR;
return Astronomy_DeltaT_EspenakMeeus(ut);
}
static astro_deltat_func DeltaTFunc = Astronomy_DeltaT_EspenakMeeus;
/**
* @brief Changes the function Astronomy Engine uses to calculate Delta T.
*
* Most programs should not call this function. It is for advanced use cases only.
* By default, Astronomy Engine uses the function #Astronomy_DeltaT_EspenakMeeus
* to estimate changes in the Earth's rotation rate over time.
* However, for the sake of unit tests that compare calculations against
* external data sources that use alternative models for Delta T,
* it is sometimes useful to replace the Delta T model to match.
* This function allows replacing the Delta T model with any other
* desired model.
*
* @param func
* A pointer to a function to convert UT values to DeltaT values.
*/
void Astronomy_SetDeltaTFunction(astro_deltat_func func)
{
DeltaTFunc = func;
}
static double TerrestrialTime(double ut)
{
return ut + DeltaTFunc(ut)/86400.0;
}
/**
* @brief Converts a J2000 day value to an #astro_time_t value.
*
* This function can be useful for reproducing an #astro_time_t structure
* from its `ut` field only.
*
* @param ut
* The floating point number of days since noon UTC on January 1, 2000.
* This time is based on UTC/UT1 civil time.
* See #Astronomy_TerrestrialTime if you instead want to create
* a time value based on atomic Terrestrial Time (TT).
*
* @returns
* An #astro_time_t value for the given `ut` value.
*/
astro_time_t Astronomy_TimeFromDays(double ut)
{
astro_time_t time;
time.ut = ut;
time.tt = TerrestrialTime(ut);
time.psi = time.eps = time.st = NAN;
return time;
}
/**
* @brief Converts a terrestrial time value into an #astro_time_t value.
*
* This function can be used in rare cases where a time must be based
* on Terrestrial Time (TT) rather than Universal Time (UT).
* Most developers will want to call #Astronomy_TimeFromDays instead of
* this function, because usually time is based on civil time adjusted
* by leap seconds to match the Earth's rotation, rather than the uniformly
* flowing TT used to calculate solar system dynamics. In rare cases
* where the caller already knows TT, this function is provided to create
* an #astro_time_t value that can be passed to Astronomy Engine functions.
*
* @param tt
* The floating point number of days of uniformly flowing
* Terrestrial Time since the J2000 epoch.
*
* @returns
* An #astro_time_t value for the given `tt` value.
*/
astro_time_t Astronomy_TerrestrialTime(double tt)
{
/* Iterate to solve to find the correct ut for a given tt, and create an astro_time_t for that time. */
astro_time_t time = Astronomy_TimeFromDays(tt);
for(;;)
{
double err = tt - time.tt;
if (fabs(err) < 1.0e-12)
return time;
time = Astronomy_AddDays(time, err);
}
}
/**
* @brief Returns the computer's current date and time in the form of an #astro_time_t.
*
* Uses the computer's system clock to find the current UTC date and time with 1-second granularity.
* Converts that date and time to an #astro_time_t value and returns the result.
* Callers can pass this value to other Astronomy Engine functions to calculate
* current observational conditions.
*/
astro_time_t Astronomy_CurrentTime(void)
{
astro_time_t t;
/* Get seconds since midnight January 1, 1970, divide to convert to days, */
/* then subtract to get days since noon on January 1, 2000. */
t.ut = (time(NULL) / SECONDS_PER_DAY) - 10957.5;
t.tt = TerrestrialTime(t.ut);
t.psi = t.eps = t.st = NAN;
return t;
}
/**
* @brief Creates an #astro_time_t value from a given calendar date and time.
*
* Given a UTC calendar date and time, calculates an #astro_time_t value that can
* be passed to other Astronomy Engine functions for performing various calculations
* relating to that date and time.
*
* It is the caller's responsibility to ensure that the parameter values are correct.
* The parameters are not checked for validity,