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 # 2001/09/15 some changes to take care of cases where the object
22 # doesn't rise or set on a given day...
30 @EXPORT = qw($pi $d2r $r2d );
34 use vars qw($VERSION $BRANCH);
35 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
36 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
37 $main::build += $VERSION;
38 $main::branch += $BRANCH;
40 use vars qw($pi $d2r $r2d);
51 # reload the keps data
55 my $s = readfilestr("$main::root/local/Keps.pm");
70 $year=$year-1 if( $month <= 2 );
71 $month=$month+12 if( $month <= 2);
73 $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
76 sub Julian_Date_of_Epoch
79 my $year=int($epoch*1e-3);
80 $year=$year+2000 if ($year < 57);
81 $year=$year+1900 if ($year >= 57);
82 my $day=$epoch-$year*1e3;
83 my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
84 return $Julian_Date_of_Epoch;
86 sub Julian_Date_of_Year
92 my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
94 return $Julian_Date_of_Year;
99 my $omega_E=1.00273790934; # earth rotations per sidereal day
101 my $UT=($jd+0.5)-int($jd+0.5);
103 my $TU=($jd-2451545.0)/36525;
104 my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
105 my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
109 sub reduce_angle_to_360
113 $angle=$angle-int($angle/360)*360;
114 $angle=$angle+360 if( $angle < 0 );
122 $angle=$angle-int($angle/$twopi)*$twopi;
123 $angle=$angle+$twopi if( $angle < 0 );
128 my $angle_in_degrees = shift;
130 return sin($angle_in_degrees*$d2r);
134 my $angle_in_degrees = shift;
136 return cos($angle_in_degrees*$d2r);
140 my $angle_in_degrees = shift;
142 return tan($angle_in_degrees*$d2r);
150 my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
151 my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
163 my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus...
167 my ($risehr,$risemin,$risetime,$sethr,$setmin,$settime);
169 my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3);
170 my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr);
171 my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow);
173 my $julianday=Julian_Day($year,$month,$day);
174 my $tt1 = ($julianday-1-2451545)/36525.;
175 my $tt2 = ($julianday-2451545)/36525.;
176 my $tt3 = ($julianday+1-2451545)/36525.;
177 my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
179 my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
180 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
181 $theta0=reduce_angle_to_360($theta0);
183 my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
184 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
185 $thetanow=reduce_angle_to_360($thetanow);
187 if ( $sun0_moon1 == 0 ) {
188 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
189 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
190 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
191 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
193 $H=$thetanow-$lon-$alphanow;
194 $H=reduce_angle_to_360($H);
195 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
197 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
200 if ( $sun0_moon1 == 1 ) {
201 ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1);
202 ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2);
203 ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3);
204 ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow);
205 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.;
206 $H=$thetanow-$lon-$alphanow;
207 $H=reduce_angle_to_360($H);
208 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
209 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
210 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
213 $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
214 if ( abs($arg) > 1. ) { # either up all day or down all day
215 $norise = 1; # leave it to the user to examine
216 $noset = 1; # the elevation angle (or look outside!)
217 } # to figure out which.
219 $H0 = acos($arg)*$r2d;
220 my $aa=$alpha2-$alpha1;
221 my $ba=$alpha3-$alpha2;
222 $aa=$aa+360 if ($aa < -180);
223 $aa=$aa-360 if ($aa > 180);
224 $ba=$ba+360 if ($ba < -180);
225 $ba=$ba-360 if ($ba > 180);
228 my $ad=$delta2-$delta1;
229 my $bd=$delta3-$delta2;
230 $ad=$ad+360 if ($ad < -180);
231 $ad=$ad-360 if ($ad > 180);
232 $bd=$bd+360 if ($bd < -180);
233 $bd=$bd-360 if ($bd > 180);
236 $m0 = ($alpha2 + $lon - $theta0)/360.;
237 $m0=$m0+1 if( $m0 < 0 );
238 $m0=$m0-1 if( $m0 > 1 );
239 for ($i=1; $i<=2; $i++) {
240 $theta = $theta0+360.985647*$m0;
241 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
242 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
243 $H=$theta-$lon-$alpha;
244 $H=reduce_angle_to_360($H);
245 $H=$H-360 if ($H > 180);
246 ($az,$h)=get_az_el($H,$delta,$lat);
249 $m0=$m0+1 if( $m0 < 0 );
250 $m0=$m0-1 if( $m0 > 1 );
255 $m1 = $m0 - $H0/360.;
256 $m1=$m1+1 if( $m1 < 0 );
257 $m1=$m1-1 if( $m1 > 1 );
258 for ($i=1; $i<=2; $i++) {
259 $theta = $theta0+360.985647*$m1;
260 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
261 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
262 $H=$theta-$lon-$alpha;
263 $H=reduce_angle_to_360($H);
264 ($az,$h)=get_az_el($H,$delta,$lat);
265 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
267 $norise=1 if( $m1 < 0 || $m1 > 1);
273 $risemin=($m1*24-int($m1*24))*60+0.5;
274 if ( $risemin >= 60 ) {
275 $risemin=$risemin-60;
278 $risetime=sprintf("%02d:%02dZ",$risehr,$risemin);
284 $m2 = $m0 + $H0/360.;
285 $m2=$m2+1 if( $m2 < 0 );
286 $m2=$m2-1 if( $m2 > 1 );
287 for ($i=1; $i<=2; $i++) {
288 $theta = $theta0+360.985647*$m2;
289 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
290 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
291 $H=$theta-$lon-$alpha;
292 $H=reduce_angle_to_360($H);
293 ($az,$h)=get_az_el($H,$delta,$lat);
294 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
296 $noset=1 if( $m2 < 0 || $m2 > 1);
302 $setmin=($m2*24-int($m2*24))*60+0.5;
303 if ( $setmin >= 60 ) {
307 $settime=sprintf("%02d:%02dZ",$sethr,$setmin);
313 if ( $sun0_moon1 == 0 ) {
314 return (sprintf("%s", $risetime), sprintf("%s",$settime),$aznow+180,$hnow);
316 if ( $sun0_moon1 == 1 ) {
317 return (sprintf("%s", $risetime), sprintf("%s",$settime),
318 $aznow+180,$hnow, -40*log10($distance/385000) );
321 sub get_moon_alpha_delta
324 # Calculate the moon's right ascension and declination
328 my $Lp=218.3164477+481267.88123421*$tt-
329 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
330 $Lp=reduce_angle_to_360($Lp);
332 my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
333 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
334 $D=reduce_angle_to_360($D);
336 my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
337 $tt*$tt*$tt/24490000.;
338 $M=reduce_angle_to_360($M);
340 my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
341 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
342 $Mp=reduce_angle_to_360($Mp);
344 my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
345 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
346 $F=reduce_angle_to_360($F);
348 my $A1 = 119.75 + 131.849 * $tt;
349 $A1=reduce_angle_to_360($A1);
351 my $A2 = 53.09 + 479264.290 * $tt;
352 $A2=reduce_angle_to_360($A2);
354 my $A3 = 313.45 + 481266.484 * $tt;
355 $A3=reduce_angle_to_360($A3);
357 my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
359 my $Sl= 6288774*sindeg( 1 * $Mp ) +
360 1274027*sindeg(2 * $D + -1 * $Mp ) +
361 658314 *sindeg(2 * $D ) +
362 213618 *sindeg( 2 * $Mp ) +
363 -185116 *sindeg( 1 * $M )*$E +
364 -114332 *sindeg( 2 * $F ) +
365 58793 *sindeg(2 * $D + -2 * $Mp ) +
366 57066 *sindeg(2 * $D - 1 * $M -1 * $Mp )*$E +
367 53322 *sindeg(2 * $D + 1 * $Mp ) +
368 45758 *sindeg(2 * $D - 1 * $M )*$E +
369 -40923 *sindeg( + 1 * $M -1 * $Mp )*$E +
370 -34720 *sindeg(1 * $D ) +
371 -30383 *sindeg( + 1 * $M + 1 * $Mp )*$E +
372 15327 *sindeg(2 * $D + -2 * $F ) +
373 -12528 *sindeg( 1 * $Mp + 2 * $F ) +
374 10980 *sindeg( 1 * $Mp - 2 * $F ) +
375 10675 *sindeg(4 * $D + -1 * $Mp ) +
376 10034 *sindeg( 3 * $Mp ) +
377 8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
378 -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
379 -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
380 -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
381 4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
382 4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
383 3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
384 3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
385 3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
386 -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
387 -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
388 2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
389 -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
390 2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
391 -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
392 -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
393 2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
394 -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
395 -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
396 1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
397 -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
398 -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
399 -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
400 759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
401 -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
402 -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
403 691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
404 596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
405 549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
406 537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
407 520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
408 -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
409 -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
410 -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
411 351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
412 -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
413 330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
414 327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
415 -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
416 299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
417 294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
418 3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
420 my $Sr=-20905355 *cosdeg( 1 * $Mp ) +
421 -3699111 *cosdeg(2 * $D + -1 * $Mp ) +
422 -2955968 *cosdeg(2 * $D ) +
423 -569925 *cosdeg( 2 * $Mp ) +
424 48888 *cosdeg( 1 * $M )*$E +
425 -3149 *cosdeg( 2 * $F ) +
426 246158 *cosdeg(2 * $D + -2 * $Mp ) +
427 -152138 *cosdeg(2 * $D - 1 * $M -1 * $Mp )*$E +
428 -170733 *cosdeg(2 * $D + 1 * $Mp ) +
429 -204586 *cosdeg(2 * $D - 1 * $M )*$E +
430 -129620 *cosdeg( + 1 * $M -1 * $Mp )*$E +
431 108743 *cosdeg(1 * $D ) +
432 104755 *cosdeg( + 1 * $M + 1 * $Mp )*$E +
433 10321 *cosdeg(2 * $D + -2 * $F ) +
434 79661 *cosdeg( 1 * $Mp - 2 * $F ) +
435 -34782 *cosdeg(4 * $D + -1 * $Mp ) +
436 -23210 *cosdeg( 3 * $Mp ) +
437 -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
438 24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
439 30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
440 -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
441 -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
442 -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
443 -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
444 -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
445 14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
446 -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
447 10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
448 6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
449 -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
450 5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
451 -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
452 4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
453 -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
454 3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
455 2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
456 -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
457 -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
458 2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
459 -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
460 -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
461 -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
462 -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
463 -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
464 1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
465 8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
467 my $Sb= 5128122 *sindeg( 1 * $F ) +
468 280602 *sindeg( 1 * $Mp + 1 * $F ) +
469 277693 *sindeg( 1 * $Mp - 1 * $F ) +
470 173237 *sindeg(2 * $D - 1 * $F ) +
471 55413 *sindeg(2 * $D -1 * $Mp + 1 * $F ) +
472 46271 *sindeg(2 * $D + -1 * $Mp - 1 * $F ) +
473 32573 *sindeg(2 * $D + 1 * $F ) +
474 17198 *sindeg( 2 * $Mp + 1 * $F )+
475 9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
476 8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
477 8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
478 4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
479 4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
480 -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
481 2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
482 2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
483 2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
484 -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
485 1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
486 -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
487 -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
488 -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
489 -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
490 -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
491 -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
492 -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
493 -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
494 1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
495 1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
496 833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
497 777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
498 671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
499 607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
500 596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
501 491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
502 -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
503 439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
504 422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
505 421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
506 -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
507 -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
508 331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
509 315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
510 302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
511 -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
512 -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
513 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
514 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
515 -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
516 -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
517 -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
518 181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
519 -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
520 176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
521 166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
522 -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
523 132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
524 -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
525 115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
526 107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E
527 -2235 *sindeg($Lp) + 382*sindeg($A3) +
528 175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
529 127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
531 my $lambda=$Lp+$Sl/1000000.;
533 my $beta=$Sb/1000000.;
535 my $distance=385000.56 + $Sr/1000.;
537 my $epsilon = 23+26./60.+21.448/(60.*60.);
539 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
540 $alpha = reduce_angle_to_360($alpha);
542 my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
543 $delta = reduce_angle_to_360($delta);
545 return ($alpha,$delta,$distance);
548 sub get_sun_alpha_delta
551 # Calculate Sun's right ascension and declination
555 my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
556 $L0=reduce_angle_to_360($L0);
558 my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
559 $M=reduce_angle_to_360($M);
561 my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
562 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
563 0.000289*sindeg(3*$M);
565 my $OMEGA = 125.04 - 1934.136*$tt;
567 my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA);
569 my $epsilon = 23+26./60.+21.448/(60.*60.);
571 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
572 $alpha = reduce_angle_to_360($alpha);
574 my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
575 $delta = reduce_angle_to_360($delta);
577 return ($alpha,$delta);
579 sub get_satellite_pos
582 # This code was translated more-or-less directly from the Pascal
583 # routines contained in a report compiled by TS Kelso and based on:
584 # Spacetrack Report No. 3
585 # "Models for Propagation of NORAD Element Sets"
586 # Felix R. Hoots, Ronald L Roehrich
589 # See TS Kelso's web site for more details...
590 # Only the SGP propagation model is implemented.
592 # Steve Franke, K9AN. 9 Dec 1999.
596 #1 25338U 98030A 99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
597 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382
599 #1 21639U 91054B 99341.34471854 .00000095 00000-0 10000-3 0 4928
600 #2 21639 1.5957 88.4884 0003028 161.6582 135.4323 1.00277774 30562
602 #1 20439U 90005D 99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
603 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297
605 #Temporary keps database...
612 my $sat_ref = $keps{$satname};
613 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");
620 my $xke=.743669161e-1;
624 my $ck2=.5*$xj2*$ae**2;
625 my $ck4=-.375*$xj4*$ae**4;
626 my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
627 my $s=$ae*(1+$so/$xkmper);
629 my $epoch = $sat_ref ->{epoch};
630 #printf("epoch = %10.2f\n",$epoch);
631 my $epoch_year=int($epoch/1000);
632 my $epoch_day=$epoch-int(1000*$epoch_year);
633 #printf("epoch_year = %10.2f\n",$epoch_year);
634 #printf("epoch_day = %17.12f\n",$epoch_day);
635 my $ep_year=$epoch_year+2000 if ($epoch_year < 57);
636 $ep_year=$epoch_year+1900 if ($epoch_year >= 57);
637 my $jt_epoch=Julian_Date_of_Year($ep_year);
638 $jt_epoch=$jt_epoch+$epoch_day;
639 #printf("JT for epoch = %17.12f\n",$jt_epoch);
640 my $tsince=($jtime-$jt_epoch)*24*60;
641 #printf("tsince (min) = %17.12f\n",$tsince);
643 my $mm1 = $sat_ref ->{mm1};
644 my $mm2 = $sat_ref ->{mm2};
645 my $bstar=$sat_ref ->{bstar}; # drag term for sgp4 model
646 my $inclination=$sat_ref->{inclination}; # inclination in degrees
647 my $raan=$sat_ref->{raan}; # right ascension of ascending node in degs
648 my $eccentricity=$sat_ref ->{eccentricity}; # eccentricity - dimensionless
649 my $omegao=$sat_ref ->{argperigee}; # argument of perigee in degs
650 my $xmo=$sat_ref ->{meananomaly}; # mean anomaly in degrees
651 my $xno=$sat_ref ->{meanmotion}; # mean motion in revs per day
653 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
654 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
656 $omegao=$omegao*$d2r;
658 $inclination=$inclination*$d2r;
659 my $temp=2*$pi/$xmnpda/$xmnpda;
660 $xno=$xno*$temp*$xmnpda;
662 $mm2=$mm2*$temp/$xmnpda;
667 my $c4=$xj3*$ae**3/(4*$ck2);
668 my $cosio=cos($inclination);
669 my $sinio=sin($inclination);
670 my $a1=($xke/$xno)**(2./3.);
671 my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
672 my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
673 my $po=$ao*(1-$eccentricity*$eccentricity);
674 $qo=$ao*(1-$eccentricity);
675 my $xlo=$xmo+$omegao+$raan;
676 my $d10=$c3*$sinio*$sinio;
677 my $d20=$c2*(7.*$cosio*$cosio-1);
680 my $po2no=$xno/($po*$po);
681 my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
682 my $xnodot=-2.*$d30*$po2no;
683 my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
686 my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
687 $a=$ao*($xno/$a)**(2./3.);
689 $e =1-$qo/$a if ($a > $qo);
691 my $xnodes=$raan+$xnodot*$tsince;
692 my $omgas=$omegao+$omgdt*$tsince;
693 my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
695 my $axnsl=$e*cos($omgas);
696 my $aynsl=$e*sin($omgas)-$c6/$p;
697 my $xl=mod2p($xls-$c5/$p*$axnsl);
699 my $u=mod2p($xl-$xnodes);
705 for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
709 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
710 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
712 $tem5=$tem2/$tem5 if ($tem2 > 1);
718 my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
719 my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
720 my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
724 my $rdot=$xke*sqrt($a)/$r*$esine;
725 my $rvdot=$xke*sqrt($pl)/$r;
726 $temp=$esine/(1+sqrt(1-$el2));
727 my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
728 my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
729 my $su=atan2($sinu,$cosu);
731 my $sin2u=($cosu+$cosu)*$sinu;
732 my $cos2u=1-2*$sinu*$sinu;
733 my $rk=$r+$d10/$pl*$cos2u;
734 my $uk=$su-$d20/$pl2*$sin2u;
735 my $xnodek=$xnodes+$d30*$sin2u/$pl2;
736 my $xinck=$inclination+$d40/$pl2*$cos2u;
740 my $sinnok=sin($xnodek);
741 my $cosnok=cos($xnodek);
742 my $sinik=sin($xinck);
743 my $cosik=cos($xinck);
744 my $xmx=-$sinnok*$cosik;
745 my $xmy=$cosnok*$cosik;
746 my $ux=$xmx*$sinuk+$cosnok*$cosuk;
747 my $uy=$xmy*$sinuk+$sinnok*$cosuk;
748 my $uz=$sinik*$sinuk;
749 my $vx=$xmx*$cosuk-$cosnok*$sinuk;
750 my $vy=$xmy*$cosuk-$sinnok*$sinuk;
751 my $vz=$sinik*$cosuk;
753 my $x=$rk*$ux*$xkmper/$ae;
754 my $y=$rk*$uy*$xkmper/$ae;
755 my $z=$rk*$uz*$xkmper/$ae;
759 $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
760 $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
761 $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
762 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
763 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
764 my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
765 my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
766 return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
769 sub Calculate_LatLonAlt
772 # convert from ECI coordinates to latitude, longitude and altitude.
779 my $theta=atan2($y,$x);
780 my $lon=mod2p($theta-ThetaG_JD($time));
781 my $range=sqrt($x**2+$y**2);
782 my $f=1/298.26; # earth flattening constant
785 my $lat=atan2($z,$range);
790 $c=1/sqrt(1-$e2*sin($phi)**2);
791 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
792 } until abs($lat-$phi) < 1e-10;
793 my $alt=$range/cos($lat)-$xkmper*$c;
794 return ($lat,$lon,$alt,$theta); # radians and kilometers
798 sub Calculate_User_PosVel
800 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
801 # position and local hour angle.
806 my $theta=mod2p(ThetaG_JD($time)+$lon);
807 my $omega_E=1.00273790934; # earth rotations per sidereal day
809 my $mfactor=2*$pi*$omega_E/$secday;
810 my $f=1/298.26; # earth flattening constant
812 my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
813 my $s=(1-$f)*(1-$f)*$c;
814 my $achcp=($xkmper*$c+$alt)*cos($lat);
815 my $x_user=$achcp*cos($theta);
816 my $y_user=$achcp*sin($theta);
817 my $z_user=($xkmper*$s+$alt)*sin($lat);
818 my $xdot_user=-$mfactor*$y_user;
819 my $ydot_user=$mfactor*$x_user;
821 return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
825 # calculate the azimuth/el of an object as viewed from observers position
826 # with object position given in ECI coordinates and observer in lat/long/alt.
828 # inputs: object ECI position vector (km)
829 # object velocity vector (km/s)
831 # observer lat,lon,altitude (km)
844 my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
845 Calculate_User_PosVel($lat,$lon,$alt,$time);
849 my $xxdot=$xdot-$xdot_o;
850 my $yydot=$ydot-$ydot_o;
851 my $zzdot=$zdot-$zdot_o;
853 my $sin_lat=sin($lat);
854 my $cos_lat=cos($lat);
855 my $sin_theta=sin($theta);
856 my $cos_theta=cos($theta);
858 my $top_s=$sin_lat*$cos_theta*$xx
859 + $sin_lat*$sin_theta*$yy
862 my $top_e=-$sin_theta*$xx
865 my $top_z=$cos_lat*$cos_theta*$xx
866 + $cos_lat*$sin_theta*$yy
869 my $az=atan(-$top_e/$top_s);
870 $az=$az+$pi if ( $top_s > 0 );
871 $az=$az+2*$pi if ( $az < 0 );
873 my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
874 my $el=asin($top_z/$range);
875 return ($az, $el, $range);
878 sub Calendar_date_and_time_from_JD
880 my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
885 $alpha = int( ($z-1867216.5)/36524.25 );
886 $a=$z + 1 + $alpha - int($alpha/4);
887 $a=$z if( $z < 2299161 );
889 $c=int(($b-122.1)/365.25);
891 $e=int(($b-$d)/30.6001);
892 $dom=$b-$d-int(30.6001*$e)+$frac;
894 $mon=$e-1 if( $e < 14 );
895 $mon=$e-13 if( $e == 14 || $e == 15 );
896 $yr = $c-4716 if( $mon > 2 );
897 $yr = $c-4715 if( $mon == 1 || $mon == 2 );
899 $min= int(($frac*24 - $hr)*60+0.5);
900 if ($min == 60) { # this may well prove inadequate DJK
904 return ($yr,$mon,$day,$hr,$min);