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