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