-
Notifications
You must be signed in to change notification settings - Fork 0
/
swehel.c
3518 lines (3406 loc) · 120 KB
/
swehel.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
Heliacal risings and related calculations
Author: Victor Reijs
This program code is a translation of part of:
Victor Reijs' software ARCHAEOCOSMO (archaeoastronomy and
geodesy functions),
http://www.iol.ie/~geniet/eng/archaeocosmoprocedures.htm
Translation from VB into C by Dieter Koch
Problem reports can be sent to [email protected] or [email protected]
Copyright (c) Victor Reijs, 2008
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 public license version 2 or later
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 GNU GPL 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 GNU GPL or a compatible license.
See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
If the developer choses the Swiss Ephemeris Professional license,
he must follow the instructions as found in http://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.
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 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 "swephexp.h"
#include "sweph.h"
#include "swephlib.h"
#include <sys/stat.h>
#define PLSV 0 /*if Planet, Lunar and Stellar Visibility formula is needed PLSV=1*/
#define criticalangle 0.0 /*[deg]*/
#define BNIGHT 1479.0 /*[nL]*/
#define BNIGHT_FACTOR 1.0
#define PI M_PI
#define Min2Deg (1.0 / 60.0)
#define DEBUG 0
#define DONE 1
#define MaxTryHours 4
#define TimeStepDefault 1
#define LocalMinStep 8
/* time constants */
#define Y2D 365.25 /*[Day]*/
#define D2Y (1 / Y2D) /*[Year]*/
#define D2H 24.0 /*[Hour]*/
#define H2S 3600.0 /*[sec]*/
#define D2S (D2H * H2S) /*[sec]*/
#define S2H (1.0 / H2S) /*[Hour]*/
#define JC2D 36525.0 /*[Day]*/
#define M2S 60.0 /*[sec]*/
/* Determines which algorimths are used*/
#define REFR_SINCLAIR 0
#define REFR_BENNETTH 1
#define FormAstroRefrac REFR_SINCLAIR /*for Astronomical refraction can be "bennetth" or "sinclair"*/
#define GravitySource 2 /*0=RGO, 1=Wikipedia,2=Exp. Suppl. 1992,3=van der Werf*/
#define REarthSource 1 /*0=RGO (constant), 1=WGS84 method*/
#define StartYear 1820 /*[year]*/
#define Average 1.80546834626888 /*[msec/cy]*/
#define Periodicy 1443.67123144531 /*[year]*/
#define Amplitude 3.75606495492684 /*[msec]*/
#define phase 0 /*[deg]*/
#define MAX_COUNT_SYNPER 5 /* search within 10 synodic periods */
#define MAX_COUNT_SYNPER_MAX 1000000 /* high, so there is not max count */
#define AvgRadiusMoon (15.541 / 60) /* '[Deg] at 2007 CE or BCE*/
/* WGS84 ellipsoid constants
* http://w3sli.wcape.gov.za/Surveys/Mapping/wgs84.htm*/
#define Ra 6378136.6 /*'[m]*/
#define Rb 6356752.314 /*'[m]*/
/* choices in Schaefer's model */
#define nL2erg (1.02E-15)
#define erg2nL (1 / nL2erg) /*erg2nL to nLambert*/
#define MoonDistance 384410.4978 /*[km]*/
#define scaleHwater 3000.0 /*[m] Ricchiazzi [1997] 8200 Schaefer [2000]*/
#define scaleHrayleigh 8515.0 /*[m] Su [2003] 8200 Schaefer [2000]*/
#define scaleHaerosol 3745.0 /*m Su [2003] 1500 Schaefer [2000]*/
#define scaleHozone 20000.0 /*[m] Schaefer [2000]*/
#define astr2tau 0.921034037197618 /*LN(10 ^ 0.4)*/
#define tau2astr 1 / astr2tau
/* meteorological constants*/
#define C2K 273.15 /*[K]*/
#define DELTA 18.36
#define TempNulDiff 0.000001
#define PressRef 1000 /*[mbar]*/
#define MD 28.964 /*[kg] Mol weight of dry air van der Werf*/
#define MW 18.016 /*[kg] Mol weight of water vapor*/
#define GCR 8314.472 /*[L/kmol/K] van der Werf*/
#define LapseSA 0.0065 /*[K/m] standard atmosphere*/
#define LapseDA 0.0098 /*[K/m] dry adiabatic*/
/* lowest apparent altitude to provide*/
#define LowestAppAlt -3.5 /*[Deg]*/
/*optimization delta*/
#define epsilon 0.001
/* for Airmass usage*/
#define staticAirmass 0 /* use staticAirmass=1 instead depending on difference k's*/
/* optic stuff */
#define GOpticMag 1 /*telescope magnification*/
#define GOpticTrans 0.8 /*telescope transmission*/
#define GBinocular 1 /*1-binocular 0=monocular*/
#define GOpticDia 50 /*telescope diameter [mm]*/
static double mymin(double a, double b)
{
if (a <= b)
return a;
return b;
}
static double mymax(double a, double b)
{
if (a >= b)
return a;
return b;
}
/*###################################################################*/
static double Tanh(double x)
{
return (exp(x) - exp(-x)) / (exp(x) + exp(-x));
}
/*
' B [nL]
' SN [-]
' CVA [deg]
*/
static double CVA(double B, double SN, int32 helflag)
{
/*Schaefer, Astronomy and the limits of vision, Archaeoastronomy, 1993*/
AS_BOOL is_scotopic = FALSE;
//if (B < BNIGHT)
if (B < 1394) /* use this value for BNIGHT to make the function continous */
is_scotopic = TRUE;
if (helflag & SE_HELFLAG_VISLIM_PHOTOPIC)
is_scotopic = FALSE;
if (helflag & SE_HELFLAG_VISLIM_SCOTOPIC)
is_scotopic = TRUE;
if (is_scotopic)
return mymin(900, 380 / SN * pow(10, (0.3 * pow(B, (-0.29))))) / 60.0 / 60.0;
else
return (40.0 / SN) * pow(10, (8.28 * pow(B, (-0.29)))) / 60.0 / 60.0;
}
/*
' age [year]
' B [nL]
' PupilDia [mm]
*/
static double PupilDia(double Age, double B)
{
/* age dependancy from Garstang [2000]*/
return (0.534 - 0.00211 * Age - (0.236 - 0.00127 * Age) * Tanh(0.4 * log(B) / log(10) - 2.2)) * 10;
}
/*
'Input
' Bback [nL]
' kX [-]
' Binocular [-]
' OpticMag [-]
' OpticDia [mm]
' OpticTrans [-]
' JDNDaysUT [JDN]
' Age [Year]
' SN [-]
' ObjectName
' TypeFactor [0=itensity factor 1=background factor]
'Output
' OpticFactor [-]
*/
static double OpticFactor(double Bback, double kX, double *dobs, double JDNDaysUT, char *ObjectName, int TypeFactor, int helflag)
{
double Pst, CIb, CIi, ObjectSize, Fb, Fe, Fsc, Fci, Fcb, Ft, Fp, Fa, Fr, Fm;
double Age = dobs[0];
double SN = dobs[1], SNi;
double Binocular = dobs[2];
double OpticMag = dobs[3];
double OpticDia = dobs[4];
double OpticTrans = dobs[5];
AS_BOOL is_scotopic = FALSE;
JDNDaysUT += 0.0; /* currently not used, statement prevents compiler warning */
SNi = SN;
if (SNi <= 0.00000001) SNi = 0.00000001;
/* 23 jaar as standard from Garstang*/
Pst = PupilDia(23, Bback);
if (OpticMag == 1) { /*OpticMagn=1 means using eye*/
OpticTrans = 1;
OpticDia = Pst;
}
#if 0 /*is done in default_heliacal_parameters()*/
if (OpticMag == 0) { /*OpticMagn=0 (undefined) using eye*/
OpticTrans = 1;
OpticDia = Pst;
Binocular = 1;
OpticMag = 1;
}
#endif
/* Schaefer, Astronomy and the limits of vision, Archaeoastronomy, 1993*/
CIb = 0.7; /* color of background (from Ben Sugerman)*/
CIi = 0.5; /* Color index for white (from Ben Sugerman), should be function of ObjectName*/
ObjectSize = 0;
if (strcmp(ObjectName, "moon") == 0) {
/*ObjectSize and CI needs to be determined (depending on JDNDaysUT)*/
;
}
Fb = 1;
if (Binocular == 0) Fb = 1.41;
//if (Bback < BNIGHT)
if (Bback < 1645) /* use this value for BNIGHT to make the function continuous */
is_scotopic = TRUE;
if (helflag & SE_HELFLAG_VISLIM_PHOTOPIC)
is_scotopic = FALSE;
if (helflag & SE_HELFLAG_VISLIM_SCOTOPIC)
is_scotopic = TRUE;
if (is_scotopic) {
Fe = pow(10, (0.48 * kX));
Fsc = mymin(1, (1 - pow(Pst / 124.4, 4)) / (1 - pow((OpticDia / OpticMag / 124.4), 4)));
Fci = pow(10, (-0.4 * (1 - CIi / 2.0)));
Fcb = pow(10, (-0.4 * (1 - CIb / 2.0)));
} else {
Fe = pow(10, (0.4 * kX));
Fsc = mymin(1, pow((OpticDia / OpticMag / Pst), 2) * (1 - exp(-pow((Pst / 6.2), 2))) / (1 - exp(-pow((OpticDia / OpticMag / 6.2), 2))));
Fci = 1;
Fcb = 1;
}
Ft = 1 / OpticTrans;
Fp = mymax(1, pow((Pst / (OpticMag * PupilDia(Age, Bback))), 2));
Fa = pow((Pst / OpticDia), 2);
Fr = (1 + 0.03 * pow((OpticMag * ObjectSize / CVA(Bback, SNi, helflag)), 2)) / pow(SNi, 2);
Fm = pow(OpticMag, 2);
#if DEBUG
fprintf(stderr, "Pst=%f\n", Pst);
fprintf(stderr, "Fb =%f\n", Fb);
fprintf(stderr, "Fe =%f\n", Fe);
fprintf(stderr, "Ft =%f\n", Ft);
fprintf(stderr, "Fp =%f\n", Fp);
fprintf(stderr, "Fa =%f\n", Fa);
fprintf(stderr, "Fm =%f\n", Fm);
fprintf(stderr, "Fsc=%f\n", Fsc);
fprintf(stderr, "Fci=%f\n", Fci);
fprintf(stderr, "Fcb=%f\n", Fcb);
fprintf(stderr, "Fr =%f\n", Fr );
#endif
if (TypeFactor == 0)
return Fb * Fe * Ft * Fp * Fa * Fr * Fsc * Fci;
else
return Fb * Ft * Fp * Fa * Fm * Fsc * Fcb;
}
/*###################################################################
*/
static int32 DeterObject(char *ObjectName)
{
char s[AS_MAXCH];
char *sp;
int32 ipl;
strcpy(s, ObjectName);
for (sp = s; *sp != '\0'; sp++)
*sp = tolower(*sp);
if (strncmp(s, "sun", 3) == 0)
return SE_SUN;
if (strncmp(s, "venus", 5) == 0)
return SE_VENUS;
if (strncmp(s, "mars", 4) == 0)
return SE_MARS;
if (strncmp(s, "mercur", 6) == 0)
return SE_MERCURY;
if (strncmp(s, "jupiter", 7) == 0)
return SE_JUPITER;
if (strncmp(s, "saturn", 6) == 0)
return SE_SATURN;
if (strncmp(s, "uranus", 6) == 0)
return SE_URANUS;
if (strncmp(s, "neptun", 6) == 0)
return SE_NEPTUNE;
if (strncmp(s, "moon", 4) == 0)
return SE_MOON;
if ((ipl = atoi(s)) > 0) {
ipl += SE_AST_OFFSET;
return ipl;
}
return -1;
}
#if 0
int32 call_swe_calc(double tjd, int32 ipl, int32 iflag, double *x, char *serr)
{
int32 retval = OK, ipli, i;
double dtjd;
static TLS double tjdsv[3];
static TLS double xsv[3][6];
static TLS int32 iflagsv[3];
ipli = ipl;
if (ipli > SE_MOON)
ipli = 2;
dtjd = tjd - tjdsv[ipli];
if (tjdsv[ipli] != 0 && iflag == iflagsv[ipli] && fabs(dtjd) < 5.0 / 1440.0) {
for (i = 0; i < 3; i++)
x[i] = xsv[ipli][i] + dtjd * xsv[ipli][i+3];
for (i = 3; i < 6; i++)
x[i] = xsv[ipli][i];
} else {
retval = swe_calc(tjd, ipl, iflag, x, serr);
tjdsv[ipli] = tjd;
iflagsv[ipli] = iflag;
for (i = 0; i < 6; i++)
xsv[ipli][i] = x[i];
}
return retval;
}
#endif
/* avoids problems with star name string that may be overwritten by
swe_fixstar() */
static int32 call_swe_fixstar(char *star, double tjd, int32 iflag, double *xx, char *serr)
{
int32 retval;
char star2[AS_MAXCH];
strcpy(star2, star);
retval = swe_fixstar(star2, tjd, iflag, xx, serr);
return retval;
}
/* avoids problems with star name string that may be overwritten by
swe_fixstar_mag() */
static int32 call_swe_fixstar_mag(char *star, double *mag, char *serr)
{
int32 retval;
char star2[AS_MAXCH];
static TLS double dmag;
static TLS char star_save[AS_MAXCH];
if (strcmp(star, star_save) == 0) {
*mag = dmag;
return OK;
}
strcpy(star_save, star);
strcpy(star2, star);
retval = swe_fixstar_mag(star2, &dmag, serr);
*mag = dmag;
return retval;
}
/* avoids problems with star name string that may be overwritten by
swe_fixstar() */
static int32 call_swe_rise_trans(double tjd, int32 ipl, char *star, int32 helflag, int32 eventtype, double *dgeo, double atpress, double attemp, double *tret, char *serr)
{
int32 retval;
int32 iflag = helflag & (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH);
char star2[AS_MAXCH];
strcpy(star2, star);
retval = swe_rise_trans(tjd, ipl, star2, iflag, eventtype, dgeo, atpress, attemp, tret, serr);
return retval;
}
/*
* Written by Dieter Koch:
* Fast function for risings and settings of planets, can be used instead of
* swe_rise_trans(), which is much slower.
* For circumpolar and near-circumpolar planets use swe_rise_trans(), or
* generally use it for geographical latitudes higher than 58N/S.
* For fixed stars, swe_rise_trans() is fast enough.
*/
static int32 calc_rise_and_set(double tjd_start, int32 ipl, double *dgeo, double *datm, int32 eventflag, int32 helflag, double *trise, char *serr)
{
int retc = OK, i;
double sda, xs[6], xx[6], xaz[6], xaz2[6], dfac = 1/365.25;
double rdi, rh;
double tjd0 = tjd_start, tjdrise;
double tjdnoon = (int) tjd0 - dgeo[0] / 15.0 / 24.0;
int32 iflag = helflag & (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH);
int32 epheflag = iflag;
iflag |= SEFLG_EQUATORIAL;
if (!(helflag & SE_HELFLAG_HIGH_PRECISION))
iflag |= SEFLG_NONUT|SEFLG_TRUEPOS;
if (swe_calc_ut(tjd0, SE_SUN, iflag, xs, serr) == 0) {
if (serr != NULL)
strcpy(serr, "error in calc_rise_and_set(): calc(sun) failed ");
return ERR;
}
if (swe_calc_ut(tjd0, ipl, iflag, xx, serr) == 0) {
if (serr != NULL)
strcpy(serr, "error in calc_rise_and_set(): calc(sun) failed ");
return ERR;
}
tjdnoon -= swe_degnorm(xs[0] - xx[0])/360.0 + 0;
/* is planet above horizon or below? */
swe_azalt(tjd0, SE_EQU2HOR, dgeo, datm[0], datm[1], xx, xaz);
if (eventflag & SE_CALC_RISE) {
if (xaz[2] > 0) {
while (tjdnoon - tjd0 < 0.5) {/*printf("e");*/tjdnoon += 1;}
while (tjdnoon - tjd0 > 1.5) {/*printf("f");*/tjdnoon -= 1;}
} else {
while (tjdnoon - tjd0 < 0.0) {/*printf("g");*/tjdnoon += 1;}
while (tjdnoon - tjd0 > 1.0) {/*printf("h");*/tjdnoon -= 1;}
}
} else {
if (xaz[2] > 0) {
while (tjd0 - tjdnoon > 0.5) {/*printf("a");*/ tjdnoon += 1;}
while (tjd0 - tjdnoon < -0.5) {/*printf("b");*/ tjdnoon -= 1;}
} else {
while (tjd0 - tjdnoon > 0.0) {/*printf("c");*/ tjdnoon += 1;}
while (tjd0 - tjdnoon < -1.0) {/*printf("d");*/ tjdnoon -= 1;}
}
}
/* position of planet */
if (swe_calc_ut(tjdnoon, ipl, iflag, xx, serr) == ERR) {
if (serr != NULL)
strcpy(serr, "error in calc_rise_and_set(): calc(sun) failed ");
return ERR;
}
/* apparent radius of solar disk (ignoring refraction) */
rdi = 0;
if (ipl == SE_SUN)
rdi = asin(696000000.0 / 1.49597870691e+11 / xx[2]) / DEGTORAD;
else if (ipl == SE_MOON)
rdi = asin(1737000.0 / 1.49597870691e+11 / xx[2]) / DEGTORAD;
if (eventflag & SE_BIT_DISC_CENTER)
rdi = 0;
/* true altitude of sun, when it appears at the horizon */
/* refraction for a body visible at the horizon at 0m above sea,
* atmospheric temperature 10 deg C, atmospheric pressure 1013.25 is 34.5 arcmin*/
rh = -(34.5 / 60.0 + rdi);
/* semidiurnal arc of sun */
sda = acos(-tan(dgeo[1] * DEGTORAD) * tan(xx[1] * DEGTORAD)) * RADTODEG;
/* rough rising and setting times */
if (eventflag & SE_CALC_RISE)
tjdrise = tjdnoon - sda / 360.0;
else
tjdrise = tjdnoon + sda / 360.0;
/*ph->tset = tjd_start + sda / 360.0;*/
/* now calculate more accurate rising and setting times.
* use vertical speed in order to determine crossing of the horizon
* refraction of 34' and solar disk diameter of 16' = 50' = 0.84 deg */
iflag = epheflag|SEFLG_SPEED|SEFLG_EQUATORIAL;
if (ipl == SE_MOON)
iflag |= SEFLG_TOPOCTR;
if (!(helflag & SE_HELFLAG_HIGH_PRECISION))
iflag |= SEFLG_NONUT|SEFLG_TRUEPOS;
for (i = 0; i < 2; i++) {
if (swe_calc_ut(tjdrise, ipl, iflag, xx, serr) == ERR) {
/*fprintf(stderr, "hev4 tjd=%f, ipl=%d, iflag=%d\n", tjdrise, ipl, iflag);*/
return ERR;
}
swe_azalt(tjdrise, SE_EQU2HOR, dgeo, datm[0], datm[1], xx, xaz);
xx[0] -= xx[3] * dfac;
xx[1] -= xx[4] * dfac;
swe_azalt(tjdrise - dfac, SE_EQU2HOR, dgeo, datm[0], datm[1], xx, xaz2);
tjdrise -= (xaz[1] - rh) / (xaz[1] - xaz2[1]) * dfac;
/*fprintf(stderr, "%f\n", ph->trise);*/
}
*trise = tjdrise;
return retc;
}
static int32 my_rise_trans(double tjd, int32 ipl, char* starname, int32 eventtype, int32 helflag, double *dgeo, double *datm, double *tret, char *serr)
{
int retc = OK;
if (starname != NULL && *starname != '\0')
ipl = DeterObject(starname);
/* for non-circumpolar planets we can use a faster algorithm */
/*if (!(helflag & SE_HELFLAG_HIGH_PRECISION) && ipl != -1 && fabs(dgeo[1]) < 58) {*/
if (ipl != -1 && fabs(dgeo[1]) < 63) {
retc = calc_rise_and_set(tjd, ipl, dgeo, datm, eventtype, helflag, tret, serr);
/* for stars and circumpolar planets we use a rigorous algorithm */
} else {
retc = call_swe_rise_trans(tjd, ipl, starname, helflag, eventtype, dgeo, datm[0], datm[1], tret, serr);
}
/* printf("%f, %f\n", tjd, *tret);*/
return retc;
}
/*###################################################################
' JDNDaysUT [Days]
' dgeo [array: longitude, latitude, eye height above sea m]
' TempE [C]
' PresE [mbar]
' ObjectName (string)
' RSEvent (1=rise, 2=set,3=up transit,4=down transit)
' Rim [0=center,1=top]
' RiseSet [Day]
*/
static int32 RiseSet(double JDNDaysUT, double *dgeo, double *datm, char *ObjectName, int32 RSEvent, int32 helflag, int32 Rim, double *tret, char *serr)
{
int32 eventtype = RSEvent, Planet, retval;
if (Rim == 0)
eventtype |= SE_BIT_DISC_CENTER;
Planet = DeterObject(ObjectName);
if (Planet != -1)
retval = my_rise_trans(JDNDaysUT, Planet, "", eventtype, helflag, dgeo, datm, tret, serr);
else
retval = my_rise_trans(JDNDaysUT, -1, ObjectName, eventtype, helflag, dgeo, datm, tret, serr);
return retval;
}
/*###################################################################
' JDNDaysUT [Days]
' actual [0= approximation, 1=actual]
' SunRA [deg]
*/
static double SunRA(double JDNDaysUT, int32 helflag, char *serr)
{
int imon, iday, iyar, calflag = SE_GREG_CAL;
double dut;
static TLS double tjdlast;
static TLS double ralast;
helflag += 0; /* statement prevents compiler warning */
*serr = '\0';
if (JDNDaysUT == tjdlast)
return ralast;
#ifndef SIMULATE_VICTORVB
if (1) { /*helflag & SE_HELFLAG_HIGH_PRECISION) {*/
double tjd_tt;
double x[6];
int32 epheflag = helflag & (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH);
int32 iflag = epheflag | SEFLG_EQUATORIAL;
iflag |= SEFLG_NONUT | SEFLG_TRUEPOS;
tjd_tt = JDNDaysUT + swe_deltat_ex(JDNDaysUT, epheflag, serr);
if (swe_calc(tjd_tt, SE_SUN, iflag, x, serr) != ERR) {
ralast = x[0];
tjdlast = JDNDaysUT;
return ralast;
}
}
#endif
swe_revjul(JDNDaysUT, calflag, &iyar, &imon, &iday, &dut); /* this seems to be much faster than calling swe_revjul() ! Note: only because SunRA is called 1000s of times */
tjdlast = JDNDaysUT;
ralast = swe_degnorm((imon + (iday - 1) / 30.4 - 3.69) * 30);
/*ralast = (DatefromJDut(JDNDaysUT, 2) + (DatefromJDut(JDNDaysUT, 3) - 1) / 30.4 - 3.69) * 30;*/
return ralast;
}
/*###################################################################
' Temp [C]
' Kelvin [K]
*/
static double Kelvin(double Temp)
{
/*' http://en.wikipedia.org/wiki/Kelvin*/
return Temp + C2K;
}
/*###################################################################
' AppAlt [deg]
' TempE [C]
' PresE [mbar]
' TopoAltitudefromAppAlt [deg]
*/
static double TopoAltfromAppAlt(double AppAlt, double TempE, double PresE)
{
double R = 0;
double retalt = 0;
if (AppAlt >= LowestAppAlt) {
if (AppAlt > 17.904104638432)
R = 0.97 / tan(AppAlt * DEGTORAD);
else
R = (34.46 + 4.23 * AppAlt + 0.004 * AppAlt * AppAlt) / (1 + 0.505 * AppAlt + 0.0845 * AppAlt * AppAlt);
R = (PresE - 80) / 930 / (1 + 0.00008 * (R + 39) * (TempE - 10)) * R;
retalt = AppAlt - R * Min2Deg;
} else {
retalt = AppAlt;
}
return retalt;
}
/*###################################################################
' TopoAlt [deg]
' TempE [C]
' PresE [mbar]
' AppAltfromTopoAlt [deg]
' call this instead of swe_azalt(), because it is faster (lower precision
' is required)
*/
static double AppAltfromTopoAlt(double TopoAlt, double TempE, double PresE, int32 helflag)
{
/* using methodology of Newtown derivatives (analogue to what Swiss Emphemeris uses)*/
int i, nloop = 2;
double newAppAlt = TopoAlt;
double newTopoAlt = 0.0;
double oudAppAlt = newAppAlt;
double oudTopoAlt = newTopoAlt;
double verschil, retalt;
if (helflag & SE_HELFLAG_HIGH_PRECISION)
nloop = 5;
for (i = 0; i <= nloop; i++) {
newTopoAlt = newAppAlt - TopoAltfromAppAlt(newAppAlt, TempE, PresE);
/*newTopoAlt = newAppAlt - swe_refrac(newAppAlt, PresE, TempE, SE_CALC_APP_TO_TRUE);*/
verschil = newAppAlt - oudAppAlt;
oudAppAlt = newTopoAlt - oudTopoAlt - verschil;
if ((verschil != 0) && (oudAppAlt != 0))
verschil = newAppAlt - verschil * (TopoAlt + newTopoAlt - newAppAlt) / oudAppAlt;
else
verschil = TopoAlt + newTopoAlt;
oudAppAlt = newAppAlt;
oudTopoAlt = newTopoAlt;
newAppAlt = verschil;
}
retalt = TopoAlt + newTopoAlt;
if (retalt < LowestAppAlt)
retalt = TopoAlt;
return retalt;
}
/*###################################################################
' TopoAlt [deg]
' TopoDecl [deg]
' Lat [deg]
' HourAngle [hour]
*/
static double HourAngle(double TopoAlt, double TopoDecl, double Lat)
{
double Alti = TopoAlt * DEGTORAD;
double decli = TopoDecl * DEGTORAD;
double Lati = Lat * DEGTORAD;
double ha = (sin(Alti) - sin(Lati) * sin(decli)) / cos(Lati) / cos(decli);
if (ha < -1) ha = -1;
if (ha > 1) ha = 1;
/* from http://star-www.st-and.ac.uk/~fv/webnotes/chapt12.htm*/
return acos(ha) / DEGTORAD / 15.0;
}
/*###################################################################
' JDNDaysUT [Days]
' dgeo [array: longitude, latitude, eye height above sea m]
' TempE [C]
' PresE [mbar]
' ObjectName [-]
' Angle (0 = TopoAlt, 1 = Azi, 2=Topo Declination, 3=Topo Rectascension, 4=AppAlt,5=Geo Declination, 6=Geo Rectascension)
' ObjectLoc [deg]
*/
static int32 ObjectLoc(double JDNDaysUT, double *dgeo, double *datm, char *ObjectName, int32 Angle, int32 helflag, double *dret, char *serr)
{
double x[6], xin[3], xaz[3], tjd_tt;
int32 Planet;
int32 epheflag;
int32 iflag = SEFLG_EQUATORIAL;
epheflag = helflag & (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH);
iflag |= epheflag;
if (!(helflag & SE_HELFLAG_HIGH_PRECISION))
iflag |= SEFLG_NONUT | SEFLG_TRUEPOS;
if (Angle < 5) iflag = iflag | SEFLG_TOPOCTR;
if (Angle == 7) Angle = 0;
tjd_tt = JDNDaysUT + swe_deltat_ex(JDNDaysUT, epheflag, serr);
Planet = DeterObject(ObjectName);
if (Planet != -1) {
if (swe_calc(tjd_tt, Planet, iflag, x, serr) == ERR)
return ERR;
} else {
if (call_swe_fixstar(ObjectName, tjd_tt, iflag, x, serr) == ERR)
return ERR;
}
if (Angle == 2 || Angle == 5) {
*dret = x[1];
} else {
if (Angle == 3 || Angle == 6) {
*dret = x[0];
} else {
xin[0] = x[0];
xin[1] = x[1];
swe_azalt(JDNDaysUT, SE_EQU2HOR, dgeo, datm[0], datm[1], xin, xaz);
if (Angle == 0)
*dret = xaz[1];
if (Angle == 4)
*dret = AppAltfromTopoAlt(xaz[1], datm[0], datm[1], helflag);
if (Angle == 1) {
xaz[0] += 180;
if (xaz[0] >= 360)
xaz[0] -= 360;
*dret = xaz[0];
}
}
}
return OK;
}
/*###################################################################
' JDNDaysUT [Days]
' dgeo [array: longitude, latitude, eye height above sea m]
' TempE [C]
' PresE [mbar]
' ObjectName [-]
' Angle (0 = TopoAlt, 1 = Azi, 2=Topo Declination, 3=Topo Rectascension, 4=AppAlt,5=Geo Declination, 6=Geo Rectascension)
' ObjectLoc [deg]
*/
static int32 azalt_cart(double JDNDaysUT, double *dgeo, double *datm, char *ObjectName, int32 helflag, double *dret, char *serr)
{
double x[6], xin[3], xaz[3], tjd_tt;
int32 Planet;
int32 epheflag;
int32 iflag = SEFLG_EQUATORIAL;
epheflag = helflag & (SEFLG_JPLEPH|SEFLG_SWIEPH|SEFLG_MOSEPH);
iflag |= epheflag;
if (!(helflag & SE_HELFLAG_HIGH_PRECISION))
iflag |= SEFLG_NONUT | SEFLG_TRUEPOS;
iflag = iflag | SEFLG_TOPOCTR;
tjd_tt = JDNDaysUT + swe_deltat_ex(JDNDaysUT, epheflag, serr);
Planet = DeterObject(ObjectName);
if (Planet != -1) {
if (swe_calc(tjd_tt, Planet, iflag, x, serr) == ERR)
return ERR;
} else {
if (call_swe_fixstar(ObjectName, tjd_tt, iflag, x, serr) == ERR)
return ERR;
}
xin[0] = x[0];
xin[1] = x[1];
swe_azalt(JDNDaysUT, SE_EQU2HOR, dgeo, datm[0], datm[1], xin, xaz);
dret[0] = xaz[0];
dret[1] = xaz[1]; /* true altitude */
dret[2] = xaz[2]; /* apparent altitude */
/* also return cartesian coordinates, for apparent altitude */
xaz[1] = xaz[2];
xaz[2] = 1;
swi_polcart(xaz, xaz);
dret[3] = xaz[0];
dret[4] = xaz[1];
dret[5] = xaz[2];
return OK;
}
/*###################################################################
' LatA [rad]
' LongA [rad]
' LatB [rad]
' LongB [rad]
' DistanceAngle [rad]
*/
static double DistanceAngle(double LatA, double LongA, double LatB, double LongB)
{
double dlon = LongB - LongA;
double dlat = LatB - LatA;
/* Haversine formula
* http://www.movable-type.co.uk/scripts/GIS-FAQ-5.1.html
* R.W. Sinnott, Virtues of the Haversine, Sky and Telescope, vol. 68, no. 2, 1984, p. 159
*/
double sindlat2 = sin(dlat / 2);
double sindlon2 = sin(dlon / 2);
double corde = sindlat2 * sindlat2 + cos(LatA) * cos(LatB) * sindlon2 *sindlon2;
if (corde > 1) corde = 1;
return 2 * asin(sqrt(corde));
}
/*###################################################################
' heighteye [m]
' TempS [C]
' RH [%]
' kW [-]
*/
static double kW(double HeightEye, double TempS, double RH)
{
/* From Schaefer , Archaeoastronomy, XV, 2000, page 128*/
double WT = 0.031;
WT *= 0.94 * (RH / 100.0) * exp(TempS / 15) * exp(-1 * HeightEye / scaleHwater);
return WT;
}
/*###################################################################
' JDNDaysUT [-]
' AltS [deg]
' lat [deg]
' kOZ [-]
*/
static double kOZ(double AltS, double sunra, double Lat)
{
double CHANGEKO, OZ, LT, kOZret;
static TLS double koz_last, alts_last, sunra_last;
double altslim = 0;
if (AltS == alts_last && sunra == sunra_last)
return koz_last;
alts_last = AltS; sunra_last = sunra;
OZ = 0.031;
LT = Lat * DEGTORAD;
/* From Schaefer , Archaeoastronomy, XV, 2000, page 128*/
kOZret = OZ * (3.0 + 0.4 * (LT * cos(sunra * DEGTORAD) - cos(3 * LT))) / 3.0;
/* depending on day/night vision (altitude of sun < start astronomical twilight), KO changes from 100% to 30%
* see extinction section of Vistas in Astronomy page 343*/
altslim = -AltS - 12;
if (altslim < 0)
altslim = 0;
CHANGEKO = (100 - 11.6 * mymin(6, altslim)) / 100;
if ((0)) {
static int a = 0;
if (a == 0)
printf("bsk=%f %f\n", kOZret, AltS);
a = 1;
}
koz_last = kOZret * CHANGEKO;
return koz_last;
}
/*###################################################################
' AltS [deg]
' heighteye [m]
' kR [-]
*/
static double kR(double AltS, double HeightEye)
{
/* depending on day/night vision (altitude of sun < start astronomical twilight),
* lambda eye sensibility changes
* see extinction section of Vistas in Astronomy page 343*/
double CHANGEK, LAMBDA;
double val = -AltS - 12;
if (val < 0) val = 0;
if (val > 6) val = 6;
/*CHANGEK = (1 - 0.166667 * Min(6, Max(-AltS - 12, 0)));*/
CHANGEK = (1 - 0.166667 * val );
LAMBDA = 0.55 + (CHANGEK - 1) * 0.04;
/* From Schaefer , Archaeoastronomy, XV, 2000, page 128 */
return 0.1066 * exp(-1 * HeightEye / scaleHrayleigh) * pow(LAMBDA / 0.55 , -4);
}
static int Sgn(double x)
{
if (x < 0)
return -1;
return 1;
}
/*###################################################################
' JDNDaysUT [-]
' AltS [deg]
' lat [deg]
' heighteye [m]
' TempS [C]
' RH [%]
' VR [km]
' ka [-]
*/
static double ka(double AltS, double sunra, double Lat, double HeightEye, double TempS, double RH, double VR, char *serr)
{
double CHANGEKA, LAMBDA, BetaVr, Betaa, kaact;
double SL = Sgn(Lat);
/* depending on day/night vision (altitude of sun < start astronomical twilight),
* lambda eye sensibility changes
* see extinction section of Vistas in Astronomy page 343 */
static TLS double alts_last, sunra_last, ka_last;
if (AltS == alts_last && sunra == sunra_last)
return ka_last;
alts_last = AltS; sunra_last = sunra;
CHANGEKA = (1 - 0.166667 * mymin(6, mymax(-AltS - 12, 0)));
LAMBDA = 0.55 + (CHANGEKA - 1) * 0.04;
if (VR != 0) {
if (VR >= 1) {
/* Visbility range from http://www1.cs.columbia.edu/CAVE/publications/pdfs/Narasimhan_CVPR03.pdf
* http://www.icao.int/anb/SG/AMOSSG/meetings/amossg3/wp/SN11Rev.pdf where MOR=2.995/ke
* factor 1.3 is the relation between "prevailing visibility" and
* meteorological range was derived by Koshmeider in the 1920's */
BetaVr = 3.912 / VR;
Betaa = BetaVr - (kW(HeightEye, TempS, RH) / scaleHwater + kR(AltS, HeightEye) / scaleHrayleigh) * 1000 * astr2tau;
kaact = Betaa * scaleHaerosol / 1000 * tau2astr;
if (kaact < 0) {
if (serr != NULL)
strcpy(serr, "The provided Meteorological range is too long, when taking into acount other atmospheric parameters"); /* is a warning */
/* return 0; * return "#HIGHVR"; */
}
} else {
kaact = VR - kW(HeightEye, TempS, RH) - kR(AltS, HeightEye) - kOZ(AltS, sunra, Lat);
if (kaact < 0) {
if (serr != NULL)
strcpy(serr, "The provided atmosphic coeefficent (ktot) is too low, when taking into acount other atmospheric parameters"); /* is a warning */
/* return 0; * "#LOWktot"; */
}
}
} else {
/* From Schaefer , Archaeoastronomy, XV, 2000, page 128 */
#ifdef SIMULATE_VICTORVB
if (RH <= 0.00000001) RH = 0.00000001;
if (RH >= 99.99999999) RH = 99.99999999;
#endif
kaact = 0.1 * exp(-1 * HeightEye / scaleHaerosol) * pow(1 - 0.32 / log(RH / 100.0), 1.33) * (1 + 0.33 * SL * sin(sunra * DEGTORAD));
kaact = kaact * pow(LAMBDA / 0.55, -1.3);
}
ka_last = kaact;
return kaact;
}
/*###################################################################
' JDNDaysUT [-]
' AltS [deg]
' lat [deg]
' heighteye [m]
' TempS [C]
' RH [%]
' VR [km]
' ExtType [0=ka,1=kW,2=kR,3=kOZ,4=ktot]
' kt [-]
*/
static double kt(double AltS, double sunra, double Lat, double HeightEye, double TempS, double RH, double VR, int32 ExtType, char *serr)
{
double kRact = 0;
double kWact = 0;
double kOZact = 0;
double kaact = 0;
if (ExtType == 2 || ExtType == 4)
kRact = kR(AltS, HeightEye);
if (ExtType == 1 || ExtType == 4)
kWact = kW(HeightEye, TempS, RH);
if (ExtType == 3 || ExtType == 4)
kOZact = kOZ(AltS, sunra, Lat);
if (ExtType == 0 || ExtType == 4)
kaact = ka(AltS, sunra, Lat, HeightEye, TempS, RH, VR, serr);
if (kaact < 0)
kaact = 0;
return kWact + kRact + kOZact + kaact;
}
/*###################################################################
' AppAlt0 [deg]
' PresS [mbar]
' Airmass [??]
*/
static double Airmass(double AppAltO, double Press)
{
double airm, zend;
zend = (90 - AppAltO) * DEGTORAD;
if (zend > PI / 2)
zend = PI / 2;
airm = 1 / (cos(zend) + 0.025 * exp(-11 * cos(zend)));
return Press / 1013 * airm;
}
/*###################################################################
' scaleH '[m]
' zend [rad]
' PresS [mbar]
' Xext [-]
*/
static double Xext(double scaleH, double zend, double Press)
{
return Press / 1013.0 / (cos(zend) + 0.01 * sqrt(scaleH / 1000.0) * exp(-30.0 / sqrt(scaleH / 1000.0) * cos(zend)));
}
/*###################################################################
' scaleH '[m]
' zend [rad]
' PresS [mbar]
' Xlay [-]
*/
static double Xlay(double scaleH, double zend, double Press)
{
/*return Press / 1013.0 / sqrt(1.0 - pow(sin(zend) / (1.0 + (scaleH / Ra)), 2));*/
double a = sin(zend) / (1.0 + (scaleH / Ra));
return Press / 1013.0 / sqrt(1.0 - a * a);
}
/*###################################################################
' Meteorological formula
'###################################################################
' TempS [C]
' HeightEye [m]
' TempEfromTempS [C]
*/