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