TropicalCyclone/Storm.pm

filter: Statementstmt 100% 152 / 152 Branchbran 97.7% 43 / 44 Conditioncond 60.4% 29 / 48 MC/DCmcdc 25.9% 7 / 27 Subroutinesub 100% 37 / 37 Podpod 95.4% 21 / 22 totaltotal 87.5% 289 / 330 CC 41 SCAR 14.7
CC
41
Cov
92%
_get_file
8.4
new
8.3
fetch_forecastGraphics_urls
7.1
CRAP 4.3
« TropicalCyclone/HTTP.pm ↑ Summary TropicalCyclone/Resource.pm »
1 package Weather::NHC::TropicalCyclone::Storm;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Util::H2O::More qw/baptise/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Weather::NHC::TropicalCyclone::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Weather::NHC::TropicalCyclone::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Weather::NHC::TropicalCyclone::Validation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $DEFAULT_GRAPHICS_ROOT = q{https://www.nhc.noaa.gov/storm_graphics};
12 3 our $DEFAULT_BTK_ROOT = q{https://ftp.nhc.noaa.gov/atcf/btk};
13 3 our $CLASSIFICATIONS = Weather::NHC::TropicalCyclone::Validation->classifications;
14  
15 3 our @COMPAT_FIELDS = qw/
16 latitudelongitude latitude_numberic kmzFile34kt kmzFile50kt kmzFile64kt
17 /;
18  
19 3 our @FIELDS = qw/
20 id binNumber name classification intensity pressure latitude longitude
21 latitudeNumeric longitudeNumeric movementDir movementSpeed lastUpdate
22 publicAdvisory forecastAdvisory windSpeedProbabilities forecastDiscussion
23 forecastGraphics forecastTrack windWatchesWarnings trackCone initialWindExtent
24 forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS
25 mostLikelyTimeTSWindsGIS windSpeedProbabilitiesGIS stormSurgeWatchWarningGIS
26 potentialStormSurgeFloodingGIS peakSurgeKML
27 /;
28  
29 23 ▶sub new {
Subroutine new: called CC=8 SCAR=21.1
Pod undocumented
30 23 my ( $class, $data, %args ) = @_;
31 23 ▶ die qq{Storm constructor requires a hash reference\n} if reftype($data) ne q{HASH};
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($data) ne "HASH" 1 22 23
32  
33 22 for my $field (qw/id binNumber name classification/) {
34 die qq{Field validation errors found creating package instance for: $field\n}
35 82 ▶ if !exists $data->{$field} || !defined $data->{$field} || $data->{$field} eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $data->{$field} && defined $data->{$field} or $data->{$field} eq "" 4 78 82
Condition66%4/6
not exists $data->{$field} && defined $data->{$field} or $data->{$field} eq ""
l!l&&r!l&&!r
4078
exists $data->{$field} && defined $data->{$field}
!ll&&!rl&&r
4078
MC/DC33%1/3
not exists $data->{$field} && defined $data->{$field} or $data->{$field} eq ""
exists $data->{$field} defined $data->{$field} $data->{$field} eq ""
Truth table50%2/4
not(A && B) or C
A= exists $data->{$field}B= defined $data->{$field}C= $data->{$field} eq ""
ABCresult
0XX1
10X1
1111
1100
36 }
37 18 ▶ if ( !exists $CLASSIFICATIONS->{ $data->{classification} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$Weather::NHC::TropicalCyclone::Storm::CLASSIFICATIONS{$data->{"classification"}}) 1 17 18
38 1 die qq{Field validation errors found creating package instance for: classification\n};
39 }
40  
41 17 my %copy = %$data;
42 17 my $self = baptise -recurse, \%copy, $class, @FIELDS, @COMPAT_FIELDS, q{_http};
43 17 ▶ $self->{_http} = $args{http} || Weather::NHC::TropicalCyclone::HTTP->new;
Condition33%1/3
$args{'http'} || 'Weather::NHC::TropicalCyclone::HTTP'->new
l!l&&r!l&&!r
1700
MC/DC0%0/2
$args{'http'} || 'Weather::NHC::TropicalCyclone::HTTP'->new
$args{'http'} 'Weather::NHC::TropicalCyclone::HTTP'->new
Truth table33%1/3
A || B
A= $args{'http'}B= 'Weather::NHC::TropicalCyclone::HTTP'->new
ABresult
1X1
011
000
44 17 return $self;
45 }
46  
47 1 ▶sub _get_validation_rules {
Subroutine _get_validation_rules: called CC=1 SCAR=0.0
48 return {
49 required => [qw/id binNumber name classification/],
50 1 classifications => { %{$CLASSIFICATIONS} },
51 };
52 }
53  
54 1 ▶sub _fetch_text_types {
Subroutine _fetch_text_types: called CC=1 SCAR=0.0
55 1 return { text => Weather::NHC::TropicalCyclone::Resource->text_resources };
56 }
57  
58 11 ▶sub _fetch_data_types {
Subroutine _fetch_data_types: called CC=1 SCAR=0.0
59 11 return Weather::NHC::TropicalCyclone::Resource->file_types;
60 }
61  
62 3 ▶sub kind {
Subroutine kind: called CC=3 SCAR=11.0
Pod documented
63 3 my $self = shift;
64 3 ▶ die qq{'classification' field not set\n} if !$self->classification;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->classification 1 2 3
65 2 ▶ die qq{Unknown storm classification\n} if !exists $CLASSIFICATIONS->{ $self->classification };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$Weather::NHC::TropicalCyclone::Storm::CLASSIFICATIONS{$self->classification} 1 1 2
66 1 return $CLASSIFICATIONS->{ $self->classification };
67 }
68  
69 5 ▶sub basin {
Subroutine basin: called CC=6 SCAR=17.9
Pod documented
70 5 my $self = shift;
71 5 my $bin = $self->binNumber;
72 5 ▶ die qq{'binNumber' field not set\n} if !defined $bin || $bin eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bin or $bin eq "" 1 4 5
Condition66%2/3
not defined $bin or $bin eq ""
l!l&&r!l&&!r
104
MC/DC50%1/2
not defined $bin or $bin eq ""
not defined $bin $bin eq ""
Truth table66%2/3
not(A) or B
A= not defined $binB= $bin eq ""
ABresult
1X1
011
000
73 4 ▶ return q{atlantic} if $bin =~ /^AT[1-5]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $bin =~ /^AT[1-5]$/i 1 3 4
74 3 ▶ return q{pacific} if $bin =~ /^EP[1-5]$/i; # historical return value
Branch: 1 branch
BranchTrueFalseTotal
if $bin =~ /^EP[1-5]$/i 1 2 3
75 2 ▶ return q{central_pacific} if $bin =~ /^CP[1-5]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $bin =~ /^CP[1-5]$/i 1 1 2
76 1 return undef;
77 }
78  
79 3 ▶sub fetch_forecastGraphics_urls {
Subroutine fetch_forecastGraphics_urls: called CC=7 SCAR=19.7
Pod documented
80 3 my $self = shift;
81 3 my $graphics = $self->{forecastGraphics};
82 3 ▶ return [] if !defined $graphics || reftype($graphics) ne q{HASH} || !$graphics->{url};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $graphics or builtin::reftype($graphics) ne "HASH" or not $graphics->{"url"} 1 2 3
Condition66%4/6
not defined $graphics or builtin::reftype($graphics) ne "HASH" or not $graphics->{"url"}
l!l&&r!l&&!r
102
not defined $graphics or builtin::reftype($graphics) ne "HASH"
l!l&&r!l&&!r
102
MC/DC33%1/3
not defined $graphics or builtin::reftype($graphics) ne "HASH" or not $graphics->{"url"}
not defined $graphics builtin::reftype($graphics) ne "HASH" not $graphics->{"url"}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $graphicsB= builtin::reftype($graphics) ne "HASH"C= not $graphics->{"url"}
ABCresult
1XX1
01X1
0011
0000
83  
84 2 my $response = $self->{_http}->get( $graphics->{url} );
85 2 ▶ my $html = $response->{content} // q{};
Condition50%1/2
$response->{"content"} // ""
l!l
20
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
86 2 my ($prefix) = $html =~ m{storm_graphics/(.+?)/refresh};
87 2 ▶ return [] if !$prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 1 1 2
88  
89 1 my $base = "$DEFAULT_GRAPHICS_ROOT/$prefix";
90 1 $response = $self->{_http}->get($base);
91 1 ▶ $html = $response->{content} // q{};
Condition50%1/2
$response->{"content"} // ""
l!l
10
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
92  
93 1 my $id = uc $self->id;
94 1 my @images = ( $html =~ m{href=["']($id[^"']+\.png)["']}gi );
95 1 return [ map { "$base/$_" } @images ];
96 }
97  
98 10 ▶sub _get_text {
Subroutine _get_text: called CC=7 SCAR=19.6
99 10 my ( $self, $resource, $local_file ) = @_;
100 10 ▶ die qq{Resource '$resource' is not a supported text resource\n}
Branch: 1 branch
BranchTrueFalseTotal
if "Weather::NHC::TropicalCyclone::Resource"->kind($resource, undef) ne "text" 1 9 10
101 if Weather::NHC::TropicalCyclone::Resource->kind( $resource, undef ) ne q{text};
102  
103 9 my $object = $self->{$resource};
104 9 ▶ return undef if !defined $object || reftype($object) ne q{HASH} || !$object->{url};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $object or builtin::reftype($object) ne "HASH" or not $object->{"url"} 1 8 9
Condition66%4/6
not defined $object or builtin::reftype($object) ne "HASH" or not $object->{"url"}
l!l&&r!l&&!r
108
not defined $object or builtin::reftype($object) ne "HASH"
l!l&&r!l&&!r
108
MC/DC33%1/3
not defined $object or builtin::reftype($object) ne "HASH" or not $object->{"url"}
not defined $object builtin::reftype($object) ne "HASH" not $object->{"url"}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $objectB= builtin::reftype($object) ne "HASH"C= not $object->{"url"}
ABCresult
1XX1
01X1
0011
0000
105  
106 8 my $response = $self->{_http}->get( $object->{url} );
107 8 ▶ my $text = _extract_pre_text( $response->{content} // q{}, $object->{url} );
Condition50%1/2
$response->{"content"} // ""
l!l
80
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
108  
109 7 ▶ _write_file( $local_file, $text ) if defined $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_file 2 5 7
110 6 return ( $text, $object->{advNum}, $local_file );
111 }
112  
113 3 ▶sub fetch_publicAdvisory {
Subroutine fetch_publicAdvisory: called CC=1 SCAR=0.0
Pod documented
114 3 my ( $self, $local_file ) = @_;
115 3 return $self->_get_text( q{publicAdvisory}, $local_file );
116 }
117  
118 1 ▶sub fetch_forecastAdvisory {
Subroutine fetch_forecastAdvisory: called CC=1 SCAR=0.0
Pod documented
119 1 my ( $self, $local_file ) = @_;
120 1 return $self->_get_text( q{forecastAdvisory}, $local_file );
121 }
122  
123 2 ▶sub fetch_forecastAdvisory_as_atcf {
Subroutine fetch_forecastAdvisory_as_atcf: called CC=2 SCAR=6.9
Pod documented
124 2 my ( $self, $local_file ) = @_;
125 2 my ( $text, $adv_num ) = $self->_get_text(q{forecastAdvisory});
126 2 require Weather::NHC::TropicalCyclone::ForecastAdvisory;
127  
128 2 my $forecast = Weather::NHC::TropicalCyclone::ForecastAdvisory->new(
129 input_text => $text,
130 output_file => $local_file,
131 );
132 2 $forecast->extract_atcf;
133 2 ▶ $forecast->save_atcf if defined $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_file 1 1 2
134 2 return ( $forecast->as_atcf, $adv_num, $local_file );
135 }
136  
137 2 ▶sub fetch_forecastDiscussion {
Subroutine fetch_forecastDiscussion: called CC=1 SCAR=0.0
Pod documented
138 2 my ( $self, $local_file ) = @_;
139 2 return $self->_get_text( q{forecastDiscussion}, $local_file );
140 }
141  
142 1 ▶sub fetch_windspeedProbabilities {
Subroutine fetch_windspeedProbabilities: called CC=1 SCAR=0.0
Pod documented
143 1 my ( $self, $local_file ) = @_;
144 1 return $self->_get_text( q{windSpeedProbabilities}, $local_file );
145 }
146  
147 2 ▶sub fetch_forecastTrack { my $s = shift; return $s->_get_file( q{forecastTrack}, @_ ) }
Subroutine fetch_forecastTrack: called CC=1 SCAR=0.0
Pod documented
148 2 ▶sub fetch_windWatchesWarnings { my $s = shift; return $s->_get_file( q{windWatchesWarnings}, @_ ) }
Subroutine fetch_windWatchesWarnings: called CC=1 SCAR=0.0
Pod documented
149 3 ▶sub fetch_trackCone { my $s = shift; return $s->_get_file( q{trackCone}, @_ ) }
Subroutine fetch_trackCone: called CC=1 SCAR=0.0
Pod documented
150 2 ▶sub fetch_initialWindExtent { my $s = shift; return $s->_get_file( q{initialWindExtent}, @_ ) }
Subroutine fetch_initialWindExtent: called CC=1 SCAR=0.0
Pod documented
151 2 ▶sub fetch_forecastWindRadiiGIS { my $s = shift; return $s->_get_file( q{forecastWindRadiiGIS}, @_ ) }
Subroutine fetch_forecastWindRadiiGIS: called CC=1 SCAR=0.0
Pod documented
152 2 ▶sub fetch_bestTrackGIS { my $s = shift; return $s->_get_file( q{bestTrackGIS}, @_ ) }
Subroutine fetch_bestTrackGIS: called CC=1 SCAR=0.0
Pod documented
153 1 ▶sub fetch_earliestArrivalTimeTSWindsGIS { my $s = shift; return $s->_get_file( q{earliestArrivalTimeTSWindsGIS}, @_ ) }
Subroutine fetch_earliestArrivalTimeTSWindsGIS: called CC=1 SCAR=0.0
Pod documented
154 1 ▶sub fetch_mostLikelyTimeTSWindsGIS { my $s = shift; return $s->_get_file( q{mostLikelyTimeTSWindsGIS}, @_ ) }
Subroutine fetch_mostLikelyTimeTSWindsGIS: called CC=1 SCAR=0.0
Pod documented
155 5 ▶sub fetch_windSpeedProbabilitiesGIS { my $s = shift; return $s->_get_file( q{windSpeedProbabilitiesGIS}, @_ ) }
Subroutine fetch_windSpeedProbabilitiesGIS: called CC=1 SCAR=0.0
Pod documented
156 1 ▶sub fetch_stormSurgeWatchWarningGIS { my $s = shift; return $s->_get_file( q{stormSurgeWatchWarningGIS}, @_ ) }
Subroutine fetch_stormSurgeWatchWarningGIS: called CC=1 SCAR=0.0
Pod documented
157 2 ▶sub fetch_potentialStormSurgeFloodingGIS { my $s = shift; return $s->_get_file( q{potentialStormSurgeFloodingGIS}, @_ ) }
Subroutine fetch_potentialStormSurgeFloodingGIS: called CC=1 SCAR=0.0
Pod documented
158 1 ▶sub fetch_peakSurgeKML { my $s = shift; return $s->_get_file( q{peakSurgeKML}, @_ ) }
Subroutine fetch_peakSurgeKML: called CC=1 SCAR=0.0
Pod documented
159  
160 24 ▶sub _get_file {
Subroutine _get_file: called CC=8 SCAR=21.2
161 24 my ( $self, $resource, $type, $local_file ) = @_;
162  
163 24 ▶ if ( !Weather::NHC::TropicalCyclone::Resource->is_valid_file_type( $resource, $type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Weather::NHC::TropicalCyclone::Resource"->is_valid_file_type($resource, $type)) 1 23 24
164 1 die qq{'$type' is not a valid type provided by '$resource'.\n};
165 }
166  
167 23 my $object = $self->{$resource};
168 23 ▶ return undef if !defined $object || reftype($object) ne q{HASH} || !$object->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $object or builtin::reftype($object) ne "HASH" or not $object->{$type} 2 21 23
Condition66%4/6
not defined $object or builtin::reftype($object) ne "HASH" or not $object->{$type}
l!l&&r!l&&!r
2021
not defined $object or builtin::reftype($object) ne "HASH"
l!l&&r!l&&!r
2021
MC/DC33%1/3
not defined $object or builtin::reftype($object) ne "HASH" or not $object->{$type}
not defined $object builtin::reftype($object) ne "HASH" not $object->{$type}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $objectB= builtin::reftype($object) ne "HASH"C= not $object->{$type}
ABCresult
1XX1
01X1
0011
0000
169  
170 21 my $url = $object->{$type};
171 21 ▶ $local_file //= _filename_from_url($url);
Condition33%1/3
$local_file //= _filename_from_url($url)
l!l&&r!l&&!r
0210
MC/DC0%0/2
$local_file //= _filename_from_url($url)
$local_file _filename_from_url($url)
Truth table33%1/3
A //= B
A= $local_fileB= _filename_from_url($url)
ABresult
1X1
011
000
172 21 $self->{_http}->mirror( $url, $local_file );
173  
174 my $adv_num = exists $object->{advNum} && defined $object->{advNum}
175 ? $object->{advNum}
176 21 ▶ : q{N/A};
Branch: 1 branch
BranchTrueFalseTotal
exists $object->{'advNum'} && defined $object->{'advNum'} ? : 14 7 21
Condition66%2/3
exists $object->{"advNum"} and defined $object->{"advNum"}
!ll&&!rl&&r
7014
MC/DC50%1/2
exists $object->{"advNum"} and defined $object->{"advNum"}
exists $object->{"advNum"} defined $object->{"advNum"}
Truth table66%2/3
A and B
A= exists $object->{"advNum"}B= defined $object->{"advNum"}
ABresult
0X0
100
111
177 21 return ( $local_file, $adv_num );
178 }
179  
180 2 ▶sub fetch_best_track {
Subroutine fetch_best_track: called CC=2 SCAR=7.0
Pod documented
181 2 my ( $self, $local_file ) = @_;
182 2 my $btk_file = sprintf q{b%s.dat}, $self->id;
183 2 my $url = "$DEFAULT_BTK_ROOT/$btk_file";
184 2 ▶ $local_file //= $btk_file;
Condition66%2/3
$local_file //= $btk_file
l!l&&r!l&&!r
110
MC/DC0%0/2
$local_file //= $btk_file
$local_file $btk_file
Truth table66%2/3
A //= B
A= $local_fileB= $btk_file
ABresult
1X1
011
000
185 2 $self->{_http}->mirror( $url, $local_file );
186 2 return $local_file;
187 }
188  
189 9 ▶sub _extract_pre_text {
Subroutine _extract_pre_text: called CC=2 SCAR=6.9
190 9 my ( $html, $url ) = @_;
191 9 my ($text) = $html =~ m{<pre\b[^>]*>(.*?)</pre>}is;
192 9 ▶ die qq{No <pre> advisory text found at $url\n} if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 8 9
193  
194 # NHC text products are intentionally plain inside <pre>. Decode the small
195 # set of entities that can appear without pulling in a full HTML parser.
196 8 $text =~ s/<[^>]+>//g;
197 8 $text =~ s/&nbsp;/ /gi;
198 8 $text =~ s/&lt;/</gi;
199 8 $text =~ s/&gt;/>/gi;
200 8 $text =~ s/&quot;/"/gi;
201 8 $text =~ s/&#39;/'/gi;
202 8 $text =~ s/&amp;/&/gi;
203 8 $text =~ s/&#(\d+);/chr($1)/ge;
204 8 return $text;
205 }
206  
207 22 ▶sub _filename_from_url {
Subroutine _filename_from_url: called CC=3 SCAR=11.0
208 22 my ($url) = @_;
209 22 my ($file) = $url =~ m{/([^/?]+)(?:\?.*)?$};
210 22 ▶ die qq{Unable to determine local filename from URL '$url'\n} if !defined $file || $file eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 1 21 22
Condition66%2/3
not defined $file or $file eq ""
l!l&&r!l&&!r
1021
MC/DC50%1/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table66%2/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
211 21 return $file;
212 }
213  
214 2 ▶sub _write_file {
Subroutine _write_file: called CC=3 SCAR=11.0
215 2 my ( $file, $content ) = @_;
216 2 ▶ open my $fh, q{>}, $file or die qq{Failed to open '$file' for writing: $!\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open '${file}' for writing: $!\n" 1 1 2
217 1 print {$fh} $content;
218 1 ▶ close $fh or die qq{Failed to close '$file': $!\n};
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close '${file}': $!\n" 1 0 1
219 1 return $file;
220 }
221  
222 3 1;
223  
224 __END__
« TropicalCyclone/HTTP.pm ↑ Summary TropicalCyclone/Resource.pm »