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