09ebdbac32248e2969ea4335ade79749c628416b
[spider.git] / perl / Sun.pm
1 #!/usr/bin/perl -w
2 #
3 # This module was written by Steve Franke K9AN. 
4 # November, 1999.
5
6 # The formulas used in this module 
7 # are described in: 
8 # Astronomical Algorithms, Second Edition
9 # by Jean Meeus, 1998
10 # Published by Willmann-Bell, Inc.
11 # P.O. Box 35025, Richmond, Virginia 23235
12 #
13 # Atmospheric refraction and parallax are taken into
14 # account when calculating positions of the sun and moon, 
15 # and also when calculating the rise and set times.
16 #
17 # Copyright (c) 1999 - Steve Franke K9AN
18 #
19 # $Id$
20
21
22 package Sun;
23
24 use POSIX;
25
26 require Exporter;
27 @ISA = qw(Exporter);
28 @EXPORT = qw($pi $d2r $r2d );
29
30 use strict;
31 use vars qw($pi $d2r $r2d);
32  
33 $pi = 3.141592653589;
34 $d2r = ($pi/180);
35 $r2d = (180/$pi);
36
37 use vars qw(%keps);
38 use Keps;
39 use DXVars;
40 use DXUtil;
41
42 # reload the keps data
43 sub load
44 {
45         my @out;
46         my $s = readfilestr("$main::root/local/Keps.pm");
47         if ($s) {
48                 eval $s;
49                 push @out, $@ if $@;
50         }
51     return @out;
52 }
53
54 sub Julian_Day
55 {
56         my $year = shift;
57         my $month = shift;
58         my $day = shift;
59         my $julianday;
60
61         $year=$year-1 if( $month <= 2 );
62         $month=$month+12 if( $month <= 2);
63
64         $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
65         return $julianday;
66 }
67 sub Julian_Date_of_Epoch
68 {
69         my $epoch=shift;
70         my $year=int($epoch*1e-3);
71         $year=$year+2000 if ($year < 57);
72         $year=$year+1900 if ($year >= 57);
73         my $day=$epoch-$year*1e3;
74         my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
75         return $Julian_Date_of_Epoch;
76 }
77 sub Julian_Date_of_Year
78 {
79         my $year=shift;
80         $year=$year-1;
81         my $A=int($year/100);
82         my $B=2-$A+int($A/4);
83         my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
84                 1720994.5+$B;
85         return $Julian_Date_of_Year;
86 }       
87 sub ThetaG_JD
88 {
89         my $jd=shift;
90         my $omega_E=1.00273790934; # earth rotations per sidereal day
91         my $secday=86400;
92         my $UT=($jd+0.5)-int($jd+0.5);
93         $jd=$jd-$UT;
94         my $TU=($jd-2451545.0)/36525;
95         my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
96         my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
97         return $thetag_jd;
98 }
99
100 sub reduce_angle_to_360
101 {
102         my $angle = shift;
103
104         $angle=$angle-int($angle/360)*360;
105         $angle=$angle+360 if( $angle < 0 );             
106         return $angle;
107 }
108 sub mod2p
109 {
110         my $twopi=$pi*2;
111         my $angle = shift;
112
113         $angle=$angle-int($angle/$twopi)*$twopi;
114         $angle=$angle+$twopi if( $angle < 0 );          
115         return $angle;
116 }
117 sub sindeg
118 {
119         my $angle_in_degrees = shift;
120
121         return sin($angle_in_degrees*$d2r);
122 }
123 sub cosdeg
124 {
125         my $angle_in_degrees = shift;
126
127         return cos($angle_in_degrees*$d2r);
128 }
129 sub tandeg
130 {
131         my $angle_in_degrees = shift;
132
133         return tan($angle_in_degrees*$d2r);
134 }
135 sub get_az_el
136 {
137         my $H=shift;
138         my $delta=shift;
139         my $lat=shift;
140
141         my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
142         my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
143         return ($az,$h);
144 }
145 sub rise_set
146 {
147         my $year = shift;
148         my $month = shift;
149         my $day = shift;
150         my $hr = shift;
151         my $min = shift;
152         my $lat = shift;
153         my $lon = shift;
154         my $sun0_moon1=shift;           # 0 for sun, 1 for moon, 2 for venus...
155
156         my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3);
157         my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr);
158         my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow);
159         
160         my $julianday=Julian_Day($year,$month,$day);
161         my $tt1 = ($julianday-1-2451545)/36525.;
162         my $tt2 = ($julianday-2451545)/36525.;
163         my $tt3 = ($julianday+1-2451545)/36525.;
164         my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
165
166         my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
167                 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
168         $theta0=reduce_angle_to_360($theta0);
169
170         my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
171                 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
172         $thetanow=reduce_angle_to_360($thetanow);
173
174         if ( $sun0_moon1 == 0 ) {
175                 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
176                 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
177                 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
178                 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
179                 $h0=-0.8333;
180                 $H=$thetanow-$lon-$alphanow;
181                 $H=reduce_angle_to_360($H);
182                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
183                 $hnow=$hnow +
184                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
185         }
186
187         if ( $sun0_moon1 == 1 ) {
188                 ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1);
189                 ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2);
190                 ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3);
191                 ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow);
192                 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.;
193                 $H=$thetanow-$lon-$alphanow;
194                 $H=reduce_angle_to_360($H);
195                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
196                 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
197                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
198         }
199
200         $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
201         $argtest = tandeg($lat)*tandeg($delta2);
202
203         if ( $argtest < -1. ) {
204                 return sprintf("Doesn't rise.");
205         }
206         if ( $argtest > 1. ) {
207                 return sprintf("Doesn't set.");
208         }
209
210         $H0 = acos($arg)*$r2d;
211         my $aa=$alpha2-$alpha1;
212         my $ba=$alpha3-$alpha2;
213         $aa=$aa+360 if ($aa < -180);
214         $aa=$aa-360 if ($aa >  180);
215         $ba=$ba+360 if ($ba < -180);
216         $ba=$ba-360 if ($ba >  180);
217         my $ca=$ba-$aa;
218
219         my $ad=$delta2-$delta1;
220         my $bd=$delta3-$delta2;
221         $ad=$ad+360 if ($ad < -180);
222         $ad=$ad-360 if ($ad >  180);
223         $bd=$bd+360 if ($bd < -180);
224         $bd=$bd-360 if ($bd >  180);
225         my $cd=$bd-$ad;
226
227         $m0 = ($alpha2 + $lon - $theta0)/360.;
228         $m0=$m0+1 if( $m0 < 0 );
229         $m0=$m0-1 if( $m0 > 1 );
230         for ($i=1; $i<=2; $i++) {       
231                 $theta = $theta0+360.985647*$m0;
232                 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
233                 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
234                 $H=$theta-$lon-$alpha;
235                 $H=reduce_angle_to_360($H);
236                 $H=$H-360 if ($H > 180);
237                 ($az,$h)=get_az_el($H,$delta,$lat);
238                 $corr=-$H/360;
239                 $m0=$m0+$corr;
240                 $m0=$m0+1 if( $m0 < 0 );
241                 $m0=$m0-1 if( $m0 > 1 );
242         }
243
244         $m1 = $m0 - $H0/360.;
245         $m1=$m1+1 if( $m1 < 0 );
246         $m1=$m1-1 if( $m1 > 1 );
247         for ($i=1; $i<=2; $i++) {
248                 $theta = $theta0+360.985647*$m1;
249                 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
250                 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
251                 $H=$theta-$lon-$alpha;
252                 $H=reduce_angle_to_360($H);
253                 ($az,$h)=get_az_el($H,$delta,$lat);
254                 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
255                 $m1=$m1+$corr;
256                 $m1=$m1+1 if( $m1 < 0 );
257                 $m1=$m1-1 if( $m1 > 1 );
258         }
259
260         $m2 = $m0 + $H0/360.;
261         $m2=$m2+1 if( $m2 < 0 );
262         $m2=$m2-1 if( $m2 > 1 );
263         for ($i=1; $i<=2; $i++) {
264                 $theta = $theta0+360.985647*$m2;
265                 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
266                 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
267                 $H=$theta-$lon-$alpha;
268                 $H=reduce_angle_to_360($H);
269                 ($az,$h)=get_az_el($H,$delta,$lat);
270                 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
271                 $m2 = $m2 + $corr;
272                 $m2=$m2+1 if( $m2 < 0 );
273                 $m2=$m2-1 if( $m2 > 1 );
274         }
275         my ($risehr,$risemin,$sethr,$setmin);
276         $risehr=int($m1*24);
277         $risemin=($m1*24-int($m1*24))*60+0.5;
278         if ( $risemin >= 60 ) {
279                 $risemin=$risemin-60;
280                 $risehr=$risehr+1;
281         }
282         $sethr=int($m2*24);
283         $setmin=($m2*24-int($m2*24))*60+0.5;
284         if ( $setmin >= 60 ) {
285                 $setmin=$setmin-60;
286                 $sethr=$sethr+1;
287         }
288
289         if ( $sun0_moon1 == 0 ) {
290                 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow);
291         }
292         if ( $sun0_moon1 == 1 ) {
293                 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin), 
294                                 $aznow+180,$hnow, -40*log10($distance/385000) );
295         }
296 }
297 sub get_moon_alpha_delta 
298 {
299         #
300         # Calculate the moon's right ascension and declination
301         #
302         my $tt=shift;
303
304         my $Lp=218.3164477+481267.88123421*$tt-
305                 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
306         $Lp=reduce_angle_to_360($Lp);
307
308         my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
309                 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
310         $D=reduce_angle_to_360($D);             
311
312         my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
313                 $tt*$tt*$tt/24490000.;
314         $M=reduce_angle_to_360($M);
315
316         my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
317                 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
318         $Mp=reduce_angle_to_360($Mp);
319
320         my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
321                 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
322         $F=reduce_angle_to_360($F);
323
324         my $A1 = 119.75 + 131.849 * $tt;
325         $A1=reduce_angle_to_360($A1);
326
327         my $A2 =  53.09 + 479264.290 * $tt;
328         $A2=reduce_angle_to_360($A2);
329
330         my $A3 = 313.45 + 481266.484 * $tt;
331         $A3=reduce_angle_to_360($A3);
332
333         my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
334
335         my $Sl=  6288774*sindeg(                  1 * $Mp          ) +
336                  1274027*sindeg(2 * $D +         -1 * $Mp          ) +
337                  658314 *sindeg(2 * $D                             ) +
338                  213618 *sindeg(                  2 * $Mp          ) +
339                 -185116 *sindeg(         1 * $M                    )*$E +
340                 -114332 *sindeg(                            2 * $F ) +
341                   58793 *sindeg(2 * $D +         -2 * $Mp          ) +
342                   57066 *sindeg(2 * $D - 1 * $M  -1 * $Mp          )*$E +
343                   53322 *sindeg(2 * $D +          1 * $Mp          ) +
344                   45758 *sindeg(2 * $D - 1 * $M                    )*$E +
345                  -40923 *sindeg(       + 1 * $M  -1 * $Mp          )*$E +
346                  -34720 *sindeg(1 * $D                             ) +
347                  -30383 *sindeg(       + 1 * $M + 1 * $Mp          )*$E +
348                   15327 *sindeg(2 * $D +                   -2 * $F ) +
349                  -12528 *sindeg(                  1 * $Mp + 2 * $F ) +
350                   10980 *sindeg(                  1 * $Mp - 2 * $F ) +
351                   10675 *sindeg(4 * $D +         -1 * $Mp          ) +
352                   10034 *sindeg(                  3 * $Mp          ) +
353                    8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
354                   -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
355                   -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
356                   -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
357                    4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
358                    4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
359                    3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
360                    3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
361                    3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
362                   -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
363                   -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
364                    2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
365                   -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
366                    2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
367                   -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
368                   -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
369                    2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
370                   -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
371                   -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
372                    1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
373                   -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
374                    -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
375                    -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
376                     759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
377                    -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
378                    -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
379                     691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
380                     596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
381                     549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
382                     537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
383                     520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
384                    -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
385                    -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
386                    -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
387                     351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
388                    -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
389                     330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
390                     327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
391                    -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
392                     299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
393                     294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
394                    3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
395
396         my $Sr=-20905355 *cosdeg(                   1 * $Mp          ) +
397                 -3699111 *cosdeg(2 * $D +          -1 * $Mp          ) +
398                 -2955968 *cosdeg(2 * $D                              ) +
399                  -569925 *cosdeg(                   2 * $Mp          ) +
400                    48888 *cosdeg(         1 * $M                     )*$E +
401                    -3149 *cosdeg(                             2 * $F ) + 
402                   246158 *cosdeg(2 * $D +          -2 * $Mp           ) +
403                  -152138 *cosdeg(2 * $D - 1 * $M   -1 * $Mp           )*$E +
404                  -170733 *cosdeg(2 * $D +           1 * $Mp           ) +
405                  -204586 *cosdeg(2 * $D - 1 * $M                      )*$E +
406                  -129620 *cosdeg(       + 1 * $M   -1 * $Mp           )*$E +
407                   108743 *cosdeg(1 * $D                              ) +
408                   104755 *cosdeg(       + 1 * $M  + 1 * $Mp           )*$E +
409                   10321 *cosdeg(2 * $D +                     -2 * $F ) +
410                   79661 *cosdeg(                   1 * $Mp -  2 * $F ) +
411                  -34782 *cosdeg(4 * $D +          -1 * $Mp           ) +
412                  -23210 *cosdeg(                   3 * $Mp           ) +
413                  -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
414                   24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
415                   30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
416                   -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
417                  -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
418                  -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
419                  -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
420                  -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
421                   14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
422                   -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
423                   10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
424                    6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
425                   -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
426                    5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
427                   -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
428                    4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
429                   -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
430                    3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
431                    2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
432                   -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
433                   -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
434                    2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
435                   -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
436                   -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
437                   -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
438                   -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
439                   -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
440                    1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
441                    8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
442
443         my $Sb=  5128122 *sindeg(                            1 * $F  ) +
444                   280602 *sindeg(                  1 * $Mp + 1 * $F  ) +
445                   277693 *sindeg(                  1 * $Mp - 1 * $F  ) +
446                   173237 *sindeg(2 * $D                    - 1 * $F  ) +
447                    55413 *sindeg(2 * $D           -1 * $Mp + 1 * $F  ) +
448                    46271 *sindeg(2 * $D +         -1 * $Mp - 1 * $F  ) +
449                    32573 *sindeg(2 * $D +                    1 * $F  ) +
450                    17198 *sindeg(                  2 * $Mp + 1 * $F  )+
451                     9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
452                     8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
453                     8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
454                     4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
455                     4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
456                    -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
457                     2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
458                     2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
459                     2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
460                    -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
461                     1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
462                    -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
463                    -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
464                    -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
465                    -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
466                    -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
467                    -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
468                    -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
469                    -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
470                     1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
471                     1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
472                      833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
473                      777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
474                      671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
475                      607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
476                      596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
477                      491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
478                     -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
479                      439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
480                      422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
481                      421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
482                     -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
483                     -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
484                      331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
485                      315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
486                      302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
487                     -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
488                     -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
489                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
490                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
491                     -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
492                     -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
493                     -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
494                      181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
495                     -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
496                      176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
497                      166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
498                     -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
499                      132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
500                     -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
501                      115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
502                      107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E  
503                    -2235 *sindeg($Lp) + 382*sindeg($A3) + 
504                      175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
505                      127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
506  
507         my $lambda=$Lp+$Sl/1000000.; 
508
509         my $beta=$Sb/1000000.;
510
511         my $distance=385000.56 + $Sr/1000.;
512
513         my $epsilon = 23+26./60.+21.448/(60.*60.);
514
515         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
516         $alpha = reduce_angle_to_360($alpha);
517
518         my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
519         $delta = reduce_angle_to_360($delta);
520
521         return ($alpha,$delta,$distance);
522 }
523  
524 sub get_sun_alpha_delta 
525 {
526 #
527 # Calculate Sun's right ascension and declination
528 #
529         my $tt = shift;
530
531         my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
532         $L0=reduce_angle_to_360($L0);
533
534         my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
535         $M=reduce_angle_to_360($M);
536
537         my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
538                 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
539                 0.000289*sindeg(3*$M);
540
541         my $OMEGA = 125.04 - 1934.136*$tt;
542         
543         my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA); 
544
545         my $epsilon = 23+26./60.+21.448/(60.*60.);
546
547         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
548         $alpha = reduce_angle_to_360($alpha);
549
550         my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
551         $delta = reduce_angle_to_360($delta);
552
553         return ($alpha,$delta);
554 }
555 sub get_satellite_pos
556 {
557 #
558 # This code was translated more-or-less directly from the Pascal
559 # routines contained in a report compiled by TS Kelso and based on:
560 # Spacetrack Report No. 3
561 # "Models for Propagation of NORAD Element Sets"
562 # Felix R. Hoots, Ronald L Roehrich
563 # December 1980
564 #
565 # See TS Kelso's web site for more details...
566 # Only the SGP propagation model is implemented. 
567 #
568 # Steve Franke, K9AN.   9 Dec 1999.
569
570 #
571 #NOAA 15
572 #1 25338U 98030A   99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
573 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382          
574 #TDRS 5
575 #1 21639U 91054B   99341.34471854  .00000095  00000-0  10000-3 0  4928
576 #2 21639   1.5957  88.4884 0003028 161.6582 135.4323  1.00277774 30562
577 #OSCAR 16 (PACSAT)
578 #1 20439U 90005D   99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
579 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297      
580 #
581 #Temporary keps database...
582 #
583         my $jtime = shift;
584         my $lat = shift;
585         my $lon = shift;
586         my $alt = shift;
587         my $satname = shift;
588         my $sat_ref = $keps{$satname};
589 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");   
590
591         my $qo=120;
592         my $so=78;
593         my $xj2=1.082616e-3;
594         my $xj3=-.253881e-5;
595         my $xj4=-1.65597e-6;
596         my $xke=.743669161e-1;
597         my $xkmper=6378.135;
598         my $xmnpda=1440.;
599         my $ae=1.;
600         my $ck2=.5*$xj2*$ae**2;
601         my $ck4=-.375*$xj4*$ae**4;
602         my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
603         my $s=$ae*(1+$so/$xkmper);
604
605         my $epoch = $sat_ref ->{epoch};
606 #printf("epoch = %10.2f\n",$epoch);
607         my $epoch_year=int($epoch/1000);
608         my $epoch_day=$epoch-int(1000*$epoch_year);
609 #printf("epoch_year = %10.2f\n",$epoch_year);
610 #printf("epoch_day = %17.12f\n",$epoch_day);
611         my $ep_year=$epoch_year+2000 if ($epoch_year < 57);
612         $ep_year=$epoch_year+1900 if ($epoch_year >= 57);
613         my $jt_epoch=Julian_Date_of_Year($ep_year);
614         $jt_epoch=$jt_epoch+$epoch_day;
615 #printf("JT for epoch = %17.12f\n",$jt_epoch);
616         my $tsince=($jtime-$jt_epoch)*24*60;
617 #printf("tsince (min) = %17.12f\n",$tsince);
618
619         my $mm1 = $sat_ref ->{mm1};
620         my $mm2 = $sat_ref ->{mm2};
621         my $bstar=$sat_ref ->{bstar};             # drag term for sgp4 model 
622         my $inclination=$sat_ref->{inclination};  # inclination in degrees
623         my $raan=$sat_ref->{raan};                # right ascension of ascending node in degs
624         my $eccentricity=$sat_ref ->{eccentricity};  # eccentricity - dimensionless
625         my $omegao=$sat_ref ->{argperigee};          # argument of perigee in degs
626         my $xmo=$sat_ref ->{meananomaly};            # mean anomaly in degrees
627         my $xno=$sat_ref ->{meanmotion};             # mean motion in revs per day
628
629 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
630 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
631         $raan=$raan*$d2r;
632         $omegao=$omegao*$d2r;
633         $xmo=$xmo*$d2r;
634         $inclination=$inclination*$d2r;
635         my $temp=2*$pi/$xmnpda/$xmnpda;
636         $xno=$xno*$temp*$xmnpda;
637         $mm1=$mm1*$temp;
638         $mm2=$mm2*$temp/$xmnpda;
639
640         my $c1=$ck2*1.5;
641         my $c2=$ck2/4.0;
642         my $c3=$ck2/2.0;
643         my $c4=$xj3*$ae**3/(4*$ck2);
644         my $cosio=cos($inclination);
645         my $sinio=sin($inclination);
646         my $a1=($xke/$xno)**(2./3.);
647         my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
648         my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
649         my $po=$ao*(1-$eccentricity*$eccentricity);
650         $qo=$ao*(1-$eccentricity);
651         my $xlo=$xmo+$omegao+$raan;
652         my $d10=$c3*$sinio*$sinio;
653         my $d20=$c2*(7.*$cosio*$cosio-1);
654         my $d30=$c1*$cosio;
655         my $d40=$d30*$sinio;
656         my $po2no=$xno/($po*$po);
657         my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
658         my $xnodot=-2.*$d30*$po2no;
659         my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
660         my $c6=$c4*$sinio;
661         
662         my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
663         $a=$ao*($xno/$a)**(2./3.);
664         my $e=1e-6;
665         $e =1-$qo/$a if ($a > $qo);
666         my $p=$a*(1-$e*$e);
667         my $xnodes=$raan+$xnodot*$tsince;
668         my $omgas=$omegao+$omgdt*$tsince;
669         my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
670
671         my $axnsl=$e*cos($omgas);
672         my $aynsl=$e*sin($omgas)-$c6/$p;
673         my $xl=mod2p($xls-$c5/$p*$axnsl);
674
675         my $u=mod2p($xl-$xnodes);
676         my $item3;
677         my $eo1=$u;
678         my $tem5=1;
679         my $coseo1=0;
680         my $sineo1=0;
681         for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
682         {
683                 $sineo1=sin($eo1);
684                 $coseo1=cos($eo1);
685                 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
686                 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
687                 my $tem2=abs($tem5);
688                 $tem5=$tem2/$tem5 if ($tem2 > 1);
689                 $eo1=$eo1+$tem5;
690         }
691
692         $sineo1=sin($eo1);
693         $coseo1=cos($eo1);
694         my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
695         my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
696         my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
697         my $pl=$a*(1-$el2);
698         my $pl2=$pl*$pl;
699         my $r=$a*(1-$ecose);
700         my $rdot=$xke*sqrt($a)/$r*$esine;
701         my $rvdot=$xke*sqrt($pl)/$r;
702         $temp=$esine/(1+sqrt(1-$el2));
703         my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
704         my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
705         my $su=atan2($sinu,$cosu);
706
707         my $sin2u=($cosu+$cosu)*$sinu;
708         my $cos2u=1-2*$sinu*$sinu;
709         my $rk=$r+$d10/$pl*$cos2u;
710         my $uk=$su-$d20/$pl2*$sin2u;
711         my $xnodek=$xnodes+$d30*$sin2u/$pl2;
712         my $xinck=$inclination+$d40/$pl2*$cos2u;
713
714         my $sinuk=sin($uk);
715         my $cosuk=cos($uk);
716         my $sinnok=sin($xnodek);
717         my $cosnok=cos($xnodek);
718         my $sinik=sin($xinck);
719         my $cosik=cos($xinck);
720         my $xmx=-$sinnok*$cosik;
721         my $xmy=$cosnok*$cosik;
722         my $ux=$xmx*$sinuk+$cosnok*$cosuk;
723         my $uy=$xmy*$sinuk+$sinnok*$cosuk;
724         my $uz=$sinik*$sinuk;
725         my $vx=$xmx*$cosuk-$cosnok*$sinuk;
726         my $vy=$xmy*$cosuk-$sinnok*$sinuk;
727         my $vz=$sinik*$cosuk;
728
729         my $x=$rk*$ux*$xkmper/$ae;
730         my $y=$rk*$uy*$xkmper/$ae;
731         my $z=$rk*$uz*$xkmper/$ae;
732         my $xdot=$rdot*$ux;
733         my $ydot=$rdot*$uy;
734         my $zdot=$rdot*$uz;
735         $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
736         $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
737         $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
738 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
739 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
740         my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
741         my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
742         return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
743 }
744
745 sub Calculate_LatLonAlt
746 {
747 #
748 # convert from ECI coordinates to latitude, longitude and altitude.
749 #
750         my $x=shift;
751         my $y=shift;
752         my $z=shift;
753         my $time=shift;
754
755         my $theta=atan2($y,$x);
756         my $lon=mod2p($theta-ThetaG_JD($time));
757         my $range=sqrt($x**2+$y**2);
758         my $f=1/298.26;      # earth flattening constant
759         my $e2=$f*(2-$f);
760         my $xkmper=6378.135;
761         my $lat=atan2($z,$range);
762         my ($phi,$c);
763         do
764         {
765                 $phi=$lat;
766                 $c=1/sqrt(1-$e2*sin($phi)**2);
767                 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
768         } until abs($lat-$phi) < 1e-10;
769         my $alt=$range/cos($lat)-$xkmper*$c;
770         return ($lat,$lon,$alt,$theta); # radians and kilometers
771         
772 }                       
773
774 sub Calculate_User_PosVel
775 {
776 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
777 # position and local hour angle.
778         my $lat=shift;
779         my $lon=shift;
780         my $alt=shift;
781         my $time=shift;
782         my $theta=mod2p(ThetaG_JD($time)+$lon);
783         my $omega_E=1.00273790934; # earth rotations per sidereal day
784         my $secday=86400;
785         my $mfactor=2*$pi*$omega_E/$secday;
786         my $f=1/298.26;      # earth flattening constant
787         my $xkmper=6378.135;
788         my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
789         my $s=(1-$f)*(1-$f)*$c;
790         my $achcp=($xkmper*$c+$alt)*cos($lat);
791         my $x_user=$achcp*cos($theta);
792         my $y_user=$achcp*sin($theta);
793         my $z_user=($xkmper*$s+$alt)*sin($lat);
794         my $xdot_user=-$mfactor*$y_user;
795         my $ydot_user=$mfactor*$x_user;
796         my $zdot_user=0;
797         return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
798 }
799 sub Calculate_Obs
800 {
801 # calculate the azimuth/el of an object as viewed from observers position
802 # with object position given in ECI coordinates and observer in lat/long/alt.
803 #
804 # inputs:       object ECI position vector (km)
805 #               object velocity vector (km/s)
806 #               julian time
807 #               observer lat,lon,altitude (km)
808         my $x=shift;
809         my $y=shift;
810         my $z=shift;
811         my $theta_s=shift;
812         my $xdot=shift; 
813         my $ydot=shift; 
814         my $zdot=shift; 
815         my $time=shift;
816         my $lat=shift;
817         my $lon=shift;
818         my $alt=shift;
819
820         my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
821                 Calculate_User_PosVel($lat,$lon,$alt,$time);
822         my $xx=$x-$x_o;
823         my $yy=$y-$y_o;
824         my $zz=$z-$z_o;
825         my $xxdot=$xdot-$xdot_o;
826         my $yydot=$ydot-$ydot_o;
827         my $zzdot=$zdot-$zdot_o;
828
829         my $sin_lat=sin($lat);
830         my $cos_lat=cos($lat);
831         my $sin_theta=sin($theta);
832         my $cos_theta=cos($theta);
833         
834         my $top_s=$sin_lat*$cos_theta*$xx
835                 + $sin_lat*$sin_theta*$yy
836                 - $cos_lat*$zz;
837
838         my $top_e=-$sin_theta*$xx
839                 + $cos_theta*$yy;
840
841         my $top_z=$cos_lat*$cos_theta*$xx
842                 + $cos_lat*$sin_theta*$yy
843                 + $sin_lat*$zz;
844
845         my $az=atan(-$top_e/$top_s);
846         $az=$az+$pi if ( $top_s > 0 );
847         $az=$az+2*$pi if ( $az < 0 );
848
849         my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
850         my $el=asin($top_z/$range);
851         return ($az, $el, $range);
852 }
853
854 sub Calendar_date_and_time_from_JD
855 {
856         my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
857         $jd=shift;
858         $jd=$jd+0.5;
859         $z=int($jd);
860         $frac=$jd-$z;
861         $alpha = int( ($z-1867216.5)/36524.25 );
862         $a=$z + 1 + $alpha - int($alpha/4);
863         $a=$z if( $z < 2299161 );
864         $b=$a+1524;
865         $c=int(($b-122.1)/365.25);
866         $d=int(365.25*$c);
867         $e=int(($b-$d)/30.6001);
868         $dom=$b-$d-int(30.6001*$e)+$frac;
869         $day=int($dom);
870         $mon=$e-1 if( $e < 14 );
871         $mon=$e-13 if( $e == 14 || $e == 15 );
872         $yr = $c-4716 if( $mon > 2 );
873         $yr = $c-4715 if( $mon == 1 || $mon == 2 );
874         $hr = int($frac*24);
875         $min= int(($frac*24 - $hr)*60+0.5);
876         if ($min == 60) {   # this may well prove inadequate DJK
877                 $hr += 1;
878                 $min = 0;
879         }
880         return ($yr,$mon,$day,$hr,$min);
881 }
882         
883
884