X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FSun.pm;h=5190db184e58c2d311ce5ddf60ac3abd9d0a2873;hb=f63d598af3f797b56b8d5e23ec4ff5254192eee9;hp=4bb349f6ee77d93e5168a69005b77667ece4ee4f;hpb=589de1133c9ef2ecc6567473c24cff1717fb604b;p=spider.git diff --git a/perl/Sun.pm b/perl/Sun.pm index 4bb349f6..5190db18 100644 --- a/perl/Sun.pm +++ b/perl/Sun.pm @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#/usr/bin/perl -w # # This module was written by Steve Franke K9AN. # November, 1999. @@ -16,23 +16,42 @@ # # Copyright (c) 1999 - Steve Franke K9AN # -# $Id$ +# # +# 2005/02/25 add calculation of civil dawn and dusk, defined to be times +# when solar zenith angle is 96 degrees. +# 2001/12/16 Fixed Julian_Date_of_Epoch and now I actually use it... +# 2001/09/15 some changes to take care of cases where the object +# doesn't rise or set on a given day... package Sun; -use POSIX; require Exporter; @ISA = qw(Exporter); @EXPORT = qw($pi $d2r $r2d ); use strict; -use vars qw($pi $d2r $r2d ); - -$pi = 3.141592653589; -$d2r = ($pi/180); -$r2d = (180/$pi); + +use vars qw(%keps); +use Keps; +use DXVars; +use DXUtil; +use DXDebug; + +use POSIX qw(:math_h); + +# reload the keps data +sub load +{ + my @out; + my $s = readfilestr("$main::root/local/Keps.pm"); + if ($s) { + eval $s; + push @out, $@ if $@; + } + return @out; +} sub Julian_Day { @@ -50,13 +69,18 @@ sub Julian_Day sub Julian_Date_of_Epoch { my $epoch=shift; - my $year=int($epoch*1e-3); - $year=$year+2000 if ($year < 57); - $year=$year+1900 if ($year >= 57); - my $day=$epoch-$year*1e3; + my $year=int($epoch/1000); + my $day=$epoch-$year*1000; + if ($year < 57 ) { + $year=$year+2000; + } + else { + $year=$year+1900; + } my $Julian_Date_of_Epoch=Julian_Date_of_Year($year)+$day; return $Julian_Date_of_Epoch; } + sub Julian_Date_of_Year { my $year=shift; @@ -135,10 +159,13 @@ sub rise_set my $lat = shift; my $lon = shift; my $sun0_moon1=shift; # 0 for sun, 1 for moon, 2 for venus... + my ($alpha1,$delta1,$alpha2,$delta2,$alpha3,$delta3); + my ($aznow,$hnow,$alphanow,$deltanow,$distance,$distancenow); + my ($h0,$H); + my ($risetime,$settime); + my ($dawntime,$dusktime); - my ($alpha1,$alpha2,$alpha3,$delta1,$delta2,$delta3); - my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$h0,$aznow,$hnow,$corr); - my ($i,$arg,$argtest,$H0,$alphanow,$deltanow,$distance,$distancenow); + my ($ifrac,$ifracnow); my $julianday=Julian_Day($year,$month,$day); my $tt1 = ($julianday-1-2451545)/36525.; @@ -159,36 +186,74 @@ sub rise_set ($alpha2, $delta2)=get_sun_alpha_delta($tt2); ($alpha3, $delta3)=get_sun_alpha_delta($tt3); ($alphanow, $deltanow)=get_sun_alpha_delta($ttnow); - $h0=-0.8333; $H=$thetanow-$lon-$alphanow; $H=reduce_angle_to_360($H); ($aznow,$hnow)=get_az_el($H,$deltanow,$lat); $hnow=$hnow + 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60; + $h0=-0.8333; # this is for sun rise and sun set + ($risetime,$settime)= + do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1, + $alpha2,$delta2,$alpha3,$delta3); + $h0=-6.0; # this is for civil dawn and dusk + ($dawntime,$dusktime)= + do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1, + $alpha2,$delta2,$alpha3,$delta3); + $dawntime = "------" if( $dawntime eq "NoRise" ); + $dusktime = "------" if( $dusktime eq "NoSet " ); + + return ( + sprintf("%s", $dawntime), sprintf("%s",$risetime), + sprintf("%s", $settime), sprintf("%s",$dusktime), + $aznow+180,$hnow + ); } if ( $sun0_moon1 == 1 ) { - ($alpha1, $delta1, $distance)=get_moon_alpha_delta($tt1); - ($alpha2, $delta2, $distance)=get_moon_alpha_delta($tt2); - ($alpha3, $delta3, $distance)=get_moon_alpha_delta($tt3); - ($alphanow, $deltanow, $distancenow)=get_moon_alpha_delta($ttnow); - $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34./60.; + ($alpha1, $delta1, $distance, $ifrac)=get_moon_alpha_delta($tt1); + ($alpha2, $delta2, $distance, $ifrac)=get_moon_alpha_delta($tt2); + ($alpha3, $delta3, $distance, $ifrac)=get_moon_alpha_delta($tt3); + ($alphanow, $deltanow, $distancenow, $ifracnow)=get_moon_alpha_delta($ttnow); + $h0=0.7275*$r2d*asin(6378.14/$distancenow)-34.0/60.; $H=$thetanow-$lon-$alphanow; $H=reduce_angle_to_360($H); ($aznow,$hnow)=get_az_el($H,$deltanow,$lat); $hnow=$hnow-$r2d*asin(sin(6378.14/$distancenow)*cosdeg($hnow))+ 1.02/(tandeg($hnow+10.3/($hnow+5.11)))/60; + ($risetime,$settime)= + do_rise_set_calculations($h0,$theta0,$lat,$lon,$alpha1,$delta1, + $alpha2,$delta2,$alpha3,$delta3); + return (sprintf("%s", $risetime), sprintf("%s",$settime), + $aznow+180,$hnow, -40*log10($distance/385000), $ifracnow ); + } - $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2)); - $argtest = tandeg($lat)*tandeg($delta2); +} - if ( $argtest < -1. ) { - return sprintf("Doesn't rise."); - } - if ( $argtest > 1. ) { - return sprintf("Doesn't set."); - } +sub do_rise_set_calculations +{ + my $norise = 0; + my $noset = 0; + my ($risehr,$risemin,$risetime,$sethr,$setmin,$settime); + my ($m0,$m1,$m2,$theta,$alpha,$delta,$H,$az,$h,$corr); + my ($i,$arg,$argtest,$H0); + + my $h0=shift; + my $theta0=shift; + my $lat=shift; + my $lon=shift; + my $alpha1=shift; + my $delta1=shift; + my $alpha2=shift; + my $delta2=shift; + my $alpha3=shift; + my $delta3=shift; + + $arg = (sindeg($h0)-sindeg($lat)*sindeg($delta2))/(cosdeg($lat)*cosdeg($delta2)); + if ( abs($arg) > 1. ) { # either up all day or down all day + $norise = 1; # leave it to the user to examine + $noset = 1; # the elevation angle (or look outside!) + } # to figure out which. $H0 = acos($arg)*$r2d; my $aa=$alpha2-$alpha1; @@ -221,67 +286,86 @@ sub rise_set $corr=-$H/360; $m0=$m0+$corr; $m0=$m0+1 if( $m0 < 0 ); - $m0=$m0-1 if( $m0 > 1 ); + $m0=$m0-1 if( $m0 >= 1 ); } - $m1 = $m0 - $H0/360.; - $m1=$m1+1 if( $m1 < 0 ); - $m1=$m1-1 if( $m1 > 1 ); - for ($i=1; $i<=2; $i++) { - $theta = $theta0+360.985647*$m1; - $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2; - $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2; - $H=$theta-$lon-$alpha; - $H=reduce_angle_to_360($H); - ($az,$h)=get_az_el($H,$delta,$lat); - $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H))); - $m1=$m1+$corr; + + if( !$norise ){ + $m1 = $m0 - $H0/360.; $m1=$m1+1 if( $m1 < 0 ); $m1=$m1-1 if( $m1 > 1 ); + for ($i=1; $i<=2; $i++) { + $theta = $theta0+360.985647*$m1; + $alpha=$alpha2+$m1*($aa+$ba+$m1*$ca)/2; + $delta=$delta2+$m1*($ad+$bd+$m1*$cd)/2; + $H=$theta-$lon-$alpha; + $H=reduce_angle_to_360($H); + ($az,$h)=get_az_el($H,$delta,$lat); + $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H))); + $m1=$m1+$corr; +# $norise=1 if( $m1 < 0 || $m1 > 1); + $m1=$m1-1 if( $m1 >= 1); + $m1=$m1+1 if( $m1 < 0); + } } - $m2 = $m0 + $H0/360.; - $m2=$m2+1 if( $m2 < 0 ); - $m2=$m2-1 if( $m2 > 1 ); - for ($i=1; $i<=2; $i++) { - $theta = $theta0+360.985647*$m2; - $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2; - $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2; - $H=$theta-$lon-$alpha; - $H=reduce_angle_to_360($H); - ($az,$h)=get_az_el($H,$delta,$lat); - $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H))); - $m2 = $m2 + $corr; - $m2=$m2+1 if( $m2 < 0 ); - $m2=$m2-1 if( $m2 > 1 ); - } - my ($risehr,$risemin,$sethr,$setmin); - $risehr=int($m1*24); - $risemin=($m1*24-int($m1*24))*60+0.5; - if ( $risemin >= 60 ) { - $risemin=$risemin-60; - $risehr=$risehr+1; - } - $sethr=int($m2*24); - $setmin=($m2*24-int($m2*24))*60+0.5; - if ( $setmin >= 60 ) { - $setmin=$setmin-60; - $sethr=$sethr+1; + if( !$norise ) { + $risehr=int($m1*24); + $risemin=($m1*24-int($m1*24))*60+0.5; + if ( $risemin >= 60 ) { + $risemin=$risemin-60; + $risehr=$risehr+1; + } + $risehr=0 if($risehr==24); + $risetime=sprintf("%02d:%02dZ",$risehr,$risemin); + } else { + $risetime="NoRise"; } - if ( $sun0_moon1 == 0 ) { - return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin),$aznow+180,$hnow); - } - if ( $sun0_moon1 == 1 ) { - return (sprintf("%02d:%02dZ", $risehr,$risemin), sprintf("%02d:%02dZ",$sethr,$setmin), - $aznow+180,$hnow, -40*log10($distance/385000) ); + if( !$noset ){ + $m2 = $m0 + $H0/360.; + $m2=$m2+1 if( $m2 < 0 ); + $m2=$m2-1 if( $m2 >= 1 ); + for ($i=1; $i<=2; $i++) { + $theta = $theta0+360.985647*$m2; + $alpha=$alpha2+$m2*($aa+$ba+$m2*$ca)/2; + $delta=$delta2+$m2*($ad+$bd+$m2*$cd)/2; + $H=$theta-$lon-$alpha; + $H=reduce_angle_to_360($H); + ($az,$h)=get_az_el($H,$delta,$lat); + $corr=($h-$h0)/(360*(cosdeg($delta)*cosdeg($lat)*sindeg($H))); + $m2 = $m2 + $corr; +# $noset=1 if( $m2 < 0 || $m2 > 1); + $m2=$m2-1 if( $m2 >= 1); + $m2=$m2+1 if( $m2 < 0); + } } + + if( !$noset ) { + $sethr=int($m2*24); + $setmin=($m2*24-int($m2*24))*60+0.5; + if ( $setmin >= 60 ) { + $setmin=$setmin-60; + $sethr=$sethr+1; + } + $sethr=0 if($sethr==24); + $settime=sprintf("%02d:%02dZ",$sethr,$setmin); + } else { + $settime="NoSet "; + } + return $risetime,$settime; } + + + sub get_moon_alpha_delta { # # Calculate the moon's right ascension and declination # + # As of October 2001, also calculate the illuminated fraction of the + # moon's disk... (why not?) + # my $tt=shift; my $Lp=218.3164477+481267.88123421*$tt- @@ -501,7 +585,20 @@ sub get_moon_alpha_delta my $delta=asin(cosdeg($beta)*sindeg($epsilon)*sindeg($lambda)+sindeg($beta)*cosdeg($epsilon))*$r2d; $delta = reduce_angle_to_360($delta); - return ($alpha,$delta,$distance); +# $phase will be the "moon phase angle" from p. 346 of Meeus' book... + my $phase=180.0 - $D - 6.289 *sindeg($Mp) + + 2.100 *sindeg($M) + - 1.274 *sindeg(2.*$D - $Mp) + - 0.658 *sindeg(2.*$D) + - 0.214 *sindeg(2.*$Mp) + - 0.110 *sindeg($D); + +# $illum_frac is the fraction of the disk that is illuminated, and will be +# zero at new moon and 1.0 at full moon. + + my $illum_frac = (1.0 + cosdeg( $phase ))/2.; + + return ($alpha,$delta,$distance,$illum_frac); } sub get_sun_alpha_delta @@ -563,50 +660,6 @@ sub get_satellite_pos # #Temporary keps database... # -my %keps = ( - noaa15 => { - number => 25338, - id => 98030, - epoch => 99341.00000000, - mm1 => .00000376, - mm2 => .00000e-0, - bstar => .18612e-3, - inclination => 98.6601, - raan => 8.2003, - eccentricity => .0011401, - argperigee => 112.4684, - meananomaly => 42.5140, - meanmotion => 14.23047277081382, - }, - tdrs5 => { - number => 21639, - id => 91054, - epoch => 99341.34471854, - mm1 => .00000095, - mm2 => .00000e-0, - bstar => .10000e-3, - inclination => 1.5957, - raan => 88.4884, - eccentricity => .003028, - argperigee => 161.6582, - meananomaly => 135.4323, - meanmotion => 1.00277774, - }, - oscar16 => { - number => 20439, - id => 90005, - epoch => 99341.14501399, - mm1 => .00000343, - mm2 => .00000e-0, - bstar => .14841e-3, - inclination => 98.4690, - raan => 55.0032, - eccentricity => .0012163, - argperigee => 66.4615, - meananomaly => 293.7842, - meanmotion => 14.303202855, - }, -); my $jtime = shift; my $lat = shift; my $lon = shift; @@ -631,14 +684,7 @@ my %keps = ( my $epoch = $sat_ref ->{epoch}; #printf("epoch = %10.2f\n",$epoch); - my $epoch_year=int($epoch/1000); - my $epoch_day=$epoch-int(1000*$epoch_year); -#printf("epoch_year = %10.2f\n",$epoch_year); -#printf("epoch_day = %17.12f\n",$epoch_day); - $epoch_year=$epoch_year+2000 if ($epoch_year < 57); - $epoch_year=$epoch_year+1900 if ($epoch_year >= 57); - my $jt_epoch=Julian_Date_of_Year($epoch_year); - $jt_epoch=$jt_epoch+$epoch_day; + my $jt_epoch=Julian_Date_of_Epoch($epoch); #printf("JT for epoch = %17.12f\n",$jt_epoch); my $tsince=($jtime-$jt_epoch)*24*60; #printf("tsince (min) = %17.12f\n",$tsince); @@ -700,16 +746,15 @@ my %keps = ( my $xl=mod2p($xls-$c5/$p*$axnsl); my $u=mod2p($xl-$xnodes); - my $item3=0; + my $item3; my $eo1=$u; my $tem5=1; my $coseo1=0; my $sineo1=0; - while ( abs($tem5) >= 1e-6 && $item3 < 10 ) + for ($item3=0; abs($tem5) >= 1e-6 && $item3 < 10; $item3++ ) { $sineo1=sin($eo1); $coseo1=cos($eo1); - $item3 = $item3+1; $tem5=1-$coseo1*$axnsl-$sineo1*$aynsl; $tem5=($u-$aynsl*$coseo1+$axnsl*$sineo1-$eo1)/$tem5; my $tem2=abs($tem5); @@ -901,6 +946,10 @@ sub Calendar_date_and_time_from_JD $yr = $c-4715 if( $mon == 1 || $mon == 2 ); $hr = int($frac*24); $min= int(($frac*24 - $hr)*60+0.5); + if ($min == 60) { # this may well prove inadequate DJK + $hr += 1; + $min = 0; + } return ($yr,$mon,$day,$hr,$min); }