line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Astro::SunTime;
|
2
|
1
|
|
|
1
|
|
624
|
use vars qw(@ISA @EXPORT $VERSION);
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
96
|
|
3
|
|
|
|
|
|
|
$VERSION = 0.01;
|
4
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
5
|
|
|
|
|
|
|
@EXPORT = qw(sun_time);
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Results can be checked with: http://aa.usno.navy.mil/AA/data/docs/RS_OneYear.html
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
985
|
use Time::ParseDate;
|
|
1
|
|
|
|
|
14722
|
|
|
1
|
|
|
|
|
93
|
|
10
|
1
|
|
|
1
|
|
1113
|
use POSIX;
|
|
1
|
|
|
|
|
14114
|
|
|
1
|
|
|
|
|
6
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
3142
|
use strict;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
842
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# sun_time takes:
|
15
|
|
|
|
|
|
|
# type => 'rise' | 'set'
|
16
|
|
|
|
|
|
|
# latitude
|
17
|
|
|
|
|
|
|
# longitude
|
18
|
|
|
|
|
|
|
# time_zone => hours from GMT
|
19
|
|
|
|
|
|
|
# date => date parsable by Time::ParseDate::parsedate()
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub sun_time
|
22
|
|
|
|
|
|
|
{
|
23
|
0
|
|
|
0
|
0
|
|
my %params = @_;
|
24
|
|
|
|
|
|
|
|
25
|
0
|
|
0
|
|
|
|
my $type = $params{type} || 'rise';
|
26
|
0
|
|
0
|
|
|
|
my $latitude = $params{latitude} || 38.74274;
|
27
|
0
|
|
0
|
|
|
|
my $longitude = $params{longitude} || -90.560143;
|
28
|
0
|
|
0
|
|
|
|
my $time_zone = $params{time_zone} || -6;
|
29
|
0
|
|
|
|
|
|
my @suntime = localtime(parsedate($params{date}));
|
30
|
0
|
|
|
|
|
|
my $yday = $suntime[7] + 1;
|
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $A = 1.5708;
|
33
|
0
|
|
|
|
|
|
my $B = 3.14159;
|
34
|
0
|
|
|
|
|
|
my $C = 4.71239;
|
35
|
0
|
|
|
|
|
|
my $D = 6.28319;
|
36
|
0
|
|
|
|
|
|
my $E = 0.0174533 * $latitude;
|
37
|
0
|
|
|
|
|
|
my $F = 0.0174533 * $longitude;
|
38
|
0
|
|
|
|
|
|
my $G = 0.261799 * $time_zone;
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# For astronomical twilight, use R = -.309017
|
41
|
|
|
|
|
|
|
# For nautical twilight, use R = -.207912
|
42
|
|
|
|
|
|
|
# For civil twilight, use R = -.104528
|
43
|
|
|
|
|
|
|
# For sunrise or sunset, use R = -.0145439
|
44
|
0
|
|
|
|
|
|
my $R = -.0145439;
|
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
my $J = ($type eq 'rise') ? $A : $C;
|
47
|
0
|
|
|
|
|
|
my $K = $yday + (($J - $F) / $D);
|
48
|
0
|
|
|
|
|
|
my $L = ($K * .017202) - .0574039; # Solar Mean Anomoly
|
49
|
0
|
|
|
|
|
|
my $M = $L + .0334405 * sin($L); # Solar True Longitude
|
50
|
0
|
|
|
|
|
|
$M += 4.93289 + (3.49066E-04) * sin(2 * $L);
|
51
|
0
|
|
|
|
|
|
$M = &normalize($M, $D); # Quadrant Determination
|
52
|
0
|
0
|
|
|
|
|
$M += 4.84814E-06 if ($M / $A) - int($M / $A) == 0;
|
53
|
0
|
|
|
|
|
|
my $P = sin($M) / cos($M); # Solar Right Ascension
|
54
|
0
|
|
|
|
|
|
$P = atan2(.91746 * $P, 1);
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Quadrant Adjustment
|
57
|
0
|
0
|
|
|
|
|
if ($M > $C)
|
|
|
0
|
|
|
|
|
|
58
|
|
|
|
|
|
|
{
|
59
|
0
|
|
|
|
|
|
$P += $D;
|
60
|
|
|
|
|
|
|
}
|
61
|
|
|
|
|
|
|
elsif ($M > $A)
|
62
|
|
|
|
|
|
|
{
|
63
|
0
|
|
|
|
|
|
$P += $B;
|
64
|
|
|
|
|
|
|
}
|
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
my $Q = .39782 * sin($M); # Solar Declination
|
67
|
0
|
|
|
|
|
|
$Q = $Q / sqrt(-$Q * $Q + 1); # This is how the original author wrote it!
|
68
|
0
|
|
|
|
|
|
$Q = atan2($Q, 1);
|
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $S = $R - (sin($Q) * sin($E));
|
71
|
0
|
|
|
|
|
|
$S = $S / (cos($Q) * cos($E));
|
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
return 'none' if abs($S) > 1; # Null phenomenon
|
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
$S = $S / sqrt(-$S * $S + 1);
|
76
|
0
|
|
|
|
|
|
$S = $A - atan2($S, 1);
|
77
|
0
|
0
|
|
|
|
|
$S = $D - $S if $type eq 'rise';
|
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time
|
80
|
0
|
|
|
|
|
|
my $U = $T - $F; # Universal timer
|
81
|
0
|
|
|
|
|
|
my $V = $U + $G; # Wall clock time
|
82
|
0
|
|
|
|
|
|
$V = &normalize($V, $D);
|
83
|
0
|
|
|
|
|
|
$V = $V * 3.81972;
|
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $hour = int($V);
|
86
|
0
|
|
|
|
|
|
my $min = int(($V - $hour) * 60 + 0.5);
|
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
@suntime[2,1,0,8] = ($hour, $min, 0, 0);
|
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
@suntime = localtime(mktime(@suntime)); # normalize date structure
|
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return sprintf("%d:%02d", @suntime[2,1]);
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub normalize
|
96
|
|
|
|
|
|
|
{
|
97
|
0
|
|
|
0
|
0
|
|
my $Z = shift;
|
98
|
0
|
|
|
|
|
|
my $D = shift;
|
99
|
|
|
|
|
|
|
|
100
|
0
|
0
|
|
|
|
|
die "Trying to normalize with zero offset..." if ($D == 0);
|
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
while ($Z < 0) {$Z = $Z + $D}
|
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
while ($Z >= $D) {$Z = $Z - $D}
|
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return $Z;
|
106
|
|
|
|
|
|
|
}
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1;
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__
|