TropicalCyclone/Storm.pm

filter: Statementstmt 58.9% 89 / 151 Branchbran 32.3% 11 / 34 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 73.5% 25 / 34 Podpod 90.4% 19 / 21 totaltotal 55.2% 148 / 268 CC 26 SCAR 21.9
CC
26
Cov
51%
_get_text
42.0
basin
20.0
_get_file
11.7
CRAP 9.0
« TropicalCyclone/ForecastAdvisory.pm ↑ Summary TropicalCyclone.pm »
1 package Weather::NHC::TropicalCyclone::Storm;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Status qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Util::H2O::More qw/baptise/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Validate::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTML::TreeBuilder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $DEFAULT_GRAPHICS_ROOT = q{https://www.nhc.noaa.gov/storm_graphics};
13 2 our $DEFAULT_BTK_ROOT = q{https://ftp.nhc.noaa.gov/atcf/btk};
14 2 our $CLASSIFICATIONS = {
15 TD => q{Tropical Depression},
16 STD => q{Subtropical Depression},
17 TS => q{Tropical Storm},
18 HU => q{Hurricane},
19 STS => q{Subtropical Storm},
20 PTC => q{Post-tropical Cyclone / Remnants},
21 TY => q{Typhoon (we don't use this currently)},
22 PC => q{Potential Tropical Cyclone},
23 };
24  
25 # constructor
26 sub new {
27 4 ▶ my ( $pkg, $self ) = @_;
Subroutine new: called CC=2 SCAR=7.5
Pod undocumented
28  
29 4 my $v = Validate::Tiny->new;
30 4 my $validation = $v->check( $self, $pkg->_get_validation_rules );
31 4 ▶ if ( not $v->success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v->success) 0 4 4
32 0 die qq{Field validation errors found creating package instance for: } . join( q{, }, keys %{ $validation->error } ) . qq{\n};
33 }
34  
35 4 my @fields =
36 (qw/id binNumber name classification intensity pressure latitudelongitude latitude_numberic movementDir movementSpeed lastUpdate publicAdvisory forecastAdvisory windSpeedProbabilities forecastDiscussion forecastGraphics forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS mostLikelyTimeTSWindsGIS windSpeedProbabilitiesGIS kmzFile34kt kmzFile50kt kmzFile64kt stormSurgeWatchWarningGIS potentialStormSurgeFloodingGIS/);
37  
38 4 return baptise -recurse, $self, $pkg, @fields;
39 }
40  
41 sub _get_validation_rules {
42 4 ▶ my $self = shift;
Subroutine _get_validation_rules: called CC=1 SCAR=0.1
43 return {
44 fields => [qw/id binNumber name classification/],
45 checks => [
46 [qw/id binNumber name classification/] => Validate::Tiny::is_required(),
47 classification => sub {
48 4 ▶ my ( $value, $params ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.4
49  
50 # branch, if true indicates failed validation
51 4 ▶ if ( not grep { /$value/ } ( keys %$CLASSIFICATIONS ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/$value/;} keys %$Weather::NHC::TropicalCyclone::Storm::CLASSIFICATIONS) 0 4 4
52 0 return q{Invalid classification, not defined in NHC specification.};
53 }
54  
55 # indicates successful validation
56 4 return undef;
57 },
58 4 ],
59 };
60 }
61  
62 sub _fetch_text_types {
63 0 ▶ my $self = shift;
Subroutine _fetch_text_types: not called CC=1 SCAR=6.9
64  
65 # white list of resources and URL attributes they provide
66 0 my $types = {
67 text => [qw/publicAdvisory forecastAdvisory forecastDiscussion windSpeedProbabilities/],
68 };
69  
70 0 return $types;
71 }
72  
73 sub _fetch_data_types {
74 41 ▶ my $self = shift;
Subroutine _fetch_data_types: called CC=1 SCAR=0.0
75  
76 # white list of resources and URL attributes they provide
77 41 my $types = {
78 zipFile => [qw/forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS potentialStormSurgeFloodingGIS/],
79 kmzFile => [qw/forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS mostLikelyTimeTSWindsGIS/],
80 zipFile5km => [qw/windSpeedProbabilitiesGIS/],
81 zipFile0p5deg => [qw/windSpeedProbabilitiesGIS/],
82 kmzFile34kt => [qw/windSpeedProbabilitiesGIS/],
83 kmzFile50kt => [qw/windSpeedProbabilitiesGIS/],
84 kmzFile64kt => [qw/windSpeedProbabilitiesGIS/],
85 kmlFile => [qw/stormSurgeWatchWarningGIS/],
86 zipFileTidalMask => [qw/potentialStormSurgeFloodingGIS/],
87 };
88  
89 41 return $types;
90 }
91  
92 # get storm classification "real classification"
93 sub kind {
94 2 ▶ my $self = shift;
Subroutine kind: called CC=3 SCAR=11.4
Pod documented
95 2 ▶ die qq{'classification' field not set\n} if not $self->classification;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->classification 0 2 2
96 2 ▶ die qq{Unknown storm classification\n} if not $CLASSIFICATIONS->{ $self->classification };
Branch: 1 branch
BranchTrueFalseTotal
unless $$Weather::NHC::TropicalCyclone::Storm::CLASSIFICATIONS{$self->classification} 0 2 2
97 2 return $CLASSIFICATIONS->{ $self->classification };
98 }
99  
100 # determine basin based on binNumber
101 sub basin {
102 0 ▶ my $self = shift;
Subroutine basin: not called CC=4 SCAR=30.0
Pod undocumented
103 0 ▶ die qq{'binNumber' field not set\n} if not $self->binNumber;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binNumber 0 0 0
104  
105 # allow for easy querying of "basin"
106 0 my $BASINS = {
107 atlantic => qr/^AT[1-5]$/i,
108 pacific => qr/^EP[1-5]$/i,
109 };
110 0 for my $basin ( keys %$BASINS ) {
111 0 ▶ return $basin if $self->binNumber =~ $BASINS->{$basin};
Branch: 1 branch
BranchTrueFalseTotal
if $self->binNumber =~ /$BASINS->{$basin}/ 0 0 0
112 }
113 0 return undef;
114 }
115  
116 # attempts to get base graphics directory, then scrapes
117 # the index page for the files and returns an array reference
118 # of all image addresses for this storm
119 sub fetch_forecastGraphics_urls {
120 0 ▶ my $self = shift;
Subroutine fetch_forecastGraphics_urls: not called CC=2 SCAR=17.9
Pod documented
121  
122 0 my $url = $self->forecastGraphics->url;
123  
124 0 my $http = HTTP::Tiny->new;
125  
126 0 my $response = $http->get($url);
127  
128 0 my $html = $response->{content};
129  
130 0 $html =~ m/storm_graphics\/(.+)\/refresh/;
131 0 my $prefix = $1;
132 0 ▶ return [] if not $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
133  
134 0 my $base = sprintf( qq{%s/%s}, $DEFAULT_GRAPHICS_ROOT, $prefix );
135 0 $response = $http->get($base);
136  
137 0 $html = $response->{content};
138 0 my $id = uc $self->id;
139 0 my @imgs = ( $html =~ m/href="($id.+\.png)"/g );
140 0 @imgs = map { qq{$base/$_} } @imgs;
141  
142 0 return \@imgs;
143 }
144  
145 # rolls up requesting url and extracting text inside of the <pre></pre>
146 # tag into one subroutine
147 sub _get_text {
148 0 ▶ my ( $self, $resource, $local_file ) = @_;
Subroutine _get_text: not called CC=6 SCAR=37.4
149  
150 # note, accessors like "->advNum" are generated in Weather::NHC::TropicalCyclone using Util::H2O::h2o
151 0 ▶ if ( not( $self->$resource->advNum or $self->$resource->issuance or $self->$resource->url ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->$resource->advNum or $self->$resource->issuance or $self->$resource->url) 0 0 0
Condition0%0/6
$self->$resource->advNum or $self->$resource->issuance or $self->$resource->url
l!l&&r!l&&!r
000
$self->$resource->advNum or $self->$resource->issuance
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->$resource->advNum or $self->$resource->issuance or $self->$resource->url
$self->$resource->advNum $self->$resource->issuance $self->$resource->url
Truth table0%0/4
A or B or C
A= $self->$resource->advNumB= $self->$resource->issuanceC= $self->$resource->url
ABCresult
1XX1
01X1
0011
0000
152 0 die qq{Resource must be one of: 'publicAdvisory', 'forecastAdvisory', or 'forecastDiscssion'\n};
153 }
154  
155 0 my $url = $self->$resource->url;
156  
157 0 my $http = HTTP::Tiny->new;
158  
159 0 my $response = $http->get($url);
160  
161 # extract actual advisory text from <pre></pre> and return just that text
162 0 my $htb = HTML::TreeBuilder->new;
163 0 $htb->parse( $response->{content} );
164  
165 0 my $pre = $htb->look_down( '_tag', 'pre' );
166  
167 0 ▶ if ($local_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_file) 0 0 0
168 0 ▶ open my $fh, q{>}, $local_file or die qq{Failed to open '$local_file' for writing: $!\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $local_file or die "Failed to open '${local_file}' for writing: $!\n" 0 0 0
169 0 print $fh $pre->as_text;
170 0 close $fh;
171 }
172  
173 0 return ( $pre->as_text, $self->$resource->advNum, $local_file );
174 }
175  
176 # optionally provide a local file name to save fetched file to
177 sub fetch_publicAdvisory {
178 0 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_publicAdvisory: not called CC=1 SCAR=6.9
Pod documented
179 0 return $self->_get_text( q{publicAdvisory}, $local_file );
180 }
181  
182 # optionally provide a local file name to save fetched file to
183 sub fetch_forecastAdvisory {
184 0 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_forecastAdvisory: not called CC=1 SCAR=6.9
Pod documented
185 0 return $self->_get_text( q{forecastAdvisory}, $local_file );
186 }
187  
188 # in this case, the $local_file is the file to which the ATCF data is saved,
189 # the forecast advisory is only handled as text; ATCF is returned as an arrary
190 # ref
191 sub fetch_forecastAdvisory_as_atcf {
192 0 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_forecastAdvisory_as_atcf: not called CC=2 SCAR=17.9
Pod documented
193 0 my ( $text, $advNum, $_ignore ) = $self->_get_text(q{forecastAdvisory});
194 0 require Weather::NHC::TropicalCyclone::ForecastAdvisory;
195  
196 0 my $fst_ref = Weather::NHC::TropicalCyclone::ForecastAdvisory->new( input_text => $text, output_file => $local_file );
197 0 my $atcf_text = $fst_ref->extract_atcf;
198  
199 # save file if $local_file is passed
200 0 ▶ if ($local_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_file) 0 0 0
201 0 $fst_ref->save_atcf;
202 }
203  
204 0 return ( $fst_ref->as_atcf, $advNum, $local_file );
205 }
206  
207 # optionally provide a local file name to save fetched file to
208 sub fetch_forecastDiscussion {
209 0 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_forecastDiscussion: not called CC=1 SCAR=6.9
Pod documented
210 0 return $self->_get_text( q{forecastDiscussion}, $local_file );
211 }
212  
213 # optionally provide a local file name to save fetched file to
214 sub fetch_windspeedProbabilities {
215 0 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_windspeedProbabilities: not called CC=1 SCAR=6.9
Pod documented
216 0 return $self->_get_text( q{windSpeedProbabilities}, $local_file );
217 }
218  
219 # optionally provide a local file name to save fetched file to
220 sub fetch_forecastTrack {
221 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_forecastTrack: called CC=1 SCAR=0.0
Pod documented
222 4 return $self->_get_file( q{forecastTrack}, $type, $local_file );
223 }
224  
225 # optionally provide a local file name to save fetched file to
226 sub fetch_windWatchesWarnings {
227 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_windWatchesWarnings: called CC=1 SCAR=0.0
Pod documented
228 4 return $self->_get_file( q{windWatchesWarnings}, $type, $local_file );
229 }
230  
231 # optionally provide a local file name to save fetched file to
232 sub fetch_trackCone {
233 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_trackCone: called CC=1 SCAR=0.0
Pod documented
234 4 return $self->_get_file( q{trackCone}, $type, $local_file );
235 }
236  
237 # optionally provide a local file name to save fetched file to
238 sub fetch_initialWindExtent {
239 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_initialWindExtent: called CC=1 SCAR=0.0
Pod documented
240 4 return $self->_get_file( q{initialWindExtent}, $type, $local_file );
241 }
242  
243 # optionally provide a local file name to save fetched file to
244 sub fetch_forecastWindRadiiGIS {
245 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_forecastWindRadiiGIS: called CC=1 SCAR=0.0
Pod documented
246 4 return $self->_get_file( q{forecastWindRadiiGIS}, $type, $local_file );
247 }
248  
249 # optionally provide a local file name to save fetched file to
250 sub fetch_bestTrackGIS {
251 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_bestTrackGIS: called CC=1 SCAR=0.0
Pod documented
252 4 return $self->_get_file( q{bestTrackGIS}, $type, $local_file );
253 }
254  
255 # optionally provide a local file name to save fetched file to
256 sub fetch_earliestArrivalTimeTSWindsGIS {
257 2 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_earliestArrivalTimeTSWindsGIS: called CC=1 SCAR=0.0
Pod documented
258 2 return $self->_get_file( q{earliestArrivalTimeTSWindsGIS}, $type, $local_file );
259 }
260  
261 # optionally provide a local file name to save fetched file to
262 sub fetch_mostLikelyTimeTSWindsGIS {
263 2 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_mostLikelyTimeTSWindsGIS: called CC=1 SCAR=0.0
Pod documented
264 2 return $self->_get_file( q{mostLikelyTimeTSWindsGIS}, $type, $local_file );
265 }
266  
267 # optionally provide a local file name to save fetched file to
268 sub fetch_windSpeedProbabilitiesGIS {
269 5 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_windSpeedProbabilitiesGIS: called CC=1 SCAR=0.0
Pod documented
270 5 return $self->_get_file( q{windSpeedProbabilitiesGIS}, $type, $local_file );
271 }
272  
273 # optionally provide a local file name to save fetched file to
274 sub fetch_stormSurgeWatchWarningGIS {
275 2 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_stormSurgeWatchWarningGIS: called CC=1 SCAR=0.0
Pod documented
276 2 return $self->_get_file( q{stormSurgeWatchWarningGIS}, $type, $local_file );
277 }
278  
279 # optionally provide a local file name to save fetched file to
280 sub fetch_potentialStormSurgeFloodingGIS {
281 4 ▶ my ( $self, $type, $local_file ) = @_;
Subroutine fetch_potentialStormSurgeFloodingGIS: called CC=1 SCAR=0.0
Pod documented
282 4 return $self->_get_file( q{potentialStormSurgeFloodingGIS}, $type, $local_file );
283 }
284  
285 # rolls up requesting file, based on url associated with file key ("url" key is not specified)
286 sub _get_file {
287 39 ▶ my ( $self, $resource, $urlKey, $local_file ) = @_;
Subroutine _get_file: called CC=9 SCAR=24.6
288  
289 39 my $types = $self->_fetch_data_types;
290  
291 # make sure $urlKey is provided by the resource (defined in $types hash ref above)
292 39 ▶ die qq{'$urlKey' is not a valid type provided by '$resource'.\n} if not grep { /$resource/ } ( @{ $types->{$urlKey} } );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/$resource/;} @{$$types{$urlKey};} 0 39 39
293  
294 # check to make sure $resource is not 'null'
295 39 ▶ return undef if ref $self->$resource ne q{HASH} and not $self->$resource->{$urlKey};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->$resource ne "HASH" and not $self->$resource->{$urlKey} 0 39 39
Condition33%1/3
ref $self->$resource ne "HASH" and not $self->$resource->{$urlKey}
!ll&&!rl&&r
0390
MC/DC0%0/2
ref $self->$resource ne "HASH" and not $self->$resource->{$urlKey}
ref $self->$resource ne "HASH" not $self->$resource->{$urlKey}
Truth table33%1/3
A and not(B)
A= ref $self->$resource ne "HASH"B= not $self->$resource->{$urlKey}
ABresult
0X0
100
111
296  
297 39 my $url = $self->$resource->{$urlKey};
298  
299 # extract file name from URL if no $local_file is specified
300 39 ▶ if ( not $local_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local_file) 39 0 39
301  
302 # extract file name from the end of the URL
303 39 $url =~ m/\/([a-zA-Z0-9_]+)\.([a-zA-Z]+)$/;
304 39 $local_file = qq{$1.$2};
305 }
306  
307 39 my $http = HTTP::Tiny->new;
308  
309 39 my $response = $http->mirror( $url, $local_file );
310  
311 39 ▶ if ( not $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 39 39
312 0 ▶ my $status = $response->{status} // q{Unknown};
Condition0%0/2
$response->{"status"} // "Unknown"
l!l
00
MC/DC0%0/1
$response->{"status"} // "Unknown"
$response->{"status"}
Truth table0%0/2
A
A= $response->{"status"}
Aresult
11
00
313 0 die qq{Download of $url failed. HTTP status: $status\n};
314 }
315  
316 # bestTrackGIS resource doesn't provide "advNum" per specification
317 # so it doesn't try to deref an method that may not exist
318  
319 # 'can($method)' is a limitation of what Util:H2O::h2o gives us, unfortunately
320 39 ▶ my $advNum = ( $self->$resource->can('advNum') ) ? $self->$resource->advNum // q{N/A} : q{N/A};
Branch: 1 branch
BranchTrueFalseTotal
$self->$resource->can('advNum') ? : 30 9 39
Condition50%1/2
$self->$resource->advNum // "N/A"
l!l
300
MC/DC0%0/1
$self->$resource->advNum // "N/A"
$self->$resource->advNum
Truth table50%1/2
A
A= $self->$resource->advNum
Aresult
11
00
321 39 return ( $local_file, $advNum );
322 }
323  
324 # auxillary methods to fetch the best track ".dat" file via NHC's FTP over HTTPS
325  
326 sub fetch_best_track {
327 4 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_best_track: called CC=4 SCAR=15.5
Pod documented
328  
329 4 my $btk_file = sprintf( "b%s.dat", $self->id );
330 4 my $url = sprintf( "%s/%s", $DEFAULT_BTK_ROOT, $btk_file );
331  
332 4 ▶ $local_file //= $btk_file;
Condition66%2/3
$local_file //= $btk_file
l!l&&r!l&&!r
220
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
333  
334 4 my $http = HTTP::Tiny->new;
335  
336 4 my $response = $http->mirror( $url, $local_file );
337  
338 4 ▶ if ( not $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 4 4
339 0 ▶ my $status = $response->{status} // q{Unknown};
Condition0%0/2
$response->{"status"} // "Unknown"
l!l
00
MC/DC0%0/1
$response->{"status"} // "Unknown"
$response->{"status"}
Truth table0%0/2
A
A= $response->{"status"}
Aresult
11
00
340 0 die qq{Download of $url failed. HTTP status: $status\n};
341 }
342  
343 # bestTrackGIS resource doesn't provide "advNum" per specification
344 4 return $local_file;
345 }
346  
347 2 1;
348  
349 __END__
« TropicalCyclone/ForecastAdvisory.pm ↑ Summary TropicalCyclone.pm »