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