| 1 | package Weather::NHC::TropicalCyclone::Validation; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | our @REQUIRED_STORM_FIELDS = qw/id binNumber name classification/; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | our %CLASSIFICATIONS = ( | |||||||||||||||||||||||||||||||||||||||||
| 10 | TD => q{Tropical Depression}, | ||||||||||||||||||||||||||||||||||||||||||
| 11 | STD => q{Subtropical Depression}, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | TS => q{Tropical Storm}, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | HU => q{Hurricane}, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | STS => q{Subtropical Storm}, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | PTC => q{Post-tropical Cyclone / Remnants}, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | TY => q{Typhoon}, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | PC => q{Potential Tropical Cyclone}, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | our %KNOWN_STORM_FIELDS = map { $_ => 1 } qw/ | |||||||||||||||||||||||||||||||||||||||||
| 21 | id binNumber name classification intensity pressure latitude longitude | ||||||||||||||||||||||||||||||||||||||||||
| 22 | latitudeNumeric longitudeNumeric movementDir movementSpeed lastUpdate | ||||||||||||||||||||||||||||||||||||||||||
| 23 | publicAdvisory forecastAdvisory windSpeedProbabilities forecastDiscussion | ||||||||||||||||||||||||||||||||||||||||||
| 24 | forecastGraphics forecastTrack windWatchesWarnings trackCone initialWindExtent | ||||||||||||||||||||||||||||||||||||||||||
| 25 | forecastWindRadiiGIS bestTrackGIS earliestArrivalTimeTSWindsGIS | ||||||||||||||||||||||||||||||||||||||||||
| 26 | mostLikelyTimeTSWindsGIS windSpeedProbabilitiesGIS stormSurgeWatchWarningGIS | ||||||||||||||||||||||||||||||||||||||||||
| 27 | potentialStormSurgeFloodingGIS peakSurgeKML | ||||||||||||||||||||||||||||||||||||||||||
| 28 | /; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | sub classifications { return { %CLASSIFICATIONS } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
classifications: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub known_storm_fields { return [ sort keys %KNOWN_STORM_FIELDS ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
known_storm_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 28 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 28 | my ( $class, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 28 | my ( @errors, @warnings ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 28 | ▶ | if ( ref $data ne q{HASH} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | push @errors, q{NHC JSON root must be an object}; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | return { errors => \@errors, warnings => \@warnings }; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 27 | ▶ | if ( ref $data->{activeStorms} ne q{ARRAY} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | push @errors, q{NHC JSON root must contain an activeStorms array}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | return { errors => \@errors, warnings => \@warnings }; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 25 | for my $index ( 0 .. $#{ $data->{activeStorms} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 48 | 31 | my $storm = $data->{activeStorms}->[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 31 | ▶ | if ( ref $storm ne q{HASH} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | push @errors, "activeStorms[$index] must be an object"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 30 | for my $field (@REQUIRED_STORM_FIELDS) { | |||||||||||||||||||||||||||||||||||||||||
| 55 | push @errors, "activeStorms[$index] is missing required field '$field'" | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 120 | ▶ | if !exists $storm->{$field} || !defined $storm->{$field} || $storm->{$field} eq q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not exists $storm->{$field} && defined $storm->{$field} or $storm->{$field} eq ""
exists $storm->{$field} && defined $storm->{$field}
MC/DC33%1/3
not exists $storm->{$field} && defined $storm->{$field} or $storm->{$field} eq ""
exists $storm->{$field}
defined $storm->{$field}
$storm->{$field} eq ""
Truth table50%2/4
not(A && B) or C
A= exists $storm->{$field}B= defined $storm->{$field}C= $storm->{$field} eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 30 | ▶ | if ( defined $storm->{classification} && !exists $CLASSIFICATIONS{ $storm->{classification} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $storm->{"classification"} and not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
MC/DC100%2/2
defined $storm->{"classification"} and not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
defined $storm->{"classification"}
not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
Truth table100%3/3
A and not(B)
A= defined $storm->{"classification"}B= not exists $Weather::NHC::TropicalCyclone::Validation::CLASSIFICATIONS{$storm->{"classification"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | push @errors, "activeStorms[$index] has unknown classification '$storm->{classification}'"; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 30 | ▶ | if ( defined $storm->{binNumber} && $storm->{binNumber} !~ /^(?:AT|EP|CP)[1-5]$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $storm->{"binNumber"} and not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
MC/DC100%2/2
defined $storm->{"binNumber"} and not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
defined $storm->{"binNumber"}
not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
Truth table100%3/3
A and not(B)
A= defined $storm->{"binNumber"}B= not $storm->{"binNumber"} =~ /^(?:AT|EP|CP)[1-5]$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | push @errors, "activeStorms[$index] has unexpected binNumber '$storm->{binNumber}'"; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 30 | ▶ | if ( defined $storm->{id} && $storm->{id} !~ /^(?:al|ep|cp)\d{2}\d{4}$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $storm->{"id"} and not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
MC/DC100%2/2
defined $storm->{"id"} and not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
defined $storm->{"id"}
not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
Truth table100%3/3
A and not(B)
A= defined $storm->{"id"}B= not $storm->{"id"} =~ /^(?:al|ep|cp)\d{2}\d{4}$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | push @errors, "activeStorms[$index] has unexpected storm id '$storm->{id}'"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 30 | for my $field (qw/latitudeNumeric longitudeNumeric movementDir movementSpeed/) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 120 | ▶ | next if !exists $storm->{$field} || !defined $storm->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $storm->{$field} and defined $storm->{$field}
MC/DC0%0/2
exists $storm->{$field} and defined $storm->{$field}
exists $storm->{$field}
defined $storm->{$field}
Truth table33%1/3
A and B
A= exists $storm->{$field}B= defined $storm->{$field}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | push @errors, "activeStorms[$index].$field must be numeric" | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 120 | ▶ | if !looks_like_number( $storm->{$field} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 30 | for my $field ( sort keys %$storm ) { | |||||||||||||||||||||||||||||||||||||||||
| 78 | 634 | ▶ | next if $KNOWN_STORM_FIELDS{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | push @warnings, "activeStorms[$index] contains unknown field '$field'; preserved without interpretation"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 25 | return { errors => \@errors, warnings => \@warnings }; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||||||||||||