3 # This module was written by Steve Franke K9AN.
6 # The formulas used in this module
8 # Astronomical Algorithms, Second Edition
10 # Published by Willmann-Bell, Inc.
11 # P.O. Box 35025, Richmond, Virginia 23235
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.
17 # Copyright (c) 1999 - Steve Franke K9AN
28 @EXPORT = qw($pi $d2r $r2d );
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;
38 use vars qw($pi $d2r $r2d);
49 # reload the keps data
53 my $s = readfilestr("$main::root/local/Keps.pm");
68 $year=$year-1 if( $month <= 2 );
69 $month=$month+12 if( $month <= 2);
71 $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
74 sub Julian_Date_of_Epoch
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;
84 sub Julian_Date_of_Year
90 my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
92 return $Julian_Date_of_Year;
97 my $omega_E=1.00273790934; # earth rotations per sidereal day
99 my $UT=($jd+0.5)-int($jd+0.5);
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));
107 sub reduce_angle_to_360
111 $angle=$angle-int($angle/360)*360;
112 $angle=$angle+360 if( $angle < 0 );
120 $angle=$angle-int($angle/$twopi)*$twopi;
121 $angle=$angle+$twopi if( $angle < 0 );
126 my $angle_in_degrees = shift;
128 return sin($angle_in_degrees*$d2r);
132 my $angle_in_degrees = shift;
134 return cos($angle_in_degrees*$d2r);
138 my $angle_in_degrees = shift;
140 return tan($angle_in_degrees*$d2r);
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) );
161 my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus...
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);
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.;
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);
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);
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);
187 $H=$thetanow-$lon-$alphanow;
188 $H=reduce_angle_to_360($H);
189 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
191 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
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;
207 $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
208 $argtest = tandeg($lat)*tandeg($delta2);
210 if ( $argtest < -1. ) {
211 return sprintf("Doesn't rise.");
213 if ( $argtest > 1. ) {
214 return sprintf("Doesn't set.");
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);
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);
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);
247 $m0=$m0+1 if( $m0 < 0 );
248 $m0=$m0-1 if( $m0 > 1 );
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)));
263 $m1=$m1+1 if( $m1 < 0 );
264 $m1=$m1-1 if( $m1 > 1 );
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)));
279 $m2=$m2+1 if( $m2 < 0 );
280 $m2=$m2-1 if( $m2 > 1 );
282 my ($risehr,$risemin,$sethr,$setmin);
284 $risemin=($m1*24-int($m1*24))*60+0.5;
285 if ( $risemin >= 60 ) {
286 $risemin=$risemin-60;
290 $setmin=($m2*24-int($m2*24))*60+0.5;
291 if ( $setmin >= 60 ) {
296 if ( $sun0_moon1 == 0 ) {
297 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow);
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) );
304 sub get_moon_alpha_delta
307 # Calculate the moon's right ascension and declination
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);
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);
319 my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
320 $tt*$tt*$tt/24490000.;
321 $M=reduce_angle_to_360($M);
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);
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);
331 my $A1 = 119.75 + 131.849 * $tt;
332 $A1=reduce_angle_to_360($A1);
334 my $A2 = 53.09 + 479264.290 * $tt;
335 $A2=reduce_angle_to_360($A2);
337 my $A3 = 313.45 + 481266.484 * $tt;
338 $A3=reduce_angle_to_360($A3);
340 my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
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);
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 );
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);
514 my $lambda=$Lp+$Sl/1000000.;
516 my $beta=$Sb/1000000.;
518 my $distance=385000.56 + $Sr/1000.;
520 my $epsilon = 23+26./60.+21.448/(60.*60.);
522 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
523 $alpha = reduce_angle_to_360($alpha);
525 my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
526 $delta = reduce_angle_to_360($delta);
528 return ($alpha,$delta,$distance);
531 sub get_sun_alpha_delta
534 # Calculate Sun's right ascension and declination
538 my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
539 $L0=reduce_angle_to_360($L0);
541 my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
542 $M=reduce_angle_to_360($M);
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);
548 my $OMEGA = 125.04 - 1934.136*$tt;
550 my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA);
552 my $epsilon = 23+26./60.+21.448/(60.*60.);
554 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
555 $alpha = reduce_angle_to_360($alpha);
557 my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
558 $delta = reduce_angle_to_360($delta);
560 return ($alpha,$delta);
562 sub get_satellite_pos
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
572 # See TS Kelso's web site for more details...
573 # Only the SGP propagation model is implemented.
575 # Steve Franke, K9AN. 9 Dec 1999.
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
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
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
588 #Temporary keps database...
595 my $sat_ref = $keps{$satname};
596 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");
603 my $xke=.743669161e-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);
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);
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
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);
639 $omegao=$omegao*$d2r;
641 $inclination=$inclination*$d2r;
642 my $temp=2*$pi/$xmnpda/$xmnpda;
643 $xno=$xno*$temp*$xmnpda;
645 $mm2=$mm2*$temp/$xmnpda;
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);
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);
669 my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
670 $a=$ao*($xno/$a)**(2./3.);
672 $e =1-$qo/$a if ($a > $qo);
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);
678 my $axnsl=$e*cos($omgas);
679 my $aynsl=$e*sin($omgas)-$c6/$p;
680 my $xl=mod2p($xls-$c5/$p*$axnsl);
682 my $u=mod2p($xl-$xnodes);
688 for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
692 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
693 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
695 $tem5=$tem2/$tem5 if ($tem2 > 1);
701 my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
702 my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
703 my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
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);
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;
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;
736 my $x=$rk*$ux*$xkmper/$ae;
737 my $y=$rk*$uy*$xkmper/$ae;
738 my $z=$rk*$uz*$xkmper/$ae;
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);
752 sub Calculate_LatLonAlt
755 # convert from ECI coordinates to latitude, longitude and altitude.
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
768 my $lat=atan2($z,$range);
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
781 sub Calculate_User_PosVel
783 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
784 # position and local hour angle.
789 my $theta=mod2p(ThetaG_JD($time)+$lon);
790 my $omega_E=1.00273790934; # earth rotations per sidereal day
792 my $mfactor=2*$pi*$omega_E/$secday;
793 my $f=1/298.26; # earth flattening constant
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;
804 return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
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.
811 # inputs: object ECI position vector (km)
812 # object velocity vector (km/s)
814 # observer lat,lon,altitude (km)
827 my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
828 Calculate_User_PosVel($lat,$lon,$alt,$time);
832 my $xxdot=$xdot-$xdot_o;
833 my $yydot=$ydot-$ydot_o;
834 my $zzdot=$zdot-$zdot_o;
836 my $sin_lat=sin($lat);
837 my $cos_lat=cos($lat);
838 my $sin_theta=sin($theta);
839 my $cos_theta=cos($theta);
841 my $top_s=$sin_lat*$cos_theta*$xx
842 + $sin_lat*$sin_theta*$yy
845 my $top_e=-$sin_theta*$xx
848 my $top_z=$cos_lat*$cos_theta*$xx
849 + $cos_lat*$sin_theta*$yy
852 my $az=atan(-$top_e/$top_s);
853 $az=$az+$pi if ( $top_s > 0 );
854 $az=$az+2*$pi if ( $az < 0 );
856 my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
857 my $el=asin($top_z/$range);
858 return ($az, $el, $range);
861 sub Calendar_date_and_time_from_JD
863 my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
868 $alpha = int( ($z-1867216.5)/36524.25 );
869 $a=$z + 1 + $alpha - int($alpha/4);
870 $a=$z if( $z < 2299161 );
872 $c=int(($b-122.1)/365.25);
874 $e=int(($b-$d)/30.6001);
875 $dom=$b-$d-int(30.6001*$e)+$frac;
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 );
882 $min= int(($frac*24 - $hr)*60+0.5);
883 if ($min == 60) { # this may well prove inadequate DJK
887 return ($yr,$mon,$day,$hr,$min);