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