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