TropicalCyclone/ForecastAdvisory.pm

filter: Statementstmt 100% 268 / 268 Branchbran 76.1% 67 / 88 Conditioncond 63.3% 19 / 30 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 90.0% 372 / 413 CC 56 SCAR 34.9
CC
56
Cov
92%
extract_atcf
45.8
new
6.0
_parseIotachs
5.0
CRAP 32.8
↑ Summary TropicalCyclone/Validation.pm »
1 package Weather::NHC::TropicalCyclone::ForecastAdvisory;
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 Time::Local qw/timegm/;
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  
8 14 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
9 14 my ( $pkg, %self ) = @_;
10  
11 # input_file and input_text are mutually exclusive via the contructor
12 14 ▶ if ( not( exists $self{input_file} or exists $self{input_text} ) or ( exists $self{input_file} and exists $self{input_text} ) or not exists $self{output_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self{"input_file"} || exists $self{"input_text"} or exists $self{"input_file"} and exists $self{"input_text"} or not exists $self{"output_file"}) 4 10 14
Condition100%12/12
not exists $self{"input_file"} || exists $self{"input_text"} or exists $self{"input_file"} and exists $self{"input_text"} or not exists $self{"output_file"}
l!l&&r!l&&!r
3110
not exists $self{"input_file"} || exists $self{"input_text"} or exists $self{"input_file"} and exists $self{"input_text"}
l!l&&r!l&&!r
2111
exists $self{'input_file'} || exists $self{'input_text'}
l!l&&r!l&&!r
392
exists $self{"input_file"} and exists $self{"input_text"}
!ll&&!rl&&r
921
MC/DC80%4/5
not exists $self{"input_file"} || exists $self{"input_text"} or exists $self{"input_file"} and exists $self{"input_text"} or not exists $self{"output_file"}
exists $self{'input_file'} exists $self{'input_text'} exists $self{"input_file"} exists $self{"input_text"} not exists $self{"output_file"}
Truth table45%5/11
not(A || B) or C and D or not(E)
A= exists $self{'input_file'}B= exists $self{'input_text'}C= exists $self{"input_file"}D= exists $self{"input_text"}E= not exists $self{"output_file"}
ABCDEresult
00XXX1
1X11X1
0111X1
1X0X11
1X1011
010X11
011011
1X0X00
1X1000
010X00
011000
13 4 die qq{Constructor requires specifying the 'input_file' xor 'input_text', and 'output_file' parameters.\n};
14 }
15 10 my @fields = (qw/input_file input_text output_file as_atcf/);
16 10 my $self = baptise -recurse, \%self, $pkg, @fields;
17 10 return $self;
18 }
19  
20 1 ▶sub extract_and_save_atcf {
Subroutine extract_and_save_atcf: called CC=1 SCAR=0.0
Pod documented
21 1 my $self = shift;
22 1 $self->extract_atcf;
23 1 return $self->save_atcf;
24 }
25  
26 4 ▶sub save_atcf {
Subroutine save_atcf: called CC=4 SCAR=14.0
Pod documented
27 4 my $self = shift;
28 4 my $output_file = $self->output_file;
29 4 ▶ die qq{No output_file was provided for saving ATCF data.\n}
Branch: 1 branch
BranchTrueFalseTotal
if not defined $output_file or $output_file eq "" 1 3 4
Condition66%2/3
not defined $output_file or $output_file eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $output_file or $output_file eq ""
not defined $output_file $output_file eq ""
Truth table66%2/3
not(A) or B
A= not defined $output_fileB= $output_file eq ""
ABresult
1X1
011
000
30 if !defined $output_file || $output_file eq q{};
31 3 ▶ open( my $fh, q{>}, $output_file ) || die qq{Failed to open output ATCF file $output_file : $!.\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $output_file or die "Failed to open output ATCF file $output_file : $!.\n" 3 0 3
32 3 my $output_ref = $self->as_atcf;
33 3 print $fh join( qq{\n}, @$output_ref );
34 3 close $fh;
35 3 return $self->output_file;
36 }
37  
38 11 ▶sub extract_atcf {
Subroutine extract_atcf: called CC=44 SCAR=38.3
Pod documented
39 11 my $self = shift;
40  
41 11 my @lines = ();
42  
43 ADVISORY_SOURCE:
44 11 ▶ if ( $self->input_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->input_file) { } 2 9 11
elsif ($self->input_text) { } 9 0 9
45 2 ▶ open( my $INPUT, q{<}, $self->input_file ) || die q{Failed to open forecast advisory file} . $self->input_file . qq{ for conversion to ATCF format: $!.\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $INPUT, "<", $self->input_file or die 'Failed to open forecast advisory file' . $self->input_file . " for conversion to ATCF format: $!.\n" 2 0 2
46 2 @lines = (<$INPUT>);
47 2 close $INPUT;
48 }
49 elsif ( $self->input_text ) {
50 9 @lines = split /\n/, $self->input_text;
51 }
52  
53 11 my @output = (); # accumulate content for $output, do write at the very end
54  
55 # parse rss reports from Nat'l Hurricane Center Atlantic Marine Forecast
56 # Advisory. NHC Mail (Atlantic Marine)
57 # <mail-storm-atlan-marine@seahorse.nhc.noaa.gov> See:
58 # http://www.nhc.noaa.gov/signup.shtml and select the following list: Atlantic
59 # Marine (Forecast/Advisories and updates ONLY)
60  
61 # BASIN,CY,YYYYMMDDHH,TECHNUM,TECH,TAU,LatN/S,LonE/W,VMAX,MSLP,TY,RAD,WINDCODE,RAD1,RAD2,RAD3,RAD4,RADP,RRP,MRD,GUSTS,EYE,SUBREGION,MAXSEAS,INITIALS,DIR,SPEED,STORMNAME,DEPTH,SEAS,SEASCODE,SEAS1,SEAS2,SEAS3,SEAS4
62 #
63 # 1 1 1 1 1 1 1 1 1 1
64 # 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
65 #01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
66 #AL, 01, 2009052912, 03, OFCL, 0, 393N, 649W, 30, 1006, TD, 34, NEQ, 0, 0, 0, 0, 0, 0, 40, 40, 0, , 0, TBK, 65, 17, , , 12, NEQ, 60, 60, 0, 0
67 11 my $template = "_BASIN_, 01, 2009010100, , OFCL, 0, 000N, 000W, 30, 0, , 34, NEQ, 0, 0, 0, 0, 0, 0, 0, 40, 0, , 0, TBK, 65, 17, , , 12, NEQ, 60, 60, 0, 0";
68 11 my %month_lookup = ( dummy => '00', JAN => '01', FEB => '02', MAR => '03', APR => '04', MAY => '05', JUN => '06', JUL => '07', AUG => '08', SEP => '09', OCT => '10', NOV => '11', DEC => '12' );
69  
70 # These are the data we are extracting
71 11 my $pressure;
72 11 my $storm_class = "";
73 11 my $storm_name;
74 my $storm_basin;
75 11 my $storm_number;
76 11 my $adv_num;
77 11 my $storm_year;
78 11 my $nowcast_year;
79 11 my $forecast_year;
80 11 my $nowcast_month;
81 11 my $forecast_month;
82 11 my $nowcast_day;
83 11 my $forecast_day;
84 11 my $nowcast_hour;
85 11 my $forecast_hour;
86 11 my $date_time;
87 11 my $forecast_date_time;
88 11 my $nowcast_date_time;
89 11 my $nowcast_max_wind;
90 11 my $forecast_max_wind;
91 11 my $atcf_line = $template;
92 11 my $lat;
93 my $lon;
94 11 my $vmax;
95 11 my $gusts; # in kt
96 11 my $center_direction = 65;
97 11 my $center_speed = 17;
98  
99 11 my $body_ref = \@lines;
100 11 my $cnt = @{$body_ref};
101 #
102 11 my @match = ();
103  
104 # Get the NHC Number
105 # NWS TPC/NATIONAL HURRICANE CENTER MIAMI FL AL172005
106 11 @match = grep /(?:AL|EP|CP|WP|IO|SH|LS)\d{2}\d{4}$/, @{$body_ref};
107 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
108 11 ▶ if ( $match[0] =~ /(AL|EP|CP|WP|IO|SH|LS)(\d{2})(\d{4})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /(AL|EP|CP|WP|IO|SH|LS)(\d{2})(\d{4})$/) 11 0 11
109 11 $storm_basin = $1;
110 11 $storm_number = $2;
111 11 $storm_year = $3;
112 }
113 11 ▶ die qq{NO NHC NUMBER/YEAR\n} if not $storm_number or not $storm_year or not $storm_basin;
Branch: 1 branch
BranchTrueFalseTotal
unless $storm_number and $storm_year and $storm_basin 0 11 11
Condition33%2/6
$storm_number and $storm_year and $storm_basin
!ll&&!rl&&r
0011
$storm_number and $storm_year
!ll&&!rl&&r
0011
MC/DC0%0/3
$storm_number and $storm_year and $storm_basin
$storm_number $storm_year $storm_basin
Truth table25%1/4
A and B and C
A= $storm_numberB= $storm_yearC= $storm_basin
ABCresult
0XX0
10X0
1100
1111
114 }
115 11 $atcf_line =~ s/_BASIN_/$storm_basin/;
116 11 my $storm_number_str = sprintf( "%02d", $storm_number );
117 11 substr( $atcf_line, 4, 2 ) = $storm_number_str;
118  
119 # Date format
120 # 1500Z THU SEP 02 2004
121 # July 18th TD 2. HAS CHANGED
122 # NOTE NOTE NOW! 1500 UTC TUE JUL 18 2006
123 11 ▶ if ( $storm_year > 2005 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storm_year > 2005) { } 10 1 11
124 10 @match = grep /^\d{4} .+ \d{4}$/, @{$body_ref};
125 }
126 else {
127 1 @match = grep /^\d{4}Z .+ \d{4}$/, @{$body_ref};
128 }
129 #
130 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
131 11 $date_time = $match[0];
132 11 chomp $date_time;
133 11 my @vals = split( ' ', $date_time );
134 11 $nowcast_hour = substr( $vals[0], 0, 2 );
135 11 ▶ if ( $storm_year > 2005 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storm_year > 2005) { } 10 1 11
136 10 $nowcast_year = $vals[5];
137 10 $nowcast_month = $month_lookup{ $vals[3] };
138 10 $nowcast_day = $vals[4];
139 }
140 else {
141 1 $nowcast_year = $vals[4];
142 1 $nowcast_month = $month_lookup{ $vals[2] };
143 1 $nowcast_day = $vals[3];
144 }
145 11 $nowcast_date_time = $nowcast_year . $nowcast_month . $nowcast_day . $nowcast_hour;
146 11 substr( $atcf_line, 8, 10 ) = sprintf( "%10d", $nowcast_date_time );
147 }
148  
149 # advisory number does not appear in the ATCF format
150 11 @match = grep /FORECAST.+ADVISORY NUMBER/, @{$body_ref};
151  
152 # HURRICANE FRANCES ADVISORY NUMBER 40
153 # HURRICANE FRANCES FORECAST/ADVISORY NUMBER 37
154 # HURRICANE FRANCES FORECAST/ADVISORY NUMBER 37...CORRECTED
155 # HURRICANE FRANCES SPECIAL FORECAST/ADVISORY NUMBER 37
156 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) { } 10 1 11
157 10 ▶ if ( $match[0] =~ /^(.+)\s+FORECAST.+ADVISORY NUMBER\s+(\d{1,3})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /^(.+)\s+FORECAST.+ADVISORY NUMBER\s+(\d{1,3})/) 10 0 10
158 10 $storm_name = $1;
159 10 $adv_num = $2;
160 10 $storm_name =~ s/SPECIAL//;
161 }
162 }
163 else {
164 1 @match = grep /ADVISORY NUMBER/, @{$body_ref};
165 1 ▶ if ( $match[0] =~ /^(.+)\s+ADVISORY NUMBER\s+(\d{1,3})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /^(.+)\s+ADVISORY NUMBER\s+(\d{1,3})/) 1 0 1
166 1 $storm_name = $1;
167 1 $adv_num = $2;
168 }
169 }
170 11 my @tmp = split( ' ', $storm_name );
171 11 ▶ if ( $tmp[0] eq 'HURRICANE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tmp[0] eq 'HURRICANE') { } 8 3 11
elsif ($tmp[0] eq 'POTENTIAL') { } 1 2 3
elsif ($tmp[0] eq 'TROPICAL' or $tmp[0] eq 'SUBTROPICAL' or $tmp[0] eq 'REMNANTS' or $tmp[0] eq 'POST-TROPICAL') { } 2 0 2
172 8 $storm_class = $tmp[0];
173 8 $storm_name = $tmp[1];
174 }
175 elsif ( $tmp[0] eq 'POTENTIAL' ) {
176 1 $storm_class = "$tmp[0] $tmp[1] $tmp[2]";
177 1 $storm_name = $tmp[3];
178 }
179 ▶ elsif ( $tmp[0] eq 'TROPICAL' or $tmp[0] eq 'SUBTROPICAL' or $tmp[0] eq 'REMNANTS' or $tmp[0] eq 'POST-TROPICAL' ) {
Condition33%3/9
$tmp[0] eq "TROPICAL" or $tmp[0] eq "SUBTROPICAL" or $tmp[0] eq "REMNANTS" or $tmp[0] eq "POST-TROPICAL"
l!l&&r!l&&!r
200
$tmp[0] eq "TROPICAL" or $tmp[0] eq "SUBTROPICAL" or $tmp[0] eq "REMNANTS"
l!l&&r!l&&!r
200
$tmp[0] eq "TROPICAL" or $tmp[0] eq "SUBTROPICAL"
l!l&&r!l&&!r
200
MC/DC0%0/4
$tmp[0] eq "TROPICAL" or $tmp[0] eq "SUBTROPICAL" or $tmp[0] eq "REMNANTS" or $tmp[0] eq "POST-TROPICAL"
$tmp[0] eq "TROPICAL" $tmp[0] eq "SUBTROPICAL" $tmp[0] eq "REMNANTS" $tmp[0] eq "POST-TROPICAL"
Truth table20%1/5
A or B or C or D
A= $tmp[0] eq "TROPICAL"B= $tmp[0] eq "SUBTROPICAL"C= $tmp[0] eq "REMNANTS"D= $tmp[0] eq "POST-TROPICAL"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
180  
181 # SUBTROPICAL is rare. see 2007 01
182 2 $storm_class = "$tmp[0] $tmp[1]";
183 2 $storm_name = $tmp[2];
184 }
185 11 substr( $atcf_line, 148, 10 ) = sprintf( "%10s", $storm_name );
186 11 my $adv_num_str = sprintf( "%02d", $adv_num );
187 11 my $adv_num_url_str = sprintf( "%03d", $adv_num );
188  
189 # HURRICANE CENTER LOCATED NEAR 23.4N 73.9W AT 02/1500Z
190 # or
191 #TROPICAL DEPRESSION DISSIPATING NEAR 29.0N 70.0W AT 24/2100Z
192 # or
193 # TROPICAL DEPRESSION CENTER LOCATED NEAR 11.2N 36.0W AT 25/0300Z
194 11 @match = grep /(CENTER LOCATED|DISSIPATING) NEAR/, @{$body_ref};
195 11 my $ns_hem = "N";
196 11 my $ew_hem = "W";
197 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
198 11 ▶ if ( $match[0] =~ /CENTER LOCATED NEAR\s+(\d{1,3}\.\d{1,2})([N|S])\s+(\d{1,3}\.\d{1,2})([E|W])\s+AT/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /CENTER LOCATED NEAR\s+(\d{1,3}\.\d{1,2})([N|S])\s+(\d{1,3}\.\d{1,2})([E|W])\s+AT/) 10 1 11
199 10 $lat = $1;
200 10 $ns_hem = $2;
201 10 $lon = $3;
202 10 $ew_hem = $4;
203 }
204 11 ▶ if ( $match[0] =~ /DISSIPATING NEAR\s+(\d{1,3}\.\d{1,2})([N|S])\s+(\d{1,3}\.\d{1,2})([E|W])\s+AT/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /DISSIPATING NEAR\s+(\d{1,3}\.\d{1,2})([N|S])\s+(\d{1,3}\.\d{1,2})([E|W])\s+AT/) 1 10 11
205 1 $lat = $1;
206 1 $ns_hem = $2;
207 1 $lon = $3;
208 1 $ew_hem = $4;
209 }
210 }
211  
212 11 my $nowcast_lat = sprintf( "%4d$ns_hem", $lat * 10 );
213 11 my $nowcast_lon = sprintf( "%4d$ew_hem", $lon * 10 );
214 11 substr( $atcf_line, 34, 5 ) = sprintf( "%5s", $nowcast_lat );
215 11 substr( $atcf_line, 41, 5 ) = sprintf( "%5s", $nowcast_lon );
216  
217 #PRESENT MOVEMENT TOWARD THE NORTH-NORTHWEST OR 330 DEGREES AT 9 KT
218 11 @match = grep /^PRESENT MOVEMENT TOWARD THE/, @{$body_ref};
219 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
220 11 ▶ if ( $match[0] =~ /PRESENT MOVEMENT TOWARD THE.+OR\s+(\d{1,3})\s+DEGREES AT\s+(\d{1,2})\s+KT/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /PRESENT MOVEMENT TOWARD THE.+OR\s+(\d{1,3})\s+DEGREES AT\s+(\d{1,2})\s+KT/) 11 0 11
221 11 $center_direction = $1;
222 11 $center_speed = $2;
223 }
224 11 substr( $atcf_line, 138, 4 ) = sprintf( "%4d", $center_direction );
225 11 substr( $atcf_line, 143, 4 ) = sprintf( "%4d", $center_speed );
226 }
227  
228 11 @match = grep /^ESTIMATED MINIMUM CENTRAL PRESSURE/, @{$body_ref};
229 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
230 11 ▶ if ( $match[0] =~ /^ESTIMATED MINIMUM CENTRAL PRESSURE\s+(.+)\s+MB/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /^ESTIMATED MINIMUM CENTRAL PRESSURE\s+(.+)\s+MB/) 11 0 11
231 11 $pressure = $1;
232 }
233 }
234 11 substr( $atcf_line, 53, 4 ) = sprintf( "%4d", $pressure );
235  
236 #MAX SUSTAINED WINDS 25 KT WITH GUSTS TO 35 KT.
237 #MAX SUSTAINED WINDS 125 KT WITH GUSTS TO 155 KT.
238  
239 11 @match = grep /^MAX SUSTAINED WINDS/, @{$body_ref};
240  
241 11 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) 11 0 11
242 11 ▶ if ( $match[0] =~ /^MAX SUSTAINED WINDS\s+(\d{1,4}) KT WITH GUSTS TO\s+(\d{1,4})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] =~ /^MAX SUSTAINED WINDS\s+(\d{1,4}) KT WITH GUSTS TO\s+(\d{1,4})/) 11 0 11
243 11 $vmax = $1;
244 11 $gusts = $2;
245 }
246 }
247  
248 11 substr( $atcf_line, 47, 4 ) = sprintf( "%4d", $vmax );
249 11 substr( $atcf_line, 113, 4 ) = sprintf( "%4d", $gusts );
250 11 my $forecast_atcf_filename = lc($storm_name) . "_advisory_" . $adv_num_str . ".fst";
251 #
252 # collect nowcast wind radii, if any
253 11 my $isotachs_found = 0;
254 11 my @isotachs;
255 11 for my $i ( 0 ... $#{$body_ref} ) {
256 174 ▶ if ( @{$body_ref}[$i] =~ /^MAX SUSTAINED WINDS/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$body_ref;}[$i] =~ /^MAX SUSTAINED WINDS/) 11 163 174
257  
258 #64 KT....... 45NE 30SE 20SW 30NW.
259 #50 KT.......120NE 75SE 60SW 75NW.
260 #34 KT.......175NE 120SE 120SW 120NW.
261 11 $i++;
262 11 while (1) {
263 29 ▶ if ( @{$body_ref}[$i] =~ /^(\d{1,2}) KT\.{7}\s{0,}(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$body_ref;}[$i] =~ /^(\d{1,2}) KT\.{7}\s{0,}(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]/) { } 18 11 29
264 18 $isotachs_found++;
265 18 my @wind_radii = ( $1, $2, $3, $4, $5 );
266 18 push @isotachs, @wind_radii;
267 }
268 else {
269 11 last;
270 }
271 18 $i++;
272 }
273 11 ▶ for ( my $j = $isotachs_found; $j > 0; $j-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) 18 11 29
274 18 ▶ for ( my $k = 0; $k < 4; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 4) 72 18 90
275 72 my $starting_pos = 72 + ( $k * 6 );
276 72 my $list_pos = 1 + $k + ( 5 * ( $j - 1 ) );
277  
278 # fill in wind radii
279 72 substr( $atcf_line, $starting_pos, 5 ) = sprintf( "%5d", $isotachs[$list_pos] );
280  
281 # fill in isotach
282 72 substr( $atcf_line, 63, 3 ) = sprintf( "%3d", $isotachs[ 5 * ( $j - 1 ) ] );
283 }
284 18 push @output, $atcf_line;
285 }
286 11 ▶ unless ($isotachs_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isotachs_found) 5 6 11
287 5 push @output, $atcf_line;
288 }
289 11 last;
290 }
291 }
292  
293 # FORECAST tracks and points
294 # FORECAST VALID 10/0000Z 21.5N 84.5W FORECASTs have a bizzare date format:
295 # dd/hhmm BUT near end of the month say 31/0000 the forecast dates switch to
296 # 01/0000, 01/12000 so we need to check for this and increment the month.
297 11 my $forecast_period = 0;
298 11 $forecast_year = $nowcast_year;
299 11 $forecast_month = $nowcast_month;
300 11 $forecast_day = $nowcast_day;
301 11 $forecast_hour = $nowcast_hour;
302 11 my $i = 0;
303 11 ▶ while ( $i < $#{$body_ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#{$body_ref;}) 344 10 354
304 344 ▶ if ( @{$body_ref}[$i] =~ /^(FORECAST|OUTLOOK) VALID/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$body_ref;}[$i] =~ /^(FORECAST|OUTLOOK) VALID/) 53 291 344
305 53 my $atcf_line = $template;
306 53 $atcf_line =~ s/_BASIN_/$storm_basin/;
307  
308 # jgf20160105: fill in the storm number
309 53 substr( $atcf_line, 4, 2 ) = $storm_number_str;
310  
311 # fill in the nowcast time
312 53 substr( $atcf_line, 8, 10 ) = sprintf( "%10d", $nowcast_date_time );
313  
314 # fill in the storm name
315 53 substr( $atcf_line, 148, 10 ) = sprintf( "%10s", $storm_name );
316 53 my $line = @{$body_ref}[$i];
317 53 chomp $line;
318  
319 # if the storm will dissipate, there is no more data to process
320 53 ▶ if ( $line =~ /DISSIPATED/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /DISSIPATED/) 1 52 53
321 1 last;
322 }
323 52 ▶ if ( $line =~ /^(FORECAST|OUTLOOK) VALID\s+(\d{2})\/(\d{4})Z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^(FORECAST|OUTLOOK) VALID\s+(\d{2})/(\d{4})Z]) 52 0 52
324 52 $forecast_day = $2;
325 52 $forecast_hour = substr( $3, 0, 2 );
326 }
327 52 ▶ if ( $line =~ /Z\s+(\d{1,2}\.\d{1,2})([N|S])\s+(\d{1,2}\.\d{1,2})([E|W])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /Z\s+(\d{1,2}\.\d{1,2})([N|S])\s+(\d{1,2}\.\d{1,2})([E|W])/) 52 0 52
328 52 $lat = $1;
329 52 $ns_hem = $2;
330 52 $lon = $3;
331 52 $ew_hem = $4;
332 }
333 52 my $forecast_lat = sprintf( "%4d$ns_hem", $lat * 10 );
334 52 my $forecast_lon = sprintf( "%4d$ew_hem", $lon * 10 );
335 52 substr( $atcf_line, 34, 5 ) = sprintf( "%5s", $forecast_lat );
336 52 substr( $atcf_line, 41, 5 ) = sprintf( "%5s", $forecast_lon );
337  
338 # Get the next line
339 # MAX WIND 30 KT...GUSTS 40 KT.
340 52 $i++;
341 52 $line = @{$body_ref}[$i];
342 52 chomp $line;
343 52 ▶ if ( $line =~ /^MAX WIND\s+(\d{1,4}) KT\.\.\.GUSTS\s+(\d{1,4}) KT\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^MAX WIND\s+(\d{1,4}) KT\.\.\.GUSTS\s+(\d{1,4}) KT\./) 52 0 52
344 52 $vmax = $1;
345 52 $gusts = $2;
346 }
347 52 substr( $atcf_line, 47, 4 ) = sprintf( "%4d", $vmax );
348 52 substr( $atcf_line, 113, 4 ) = sprintf( "%4d", $gusts );
349 52 $forecast_date_time = sprintf( "%04d%02d%02d%02d", $forecast_year, $forecast_month, $forecast_day, $forecast_hour );
350  
351 # check to see if we have crossed into the next month
352 52 ▶ if ( $forecast_date_time < $nowcast_date_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forecast_date_time < $nowcast_date_time) 1 51 52
353 1 $forecast_month++;
354 1 ▶ if ( $forecast_month > 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forecast_month > 12) 1 0 1
355 1 $forecast_month = 1;
356 1 $forecast_year++;
357 }
358 }
359  
360 # Determine the time in hours (forecast period) between the current
361 # forecast and the nowcast time
362 52 my $nowcast_epoch = timegm( 0, 0, $nowcast_hour, $nowcast_day, $nowcast_month - 1, $nowcast_year );
363 52 my $forecast_epoch = timegm( 0, 0, $forecast_hour, $forecast_day, $forecast_month - 1, $forecast_year );
364 52 my $forecast_period = int( ( $forecast_epoch - $nowcast_epoch ) / 3600 );
365 52 substr( $atcf_line, 29, 4 ) = sprintf( "%4d", $forecast_period );
366  
367 # Get the next line and parse the isotachs
368 52 $i++;
369 52 ( $i, my $output_ref ) = _parseIotachs( $body_ref, $i, $atcf_line );
370 52 push @output, @$output_ref;
371 }
372 343 $i++;
373 }
374  
375 # save in instance's internal field
376 11 $self->{as_atcf} = \@output;
377  
378 # return array reference for convenience
379 11 return $self->as_atcf;
380 }
381  
382 54 ▶sub _parseIotachs {
Subroutine _parseIotachs: called CC=5 SCAR=16.1
383 54 my ( $body_ref, $i, $atcf_line ) = @_;
384 54 my $isotachs_found = 0;
385 54 my @isotachs = ();
386 54 my @output = ();
387 54 while (1) {
388  
389 #64 KT... 45NE 30SE 20SW 30NW.
390 151 ▶ if ( @{$body_ref}[$i] =~ /^(\d{1,2}) KT\.{3}\s{0,}(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$body_ref;}[$i] =~ /^(\d{1,2}) KT\.{3}\s{0,}(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]\s+(\d{1,3})[N|S][E|W]/) { } 97 54 151
391 97 $isotachs_found++;
392 97 my @wind_radii = ( $1, $2, $3, $4, $5 );
393 97 push @isotachs, @wind_radii;
394 }
395 else {
396 54 last;
397 }
398 97 $i++;
399 }
400 54 ▶ for ( my $j = $isotachs_found; $j > 0; $j-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) 97 54 151
401 97 ▶ for ( my $k = 0; $k < 4; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 4) 388 97 485
402 388 my $starting_pos = 72 + ( $k * 6 );
403 388 my $list_pos = 1 + $k + ( 5 * ( $j - 1 ) );
404  
405 # fill in wind radii
406 388 substr( $atcf_line, $starting_pos, 5 ) = sprintf( "%5d", $isotachs[$list_pos] );
407  
408 # fill in isotach
409 388 substr( $atcf_line, 63, 3 ) = sprintf( "%3d", $isotachs[ 5 * ( $j - 1 ) ] );
410 }
411 97 push @output, $atcf_line;
412 }
413 54 ▶ unless ($isotachs_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isotachs_found) 17 37 54
414 17 push @output, $atcf_line;
415 }
416 54 return ( $i, \@output );
417 }
418  
419 3 1;
420  
421 __END__
↑ Summary TropicalCyclone/Validation.pm »