Jewish/Haversine.pm

filter: Statementstmt 20.6% 18 / 87 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 19.8% 27 / 136 CC 17 SCAR 35.2
CC
17
Cov
15%
recalculate_coordinate
72.0
azimuth
30.0
points2distance
20.0
CRAP 33.9
↑ Summary Jewish/Declination.pm »
1 package DateTime::Event::Jewish::Haversine;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 @EXPORT_OK = qw(recalculate_coordinate azimuth elevation point2distance);
7 1 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = '0.01';
9  
10  
11 # Python implementation of Haversine formula
12 # Copyright (C) <2009> Bartek Garny <bartek at gorny.edu.pl>
13 # Converted to Perl by Raphael Mankin <rapmankin at cpan.org> Feb 2010
14 #
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program. If not, see <http://www.gnu.org/licenses/>
27 #
28 # Apparent angular diameters
29 # Sun: 31.6' - 32.7'
30 # Moon: 29.3' - 34.1'
31  
32 =head1 NAME
33  
34 Haversine.pm - Calculations using haversine formula
35  
36 =head1 SYNOPSIS
37  
38 use Haversine;
39 my $degrees = recalculate_coordinate([51, 12, 0], 'deg');
40 my $radians = recalculate_coordinate([51, 12, 0], 'rad');
41 my $km = points2distance($start, $end);
42 my $degrees = azimuth($start, $end, 'deg');
43  
44 =cut
45  
46  
47 =head3 recalculate_coordinate($location, $as)
48  
49 Convert a tupe of (degrees, minutes, seconds) to a normal form.
50  
51 =over
52  
53 =item $location
54  
55 An arrayref of three components: degrees, minutes, seconds.
56  
57 =item $as
58  
59 Return value type. Can be specified as 'deg', 'min', 'sec' or 'rad'; default
60 return value is a proper coordinate tuple.
61  
62 =back
63  
64 =cut
65  
66 0 ▶sub recalculate_coordinate {
Subroutine recalculate_coordinate: not called CC=8 SCAR=42.8
Pod documented
67 0 my $val = shift;
68 0 my $_as = shift;
69
70 # Accepts a coordinate as a tuple (degree, minutes, seconds)
71 # You can give only one of them (e.g. only minutes as a floating point number) and it will be duly
72 # recalculated into degrees, minutes and seconds.
73  
74  
75 0 my ($deg, $min, $sec) = @$val;
76 # pass outstanding values from right to left
77 0 ▶ $min = ($min or 0) + int($sec) / 60;
Condition0%0/2
$min || 0
l!l
00
MC/DC0%0/1
$min || 0
$min
Truth table0%0/2
A
A= $min
Aresult
11
00
78 0 $sec = $sec % 60;
79 0 ▶ $deg = ($deg or 0) + int($min) / 60;
Condition0%0/2
$deg || 0
l!l
00
MC/DC0%0/1
$deg || 0
$deg
Truth table0%0/2
A
A= $deg
Aresult
11
00
80 0 $min = $min % 60;
81 # pass decimal part from left to right
82 0 my $dint = int($deg);
83 0 my $dfrac = $deg - $dint;
84 0 $min = $min + $dfrac * 60;
85 0 $deg = $dint;
86 0 my $mint = int($min);
87 0 my $mfrac = $min - $mint;
88 0 $sec = $sec + $mfrac * 60;
89 0 $min = $mint;
90 0 ▶ if ($_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_as) 0 0 0
91 0 $sec = $sec + $min * 60 + $deg * 3600;
92 0 ▶ if ($_as eq 'sec') { return $sec;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "sec") 0 0 0
93 0 ▶ if ($_as eq 'min') { return $sec / 60;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "min") 0 0 0
94 0 ▶ if ($_as eq 'deg') { return $sec / 3600;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "deg") 0 0 0
95 0 ▶ if ($_as eq 'rad') { return deg2rad($sec / 3600);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "rad") 0 0 0
96 }
97 0 return [$deg, $min, $sec];
98 }
99
100  
101 =head3 points2distance($start, $end)
102  
103 Calculate distance (in kilometers) between two points
104 given as (lat, long) pairs based on Haversine formula
105 (http://en.wikipedia.org/wiki/Haversine_formula).
106 Implementation inspired by JavaScript implementation from http://www.movable-type.co.uk/scripts/latlong.html
107  
108 Accepts coordinates as tuples (deg, min, sec), but coordinates can be given in any form - e.g.
109 can specify only minutes:
110 (0, 3133.9333, 0)
111 is interpreted as
112 (52.0, 13.0, 55.998000000008687)
113 which, not accidentally, is the latitude of Warsaw, Poland.
114  
115 =cut
116  
117 0 ▶sub points2distance {
Subroutine points2distance: not called CC=4 SCAR=30.0
Pod documented
118 0 my ($start, $end) = @_;
119 0 shift; shift;
120 0 my $_as =shift;
121
122
123 0 my $start_lat = recalculate_coordinate($start->[0], 'rad');
124 0 my $start_long= recalculate_coordinate($start->[1], 'rad');
125 0 my $end_long = recalculate_coordinate($end->[1], 'rad');
126 0 my $end_lat = recalculate_coordinate($end->[0], 'rad');
127 0 my $d_lat = $end_lat - $start_lat;
128 0 my $d_long = $end_long - $start_long;
129 0 my $a = sin($d_lat/2)**2 + cos($start_lat) * cos($end_lat) * sin($d_long/2)**2;
130 0 my $c = 2 * atan2(sqrt($a), sqrt(1-$a));
131 0 ▶ if ($_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_as) 0 0 0
132 0 ▶ if ($_as eq 'rad'){ return $c;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "rad") 0 0 0
133 0 ▶ if ($_as eq 'deg'){ return rad2deg($c);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "deg") 0 0 0
134 }
135 0 return 6371 * $c;
136 }
137  
138 =head3 azimuth($start, $end)
139  
140 Calculate azimuth (bearing) of one point from another
141 given as (lat, long) pairs based on Haversine formula
142  
143 =cut
144  
145 0 ▶sub azimuth {
Subroutine azimuth: not called CC=5 SCAR=34.0
Pod documented
146 0 my ($start, $end) = @_;
147 0 shift; shift;
148 0 ▶ my $_as= shift||'deg';
Condition0%0/2
shift() || 'deg'
l!l
00
MC/DC0%0/1
shift() || 'deg'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
149
150 # Calculate the azimuth (initial bearing) to travel by a
151 # great circle path from 'start' to 'end'
152
153 0 my $phi1 = recalculate_coordinate($start->[0], 'rad');
154 0 my $phi2 = recalculate_coordinate($end->[0], 'rad');
155 0 my $lambda1 = recalculate_coordinate($start->[1], 'rad');
156 0 my $lambda2 = recalculate_coordinate($end->[1], 'rad');
157 0 my $y = sin($lambda2-$lambda1)*cos($phi2);
158 0 my $x = cos($phi1)*sin($phi2) - sin($phi1)*cos($phi2)*cos($lambda2-$lambda1);
159 0 my $az = atan2($y,$x);
160 0 ▶ if ($az < 0) { $az += 2*pi; }
Branch: 1 branch
BranchTrueFalseTotal
if ($az < 0) 0 0 0
161 0 ▶ if ($_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_as) 0 0 0
162 0 ▶ if($_as eq 'deg'){ return rad2deg($az);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "deg") 0 0 0
163 }
164 0 return $az;
165 }
166  
167 =head3 elevation($ground, $star)
168  
169 Calculate the elevation of a star as seen from a point on the ground.
170 'star' is the declination and hour angle (lat, long) of the star,
171 'ground' is the (lat,long) of the ground point.
172  
173 The elevation is just the complement of the angular distance between
174 the two ground points. We assume that the star is at infinity. Not quite
175 true for the sun or moon.
176  
177 =cut
178  
179 0 ▶sub elevation {
Subroutine elevation: not called CC=3 SCAR=24.8
Pod documented
180 0 my ($ground, $star) = @_;
181 0 shift; shift;
182 0 ▶ my $_as= shift||'deg';
Condition0%0/2
shift() || 'deg'
l!l
00
MC/DC0%0/1
shift() || 'deg'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
183
184
185 0 my $el = 90-points2distance($ground, $star, 'deg');
186 0 ▶ if ($_as eq 'rad') { return deg2rad($el);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_as eq "rad") 0 0 0
187 0 return $el;
188 }
189  
190 1 1;
191  
192 __END__
↑ Summary Jewish/Declination.pm »