| 1 | |||||||||||||||||||
| 2 | # Copyright (c) 1999-2017 Rob Fugina <robf@fugina.com> | ||||||||||||||||||
| 3 | # Distributed under the terms of the GNU Public License, Version 3.0 | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | package Astro::SunTime; | ||||||||||||||||||
| 6 | 2 | ▶ | use vars qw(@ISA @EXPORT $VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 2 | $VERSION = 0.06; | |||||||||||||||||
| 8 | 2 | @ISA = qw(Exporter); | |||||||||||||||||
| 9 | 2 | @EXPORT = qw(sun_time); | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | # Results can be checked with: http://aa.usno.navy.mil/data/docs/RS_OneYear.php | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | # 09/03/00 :: winter Make ParseDate optional. It is overkill and I could not get it to | ||||||||||||||||||
| 14 | # compile in perl2exe. It gave runaway comment errors :( | ||||||||||||||||||
| 15 | # 10/12/00 :: winter Change time_zone check to defined, to allow for time_zone 0 | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 2 | ▶ | use POSIX; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | # sun_time takes: | ||||||||||||||||||
| 22 | # type => 'rise' | 'set' | ||||||||||||||||||
| 23 | # latitude | ||||||||||||||||||
| 24 | # longitude | ||||||||||||||||||
| 25 | # time_zone => hours from GMT | ||||||||||||||||||
| 26 | # date => date parsable by Time::ParseDate::parsedate() | ||||||||||||||||||
| 27 | # time => to feed to localtime | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | sub sun_time | ||||||||||||||||||
| 30 | 16 | ▶ | { | ||||||||||||||||
|
Subroutine
sun_time: called CC=17 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||
| 31 | 16 | my %params = @_; | |||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 16 | ▶ | my $type = $params{type} || 'rise'; | ||||||||||||||||
|
Condition50%1/2
$params{'type'} || 'rise'
MC/DC0%0/1
$params{'type'} || 'rise'
$params{'type'}
Truth table50%1/2
A
A= $params{'type'}
| |||||||||||||||||||
| 34 | 16 | ▶ | my $latitude = (defined $params{latitude}) ? $params{latitude} : 38.74274; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | 16 | ▶ | my $longitude = (defined $params{longitude}) ? $params{longitude} : -90.560143; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | 16 | ▶ | my $time_zone = (defined $params{time_zone}) ? $params{time_zone} : -6; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 16 | my $time; | |||||||||||||||||
| 39 | 16 | ▶ | if ($params{date}) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 40 | 16 | require Time::ParseDate; | |||||||||||||||||
| 41 | 16 | $time = Time::ParseDate::parsedate($params{date}); | |||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | elsif ($params{time}) { | ||||||||||||||||||
| 44 | 0 | $time = $params{time}; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | else { | ||||||||||||||||||
| 47 | 0 | $time = time; | |||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | 16 | my @suntime = localtime($time); | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 16 | my $yday = $suntime[7] + 1; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 16 | my $A = 1.5708; | |||||||||||||||||
| 54 | 16 | my $B = 3.14159; | |||||||||||||||||
| 55 | 16 | my $C = 4.71239; | |||||||||||||||||
| 56 | 16 | my $D = 6.28319; | |||||||||||||||||
| 57 | 16 | my $E = 0.0174533 * $latitude; | |||||||||||||||||
| 58 | 16 | my $F = 0.0174533 * $longitude; | |||||||||||||||||
| 59 | 16 | my $G = 0.261799 * $time_zone; | |||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | # For astronomical twilight, use R = -.309017 | ||||||||||||||||||
| 62 | # For nautical twilight, use R = -.207912 | ||||||||||||||||||
| 63 | # For civil twilight, use R = -.104528 | ||||||||||||||||||
| 64 | # For sunrise or sunset, use R = -.0145439 | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 16 | my $R = -.0145439; | |||||||||||||||||
| 67 | 16 | ▶ | if ($params{twilight}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | 0 | ▶ | if($params{twilight} eq 'astronomical') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 69 | 0 | $R = -.309017; | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | elsif($params{twilight} eq 'nautical') { | ||||||||||||||||||
| 72 | 0 | $R = -.207912; | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | elsif($params{twilight} eq 'civil') { | ||||||||||||||||||
| 75 | 0 | $R = -.104528; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 16 | ▶ | my $J = ($type eq 'rise') ? $A : $C; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | 16 | my $K = $yday + (($J - $F) / $D); | |||||||||||||||||
| 82 | 16 | my $L = ($K * .017202) - .0574039; # Solar Mean Anomoly | |||||||||||||||||
| 83 | 16 | my $M = $L + .0334405 * sin($L); # Solar True Longitude | |||||||||||||||||
| 84 | 16 | $M += 4.93289 + (3.49066E-04) * sin(2 * $L); | |||||||||||||||||
| 85 | 16 | $M = &normalize($M, $D); # Quadrant Determination | |||||||||||||||||
| 86 | 16 | ▶ | $M += 4.84814E-06 if ($M / $A) - int($M / $A) == 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | 16 | my $P = sin($M) / cos($M); # Solar Right Ascension | |||||||||||||||||
| 88 | 16 | $P = atan2(.91746 * $P, 1); | |||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | # Quadrant Adjustment | ||||||||||||||||||
| 91 | 16 | ▶ | if ($M > $C) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 92 | { | ||||||||||||||||||
| 93 | 8 | $P += $D; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | elsif ($M > $A) | ||||||||||||||||||
| 96 | { | ||||||||||||||||||
| 97 | 8 | $P += $B; | |||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | 16 | my $Q = .39782 * sin($M); # Solar Declination | |||||||||||||||||
| 101 | 16 | $Q = $Q / sqrt(-$Q * $Q + 1); # This is how the original author wrote it! | |||||||||||||||||
| 102 | 16 | $Q = atan2($Q, 1); | |||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 16 | my $S = $R - (sin($Q) * sin($E)); | |||||||||||||||||
| 105 | 16 | $S = $S / (cos($Q) * cos($E)); | |||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 16 | ▶ | return 'none' if abs($S) > 1; # Null phenomenon | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 16 | $S = $S / sqrt(-$S * $S + 1); | |||||||||||||||||
| 110 | 16 | $S = $A - atan2($S, 1); | |||||||||||||||||
| 111 | 16 | ▶ | $S = $D - $S if $type eq 'rise'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 16 | my $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time | |||||||||||||||||
| 114 | 16 | my $U = $T - $F; # Universal timer | |||||||||||||||||
| 115 | 16 | my $V = $U + $G; # Wall clock time | |||||||||||||||||
| 116 | 16 | $V = &normalize($V, $D); | |||||||||||||||||
| 117 | 16 | $V = $V * 3.81972; | |||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 16 | my $hour = int($V); | |||||||||||||||||
| 120 | 16 | my $min = int(($V - $hour) * 60 + 0.5); | |||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 16 | @suntime[2,1,0] = ($hour, $min, 0); | |||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 16 | @suntime = localtime(mktime(@suntime)); # normalize date structure | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 16 | return sprintf("%d:%02d", @suntime[2,1]); | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | sub normalize | ||||||||||||||||||
| 130 | 32 | ▶ | { | ||||||||||||||||
|
Subroutine
normalize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||
| 131 | 32 | my $Z = shift; | |||||||||||||||||
| 132 | 32 | my $D = shift; | |||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 32 | ▶ | die "Trying to normalize with zero offset..." if ($D == 0); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 32 | ▶ | while ($Z < 0) {$Z = $Z + $D} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 137 | 32 | ▶ | while ($Z >= $D) {$Z = $Z - $D} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | 32 | return $Z; | |||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 2 | 1; | |||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||