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 );
44 $year=$year-1 if( $month <= 2 );
45 $month=$month+12 if( $month <= 2);
47 $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
50 sub Julian_Date_of_Epoch
53 my $year=int($epoch*1e-3);
54 $year=$year+2000 if ($year < 57);
55 $year=$year+1900 if ($year >= 57);
56 my $day=$epoch-$year*1e3;
57 my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
58 return $Julian_Date_of_Epoch;
60 sub Julian_Date_of_Year
66 my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
68 return $Julian_Date_of_Year;
73 my $omega_E=1.00273790934; # earth rotations per sidereal day
75 my $UT=($jd+0.5)-int($jd+0.5);
77 my $TU=($jd-2451545.0)/36525;
78 my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
79 my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
83 sub reduce_angle_to_360
87 $angle=$angle-int($angle/360)*360;
88 $angle=$angle+360 if( $angle < 0 );
96 $angle=$angle-int($angle/$twopi)*$twopi;
97 $angle=$angle+$twopi if( $angle < 0 );
102 my $angle_in_degrees = shift;
104 return sin($angle_in_degrees*$d2r);
108 my $angle_in_degrees = shift;
110 return cos($angle_in_degrees*$d2r);
114 my $angle_in_degrees = shift;
116 return tan($angle_in_degrees*$d2r);
124 my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
125 my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
137 my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus...
139 my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3);
140 my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr);
141 my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow);
143 my $julianday=Julian_Day($year,$month,$day);
144 my $tt1 = ($julianday-1-2451545)/36525.;
145 my $tt2 = ($julianday-2451545)/36525.;
146 my $tt3 = ($julianday+1-2451545)/36525.;
147 my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
149 my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
150 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
151 $theta0=reduce_angle_to_360($theta0);
153 my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
154 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
155 $thetanow=reduce_angle_to_360($thetanow);
157 if ( $sun0_moon1 == 0 ) {
158 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
159 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
160 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
161 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
163 $H=$thetanow-$lon-$alphanow;
164 $H=reduce_angle_to_360($H);
165 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
167 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
170 if ( $sun0_moon1 == 1 ) {
171 ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1);
172 ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2);
173 ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3);
174 ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow);
175 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.;
176 $H=$thetanow-$lon-$alphanow;
177 $H=reduce_angle_to_360($H);
178 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
179 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
180 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
183 $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
184 $argtest = tandeg($lat)*tandeg($delta2);
186 if ( $argtest < -1. ) {
187 return sprintf("Doesn't rise.");
189 if ( $argtest > 1. ) {
190 return sprintf("Doesn't set.");
193 $H0 = acos($arg)*$r2d;
194 my $aa=$alpha2-$alpha1;
195 my $ba=$alpha3-$alpha2;
196 $aa=$aa+360 if ($aa < -180);
197 $aa=$aa-360 if ($aa > 180);
198 $ba=$ba+360 if ($ba < -180);
199 $ba=$ba-360 if ($ba > 180);
202 my $ad=$delta2-$delta1;
203 my $bd=$delta3-$delta2;
204 $ad=$ad+360 if ($ad < -180);
205 $ad=$ad-360 if ($ad > 180);
206 $bd=$bd+360 if ($bd < -180);
207 $bd=$bd-360 if ($bd > 180);
210 $m0 = ($alpha2 + $lon - $theta0)/360.;
211 $m0=$m0+1 if( $m0 < 0 );
212 $m0=$m0-1 if( $m0 > 1 );
213 for ($i=1; $i<=2; $i++) {
214 $theta = $theta0+360.985647*$m0;
215 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
216 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
217 $H=$theta-$lon-$alpha;
218 $H=reduce_angle_to_360($H);
219 $H=$H-360 if ($H > 180);
220 ($az,$h)=get_az_el($H,$delta,$lat);
223 $m0=$m0+1 if( $m0 < 0 );
224 $m0=$m0-1 if( $m0 > 1 );
227 $m1 = $m0 - $H0/360.;
228 $m1=$m1+1 if( $m1 < 0 );
229 $m1=$m1-1 if( $m1 > 1 );
230 for ($i=1; $i<=2; $i++) {
231 $theta = $theta0+360.985647*$m1;
232 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
233 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
234 $H=$theta-$lon-$alpha;
235 $H=reduce_angle_to_360($H);
236 ($az,$h)=get_az_el($H,$delta,$lat);
237 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
239 $m1=$m1+1 if( $m1 < 0 );
240 $m1=$m1-1 if( $m1 > 1 );
243 $m2 = $m0 + $H0/360.;
244 $m2=$m2+1 if( $m2 < 0 );
245 $m2=$m2-1 if( $m2 > 1 );
246 for ($i=1; $i<=2; $i++) {
247 $theta = $theta0+360.985647*$m2;
248 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
249 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
250 $H=$theta-$lon-$alpha;
251 $H=reduce_angle_to_360($H);
252 ($az,$h)=get_az_el($H,$delta,$lat);
253 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
255 $m2=$m2+1 if( $m2 < 0 );
256 $m2=$m2-1 if( $m2 > 1 );
258 my ($risehr,$risemin,$sethr,$setmin);
260 $risemin=($m1*24-int($m1*24))*60+0.5;
261 if ( $risemin >= 60 ) {
262 $risemin=$risemin-60;
266 $setmin=($m2*24-int($m2*24))*60+0.5;
267 if ( $setmin >= 60 ) {
272 if ( $sun0_moon1 == 0 ) {
273 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow);
275 if ( $sun0_moon1 == 1 ) {
276 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),
277 $aznow+180,$hnow, -40*log10($distance/385000) );
280 sub get_moon_alpha_delta
283 # Calculate the moon's right ascension and declination
287 my $Lp=218.3164477+481267.88123421*$tt-
288 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
289 $Lp=reduce_angle_to_360($Lp);
291 my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
292 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
293 $D=reduce_angle_to_360($D);
295 my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
296 $tt*$tt*$tt/24490000.;
297 $M=reduce_angle_to_360($M);
299 my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
300 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
301 $Mp=reduce_angle_to_360($Mp);
303 my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
304 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
305 $F=reduce_angle_to_360($F);
307 my $A1 = 119.75 + 131.849 * $tt;
308 $A1=reduce_angle_to_360($A1);
310 my $A2 = 53.09 + 479264.290 * $tt;
311 $A2=reduce_angle_to_360($A2);
313 my $A3 = 313.45 + 481266.484 * $tt;
314 $A3=reduce_angle_to_360($A3);
316 my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
318 my $Sl= 6288774*sindeg( 1 * $Mp ) +
319 1274027*sindeg(2 * $D + -1 * $Mp ) +
320 658314 *sindeg(2 * $D ) +
321 213618 *sindeg( 2 * $Mp ) +
322 -185116 *sindeg( 1 * $M )*$E +
323 -114332 *sindeg( 2 * $F ) +
324 58793 *sindeg(2 * $D + -2 * $Mp ) +
325 57066 *sindeg(2 * $D - 1 * $M -1 * $Mp )*$E +
326 53322 *sindeg(2 * $D + 1 * $Mp ) +
327 45758 *sindeg(2 * $D - 1 * $M )*$E +
328 -40923 *sindeg( + 1 * $M -1 * $Mp )*$E +
329 -34720 *sindeg(1 * $D ) +
330 -30383 *sindeg( + 1 * $M + 1 * $Mp )*$E +
331 15327 *sindeg(2 * $D + -2 * $F ) +
332 -12528 *sindeg( 1 * $Mp + 2 * $F ) +
333 10980 *sindeg( 1 * $Mp - 2 * $F ) +
334 10675 *sindeg(4 * $D + -1 * $Mp ) +
335 10034 *sindeg( 3 * $Mp ) +
336 8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
337 -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
338 -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
339 -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
340 4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
341 4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
342 3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
343 3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
344 3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
345 -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
346 -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
347 2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
348 -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
349 2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
350 -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
351 -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
352 2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
353 -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
354 -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
355 1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
356 -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
357 -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
358 -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
359 759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
360 -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
361 -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
362 691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
363 596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
364 549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
365 537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
366 520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
367 -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
368 -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
369 -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
370 351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
371 -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
372 330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
373 327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
374 -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
375 299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
376 294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
377 3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
379 my $Sr=-20905355 *cosdeg( 1 * $Mp ) +
380 -3699111 *cosdeg(2 * $D + -1 * $Mp ) +
381 -2955968 *cosdeg(2 * $D ) +
382 -569925 *cosdeg( 2 * $Mp ) +
383 48888 *cosdeg( 1 * $M )*$E +
384 -3149 *cosdeg( 2 * $F ) +
385 246158 *cosdeg(2 * $D + -2 * $Mp ) +
386 -152138 *cosdeg(2 * $D - 1 * $M -1 * $Mp )*$E +
387 -170733 *cosdeg(2 * $D + 1 * $Mp ) +
388 -204586 *cosdeg(2 * $D - 1 * $M )*$E +
389 -129620 *cosdeg( + 1 * $M -1 * $Mp )*$E +
390 108743 *cosdeg(1 * $D ) +
391 104755 *cosdeg( + 1 * $M + 1 * $Mp )*$E +
392 10321 *cosdeg(2 * $D + -2 * $F ) +
393 79661 *cosdeg( 1 * $Mp - 2 * $F ) +
394 -34782 *cosdeg(4 * $D + -1 * $Mp ) +
395 -23210 *cosdeg( 3 * $Mp ) +
396 -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
397 24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
398 30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
399 -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
400 -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
401 -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
402 -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
403 -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
404 14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
405 -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
406 10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
407 6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
408 -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
409 5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
410 -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
411 4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
412 -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
413 3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
414 2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
415 -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
416 -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
417 2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
418 -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
419 -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
420 -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
421 -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
422 -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
423 1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
424 8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
426 my $Sb= 5128122 *sindeg( 1 * $F ) +
427 280602 *sindeg( 1 * $Mp + 1 * $F ) +
428 277693 *sindeg( 1 * $Mp - 1 * $F ) +
429 173237 *sindeg(2 * $D - 1 * $F ) +
430 55413 *sindeg(2 * $D -1 * $Mp + 1 * $F ) +
431 46271 *sindeg(2 * $D + -1 * $Mp - 1 * $F ) +
432 32573 *sindeg(2 * $D + 1 * $F ) +
433 17198 *sindeg( 2 * $Mp + 1 * $F )+
434 9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
435 8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
436 8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
437 4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
438 4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
439 -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
440 2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
441 2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
442 2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
443 -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
444 1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
445 -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
446 -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
447 -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
448 -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
449 -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
450 -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
451 -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
452 -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
453 1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
454 1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
455 833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
456 777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
457 671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
458 607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
459 596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
460 491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
461 -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
462 439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
463 422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
464 421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
465 -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
466 -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
467 331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
468 315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
469 302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
470 -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
471 -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
472 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
473 223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
474 -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
475 -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
476 -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
477 181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
478 -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
479 176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
480 166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
481 -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
482 132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
483 -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
484 115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
485 107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E
486 -2235 *sindeg($Lp) + 382*sindeg($A3) +
487 175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
488 127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
490 my $lambda=$Lp+$Sl/1000000.;
492 my $beta=$Sb/1000000.;
494 my $distance=385000.56 + $Sr/1000.;
496 my $epsilon = 23+26./60.+21.448/(60.*60.);
498 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
499 $alpha = reduce_angle_to_360($alpha);
501 my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
502 $delta = reduce_angle_to_360($delta);
504 return ($alpha,$delta,$distance);
507 sub get_sun_alpha_delta
510 # Calculate Sun's right ascension and declination
514 my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
515 $L0=reduce_angle_to_360($L0);
517 my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
518 $M=reduce_angle_to_360($M);
520 my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
521 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
522 0.000289*sindeg(3*$M);
524 my $OMEGA = 125.04 - 1934.136*$tt;
526 my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA);
528 my $epsilon = 23+26./60.+21.448/(60.*60.);
530 my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
531 $alpha = reduce_angle_to_360($alpha);
533 my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
534 $delta = reduce_angle_to_360($delta);
536 return ($alpha,$delta);
538 sub get_satellite_pos
541 # This code was translated more-or-less directly from the Pascal
542 # routines contained in a report compiled by TS Kelso and based on:
543 # Spacetrack Report No. 3
544 # "Models for Propagation of NORAD Element Sets"
545 # Felix R. Hoots, Ronald L Roehrich
548 # See TS Kelso's web site for more details...
549 # Only the SGP propagation model is implemented.
551 # Steve Franke, K9AN. 9 Dec 1999.
555 #1 25338U 98030A 99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
556 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382
558 #1 21639U 91054B 99341.34471854 .00000095 00000-0 10000-3 0 4928
559 #2 21639 1.5957 88.4884 0003028 161.6582 135.4323 1.00277774 30562
561 #1 20439U 90005D 99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
562 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297
564 #Temporary keps database...
570 epoch => 99341.00000000,
574 inclination => 98.6601,
576 eccentricity => .0011401,
577 argperigee => 112.4684,
578 meananomaly => 42.5140,
579 meanmotion => 14.23047277081382,
584 epoch => 99341.34471854,
588 inclination => 1.5957,
590 eccentricity => .003028,
591 argperigee => 161.6582,
592 meananomaly => 135.4323,
593 meanmotion => 1.00277774,
598 epoch => 99341.14501399,
602 inclination => 98.4690,
604 eccentricity => .0012163,
605 argperigee => 66.4615,
606 meananomaly => 293.7842,
607 meanmotion => 14.303202855,
615 my $sat_ref = $keps{$satname};
616 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");
623 my $xke=.743669161e-1;
627 my $ck2=.5*$xj2*$ae**2;
628 my $ck4=-.375*$xj4*$ae**4;
629 my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
630 my $s=$ae*(1+$so/$xkmper);
632 my $epoch = $sat_ref ->{epoch};
633 #printf("epoch = %10.2f\n",$epoch);
634 my $epoch_year=int($epoch/1000);
635 my $epoch_day=$epoch-int(1000*$epoch_year);
636 #printf("epoch_year = %10.2f\n",$epoch_year);
637 #printf("epoch_day = %17.12f\n",$epoch_day);
638 $epoch_year=$epoch_year+2000 if ($epoch_year < 57);
639 $epoch_year=$epoch_year+1900 if ($epoch_year >= 57);
640 my $jt_epoch=Julian_Date_of_Year($epoch_year);
641 $jt_epoch=$jt_epoch+$epoch_day;
642 #printf("JT for epoch = %17.12f\n",$jt_epoch);
643 my $tsince=($jtime-$jt_epoch)*24*60;
644 #printf("tsince (min) = %17.12f\n",$tsince);
646 my $mm1 = $sat_ref ->{mm1};
647 my $mm2 = $sat_ref ->{mm2};
648 my $bstar=$sat_ref ->{bstar}; # drag term for sgp4 model
649 my $inclination=$sat_ref->{inclination}; # inclination in degrees
650 my $raan=$sat_ref->{raan}; # right ascension of ascending node in degs
651 my $eccentricity=$sat_ref ->{eccentricity}; # eccentricity - dimensionless
652 my $omegao=$sat_ref ->{argperigee}; # argument of perigee in degs
653 my $xmo=$sat_ref ->{meananomaly}; # mean anomaly in degrees
654 my $xno=$sat_ref ->{meanmotion}; # mean motion in revs per day
656 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
657 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
659 $omegao=$omegao*$d2r;
661 $inclination=$inclination*$d2r;
662 my $temp=2*$pi/$xmnpda/$xmnpda;
663 $xno=$xno*$temp*$xmnpda;
665 $mm2=$mm2*$temp/$xmnpda;
670 my $c4=$xj3*$ae**3/(4*$ck2);
671 my $cosio=cos($inclination);
672 my $sinio=sin($inclination);
673 my $a1=($xke/$xno)**(2./3.);
674 my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
675 my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
676 my $po=$ao*(1-$eccentricity*$eccentricity);
677 $qo=$ao*(1-$eccentricity);
678 my $xlo=$xmo+$omegao+$raan;
679 my $d10=$c3*$sinio*$sinio;
680 my $d20=$c2*(7.*$cosio*$cosio-1);
683 my $po2no=$xno/($po*$po);
684 my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
685 my $xnodot=-2.*$d30*$po2no;
686 my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
689 my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
690 $a=$ao*($xno/$a)**(2./3.);
692 $e =1-$qo/$a if ($a > $qo);
694 my $xnodes=$raan+$xnodot*$tsince;
695 my $omgas=$omegao+$omgdt*$tsince;
696 my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
698 my $axnsl=$e*cos($omgas);
699 my $aynsl=$e*sin($omgas)-$c6/$p;
700 my $xl=mod2p($xls-$c5/$p*$axnsl);
702 my $u=mod2p($xl-$xnodes);
708 while ( abs($tem5) >= 1e-6 && $item3 < 10 )
713 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
714 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
716 $tem5=$tem2/$tem5 if ($tem2 > 1);
722 my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
723 my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
724 my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
728 my $rdot=$xke*sqrt($a)/$r*$esine;
729 my $rvdot=$xke*sqrt($pl)/$r;
730 $temp=$esine/(1+sqrt(1-$el2));
731 my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
732 my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
733 my $su=atan2($sinu,$cosu);
735 my $sin2u=($cosu+$cosu)*$sinu;
736 my $cos2u=1-2*$sinu*$sinu;
737 my $rk=$r+$d10/$pl*$cos2u;
738 my $uk=$su-$d20/$pl2*$sin2u;
739 my $xnodek=$xnodes+$d30*$sin2u/$pl2;
740 my $xinck=$inclination+$d40/$pl2*$cos2u;
744 my $sinnok=sin($xnodek);
745 my $cosnok=cos($xnodek);
746 my $sinik=sin($xinck);
747 my $cosik=cos($xinck);
748 my $xmx=-$sinnok*$cosik;
749 my $xmy=$cosnok*$cosik;
750 my $ux=$xmx*$sinuk+$cosnok*$cosuk;
751 my $uy=$xmy*$sinuk+$sinnok*$cosuk;
752 my $uz=$sinik*$sinuk;
753 my $vx=$xmx*$cosuk-$cosnok*$sinuk;
754 my $vy=$xmy*$cosuk-$sinnok*$sinuk;
755 my $vz=$sinik*$cosuk;
757 my $x=$rk*$ux*$xkmper/$ae;
758 my $y=$rk*$uy*$xkmper/$ae;
759 my $z=$rk*$uz*$xkmper/$ae;
763 $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
764 $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
765 $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
766 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
767 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
768 my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
769 my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
770 return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
773 sub Calculate_LatLonAlt
776 # convert from ECI coordinates to latitude, longitude and altitude.
783 my $theta=atan2($y,$x);
784 my $lon=mod2p($theta-ThetaG_JD($time));
785 my $range=sqrt($x**2+$y**2);
786 my $f=1/298.26; # earth flattening constant
789 my $lat=atan2($z,$range);
794 $c=1/sqrt(1-$e2*sin($phi)**2);
795 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
796 } until abs($lat-$phi) < 1e-10;
797 my $alt=$range/cos($lat)-$xkmper*$c;
798 return ($lat,$lon,$alt,$theta); # radians and kilometers
802 sub Calculate_User_PosVel
804 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
805 # position and local hour angle.
810 my $theta=mod2p(ThetaG_JD($time)+$lon);
811 my $omega_E=1.00273790934; # earth rotations per sidereal day
813 my $mfactor=2*$pi*$omega_E/$secday;
814 my $f=1/298.26; # earth flattening constant
816 my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
817 my $s=(1-$f)*(1-$f)*$c;
818 my $achcp=($xkmper*$c+$alt)*cos($lat);
819 my $x_user=$achcp*cos($theta);
820 my $y_user=$achcp*sin($theta);
821 my $z_user=($xkmper*$s+$alt)*sin($lat);
822 my $xdot_user=-$mfactor*$y_user;
823 my $ydot_user=$mfactor*$x_user;
825 return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
829 # calculate the azimuth/el of an object as viewed from observers position
830 # with object position given in ECI coordinates and observer in lat/long/alt.
832 # inputs: object ECI position vector (km)
833 # object velocity vector (km/s)
835 # observer lat,lon,altitude (km)
848 my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
849 Calculate_User_PosVel($lat,$lon,$alt,$time);
853 my $xxdot=$xdot-$xdot_o;
854 my $yydot=$ydot-$ydot_o;
855 my $zzdot=$zdot-$zdot_o;
857 my $sin_lat=sin($lat);
858 my $cos_lat=cos($lat);
859 my $sin_theta=sin($theta);
860 my $cos_theta=cos($theta);
862 my $top_s=$sin_lat*$cos_theta*$xx
863 + $sin_lat*$sin_theta*$yy
866 my $top_e=-$sin_theta*$xx
869 my $top_z=$cos_lat*$cos_theta*$xx
870 + $cos_lat*$sin_theta*$yy
873 my $az=atan(-$top_e/$top_s);
874 $az=$az+$pi if ( $top_s > 0 );
875 $az=$az+2*$pi if ( $az < 0 );
877 my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
878 my $el=asin($top_z/$range);
879 return ($az, $el, $range);
882 sub Calendar_date_and_time_from_JD
884 my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
889 $alpha = int( ($z-1867216.5)/36524.25 );
890 $a=$z + 1 + $alpha - int($alpha/4);
891 $a=$z if( $z < 2299161 );
893 $c=int(($b-122.1)/365.25);
895 $e=int(($b-$d)/30.6001);
896 $dom=$b-$d-int(30.6001*$e)+$frac;
898 $mon=$e-1 if( $e < 14 );
899 $mon=$e-13 if( $e == 14 || $e == 15 );
900 $yr = $c-4716 if( $mon > 2 );
901 $yr = $c-4715 if( $mon == 1 || $mon == 2 );
903 $min= int(($frac*24 - $hr)*60+0.5);
904 return ($yr,$mon,$day,$hr,$min);