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