TropicalCyclone/Storm.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ 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 Validate::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use HTML::TreeBuilder ();
10  
11 our $DEFAULT_GRAPHICS_ROOT = q{https://www.nhc.noaa.gov/storm_graphics/};
12  
13 use Object::Tiny
14 qw/id binNumber name classification intensity pressure latitude longitude latitude_numberic movementDir movementSpeed lastUpdate publicAdvisory forecastAdvisory windSpeedProbabilities forecastDiscussion forecastGraphics forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS mostLikelyTimeTSWindsGIS windSpeedProbabilitiesGIS kmzFile34kt kmzFile50kt kmzFile64kt stormSurgeWatchWarningGIS potentialStormSurgeFloodingGIS/;
15  
16 our $CLASSIFICATIONS = {
17 TD => q{Tropical Depression},
18 STD => q{Subtropical Depression},
19 TS => q{Tropical Storm},
20 HU => q{Hurricane},
21 STS => q{Subtropical Storm},
22 PTC => q{Post-tropical Cyclone / Remnants},
23 TY => q{Typhoon (we don't use this currently)},
24 PC => q{Potential Tropical Cyclone},
25 };
26  
27 my $validation_rules = {
28 fields => [qw/id binNumber name classification/],
29 checks => [
30 [qw/id binNumber name classification/] => Validate::Tiny::is_required(),
31 classification => sub {
32 my ( $value, $params ) = @_;
33  
34 # branch, if true indicates failed validation
35 if ( not grep { /$value/ } ( keys %$CLASSIFICATIONS ) ) {
36 return q{Invalid classification, not defined in NHC specification.};
37 }
38  
39 # indicates successful validation
40 return undef;
41 },
42 ],
43 };
44  
45 sub new {
46 my ( $pkg, $self ) = @_;
47  
48 my $v = Validate::Tiny->new;
49 my $validation = $v->check( $self, $validation_rules );
50 if ( not $v->success ) {
51 die qq{Field validation errors found creating package instance for: } . join( q{, }, keys %{ $validation->error } ) . qq{\n};
52 }
53  
54 bless $self, $pkg;
55 return $self;
56 }
57  
58 # get storm classification "real classification"
59 sub classification_full {
60 my $self = shift;
61 die qq{'classification' field not set\n} if not $self->classification;
62 die qq{Unknown storm classification\n} if not $CLASSIFICATIONS->{ $self->classification };
63 return $CLASSIFICATIONS->{ $self->classification };
64 }
65  
66 # determine basin based on binNumber
67 sub basin {
68 my $self = shift;
69 die qq{'binNumber' field not set\n} if not $self->binNumber;
70  
71 # allow for easy querying of "basin"
72 my $BASINS = {
73 atlantic => qr/^AT[1-5]$/i,
74 pacific => qr/^EP[1-5]$/i,
75 };
76 for my $basin ( keys %$BASINS ) {
77 return $basin if $self->binNumber =~ $BASINS->{$basin};
78 }
79 return undef;
80 }
81  
82 # attempts to get base graphics directory, then scrapes
83 # the index page for the files and returns an array reference
84 # of all image addresses for this storm
85 sub fetch_forecastGraphics {
86 my $self = shift;
87  
88 my $url = $self->forecastGraphics->{url};
89  
90 my $http = HTTP::Tiny->new;
91  
92 my $response = $http->get($url);
93  
94 my $html = $response->{content};
95  
96 $html =~ m/storm_graphics\/(.+)\/refresh/;
97 my $prefix = $1;
98  
99 my $base = sprintf( qq{%s%s}, $DEFAULT_GRAPHICS_ROOT, $prefix );
100 $response = $http->get($base);
101  
102 $html = $response->{content};
103 my $id = uc $self->id;
104 my @imgs = ( $html =~ m/href="($id.+\.png)"/g );
105 @imgs = map { qq{$base/$_} } @imgs;
106  
107 return \@imgs;
108 }
109  
110 sub _fetch_text_types {
111 my $self = shift;
112  
113 # white list of resources and URL attributes they provide
114 my $types = {
115 text => [qw/publicAdvisory forecastAdvisory forecastDiscussion windSpeedProbabilities/],
116 };
117  
118 return $types;
119 }
120  
121 # rolls up requesting url and extracting text inside of the <pre></pre>
122 # tag into one subroutine
123 sub _get_text {
124 my ( $self, $resource, $local_file ) = @_;
125  
126 if ( not( $self->$resource->{advNum} or $self->$resource->{issuance} or $self->$resource->{url} ) ) {
127 die qq{Resource must be one of: 'publicAdvisory', 'forecastAdvisory', or 'forecastDiscssion'\n};
128 }
129  
130 my $url = $self->$resource->{url};
131  
132 my $http = HTTP::Tiny->new;
133  
134 my $response = $http->get($url);
135  
136 # extract actual advisory text from <pre></pre> and return just that text
137 my $htb = HTML::TreeBuilder->new;
138 $htb->parse( $response->{content} );
139  
140 my $pre = $htb->look_down( '_tag', 'pre' );
141  
142 if ($local_file) {
143 open my $fh, q{>}, $local_file or die qq{Failed to open '$local_file' for writing: $!\n};
144 print $fh $pre->as_text;
145 close $fh;
146 }
147  
148 return ( $pre->as_text, $self->$resource->{advNum}, $local_file );
149 }
150  
151 # optionally provide a local file name to save fetched file to
152 sub fetch_publicAdvisory {
153 my ( $self, $local_file ) = @_;
154 return $self->_get_text( q{publicAdvisory}, $local_file );
155 }
156  
157 # optionally provide a local file name to save fetched file to
158 sub fetch_forecastAdvisory {
159 my ( $self, $local_file ) = @_;
160 return $self->_get_text( q{forecastAdvisory}, $local_file );
161 }
162  
163 # optionally provide a local file name to save fetched file to
164 sub fetch_forecastDiscussion {
165 my ( $self, $local_file ) = @_;
166 return $self->_get_text( q{forecastDiscussion}, $local_file );
167 }
168  
169 # optionally provide a local file name to save fetched file to
170 sub fetch_windspeedProbabilities {
171 my ( $self, $local_file ) = @_;
172 return $self->_get_text( q{windSpeedProbabilities}, $local_file );
173 }
174  
175 # optionally provide a local file name to save fetched file to
176 sub fetch_forecastTrack {
177 my ( $self, $type, $local_file ) = @_;
178 return $self->_get_file( q{forecastTrack}, $type, $local_file );
179 }
180  
181 # optionally provide a local file name to save fetched file to
182 sub fetch_windWatchesWarnings {
183 my ( $self, $type, $local_file ) = @_;
184 return $self->_get_file( q{windWatchesWarnings}, $type, $local_file );
185 }
186  
187 # optionally provide a local file name to save fetched file to
188 sub fetch_trackCone {
189 my ( $self, $type, $local_file ) = @_;
190 return $self->_get_file( q{trackCone}, $type, $local_file );
191 }
192  
193 # optionally provide a local file name to save fetched file to
194 sub fetch_initialWindExtent {
195 my ( $self, $type, $local_file ) = @_;
196 return $self->_get_file( q{initialWindExtent}, $type, $local_file );
197 }
198  
199 # optionally provide a local file name to save fetched file to
200 sub fetch_forecastWindRadiiGIS {
201 my ( $self, $type, $local_file ) = @_;
202 return $self->_get_file( q{forecastWindRadiiGIS}, $type, $local_file );
203 }
204  
205 # optionally provide a local file name to save fetched file to
206 sub fetch_bestTrackGIS {
207 my ( $self, $type, $local_file ) = @_;
208 return $self->_get_file( q{bestTrackGIS}, $type, $local_file );
209 }
210  
211 # optionally provide a local file name to save fetched file to
212 sub fetch_earliestArrivalTimeTSWindsGIS {
213 my ( $self, $type, $local_file ) = @_;
214 return $self->_get_file( q{earliestArrivalTimeTSWindsGIS}, $type, $local_file );
215 }
216  
217 # optionally provide a local file name to save fetched file to
218 sub fetch_mostLikelyTimeTSWindsGIS {
219 my ( $self, $type, $local_file ) = @_;
220 return $self->_get_file( q{mostLikelyTimeTSWindsGIS}, $type, $local_file );
221 }
222  
223 # optionally provide a local file name to save fetched file to
224 sub fetch_windSpeedProbabilitiesGIS {
225 my ( $self, $type, $local_file ) = @_;
226 return $self->_get_file( q{windSpeedProbabilitiesGIS}, $type, $local_file );
227 }
228  
229 # optionally provide a local file name to save fetched file to
230 sub fetch_stormSurgeWatchWarningGIS {
231 my ( $self, $type, $local_file ) = @_;
232 return $self->_get_file( q{stormSurgeWatchWarningGIS}, $type, $local_file );
233 }
234  
235 # optionally provide a local file name to save fetched file to
236 sub fetch_potentialStormSurgeFloodingGIS {
237 my ( $self, $type, $local_file ) = @_;
238 return $self->_get_file( q{potentialStormSurgeFloodingGIS}, $type, $local_file );
239 }
240  
241 sub _fetch_data_types {
242 my $self = shift;
243  
244 # white list of resources and URL attributes they provide
245 my $types = {
246 zipFile => [qw/forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS potentialStormSurgeFloodingGIS/],
247 kmzFile => [qw/forecastTrack windWatchesWarnings trackCone initialWindExtent forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS mostLikelyTimeTSWindsGIS/],
248 zipFile5km => [qw/windSpeedProbabilitiesGIS/],
249 zipFile0p5deg => [qw/windSpeedProbabilitiesGIS/],
250 kmzFiles34kt => [qw/windSpeedProbabilitiesGIS/],
251 kmzFiles50kt => [qw/windSpeedProbabilitiesGIS/],
252 kmzFiles64kt => [qw/windSpeedProbabilitiesGIS/],
253 kmlFile => [qw/stormSurgeWatchWarningGIS/],
254 zipFileTidalMask => [qw/potentialStormSurgeFloodingGIS/],
255 };
256  
257 return $types;
258 }
259  
260 # rolls up requesting file, based on url associated with file key ("url" key is not specified)
261 sub _get_file {
262 my ( $self, $resource, $urlKey, $local_file ) = @_;
263  
264 my $types = $self->_fetch_data_types;
265  
266 # make sure $urlKey is provided by the resource (defined in $types hash ref above)
267 die qq{'$urlKey' is not a valid type provided by '$resource'.\n} if not grep { /$resource/ } ( @{ $types->{$urlKey} } );
268  
269 # check to make sure $resource is not 'null'
270 return undef if ref $self->$resource ne q{HASH} or not $self->$resource->{$urlKey};
271  
272 my $url = $self->$resource->{$urlKey};
273  
274 # extract file name from URL if no $local_file is specified
275 if ( not $local_file ) {
276  
277 # extract file name from the end of the URL
278 $url =~ m/\/([a-zA-Z0-9_]+)\.([a-zA-Z]+)$/;
279 $local_file = qq{$1.$2};
280 }
281  
282 my $http = HTTP::Tiny->new;
283  
284 my $response = $http->mirror( $url, $local_file );
285  
286 if ( not $response->{success} ) {
287 my $status = $response->{status} // q{Unknown};
288 die qq{Download of $url failed. HTTP status: $status\n};
289 }
290  
291 # bestTrackGIS resource doesn't provide "advNum" per specification
292 return ( $local_file, $self->$resource->{advNum} // q{N/A} );
293 }
294  
295 1;
296  
297 __END__
↑ Summary TropicalCyclone.pm »