new sh/sun
[spider.git] / perl / Sun.pm
1 #!/usr/bin/perl -w
2 #
3 # This module was written by Steve Franke K9AN. 
4 # November, 1999.
5
6 # The formulas used in this module 
7 # are described in: 
8 # Astronomical Algorithms, Second Edition
9 # by Jean Meeus, 1998
10 # Published by Willmann-Bell, Inc.
11 # P.O. Box 35025, Richmond, Virginia 23235
12 #
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.
16 #
17 # Copyright (c) 1999 - Steve Franke K9AN
18 #
19 # $Id$
20
21
22 package Sun;
23
24 use POSIX;
25
26 require Exporter;
27 @ISA = qw(Exporter);
28 @EXPORT = qw($pi $d2r $r2d );
29
30 use strict;
31 use vars qw($pi $d2r $r2d );
32  
33 $pi = 3.141592653589;
34 $d2r = ($pi/180);
35 $r2d = (180/$pi);
36
37 sub julian_day
38 {
39         my $year = shift;
40         my $month = shift;
41         my $day = shift;
42         my $julianday;
43
44         $year=$year-1 if( $month <= 2 );
45         $month=$month+12 if( $month <= 2);
46
47         $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
48         return $julianday;
49 }
50 sub reduce_angle_to_360
51 {
52         my $angle = shift;
53
54         $angle=$angle-int($angle/360)*360;
55         $angle=$angle+360 if( $angle < 0 );             
56         return $angle;
57 }
58 sub sindeg
59 {
60         my $angle_in_degrees = shift;
61
62         return sin($angle_in_degrees*$d2r);
63 }
64 sub cosdeg
65 {
66         my $angle_in_degrees = shift;
67
68         return cos($angle_in_degrees*$d2r);
69 }
70 sub tandeg
71 {
72         my $angle_in_degrees = shift;
73
74         return tan($angle_in_degrees*$d2r);
75 }
76 sub get_az_el
77 {
78         my $H=shift;
79         my $delta=shift;
80         my $lat=shift;
81
82         my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
83         my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
84         return ($az,$h);
85 }
86 sub rise_set
87 {
88         my $year = shift;
89         my $month = shift;
90         my $day = shift;
91         my $hr = shift;
92         my $min = shift;
93         my $lat = shift;
94         my $lon = shift;
95         my $sun0_moon1=shift;           # 0 for sun, 1 for moon, 2 for venus...
96
97         my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3);
98         my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr);
99         my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow);
100         
101         my $julianday=julian_day($year,$month,$day);
102         my $tt1 = ($julianday-1-2451545)/36525.;
103         my $tt2 = ($julianday-2451545)/36525.;
104         my $tt3 = ($julianday+1-2451545)/36525.;
105         my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
106
107         my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
108                 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
109         $theta0=reduce_angle_to_360($theta0);
110
111         my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
112                 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
113         $thetanow=reduce_angle_to_360($thetanow);
114
115         if ( $sun0_moon1 == 0 ) {
116                 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
117                 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
118                 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
119                 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
120                 $h0=-0.8333;
121                 $H=$thetanow-$lon-$alphanow;
122                 $H=reduce_angle_to_360($H);
123                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
124                 $hnow=$hnow +
125                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
126         }
127
128         if ( $sun0_moon1 == 1 ) {
129                 ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1);
130                 ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2);
131                 ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3);
132                 ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow);
133                 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.;
134                 $H=$thetanow-$lon-$alphanow;
135                 $H=reduce_angle_to_360($H);
136                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
137                 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
138                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
139         }
140
141         $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
142         $argtest = tandeg($lat)*tandeg($delta2);
143
144         if ( $argtest < -1. ) {
145                 return sprintf("Doesn't rise.");
146         }
147         if ( $argtest > 1. ) {
148                 return sprintf("Doesn't set.");
149         }
150
151         $H0 = acos($arg)*$r2d;
152         my $aa=$alpha2-$alpha1;
153         my $ba=$alpha3-$alpha2;
154         $aa=$aa+360 if ($aa < -180);
155         $aa=$aa-360 if ($aa >  180);
156         $ba=$ba+360 if ($ba < -180);
157         $ba=$ba-360 if ($ba >  180);
158         my $ca=$ba-$aa;
159
160         my $ad=$delta2-$delta1;
161         my $bd=$delta3-$delta2;
162         $ad=$ad+360 if ($ad < -180);
163         $ad=$ad-360 if ($ad >  180);
164         $bd=$bd+360 if ($bd < -180);
165         $bd=$bd-360 if ($bd >  180);
166         my $cd=$bd-$ad;
167
168         $m0 = ($alpha2 + $lon - $theta0)/360.;
169         $m0=$m0+1 if( $m0 < 0 );
170         $m0=$m0-1 if( $m0 > 1 );
171         for ($i=1; $i<=2; $i++) {       
172                 $theta = $theta0+360.985647*$m0;
173                 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
174                 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
175                 $H=$theta-$lon-$alpha;
176                 $H=reduce_angle_to_360($H);
177                 $H=$H-360 if ($H > 180);
178                 ($az,$h)=get_az_el($H,$delta,$lat);
179                 $corr=-$H/360;
180                 $m0=$m0+$corr;
181                 $m0=$m0+1 if( $m0 < 0 );
182                 $m0=$m0-1 if( $m0 > 1 );
183         }
184
185         $m1 = $m0 - $H0/360.;
186         $m1=$m1+1 if( $m1 < 0 );
187         $m1=$m1-1 if( $m1 > 1 );
188         for ($i=1; $i<=2; $i++) {
189                 $theta = $theta0+360.985647*$m1;
190                 $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
191                 $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
192                 $H=$theta-$lon-$alpha;
193                 $H=reduce_angle_to_360($H);
194                 ($az,$h)=get_az_el($H,$delta,$lat);
195                 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
196                 $m1=$m1+$corr;
197                 $m1=$m1+1 if( $m1 < 0 );
198                 $m1=$m1-1 if( $m1 > 1 );
199         }
200
201         $m2 = $m0 + $H0/360.;
202         $m2=$m2+1 if( $m2 < 0 );
203         $m2=$m2-1 if( $m2 > 1 );
204         for ($i=1; $i<=2; $i++) {
205                 $theta = $theta0+360.985647*$m2;
206                 $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
207                 $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
208                 $H=$theta-$lon-$alpha;
209                 $H=reduce_angle_to_360($H);
210                 ($az,$h)=get_az_el($H,$delta,$lat);
211                 $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
212                 $m2 = $m2 + $corr;
213                 $m2=$m2+1 if( $m2 < 0 );
214                 $m2=$m2-1 if( $m2 > 1 );
215         }
216         my ($risehr,$risemin,$sethr,$setmin);
217         $risehr=int($m1*24);
218         $risemin=($m1*24-int($m1*24))*60+0.5;
219         if ( $risemin >= 60 ) {
220                 $risemin=$risemin-60;
221                 $risehr=$risehr+1;
222         }
223         $sethr=int($m2*24);
224         $setmin=($m2*24-int($m2*24))*60+0.5;
225         if ( $setmin >= 60 ) {
226                 $setmin=$setmin-60;
227                 $sethr=$sethr+1;
228         }
229
230         if ( $sun0_moon1 == 0 ) {
231                 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow);
232         }
233         if ( $sun0_moon1 == 1 ) {
234                 return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin), 
235                                 $aznow+180,$hnow, -40*log10($distance/385000) );
236         }
237 }
238 sub get_moon_alpha_delta 
239 {
240         #
241         # Calculate the moon's right ascension and declination
242         #
243         my $tt=shift;
244
245         my $Lp=218.3164477+481267.88123421*$tt-
246                 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
247         $Lp=reduce_angle_to_360($Lp);
248
249         my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
250                 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
251         $D=reduce_angle_to_360($D);             
252
253         my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
254                 $tt*$tt*$tt/24490000.;
255         $M=reduce_angle_to_360($M);
256
257         my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
258                 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
259         $Mp=reduce_angle_to_360($Mp);
260
261         my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
262                 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
263         $F=reduce_angle_to_360($F);
264
265         my $A1 = 119.75 + 131.849 * $tt;
266         $A1=reduce_angle_to_360($A1);
267
268         my $A2 =  53.09 + 479264.290 * $tt;
269         $A2=reduce_angle_to_360($A2);
270
271         my $A3 = 313.45 + 481266.484 * $tt;
272         $A3=reduce_angle_to_360($A3);
273
274         my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
275
276         my $Sl=  6288774*sindeg(                  1 * $Mp          ) +
277                  1274027*sindeg(2 * $D +         -1 * $Mp          ) +
278                  658314 *sindeg(2 * $D                             ) +
279                  213618 *sindeg(                  2 * $Mp          ) +
280                 -185116 *sindeg(         1 * $M                    )*$E +
281                 -114332 *sindeg(                            2 * $F ) +
282                   58793 *sindeg(2 * $D +         -2 * $Mp          ) +
283                   57066 *sindeg(2 * $D - 1 * $M  -1 * $Mp          )*$E +
284                   53322 *sindeg(2 * $D +          1 * $Mp          ) +
285                   45758 *sindeg(2 * $D - 1 * $M                    )*$E +
286                  -40923 *sindeg(       + 1 * $M  -1 * $Mp          )*$E +
287                  -34720 *sindeg(1 * $D                             ) +
288                  -30383 *sindeg(       + 1 * $M + 1 * $Mp          )*$E +
289                   15327 *sindeg(2 * $D +                   -2 * $F ) +
290                  -12528 *sindeg(                  1 * $Mp + 2 * $F ) +
291                   10980 *sindeg(                  1 * $Mp - 2 * $F ) +
292                   10675 *sindeg(4 * $D +         -1 * $Mp          ) +
293                   10034 *sindeg(                  3 * $Mp          ) +
294                    8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
295                   -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
296                   -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
297                   -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
298                    4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
299                    4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
300                    3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
301                    3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
302                    3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
303                   -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
304                   -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
305                    2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
306                   -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
307                    2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
308                   -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
309                   -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
310                    2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
311                   -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
312                   -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
313                    1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
314                   -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
315                    -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
316                    -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
317                     759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
318                    -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
319                    -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
320                     691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
321                     596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
322                     549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
323                     537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
324                     520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
325                    -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
326                    -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
327                    -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
328                     351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
329                    -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
330                     330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
331                     327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
332                    -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
333                     299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
334                     294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
335                    3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
336
337         my $Sr=-20905355 *cosdeg(                   1 * $Mp          ) +
338                 -3699111 *cosdeg(2 * $D +          -1 * $Mp          ) +
339                 -2955968 *cosdeg(2 * $D                              ) +
340                  -569925 *cosdeg(                   2 * $Mp          ) +
341                    48888 *cosdeg(         1 * $M                     )*$E +
342                    -3149 *cosdeg(                             2 * $F ) + 
343                   246158 *cosdeg(2 * $D +          -2 * $Mp           ) +
344                  -152138 *cosdeg(2 * $D - 1 * $M   -1 * $Mp           )*$E +
345                  -170733 *cosdeg(2 * $D +           1 * $Mp           ) +
346                  -204586 *cosdeg(2 * $D - 1 * $M                      )*$E +
347                  -129620 *cosdeg(       + 1 * $M   -1 * $Mp           )*$E +
348                   108743 *cosdeg(1 * $D                              ) +
349                   104755 *cosdeg(       + 1 * $M  + 1 * $Mp           )*$E +
350                   10321 *cosdeg(2 * $D +                     -2 * $F ) +
351                   79661 *cosdeg(                   1 * $Mp -  2 * $F ) +
352                  -34782 *cosdeg(4 * $D +          -1 * $Mp           ) +
353                  -23210 *cosdeg(                   3 * $Mp           ) +
354                  -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
355                   24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
356                   30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
357                   -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
358                  -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
359                  -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
360                  -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
361                  -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
362                   14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
363                   -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
364                   10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
365                    6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
366                   -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
367                    5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
368                   -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
369                    4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
370                   -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
371                    3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
372                    2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
373                   -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
374                   -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
375                    2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
376                   -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
377                   -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
378                   -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
379                   -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
380                   -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
381                    1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
382                    8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
383
384         my $Sb=  5128122 *sindeg(                            1 * $F  ) +
385                   280602 *sindeg(                  1 * $Mp + 1 * $F  ) +
386                   277693 *sindeg(                  1 * $Mp - 1 * $F  ) +
387                   173237 *sindeg(2 * $D                    - 1 * $F  ) +
388                    55413 *sindeg(2 * $D           -1 * $Mp + 1 * $F  ) +
389                    46271 *sindeg(2 * $D +         -1 * $Mp - 1 * $F  ) +
390                    32573 *sindeg(2 * $D +                    1 * $F  ) +
391                    17198 *sindeg(                  2 * $Mp + 1 * $F  )+
392                     9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
393                     8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
394                     8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
395                     4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
396                     4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
397                    -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
398                     2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
399                     2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
400                     2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
401                    -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
402                     1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
403                    -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
404                    -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
405                    -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
406                    -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
407                    -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
408                    -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
409                    -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
410                    -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
411                     1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
412                     1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
413                      833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
414                      777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
415                      671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
416                      607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
417                      596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
418                      491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
419                     -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
420                      439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
421                      422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
422                      421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
423                     -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
424                     -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
425                      331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
426                      315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
427                      302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
428                     -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
429                     -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
430                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
431                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
432                     -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
433                     -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
434                     -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
435                      181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
436                     -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
437                      176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
438                      166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
439                     -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
440                      132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
441                     -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
442                      115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
443                      107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E  
444                    -2235 *sindeg($Lp) + 382*sindeg($A3) + 
445                      175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
446                      127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
447  
448         my $lambda=$Lp+$Sl/1000000.; 
449
450         my $beta=$Sb/1000000.;
451
452         my $distance=385000.56 + $Sr/1000.;
453
454         my $epsilon = 23+26./60.+21.448/(60.*60.);
455
456         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
457         $alpha = reduce_angle_to_360($alpha);
458
459         my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
460         $delta = reduce_angle_to_360($delta);
461
462         return ($alpha,$delta,$distance);
463 }
464  
465 sub get_sun_alpha_delta 
466 {
467         #
468         # Calculate Sun's right ascension and declination
469         #
470         my $tt = shift;
471
472         my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
473         $L0=reduce_angle_to_360($L0);
474
475         my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
476         $M=reduce_angle_to_360($M);
477
478         my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
479                 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
480                 0.000289*sindeg(3*$M);
481
482         my $OMEGA = 125.04 - 1934.136*$tt;
483         
484         my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA); 
485
486         my $epsilon = 23+26./60.+21.448/(60.*60.);
487
488         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
489         $alpha = reduce_angle_to_360($alpha);
490
491         my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
492         $delta = reduce_angle_to_360($delta);
493
494         return ($alpha,$delta);
495 }
496