forked from aloistr/swisseph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweph.c
8613 lines (8479 loc) · 284 KB
/
sweph.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
/* SWISSEPH
Ephemeris computations
Authors: Dieter Koch and Alois Treindl, Astrodienst Zurich
**************************************************************/
/* Copyright (C) 1997 - 2021 Astrodienst AG, Switzerland. All rights reserved.
License conditions
------------------
This file is part of Swiss Ephemeris.
Swiss Ephemeris is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing.
Swiss Ephemeris is made available by its authors under a dual licensing
system. The software developer, who uses any part of Swiss Ephemeris
in his or her software, must choose between one of the two license models,
which are
a) GNU Affero General Public License (AGPL)
b) Swiss Ephemeris Professional License
The choice must be made before the software developer distributes software
containing parts of Swiss Ephemeris to others, and before any public
service using the developed software is activated.
If the developer choses the AGPL software license, he or she must fulfill
the conditions of that license, which includes the obligation to place his
or her whole software project under the AGPL or a compatible license.
See https://www.gnu.org/licenses/agpl-3.0.html
If the developer choses the Swiss Ephemeris Professional license,
he must follow the instructions as found in https://www.astro.com/swisseph/
and purchase the Swiss Ephemeris Professional Edition from Astrodienst
and sign the corresponding license contract.
The License grants you the right to use, copy, modify and redistribute
Swiss Ephemeris, but only under certain conditions described in the License.
Among other things, the License requires that the copyright notices and
this notice be preserved on all copies.
Authors of the Swiss Ephemeris: Dieter Koch and Alois Treindl
The authors of Swiss Ephemeris have no control or influence over any of
the derived works, i.e. over software or services created by other
programmers which use Swiss Ephemeris functions.
The names of the authors or of the copyright holder (Astrodienst) must not
be used for promoting any software, product or service which uses or contains
the Swiss Ephemeris. This copyright notice is the ONLY place where the
names of the authors can legally appear, except in cases where they have
given special permission in writing.
The trademarks 'Swiss Ephemeris' and 'Swiss Ephemeris inside' may be used
for promoting such software, products or services.
*/
#include <string.h>
#include <ctype.h>
#if MSDOS
#include <tchar.h>
#include <windows.h>
#endif
#include "swejpl.h"
#include "swephexp.h"
#include "sweph.h"
#include "swephlib.h"
#ifdef _MSC_VER
#define CMP_CALL_CONV __cdecl
#else
#define CMP_CALL_CONV
#endif
#define IS_PLANET 0
#define IS_MOON 1
#define IS_ANY_BODY 2
#define IS_MAIN_ASTEROID 3
#define DO_SAVE TRUE
#define NO_SAVE FALSE
#define SEFLG_EPHMASK (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH)
#define SEFLG_COORDSYS (SEFLG_EQUATORIAL | SEFLG_XYZ | SEFLG_RADIANS)
struct meff_ele {double r,m;};
/****************
* global stuff *
****************/
TLS struct swe_data swed = {FALSE, /* ephe_path_is_set = FALSE */
FALSE, /* jpl_file_is_open = FALSE */
NULL, /* fixfp, fixed stars file pointer */
"", /* ephepath, ephemeris path */
"", /* jplfnam, JPL file name, default */
0, /* jpldenum */
0, /* last_epheflag */
FALSE, /* geopos_is_set, for topocentric */
FALSE, /* ayana_is_set, ayanamsa is set */
FALSE, /* is_old_starfile, fixstars.cat is used (default is sefstars.txt) */
0.0, 0.0, 0.0, 0.0, /* eop_tjd_... */
0, /* eop_dpsi_loaded */
0.0, /* tid_acc */
FALSE, /* is_tid_acc_manual */
FALSE, /* init_dt_done */
FALSE, /* swed_is_initialised */
FALSE, /* delta_t_userdef_is_set */
0.0, /* delta_t_userdef */
0.0, /* ast_G */
0.0, /* ast_H */
0.0, /* ast_diam */
"", /* astelem[] */
0, /* i_saved_planet_name */
"", /* saved_planet_name[] */
NULL, /* dpsi */
NULL, /* deps */
0, /* timeout */
{0,0,0,0,0,0,0,0,}, /* astro_models */
};
/*************
* constants *
*************/
static const char *ayanamsa_name[] = {
"Fagan/Bradley", /* 0 SE_SIDM_FAGAN_BRADLEY */
"Lahiri", /* 1 SE_SIDM_LAHIRI */
"De Luce", /* 2 SE_SIDM_DELUCE */
"Raman", /* 3 SE_SIDM_RAMAN */
"Usha/Shashi", /* 4 SE_SIDM_USHASHASHI */
"Krishnamurti", /* 5 SE_SIDM_KRISHNAMURTI */
"Djwhal Khul", /* 6 SE_SIDM_DJWHAL_KHUL */
"Yukteshwar", /* 7 SE_SIDM_YUKTESHWAR */
"J.N. Bhasin", /* 8 SE_SIDM_JN_BHASIN */
"Babylonian/Kugler 1", /* 9 SE_SIDM_BABYL_KUGLER1 */
"Babylonian/Kugler 2", /* 10 SE_SIDM_BABYL_KUGLER2 */
"Babylonian/Kugler 3", /* 11 SE_SIDM_BABYL_KUGLER3 */
"Babylonian/Huber", /* 12 SE_SIDM_BABYL_HUBER */
"Babylonian/Eta Piscium", /* 13 SE_SIDM_BABYL_ETPSC */
"Babylonian/Aldebaran = 15 Tau", /* 14 SE_SIDM_ALDEBARAN_15TAU */
"Hipparchos", /* 15 SE_SIDM_HIPPARCHOS */
"Sassanian", /* 16 SE_SIDM_SASSANIAN */
"Galact. Center = 0 Sag", /* 17 SE_SIDM_GALCENT_0SAG */
"J2000", /* 18 SE_SIDM_J2000 */
"J1900", /* 19 SE_SIDM_J1900 */
"B1950", /* 20 SE_SIDM_B1950 */
"Suryasiddhanta", /* 21 SE_SIDM_SURYASIDDHANTA */
"Suryasiddhanta, mean Sun", /* 22 SE_SIDM_SURYASIDDHANTA_MSUN */
"Aryabhata", /* 23 SE_SIDM_ARYABHATA */
"Aryabhata, mean Sun", /* 24 SE_SIDM_ARYABHATA_MSUN */
"SS Revati", /* 25 SE_SIDM_SS_REVATI */
"SS Citra", /* 26 SE_SIDM_SS_CITRA */
"True Citra", /* 27 SE_SIDM_TRUE_CITRA */
"True Revati", /* 28 SE_SIDM_TRUE_REVATI */
"True Pushya (PVRN Rao)", /* 29 SE_SIDM_TRUE_PUSHYA */
"Galactic Center (Gil Brand)", /* 30 SE_SIDM_GALCENT_RGILBRAND */
"Galactic Equator (IAU1958)", /* 31 SE_SIDM_GALEQU_IAU1958 */
"Galactic Equator", /* 32 SE_SIDM_GALEQU_TRUE */
"Galactic Equator mid-Mula", /* 33 SE_SIDM_GALEQU_MULA */
"Skydram (Mardyks)", /* 34 SE_SIDM_GALALIGN_MARDYKS */
"True Mula (Chandra Hari)", /* 35 SE_SIDM_TRUE_MULA */
"Dhruva/Gal.Center/Mula (Wilhelm)", /* 36 SE_SIDM_GALCENT_MULA_WILHELM */
"Aryabhata 522", /* 37 SE_SIDM_ARYABHATA_522 */
"Babylonian/Britton", /* 38 SE_SIDM_BABYL_BRITTON */
"\"Vedic\"/Sheoran", /* 39 SE_SIDM_TRUE_SHEORAN */
"Cochrane (Gal.Center = 0 Cap)", /* 40 SE_SIDM_GALCENT_COCHRANE */
"Galactic Equator (Fiorenza)", /* 41 SE_SIDM_GALEQU_FIORENZA */
"Vettius Valens", /* 42 SE_SIDM_VALENS_MOON */
"Lahiri 1940", /* 43 SE_SIDM_LAHIRI_1940 */
"Lahiri VP285", /* 44 SE_SIDM_LAHIRI_VP285 */
"Krishnamurti-Senthilathiban", /* 45 SE_SIDM_KRISHNAMURTI_VP291 */
"Lahiri ICRC", /* 46 SE_SIDM_LAHIRI_ICRC */
/*"Manjula/Laghumanasa",*/
};
static const int pnoint2jpl[] = PNOINT2JPL;
static const int pnoext2int[] = {SEI_SUN, SEI_MOON, SEI_MERCURY, SEI_VENUS, SEI_MARS, SEI_JUPITER, SEI_SATURN, SEI_URANUS, SEI_NEPTUNE, SEI_PLUTO, 0, 0, 0, 0, SEI_EARTH, SEI_CHIRON, SEI_PHOLUS, SEI_CERES, SEI_PALLAS, SEI_JUNO, SEI_VESTA, };
static int32 swecalc(double tjd, int ipl, int iplmoon, int32 iflag, double *x, char *serr);
static int do_fread(void *targ, int size, int count, int corrsize,
FILE *fp, int32 fpos, int freord, int fendian, int ifno,
char *serr);
static int get_new_segment(double tjd, int ipli, int ifno, char *serr);
static int main_planet(double tjd, int ipli, int iplmoon, int32 epheflag, int32 iflag,
char *serr);
static int main_planet_bary(double tjd, int ipli, int32 epheflag, int32 iflag,
AS_BOOL do_save,
double *xp, double *xe, double *xs, double *xm,
char *serr);
static int sweplan(double tjd, int ipli, int ifno, int32 iflag, AS_BOOL do_save,
double *xp, double *xpe, double *xps, double *xpm,
char *serr);
static int swemoon(double tjd, int32 iflag, AS_BOOL do_save, double *xp, char *serr);
static int sweph(double tjd, int ipli, int ifno, int32 iflag, double *xsunb, AS_BOOL do_save,
double *xp, char *serr);
static int jplplan(double tjd, int ipli, int32 iflag, AS_BOOL do_save,
double *xp, double *xpe, double *xps, char *serr);
static void rot_back(int ipl);
static int read_const(int ifno, char *serr);
static void embofs(double *xemb, double *xmoon);
static int app_pos_etc_plan(int ipli, int iplmoon, int32 iflag, char *serr);
static int app_pos_etc_plan_osc(int ipl, int ipli, int32 iflag, char *serr);
static int app_pos_etc_sun(int32 iflag, char *serr);
static int app_pos_etc_moon(int32 iflag, char *serr);
static int app_pos_etc_sbar(int32 iflag, char *serr);
extern int swi_plan_for_osc_elem(int32 iflag, double tjd, double *xx);
static void swi_close_keep_topo_etc(void);
static int app_pos_etc_mean(int ipl, int32 iflag, char *serr);
static void nut_matrix(struct nut *nu, struct epsilon *oec);
static void calc_epsilon(double tjd, int32 iflag, struct epsilon *e);
static int lunar_osc_elem(double tjd, int ipl, int32 iflag, char *serr);
static int intp_apsides(double tjd, int ipl, int32 iflag, char *serr);
static double meff(double r);
static void denormalize_positions(double *x0, double *x1, double *x2);
static void calc_speed(double *x0, double *x1, double *x2, double dt);
static int32 plaus_iflag(int32 iflag, int32 ipl, double tjd, char *serr);
static int app_pos_rest(struct plan_data *pdp, int32 iflag,
double *xx, double *x2000, struct epsilon *oe, char *serr);
static int open_jpl_file(double *ss, char *fname, char *fpath, char *serr);
static void free_planets(void);
#ifdef TRACE
static void trace_swe_calc(int param, double tjd, int ipl, int32 iflag, double *xx, char *serr);
static void trace_swe_fixstar(int swtch, char *star, double tjd, int32 iflag, double *xx, char *serr);
static void trace_swe_get_planet_name(int swtch, int ipl, char *s);
#endif
char *CALL_CONV swe_version(char *s)
{
strcpy(s, SE_VERSION);
return s;
}
#ifndef NO_SWE_GLP // -DNO_SWE_GLP to suppress this function
#if MSDOS
HANDLE dllhandle = NULL; // global used in swe_version
// if DLL, set by DllMain()
#else
#ifdef __GNUC__
// The following define is actually forbidden.
// It would be better to compile with -D_GNU_SOURCE.
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <dlfcn.h> // must be linked with -ldl
static Dl_info dli;
#endif
#endif // MSDOS
char *CALL_CONV swe_get_library_path(char *s)
{
size_t bytes;
size_t len;
*s = '\0';
#if !defined(__APPLE)
len = AS_MAXCH;
bytes = 0;
#if MSDOS
bytes = GetModuleFileName((HMODULE) dllhandle, (TCHAR*) s, (DWORD) len);
#else
#ifdef __GNUC__
if (dladdr((void *)swe_version, &dli) != 0) {
if (strlen(dli.dli_fname) >= len) {
strncpy(s, dli.dli_fname, len);
s[len] = '\0';
} else{
strcpy(s, dli.dli_fname);
}
bytes = strlen(s);
} else {
bytes = readlink("/proc/self/exe", s, len);
}
#else
bytes = readlink("/proc/self/exe", s, len);
#endif
#endif
s[bytes] = '\0';
#endif
return s;
}
#else // NO_SWE_GLP
// we need this function because swetest requires it
char *CALL_CONV swe_get_library_path(char *s)
{
*s = '\0';
return s;
}
#endif // NO_SWE_GLP
/* The routine called by the user.
* It checks whether a position for the same planet, the same t, and the
* same flag bits has already been computed.
* If yes, this position is returned. Otherwise it is computed.
* -> If the SEFLG_SPEED flag has been specified, the speed will be returned
* at offset 3 of position array x[]. Its precision is probably better
* than 0.002"/day.
* -> If the SEFLG_SPEED3 flag has been specified, the speed will be computed
* from three positions. This speed is less accurate than SEFLG_SPEED,
* i.e. better than 0.1"/day. And it is much slower. It is used for
* program tests only.
* -> If no speed flag has been specified, no speed will be returned.
*/
int32 CALL_CONV swe_calc(double tjd, int ipl, int32 iflag,
double *xx, char *serr)
{
int i, j;
int32 iplmoon = 0, iflgsave = iflag;
int32 epheflag;
AS_BOOL use_speed3 = FALSE;
struct save_positions *sd;
double x[6], *xs, x0[24], x2[24];
double dt;
if (serr != NULL)
*serr = '\0';
#ifdef TRACE
#ifdef FORCE_IFLAG
/*
* If this source file is compiled with /DFORCE_IFLAG or -DFORCE_IFLAG
* and also with TRACE, then the actual value of iflag used in swe_calc()
* can be manipulated from the outside of an application:
* Create a text file 'force.flg' and put one text line into it
* containing a number, e.g. 1024
* This number will be or'ed into the iflag used by the caller of swe_calc()
*
* See the code below for the details.
* This is not an important mechanism. We used it to debug an application
* which showed strange behaviour, by compiling a special DLL with TRACE and
* FORCE_IFLAG and then running the application with this DLL (we had no
* source code of the application itself).
*/
static TLS int force_flag = 0;
static TLS int32 iflag_forced = 0;
static TLS int force_flag_checked = 0;
FILE *fp;
char s[AS_MAXCH], *sp;
memset(x, 0, sizeof(double) * 6);
/* if the following file exists, flag is read from it and or'ed into iflag */
if (!force_flag_checked) {
if ((fp = fopen(fname_force_flg, BFILE_R_ACCESS)) != NULL) {
force_flag = 1;
fgets(s, AS_MAXCH, fp);
if ((sp = strchr(s, '\n')) != NULL)
*sp = '\0';
iflag_forced = atol(s);
fclose(fp);
}
force_flag_checked = 1;
}
if (force_flag)
iflag |= iflag_forced;
#endif
swi_open_trace(serr);
trace_swe_calc(1, tjd, ipl, iflag, xx, NULL);
#endif /* TRACE */
/* function calls for Pluto with asteroid number 134340
* are treated as calls for Pluto as main body SE_PLUTO.
* Reason: Our numerical integrator takes into account Pluto
* perturbation and therefore crashes with body 134340 Pluto. */
if (ipl == SE_AST_OFFSET + 134340)
ipl = SE_PLUTO;
/* if ephemeris flag != ephemeris flag of last call,
* we clear the save area, to prevent swecalc() using
* previously computed data for current calculation.
* except with ipl = SE_ECL_NUT which is not dependent
* on ephemeris, and except if change is from
* ephemeris = 0 to ephemeris = SEFLG_DEFAULTEPH
* or vice-versa.
*/
epheflag = iflag & SEFLG_EPHMASK;
if (epheflag & SEFLG_MOSEPH) {
epheflag = SEFLG_MOSEPH;
} else if (epheflag & SEFLG_JPLEPH) {
epheflag = SEFLG_JPLEPH;
} else {
epheflag = SEFLG_SWIEPH;
}
if (swi_init_swed_if_start() == 1 && !(epheflag & SEFLG_MOSEPH) && serr != NULL) {
strcpy(serr, "Please call swe_set_ephe_path() or swe_set_jplfile() before calling swe_calc() or swe_calc_ut()");
}
if (swed.last_epheflag != epheflag) {
free_planets();
/* close and free ephemeris files */
if (ipl != SE_ECL_NUT) { /* because file will not be reopened with this ipl */
if (swed.jpl_file_is_open) {
swi_close_jpl_file();
swed.jpl_file_is_open = FALSE;
}
for (i = 0; i < SEI_NEPHFILES; i ++) {
if (swed.fidat[i].fptr != NULL)
fclose(swed.fidat[i].fptr);
memset((void *) &swed.fidat[i], 0, sizeof(struct file_data));
}
swed.last_epheflag = epheflag;
}
}
/* high precision speed prevails fast speed */
if ((iflag & SEFLG_SPEED3) && (iflag & SEFLG_SPEED))
iflag = iflag & ~SEFLG_SPEED3;
if (iflag & SEFLG_SPEED3)
use_speed3 = TRUE;
/* topocentric with SEFLG_SPEED is not good if aberration is included.
* in such cases we calculate speed from three positions */
if ((iflag & SEFLG_SPEED) && (iflag & SEFLG_TOPOCTR) && !(iflag & SEFLG_NOABERR))
use_speed3 = TRUE;
/* cartesian flag excludes radians flag */
if ((iflag & SEFLG_XYZ) && (iflag & SEFLG_RADIANS))
iflag = iflag & ~SEFLG_RADIANS;
/* if (iflag & SEFLG_ICRS)
iflag |= SEFLG_J2000;*/
/* planetary center of body or planetary moon: either planet is called
* with SEFLG_CENTER_BODY or center of body with ipl = 9n99 is called.
* we want to handle both cases the same way. */
// planet is called with SE_PLUTO etc. and SEFLG_CENTER_BODY:
// get number of center of body
if ((iflag & SEFLG_CENTER_BODY) && ipl <= SE_PLUTO && (iflag & SEFLG_TEST_PLMOON) != SEFLG_TEST_PLMOON) {
iplmoon = ipl * 100 + 9099; // planetary center of body
}
// planet center of body or planetary moon is called using 9... number:
// moon number and planet number
if (ipl >= SE_PLMOON_OFFSET && ipl < SE_AST_OFFSET && (iflag & SEFLG_TEST_PLMOON) != SEFLG_TEST_PLMOON) {
iplmoon = ipl; // planetary center of body or planetary moon
ipl = (int) ((ipl - 9000) / 100);
iflag |= SEFLG_CENTER_BODY;
}
// with Mercury to Mars, we do not have center of body different from barycenter
if ((iflag & SEFLG_CENTER_BODY) && ipl <= SE_MARS && (iplmoon % 100) == 99) {
iplmoon = 0;
iflag &= ~SEFLG_CENTER_BODY;
}
if ((iflag & SEFLG_CENTER_BODY) || iplmoon > 0)
swi_force_app_pos_etc();
/* pointer to save area */
if (ipl < SE_NPLANETS && ipl >= SE_SUN) {
sd = &swed.savedat[ipl];
// if (iflag & SEFLG_CENTER_BODY)
// sd = &swed.savedat[SE_NPLANETS];
} else {
/* other bodies, e.g. asteroids called with ipl = SE_AST_OFFSET + MPC# */
sd = &swed.savedat[SE_NPLANETS];
}
/*
* if position is available in save area, it is returned.
* this is the case, if tjd = tsave and iflag = iflgsave.
* coordinate flags can be neglected, because save area
* provides all coordinate types.
* if ipl > SE_AST(EROID)_OFFSET, ipl must be checked,
* because all asteroids called by MPC number share the same
* save area.
*/
if (sd->tsave == tjd && tjd != 0 && ipl == sd->ipl && iplmoon == 0) {
if ((sd->iflgsave & ~SEFLG_COORDSYS) == (iflag & ~SEFLG_COORDSYS))
goto end_swe_calc;
}
/*
* otherwise, new position must be computed
*/
if (!use_speed3) {
/*
* with high precision speed from one call of swecalc()
* (FAST speed)
*/
sd->tsave = tjd;
sd->ipl = ipl;
if ((sd->iflgsave = swecalc(tjd, ipl, iplmoon, iflag, sd->xsaves, serr)) == ERR)
goto return_error;
} else {
/*
* with speed from three calls of swecalc(), slower and less accurate.
* (SLOW speed, for test only)
*/
sd->tsave = tjd;
sd->ipl = ipl;
switch(ipl) {
case SE_MOON:
dt = MOON_SPEED_INTV;
break;
case SE_OSCU_APOG:
case SE_TRUE_NODE:
/* this is the optimum dt with Moshier ephemeris, but not with
* JPL ephemeris or SWISSEPH. To avoid completely false speed
* in case that JPL is wanted but the program returns Moshier,
* we use Moshier optimum.
* For precise speed, use JPL and FAST speed computation,
*/
dt = NODE_CALC_INTV_MOSH;
break;
default:
dt = PLAN_SPEED_INTV;
break;
}
if ((sd->iflgsave = swecalc(tjd-dt, ipl, iplmoon, iflag, x0, serr)) == ERR)
goto return_error;
if ((sd->iflgsave = swecalc(tjd+dt, ipl, iplmoon, iflag, x2, serr)) == ERR)
goto return_error;
if ((sd->iflgsave = swecalc(tjd, ipl, iplmoon, iflag, sd->xsaves, serr)) == ERR)
goto return_error;
denormalize_positions(x0, sd->xsaves, x2);
calc_speed(x0, sd->xsaves, x2, dt);
}
end_swe_calc:
if (iflag & SEFLG_EQUATORIAL) {
xs = sd->xsaves+12; /* equatorial coordinates */
} else {
xs = sd->xsaves; /* ecliptic coordinates */
}
if (iflag & SEFLG_XYZ)
xs = xs+6; /* cartesian coordinates */
if (ipl == SE_ECL_NUT) {
i = 4;
} else {
i = 3;
}
for (j = 0; j < i; j++)
x[j] = *(xs + j);
for (j = i; j < 6; j++)
x[j] = 0;
if (iflag & (SEFLG_SPEED3 | SEFLG_SPEED)) {
for (j = 3; j < 6; j++)
x[j] = *(xs + j);
}
#if 1
if (iflag & SEFLG_RADIANS) {
if (ipl == SE_ECL_NUT) {
for (j = 0; j < 4; j++)
x[j] *= DEGTORAD;
} else {
for (j = 0; j < 2; j++)
x[j] *= DEGTORAD;
if (iflag & (SEFLG_SPEED3 | SEFLG_SPEED)) {
for (j = 3; j < 5; j++)
x[j] *= DEGTORAD;
}
}
}
#endif
for (i = 0; i <= 5; i++)
xx[i] = x[i];
//iflag = sd->iflgsave | (iflag & SEFLG_COORDSYS);
// iflag from previous call of swe_calc(), without coordinate system flags
iflag = sd->iflgsave & ~SEFLG_COORDSYS;
// add correct coordinate system flags
iflag |= (iflgsave & SEFLG_COORDSYS);
/* if no ephemeris has been specified, do not return chosen ephemeris */
if ((iflgsave & SEFLG_EPHMASK) == 0)
iflag = iflag & ~SEFLG_DEFAULTEPH;
#ifdef TRACE
trace_swe_calc(2, tjd, ipl, iflag, xx, serr);
#endif
return iflag;
return_error:
for (i = 0; i <= 5; i++)
xx[i] = 0;
#ifdef TRACE
trace_swe_calc(2, tjd, ipl, iflag, xx, serr);
#endif
return ERR;
}
int32 CALL_CONV swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag,
double *xx, char *serr)
{
double deltat;
int32 retval = OK;
int32 epheflag = 0;
iflag = plaus_iflag(iflag, ipl, tjd_ut, serr);
epheflag = iflag & SEFLG_EPHMASK;
if (epheflag == 0) {
epheflag = SEFLG_SWIEPH;
iflag |= SEFLG_SWIEPH;
}
deltat = swe_deltat_ex(tjd_ut, iflag, serr);
retval = swe_calc(tjd_ut + deltat, ipl, iflag, xx, serr);
/* if ephe required is not ephe returned, adjust delta t: */
if ((retval & SEFLG_EPHMASK) != epheflag) {
deltat = swe_deltat_ex(tjd_ut, retval, NULL);
retval = swe_calc(tjd_ut + deltat, ipl, iflag, xx, NULL);
}
return retval;
}
static int32 swecalc(double tjd, int ipl, int32 iplmoon, int32 iflag, double *x, char *serr)
{
int i;
int ipli, ipli_ast, ifno;
int retc;
int32 epheflag = SEFLG_DEFAULTEPH;
struct plan_data *pdp;
struct plan_data *pedp = &swed.pldat[SEI_EARTH];
struct plan_data *psdp = &swed.pldat[SEI_SUNBARY];
struct plan_data *ndp;
double *xp, *xp2;
double ss[3];
char serr2[AS_MAXCH];
//if (serr != NULL)
// *serr = '\0'; // is done in calling function
serr2[0] = '\0';
/******************************************
* iflag plausible? *
******************************************/
iflag = plaus_iflag(iflag, ipl, tjd, serr);
/******************************************
* which ephemeris is wanted, which is used?
* Three ephemerides are possible: MOSEPH, SWIEPH, JPLEPH.
* JPLEPH is best, SWIEPH is nearly as good, MOSEPH is least precise.
* The availability of the various ephemerides depends on the installed
* ephemeris files in the users ephemeris directory. This can change at
* any time.
* Swisseph should try to fulfil the wish of the user for a specific
* ephemeris, but use a less precise one if the desired ephemeris is not
* available for the given date and body.
* If internal ephemeris errors are detected (data error, file length error)
* an error is returned.
* If the time range is bad but another ephemeris can deliver this range,
* the other ephemeris is used.
* If no ephemeris is specified, DEFAULTEPH is assumed as desired.
* DEFAULTEPH is defined at compile time, usually as JPLEPH.
* The caller learns from the return flag which ephemeris was used.
* ephe_flag is extracted from iflag, but can change later if the
* desired ephe is not available.
******************************************/
if (iflag & SEFLG_MOSEPH)
epheflag = SEFLG_MOSEPH;
if (iflag & SEFLG_SWIEPH)
epheflag = SEFLG_SWIEPH;
if (iflag & SEFLG_JPLEPH)
epheflag = SEFLG_JPLEPH;
/* no barycentric calculations with Moshier ephemeris */
if ((iflag & SEFLG_BARYCTR) && (iflag & SEFLG_MOSEPH)) {
if (serr != NULL)
strcpy(serr, "barycentric Moshier positions are not supported.");
return ERR;
}
if (epheflag != SEFLG_MOSEPH && !swed.ephe_path_is_set && !swed.jpl_file_is_open)
swe_set_ephe_path(NULL);
if ((iflag & SEFLG_SIDEREAL) && !swed.ayana_is_set)
swe_set_sid_mode(SE_SIDM_FAGAN_BRADLEY, 0, 0);
/******************************************
* obliquity of ecliptic 2000 and of date *
******************************************/
swi_check_ecliptic(tjd, iflag);
/******************************************
* nutation *
******************************************/
swi_check_nutation(tjd, iflag);
/******************************************
* select planet and ephemeris *
* *
* ecliptic and nutation *
******************************************/
if (ipl == SE_ECL_NUT) {
x[0] = swed.oec.eps + swed.nut.nutlo[1]; /* true ecliptic */
x[1] = swed.oec.eps; /* mean ecliptic */
x[2] = swed.nut.nutlo[0]; /* nutation in longitude */
x[3] = swed.nut.nutlo[1]; /* nutation in obliquity */
/*if ((iflag & SEFLG_RADIANS) == 0)*/
for (i = 0; i <= 3; i++)
x[i] *= RADTODEG;
return(iflag);
/******************************************
* moon *
******************************************/
} else if (ipl == SE_MOON) {
/* internal planet number */
ipli = SEI_MOON;
pdp = &swed.pldat[ipli];
xp = pdp->xreturn;
switch(epheflag) {
case SEFLG_JPLEPH:
retc = jplplan(tjd, ipli, iflag, DO_SAVE, NULL, NULL, NULL, serr);
/* read error or corrupt file */
if (retc == ERR)
goto return_error;
/* jpl ephemeris not on disk or date beyond ephemeris range
* or file corrupt */
if (retc == NOT_AVAILABLE) {
iflag = (iflag & ~SEFLG_JPLEPH) | SEFLG_SWIEPH;
if (serr != NULL && strlen(serr) + 30 < AS_MAXCH)
strcat(serr, " \ntrying Swiss Eph; ");
goto sweph_moon;
} else if (retc == BEYOND_EPH_LIMITS) {
if (tjd > MOSHLUEPH_START && tjd < MOSHLUEPH_END) {
iflag = (iflag & ~SEFLG_JPLEPH) | SEFLG_MOSEPH;
if (serr != NULL && strlen(serr) + 30 < AS_MAXCH)
strcat(serr, " \nusing Moshier Eph; ");
goto moshier_moon;
} else
goto return_error;
}
break;
case SEFLG_SWIEPH:
sweph_moon:
retc = sweplan(tjd, ipli, SEI_FILE_MOON, iflag, DO_SAVE,
NULL, NULL, NULL, NULL, serr);
if (retc == ERR)
goto return_error;
/* if sweph file not found, switch to moshier */
if (retc == NOT_AVAILABLE) {
if (tjd > MOSHLUEPH_START && tjd < MOSHLUEPH_END) {
iflag = (iflag & ~SEFLG_SWIEPH) | SEFLG_MOSEPH;
if (serr != NULL && strlen(serr) + 30 < AS_MAXCH)
strcat(serr, " \nusing Moshier eph.; ");
goto moshier_moon;
} else
goto return_error;
}
break;
case SEFLG_MOSEPH:
moshier_moon:
retc = swi_moshmoon(tjd, DO_SAVE, NULL, serr);/**/
if (retc == ERR)
goto return_error;
/* for hel. position, we need earth as well */
retc = swi_moshplan(tjd, SEI_EARTH, DO_SAVE, NULL, NULL, serr);/**/
if (retc == ERR)
goto return_error;
break;
default:
break;
}
/* heliocentric, lighttime etc. */
if ((retc = app_pos_etc_moon(iflag, serr)) != OK)
goto return_error; /* retc may be wrong with sidereal calculation */
/**********************************************
* barycentric sun *
* (only JPL and SWISSEPH ephemerises) *
**********************************************/
} else if (ipl == SE_SUN && (iflag & SEFLG_BARYCTR)) {
/* barycentric sun must be handled separately because of
* the following reasons:
* ordinary planetary computations use the function
* main_planet() and its subfunction jplplan(),
* see further below.
* now, these functions need the swisseph internal
* planetary indices, where SEI_EARTH = SEI_SUN = 0.
* therefore they don't know the difference between
* a barycentric sun and a barycentric earth and
* always return barycentric earth.
* to avoid this problem, many functions would have to
* be changed. as an alternative, we choose a more
* separate handling. */
ipli = SEI_SUN; /* = SEI_EARTH ! */
xp = pedp->xreturn;
switch(epheflag) {
case SEFLG_JPLEPH:
/* open ephemeris, if still closed */
if (!swed.jpl_file_is_open) {
retc = open_jpl_file(ss, swed.jplfnam, swed.ephepath, serr);
if (retc != OK)
goto sweph_sbar;
}
retc = swi_pleph(tjd, J_SUN, J_SBARY, psdp->x, serr);
if (retc == ERR || retc == BEYOND_EPH_LIMITS) {
swi_close_jpl_file();
swed.jpl_file_is_open = FALSE;
goto return_error;
}
/* jpl ephemeris not on disk or date beyond ephemeris range
* or file corrupt */
if (retc == NOT_AVAILABLE) {
iflag = (iflag & ~SEFLG_JPLEPH) | SEFLG_SWIEPH;
if (serr != NULL && strlen(serr) + 30 < AS_MAXCH)
strcat(serr, " \ntrying Swiss Eph; ");
goto sweph_sbar;
}
psdp->teval = tjd;
break;
case SEFLG_SWIEPH:
sweph_sbar:
/* sweplan() provides barycentric sun as a by-product in save area;
* it is saved in swed.pldat[SEI_SUNBARY].x */
retc = sweplan(tjd, SEI_EARTH, SEI_FILE_PLANET, iflag, DO_SAVE, NULL, NULL, NULL, NULL, serr);
#if 1
if (retc == ERR || retc == NOT_AVAILABLE)
goto return_error;
#else /* this code would be needed if barycentric moshier calculation
* were implemented */
if (retc == ERR)
goto return_error;
/* if sweph file not found, switch to moshier */
if (retc == NOT_AVAILABLE) {
if (tjd > MOSHLUEPH_START && tjd < MOSHLUEPH_END) {
iflag = (iflag & ~SEFLG_SWIEPH) | SEFLG_MOSEPH;
if (serr != NULL && strlen(serr) + 30 < AS_MAXCH)
strcat(serr, " \nusing Moshier; ");
goto moshier_sbar;
} else
goto return_error;
}
#endif
psdp->teval = tjd;
/* pedp->teval = tjd; */
break;
default:
return ERR;
break;
}
/* flags */
if ((retc = app_pos_etc_sbar(iflag, serr)) != OK)
goto return_error;
/* iflag has possibly changed */
iflag = pedp->xflgs;
/* barycentric sun is now in save area of barycentric earth.
* (pedp->xreturn = swed.pldat[SEI_EARTH].xreturn).
* in case a barycentric earth computation follows for the same
* date, the planetary functions will return the barycentric
* SUN unless we force a new computation of pedp->xreturn.
* this can be done by initializing the save of iflag.
*/
pedp->xflgs = -1;
/******************************************
* mercury - pluto *
******************************************/
} else if (ipl == SE_SUN /* main planet */
|| ipl == SE_MERCURY
|| ipl == SE_VENUS
|| ipl == SE_MARS
|| ipl == SE_JUPITER
|| ipl == SE_SATURN
|| ipl == SE_URANUS
|| ipl == SE_NEPTUNE
|| ipl == SE_PLUTO
|| ipl == SE_EARTH) {
if (iflag & SEFLG_HELCTR) {
if (ipl == SE_SUN) {
/* heliocentric position of Sun does not exist */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
} else if (iflag & SEFLG_BARYCTR) {
;
} else { /* geocentric */
if (ipl == SE_EARTH) {
/* geocentric position of Earth does not exist */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
}
/* internal planet number */
ipli = pnoext2int[ipl];
pdp = &swed.pldat[ipli];
xp = pdp->xreturn;
retc = main_planet(tjd, ipli, iplmoon, epheflag, iflag, serr);
if (retc == ERR)
goto return_error;
/* iflag has possibly changed in main_planet() */
iflag = pdp->xflgs;
/*********************i************************
* mean lunar node *
* for comment s. moshmoon.c, swi_mean_node() *
**********************************************/
} else if (ipl == SE_MEAN_NODE) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar node not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
ndp = &swed.nddat[SEI_MEAN_NODE];
xp = ndp->xreturn;
xp2 = ndp->x;
retc = swi_mean_node(tjd, xp2, serr);
if (retc == ERR)
goto return_error;
/* speed (is almost constant; variation < 0.001 arcsec) */
retc = swi_mean_node(tjd - MEAN_NODE_SPEED_INTV, xp2+3, serr);
if (retc == ERR)
goto return_error;
xp2[3] = swe_difrad2n(xp2[0], xp2[3]) / MEAN_NODE_SPEED_INTV;
xp2[4] = xp2[5] = 0;
ndp->teval = tjd;
ndp->xflgs = -1;
/* lighttime etc. */
if ((retc = app_pos_etc_mean(SEI_MEAN_NODE, iflag, serr)) != OK)
goto return_error;
/* to avoid infinitesimal deviations from latitude = 0
* that result from conversions */
if (!(iflag & SEFLG_SIDEREAL) && !(iflag & SEFLG_J2000)) {
ndp->xreturn[1] = 0.0; /* ecl. latitude */
ndp->xreturn[4] = 0.0; /* speed */
ndp->xreturn[5] = 0.0; /* radial speed */
ndp->xreturn[8] = 0.0; /* z coordinate */
ndp->xreturn[11] = 0.0; /* speed */
}
if (retc == ERR)
goto return_error;
/**********************************************
* mean lunar apogee ('dark moon', 'lilith') *
* for comment s. moshmoon.c, swi_mean_apog() *
**********************************************/
} else if (ipl == SE_MEAN_APOG) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar apogee not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
ndp = &swed.nddat[SEI_MEAN_APOG];
xp = ndp->xreturn;
xp2 = ndp->x;
retc = swi_mean_apog(tjd, xp2, serr);
if (retc == ERR)
goto return_error;
/* speed (is not constant! variation ~= several arcsec) */
retc = swi_mean_apog(tjd - MEAN_NODE_SPEED_INTV, xp2+3, serr);
if (retc == ERR)
goto return_error;
for(i = 0; i <= 1; i++)
xp2[3+i] = swe_difrad2n(xp2[i], xp2[3+i]) / MEAN_NODE_SPEED_INTV;
xp2[5] = 0;
ndp->teval = tjd;
ndp->xflgs = -1;
/* lighttime etc. */
if ((retc = app_pos_etc_mean(SEI_MEAN_APOG, iflag, serr)) != OK)
goto return_error;
/* to avoid infinitesimal deviations from r-speed = 0
* that result from conversions */
ndp->xreturn[5] = 0.0; /* speed */
if (retc == ERR)
goto return_error;
/***********************************************
* osculating lunar node ('true node') *
***********************************************/
} else if (ipl == SE_TRUE_NODE) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar node not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
ndp = &swed.nddat[SEI_TRUE_NODE];
xp = ndp->xreturn;
retc = lunar_osc_elem(tjd, SEI_TRUE_NODE, iflag, serr);
iflag = ndp->xflgs;
/* to avoid infinitesimal deviations from latitude = 0
* that result from conversions */
if (!(iflag & SEFLG_SIDEREAL) && !(iflag & SEFLG_J2000)) {
ndp->xreturn[1] = 0.0; /* ecl. latitude */
ndp->xreturn[4] = 0.0; /* speed */
ndp->xreturn[8] = 0.0; /* z coordinate */
ndp->xreturn[11] = 0.0; /* speed */
}
if (retc == ERR)
goto return_error;
/***********************************************
* osculating lunar apogee *
***********************************************/
} else if (ipl == SE_OSCU_APOG) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar apogee not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
ndp = &swed.nddat[SEI_OSCU_APOG];
xp = ndp->xreturn;
retc = lunar_osc_elem(tjd, SEI_OSCU_APOG, iflag, serr);
iflag = ndp->xflgs;
if (retc == ERR)
goto return_error;
/***********************************************
* interpolated lunar apogee *
***********************************************/
} else if (ipl == SE_INTP_APOG) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar apogee not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;
}
if (tjd < MOSHLUEPH_START || tjd > MOSHLUEPH_END) {
for (i = 0; i < 24; i++)
x[i] = 0;
if (serr != NULL)
sprintf(serr, "Interpolated apsides are restricted to JD %8.1f - JD %8.1f",
MOSHLUEPH_START, MOSHLUEPH_END);
return ERR;
}
ndp = &swed.nddat[SEI_INTP_APOG];
xp = ndp->xreturn;
retc = intp_apsides(tjd, SEI_INTP_APOG, iflag, serr);
iflag = ndp->xflgs;
if (retc == ERR)
goto return_error;
/***********************************************
* interpolated lunar perigee *
***********************************************/
} else if (ipl == SE_INTP_PERG) {
if ((iflag & SEFLG_HELCTR) || (iflag & SEFLG_BARYCTR)) {
/* heliocentric/barycentric lunar apogee not allowed */
for (i = 0; i < 24; i++)
x[i] = 0;
return iflag;