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