blib/lib/Ham/Resources/Utils.pm

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