6b9b88eb4da0edf6dc59656e2b6af2a8d270d986
[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 # 2005/02/25 add calculation of civil dawn and dusk, defined to be times
22 #            when solar zenith angle is 96 degrees.
23 # 2001/12/16 Fixed Julian_Date_of_Epoch and now I actually use it...
24 # 2001/09/15 some changes to take care of cases where the object 
25 #            doesn't rise or set on a given day... 
26
27 package Sun;
28
29 use POSIX;
30
31 require Exporter;
32 @ISA = qw(Exporter);
33 @EXPORT = qw($pi $d2r $r2d );
34
35 use strict;
36
37 use vars qw($VERSION $BRANCH);
38 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
39 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
40 $main::build += $VERSION;
41 $main::branch += $BRANCH;
42
43 use vars qw($pi $d2r $r2d);
44  
45 $pi = 3.141592653589;
46 $d2r = ($pi/180);
47 $r2d = (180/$pi);
48
49 use vars qw(%keps);
50 use Keps;
51 use DXVars;
52 use DXUtil;
53
54 # reload the keps data
55 sub load
56 {
57         my @out;
58         my $s = readfilestr("$main::root/local/Keps.pm");
59         if ($s) {
60                 eval $s;
61                 push @out, $@ if $@;
62         }
63     return @out;
64 }
65
66 sub Julian_Day
67 {
68         my $year = shift;
69         my $month = shift;
70         my $day = shift;
71         my $julianday;
72
73         $year=$year-1 if( $month <= 2 );
74         $month=$month+12 if( $month <= 2);
75
76         $julianday = int(365.25*($year+4716)+int(30.6001*($month+1)))+$day-13-1524.5;
77         return $julianday;
78 }
79 sub Julian_Date_of_Epoch
80 {
81         my $epoch=shift;
82         my $year=int($epoch/1000);
83         my $day=$epoch-$year*1000;
84         if ($year < 57 ) {
85                 $year=$year+2000;
86         }
87         else {
88                 $year=$year+1900;
89         }
90         my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day;
91         return $Julian_Date_of_Epoch;
92 }
93
94 sub Julian_Date_of_Year
95 {
96         my $year=shift;
97         $year=$year-1;
98         my $A=int($year/100);
99         my $B=2-$A+int($A/4);
100         my $Julian_Date_of_Year=int(365.25*$year)+int(30.6001*14)+
101                 1720994.5+$B;
102         return $Julian_Date_of_Year;
103 }       
104 sub ThetaG_JD
105 {
106         my $jd=shift;
107         my $omega_E=1.00273790934; # earth rotations per sidereal day
108         my $secday=86400;
109         my $UT=($jd+0.5)-int($jd+0.5);
110         $jd=$jd-$UT;
111         my $TU=($jd-2451545.0)/36525;
112         my $GMST=24110.54841+$TU*(8640184.812866+$TU*(0.093104-$TU*6.2e-6));
113         my $thetag_jd=mod2p(2*$pi*($GMST/$secday+$omega_E*$UT));
114         return $thetag_jd;
115 }
116
117 sub reduce_angle_to_360
118 {
119         my $angle = shift;
120
121         $angle=$angle-int($angle/360)*360;
122         $angle=$angle+360 if( $angle < 0 );             
123         return $angle;
124 }
125 sub mod2p
126 {
127         my $twopi=$pi*2;
128         my $angle = shift;
129
130         $angle=$angle-int($angle/$twopi)*$twopi;
131         $angle=$angle+$twopi if( $angle < 0 );          
132         return $angle;
133 }
134 sub sindeg
135 {
136         my $angle_in_degrees = shift;
137
138         return sin($angle_in_degrees*$d2r);
139 }
140 sub cosdeg
141 {
142         my $angle_in_degrees = shift;
143
144         return cos($angle_in_degrees*$d2r);
145 }
146 sub tandeg
147 {
148         my $angle_in_degrees = shift;
149
150         return tan($angle_in_degrees*$d2r);
151 }
152 sub get_az_el
153 {
154         my $H=shift;
155         my $delta=shift;
156         my $lat=shift;
157
158         my $az=$r2d * atan2( sindeg($H), cosdeg($H)*sindeg($lat)-tandeg($delta)*cosdeg($lat) );
159         my $h=$r2d * asin( sindeg($lat)*sindeg($delta)+cosdeg($lat)*cosdeg($delta)*cosdeg($H) );
160         return ($az,$h);
161 }
162 sub rise_set
163 {
164         my $year = shift;
165         my $month = shift;
166         my $day = shift;
167         my $hr = shift;
168         my $min = shift;
169         my $lat = shift;
170         my $lon = shift;
171         my $sun0_moon1=shift;           # 0 for sun, 1 for moon, 2 for venus...
172         my ($alpha1,$delta1,$alpha2,$delta2,$alpha3,$delta3);
173         my ($aznow,$hnow,$alphanow,$deltanow,$distance,$distancenow);
174         my ($h0,$H);
175         my ($risetime,$settime);
176         my ($dawntime,$dusktime);
177
178         my ($ifrac,$ifracnow);
179         
180         my $julianday=Julian_Day($year,$month,$day);
181         my $tt1 = ($julianday-1-2451545)/36525.;
182         my $tt2 = ($julianday-2451545)/36525.;
183         my $tt3 = ($julianday+1-2451545)/36525.;
184         my $ttnow = ($julianday+$hr/24+$min/24/60-2451545)/36525.;
185
186         my $theta0=280.46061837+360.98564736629*($julianday-2451545.0)+
187                 0.000387933*$tt2*$tt2-$tt2*$tt2*$tt2/38710000;
188         $theta0=reduce_angle_to_360($theta0);
189
190         my $thetanow=280.46061837+360.98564736629*($julianday+$hr/24+$min/24/60-2451545.0)+
191                 0.000387933*$ttnow*$ttnow-$ttnow*$ttnow*$ttnow/38710000;
192         $thetanow=reduce_angle_to_360($thetanow);
193
194         if ( $sun0_moon1 == 0 ) {
195                 ($alpha1, $delta1)=get_sun_alpha_delta($tt1);
196                 ($alpha2, $delta2)=get_sun_alpha_delta($tt2);
197                 ($alpha3, $delta3)=get_sun_alpha_delta($tt3);
198                 ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow);
199                 $H=$thetanow-$lon-$alphanow;
200                 $H=reduce_angle_to_360($H);
201                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
202                 $hnow=$hnow +
203                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
204                 $h0=-0.8333;      # this is for sun rise and sun set
205                 ($risetime,$settime)=
206                         do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
207                                 $alpha2,$delta2,$alpha3,$delta3);
208                 $h0=-6.0;         # this is for civil dawn and dusk
209                 ($dawntime,$dusktime)=
210                         do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
211                                 $alpha2,$delta2,$alpha3,$delta3);
212                 $dawntime = "------" if( $dawntime eq "NoRise" );
213                 $dusktime = "------" if( $dusktime eq "NoSet " );
214
215                 return (
216                         sprintf("%s", $dawntime), sprintf("%s",$risetime),
217                         sprintf("%s", $settime), sprintf("%s",$dusktime),
218                         $aznow+180,$hnow
219                         );
220         }
221
222         if ( $sun0_moon1 == 1 ) {
223                 ($alpha1, $delta1, $distance, $ifrac)=get_moon_alpha_delta($tt1);
224                 ($alpha2, $delta2, $distance, $ifrac)=get_moon_alpha_delta($tt2);
225                 ($alpha3, $delta3, $distance, $ifrac)=get_moon_alpha_delta($tt3);
226                 ($alphanow, $deltanow, $distancenow, $ifracnow)=get_moon_alpha_delta($ttnow);
227                 $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34.0/60.;
228                 $H=$thetanow-$lon-$alphanow;
229                 $H=reduce_angle_to_360($H);
230                 ($aznow,$hnow)=get_az_el($H,$deltanow,$lat);
231                 $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+
232                         1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60;
233                 ($risetime,$settime)=
234                         do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1,
235                                         $alpha2,$delta2,$alpha3,$delta3);
236                 return (sprintf("%s", $risetime), sprintf("%s",$settime), 
237                         $aznow+180,$hnow, -40*log10($distance/385000), $ifracnow );
238
239         }
240
241 }
242
243 sub do_rise_set_calculations
244 {
245         my $norise = 0;
246         my $noset = 0;
247         my ($risehr,$risemin,$risetime,$sethr,$setmin,$settime);
248         my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$corr);
249         my ($i,$arg,$argtest,$H0);
250
251     my $h0=shift;
252     my $theta0=shift;
253     my $lat=shift;
254     my $lon=shift;
255     my $alpha1=shift;
256     my $delta1=shift;
257     my $alpha2=shift;
258     my $delta2=shift;
259     my $alpha3=shift;
260     my $delta3=shift;
261     
262         $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2));
263         if ( abs($arg) > 1. ) {    # either up all day or down all day 
264                 $norise = 1;       # leave it to the user to examine 
265                 $noset = 1;        # the elevation angle (or look outside!) 
266         }                          # to figure out which.
267
268         $H0 = acos($arg)*$r2d;
269         my $aa=$alpha2-$alpha1;
270         my $ba=$alpha3-$alpha2;
271         $aa=$aa+360 if ($aa < -180);
272         $aa=$aa-360 if ($aa >  180);
273         $ba=$ba+360 if ($ba < -180);
274         $ba=$ba-360 if ($ba >  180);
275         my $ca=$ba-$aa;
276
277         my $ad=$delta2-$delta1;
278         my $bd=$delta3-$delta2;
279         $ad=$ad+360 if ($ad < -180);
280         $ad=$ad-360 if ($ad >  180);
281         $bd=$bd+360 if ($bd < -180);
282         $bd=$bd-360 if ($bd >  180);
283         my $cd=$bd-$ad;
284
285         $m0 = ($alpha2 + $lon - $theta0)/360.;
286         $m0=$m0+1 if( $m0 < 0 );
287         $m0=$m0-1 if( $m0 > 1 );
288         for ($i=1; $i<=2; $i++) {       
289                 $theta = $theta0+360.985647*$m0;
290                 $alpha=$alpha2+$m0*($aa+$ba+$m0*$ca)/2;
291                 $delta=$delta2+$m0*($ad+$bd+$m0*$cd)/2;
292                 $H=$theta-$lon-$alpha;
293                 $H=reduce_angle_to_360($H);
294                 $H=$H-360 if ($H > 180);
295                 ($az,$h)=get_az_el($H,$delta,$lat);
296                 $corr=-$H/360;
297                 $m0=$m0+$corr;
298                 $m0=$m0+1 if( $m0 < 0 );
299                 $m0=$m0-1 if( $m0 >= 1 );
300         }
301
302
303         if( !$norise ){
304                 $m1 = $m0 - $H0/360.;
305                 $m1=$m1+1 if( $m1 < 0 );
306                 $m1=$m1-1 if( $m1 > 1 );
307                 for ($i=1; $i<=2; $i++) {
308                         $theta = $theta0+360.985647*$m1;
309                         $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2;
310                         $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2;
311                         $H=$theta-$lon-$alpha;
312                         $H=reduce_angle_to_360($H);
313                         ($az,$h)=get_az_el($H,$delta,$lat);
314                         $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
315                         $m1=$m1+$corr;
316 #                       $norise=1 if( $m1 < 0 || $m1 > 1);
317             $m1=$m1-1 if( $m1 >= 1);
318             $m1=$m1+1 if( $m1 < 0); 
319                 }
320         }
321
322         if( !$norise ) {
323                 $risehr=int($m1*24);
324                 $risemin=($m1*24-int($m1*24))*60+0.5;
325                 if ( $risemin >= 60 ) {
326                         $risemin=$risemin-60;
327                         $risehr=$risehr+1;
328                 }
329                 $risehr=0 if($risehr==24);
330                 $risetime=sprintf("%02d:%02dZ",$risehr,$risemin);
331         } else {
332                 $risetime="NoRise";
333         }
334
335         if( !$noset ){
336                 $m2 = $m0 + $H0/360.;
337                 $m2=$m2+1 if( $m2 < 0 );
338                 $m2=$m2-1 if( $m2 >= 1 );
339                 for ($i=1; $i<=2; $i++) {
340                         $theta = $theta0+360.985647*$m2;
341                         $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2;
342                         $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2;
343                         $H=$theta-$lon-$alpha;
344                         $H=reduce_angle_to_360($H);
345                         ($az,$h)=get_az_el($H,$delta,$lat);
346                         $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H)));
347                         $m2 = $m2 + $corr;
348 #                       $noset=1 if( $m2 < 0 || $m2 > 1); 
349             $m2=$m2-1 if( $m2 >= 1);
350             $m2=$m2+1 if( $m2 < 0);
351                 }
352         }
353
354         if( !$noset ) {
355                 $sethr=int($m2*24);
356                 $setmin=($m2*24-int($m2*24))*60+0.5;
357                 if ( $setmin >= 60 ) {
358                         $setmin=$setmin-60;
359                         $sethr=$sethr+1;
360                 }
361                 $sethr=0 if($sethr==24);
362                 $settime=sprintf("%02d:%02dZ",$sethr,$setmin);
363         } else {
364                 $settime="NoSet ";
365         }                       
366         return $risetime,$settime;
367 }
368
369
370
371 sub get_moon_alpha_delta 
372 {
373         #
374         # Calculate the moon's right ascension and declination
375         #
376         # As of October 2001, also calculate the illuminated fraction of the 
377         # moon's disk... (why not?)
378         #
379         my $tt=shift;
380
381         my $Lp=218.3164477+481267.88123421*$tt-
382                 0.0015786*$tt*$tt+$tt*$tt*$tt/538841-$tt*$tt*$tt*$tt/65194000;
383         $Lp=reduce_angle_to_360($Lp);
384
385         my $D = 297.8501921+445267.1114034*$tt-0.0018819*$tt*$tt+
386                 $tt*$tt*$tt/545868.-$tt*$tt*$tt*$tt/113065000.;
387         $D=reduce_angle_to_360($D);             
388
389         my $M = 357.5291092 + 35999.0502909*$tt-0.0001536*$tt*$tt+
390                 $tt*$tt*$tt/24490000.;
391         $M=reduce_angle_to_360($M);
392
393         my $Mp = 134.9633964 + 477198.8675055*$tt+0.0087414*$tt*$tt+
394                 $tt*$tt*$tt/69699-$tt*$tt*$tt*$tt/14712000;
395         $Mp=reduce_angle_to_360($Mp);
396
397         my $F = 93.2720950 + 483202.0175233*$tt - 0.0036539*$tt*$tt-
398                 $tt*$tt*$tt/3526000 + $tt*$tt*$tt*$tt/863310000;
399         $F=reduce_angle_to_360($F);
400
401         my $A1 = 119.75 + 131.849 * $tt;
402         $A1=reduce_angle_to_360($A1);
403
404         my $A2 =  53.09 + 479264.290 * $tt;
405         $A2=reduce_angle_to_360($A2);
406
407         my $A3 = 313.45 + 481266.484 * $tt;
408         $A3=reduce_angle_to_360($A3);
409
410         my $E = 1 - 0.002516 * $tt - 0.0000074 * $tt * $tt;
411
412         my $Sl=  6288774*sindeg(                  1 * $Mp          ) +
413                  1274027*sindeg(2 * $D +         -1 * $Mp          ) +
414                  658314 *sindeg(2 * $D                             ) +
415                  213618 *sindeg(                  2 * $Mp          ) +
416                 -185116 *sindeg(         1 * $M                    )*$E +
417                 -114332 *sindeg(                            2 * $F ) +
418                   58793 *sindeg(2 * $D +         -2 * $Mp          ) +
419                   57066 *sindeg(2 * $D - 1 * $M  -1 * $Mp          )*$E +
420                   53322 *sindeg(2 * $D +          1 * $Mp          ) +
421                   45758 *sindeg(2 * $D - 1 * $M                    )*$E +
422                  -40923 *sindeg(       + 1 * $M  -1 * $Mp          )*$E +
423                  -34720 *sindeg(1 * $D                             ) +
424                  -30383 *sindeg(       + 1 * $M + 1 * $Mp          )*$E +
425                   15327 *sindeg(2 * $D +                   -2 * $F ) +
426                  -12528 *sindeg(                  1 * $Mp + 2 * $F ) +
427                   10980 *sindeg(                  1 * $Mp - 2 * $F ) +
428                   10675 *sindeg(4 * $D +         -1 * $Mp          ) +
429                   10034 *sindeg(                  3 * $Mp          ) +
430                    8548 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
431                   -7888 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
432                   -6766 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
433                   -5163 *sindeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
434                    4987 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
435                    4036 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
436                    3994 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
437                    3861 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
438                    3665 *sindeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
439                   -2689 *sindeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
440                   -2602 *sindeg(2 * $D + 0 * $M - 1 * $Mp + 2 * $F ) +
441                    2390 *sindeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
442                   -2348 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
443                    2236 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
444                   -2120 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
445                   -2069 *sindeg(0 * $D + 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
446                    2048 *sindeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
447                   -1773 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F ) +
448                   -1595 *sindeg(2 * $D + 0 * $M + 0 * $Mp + 2 * $F ) +
449                    1215 *sindeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
450                   -1110 *sindeg(0 * $D + 0 * $M + 2 * $Mp + 2 * $F ) +
451                    -892 *sindeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
452                    -810 *sindeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
453                     759 *sindeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
454                    -713 *sindeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
455                    -700 *sindeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
456                     691 *sindeg(2 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
457                     596 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 2 * $F )*$E +
458                     549 *sindeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
459                     537 *sindeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F ) +
460                     520 *sindeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
461                    -487 *sindeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
462                    -399 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 2 * $F )*$E +
463                    -381 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F ) +
464                     351 *sindeg(1 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
465                    -340 *sindeg(3 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
466                     330 *sindeg(4 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
467                     327 *sindeg(2 * $D - 1 * $M + 2 * $Mp + 0 * $F )*$E +
468                    -323 *sindeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
469                     299 *sindeg(1 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
470                     294 *sindeg(2 * $D + 0 * $M + 3 * $Mp + 0 * $F ) +
471                    3958 *sindeg($A1) + 1962*sindeg($Lp - $F) + 318*sindeg($A2);
472
473         my $Sr=-20905355 *cosdeg(                   1 * $Mp          ) +
474                 -3699111 *cosdeg(2 * $D +          -1 * $Mp          ) +
475                 -2955968 *cosdeg(2 * $D                              ) +
476                  -569925 *cosdeg(                   2 * $Mp          ) +
477                    48888 *cosdeg(         1 * $M                     )*$E +
478                    -3149 *cosdeg(                             2 * $F ) + 
479                   246158 *cosdeg(2 * $D +          -2 * $Mp           ) +
480                  -152138 *cosdeg(2 * $D - 1 * $M   -1 * $Mp           )*$E +
481                  -170733 *cosdeg(2 * $D +           1 * $Mp           ) +
482                  -204586 *cosdeg(2 * $D - 1 * $M                      )*$E +
483                  -129620 *cosdeg(       + 1 * $M   -1 * $Mp           )*$E +
484                   108743 *cosdeg(1 * $D                              ) +
485                   104755 *cosdeg(       + 1 * $M  + 1 * $Mp           )*$E +
486                   10321 *cosdeg(2 * $D +                     -2 * $F ) +
487                   79661 *cosdeg(                   1 * $Mp -  2 * $F ) +
488                  -34782 *cosdeg(4 * $D +          -1 * $Mp           ) +
489                  -23210 *cosdeg(                   3 * $Mp           ) +
490                  -21636 *cosdeg(4 * $D + 0 * $M - 2 * $Mp + 0 * $F ) +
491                   24208 *cosdeg(2 * $D + 1 * $M - 1 * $Mp + 0 * $F )*$E +
492                   30824 *cosdeg(2 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
493                   -8379 *cosdeg(1 * $D + 0 * $M - 1 * $Mp + 0 * $F ) +
494                  -16675 *cosdeg(1 * $D + 1 * $M + 0 * $Mp + 0 * $F )*$E +
495                  -12831 *cosdeg(2 * $D - 1 * $M + 1 * $Mp + 0 * $F )*$E +
496                  -10445 *cosdeg(2 * $D + 0 * $M + 2 * $Mp + 0 * $F ) +
497                  -11650 *cosdeg(4 * $D + 0 * $M + 0 * $Mp + 0 * $F ) +
498                   14403 *cosdeg(2 * $D + 0 * $M - 3 * $Mp + 0 * $F ) +
499                   -7003 *cosdeg(0 * $D + 1 * $M - 2 * $Mp + 0 * $F )*$E +
500                   10056 *cosdeg(2 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
501                    6322 *cosdeg(1 * $D + 0 * $M + 1 * $Mp + 0 * $F ) +
502                   -9884 *cosdeg(2 * $D - 2 * $M + 0 * $Mp + 0 * $F )*$E*$E +
503                    5751 *cosdeg(0 * $D + 1 * $M + 2 * $Mp + 0 * $F )*$E +
504                   -4950 *cosdeg(2 * $D - 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
505                    4130 *cosdeg(2 * $D + 0 * $M + 1 * $Mp - 2 * $F )+
506                   -3958 *cosdeg(4 * $D - 1 * $M - 1 * $Mp + 0 * $F )*$E +
507                    3258 *cosdeg(3 * $D + 0 * $M - 1 * $Mp + 0 * $F )+
508                    2616 *cosdeg(2 * $D + 1 * $M + 1 * $Mp + 0 * $F )*$E +
509                   -1897 *cosdeg(4 * $D - 1 * $M - 2 * $Mp + 0 * $F )*$E +
510                   -2117 *cosdeg(0 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
511                    2354 *cosdeg(2 * $D + 2 * $M - 1 * $Mp + 0 * $F )*$E*$E +
512                   -1423 *cosdeg(4 * $D + 0 * $M + 1 * $Mp + 0 * $F )+
513                   -1117 *cosdeg(0 * $D + 0 * $M + 4 * $Mp + 0 * $F )+
514                   -1571 *cosdeg(4 * $D - 1 * $M + 0 * $Mp + 0 * $F )*$E +
515                   -1739 *cosdeg(1 * $D + 0 * $M - 2 * $Mp + 0 * $F )+
516                   -4421 *cosdeg(0 * $D + 0 * $M + 2 * $Mp - 2 * $F )+
517                    1165 *cosdeg(0 * $D + 2 * $M + 1 * $Mp + 0 * $F )*$E*$E +
518                    8752 *cosdeg(2 * $D + 0 * $M - 1 * $Mp - 2 * $F );
519
520         my $Sb=  5128122 *sindeg(                            1 * $F  ) +
521                   280602 *sindeg(                  1 * $Mp + 1 * $F  ) +
522                   277693 *sindeg(                  1 * $Mp - 1 * $F  ) +
523                   173237 *sindeg(2 * $D                    - 1 * $F  ) +
524                    55413 *sindeg(2 * $D           -1 * $Mp + 1 * $F  ) +
525                    46271 *sindeg(2 * $D +         -1 * $Mp - 1 * $F  ) +
526                    32573 *sindeg(2 * $D +                    1 * $F  ) +
527                    17198 *sindeg(                  2 * $Mp + 1 * $F  )+
528                     9266 *sindeg(2 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
529                     8822 *sindeg(0 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
530                     8216 *sindeg(2 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
531                     4324 *sindeg(2 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
532                     4200 *sindeg(2 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
533                    -3359 *sindeg(2 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
534                     2463 *sindeg(2 * $D - 1 * $M - 1 * $Mp + 1 * $F )*$E +
535                     2211 *sindeg(2 * $D - 1 * $M + 0 * $Mp + 1 * $F )*$E +
536                     2065 *sindeg(2 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
537                    -1870 *sindeg(0 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
538                     1828 *sindeg(4 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
539                    -1794 *sindeg(0 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
540                    -1749 *sindeg(0 * $D + 0 * $M + 0 * $Mp + 3 * $F ) +
541                    -1565 *sindeg(0 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
542                    -1491 *sindeg(1 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
543                    -1475 *sindeg(0 * $D + 1 * $M + 1 * $Mp + 1 * $F )*$E +
544                    -1410 *sindeg(0 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
545                    -1344 *sindeg(0 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
546                    -1335 *sindeg(1 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
547                     1107 *sindeg(0 * $D + 0 * $M + 3 * $Mp + 1 * $F ) +
548                     1021 *sindeg(4 * $D + 0 * $M + 0 * $Mp - 1 * $F ) +
549                      833 *sindeg(4 * $D + 0 * $M - 1 * $Mp + 1 * $F ) +
550                      777 *sindeg(0 * $D + 0 * $M + 1 * $Mp - 3 * $F ) +
551                      671 *sindeg(4 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
552                      607 *sindeg(2 * $D + 0 * $M + 0 * $Mp - 3 * $F ) +
553                      596 *sindeg(2 * $D + 0 * $M + 2 * $Mp - 1 * $F ) +
554                      491 *sindeg(2 * $D - 1 * $M + 1 * $Mp - 1 * $F )*$E +
555                     -451 *sindeg(2 * $D + 0 * $M - 2 * $Mp + 1 * $F ) +
556                      439 *sindeg(0 * $D + 0 * $M + 3 * $Mp - 1 * $F ) +
557                      422 *sindeg(2 * $D + 0 * $M + 2 * $Mp + 1 * $F ) +
558                      421 *sindeg(2 * $D + 0 * $M - 3 * $Mp - 1 * $F ) +
559                     -366 *sindeg(2 * $D + 1 * $M - 1 * $Mp + 1 * $F )*$E +
560                     -351 *sindeg(2 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
561                      331 *sindeg(4 * $D + 0 * $M + 0 * $Mp + 1 * $F ) +
562                      315 *sindeg(2 * $D - 1 * $M + 1 * $Mp + 1 * $F )*$E +
563                      302 *sindeg(2 * $D - 2 * $M + 0 * $Mp - 1 * $F )*$E*$E +
564                     -283 *sindeg(0 * $D + 0 * $M + 1 * $Mp + 3 * $F ) +
565                     -229 *sindeg(2 * $D + 1 * $M + 1 * $Mp - 1 * $F )*$E +
566                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp - 1 * $F )*$E +
567                      223 *sindeg(1 * $D + 1 * $M + 0 * $Mp + 1 * $F )*$E +
568                     -220 *sindeg(0 * $D + 1 * $M - 2 * $Mp - 1 * $F )*$E +
569                     -220 *sindeg(2 * $D + 1 * $M - 1 * $Mp - 1 * $F )*$E +
570                     -185 *sindeg(1 * $D + 0 * $M + 1 * $Mp + 1 * $F ) +
571                      181 *sindeg(2 * $D - 1 * $M - 2 * $Mp - 1 * $F )*$E +
572                     -177 *sindeg(0 * $D + 1 * $M + 2 * $Mp + 1 * $F )*$E +
573                      176 *sindeg(4 * $D + 0 * $M - 2 * $Mp - 1 * $F ) +
574                      166 *sindeg(4 * $D - 1 * $M - 1 * $Mp - 1 * $F )*$E +
575                     -164 *sindeg(1 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
576                      132 *sindeg(4 * $D + 0 * $M + 1 * $Mp - 1 * $F ) +
577                     -119 *sindeg(1 * $D + 0 * $M - 1 * $Mp - 1 * $F ) +
578                      115 *sindeg(4 * $D - 1 * $M + 0 * $Mp - 1 * $F )*$E +
579                      107 *sindeg(2 * $D - 2 * $M + 0 * $Mp + 1 * $F )*$E*$E  
580                    -2235 *sindeg($Lp) + 382*sindeg($A3) + 
581                      175 *sindeg($A1-$F) + 175*sindeg($A1+$F) +
582                      127 *sindeg($Lp-$Mp) - 115*sindeg($Lp+$Mp);
583  
584         my $lambda=$Lp+$Sl/1000000.; 
585
586         my $beta=$Sb/1000000.;
587
588         my $distance=385000.56 + $Sr/1000.;
589
590         my $epsilon = 23+26./60.+21.448/(60.*60.);
591
592         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda)-tandeg($beta)*sindeg($epsilon),cosdeg($lambda))*$r2d;
593         $alpha = reduce_angle_to_360($alpha);
594
595         my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d;
596         $delta = reduce_angle_to_360($delta);
597
598 # $phase will be the "moon phase angle" from p. 346 of Meeus' book...
599         my $phase=180.0 - $D - 6.289 *sindeg($Mp)
600                                 + 2.100 *sindeg($M)
601                                 - 1.274 *sindeg(2.*$D - $Mp)
602                                 - 0.658 *sindeg(2.*$D)
603                                 - 0.214 *sindeg(2.*$Mp)
604                                 - 0.110 *sindeg($D);
605
606 # $illum_frac is the fraction of the disk that is illuminated, and will be
607 # zero at new moon and 1.0 at full moon.
608
609         my $illum_frac = (1.0 + cosdeg( $phase ))/2.;   
610
611         return ($alpha,$delta,$distance,$illum_frac);
612 }
613  
614 sub get_sun_alpha_delta 
615 {
616 #
617 # Calculate Sun's right ascension and declination
618 #
619         my $tt = shift;
620
621         my $L0 = 280.46646+36000.76983*$tt+0.0003032*($tt^2);
622         $L0=reduce_angle_to_360($L0);
623
624         my $M = 357.52911 + 35999.05029*$tt-0.0001537*($tt^2);
625         $M=reduce_angle_to_360($M);
626
627         my $C = (1.914602 - 0.004817*$tt-0.000014*($tt^2))*sindeg($M) +
628                 (0.019993 - 0.000101*$tt)*sindeg(2*$M) +
629                 0.000289*sindeg(3*$M);
630
631         my $OMEGA = 125.04 - 1934.136*$tt;
632         
633         my $lambda=$L0+$C-0.00569-0.00478*sindeg($OMEGA); 
634
635         my $epsilon = 23+26./60.+21.448/(60.*60.);
636
637         my $alpha=atan2(cosdeg($epsilon)*sindeg($lambda),cosdeg($lambda))*$r2d;
638         $alpha = reduce_angle_to_360($alpha);
639
640         my $delta=asin(sin($epsilon*$d2r)*sin($lambda*$d2r))*$r2d;
641         $delta = reduce_angle_to_360($delta);
642
643         return ($alpha,$delta);
644 }
645 sub get_satellite_pos
646 {
647 #
648 # This code was translated more-or-less directly from the Pascal
649 # routines contained in a report compiled by TS Kelso and based on:
650 # Spacetrack Report No. 3
651 # "Models for Propagation of NORAD Element Sets"
652 # Felix R. Hoots, Ronald L Roehrich
653 # December 1980
654 #
655 # See TS Kelso's web site for more details...
656 # Only the SGP propagation model is implemented. 
657 #
658 # Steve Franke, K9AN.   9 Dec 1999.
659
660 #
661 #NOAA 15
662 #1 25338U 98030A   99341.00000000 +.00000376 +00000-0 +18612-3 0 05978
663 #2 25338 098.6601 008.2003 0011401 112.4684 042.5140 14.23047277081382          
664 #TDRS 5
665 #1 21639U 91054B   99341.34471854  .00000095  00000-0  10000-3 0  4928
666 #2 21639   1.5957  88.4884 0003028 161.6582 135.4323  1.00277774 30562
667 #OSCAR 16 (PACSAT)
668 #1 20439U 90005D   99341.14501399 +.00000343 +00000-0 +14841-3 0 02859
669 #2 20439 098.4690 055.0032 0012163 066.4615 293.7842 14.30320285515297      
670 #
671 #Temporary keps database...
672 #
673         my $jtime = shift;
674         my $lat = shift;
675         my $lon = shift;
676         my $alt = shift;
677         my $satname = shift;
678         my $sat_ref = $keps{$satname};
679 #printf("$jtime $lat $lon $alt Satellite name = $satname\n");   
680
681         my $qo=120;
682         my $so=78;
683         my $xj2=1.082616e-3;
684         my $xj3=-.253881e-5;
685         my $xj4=-1.65597e-6;
686         my $xke=.743669161e-1;
687         my $xkmper=6378.135;
688         my $xmnpda=1440.;
689         my $ae=1.;
690         my $ck2=.5*$xj2*$ae**2;
691         my $ck4=-.375*$xj4*$ae**4;
692         my $qoms2t=(($qo-$so)*$ae/$xkmper)**4;
693         my $s=$ae*(1+$so/$xkmper);
694
695         my $epoch = $sat_ref ->{epoch};
696 #printf("epoch = %10.2f\n",$epoch);
697         my $jt_epoch=Julian_Date_of_Epoch($epoch);
698 #printf("JT for epoch = %17.12f\n",$jt_epoch);
699         my $tsince=($jtime-$jt_epoch)*24*60;
700 #printf("tsince (min) = %17.12f\n",$tsince);
701
702         my $mm1 = $sat_ref ->{mm1};
703         my $mm2 = $sat_ref ->{mm2};
704         my $bstar=$sat_ref ->{bstar};             # drag term for sgp4 model 
705         my $inclination=$sat_ref->{inclination};  # inclination in degrees
706         my $raan=$sat_ref->{raan};                # right ascension of ascending node in degs
707         my $eccentricity=$sat_ref ->{eccentricity};  # eccentricity - dimensionless
708         my $omegao=$sat_ref ->{argperigee};          # argument of perigee in degs
709         my $xmo=$sat_ref ->{meananomaly};            # mean anomaly in degrees
710         my $xno=$sat_ref ->{meanmotion};             # mean motion in revs per day
711
712 #printf("%10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
713 #$mm1,$mm2,$bstar,$inclination,$raan,$eccentricity,$omegao,$xmo,$xno);
714         $raan=$raan*$d2r;
715         $omegao=$omegao*$d2r;
716         $xmo=$xmo*$d2r;
717         $inclination=$inclination*$d2r;
718         my $temp=2*$pi/$xmnpda/$xmnpda;
719         $xno=$xno*$temp*$xmnpda;
720         $mm1=$mm1*$temp;
721         $mm2=$mm2*$temp/$xmnpda;
722
723         my $c1=$ck2*1.5;
724         my $c2=$ck2/4.0;
725         my $c3=$ck2/2.0;
726         my $c4=$xj3*$ae**3/(4*$ck2);
727         my $cosio=cos($inclination);
728         my $sinio=sin($inclination);
729         my $a1=($xke/$xno)**(2./3.);
730         my $d1=$c1/$a1/$a1*(3*$cosio*$cosio-1)/(1-$eccentricity*$eccentricity)**1.5;
731         my $ao=$a1*(1-1./3.*$d1-$d1*$d1-134./81.*$d1*$d1*$d1);
732         my $po=$ao*(1-$eccentricity*$eccentricity);
733         $qo=$ao*(1-$eccentricity);
734         my $xlo=$xmo+$omegao+$raan;
735         my $d10=$c3*$sinio*$sinio;
736         my $d20=$c2*(7.*$cosio*$cosio-1);
737         my $d30=$c1*$cosio;
738         my $d40=$d30*$sinio;
739         my $po2no=$xno/($po*$po);
740         my $omgdt=$c1*$po2no*(5.*$cosio*$cosio-1);
741         my $xnodot=-2.*$d30*$po2no;
742         my $c5=0.5*$c4*$sinio*(3+5*$cosio)/(1+$cosio);
743         my $c6=$c4*$sinio;
744         
745         my $a=$xno+(2*$mm1+3*$mm2*$tsince)*$tsince;
746         $a=$ao*($xno/$a)**(2./3.);
747         my $e=1e-6;
748         $e =1-$qo/$a if ($a > $qo);
749         my $p=$a*(1-$e*$e);
750         my $xnodes=$raan+$xnodot*$tsince;
751         my $omgas=$omegao+$omgdt*$tsince;
752         my $xls=mod2p($xlo+($xno+$omgdt+$xnodot+($mm1+$mm2*$tsince)*$tsince)*$tsince);
753
754         my $axnsl=$e*cos($omgas);
755         my $aynsl=$e*sin($omgas)-$c6/$p;
756         my $xl=mod2p($xls-$c5/$p*$axnsl);
757
758         my $u=mod2p($xl-$xnodes);
759         my $item3;
760         my $eo1=$u;
761         my $tem5=1;
762         my $coseo1=0;
763         my $sineo1=0;
764         for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ )
765         {
766                 $sineo1=sin($eo1);
767                 $coseo1=cos($eo1);
768                 $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl;
769                 $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5;
770                 my $tem2=abs($tem5);
771                 $tem5=$tem2/$tem5 if ($tem2 > 1);
772                 $eo1=$eo1+$tem5;
773         }
774
775         $sineo1=sin($eo1);
776         $coseo1=cos($eo1);
777         my $ecose=$axnsl*$coseo1+$aynsl*$sineo1;
778         my $esine=$axnsl*$sineo1-$aynsl*$coseo1;
779         my $el2=$axnsl*$axnsl+$aynsl*$aynsl;
780         my $pl=$a*(1-$el2);
781         my $pl2=$pl*$pl;
782         my $r=$a*(1-$ecose);
783         my $rdot=$xke*sqrt($a)/$r*$esine;
784         my $rvdot=$xke*sqrt($pl)/$r;
785         $temp=$esine/(1+sqrt(1-$el2));
786         my $sinu=$a/$r*($sineo1-$aynsl-$axnsl*$temp);
787         my $cosu=$a/$r*($coseo1-$axnsl+$aynsl*$temp);
788         my $su=atan2($sinu,$cosu);
789
790         my $sin2u=($cosu+$cosu)*$sinu;
791         my $cos2u=1-2*$sinu*$sinu;
792         my $rk=$r+$d10/$pl*$cos2u;
793         my $uk=$su-$d20/$pl2*$sin2u;
794         my $xnodek=$xnodes+$d30*$sin2u/$pl2;
795         my $xinck=$inclination+$d40/$pl2*$cos2u;
796
797         my $sinuk=sin($uk);
798         my $cosuk=cos($uk);
799         my $sinnok=sin($xnodek);
800         my $cosnok=cos($xnodek);
801         my $sinik=sin($xinck);
802         my $cosik=cos($xinck);
803         my $xmx=-$sinnok*$cosik;
804         my $xmy=$cosnok*$cosik;
805         my $ux=$xmx*$sinuk+$cosnok*$cosuk;
806         my $uy=$xmy*$sinuk+$sinnok*$cosuk;
807         my $uz=$sinik*$sinuk;
808         my $vx=$xmx*$cosuk-$cosnok*$sinuk;
809         my $vy=$xmy*$cosuk-$sinnok*$sinuk;
810         my $vz=$sinik*$cosuk;
811
812         my $x=$rk*$ux*$xkmper/$ae;
813         my $y=$rk*$uy*$xkmper/$ae;
814         my $z=$rk*$uz*$xkmper/$ae;
815         my $xdot=$rdot*$ux;
816         my $ydot=$rdot*$uy;
817         my $zdot=$rdot*$uz;
818         $xdot=($rvdot*$vx+$xdot)*$xkmper/$ae*$xmnpda/86400;
819         $ydot=($rvdot*$vy+$ydot)*$xkmper/$ae*$xmnpda/86400;
820         $zdot=($rvdot*$vz+$zdot)*$xkmper/$ae*$xmnpda/86400;
821 #printf("x=%17.6f y=%17.6f z=%17.6f \n",$x,$y,$z);
822 #printf("xdot=%17.6f ydot=%17.6f zdot=%17.6f \n",$xdot,$ydot,$zdot);
823         my ($sat_lat,$sat_lon,$sat_alt,$sat_theta)=Calculate_LatLonAlt($x,$y,$z,$jtime);
824         my ($az, $el, $distance) = Calculate_Obs($x,$y,$z,$sat_theta,$xdot,$ydot,$zdot,$jtime,$lat,$lon,$alt);
825         return ($sat_lat,$sat_lon,$sat_alt,$az,$el,$distance);
826 }
827
828 sub Calculate_LatLonAlt
829 {
830 #
831 # convert from ECI coordinates to latitude, longitude and altitude.
832 #
833         my $x=shift;
834         my $y=shift;
835         my $z=shift;
836         my $time=shift;
837
838         my $theta=atan2($y,$x);
839         my $lon=mod2p($theta-ThetaG_JD($time));
840         my $range=sqrt($x**2+$y**2);
841         my $f=1/298.26;      # earth flattening constant
842         my $e2=$f*(2-$f);
843         my $xkmper=6378.135;
844         my $lat=atan2($z,$range);
845         my ($phi,$c);
846         do
847         {
848                 $phi=$lat;
849                 $c=1/sqrt(1-$e2*sin($phi)**2);
850                 $lat=atan2($z+$xkmper*$c*$e2*sin($phi),$range);
851         } until abs($lat-$phi) < 1e-10;
852         my $alt=$range/cos($lat)-$xkmper*$c;
853         return ($lat,$lon,$alt,$theta); # radians and kilometers
854         
855 }                       
856
857 sub Calculate_User_PosVel
858 {
859 # change from lat/lon/alt/time coordinates to earth centered inertial (ECI)
860 # position and local hour angle.
861         my $lat=shift;
862         my $lon=shift;
863         my $alt=shift;
864         my $time=shift;
865         my $theta=mod2p(ThetaG_JD($time)+$lon);
866         my $omega_E=1.00273790934; # earth rotations per sidereal day
867         my $secday=86400;
868         my $mfactor=2*$pi*$omega_E/$secday;
869         my $f=1/298.26;      # earth flattening constant
870         my $xkmper=6378.135;
871         my $c=1/sqrt(1+$f*($f-2)*sin($lat)**2);
872         my $s=(1-$f)*(1-$f)*$c;
873         my $achcp=($xkmper*$c+$alt)*cos($lat);
874         my $x_user=$achcp*cos($theta);
875         my $y_user=$achcp*sin($theta);
876         my $z_user=($xkmper*$s+$alt)*sin($lat);
877         my $xdot_user=-$mfactor*$y_user;
878         my $ydot_user=$mfactor*$x_user;
879         my $zdot_user=0;
880         return ($x_user,$y_user,$z_user,$xdot_user,$ydot_user,$zdot_user,$theta);
881 }
882 sub Calculate_Obs
883 {
884 # calculate the azimuth/el of an object as viewed from observers position
885 # with object position given in ECI coordinates and observer in lat/long/alt.
886 #
887 # inputs:       object ECI position vector (km)
888 #               object velocity vector (km/s)
889 #               julian time
890 #               observer lat,lon,altitude (km)
891         my $x=shift;
892         my $y=shift;
893         my $z=shift;
894         my $theta_s=shift;
895         my $xdot=shift; 
896         my $ydot=shift; 
897         my $zdot=shift; 
898         my $time=shift;
899         my $lat=shift;
900         my $lon=shift;
901         my $alt=shift;
902
903         my ($x_o,$y_o,$z_o,$xdot_o,$ydot_o,$zdot_o,$theta)=
904                 Calculate_User_PosVel($lat,$lon,$alt,$time);
905         my $xx=$x-$x_o;
906         my $yy=$y-$y_o;
907         my $zz=$z-$z_o;
908         my $xxdot=$xdot-$xdot_o;
909         my $yydot=$ydot-$ydot_o;
910         my $zzdot=$zdot-$zdot_o;
911
912         my $sin_lat=sin($lat);
913         my $cos_lat=cos($lat);
914         my $sin_theta=sin($theta);
915         my $cos_theta=cos($theta);
916         
917         my $top_s=$sin_lat*$cos_theta*$xx
918                 + $sin_lat*$sin_theta*$yy
919                 - $cos_lat*$zz;
920
921         my $top_e=-$sin_theta*$xx
922                 + $cos_theta*$yy;
923
924         my $top_z=$cos_lat*$cos_theta*$xx
925                 + $cos_lat*$sin_theta*$yy
926                 + $sin_lat*$zz;
927
928         my $az=atan(-$top_e/$top_s);
929         $az=$az+$pi if ( $top_s > 0 );
930         $az=$az+2*$pi if ( $az < 0 );
931
932         my $range=sqrt($xx*$xx+$yy*$yy+$zz*$zz);
933         my $el=asin($top_z/$range);
934         return ($az, $el, $range);
935 }
936
937 sub Calendar_date_and_time_from_JD
938 {
939         my ($jd,$z,$frac,$alpha,$a,$b,$c,$d,$e,$dom,$yr,$mon,$day,$hr,$min);
940         $jd=shift;
941         $jd=$jd+0.5;
942         $z=int($jd);
943         $frac=$jd-$z;
944         $alpha = int( ($z-1867216.5)/36524.25 );
945         $a=$z + 1 + $alpha - int($alpha/4);
946         $a=$z if( $z < 2299161 );
947         $b=$a+1524;
948         $c=int(($b-122.1)/365.25);
949         $d=int(365.25*$c);
950         $e=int(($b-$d)/30.6001);
951         $dom=$b-$d-int(30.6001*$e)+$frac;
952         $day=int($dom);
953         $mon=$e-1 if( $e < 14 );
954         $mon=$e-13 if( $e == 14 || $e == 15 );
955         $yr = $c-4716 if( $mon > 2 );
956         $yr = $c-4715 if( $mon == 1 || $mon == 2 );
957         $hr = int($frac*24);
958         $min= int(($frac*24 - $hr)*60+0.5);
959         if ($min == 60) {   # this may well prove inadequate DJK
960                 $hr += 1;
961                 $min = 0;
962         }
963         return ($yr,$mon,$day,$hr,$min);
964 }
965         
966
967