blib/lib/Astro/Sunrise.pm

filter: Statementstmt 76.0% 222 / 292 Branchbran 78.7% 74 / 94 Conditioncond 82.5% 33 / 40 MC/DCmcdc 68.1% 15 / 22 Subroutinesub 91.8% 34 / 37 Podpod 52.3% 11 / 21 totaltotal 76.8% 389 / 506 CC 66 SCAR 26.4
CC
66
Cov
77%
sunrise
30.4
sun_rise_sun_set
15.4
sun_rise_set
13.5
CRAP 14.0
↑ Summary
1 # -*- encoding: utf-8; indent-tabs-mode: nil -*-
2 #
3 # Perl extension for computing the sunrise/sunset on a given day
4 # Copyright (C) 1999-2003, 2013, 2015, 2017, 2019, 2021 Ron Hill and Jean Forget
5 #
6 # See the license in the embedded documentation below.
7 #
8 package Astro::Sunrise;
9  
10 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $RADEG $DEGRAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 require Exporter;
18  
19 11 @ISA = qw( Exporter );
20 11 @EXPORT = qw( sunrise sun_rise sun_set );
21 11 @EXPORT_OK = qw( DEFAULT CIVIL NAUTICAL AMATEUR ASTRONOMICAL sind cosd tand asind acosd atand atan2d equal );
22 11 %EXPORT_TAGS = (
23 constants => [ qw/DEFAULT CIVIL NAUTICAL AMATEUR ASTRONOMICAL/ ],
24 trig => [ qw/sind cosd tand asind acosd atand atan2d equal/ ],
25 );
26  
27 11 $VERSION = '0.99';
28 11 $RADEG = ( 180 / pi );
29 11 $DEGRAD = ( pi / 180 );
30 11 my $INV360 = ( 1.0 / 360.0 );
31  
32 5 ▶sub sun_rise {
Subroutine sun_rise: called CC=1 SCAR=0.0
Pod documented
33 5 my ($sun_rise, undef) = sun_rise_sun_set(@_);
34 2 return $sun_rise;
35 }
36 2 ▶sub sun_set {
Subroutine sun_set: called CC=1 SCAR=0.0
Pod documented
37 2 my (undef, $sun_set) = sun_rise_sun_set(@_);
38 2 return $sun_set;
39 }
40  
41 7 ▶sub sun_rise_sun_set {
Subroutine sun_rise_sun_set: called CC=15 SCAR=27.4
Pod undocumented
42 7 my %arg;
43 7 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 7 0 7
44 7 %arg = %{$_[0]};
45 }
46 else {
47 0 @arg{ qw/lon lat alt offset/ } = @_;
48 }
49  
50 # This trick aims to fulfill two antagonistic purposes:
51 # -- do not load DateTime if the only function called is "sunrise"
52 # -- load DateTime implicitly if the user calls "sun_rise" or "sun_set". This is to be backward
53 # compatible with 0.92 or earlier, when Astro::Sunrise would load DateTime and thus, allow
54 # the user to remove this line from his script.
55 7 ▶ unless ($INC{DateTime}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"DateTime"}) 7 0 7
56 7 ▶ eval "use DateTime";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
57 7 ▶ croak $@
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
58 if $@;
59 }
60  
61 7 my ($longitude, $latitude) = @arg{ qw/lon lat/ };
62 7 ▶ my $alt = defined($arg{alt} ) ? $arg{alt} : -0.833;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'alt'} ? : 2 5 7
63 7 ▶ my $offset = defined($arg{offset} ) ? int($arg{offset}) : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'offset'} ? : 2 5 7
64 7 ▶ my $tz = defined($arg{time_zone}) ? $arg{time_zone} : 'local';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'time_zone'} ? : 4 3 7
65 7 ▶ $arg{precise} ||= 0;
Condition50%1/2
$arg{"precise"} ||= 0
l!l
07
MC/DC0%0/1
$arg{"precise"} ||= 0
$arg{"precise"}
Truth table50%1/2
A
A= $arg{"precise"}
Aresult
11
00
66 7 ▶ $arg{upper_limb} ||= 0;
Condition50%1/2
$arg{"upper_limb"} ||= 0
l!l
07
MC/DC0%0/1
$arg{"upper_limb"} ||= 0
$arg{"upper_limb"}
Truth table50%1/2
A
A= $arg{"upper_limb"}
Aresult
11
00
67 7 ▶ $arg{polar} ||= 'warn';
Condition100%2/2
$arg{"polar"} ||= "warn"
l!l
16
MC/DC100%1/1
$arg{"polar"} ||= "warn"
$arg{"polar"}
Truth table100%2/2
A
A= $arg{"polar"}
Aresult
11
00
68 7 ▶ $arg{trace} ||= 0;
Condition50%1/2
$arg{"trace"} ||= 0
l!l
07
MC/DC0%0/1
$arg{"trace"} ||= 0
$arg{"trace"}
Truth table50%1/2
A
A= $arg{"trace"}
Aresult
11
00
69 7 ▶ croak "Longitude parameter (keyword: 'lon') is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $longitude 1 6 7
70 unless defined $longitude;
71 6 ▶ croak "Latitude parameter (keyword: 'lat') is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $latitude 1 5 6
72 unless defined $latitude;
73 croak "Wrong value of the 'polar' argument: should be either 'warn' or 'retval'"
74 5 ▶ if $arg{polar} ne 'warn' and $arg{polar} ne 'retval';
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"polar"} ne "warn" and $arg{"polar"} ne "retval" 1 4 5
Condition66%2/3
$arg{"polar"} ne "warn" and $arg{"polar"} ne "retval"
!ll&&!rl&&r
401
MC/DC50%1/2
$arg{"polar"} ne "warn" and $arg{"polar"} ne "retval"
$arg{"polar"} ne "warn" $arg{"polar"} ne "retval"
Truth table66%2/3
A and B
A= $arg{"polar"} ne "warn"B= $arg{"polar"} ne "retval"
ABresult
0X0
100
111
75  
76 4 my $today = DateTime->today(time_zone => $tz);
77 4 $today->set( hour => 12 );
78 4 $today->add( days => $offset );
79  
80 my( $sun_rise, $sun_set ) = sunrise( { year => $today->year,
81 month => $today->mon,
82 day => $today->mday,
83 lon => $longitude,
84 lat => $latitude,
85 tz => ( $today->offset / 3600 ),
86 #
87 # DST is always 0 because DateTime
88 # currently (v 0.16) adds one to the
89 # offset during DST hours
90 isdst => 0,
91 alt => $alt,
92 precise => $arg{precise},
93 upper_limb => $arg{upper_limb},
94 polar => $arg{polar},
95 trace => $arg{trace},
96 4 } );
97 4 return ($sun_rise, $sun_set);
98 }
99  
100 957 ▶sub sunrise {
Subroutine sunrise: called CC=30 SCAR=34.1
Pod documented
101 957 my %arg;
102 957 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 840 117 957
103 840 %arg = %{$_[0]};
104 }
105 else {
106 117 @arg{ qw/year month day lon lat tz isdst alt precise/ } = @_;
107 }
108 my ( $year, $month, $day, $lon, $lat, $TZ, $isdst, $trace)
109 957 = @arg{ qw/year month day lon lat tz isdst trace/ };
110 957 ▶ my $altit = defined($arg{alt} ) ? $arg{alt} : -0.833;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'alt'} ? : 834 123 957
111 957 ▶ $arg{precise} ||= 0;
Condition100%2/2
$arg{"precise"} ||= 0
l!l
182775
MC/DC100%1/1
$arg{"precise"} ||= 0
$arg{"precise"}
Truth table100%2/2
A
A= $arg{"precise"}
Aresult
11
00
112 957 ▶ $arg{upper_limb} ||= 0;
Condition100%2/2
$arg{"upper_limb"} ||= 0
l!l
414543
MC/DC100%1/1
$arg{"upper_limb"} ||= 0
$arg{"upper_limb"}
Truth table100%2/2
A
A= $arg{"upper_limb"}
Aresult
11
00
113 957 ▶ $arg{polar} ||= 'warn';
Condition100%2/2
$arg{"polar"} ||= "warn"
l!l
365592
MC/DC100%1/1
$arg{"polar"} ||= "warn"
$arg{"polar"}
Truth table100%2/2
A
A= $arg{"polar"}
Aresult
11
00
114 957 ▶ $trace ||= 0;
Condition50%1/2
$trace ||= 0
l!l
0957
MC/DC0%0/1
$trace ||= 0
$trace
Truth table50%1/2
A
A= $trace
Aresult
11
00
115 957 ▶ croak "Year parameter is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $year 1 956 957
116 unless defined $year;
117 956 ▶ croak "Month parameter is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $month 1 955 956
118 unless defined $month;
119 955 ▶ croak "Day parameter is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $day 1 954 955
120 unless defined $day;
121 954 ▶ croak "Longitude parameter (keyword: 'lon') is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lon 1 953 954
122 unless defined $lon;
123 953 ▶ croak "Latitude parameter (keyword: 'lat') is mandatory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lat 1 952 953
124 unless defined $lat;
125 croak "Wrong value of the 'polar' argument: should be either 'warn' or 'retval'"
126 952 ▶ if $arg{polar} ne 'warn' and $arg{polar} ne 'retval';
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"polar"} ne "warn" and $arg{"polar"} ne "retval" 1 951 952
Condition100%3/3
$arg{"polar"} ne "warn" and $arg{"polar"} ne "retval"
!ll&&!rl&&r
5913601
MC/DC100%2/2
$arg{"polar"} ne "warn" and $arg{"polar"} ne "retval"
$arg{"polar"} ne "warn" $arg{"polar"} ne "retval"
Truth table100%3/3
A and B
A= $arg{"polar"} ne "warn"B= $arg{"polar"} ne "retval"
ABresult
0X0
100
111
127  
128 951 ▶ if (! $arg{precise}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg{'precise'}) { } 769 182 951
129 769 my $revsub = \&rev180; # normalizing angles around 0 degrees
130 769 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 769 769
131 printf $trace "\nBasic computation for %04d-%02d-%02d, lon %.3f, lat %.3f, altitude %.3f, upper limb %d\n"
132 , $year, $month, $day
133 , $lon
134 , $lat
135 , $altit
136 0 , $arg{upper_limb};
137 }
138 769 my $d = days_since_2000_Jan_0( $year, $month, $day ) + 0.5 - $lon / 360.0;
139 769 my ($h1, $h2) = sun_rise_set($d, $lon, $lat, $altit, 15.0, $arg{upper_limb}, $arg{polar}, $trace, $revsub);
140 769 ▶ if ($h1 eq 'day' or $h1 eq 'night' or $h2 eq 'day' or $h2 eq 'night') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h1 eq "day" or $h1 eq "night" or $h2 eq "day" or $h2 eq "night") 150 619 769
Condition77%7/9
$h1 eq "day" or $h1 eq "night" or $h2 eq "day" or $h2 eq "night"
l!l&&r!l&&!r
1500619
$h1 eq "day" or $h1 eq "night" or $h2 eq "day"
l!l&&r!l&&!r
1500619
$h1 eq "day" or $h1 eq "night"
l!l&&r!l&&!r
10446619
MC/DC50%2/4
$h1 eq "day" or $h1 eq "night" or $h2 eq "day" or $h2 eq "night"
$h1 eq "day" $h1 eq "night" $h2 eq "day" $h2 eq "night"
Truth table60%3/5
A or B or C or D
A= $h1 eq "day"B= $h1 eq "night"C= $h2 eq "day"D= $h2 eq "night"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
141 150 return ($h1, $h2);
142 }
143 619 return convert_hour($h1, $h2, $TZ, $isdst);
144 }
145 else {
146 182 ▶ my $revsub = sub { _rev_lon($_[0], $lon) }; # normalizing angles around the local longitude
Subroutine __ANON__: called CC=1 SCAR=0.0
147 182 my $ang_speed = 15.0;
148  
149 # This is the initial start
150 182 my $d = days_since_2000_Jan_0($year, $month, $day) - $lon / 360.0;
151  
152 182 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 182 182
153 printf $trace "\nPrecise sunrise computation for %04d-%02d-%02d, lon %.3f, lat %.3f, altitude %.3f, upper limb %d angular speed %.5f\n"
154 , $year, $month, $day
155 , $lon,
156 , $lat,
157 , $altit
158 , $arg{upper_limb}
159 0 , $ang_speed;
160 }
161 182 my $h1_lmt = 12; # LMT decimal hours, noon then the successive values of sunrise
162 182 my $h1_utc; # UTC decimal hours, noon LMT then the successive values of sunrise
163 182 for my $counter (1..9) {
164 # 9 is a failsafe precaution against a possibly runaway loop
165 # but hopefully, we will leave the loop long before, with "last"
166 269 my $h2_utc;
167 269 ($h2_utc, undef) = sun_rise_set($d + $h1_lmt / 24, $lon, $lat, $altit, $ang_speed, $arg{upper_limb}, $arg{polar}, $trace, $revsub);
168 269 ▶ if ($h2_utc eq 'day' or $h2_utc eq 'night') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h2_utc eq "day" or $h2_utc eq "night") 150 119 269
Condition100%3/3
$h2_utc eq "day" or $h2_utc eq "night"
l!l&&r!l&&!r
10446119
MC/DC100%2/2
$h2_utc eq "day" or $h2_utc eq "night"
$h2_utc eq "day" $h2_utc eq "night"
Truth table100%3/3
A or B
A= $h2_utc eq "day"B= $h2_utc eq "night"
ABresult
1X1
011
000
169 150 $h1_utc = $h2_utc;
170 150 last;
171 }
172 119 $h1_utc = $h1_lmt - $lon / 15;
173 119 ▶ if (equal($h1_utc, $h2_utc, 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (equal($h1_utc, $h2_utc, 5)) 32 87 119
174 # equal within 1e-5 hour, a little less than a second
175 32 $h1_utc = $h2_utc;
176 32 last;
177 }
178 87 $h1_utc = $h2_utc;
179 87 $h1_lmt = $h1_utc + $lon / 15;
180 }
181  
182 182 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 182 182
183 printf $trace "\nPrecise sunset computation for %04d-%02d-%02d, lon %.3f, lat %.3f, altitude %.3f, upper limb %d angular speed %.5f\n"
184 , $year, $month, $day
185 , $lon,
186 , $lat,
187 , $altit
188 , $arg{upper_limb}
189 0 , $ang_speed;
190 }
191 182 my $h3_lmt = 12; # LMT decimal hours, noon then the successive values of sunset
192 182 my $h3_utc; # UTC decimal hours, noon LMT then the successive values of sunset
193 182 for my $counter (1..9) {
194 # 9 is a failsafe precaution against a possibly runaway loop
195 # but hopefully, we will leave the loop long before, with "last"
196 247 my $h4_utc;
197 247 (undef, $h4_utc) = sun_rise_set($d + $h3_lmt / 24, $lon, $lat, $altit, $ang_speed, $arg{upper_limb}, $arg{polar}, $trace, $revsub);
198 247 ▶ if ($h4_utc eq 'day' or $h4_utc eq 'night') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h4_utc eq "day" or $h4_utc eq "night") 150 97 247
Condition100%3/3
$h4_utc eq "day" or $h4_utc eq "night"
l!l&&r!l&&!r
1044697
MC/DC100%2/2
$h4_utc eq "day" or $h4_utc eq "night"
$h4_utc eq "day" $h4_utc eq "night"
Truth table100%3/3
A or B
A= $h4_utc eq "day"B= $h4_utc eq "night"
ABresult
1X1
011
000
199 150 $h3_utc = $h4_utc;
200 150 last;
201 }
202 97 $h3_utc = $h3_lmt - $lon / 15;
203 97 ▶ if (equal($h3_utc, $h4_utc, 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (equal($h3_utc, $h4_utc, 5)) 32 65 97
204 # equal within 1e-5 hour, a little less than a second
205 32 $h3_utc = $h4_utc;
206 32 last;
207 }
208 65 $h3_utc = $h4_utc;
209 65 $h3_lmt = $h3_utc + $lon / 15;
210 }
211  
212 182 return convert_hour($h1_utc, $h3_utc, $TZ, $isdst);
213  
214 }
215 }
216 #######################################################################################
217 # end sunrise
218 ###################################################################################
219  
220 #
221 #
222 # FUNCTIONAL SEQUENCE for days_since_2000_Jan_0
223 #
224 # _GIVEN
225 # year, month, day
226 #
227 # _THEN
228 #
229 # process the year month and day (counted in days)
230 # Day 0.0 is at Jan 1 2000 0.0 UT
231 # Note that ALL divisions here should be INTEGER divisions
232 #
233 # _RETURN
234 #
235 # day number
236 #
237 951 ▶sub days_since_2000_Jan_0 {
Subroutine days_since_2000_Jan_0: called CC=1 SCAR=0.0
Pod undocumented
238 11 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 951 my ( $year, $month, $day ) = @_;
240  
241 951 my $d = 367 * $year
242 - int( ( 7 * ( $year + ( ($month + 9) / 12 ) ) ) / 4 )
243 + int( (275 * $month) / 9 )
244 + $day - 730530;
245  
246 951 return $d;
247  
248 }
249  
250 #
251 #
252 # FUNCTIONAL SEQUENCE for convert_hour
253 #
254 # _GIVEN
255 # Hour_rise, Hour_set, Time zone offset, DST setting
256 # hours are in UT
257 #
258 # _THEN
259 #
260 # convert to local time
261 #
262 #
263 # _RETURN
264 #
265 # hour:min rise and set
266 #
267  
268 801 ▶sub convert_hour {
Subroutine convert_hour: called CC=1 SCAR=0.0
Pod undocumented
269 801 my ($hour_rise_ut, $hour_set_ut, $TZ, $isdst) = @_;
270 801 return (convert_1_hour($hour_rise_ut, $TZ, $isdst),
271 convert_1_hour($hour_set_ut, $TZ, $isdst));
272 }
273 #
274 #
275 # FUNCTIONAL SEQUENCE for convert_1_hour
276 #
277 # _GIVEN
278 # Hour, Time zone offset, DST setting
279 # hours are in UT
280 #
281 # _THEN
282 #
283 # convert to local time
284 #
285 #
286 # _RETURN
287 #
288 # hour:min
289 #
290  
291 1602 ▶sub convert_1_hour {
Subroutine convert_1_hour: called CC=8 SCAR=20.9
Pod undocumented
292 1602 my ($hour_ut, $TZ, $isdst) = @_;
293  
294 1602 ▶ if ($hour_ut eq 'day' or $hour_ut eq 'night') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour_ut eq "day" or $hour_ut eq "night") 300 1302 1602
Condition100%3/3
$hour_ut eq "day" or $hour_ut eq "night"
l!l&&r!l&&!r
208921302
MC/DC100%2/2
$hour_ut eq "day" or $hour_ut eq "night"
$hour_ut eq "day" $hour_ut eq "night"
Truth table100%3/3
A or B
A= $hour_ut eq "day"B= $hour_ut eq "night"
ABresult
1X1
011
000
295 300 return $hour_ut;
296 }
297  
298 1302 my $hour_local = $hour_ut + $TZ;
299 1302 ▶ if ($isdst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isdst) 0 1302 1302
300 0 $hour_local ++;
301 }
302  
303 # The hour should be between 0 and 24;
304 1302 ▶ if ($hour_local < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hour_local < 0) { } 20 1282 1302
elsif ($hour_local > 24) { } 94 1188 1282
305 20 $hour_local += 24;
306 }
307 elsif ($hour_local > 24) {
308 94 $hour_local -= 24;
309 }
310  
311 1302 my $hour = int ($hour_local);
312  
313 1302 my $min = floor(($hour_local - $hour) * 60 + 0.5);
314  
315 1302 ▶ if ($min >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min >= 60) 7 1295 1302
316 7 $min -= 60;
317 7 $hour++;
318 7 ▶ $hour -= 24 if $hour >= 24;
Branch: 1 branch
BranchTrueFalseTotal
if $hour >= 24 0 7 7
319 }
320  
321 1302 return sprintf("%02d:%02d", $hour, $min);
322 }
323  
324  
325 1285 ▶sub sun_rise_set {
Subroutine sun_rise_set: called CC=11 SCAR=26.0
Pod undocumented
326 1285 my ($d, $lon, $lat, $altit, $ang_spd, $upper_limb, $polar, $trace, $revsub) = @_;
327  
328 1285 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 1285 1285
329 0 printf $trace "\n";
330 }
331  
332 # Compute local sidereal time of this moment
333 1285 my $gmst0 = GMST0($d);
334 1285 my $sidtime = revolution( $gmst0 + 180.0 );
335  
336 # Compute Sun's RA + Decl + distance at this moment
337 1285 my ( $sRA, $sdec, $sr ) = sun_RA_dec($d, $lon, $trace);
338  
339 # Compute time when Sun is at south - in hours (LMT then UTC)
340 1285 my $tsouth_lmt = 12.0 - $revsub->( $sidtime - $sRA ) / 15;
341 1285 my $tsouth = $tsouth_lmt - $lon / 15;
342  
343 1285 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 1285 1285
344 0 printf $trace "For day $d (%s), GMST0 $gmst0 %s %s\n", _fmt_hr(24 * ($d - int($d)), $lon, 0), _fmt_angle($gmst0 ), _fmt_dur($gmst0 / 15);
345 0 printf $trace "For day $d (%s), sidereal time $sidtime, %s %s\n", _fmt_hr(24 * ($d - int($d)), $lon, 0), _fmt_angle($sidtime), _fmt_dur($sidtime / 15);
346 0 printf $trace "For day $d (%s), right asc $sRA %s %s\n", _fmt_hr(24 * ($d - int($d)), $lon, 0), _fmt_angle($sRA ), _fmt_dur($sRA / 15);
347 0 printf $trace "For day $d (%s), declination $sdec %s %s\n", _fmt_hr(24 * ($d - int($d)), $lon, 0), _fmt_angle($sdec ), _fmt_dur($sdec / 15);
348 0 printf $trace "For day $d (%s), solar noon at $tsouth (%s)\n", _fmt_hr(24 * ($d - int($d)), $lon, 0), _fmt_hr($tsouth, $lon, 0);
349 }
350  
351 1285 ▶ if ($upper_limb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upper_limb) 575 710 1285
352 # Compute the Sun's apparent radius, degrees
353 575 my $sradius = 0.2666 / $sr;
354 575 $altit -= $sradius;
355 }
356  
357 # Compute the diurnal arc that the Sun traverses to reach
358 # the specified altitude altit:
359 1285 my $cost = ( sind($altit) - sind($lat) * sind($sdec) )
360 / ( cosd($lat) * cosd($sdec) );
361  
362 1285 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 1285 1285
363 0 print $trace "altit = $altit, sind(altit) = ", sind($altit), ", lat = $lat, sind(lat) = ", sind($lat), "\n";
364 0 print $trace "sdec = $sdec, sind(sdec) = ", sind($sdec), ", lat = $lat, cosd(lat) = ", cosd($lat), "\n";
365 0 print $trace "sdec = $sdec, cosd(sdec) = ", cosd($sdec), ", cost = $cost\n";
366 }
367  
368 1285 my $t;
369 1285 ▶ if ( $cost >= 1.0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cost >= 1.0) { } 188 1097 1285
elsif ($cost <= -1.0) { } 424 673 1097
370 188 ▶ if ($polar eq 'retval') {
Branch: 1 branch
BranchTrueFalseTotal
if ($polar eq "retval") 138 50 188
371 138 return ('night', 'night');
372 }
373 50 carp "Sun never rises!!\n";
374 50 $t = 0.0; # Sun always below altit
375 }
376 elsif ( $cost <= -1.0 ) {
377 424 ▶ if ($polar eq 'retval') {
Branch: 1 branch
BranchTrueFalseTotal
if ($polar eq "retval") 312 112 424
378 312 return ('day', 'day');
379 }
380 112 carp "Sun never sets!!\n";
381 112 $t = 12.0; # Sun always above altit
382 }
383 else {
384 673 my $arc = acosd($cost); # The diurnal arc
385 673 $t = $arc / $ang_spd; # Time to traverse the diurnal arc, hours
386 673 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 673 673
387 0 printf $trace "Diurnal arc $arc -> $t hours (%s)\n", _fmt_dur($t);
388 }
389 }
390  
391 # Store rise and set times - in hours UT
392  
393 835 my $hour_rise_ut = $tsouth - $t;
394 835 my $hour_set_ut = $tsouth + $t;
395 835 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 835 835
396 0 printf $trace "For day $d (%s), sunrise at $hour_rise_ut (%s)\n", _fmt_hr(24 * ($d - int($d)), $lon),
397 _fmt_hr($hour_rise_ut, $lon);
398 0 printf $trace "For day $d (%s), sunset at $hour_set_ut (%s)\n", _fmt_hr(24 * ($d - int($d)), $lon),
399 _fmt_hr($hour_set_ut , $lon);
400 }
401 835 return($hour_rise_ut, $hour_set_ut);
402 }
403  
404 #########################################################################################################
405 #
406 #
407 # FUNCTIONAL SEQUENCE for GMST0
408 #
409 # _GIVEN
410 # Day number
411 #
412 # _THEN
413 #
414 # computes GMST0, the Greenwich Mean Sidereal Time
415 # at 0h UT (i.e. the sidereal time at the Greenwich meridian at
416 # 0h UT). GMST is then the sidereal time at Greenwich at any
417 # time of the day..
418 #
419 #
420 # _RETURN
421 #
422 # Sidtime
423 #
424 1285 ▶sub GMST0 {
Subroutine GMST0: called CC=1 SCAR=0.0
Pod undocumented
425 1285 my ($d) = @_;
426  
427 1285 my $sidtim0 = revolution( ( 180.0 + 356.0470 + 282.9404 )
428 + ( 0.9856002585 + 4.70935E-5 ) * $d );
429 1285 return $sidtim0;
430  
431 }
432  
433 #
434 #
435 # FUNCTIONAL SEQUENCE for sun_RA_dec
436 #
437 # _GIVEN
438 # day number, $r and $lon (from sunpos)
439 #
440 # _THEN
441 #
442 # compute RA and dec
443 #
444 #
445 # _RETURN
446 #
447 # Sun's Right Ascension (RA), Declination (dec) and distance (r)
448 #
449 #
450 1285 ▶sub sun_RA_dec {
Subroutine sun_RA_dec: called CC=2 SCAR=7.0
Pod undocumented
451 1285 my ($d, $lon_noon, $trace) = @_;
452  
453 # Compute Sun's ecliptical coordinates
454 1285 my ( $r, $lon ) = sunpos($d);
455 1285 ▶ if ($trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace) 0 1285 1285
456 0 printf $trace "For day $d (%s), solar noon at ecliptic longitude $lon %s\n", _fmt_hr(24 * ($d - int($d)), $lon_noon), _fmt_angle($lon);
457 }
458  
459 # Compute ecliptic rectangular coordinates (z=0)
460 1285 my $x = $r * cosd($lon);
461 1285 my $y = $r * sind($lon);
462  
463 # Compute obliquity of ecliptic (inclination of Earth's axis)
464 1285 my $obl_ecl = 23.4393 - 3.563E-7 * $d;
465  
466 # Convert to equatorial rectangular coordinates - x is unchanged
467 1285 my $z = $y * sind($obl_ecl);
468 1285 $y = $y * cosd($obl_ecl);
469  
470 # Convert to spherical coordinates
471 1285 my $RA = atan2d( $y, $x );
472 1285 my $dec = atan2d( $z, sqrt( $x * $x + $y * $y ) );
473  
474 1285 return ( $RA, $dec, $r );
475  
476 } # sun_RA_dec
477  
478  
479 #
480 #
481 # FUNCTIONAL SEQUENCE for sunpos
482 #
483 # _GIVEN
484 # day number
485 #
486 # _THEN
487 #
488 # Computes the Sun's ecliptic longitude and distance
489 # at an instant given in d, number of days since
490 # 2000 Jan 0.0.
491 #
492 #
493 # _RETURN
494 #
495 # ecliptic longitude and distance
496 # ie. $True_solar_longitude, $Solar_distance
497 #
498 1285 ▶sub sunpos {
Subroutine sunpos: called CC=2 SCAR=6.9
Pod undocumented
499 1285 my ($d) = @_;
500  
501 # Mean anomaly of the Sun
502 # Mean longitude of perihelion
503 # Note: Sun's mean longitude = M + w
504 # Eccentricity of Earth's orbit
505 # Eccentric anomaly
506 # x, y coordinates in orbit
507 # True anomaly
508  
509 # Compute mean elements
510 1285 my $Mean_anomaly_of_sun = revolution( 356.0470 + 0.9856002585 * $d );
511 1285 my $Mean_longitude_of_perihelion = 282.9404 + 4.70935E-5 * $d;
512 1285 my $Eccentricity_of_Earth_orbit = 0.016709 - 1.151E-9 * $d;
513  
514 # Compute true longitude and radius vector
515 1285 my $Eccentric_anomaly = $Mean_anomaly_of_sun
516 + $Eccentricity_of_Earth_orbit * $RADEG
517 * sind($Mean_anomaly_of_sun)
518 * ( 1.0 + $Eccentricity_of_Earth_orbit * cosd($Mean_anomaly_of_sun) );
519  
520 1285 my $x = cosd($Eccentric_anomaly) - $Eccentricity_of_Earth_orbit;
521  
522 1285 my $y = sqrt( 1.0 - $Eccentricity_of_Earth_orbit * $Eccentricity_of_Earth_orbit )
523 * sind($Eccentric_anomaly);
524  
525 1285 my $Solar_distance = sqrt( $x * $x + $y * $y ); # Solar distance
526 1285 my $True_anomaly = atan2d( $y, $x ); # True anomaly
527  
528 1285 my $True_solar_longitude = $True_anomaly + $Mean_longitude_of_perihelion; # True solar longitude
529  
530 1285 ▶ if ( $True_solar_longitude >= 360.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($True_solar_longitude >= 360) 506 779 1285
531 506 $True_solar_longitude -= 360.0; # Make it 0..360 degrees
532 }
533  
534 1285 return ( $Solar_distance, $True_solar_longitude );
535 }
536  
537  
538  
539 9001 ▶sub sind {
Subroutine sind: called CC=1 SCAR=0.0
Pod documented
540 9001 sin( ( $_[0] ) * $DEGRAD );
541 }
542  
543 7716 ▶sub cosd {
Subroutine cosd: called CC=1 SCAR=0.0
Pod documented
544 7716 cos( ( $_[0] ) * $DEGRAD );
545 }
546  
547 6 ▶sub tand {
Subroutine tand: called CC=1 SCAR=0.0
Pod documented
548 6 tan( ( $_[0] ) * $DEGRAD );
549 }
550  
551 6 ▶sub atand {
Subroutine atand: called CC=1 SCAR=0.0
Pod documented
552 6 ( $RADEG * atan( $_[0] ) );
553 }
554  
555 6 ▶sub asind {
Subroutine asind: called CC=1 SCAR=0.0
Pod documented
556 6 ( $RADEG * asin( $_[0] ) );
557 }
558  
559 679 ▶sub acosd {
Subroutine acosd: called CC=1 SCAR=0.0
Pod documented
560 679 ( $RADEG * acos( $_[0] ) );
561 }
562  
563 3862 ▶sub atan2d {
Subroutine atan2d: called CC=1 SCAR=0.0
Pod documented
564 3862 ( $RADEG * atan2( $_[0], $_[1] ) );
565 }
566  
567 #
568 #
569 # FUNCTIONAL SEQUENCE for revolution
570 #
571 # _GIVEN
572 # any angle
573 #
574 # _THEN
575 #
576 # reduces any angle to within the first revolution
577 # by subtracting or adding even multiples of 360.0
578 #
579 #
580 # _RETURN
581 #
582 # the value of the input is >= 0.0 and < 360.0
583 #
584  
585 3855 ▶sub revolution {
Subroutine revolution: called CC=1 SCAR=0.0
Pod undocumented
586 3855 my $x = $_[0];
587 3855 return ( $x - 360.0 * floor( $x * $INV360 ) );
588 }
589 #
590 #
591 # FUNCTIONAL SEQUENCE for _rev_lon
592 #
593 # _GIVEN
594 #
595 # two angles in degrees, the variable angle and the reference angle (longitude)
596 #
597 # _THEN
598 #
599 # Reduce input variable angle to within reference-180 .. reference+180 degrees
600 #
601 #
602 # _RETURN
603 #
604 # angle that was reduced
605 #
606 516 ▶sub _rev_lon {
Subroutine _rev_lon: called CC=1 SCAR=0.0
607 516 my ($x, $lon) = @_;
608 516 return $lon + rev180($x - $lon);
609 }
610  
611 #
612 #
613 # FUNCTIONAL SEQUENCE for rev180
614 #
615 # _GIVEN
616 #
617 # any angle
618 #
619 # _THEN
620 #
621 # Reduce input to within -180..+180 degrees
622 #
623 #
624 # _RETURN
625 #
626 # angle that was reduced
627 #
628 1285 ▶sub rev180 {
Subroutine rev180: called CC=1 SCAR=0.0
Pod undocumented
629 1285 my ($x) = @_;
630  
631 1285 return ( $x - 360.0 * floor( $x * $INV360 + 0.5 ) );
632 }
633  
634 259 ▶sub equal {
Subroutine equal: called CC=1 SCAR=0.0
Pod documented
635 259 my ($A, $B, $dp) = @_;
636  
637 259 return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
638 }
639  
640 0 ▶sub _fmt_hr {
Subroutine _fmt_hr: not called CC=2 SCAR=17.9
641 0 my ($hr, $lon, $is_lmt) = @_;
642 0 my ($lmt, $utc);
643 0 ▶ if ($is_lmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_lmt) { } 0 0 0
644 0 $lmt = $hr;
645 0 $utc = $lmt - $lon / 15;
646 }
647 else {
648 0 $utc = $hr;
649 0 $lmt = $utc + $lon / 15;
650 }
651 0 my $hr_h_utc = $utc; my $hr_h_lmt = $lmt;
652 0 my $hr_d_utc = $utc / 24; my $hr_d_lmt = $lmt / 24;
653 0 my $hr_utc = floor($utc); my $hr_lmt = floor($lmt);
654 0 $utc -= $hr_utc; $lmt -= $hr_lmt;
655 0 $utc *= 60; $lmt *= 60;
656 0 my $mn_utc = floor($utc); my $mn_lmt = floor($lmt);
657 0 $utc -= $mn_utc; $lmt -= $mn_lmt;
658 0 $utc *= 60; $lmt *= 60;
659 0 my $sc_utc = floor($utc); my $sc_lmt = floor($lmt);
660 0 return sprintf("UTC: %02d:%02d:%02d %f h %f d, LMT: %02d:%02d:%02d %f h %f d", $hr_utc, $mn_utc, $sc_utc, $hr_h_utc, $hr_d_utc
661 , $hr_lmt, $mn_lmt, $sc_lmt, $hr_h_lmt, $hr_d_lmt);
662 }
663  
664 0 ▶sub _fmt_dur {
Subroutine _fmt_dur: not called CC=2 SCAR=17.9
665 0 my ($dur) = @_;
666 0 my $sign = '';
667 0 ▶ if ($dur < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dur < 0) 0 0 0
668 0 $sign = '-';
669 0 $dur *= -1;
670 }
671 0 my $hr = floor($dur);
672 0 $dur -= $hr;
673 0 $dur *= 60;
674 0 my $mn = floor($dur);
675 0 $dur -= $mn;
676 0 $dur *= 60;
677 0 my $sc = floor($dur);
678 0 return sprintf("%s%02d h %02d mn %02d s", $sign, $hr, $mn, $sc);
679 }
680  
681 0 ▶sub _fmt_angle {
Subroutine _fmt_angle: not called CC=2 SCAR=17.9
682 0 my ($angle) = @_;
683 0 my $sign = '';
684 0 ▶ if ($angle < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($angle < 0) 0 0 0
685 0 $sign = '-';
686 0 $angle *= -1;
687 }
688 0 my $hr = floor($angle);
689 0 $angle -= $hr;
690 0 $angle *= 60;
691 0 my $mn = floor($angle);
692 0 $angle -= $mn;
693 0 $angle *= 60;
694 0 my $sc = floor($angle);
695 0 return sprintf(q<%s%02d°%02d'%02d">, $sign, $hr, $mn, $sc);
696 }
697  
698 sub DEFAULT () { -0.833 }
699 sub CIVIL () { - 6 }
700 sub NAUTICAL () { -12 }
701 sub AMATEUR () { -15 }
702 sub ASTRONOMICAL () { -18 }
703  
704 # Ending a module with whatever, which risks to be zero, is wrong.
705 # Ending a module with 1 is boring. So, let us end it with:
706 11 1950;
707 # Hint: directed by BW, with GS, WH and EVS
708  
709 __END__
↑ Summary