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 );
31 use vars qw($pi $d2r $r2d);
42 # reload the keps data
46 my $s = readfilestr("$main::root/local/Keps.pm");
61 $year=$year-1 if( $month <= 2 );
62 $month=$month+12 if( $month <= 2);
64 $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
67 sub Julian_Date_of_Epoch
70 my $year=int($epoch*1e-3);
71 $year=$year+2000 if ($year < 57);
72 $year=$year+1900 if ($year >= 57);
73 my $day=$epoch-$year*1e3;
74 my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
75 return $Julian_Date_of_Epoch;
77 sub Julian_Date_of_Year
83 my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
85 return $Julian_Date_of_Year;
90 my $omega_E=1.00273790934; # earth rotations per sidereal day
92 my $UT=($jd+0.5)-int($jd+0.5);
94 my $TU=($jd-2451545.0)/36525;
95 my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
96 my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
100 sub reduce_angle_to_360
104 $angle=$angle-int($angle/360)*360;
105 $angle=$angle+360 if( $angle < 0 );
113 $angle=$angle-int($angle/$twopi)*$twopi;
114 $angle=$angle+$twopi if( $angle < 0 );
119 my $angle_in_degrees = shift;
121 return sin($angle_in_degrees*$d2r);
125 my $angle_in_degrees = shift;
127 return cos($angle_in_degrees*$d2r);
131 my $angle_in_degrees = shift;
133 return tan($angle_in_degrees*$d2r);
141 my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
142 my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
154 my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus...
156 my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3);
157 my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr);
158 my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow);
160 my $julianday=Julian_Day($year,$month,$day);
161 my $tt1 = ($julianday-1-2451545)/36525.;
162 my $tt2 = ($julianday-2451545)/36525.;
163 my $tt3 = ($julianday+1-2451545)/36525.;
164 my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
166 my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
167 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
168 $theta0=reduce_angle_to_360($theta0);
170 my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
171 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
172 $thetanow=reduce_angle_to_360($thetanow);
174 if ( $sun0_moon1 == 0 ) {
175 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
176 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
177 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
178 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
180 $H=$thetanow-$lon-$alphanow;
181 $H=reduce_angle_to_360($H);
182 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
184 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
187 if ( $sun0_moon1 == 1 ) {
188 ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1);
189 ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2);
190 ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3);
191 ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow);
192 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.;
193 $H=$thetanow-$lon-$alphanow;
194 $H=reduce_angle_to_360($H);
195 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
196 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
197 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
200 $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
201 $argtest = tandeg($lat)*tandeg($delta2);
203 if ( $argtest < -1. ) {
204 return sprintf("Doesn't rise.");
206 if ( $argtest > 1. ) {
207 return sprintf("Doesn't set.");
210 $H0 = acos($arg)*$r2d;
211 my $aa=$alpha2-$alpha1;
212 my $ba=$alpha3-$alpha2;
213 $aa=$aa+360 if ($aa < -180);
214 $aa=$aa-360 if ($aa > 180);
215 $ba=$ba+360 if ($ba < -180);
216 $ba=$ba-360 if ($ba > 180);
219 my $ad=$delta2-$delta1;
220 my $bd=$delta3-$delta2;
221 $ad=$ad+360 if ($ad < -180);
222 $ad=$ad-360 if ($ad > 180);
223 $bd=$bd+360 if ($bd < -180);
224 $bd=$bd-360 if ($bd > 180);
227 $m0 = ($alpha2 + $lon - $theta0)/360.;
228 $m0=$m0+1 if( $m0 < 0 );
229 $m0=$m0-1 if( $m0 > 1 );
230 for ($i=1; $i<=2; $i++) {
231 $theta = $theta0+360.985647*$m0;
232 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
233 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
234 $H=$theta-$lon-$alpha;
235 $H=reduce_angle_to_360($H);
236 $H=$H-360 if ($H > 180);
237 ($az,$h)=get_az_el($H,$delta,$lat);
240 $m0=$m0+1 if( $m0 < 0 );
241 $m0=$m0-1 if( $m0 > 1 );
244 $m1 = $m0 - $H0/360.;
245 $m1=$m1+1 if( $m1 < 0 );
246 $m1=$m1-1 if( $m1 > 1 );
247 for ($i=1; $i<=2; $i++) {
248 $theta = $theta0+360.985647*$m1;
249 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
250 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
251 $H=$theta-$lon-$alpha;
252 $H=reduce_angle_to_360($H);
253 ($az,$h)=get_az_el($H,$delta,$lat);
254 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
256 $m1=$m1+1 if( $m1 < 0 );
257 $m1=$m1-1 if( $m1 > 1 );
260 $m2 = $m0 + $H0/360.;
261 $m2=$m2+1 if( $m2 < 0 );
262 $m2=$m2-1 if( $m2 > 1 );
263 for ($i=1; $i<=2; $i++) {
264 $theta = $theta0+360.985647*$m2;
265 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
266 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
267 $H=$theta-$lon-$alpha;
268 $H=reduce_angle_to_360($H);
269 ($az,$h)=get_az_el($H,$delta,$lat);
270 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
272 $m2=$m2+1 if( $m2 < 0 );
273 $m2=$m2-1 if( $m2 > 1 );
275 my ($risehr,$risemin,$sethr,$setmin);
277 $risemin=($m1*24-int($m1*24))*60+0.5;
278 if ( $risemin >= 60 ) {
279 $risemin=$risemin-60;
283 $setmin=($m2*24-int($m2*24))*60+0.5;
284 if ( $setmin >= 60 ) {
289 if ( $sun0_moon1 == 0 ) {
290 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow);
292 if ( $sun0_moon1 == 1 ) {
293 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),
294 $aznow+180,$hnow, -40*log10($distance/385000) );
297 sub get_moon_alpha_delta
300 # Calculate the moon's right ascension and declination
304 my $Lp=218.3164477+481267.88123421*$tt-
305 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
306 $Lp=reduce_angle_to_360($Lp);
308 my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
309 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
310 $D=reduce_angle_to_360($D);
312 my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
313 $tt*$tt*$tt/24490000.;
314 $M=reduce_angle_to_360($M);
316 my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
317 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
318 $Mp=reduce_angle_to_360($Mp);
320 my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
321 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
322 $F=reduce_angle_to_360($F);
324 my $A1 = 119.75 + 131.849 * $tt;
325 $A1=reduce_angle_to_360($A1);
327 my $A2 = 53.09 + 479264.290 * $tt;
328 $A2=reduce_angle_to_360($A2);
330 my $A3 = 313.45 + 481266.484 * $tt;
331 $A3=reduce_angle_to_360($A3);
333 my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
335 my $Sl= 6288774*sindeg( 1 * $Mp ) +
336 1274027*sindeg(2 * $D + -1 * $Mp ) +
337 658314 *sindeg(2 * $D ) +
338 213618 *sindeg( 2 * $Mp ) +
339 -185116 *sindeg( 1 * $M )*$E +
340 -114332 *sindeg( 2 * $F ) +
341 58793 *sindeg(2 * $D + -2 * $Mp ) +
342 57066 *sindeg(2 * $D - 1 * $M -1 * $Mp )*$E +
343 53322 *sindeg(2 * $D + 1 * $Mp ) +
344 45758 *sindeg(2 * $D - 1 * $M )*$E +
345 -40923 *sindeg( + 1 * $M -1 * $Mp )*$E +
346 -34720 *sindeg(1 * $D ) +
347 -30383 *sindeg( + 1 * $M + 1 * $Mp )*$E +
348 15327 *sindeg(2 * $D + -2 * $F ) +
349 -12528 *sindeg( 1 * $Mp + 2 * $F ) +
350 10980 *sindeg( 1 * $Mp - 2 * $F ) +
351 10675 *sindeg(4 * $D + -1 * $Mp ) +
352 10034 *sindeg( 3 * $Mp ) +
353 8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
354 -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
355 -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
356 -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
357 4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
358 4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
359 3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
360 3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
361 3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
362 -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
363 -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
364 2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
365 -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
366 2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
367 -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
368 -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
369 2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
370 -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
371 -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
372 1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
373 -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
374 -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
375 -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
376 759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
377 -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
378 -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
379 691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
380 596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
381 549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
382 537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
383 520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
384 -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
385 -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
386 -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
387 351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
388 -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
389 330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
390 327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
391 -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
392 299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
393 294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
394 3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
396 my $Sr=-20905355 *cosdeg( 1 * $Mp ) +
397 -3699111 *cosdeg(2 * $D + -1 * $Mp ) +
398 -2955968 *cosdeg(2 * $D ) +
399 -569925 *cosdeg( 2 * $Mp ) +
400 48888 *cosdeg( 1 * $M )*$E +
401 -3149 *cosdeg( 2 * $F ) +
402 246158 *cosdeg(2 * $D + -2 * $Mp ) +
403 -152138 *cosdeg(2 * $D - 1 * $M -1 * $Mp )*$E +
404 -170733 *cosdeg(2 * $D + 1 * $Mp ) +
405 -204586 *cosdeg(2 * $D - 1 * $M )*$E +
406 -129620 *cosdeg( + 1 * $M -1 * $Mp )*$E +
407 108743 *cosdeg(1 * $D ) +
408 104755 *cosdeg( + 1 * $M + 1 * $Mp )*$E +
409 10321 *cosdeg(2 * $D + -2 * $F ) +
410 79661 *cosdeg( 1 * $Mp - 2 * $F ) +
411 -34782 *cosdeg(4 * $D + -1 * $Mp ) +
412 -23210 *cosdeg( 3 * $Mp ) +
413 -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
414 24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
415 30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
416 -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
417 -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
418 -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
419 -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
420 -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
421 14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
422 -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
423 10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
424 6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
425 -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
426 5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
427 -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
428 4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
429 -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
430 3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
431 2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
432 -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
433 -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
434 2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
435 -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
436 -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
437 -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
438 -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
439 -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
440 1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
441 8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
443 my $Sb= 5128122 *sindeg( 1 * $F ) +
444 280602 *sindeg( 1 * $Mp + 1 * $F ) +
445 277693 *sindeg( 1 * $Mp - 1 * $F ) +
446 173237 *sindeg(2 * $D - 1 * $F ) +
447 55413 *sindeg(2 * $D -1 * $Mp + 1 * $F ) +
448 46271 *sindeg(2 * $D + -1 * $Mp - 1 * $F ) +
449 32573 *sindeg(2 * $D + 1 * $F ) +
450 17198 *sindeg( 2 * $Mp + 1 * $F )+
451 9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
452 8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
453 8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
454 4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
455 4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
456 -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
457 2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
458 2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
459 2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
460 -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
461 1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
462 -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
463 -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
464 -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
465 -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
466 -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
467 -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
468 -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
469 -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
470 1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
471 1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
472 833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
473 777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
474 671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
475 607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
476 596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
477 491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
478 -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
479 439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
480 422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
481 421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
482 -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
483 -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
484 331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
485 315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
486 302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
487 -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
488 -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
489 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
490 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
491 -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
492 -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
493 -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
494 181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
495 -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
496 176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
497 166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
498 -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
499 132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
500 -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
501 115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
502 107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E
503 -2235 *sindeg($Lp) + 382*sindeg($A3) +
504 175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
505 127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
507 my $lambda=$Lp+$Sl/1000000.;
509 my $beta=$Sb/1000000.;
511 my $distance=385000.56 + $Sr/1000.;
513 my $epsilon = 23+26./60.+21.448/(60.*60.);
515 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
516 $alpha = reduce_angle_to_360($alpha);
518 my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
519 $delta = reduce_angle_to_360($delta);
521 return ($alpha,$delta,$distance);
524 sub get_sun_alpha_delta
527 # Calculate Sun's right ascension and declination
531 my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
532 $L0=reduce_angle_to_360($L0);
534 my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
535 $M=reduce_angle_to_360($M);
537 my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
538 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
539 0.000289*sindeg(3*$M);
541 my $OMEGA = 125.04 - 1934.136*$tt;
543 my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA);
545 my $epsilon = 23+26./60.+21.448/(60.*60.);
547 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
548 $alpha = reduce_angle_to_360($alpha);
550 my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
551 $delta = reduce_angle_to_360($delta);
553 return ($alpha,$delta);
555 sub get_satellite_pos
558 # This code was translated more-or-less directly from the Pascal
559 # routines contained in a report compiled by TS Kelso and based on:
560 # Spacetrack Report No. 3
561 # "Models for Propagation of NORAD Element Sets"
562 # Felix R. Hoots, Ronald L Roehrich
565 # See TS Kelso's web site for more details...
566 # Only the SGP propagation model is implemented.
568 # Steve Franke, K9AN. 9 Dec 1999.
572 #1 25338U 98030A 99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
573 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382
575 #1 21639U 91054B 99341.34471854 .00000095 00000-0 10000-3 0 4928
576 #2 21639 1.5957 88.4884 0003028 161.6582 135.4323 1.00277774 30562
578 #1 20439U 90005D 99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
579 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297
581 #Temporary keps database...
588 my $sat_ref = $keps{$satname};
589 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");
596 my $xke=.743669161e-1;
600 my $ck2=.5*$xj2*$ae**2;
601 my $ck4=-.375*$xj4*$ae**4;
602 my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
603 my $s=$ae*(1+$so/$xkmper);
605 my $epoch = $sat_ref ->{epoch};
606 #printf("epoch = %10.2f\n",$epoch);
607 my $epoch_year=int($epoch/1000);
608 my $epoch_day=$epoch-int(1000*$epoch_year);
609 #printf("epoch_year = %10.2f\n",$epoch_year);
610 #printf("epoch_day = %17.12f\n",$epoch_day);
611 my $ep_year=$epoch_year+2000 if ($epoch_year < 57);
612 $ep_year=$epoch_year+1900 if ($epoch_year >= 57);
613 my $jt_epoch=Julian_Date_of_Year($ep_year);
614 $jt_epoch=$jt_epoch+$epoch_day;
615 #printf("JT for epoch = %17.12f\n",$jt_epoch);
616 my $tsince=($jtime-$jt_epoch)*24*60;
617 #printf("tsince (min) = %17.12f\n",$tsince);
619 my $mm1 = $sat_ref ->{mm1};
620 my $mm2 = $sat_ref ->{mm2};
621 my $bstar=$sat_ref ->{bstar}; # drag term for sgp4 model
622 my $inclination=$sat_ref->{inclination}; # inclination in degrees
623 my $raan=$sat_ref->{raan}; # right ascension of ascending node in degs
624 my $eccentricity=$sat_ref ->{eccentricity}; # eccentricity - dimensionless
625 my $omegao=$sat_ref ->{argperigee}; # argument of perigee in degs
626 my $xmo=$sat_ref ->{meananomaly}; # mean anomaly in degrees
627 my $xno=$sat_ref ->{meanmotion}; # mean motion in revs per day
629 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
630 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
632 $omegao=$omegao*$d2r;
634 $inclination=$inclination*$d2r;
635 my $temp=2*$pi/$xmnpda/$xmnpda;
636 $xno=$xno*$temp*$xmnpda;
638 $mm2=$mm2*$temp/$xmnpda;
643 my $c4=$xj3*$ae**3/(4*$ck2);
644 my $cosio=cos($inclination);
645 my $sinio=sin($inclination);
646 my $a1=($xke/$xno)**(2./3.);
647 my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
648 my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
649 my $po=$ao*(1-$eccentricity*$eccentricity);
650 $qo=$ao*(1-$eccentricity);
651 my $xlo=$xmo+$omegao+$raan;
652 my $d10=$c3*$sinio*$sinio;
653 my $d20=$c2*(7.*$cosio*$cosio-1);
656 my $po2no=$xno/($po*$po);
657 my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
658 my $xnodot=-2.*$d30*$po2no;
659 my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
662 my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
663 $a=$ao*($xno/$a)**(2./3.);
665 $e =1-$qo/$a if ($a > $qo);
667 my $xnodes=$raan+$xnodot*$tsince;
668 my $omgas=$omegao+$omgdt*$tsince;
669 my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
671 my $axnsl=$e*cos($omgas);
672 my $aynsl=$e*sin($omgas)-$c6/$p;
673 my $xl=mod2p($xls-$c5/$p*$axnsl);
675 my $u=mod2p($xl-$xnodes);
681 for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
685 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
686 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
688 $tem5=$tem2/$tem5 if ($tem2 > 1);
694 my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
695 my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
696 my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
700 my $rdot=$xke*sqrt($a)/$r*$esine;
701 my $rvdot=$xke*sqrt($pl)/$r;
702 $temp=$esine/(1+sqrt(1-$el2));
703 my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
704 my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
705 my $su=atan2($sinu,$cosu);
707 my $sin2u=($cosu+$cosu)*$sinu;
708 my $cos2u=1-2*$sinu*$sinu;
709 my $rk=$r+$d10/$pl*$cos2u;
710 my $uk=$su-$d20/$pl2*$sin2u;
711 my $xnodek=$xnodes+$d30*$sin2u/$pl2;
712 my $xinck=$inclination+$d40/$pl2*$cos2u;
716 my $sinnok=sin($xnodek);
717 my $cosnok=cos($xnodek);
718 my $sinik=sin($xinck);
719 my $cosik=cos($xinck);
720 my $xmx=-$sinnok*$cosik;
721 my $xmy=$cosnok*$cosik;
722 my $ux=$xmx*$sinuk+$cosnok*$cosuk;
723 my $uy=$xmy*$sinuk+$sinnok*$cosuk;
724 my $uz=$sinik*$sinuk;
725 my $vx=$xmx*$cosuk-$cosnok*$sinuk;
726 my $vy=$xmy*$cosuk-$sinnok*$sinuk;
727 my $vz=$sinik*$cosuk;
729 my $x=$rk*$ux*$xkmper/$ae;
730 my $y=$rk*$uy*$xkmper/$ae;
731 my $z=$rk*$uz*$xkmper/$ae;
735 $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
736 $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
737 $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
738 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
739 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
740 my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
741 my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
742 return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
745 sub Calculate_LatLonAlt
748 # convert from ECI coordinates to latitude, longitude and altitude.
755 my $theta=atan2($y,$x);
756 my $lon=mod2p($theta-ThetaG_JD($time));
757 my $range=sqrt($x**2+$y**2);
758 my $f=1/298.26; # earth flattening constant
761 my $lat=atan2($z,$range);
766 $c=1/sqrt(1-$e2*sin($phi)**2);
767 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
768 } until abs($lat-$phi) < 1e-10;
769 my $alt=$range/cos($lat)-$xkmper*$c;
770 return ($lat,$lon,$alt,$theta); # radians and kilometers
774 sub Calculate_User_PosVel
776 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
777 # position and local hour angle.
782 my $theta=mod2p(ThetaG_JD($time)+$lon);
783 my $omega_E=1.00273790934; # earth rotations per sidereal day
785 my $mfactor=2*$pi*$omega_E/$secday;
786 my $f=1/298.26; # earth flattening constant
788 my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
789 my $s=(1-$f)*(1-$f)*$c;
790 my $achcp=($xkmper*$c+$alt)*cos($lat);
791 my $x_user=$achcp*cos($theta);
792 my $y_user=$achcp*sin($theta);
793 my $z_user=($xkmper*$s+$alt)*sin($lat);
794 my $xdot_user=-$mfactor*$y_user;
795 my $ydot_user=$mfactor*$x_user;
797 return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
801 # calculate the azimuth/el of an object as viewed from observers position
802 # with object position given in ECI coordinates and observer in lat/long/alt.
804 # inputs: object ECI position vector (km)
805 # object velocity vector (km/s)
807 # observer lat,lon,altitude (km)
820 my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
821 Calculate_User_PosVel($lat,$lon,$alt,$time);
825 my $xxdot=$xdot-$xdot_o;
826 my $yydot=$ydot-$ydot_o;
827 my $zzdot=$zdot-$zdot_o;
829 my $sin_lat=sin($lat);
830 my $cos_lat=cos($lat);
831 my $sin_theta=sin($theta);
832 my $cos_theta=cos($theta);
834 my $top_s=$sin_lat*$cos_theta*$xx
835 + $sin_lat*$sin_theta*$yy
838 my $top_e=-$sin_theta*$xx
841 my $top_z=$cos_lat*$cos_theta*$xx
842 + $cos_lat*$sin_theta*$yy
845 my $az=atan(-$top_e/$top_s);
846 $az=$az+$pi if ( $top_s > 0 );
847 $az=$az+2*$pi if ( $az < 0 );
849 my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
850 my $el=asin($top_z/$range);
851 return ($az, $el, $range);
854 sub Calendar_date_and_time_from_JD
856 my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
861 $alpha = int( ($z-1867216.5)/36524.25 );
862 $a=$z + 1 + $alpha - int($alpha/4);
863 $a=$z if( $z < 2299161 );
865 $c=int(($b-122.1)/365.25);
867 $e=int(($b-$d)/30.6001);
868 $dom=$b-$d-int(30.6001*$e)+$frac;
870 $mon=$e-1 if( $e < 14 );
871 $mon=$e-13 if( $e == 14 || $e == 15 );
872 $yr = $c-4716 if( $mon > 2 );
873 $yr = $c-4715 if( $mon == 1 || $mon == 2 );
875 $min= int(($frac*24 - $hr)*60+0.5);
876 if ($min == 60) { # this may well prove inadequate DJK
880 return ($yr,$mon,$day,$hr,$min);