blib/lib/Ham/Resources/Utils.pm

filter: Statementstmt 89.3% 243 / 272 Branchbran 52.0% 52 / 100 Conditioncond 43.5% 17 / 39 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 100% 19 / 19 Podpod 100% 14 / 14 totaltotal 74.1% 347 / 468 CC 66 SCAR 30.7
CC
66
Cov
76%
is_date
68.6
sexag2dec
23.3
cicle_sun
16.0
CRAP 21.5
↑ Summary
1 package Ham::Resources::Utils;
2  
3 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Math::Trig qw(great_circle_distance deg2rad great_circle_direction rad2deg pi asin acos tan);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Ham::Locator;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Ham::Resources::Utils - Calculation of distance and course beetwen two points
12 on Earth (through coordinates or grid locator), and Sunrise, Sunset and Midday time for these locations (in UTC). Also sexagesimal degrees and decimal degrees convertion and grid location. For use mainly for Radio Amateurs.
13  
14 =head1 VERSION
15  
16 Version 0.05
17  
18 =cut
19  
20 10 our $VERSION = '0.05';
21  
22 10 my %coordinates = (
23 long_1 => "",
24 lat_1 => "",
25 long_2 => "",
26 lat_2 => "",
27 );
28  
29 10 my $locator_precision = 8;
30  
31 10 my $self = {};
32  
33 =head1 SYNOPSIS
34  
35 This module calculates the distance and course between two points on the Earth.
36 Also Sunrise, Sunset and Midday time for both locations.
37  
38 The data of the locations may be in terrestrial coordinates or through 'Maidenhead Locator System' (grid Locator) notacion.
39  
40 The module offer the possibility to access to some methods that uses it, for
41 example conversions between sexagesimal degrees into decimal degrees or
42 conversion between grid locator to sexagesimal degrees.
43  
44 Also access to convert decimal degrees to compass names, and more.
45  
46 use Ham::Resources::Utils;
47  
48 my $foo = Ham::Resources::Utils->new();
49 ...
50  
51  
52 =head1 SUBROUTINES/METHODS
53  
54 =head2 new
55  
56 This is the constructor.
57  
58 my $foo = Ham::Resources::Utils->new();
59  
60 =cut
61  
62 8 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 =head2 data_by_coordinates
66  
67 Gets a string with the date and a hash with the coordinates in sexagesimal
68 values from point A and point B and returns a hash with all previous
69 data and Distance, course and compass values and Sunrise, Sunset and Midday
70 times for both locations if needed.
71  
72 my $date = "14-03-2012";
73 my %coordinates = ( lat_1 => "41N23",
74 long_1 => "2E11",
75 lat_2 => "30S0",
76 long_2 => "10W45");
77  
78 my %data = $foo->data_by_coordinates{$date, %coordinates};
79 print Dumper(%data);
80  
81 The $date argument is necessary for the Sun time calculations (Sunrise, Sunset and Midday).
82  
83 Distances are in kilometers (km) and Miles (mi). Times are in UTC (Universal Time).
84  
85 An output example:
86  
87 DATA_BY_COORDINATES()
88 compass: S # compass direction to point B or destination
89 course_dec: 190.94 # direction to destination in decimal degree
90 course_sexag: 190.56 # direction to destination in sexagesimal degree
91 date: 14-3-2012 # date of event (for Sun calculation porpouses)
92 distance_km: 9377.83 # distance to destination in kilometers
93 distance_mi: 17367.74 # distance to destination in miles
94 lat_1: 41N23" # Latitude of point A or origin in sexagesinal notation
95 lat_1_dec: 41.3833333333333 # Latitude of origin in decimal notation
96 lat_2: 41S54" # Latitude of point B or destination in sexagesimal notation
97 lat_2_dec: -41.9 # Latiude of destination in decimal notation
98 locator_1: JN11cj # Grid Locator of origin point
99 locator_2: IE38sc # Grid Locator of destination point
100 long_1: 2E12" # Longitude of point A or origin in sexagesimal notation
101 long_1_dec: 2.2 # Longitude of origin in decimal notation
102 long_2: 12W30" # Longitude of point B or destination in sexagesimal notation
103 long_2_dec: -12.5 # Longitude of destination in decimal notation
104 midday_arrive: 12h 1m # Midday time on point B (destination) in UTC
105 midday_departure: 12h 1m # Midday time on point A (origin) in UTC
106 sunrise_arrive: 6h 5m # Sun rise time on point B (destination) in UTC
107 sunrise_departure: 6h 5m # Sun rise time on point A (origin) in UTC
108 sunset_arrive: 17h 58m # Sun set time on point B (destination) in UTC
109 sunset_departure: 17h 58m # Sun set time on point A (origin) in UTC
110  
111  
112 =cut
113  
114 1 ▶sub data_by_coordinates {
Subroutine data_by_coordinates: called CC=1 SCAR=0.0
Pod documented
115 1 my $self = shift;
116 1 my $date = shift;
117 1 my %coordinates = @_;
118 1 my %data = data_constructor($self, $date, %coordinates);
119 }
120  
121 =head2 data_by_locator
122  
123 Gets a string with the date and a string with the locator of point 'A' and an
124 string with a locator for point 'B'. Returns a hash with the data shown it in the
125 previous method.
126  
127 my $date = "14-03-2012"; # date in 'dd-mm-yyyy' format
128 my $locator_dep = "JN11cj";
129 my $locator_arr = "IJ90ca";
130  
131 my %data = $foo->data_by_locator($date,$locator_dep,$locator_arr);
132 print Dumper(%data);
133  
134 =cut
135  
136 1 ▶sub data_by_locator {
Subroutine data_by_locator: called CC=11 SCAR=26.1
Pod documented
137 1 my $self = shift;
138 1 my $date = shift;
139 1 my ($locator_dep, $locator_arr) = @_;
140 1 my ($lat_dep, $long_dep, $lat_arr, $long_arr) = undef;
141 1 ▶ ($lat_dep, $long_dep) = loc2degree($self,$locator_dep) if ($locator_dep);
Branch: 1 branch
BranchTrueFalseTotal
if $locator_dep 1 0 1
142 1 ▶ ($lat_arr, $long_arr) = loc2degree($self,$locator_arr) if ($locator_arr);
Branch: 1 branch
BranchTrueFalseTotal
if $locator_arr 1 0 1
143  
144 1 ▶ $lat_dep =~ s/\./N/ if ($lat_dep !~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $lat_dep =~ /^\s\d+/ 1 0 1
145 1 ▶ $lat_dep =~ s/\./S/ if ($lat_dep =~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
if $lat_dep =~ /^\s\d+/ 0 1 1
146 1 ▶ $lat_arr =~ s/\./N/ if ($lat_arr !~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $lat_arr =~ /^\s\d+/ 0 1 1
147 1 ▶ $lat_arr =~ s/\./S/ if ($lat_arr =~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
if $lat_arr =~ /^\s\d+/ 1 0 1
148  
149 1 ▶ $long_dep =~ s/\./E/ if ($long_dep !~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $long_dep =~ /^\s\d+/ 1 0 1
150 1 ▶ $long_dep =~ s/\./W/ if ($long_dep =~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
if $long_dep =~ /^\s\d+/ 0 1 1
151 1 ▶ $long_arr =~ s/\./E/ if ($long_arr !~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $long_arr =~ /^\s\d+/ 0 1 1
152 1 ▶ $long_arr =~ s/\./W/ if ($long_arr =~ /^\s\d+/);
Branch: 1 branch
BranchTrueFalseTotal
if $long_arr =~ /^\s\d+/ 1 0 1
153  
154 1 my %coordinates = (
155 long_1 => $long_dep,
156 lat_1 => $lat_dep,
157 long_2 => $long_arr,
158 lat_2 => $lat_arr,
159 );
160  
161 1 my %data = data_constructor($self, $date, %coordinates);
162 }
163  
164 2 ▶sub data_constructor {
Subroutine data_constructor: called CC=2 SCAR=6.9
Pod documented
165 2 my $self = shift;
166 2 my $date = shift;
167 2 my %coord = @_;
168 2 my $error = "";
169  
170 2 %coord = (%coord, sexag2dec($self, %coord));
171 2 %coord = check_error(%coord);
172  
173 2 my @DEPARTURE = NESW( $coord{long_1_dec}, $coord{lat_1_dec} );
174 2 my @ARRIVE = NESW( $coord{long_2_dec}, $coord{lat_2_dec} );
175 2 my $km = great_circle_distance(@DEPARTURE, @ARRIVE, 6371); # medium value for Earth radii
176 2 $km = sprintf("%.2f",$km);
177 2 my $mi = $km / 1.609344; # miles conversion
178 2 $mi = sprintf("%.2f",$mi);
179 2 my $rad = great_circle_direction(@DEPARTURE, @ARRIVE);
180 2 my $sexag = dec2sexag($self, rad2deg($rad));
181 2 my $rad_round = sprintf("%.2f",(rad2deg($rad)));
182 2 my $compass = compass($self, rad2deg($rad));
183  
184 2 my $locator_dep = degree2loc($self, $coord{lat_1}, $coord{long_1});
185 2 my $locator_arr = degree2loc($self, $coord{lat_2}, $coord{long_2});
186  
187  
188 2 my %sun_departure = cicle_sun($self, $coord{lat_1_dec}, $coord{long_1_dec}, $date, "_departure");
189 2 my %sun_arrive = cicle_sun($self, $coord{lat_2_dec}, $coord{long_2_dec}, $date, "_arrive");
190  
191 2 %coord = ( %coord,
192 distance_km => $km,
193 distance_mi => $mi,
194 course_sexag => $sexag,
195 course_dec => $rad_round,
196 compass => $compass,
197 date => $date,
198 locator_1 => $locator_dep,
199 locator_2 => $locator_arr,
200 %sun_departure, %sun_arrive
201 );
202 0 ▶ if ($error) { return my %error = (_error => $error); }
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 2 2
203  
204 2 return %coord;
205 }
206  
207 =head2 loc2degree
208  
209 Gets a string with grid locator value and returns an array with the latitude and
210 longitude in sexagesimal degrees form. Grid precision only 6 digit.
211  
212 =cut
213  
214 2 ▶sub loc2degree {
Subroutine loc2degree: called CC=6 SCAR=17.9
Pod documented
215 2 my ($self,$loc) = @_;
216 2 my $grid = new Ham::Locator;
217 2 $grid->set_precision($locator_precision);
218 2 ▶ $loc = substr($loc, 0, 6) if (length($loc) > 6);
Branch: 1 branch
BranchTrueFalseTotal
if length $loc > 6 0 2 2
219  
220 2 $grid->set_loc($loc);
221 2 my ($latitude, $longitude) = $grid->loc2latlng;
222 2 my $lat_sexag = dec2sexag($self, $latitude);
223 2 my $long_sexag = dec2sexag($self, $longitude);
224  
225 2 ▶ $lat_sexag =~ tr/-/ / if($latitude < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $latitude < 0 1 1 2
226 2 ▶ $long_sexag =~ tr/S/W/ if($longitude < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $longitude < 0 1 1 2
227 2 ▶ $long_sexag =~ tr/N/E/ if($longitude > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $longitude > 0 1 1 2
228 2 ▶ $long_sexag =~ tr/-/ / if($longitude < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $longitude < 0 1 1 2
229  
230 2 return ($lat_sexag, $long_sexag);
231 }
232  
233 =head2 degree2loc
234  
235 Gets a string with the latitude and a string with the longitude, in sexagesimal
236 notation, of a point on Earth and returns a string with the grid locator notation.
237 Grid precision now is 8 digit.
238  
239 my $lat = "41N23";
240 my $long = "2E11";
241 my $locator = $foo->degree2loc($lat, $long);
242  
243 =cut
244  
245 5 ▶sub degree2loc {
Subroutine degree2loc: called CC=2 SCAR=7.0
Pod documented
246 5 my ($self, $lat, $long) = @_;
247 5 my %deg_coord = (
248 lat_1 => $lat,
249 long_1 => $long
250 );
251 5 my %dec_coord = sexag2dec($self, %deg_coord);
252 5 my %check = check_error(%dec_coord);
253 0 ▶ if ($check{_error}) { return "Error to convert degrees to locator."; }
Branch: 1 branch
BranchTrueFalseTotal
if ($check{"_error"}) 0 5 5
254  
255 5 my $m = new Ham::Locator;
256 5 $m->set_precision($locator_precision);
257 5 $m->set_latlng($dec_coord{lat_1_dec}, $dec_coord{long_1_dec});
258 5 my $loc = $m->latlng2loc;
259 }
260  
261 =head2 compass
262  
263 Gets an integer with a decimal degree and returns a string with its
264 equivalent value in a compass (North, East, ...). It uses a separation of 11.25
265 degrees for each position, 32 cardinal positions of total.
266 Values range must be between 0 to 360 degrees.
267  
268 my $compass = $foo->compass("-90.0"); # returns "W" (west)
269 my $compass = $foo->compass("270.0"); # returns "W" (west)
270  
271 =cut
272  
273 3 ▶sub compass {
Subroutine compass: called CC=4 SCAR=15.2
Pod documented
274 3 my $self = shift;
275 3 my $course = shift;
276 3 my $pattern = qr/(\+?)(-?)\d{1,3}(\.?)(\d*)$/;
277 3 my $error_msg = "Error value must be a integer between 0 to 360";
278  
279 3 ▶ if ($course !~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $course =~ /$pattern/) { } 0 3 3
280 0 return $error_msg;
281 } else {
282 0 ▶ if ($course < -360 or $course > 360) { return $error_msg; }
Branch: 1 branch
BranchTrueFalseTotal
if ($course < -360 or $course > 360) 0 3 3
Condition33%1/3
$course < -360 or $course > 360
l!l&&r!l&&!r
003
MC/DC0%0/2
$course < -360 or $course > 360
$course < -360 $course > 360
Truth table33%1/3
A or B
A= $course < -360B= $course > 360
ABresult
1X1
011
000
283 3 my @rosa = ('NbE','NNE','NEbN','NE','NEbE','ENE','EbN','E','EbS','ESE','SEbE','SE','SEbS','SSE','SbE','S','SbW','SSW','SWbS','SW','SWbW','WSW','WbS','W','WbN','WNW','NWbW','NW','NWbN','NNW','NbW','N');
284  
285 3 my $rosa_index = int((+$course / 11.25))-1;
286 3 return $rosa[$rosa_index];
287 }
288 }
289  
290 =head2 dec2sexag
291  
292 Gets an integer with a decimal degree value and returns a string with its
293 equivalence to sexagesimal degree form. Only returns degrees and minutes.
294  
295 =cut
296  
297 7 ▶sub dec2sexag{
Subroutine dec2sexag: called CC=1 SCAR=0.0
Pod documented
298 7 my ($self, $course) = @_;
299 7 my @degree_part = split(/\./, $course);
300 7 my $decimal = (('0.'.$degree_part[1]) * 60);
301 7 my $min = int((sprintf("%.2f",$decimal)));
302 7 my $sexag = $degree_part[0].".".$min;
303 }
304  
305 =head2 sexag2dec
306  
307 Gets a hash with sexagesimal value (maximun four) and returns a hash with its
308 decimal values.
309  
310 Range of values are -90 to 90 for latitudes, and -180 to 180 for longitudes.
311  
312 Values must be a pair, longitude and latitude. Two values for one point or
313 four values (two pairs) for two points.
314  
315 There is not mandatory send a complete hash (4 values), but you will receive a
316 hash with the four.
317  
318 You can use it like this:
319  
320 my %coord = (
321 Long_1 => "41N23.30",
322 Lat_1 => "2E11.10"
323 );
324 my %sexag = $foo->sexag2dec(%coord);
325 foreach my $key (sort keys %sexag) {
326 say $key." = ".$sexag{$key} if ($sexag{$key});
327 }
328  
329 The index send it, you will receive with '_dec' suffix, ie, you send
330 'latitude' and receive 'latitude_dec'
331  
332 =cut
333  
334 9 ▶sub sexag2dec {
Subroutine sexag2dec: called CC=16 SCAR=31.5
Pod documented
335 9 my $self = shift;
336 9 my %coord = @_;
337  
338 9 my $error_msg_1 = "Error sexagesimal conversion. Out of range. (-90 to 90)";
339 9 my $error_msg_2 = "Error sexagesimal conversion. Out of range. (-180 to 180)";
340  
341 9 my $grad_match = qr|(\d{1,3})([NSEOW+-\.])(\d{1,2})\.{0,1}(\d{0,2}){0,1}|i;
342 9 my %coord_dec = (
343 lat_1_dec => '',
344 lat_2_dec => '',
345 long_1_dec => '',
346 long_2_dec => '',
347 );
348 9 my $secs = undef;
349 9 foreach my $key (sort keys %coord) {
350 0 ▶ if ($4) {$secs = $4/3600;} else {$secs = 0}
Branch: 1 branch
BranchTrueFalseTotal
if ($4) { } 0 22 22
351 22 ▶ if ($key && ($coord{$key} =~ $grad_match)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $coord{$key} =~ /$grad_match/) 22 0 22
Condition33%1/3
$key and $coord{$key} =~ /$grad_match/
!ll&&!rl&&r
0022
MC/DC0%0/2
$key and $coord{$key} =~ /$grad_match/
$key $coord{$key} =~ /$grad_match/
Truth table33%1/3
A and B
A= $keyB= $coord{$key} =~ /$grad_match/
ABresult
0X0
100
111
352 22 ▶ if ($2 eq 'S' || $2 eq 'W' || $2 eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq 'S' or $2 eq 'W' or $2 eq '-') { } 10 12 22
Condition83%5/6
$2 eq "S" or $2 eq "W" or $2 eq "-"
l!l&&r!l&&!r
10012
$2 eq "S" or $2 eq "W"
l!l&&r!l&&!r
4612
MC/DC66%2/3
$2 eq "S" or $2 eq "W" or $2 eq "-"
$2 eq "S" $2 eq "W" $2 eq "-"
Truth table75%3/4
A or B or C
A= $2 eq "S"B= $2 eq "W"C= $2 eq "-"
ABCresult
1XX1
01X1
0011
0000
353 10 $coord_dec{$key.'_dec'} = (-($1+(($3/60)+$secs)));
354 0 ▶ if ($2 eq 'S' && $coord_dec{$key.'_dec'} < -90) { $coord_dec{$key.'_dec'} = $error_msg_1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "S" and $coord_dec{$key . '_dec'} < -90) 0 10 10
Condition66%2/3
$2 eq "S" and $coord_dec{$key . '_dec'} < -90
!ll&&!rl&&r
640
MC/DC0%0/2
$2 eq "S" and $coord_dec{$key . '_dec'} < -90
$2 eq "S" $coord_dec{$key . '_dec'} < -90
Truth table66%2/3
A and B
A= $2 eq "S"B= $coord_dec{$key . '_dec'} < -90
ABresult
0X0
100
111
355 0 ▶ if ($2 eq 'W' && $coord_dec{$key.'_dec'} < -180) { $coord_dec{$key.'_dec'} = $error_msg_2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "W" and $coord_dec{$key . '_dec'} < -180) 0 10 10
Condition66%2/3
$2 eq "W" and $coord_dec{$key . '_dec'} < -180
!ll&&!rl&&r
460
MC/DC0%0/2
$2 eq "W" and $coord_dec{$key . '_dec'} < -180
$2 eq "W" $coord_dec{$key . '_dec'} < -180
Truth table66%2/3
A and B
A= $2 eq "W"B= $coord_dec{$key . '_dec'} < -180
ABresult
0X0
100
111
356 } else {
357 12 $coord_dec{$key.'_dec'} = ($1+(($3/60)+$secs));
358 0 ▶ if ($2 eq 'N' && $coord_dec{$key.'_dec'} > 90) { $coord_dec{$key.'_dec'} = $error_msg_1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "N" and $coord_dec{$key . '_dec'} > 90) 0 12 12
Condition66%2/3
$2 eq "N" and $coord_dec{$key . '_dec'} > 90
!ll&&!rl&&r
570
MC/DC0%0/2
$2 eq "N" and $coord_dec{$key . '_dec'} > 90
$2 eq "N" $coord_dec{$key . '_dec'} > 90
Truth table66%2/3
A and B
A= $2 eq "N"B= $coord_dec{$key . '_dec'} > 90
ABresult
0X0
100
111
359 0 ▶ if ($2 eq 'E' && $coord_dec{$key.'_dec'} > 180) { $coord_dec{$key.'_dec'} = $error_msg_2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "E" and $coord_dec{$key . '_dec'} > 180) 0 12 12
Condition66%2/3
$2 eq "E" and $coord_dec{$key . '_dec'} > 180
!ll&&!rl&&r
750
MC/DC0%0/2
$2 eq "E" and $coord_dec{$key . '_dec'} > 180
$2 eq "E" $coord_dec{$key . '_dec'} > 180
Truth table66%2/3
A and B
A= $2 eq "E"B= $coord_dec{$key . '_dec'} > 180
ABresult
0X0
100
111
360 }
361 }
362 }
363 9 return %coord_dec;
364 }
365  
366 =head2 cicle_sun
367  
368 Gets three strings with latitude, longitude, in decimal degrees, and date, in
369 'dd-mm-yyyy' format and returns a hash with Sunrise time, Sunset time and
370 Midday time in hours and minutes format in Universal Time (UTC).
371  
372 my %sun = $foo->cicle_sun($lat,$long,$date);
373  
374 =cut
375  
376 5 ▶sub cicle_sun {
Subroutine cicle_sun: called CC=15 SCAR=27.7
Pod documented
377 5 my $self = shift;
378 5 my ($Lat, $Long, $date, $origin_point) = @_;
379  
380 5 ▶ $origin_point = "" if (!$origin_point);
Branch: 1 branch
BranchTrueFalseTotal
unless $origin_point 1 4 5
381 5 ▶ $date = "00-00-0000" if ($date =~ /Error/);
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /Error/ 0 5 5
382 5 ▶ $Lat = 0 if ($Lat =~ /Error/);
Branch: 1 branch
BranchTrueFalseTotal
if $Lat =~ /Error/ 0 5 5
383 5 ▶ $Long = 0 if ($Long =~ /Error/);
Branch: 1 branch
BranchTrueFalseTotal
if $Long =~ /Error/ 0 5 5
384  
385 5 my @date_ = date_split($self, $date);
386 5 my $day = $date_[0];
387 5 my $month = $date_[1];
388 5 my $year = $date_[2];
389 5 my $UT = '0';
390  
391 5 ▶ if ($day =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day =~ /Error/) 0 5 5
392 0 return my %solar_cycle = (_error => $day);
393 }
394  
395  
396 # Julian data
397 5 my $GGG = 1;
398 5 my $S = 1;
399  
400 0 ▶ if ($year <= 1585) { $GGG = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($year <= 1585) 0 5 5
401 5 my $JD = -1 * int(7 * (int(($month + 9) / 12 ) + $year) / 4);
402 5 ▶ if (($month - 9) < 0) { $S = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($month - 9 < 0) 5 0 5
403 5 my $A = abs($month - 9);
404 5 my $J1 = int($year + $S * int($A / 7));
405 5 $J1 = -1 * int((int($J1 / 100) + 1) * 3 / 4);
406 5 $JD = $JD + int(275 * $month / 9) + $day + ($GGG * $J1);
407 5 $JD = $JD + 1721027 + 2 * $GGG + 367 * $year - 0.5;
408 5 my $J2 = $JD;
409  
410 # Earth values
411 5 my $RAD = 180 / pi;
412 5 my $ET = 0.016718;
413 5 my $VP = 8.22e-5;
414 5 my $P = 4.93204;
415 5 my $M0 = 2.12344;
416 5 my $MN = 1.72019e-2;
417 5 my $T0 = 2444000.5;
418 5 $S = 2415020.5;
419 5 $P = $P + ($J2 - $T0) * $VP / 100;
420 5 my $AM = $M0 + $MN * ($J2 - $T0);
421 5 $AM = $AM - 2 * pi * int($AM / (2 * pi));
422  
423 # Kepler equation for the Earth
424 5 my $V = $AM + 2 * $ET * sin($AM) + 1.25 * $ET * $ET * sin(2 * $AM);
425 5 ▶ if ($V < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($V < 0) 0 5 5
426 0 $V = 2 * pi + $V;
427 }
428 5 my $L = $P + $V;
429 5 $L = $L - 2 * pi * int($L / (2 * pi));
430  
431 #AR and DEC calculus
432 5 my $Z = ($J2 - 2415020.5) / 365.2422;
433 5 my $OB = 23.452294 - (0.46845 * $Z + 0.00000059 * $Z * $Z) / 3600;
434 5 $OB = $OB / $RAD;
435 5 my $DC = asin(sin($OB) * sin($L));
436 5 my $AR = acos(cos($L) / cos($DC));
437 5 ▶ if ($L > pi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($L > "3.14159265358979") 0 5 5
438 0 $AR = 2 * pi - $AR;
439 }
440 5 $OB = $OB * $RAD;
441 5 $L = $L * $RAD;
442 5 $AR = $AR * 12 / pi;
443  
444 # HH.MM to AR conversion
445 5 my $H = int($AR);
446 5 my $M = int(($AR - int($AR)) * 60);
447 5 $S=(($AR - int($AR)) * 60 - $M) * 60;
448 5 $DC = $DC * $RAD;
449  
450 # Degrees conversion from DEC
451 5 my $D = abs($DC);
452 5 ▶ if ($DC > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DC > 0) { } 5 0 5
453 5 my $G1 = int($D);
454 } else {
455 0 my $G1 = (-1) * int($D);
456 }
457 5 my $M1 = int(($D - int($D)) * 60);
458 5 my $S1 = (($D - int($D)) * 60 - $M1) * 60;
459 5 ▶ if ($DC < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DC < 0) 0 5 5
460 0 $M1 = -$M1;
461 0 $S1 = -$S1;
462 }
463  
464 # Time equation
465 5 my $MR = 0.04301;
466 5 my $F = 13750.987;
467 5 my $C = 2 * $ET * $F * sin($AM) + 1.25 * $ET * $ET * $F * sin(2 * $AM);
468 5 my $R = -$MR * $F * sin(2 * ($P + $AM)) + $MR * $MR * $F * sin(4 * ($P + $AM)) / 2;
469 5 $ET = $C + $R;
470  
471 # Semi-diurn arc calculus
472 5 my $H0 = acos(-tan($Lat / $RAD) * tan($DC / $RAD));
473 5 $H0 = $H0 * $RAD;
474  
475 # DEC variations
476 5 my $VD = 0.9856 * sin($OB / $RAD) * cos($L / $RAD) / cos($DC / $RAD);
477  
478 # Sun rise calculus
479 5 my $VDOR = $VD * (-$H0 + 180) / 360;
480 5 my $DCOR = $DC + $VDOR;
481 5 my $HORTO = -acos(-tan($Lat / $RAD) * tan($DCOR / $RAD));
482 5 my $VHORTO = 5 / (6 * cos($Lat / $RAD) * cos($DCOR / $RAD) * sin($HORTO));
483 5 $HORTO = ($HORTO * $RAD + $VHORTO) / 15;
484 5 my $TUORTO = $HORTO + $ET / 3600 - $Long / 15 + 12;
485  
486 # Sun rise value conversion to HH.MM
487 5 my $HOR = int($TUORTO);
488 5 my $MOR = int(($TUORTO - $HOR) * 60 + 0.5);
489  
490 # AZ calculation
491 5 my $TUC = 12 + $ET / 3600 - $Long / 15;
492  
493 # AZ value conversion to HH.MM
494 5 my $HC = int($TUC);
495 5 my $MC = int(($TUC - $HC) * 60 + 0.5);
496  
497 # Sunset calculus
498 5 my $VDOC = $VD * ($H0 + 180) / 360;
499 5 my $DCOC = $DC + $VDOC;
500 5 my $HOC = acos(-tan($Lat / $RAD) * tan($DCOC / $RAD));
501 5 my $VHOC = 5 / (6 * cos($Lat / $RAD) * cos($DCOC / $RAD) * sin($HOC));
502 5 $HOC = ($HOC * $RAD + $VHOC) / 15;
503 5 my $TUOC = $HOC + $ET / 3600 - $Long / 15 + 12;
504  
505 # Sunset conversion to HH.MM
506 5 $HOC = int($TUOC);
507 5 my $MOC = int(($TUOC - $HOC) * 60 + 0.5);
508  
509 # Altitude of AZ
510 5 my $HCUL = 90 - $Lat + ($DCOR + $DCOC) / 2;
511  
512 # Degree conversion from altitude
513 5 my $GCUL = int($HCUL);
514 5 my $MCUL = int (($HCUL - $GCUL) * 60 + 0.5);
515  
516 # AZ from Sunrise and Sunset
517 5 my $ACOC = acos(-sin($DCOC / $RAD) / cos($Lat / $RAD)) * $RAD;
518 5 my $ACOR = 360 - acos(-sin($DCOR / $RAD) / cos($Lat / $RAD)) * $RAD;
519  
520 # AZ conversion to degrees
521 5 my $GACOC = int($ACOC);
522 5 my $MACOC = int(($ACOC - $GACOC) * 60 + 0.5);
523 5 my $GACOR = int($ACOR);
524 5 my $MACOR = int(($ACOR - $GACOR) * 60 + 0.5);
525  
526 5 my $sunrise = $HOR."h ".$MOR."m";
527 5 my $sunset = $HOC."h ".$MOC."m";
528 5 my $midday = $HC."h ".$MC."m";
529  
530 5 my $k_sunrise = "sunrise".$origin_point;
531 5 my $k_sunset = "sunset".$origin_point;
532 5 my $k_midday = "midday".$origin_point;
533  
534 5 my %solar_cycle = (
535 $k_sunrise => $sunrise,
536 $k_sunset => $sunset,
537 $k_midday => $midday,
538 );
539  
540 5 ▶ if ($day =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day =~ /Error/) 0 5 5
541 0 return my %solar_cycle = (_error => $day);
542 }
543 5 ▶ if ($Lat == 0 || $Long == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lat == 0 or $Long == 0) 0 5 5
Condition33%1/3
$Lat == 0 or $Long == 0
l!l&&r!l&&!r
005
MC/DC0%0/2
$Lat == 0 or $Long == 0
$Lat == 0 $Long == 0
Truth table33%1/3
A or B
A= $Lat == 0B= $Long == 0
ABresult
1X1
011
000
544 0 %solar_cycle = (_error => "Error sexagesimal conversion. Out of range.");
545 }
546  
547 5 return %solar_cycle;
548 }
549  
550 =head2 date_split
551  
552 Gets a string with date in format 'dd-mm-yyyy' and check it if value is a valid date.
553  
554 Returns an array with the day, month and year ... or error message.
555  
556 =cut
557  
558 6 ▶sub date_split {
Subroutine date_split: called CC=2 SCAR=7.2
Pod documented
559 6 my ($self, $date) = @_;
560 6 my @part_of_date;
561 6 my $check = is_date($date);
562 6 ▶ if ($check !~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $check =~ /Error/) { } 6 0 6
563 6 return @part_of_date = split(/-/,$date);
564 } else {
565 0 return $part_of_date[0] = $check;
566 }
567 }
568  
569 # ---------------
570 # INTERNAL SUBS
571 # ---------------
572  
573 =head1 Internals subs
574  
575 =head2 data_constructor
576  
577 Internal function used by data_by_coordinates() and data_by_locator() to call the others functions and create a response.
578  
579 =head2 NESW
580  
581 Internal function to convert degrees to radians.
582  
583 =head2 check_error
584  
585 Internal function to check errors in data_by_coordinates() or data_by_locator().
586  
587 =head2 is_date
588  
589 Internal function to check if a date is valid.
590  
591 =cut
592  
593 4 ▶sub NESW {
Subroutine NESW: called CC=1 SCAR=0.0
Pod documented
594 4 deg2rad($_[0]), deg2rad(90 - $_[1])
595 }
596  
597 6 ▶sub is_date {
Subroutine is_date: called CC=14 SCAR=42.3
Pod documented
598 6 my $date = shift;
599 6 my $pattern = qr/\d{1,2}(-)\d{1,2}(-)\d{4}/;
600  
601 6 ▶ if ($date !~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $date =~ /$pattern/) { } 0 6 6
602 0 return "Error date format. Must be dd-mm-yyyy";
603 } else {
604 6 my @part_of_date = split(/-/,$date);
605  
606 6 ▶ my $intDay = ($part_of_date[0] <= 9) ? sprintf("%02d", $part_of_date[0]) : sprintf("%2d", $part_of_date[0]);
Branch: 1 branch
BranchTrueFalseTotal
$part_of_date[0] <= 9 ? : 0 6 6
607 6 ▶ my $intMonth = ($part_of_date[1] <= 9) ? sprintf("%02d", $part_of_date[1]) : sprintf("%2d", $part_of_date[1]);
Branch: 1 branch
BranchTrueFalseTotal
$part_of_date[1] <= 9 ? : 6 0 6
608 6 my $intYear = $part_of_date[2];
609  
610 6 my %array_month = (
611 '01' => 31,
612 '02' => 0,
613 '03' => 31,
614 '04' => 30,
615 '05' => 31,
616 '06' => 30,
617 '07' => 31,
618 '08' => 31,
619 '09' => 30,
620 '10' => 31,
621 '11' => 30,
622 '12' => 31,
623 );
624  
625 0 ▶ if ($intMonth > 12) { return "Error in date format. This must be a valid dd-mm-yyyy." }
Branch: 1 branch
BranchTrueFalseTotal
if ($intMonth > 12) 0 6 6
626  
627 6 ▶ if ($array_month{$intMonth} != 0 && $part_of_date[0] <= $array_month{$intMonth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array_month{$intMonth} != 0 and $part_of_date[0] <= $array_month{$intMonth}) 6 0 6
Condition33%1/3
$array_month{$intMonth} != 0 and $part_of_date[0] <= $array_month{$intMonth}
!ll&&!rl&&r
006
MC/DC0%0/2
$array_month{$intMonth} != 0 and $part_of_date[0] <= $array_month{$intMonth}
$array_month{$intMonth} != 0 $part_of_date[0] <= $array_month{$intMonth}
Truth table33%1/3
A and B
A= $array_month{$intMonth} != 0B= $part_of_date[0] <= $array_month{$intMonth}
ABresult
0X0
100
111
628 6 return 1;
629 }
630  
631 0 ▶ if ($intMonth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intMonth == 0) 0 0 0
632 0 ▶ if ($intDay > 0 && $intDay < 29) {
Branch: 2 branches
BranchTrueFalseTotal
if ($intDay > 0 and $intDay < 29) { } 0 0 0
elsif ($intDay == 29) { } 0 0 0
Condition0%0/3
$intDay > 0 and $intDay < 29
!ll&&!rl&&r
000
MC/DC0%0/2
$intDay > 0 and $intDay < 29
$intDay > 0 $intDay < 29
Truth table0%0/3
A and B
A= $intDay > 0B= $intDay < 29
ABresult
0X0
100
111
633 0 return 1;
634 }
635 elsif ($intDay == 29) {
636 0 ▶ if (($intYear % 4 == 0) && ($intYear % 100 != 0) || ($intYear % 400) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intYear % 4 == 0 and $intYear % 100 != 0 or $intYear % 400 == 0) 0 0 0
Condition0%0/6
$intYear % 4 == 0 and $intYear % 100 != 0 or $intYear % 400 == 0
l!l&&r!l&&!r
000
$intYear % 4 == 0 and $intYear % 100 != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$intYear % 4 == 0 and $intYear % 100 != 0 or $intYear % 400 == 0
$intYear % 4 == 0 $intYear % 100 != 0 $intYear % 400 == 0
Truth table0%0/5
A and B or C
A= $intYear % 4 == 0B= $intYear % 100 != 0C= $intYear % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
637 0 return 1;
638 }
639 }
640 }
641  
642 0 return "Error in date format. This must be a valid dd-mm-yyyy.";
643 }
644 }
645  
646 7 ▶sub check_error {
Subroutine check_error: called CC=3 SCAR=12.5
Pod documented
647 7 my %coord = @_;
648 7 foreach my $key (sort keys %coord) {
649 36 ▶ if ($coord{$key} =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coord{$key} =~ /Error/) 0 36 36
650 0 $coord{_error} = $coord{$key};
651 0 $coord{$key} = 0;
652 }
653 }
654 7 return %coord;
655 }
656  
657  
658 =head1 Cheking Errors
659  
660 In functions that return only a string or an array, errors will detect to match /Error/ word.
661 In complex functions, like data_by_coordinates, that responses with a hash, you check the '_error' index, i.e:
662  
663 %data = $foo->data_by_locator($date,$locator_1,$locator_2);
664 if (!$data{_error}) {
665 foreach my $key (sort keys %data) {
666 say $key.": ".$data{$key};
667 }
668 } else {
669 say $data{_error};
670 }
671  
672 ... or something like this :p
673  
674 =cut
675  
676  
677 =head1 AUTHOR
678  
679 CJUAN, C<< <cjuan at cpan.org> >>
680  
681 =head1 BUGS
682  
683 Please report any bugs or feature requests to C<bug-ham-resources-utils at rt.cpan.org>, or through
684 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Ham-Resources-Utils>. I will be notified, and then you'll
685 automatically be notified of progress on your bug as I make changes.
686  
687  
688  
689  
690 =head1 SUPPORT
691  
692 You can find documentation for this module with the perldoc command.
693  
694 perldoc Ham::Resources::Utils
695  
696  
697 You can also look for information at:
698  
699 =over 4
700  
701 =item * RT: CPAN's request tracker (report bugs here)
702  
703 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Ham-Resources-Utils>
704  
705 =item * AnnoCPAN: Annotated CPAN documentation
706  
707 L<http://annocpan.org/dist/Ham-Resources-Utils>
708  
709 =item * CPAN Ratings
710  
711 L<http://cpanratings.perl.org/d/Ham-Resources-Utils>
712  
713 =item * Search CPAN
714  
715 L<http://search.cpan.org/dist/Ham-Resources-Utils/>
716  
717 =back
718  
719 =head1 TODO
720  
721 =over 4
722  
723 =item * Add long path course and distances from point A to B
724  
725 =item * Add a function to calculate X and Y coordinates based on real coordinates for use it on a geographical projection (or Plate Carree)
726  
727 =back
728  
729  
730 =head1 LICENSE AND COPYRIGHT
731  
732 Copyright 2012-2018 CJUAN.
733  
734 This program is free software; you can redistribute it and/or modify it
735 under the terms of either: the GNU General Public License as published
736 by the Free Software Foundation; or the Artistic License.
737  
738 See http://dev.perl.org/licenses/ for more information.
739  
740  
741 =cut
742  
743 10 1; # End of Ham::Resources::Utils
↑ Summary