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
21 # 2005/02/25 add calculation of civil dawn and dusk, defined to be times
22 # when solar zenith angle is 96 degrees.
23 # 2001/12/16 Fixed Julian_Date_of_Epoch and now I actually use it...
24 # 2001/09/15 some changes to take care of cases where the object
25 # doesn't rise or set on a given day...
32 @EXPORT = qw($pi $d2r $r2d );
36 use vars qw($VERSION $BRANCH);
37 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
38 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
39 $main::build += $VERSION;
40 $main::branch += $BRANCH;
42 use vars qw($pi $d2r $r2d);
54 use POSIX qw(:math_h);
56 # reload the keps data
60 my $s = readfilestr("$main::root/local/Keps.pm");
75 $year=$year-1 if( $month <= 2 );
76 $month=$month+12 if( $month <= 2);
78 $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
81 sub Julian_Date_of_Epoch
84 my $year=int($epoch/1000);
85 my $day=$epoch-$year*1000;
92 my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
93 return $Julian_Date_of_Epoch;
96 sub Julian_Date_of_Year
100 my $A=int($year/100);
101 my $B=2-$A+int($A/4);
102 my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
104 return $Julian_Date_of_Year;
109 my $omega_E=1.00273790934; # earth rotations per sidereal day
111 my $UT=($jd+0.5)-int($jd+0.5);
113 my $TU=($jd-2451545.0)/36525;
114 my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
115 my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
119 sub reduce_angle_to_360
123 $angle=$angle-int($angle/360)*360;
124 $angle=$angle+360 if( $angle < 0 );
132 $angle=$angle-int($angle/$twopi)*$twopi;
133 $angle=$angle+$twopi if( $angle < 0 );
138 my $angle_in_degrees = shift;
140 return sin($angle_in_degrees*$d2r);
144 my $angle_in_degrees = shift;
146 return cos($angle_in_degrees*$d2r);
150 my $angle_in_degrees = shift;
152 return tan($angle_in_degrees*$d2r);
160 my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
161 my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
173 my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus...
174 my ($alpha1,$delta1,$alpha2,$delta2,$alpha3,$delta3);
175 my ($aznow,$hnow,$alphanow,$deltanow,$distance,$distancenow);
177 my ($risetime,$settime);
178 my ($dawntime,$dusktime);
180 my ($ifrac,$ifracnow);
182 my $julianday=Julian_Day($year,$month,$day);
183 my $tt1 = ($julianday-1-2451545)/36525.;
184 my $tt2 = ($julianday-2451545)/36525.;
185 my $tt3 = ($julianday+1-2451545)/36525.;
186 my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
188 my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
189 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
190 $theta0=reduce_angle_to_360($theta0);
192 my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
193 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
194 $thetanow=reduce_angle_to_360($thetanow);
196 if ( $sun0_moon1 == 0 ) {
197 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
198 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
199 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
200 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
201 $H=$thetanow-$lon-$alphanow;
202 $H=reduce_angle_to_360($H);
203 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
205 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
206 $h0=-0.8333; # this is for sun rise and sun set
207 ($risetime,$settime)=
208 do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
209 $alpha2,$delta2,$alpha3,$delta3);
210 $h0=-6.0; # this is for civil dawn and dusk
211 ($dawntime,$dusktime)=
212 do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
213 $alpha2,$delta2,$alpha3,$delta3);
214 $dawntime = "------" if( $dawntime eq "NoRise" );
215 $dusktime = "------" if( $dusktime eq "NoSet " );
218 sprintf("%s", $dawntime), sprintf("%s",$risetime),
219 sprintf("%s", $settime), sprintf("%s",$dusktime),
224 if ( $sun0_moon1 == 1 ) {
225 ($alpha1, $delta1, $distance, $ifrac)=get_moon_alpha_delta($tt1);
226 ($alpha2, $delta2, $distance, $ifrac)=get_moon_alpha_delta($tt2);
227 ($alpha3, $delta3, $distance, $ifrac)=get_moon_alpha_delta($tt3);
228 ($alphanow, $deltanow, $distancenow, $ifracnow)=get_moon_alpha_delta($ttnow);
229 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34.0/60.;
230 $H=$thetanow-$lon-$alphanow;
231 $H=reduce_angle_to_360($H);
232 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
233 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
234 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
235 ($risetime,$settime)=
236 do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
237 $alpha2,$delta2,$alpha3,$delta3);
238 return (sprintf("%s", $risetime), sprintf("%s",$settime),
239 $aznow+180,$hnow, -40*log10($distance/385000), $ifracnow );
245 sub do_rise_set_calculations
249 my ($risehr,$risemin,$risetime,$sethr,$setmin,$settime);
250 my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$corr);
251 my ($i,$arg,$argtest,$H0);
264 $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
265 if ( abs($arg) > 1. ) { # either up all day or down all day
266 $norise = 1; # leave it to the user to examine
267 $noset = 1; # the elevation angle (or look outside!)
268 } # to figure out which.
270 $H0 = acos($arg)*$r2d;
271 my $aa=$alpha2-$alpha1;
272 my $ba=$alpha3-$alpha2;
273 $aa=$aa+360 if ($aa < -180);
274 $aa=$aa-360 if ($aa > 180);
275 $ba=$ba+360 if ($ba < -180);
276 $ba=$ba-360 if ($ba > 180);
279 my $ad=$delta2-$delta1;
280 my $bd=$delta3-$delta2;
281 $ad=$ad+360 if ($ad < -180);
282 $ad=$ad-360 if ($ad > 180);
283 $bd=$bd+360 if ($bd < -180);
284 $bd=$bd-360 if ($bd > 180);
287 $m0 = ($alpha2 + $lon - $theta0)/360.;
288 $m0=$m0+1 if( $m0 < 0 );
289 $m0=$m0-1 if( $m0 > 1 );
290 for ($i=1; $i<=2; $i++) {
291 $theta = $theta0+360.985647*$m0;
292 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
293 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
294 $H=$theta-$lon-$alpha;
295 $H=reduce_angle_to_360($H);
296 $H=$H-360 if ($H > 180);
297 ($az,$h)=get_az_el($H,$delta,$lat);
300 $m0=$m0+1 if( $m0 < 0 );
301 $m0=$m0-1 if( $m0 >= 1 );
306 $m1 = $m0 - $H0/360.;
307 $m1=$m1+1 if( $m1 < 0 );
308 $m1=$m1-1 if( $m1 > 1 );
309 for ($i=1; $i<=2; $i++) {
310 $theta = $theta0+360.985647*$m1;
311 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
312 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
313 $H=$theta-$lon-$alpha;
314 $H=reduce_angle_to_360($H);
315 ($az,$h)=get_az_el($H,$delta,$lat);
316 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
318 # $norise=1 if( $m1 < 0 || $m1 > 1);
319 $m1=$m1-1 if( $m1 >= 1);
320 $m1=$m1+1 if( $m1 < 0);
326 $risemin=($m1*24-int($m1*24))*60+0.5;
327 if ( $risemin >= 60 ) {
328 $risemin=$risemin-60;
331 $risehr=0 if($risehr==24);
332 $risetime=sprintf("%02d:%02dZ",$risehr,$risemin);
338 $m2 = $m0 + $H0/360.;
339 $m2=$m2+1 if( $m2 < 0 );
340 $m2=$m2-1 if( $m2 >= 1 );
341 for ($i=1; $i<=2; $i++) {
342 $theta = $theta0+360.985647*$m2;
343 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
344 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
345 $H=$theta-$lon-$alpha;
346 $H=reduce_angle_to_360($H);
347 ($az,$h)=get_az_el($H,$delta,$lat);
348 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
350 # $noset=1 if( $m2 < 0 || $m2 > 1);
351 $m2=$m2-1 if( $m2 >= 1);
352 $m2=$m2+1 if( $m2 < 0);
358 $setmin=($m2*24-int($m2*24))*60+0.5;
359 if ( $setmin >= 60 ) {
363 $sethr=0 if($sethr==24);
364 $settime=sprintf("%02d:%02dZ",$sethr,$setmin);
368 return $risetime,$settime;
373 sub get_moon_alpha_delta
376 # Calculate the moon's right ascension and declination
378 # As of October 2001, also calculate the illuminated fraction of the
379 # moon's disk... (why not?)
383 my $Lp=218.3164477+481267.88123421*$tt-
384 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
385 $Lp=reduce_angle_to_360($Lp);
387 my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
388 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
389 $D=reduce_angle_to_360($D);
391 my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
392 $tt*$tt*$tt/24490000.;
393 $M=reduce_angle_to_360($M);
395 my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
396 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
397 $Mp=reduce_angle_to_360($Mp);
399 my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
400 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
401 $F=reduce_angle_to_360($F);
403 my $A1 = 119.75 + 131.849 * $tt;
404 $A1=reduce_angle_to_360($A1);
406 my $A2 = 53.09 + 479264.290 * $tt;
407 $A2=reduce_angle_to_360($A2);
409 my $A3 = 313.45 + 481266.484 * $tt;
410 $A3=reduce_angle_to_360($A3);
412 my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
414 my $Sl= 6288774*sindeg( 1 * $Mp ) +
415 1274027*sindeg(2 * $D + -1 * $Mp ) +
416 658314 *sindeg(2 * $D ) +
417 213618 *sindeg( 2 * $Mp ) +
418 -185116 *sindeg( 1 * $M )*$E +
419 -114332 *sindeg( 2 * $F ) +
420 58793 *sindeg(2 * $D + -2 * $Mp ) +
421 57066 *sindeg(2 * $D - 1 * $M -1 * $Mp )*$E +
422 53322 *sindeg(2 * $D + 1 * $Mp ) +
423 45758 *sindeg(2 * $D - 1 * $M )*$E +
424 -40923 *sindeg( + 1 * $M -1 * $Mp )*$E +
425 -34720 *sindeg(1 * $D ) +
426 -30383 *sindeg( + 1 * $M + 1 * $Mp )*$E +
427 15327 *sindeg(2 * $D + -2 * $F ) +
428 -12528 *sindeg( 1 * $Mp + 2 * $F ) +
429 10980 *sindeg( 1 * $Mp - 2 * $F ) +
430 10675 *sindeg(4 * $D + -1 * $Mp ) +
431 10034 *sindeg( 3 * $Mp ) +
432 8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
433 -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
434 -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
435 -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
436 4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
437 4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
438 3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
439 3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
440 3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
441 -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
442 -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
443 2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
444 -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
445 2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
446 -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
447 -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
448 2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
449 -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
450 -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
451 1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
452 -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
453 -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
454 -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
455 759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
456 -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
457 -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
458 691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
459 596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
460 549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
461 537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
462 520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
463 -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
464 -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
465 -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
466 351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
467 -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
468 330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
469 327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
470 -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
471 299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
472 294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
473 3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
475 my $Sr=-20905355 *cosdeg( 1 * $Mp ) +
476 -3699111 *cosdeg(2 * $D + -1 * $Mp ) +
477 -2955968 *cosdeg(2 * $D ) +
478 -569925 *cosdeg( 2 * $Mp ) +
479 48888 *cosdeg( 1 * $M )*$E +
480 -3149 *cosdeg( 2 * $F ) +
481 246158 *cosdeg(2 * $D + -2 * $Mp ) +
482 -152138 *cosdeg(2 * $D - 1 * $M -1 * $Mp )*$E +
483 -170733 *cosdeg(2 * $D + 1 * $Mp ) +
484 -204586 *cosdeg(2 * $D - 1 * $M )*$E +
485 -129620 *cosdeg( + 1 * $M -1 * $Mp )*$E +
486 108743 *cosdeg(1 * $D ) +
487 104755 *cosdeg( + 1 * $M + 1 * $Mp )*$E +
488 10321 *cosdeg(2 * $D + -2 * $F ) +
489 79661 *cosdeg( 1 * $Mp - 2 * $F ) +
490 -34782 *cosdeg(4 * $D + -1 * $Mp ) +
491 -23210 *cosdeg( 3 * $Mp ) +
492 -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
493 24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
494 30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
495 -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
496 -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
497 -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
498 -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
499 -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
500 14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
501 -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
502 10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
503 6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
504 -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
505 5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
506 -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
507 4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
508 -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
509 3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
510 2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
511 -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
512 -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
513 2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
514 -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
515 -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
516 -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
517 -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
518 -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
519 1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
520 8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
522 my $Sb= 5128122 *sindeg( 1 * $F ) +
523 280602 *sindeg( 1 * $Mp + 1 * $F ) +
524 277693 *sindeg( 1 * $Mp - 1 * $F ) +
525 173237 *sindeg(2 * $D - 1 * $F ) +
526 55413 *sindeg(2 * $D -1 * $Mp + 1 * $F ) +
527 46271 *sindeg(2 * $D + -1 * $Mp - 1 * $F ) +
528 32573 *sindeg(2 * $D + 1 * $F ) +
529 17198 *sindeg( 2 * $Mp + 1 * $F )+
530 9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
531 8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
532 8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
533 4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
534 4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
535 -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
536 2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
537 2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
538 2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
539 -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
540 1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
541 -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
542 -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
543 -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
544 -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
545 -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
546 -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
547 -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
548 -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
549 1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
550 1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
551 833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
552 777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
553 671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
554 607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
555 596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
556 491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
557 -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
558 439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
559 422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
560 421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
561 -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
562 -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
563 331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
564 315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
565 302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
566 -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
567 -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
568 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
569 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
570 -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
571 -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
572 -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
573 181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
574 -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
575 176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
576 166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
577 -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
578 132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
579 -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
580 115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
581 107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E
582 -2235 *sindeg($Lp) + 382*sindeg($A3) +
583 175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
584 127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
586 my $lambda=$Lp+$Sl/1000000.;
588 my $beta=$Sb/1000000.;
590 my $distance=385000.56 + $Sr/1000.;
592 my $epsilon = 23+26./60.+21.448/(60.*60.);
594 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
595 $alpha = reduce_angle_to_360($alpha);
597 my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
598 $delta = reduce_angle_to_360($delta);
600 # $phase will be the "moon phase angle" from p. 346 of Meeus' book...
601 my $phase=180.0 - $D - 6.289 *sindeg($Mp)
603 - 1.274 *sindeg(2.*$D - $Mp)
604 - 0.658 *sindeg(2.*$D)
605 - 0.214 *sindeg(2.*$Mp)
608 # $illum_frac is the fraction of the disk that is illuminated, and will be
609 # zero at new moon and 1.0 at full moon.
611 my $illum_frac = (1.0 + cosdeg( $phase ))/2.;
613 return ($alpha,$delta,$distance,$illum_frac);
616 sub get_sun_alpha_delta
619 # Calculate Sun's right ascension and declination
623 my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
624 $L0=reduce_angle_to_360($L0);
626 my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
627 $M=reduce_angle_to_360($M);
629 my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
630 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
631 0.000289*sindeg(3*$M);
633 my $OMEGA = 125.04 - 1934.136*$tt;
635 my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA);
637 my $epsilon = 23+26./60.+21.448/(60.*60.);
639 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
640 $alpha = reduce_angle_to_360($alpha);
642 my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
643 $delta = reduce_angle_to_360($delta);
645 return ($alpha,$delta);
647 sub get_satellite_pos
650 # This code was translated more-or-less directly from the Pascal
651 # routines contained in a report compiled by TS Kelso and based on:
652 # Spacetrack Report No. 3
653 # "Models for Propagation of NORAD Element Sets"
654 # Felix R. Hoots, Ronald L Roehrich
657 # See TS Kelso's web site for more details...
658 # Only the SGP propagation model is implemented.
660 # Steve Franke, K9AN. 9 Dec 1999.
664 #1 25338U 98030A 99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
665 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382
667 #1 21639U 91054B 99341.34471854 .00000095 00000-0 10000-3 0 4928
668 #2 21639 1.5957 88.4884 0003028 161.6582 135.4323 1.00277774 30562
670 #1 20439U 90005D 99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
671 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297
673 #Temporary keps database...
680 my $sat_ref = $keps{$satname};
681 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");
688 my $xke=.743669161e-1;
692 my $ck2=.5*$xj2*$ae**2;
693 my $ck4=-.375*$xj4*$ae**4;
694 my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
695 my $s=$ae*(1+$so/$xkmper);
697 my $epoch = $sat_ref ->{epoch};
698 #printf("epoch = %10.2f\n",$epoch);
699 my $jt_epoch=Julian_Date_of_Epoch($epoch);
700 #printf("JT for epoch = %17.12f\n",$jt_epoch);
701 my $tsince=($jtime-$jt_epoch)*24*60;
702 #printf("tsince (min) = %17.12f\n",$tsince);
704 my $mm1 = $sat_ref ->{mm1};
705 my $mm2 = $sat_ref ->{mm2};
706 my $bstar=$sat_ref ->{bstar}; # drag term for sgp4 model
707 my $inclination=$sat_ref->{inclination}; # inclination in degrees
708 my $raan=$sat_ref->{raan}; # right ascension of ascending node in degs
709 my $eccentricity=$sat_ref ->{eccentricity}; # eccentricity - dimensionless
710 my $omegao=$sat_ref ->{argperigee}; # argument of perigee in degs
711 my $xmo=$sat_ref ->{meananomaly}; # mean anomaly in degrees
712 my $xno=$sat_ref ->{meanmotion}; # mean motion in revs per day
714 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
715 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
717 $omegao=$omegao*$d2r;
719 $inclination=$inclination*$d2r;
720 my $temp=2*$pi/$xmnpda/$xmnpda;
721 $xno=$xno*$temp*$xmnpda;
723 $mm2=$mm2*$temp/$xmnpda;
728 my $c4=$xj3*$ae**3/(4*$ck2);
729 my $cosio=cos($inclination);
730 my $sinio=sin($inclination);
731 my $a1=($xke/$xno)**(2./3.);
732 my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
733 my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
734 my $po=$ao*(1-$eccentricity*$eccentricity);
735 $qo=$ao*(1-$eccentricity);
736 my $xlo=$xmo+$omegao+$raan;
737 my $d10=$c3*$sinio*$sinio;
738 my $d20=$c2*(7.*$cosio*$cosio-1);
741 my $po2no=$xno/($po*$po);
742 my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
743 my $xnodot=-2.*$d30*$po2no;
744 my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
747 my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
748 $a=$ao*($xno/$a)**(2./3.);
750 $e =1-$qo/$a if ($a > $qo);
752 my $xnodes=$raan+$xnodot*$tsince;
753 my $omgas=$omegao+$omgdt*$tsince;
754 my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
756 my $axnsl=$e*cos($omgas);
757 my $aynsl=$e*sin($omgas)-$c6/$p;
758 my $xl=mod2p($xls-$c5/$p*$axnsl);
760 my $u=mod2p($xl-$xnodes);
766 for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
770 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
771 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
773 $tem5=$tem2/$tem5 if ($tem2 > 1);
779 my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
780 my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
781 my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
785 my $rdot=$xke*sqrt($a)/$r*$esine;
786 my $rvdot=$xke*sqrt($pl)/$r;
787 $temp=$esine/(1+sqrt(1-$el2));
788 my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
789 my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
790 my $su=atan2($sinu,$cosu);
792 my $sin2u=($cosu+$cosu)*$sinu;
793 my $cos2u=1-2*$sinu*$sinu;
794 my $rk=$r+$d10/$pl*$cos2u;
795 my $uk=$su-$d20/$pl2*$sin2u;
796 my $xnodek=$xnodes+$d30*$sin2u/$pl2;
797 my $xinck=$inclination+$d40/$pl2*$cos2u;
801 my $sinnok=sin($xnodek);
802 my $cosnok=cos($xnodek);
803 my $sinik=sin($xinck);
804 my $cosik=cos($xinck);
805 my $xmx=-$sinnok*$cosik;
806 my $xmy=$cosnok*$cosik;
807 my $ux=$xmx*$sinuk+$cosnok*$cosuk;
808 my $uy=$xmy*$sinuk+$sinnok*$cosuk;
809 my $uz=$sinik*$sinuk;
810 my $vx=$xmx*$cosuk-$cosnok*$sinuk;
811 my $vy=$xmy*$cosuk-$sinnok*$sinuk;
812 my $vz=$sinik*$cosuk;
814 my $x=$rk*$ux*$xkmper/$ae;
815 my $y=$rk*$uy*$xkmper/$ae;
816 my $z=$rk*$uz*$xkmper/$ae;
820 $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
821 $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
822 $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
823 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
824 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
825 my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
826 my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
827 return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
830 sub Calculate_LatLonAlt
833 # convert from ECI coordinates to latitude, longitude and altitude.
840 my $theta=atan2($y,$x);
841 my $lon=mod2p($theta-ThetaG_JD($time));
842 my $range=sqrt($x**2+$y**2);
843 my $f=1/298.26; # earth flattening constant
846 my $lat=atan2($z,$range);
851 $c=1/sqrt(1-$e2*sin($phi)**2);
852 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
853 } until abs($lat-$phi) < 1e-10;
854 my $alt=$range/cos($lat)-$xkmper*$c;
855 return ($lat,$lon,$alt,$theta); # radians and kilometers
859 sub Calculate_User_PosVel
861 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
862 # position and local hour angle.
867 my $theta=mod2p(ThetaG_JD($time)+$lon);
868 my $omega_E=1.00273790934; # earth rotations per sidereal day
870 my $mfactor=2*$pi*$omega_E/$secday;
871 my $f=1/298.26; # earth flattening constant
873 my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
874 my $s=(1-$f)*(1-$f)*$c;
875 my $achcp=($xkmper*$c+$alt)*cos($lat);
876 my $x_user=$achcp*cos($theta);
877 my $y_user=$achcp*sin($theta);
878 my $z_user=($xkmper*$s+$alt)*sin($lat);
879 my $xdot_user=-$mfactor*$y_user;
880 my $ydot_user=$mfactor*$x_user;
882 return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
886 # calculate the azimuth/el of an object as viewed from observers position
887 # with object position given in ECI coordinates and observer in lat/long/alt.
889 # inputs: object ECI position vector (km)
890 # object velocity vector (km/s)
892 # observer lat,lon,altitude (km)
905 my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
906 Calculate_User_PosVel($lat,$lon,$alt,$time);
910 my $xxdot=$xdot-$xdot_o;
911 my $yydot=$ydot-$ydot_o;
912 my $zzdot=$zdot-$zdot_o;
914 my $sin_lat=sin($lat);
915 my $cos_lat=cos($lat);
916 my $sin_theta=sin($theta);
917 my $cos_theta=cos($theta);
919 my $top_s=$sin_lat*$cos_theta*$xx
920 + $sin_lat*$sin_theta*$yy
923 my $top_e=-$sin_theta*$xx
926 my $top_z=$cos_lat*$cos_theta*$xx
927 + $cos_lat*$sin_theta*$yy
930 my $az=atan(-$top_e/$top_s);
931 $az=$az+$pi if ( $top_s > 0 );
932 $az=$az+2*$pi if ( $az < 0 );
934 my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
935 my $el=asin($top_z/$range);
936 return ($az, $el, $range);
939 sub Calendar_date_and_time_from_JD
941 my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
946 $alpha = int( ($z-1867216.5)/36524.25 );
947 $a=$z + 1 + $alpha - int($alpha/4);
948 $a=$z if( $z < 2299161 );
950 $c=int(($b-122.1)/365.25);
952 $e=int(($b-$d)/30.6001);
953 $dom=$b-$d-int(30.6001*$e)+$frac;
955 $mon=$e-1 if( $e < 14 );
956 $mon=$e-13 if( $e == 14 || $e == 15 );
957 $yr = $c-4716 if( $mon > 2 );
958 $yr = $c-4715 if( $mon == 1 || $mon == 2 );
960 $min= int(($frac*24 - $hr)*60+0.5);
961 if ($min == 60) { # this may well prove inadequate DJK
965 return ($yr,$mon,$day,$hr,$min);