blib/lib/Geo/WeatherNWS.pm

filter: Statementstmt 68.9% 280 / 406 Branchbran 60.5% 103 / 170 Conditioncond 56.9% 49 / 86 MC/DCmcdc 31.2% 15 / 48 Subroutinesub 70.3% 19 / 27 Podpod 100% 20 / 20 totaltotal 64.2% 486 / 757 CC 119 SCAR 47.9
CC
119
Cov
65%
decode
187.9
getreport
141.1
translate_weather
16.8
CRAP 120.1
↑ Summary
1 package Geo::WeatherNWS;
2  
3 #------------------------------------------------------------------------------
4 #
5 # Package Name: Get Observations from NWS (Geo::WeatherNWS)
6 #
7 # Last Modified: 18 December 2001 - Prepared for CPAN - Marc Slagle
8 # 24 February 2002 - Adding server/error code - Marc
9 # 10 August 2011 - changed FTP server name, added tests,
10 # docs, some restructuring. - Bob Ernst
11 # 14 November 2012 - removed unneeded /d after tr,
12 # make network tests optional,
13 # check status of opens - Bob
14 # 26 November 2012 - Address bug 14632 (METAR Decoding) from dstroma
15 # Address bug 27513 (Geo-WeatherNWS returns wrong station code)
16 # from Guenter Knauf
17 # Fix issues with undefined values,
18 # Change some conversion constants,
19 # Round instead of truncate results,
20 # Only calculate windchill for proper range,
21 # "ptemerature" is now spelled "ptemperature"
22 # Fixed handling of condition text
23 # Relax ICAO naming rules
24 # Change ICAO website
25 # Change http web site from weather.noaa.gov
26 # to www.aviationweather.gov, and change parsing to match.
27 # Add report_date and report_time items.
28 # - Bob
29 # 27 November 2012 - Add POD documentation for new functions.
30 # 1 January 2017 - Switched from POSIX module to File::Temp
31 #
32 #
33 #------------------------------------------------------------------------------
34  
35 #------------------------------------------------------------------------------
36 # We need these
37 #------------------------------------------------------------------------------
38  
39 6 require 5.005_62;
40 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Net::FTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 #------------------------------------------------------------------------------
48 # Version
49 #------------------------------------------------------------------------------
50  
51 6 our $VERSION = '1.054';
52  
53 #------------------------------------------------------------------------------
54 # Round function
55 # Using Math::Round would add another dependency
56 #------------------------------------------------------------------------------
57  
58 sub round {
59 149 ▶ my $float = shift;
Subroutine round: called CC=2 SCAR=6.9
Pod documented
60 149 my $rounded;
61  
62 149 ▶ if ( defined $float ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $float) 132 17 149
63 132 $rounded = sprintf "%.0f", $float;
64 }
65 149 return $rounded;
66 }
67  
68 #------------------------------------------------------------------------------
69 # Temperature conversion
70 # If the temperature we are converting from is undefined,
71 # then the temperature we are converting to is also undefined.
72 #------------------------------------------------------------------------------
73  
74 sub convert_f_to_c {
75 20 ▶ my $fahrenheit = shift;
Subroutine convert_f_to_c: called CC=2 SCAR=6.9
Pod documented
76 20 my $celsius;
77  
78 20 ▶ if (defined $fahrenheit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fahrenheit) 11 9 20
79 11 $celsius = (5.0/9.0) * ($fahrenheit - 32.0);
80 }
81 20 return $celsius;
82 }
83  
84 sub convert_c_to_f {
85 20 ▶ my $celsius = shift;
Subroutine convert_c_to_f: called CC=2 SCAR=6.9
Pod documented
86 20 my $fahrenheit;
87  
88 20 ▶ if (defined $celsius) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $celsius) 19 1 20
89 19 $fahrenheit = ((9.0/5.0) * $celsius) + 32.0;
90 }
91 20 return $fahrenheit;
92 }
93  
94 #------------------------------------------------------------------------------
95 # Windchill
96 #------------------------------------------------------------------------------
97  
98 sub windchill {
99 13 ▶ my $F = shift;
Subroutine windchill: called CC=5 SCAR=16.1
Pod documented
100 13 my $wind_speed_mph = shift;
101 13 my $windchill;
102  
103 # This is the North American wind chill index.
104 # Windchill temperature is only defined for:
105 # * temperatures at or below 50 F
106 # * wind speed above 3 mph
107 # Bright sunshine may increase the wind chill temperature by
108 # 10 to 18 degress F.
109  
110 13 ▶ if (defined $F && defined $wind_speed_mph) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $F and defined $wind_speed_mph) 12 1 13
Condition66%2/3
defined $F and defined $wind_speed_mph
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $F and defined $wind_speed_mph
defined $F defined $wind_speed_mph
Truth table66%2/3
A and B
A= defined $FB= defined $wind_speed_mph
ABresult
0X0
100
111
111 # Old Formula
112 # my $Windc=int(
113 # 0.0817*
114 # (3.71*$Self->{windspeedmph}**0.5 + 5.81 - 0.25*$Self->{windspeedmph})*
115 # ($F - 91.4) + 91.4);
116  
117 # New Formula
118 12 ▶ if ($F <= 50 && $wind_speed_mph > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F <= 50 and $wind_speed_mph > 3) 3 9 12
Condition100%3/3
$F <= 50 and $wind_speed_mph > 3
!ll&&!rl&&r
813
MC/DC100%2/2
$F <= 50 and $wind_speed_mph > 3
$F <= 50 $wind_speed_mph > 3
Truth table100%3/3
A and B
A= $F <= 50B= $wind_speed_mph > 3
ABresult
0X0
100
111
119 3 $windchill =
120 35.74 +
121 ( 0.6215 * $F ) -
122 ( 35.75 * ( $wind_speed_mph**0.16 ) ) +
123 ( ( 0.4275 * $F ) * ( $wind_speed_mph**0.16 ) );
124 }
125 }
126 13 return $windchill;
127 }
128  
129 #------------------------------------------------------------------------------
130 # Heat Index
131 #------------------------------------------------------------------------------
132  
133 sub heat_index {
134 10 ▶ my $F = shift;
Subroutine heat_index: called CC=3 SCAR=11.6
Pod documented
135 10 my $rh = shift;
136 10 my $heat_index;
137  
138 10 ▶ if (defined $F && defined $rh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $F and defined $rh) 10 0 10
Condition33%1/3
defined $F and defined $rh
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $F and defined $rh
defined $F defined $rh
Truth table33%1/3
A and B
A= defined $FB= defined $rh
ABresult
0X0
100
111
139 10 $heat_index =
140 -42.379 +
141 2.04901523 * $F +
142 10.14333127 * $rh -
143 0.22475541 * $F * $rh -
144 6.83783e-03 * $F**2 -
145 5.481717e-02 * $rh**2 +
146 1.22874e-03 * $F**2 * $rh +
147 8.5282e-04 * $F * $rh**2 -
148 1.99e-06 * $F**2 * $rh**2;
149 }
150 10 return $heat_index;
151 }
152  
153 #------------------------------------------------------------------------------
154 # Convert wind speed from nautical miles per hour to miles per hour
155 #------------------------------------------------------------------------------
156  
157 sub convert_kts_to_mph {
158 18 ▶ my $knots = shift;
Subroutine convert_kts_to_mph: called CC=2 SCAR=7.0
Pod documented
159 18 my $mph;
160  
161 18 ▶ if (defined $knots) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $knots) 18 0 18
162 18 $mph = $knots * 1.150779;
163 }
164 18 return $mph;
165 }
166  
167 #------------------------------------------------------------------------------
168 # Convert wind speed from nautical miles per hour to kilometers per hour
169 #------------------------------------------------------------------------------
170  
171 sub convert_kts_to_kmh {
172 17 ▶ my $knots = shift;
Subroutine convert_kts_to_kmh: called CC=2 SCAR=7.0
Pod documented
173 17 my $kmh;
174  
175 17 ▶ if (defined $knots) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $knots) 17 0 17
176 17 $kmh = $knots * 1.852;
177 }
178 17 return $kmh;
179 }
180  
181 #------------------------------------------------------------------------------
182 # Convert miles to kilometers
183 #------------------------------------------------------------------------------
184  
185 sub convert_miles_to_km {
186 8 ▶ my $miles = shift;
Subroutine convert_miles_to_km: called CC=2 SCAR=7.0
Pod documented
187 8 my $km;
188  
189 8 ▶ if (defined $miles) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $miles) 8 0 8
190 8 $km = $miles * 1.609344;
191 }
192 8 return $km;
193 }
194  
195 #------------------------------------------------------------------------------
196 # Translate Weather into readable Conditions Text
197 #
198 # Reference is WMO Code Table 4678
199 #------------------------------------------------------------------------------
200  
201 sub translate_weather {
202 10 ▶ my $coded = shift;
Subroutine translate_weather: called CC=13 SCAR=28.2
Pod documented
203 10 my $old_conditionstext = shift;
204 10 my $old_conditions1 = shift;
205 10 my $old_conditions2 = shift;
206 10 my ($conditionstext, $conditions1, $conditions2, $intensity);
207  
208 # We use %Converter to translate 2-letter codes into text
209  
210 10 my %Converter = (
211 BR => 'Mist',
212 TS => 'Thunderstorm',
213 MI => 'Shallow',
214 PR => 'Partial',
215 BC => 'Patches',
216 DR => 'Low Drifting',
217 BL => 'Blowing',
218 SH => 'Shower',
219 FZ => 'Freezing',
220 DZ => 'Drizzle',
221 RA => 'Rain',
222 SN => 'Snow',
223 SG => 'Snow Grains',
224 IC => 'Ice Crystals',
225 PE => 'Ice Pellets',
226 PL => 'Ice Pellets',
227 GR => 'Hail',
228 GS => 'Small Hail/Snow',
229 UP => 'Unknown Precipitation',
230 FG => 'Fog',
231 FU => 'Smoke',
232 VA => 'Volcanic Ash',
233 DU => 'Widespread Dust',
234 SA => 'Sand',
235 HZ => 'Haze',
236 PY => 'Spray',
237 PO => 'Dust Devils',
238 SQ => 'Squalls',
239 FC => 'Tornado',
240 SS => 'Sandstorm'
241 );
242  
243 10 ▶ if ( $coded =~ /^[-+]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coded =~ /^[-+]/) { } 6 4 10
244 # Heavy(+) or Light(-) condition
245  
246 6 ▶ if ( !$old_conditions1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_conditions1) 6 0 6
247 6 my ( $Block1, $Block2 );
248 6 my $Modifier = substr( $coded, 0, 1 ); # +/-
249 6 my $Block1t = substr( $coded, 1, 2 ); # e.g. TS
250 6 my $Block2t = substr( $coded, 3, 4 ); # e.g. RA
251  
252 6 $Block1 = $Converter{$Block1t}; # e.g. Thunderstorm
253 6 $conditions1 = $Block1; # e.g. Thunderstorm
254  
255 6 ▶ if ($Block2t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Block2t) 6 0 6
256 6 $Block2 = $Converter{$Block2t}; # e.g. Rain
257 6 $conditions2 = $Block2; # e.g. Rain
258 }
259  
260 6 ▶ if ( $Modifier =~ /^\-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Modifier =~ /^\-/) { } 3 3 6
elsif ($Modifier =~ /^\+/) { } 3 0 3
261 3 $Block1 = "Light $Block1"; # e.g. Light Thunderstorm
262 3 $intensity = "Light";
263 }
264 elsif ( $Modifier =~ /^\+/ ) {
265 3 $Block1 = "Heavy $Block1"; # e.g. Heavy Thunderstorm
266 3 $intensity = "Heavy";
267 }
268  
269 6 ▶ if ($Block2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Block2) 6 0 6
270 6 $Block1 = "$Block1 $Block2"; # e.g. Light Thunderstorm Rain
271 }
272  
273 6 ▶ if ($old_conditionstext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_conditionstext) { } 0 6 6
274 0 ▶ if ( $Block1 eq "SH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Block1 eq 'SH') { } 0 0 0
275 0 $conditionstext = "$Block2 of $Block1";
276 0 $conditions1 = "Showers of";
277 }
278 else {
279 0 $conditionstext = "$old_conditionstext and $Block1";
280 }
281 }
282 else {
283 6 $conditionstext = $Block1;
284 }
285 }
286 }
287 else {
288 # Moderate condition
289  
290 4 ▶ if ( !$old_conditions1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_conditions1) 4 0 4
291 4 my ( $Block1, $Block2 );
292 4 my $Block1t = substr( $coded, 0, 2 ); # e.g. TS
293 4 my $Block2t = substr( $coded, 2, 4 ); # e.g. RA
294  
295 4 $Block1 = $Converter{$Block1t}; # e.g. Thunderstorm
296 4 $conditions1 = $Block1; # e.g. Thunderstorm
297  
298 4 ▶ if ($Block2t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Block2t) 4 0 4
299 4 $Block2 = $Converter{$Block2t};
300 4 $conditions2 = $Block2;
301 4 $Block1 = "$Block1 $Block2";
302 }
303  
304 4 ▶ if ($old_conditionstext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_conditionstext) { } 0 4 4
305 0 ▶ if ( $Block1 eq "SH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Block1 eq 'SH') { } 0 0 0
306 0 $conditionstext = "$Block2 of $Block1";
307 0 $conditions1 = "Showers of";
308 }
309 else {
310 0 $conditionstext = "$old_conditionstext and $Block1";
311 }
312 }
313 else {
314 4 $conditionstext = $Block1;
315 }
316 }
317 }
318 10 return ($conditionstext, $conditions1, $conditions2, $intensity);
319 }
320  
321 #------------------------------------------------------------------------------
322 # Lets create a new self
323 #------------------------------------------------------------------------------
324  
325 sub new {
326 9 ▶ my $Proto = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
327 9 ▶ my $Class = ref($Proto) || $Proto || __PACKAGE__;
Condition80%4/5
ref $Proto || $Proto || __PACKAGE__
l!l
18
ref $Proto or $Proto
l!l&&r!l&&!r
018
MC/DC50%1/2
ref $Proto || $Proto || __PACKAGE__
ref $Proto $Proto
Truth table66%2/3
A or B
A= ref $ProtoB= $Proto
ABresult
1X1
011
000
328 9 my $Self = {};
329  
330 9 $Self->{servername} = "tgftp.nws.noaa.gov";
331 9 $Self->{username} = "anonymous";
332 9 $Self->{password} = 'weather@cpan.org';
333 9 $Self->{directory} = "/data/observations/metar/stations";
334 9 $Self->{timeout} = 120;
335  
336 9 bless $Self, $Class;
337 9 return $Self;
338 }
339  
340 #------------------------------------------------------------------------------
341 # Adding ability to edit server/user/directory at runtime...
342 #------------------------------------------------------------------------------
343  
344 sub setservername {
345 0 ▶ my $Self = shift;
Subroutine setservername: not called CC=1 SCAR=6.9
Pod documented
346 0 my $Servername = shift;
347 0 $Self->{servername} = $Servername;
348 0 return $Self;
349 }
350  
351 sub setusername {
352 0 ▶ my $Self = shift;
Subroutine setusername: not called CC=1 SCAR=6.9
Pod documented
353 0 my $User = shift;
354 0 $Self->{username} = $User;
355 0 return $Self;
356 }
357  
358 sub setpassword {
359 0 ▶ my $Self = shift;
Subroutine setpassword: not called CC=1 SCAR=6.9
Pod documented
360 0 my $Pass = shift;
361 0 $Self->{password} = $Pass;
362 0 return $Self;
363 }
364  
365 sub setdirectory {
366 0 ▶ my $Self = shift;
Subroutine setdirectory: not called CC=1 SCAR=6.9
Pod documented
367 0 my $Dir = shift;
368 0 $Self->{directory} = $Dir;
369 0 return $Self;
370 }
371  
372 sub settemplatefile {
373 0 ▶ my $Self = shift;
Subroutine settemplatefile: not called CC=1 SCAR=6.9
Pod documented
374 0 my $Tfile = shift;
375 0 $Self->{tfile} = $Tfile;
376 0 return $Self;
377 }
378  
379 sub settimeout {
380 0 ▶ my $Self = shift;
Subroutine settimeout: not called CC=1 SCAR=6.9
Pod documented
381 0 my $Seconds = shift;
382 0 $Self->{timeout} = $Seconds;
383 0 return $Self;
384 }
385  
386 #------------------------------------------------------------------------------
387 # Here we get to FTP to the NWS and get the data
388 #------------------------------------------------------------------------------
389  
390 sub getreporthttp {
391 0 ▶ my $Self = shift;
Subroutine getreporthttp: not called CC=1 SCAR=6.9
Pod documented
392 0 my $Code = shift;
393 # The old site was: http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$Code
394 $Self->{http} =
395 0 'http://www.aviationweather.gov/adds/metars/?station_ids=' . $Code . '&chk_metars=on&hoursStr=most+recent+only';
396 0 my $Ret = &getreport( $Self, $Code );
397 0 return $Ret;
398 }
399  
400 sub getreport {
401 0 ▶ my $Self = shift;
Subroutine getreport: not called CC=12 SCAR=49.5
Pod documented
402 0 my $Station = shift;
403  
404 0 $Self->{error} = "0";
405  
406 0 my $Tmphandle = File::Temp->new();
407 0 my $Tmpfile = $Tmphandle->filename;
408 0 close $Tmphandle;
409  
410 0 my $Code = uc($Station);
411  
412 0 ▶ if ( !$Code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Code) 0 0 0
413 0 $Self->{error} = "1";
414 0 $Self->{errortext} = "No Station Code Entered\n";
415 0 return $Self;
416 }
417  
418 0 ▶ if ( $Self->{http} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{'http'}) { } 0 0 0
419 6 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
420 0 my $Ua = LWP::UserAgent->new();
421 0 $Ua->agent("Geo::WeatherNWS $VERSION");
422  
423 0 my $Req = HTTP::Request->new( GET => $Self->{http} );
424 0 $Req->content_type('application/x-www-form-urlencoded');
425 0 my $Res = $Ua->request($Req);
426  
427 0 ▶ if ( $Res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Res->is_success) 0 0 0
428 0 my @Lines = split( /\n/, $Res->content );
429 0 foreach my $Line (@Lines) {
430 0 ▶ if ( $Line =~ /<(TITLE|H1|H2)>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Line =~ /<(TITLE|H1|H2)>/) { } 0 0 0
431 # ignore
432 }
433 else {
434 # Remove HTML elements.
435 # (This isn't very robust, but it gets the job done for now.)
436 0 $Line =~ s/<[^>]*>//g;
437  
438 # If the line starts with an ICAO, then the line is an observation (we hope)
439 0 ▶ if ( $Line =~ /^[A-Z][A-Z0-9]{3}\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Line =~ /^[A-Z][A-Z0-9]{3}\s/) 0 0 0
440 0 $Self->{obs} = $Line;
441 0 last;
442 }
443 }
444 }
445 }
446 }
447 else {
448  
449 # Some users needed this for firewalls...
450 my $Ftp = Net::FTP->new(
451 $Self->{servername},
452 Debug => 0,
453 Passive => 1,
454 Timeout => $Self->{timeout}
455 0 );
456  
457 # my $Ftp=Net::FTP->new($Self->{servername}, Debug => 0);
458 0 ▶ if ( !defined $Ftp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Ftp) 0 0 0
459 0 $Self->{error} = 1;
460 0 $Self->{errortext} = "Cannot connect to $Self->{servername}: $@";
461 0 return $Self;
462 }
463 0 $Ftp->login( $Self->{username}, $Self->{password} );
464 0 my $Rcode = $Ftp->code();
465 0 my $Message = $Ftp->message();
466  
467 0 ▶ if ( $Rcode =~ /^[45]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rcode =~ /^[45]/) 0 0 0
468 0 $Self->{error} = $Rcode;
469 0 $Self->{errortext} = $Message;
470 0 return $Self;
471 }
472  
473 0 $Ftp->cwd( $Self->{directory} );
474 0 $Rcode = $Ftp->code();
475 0 $Message = $Ftp->message();
476  
477 0 ▶ if ( $Rcode =~ /^[45]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rcode =~ /^[45]/) 0 0 0
478 0 $Self->{error} = $Rcode;
479 0 $Self->{errortext} = $Message;
480 0 return $Self;
481 }
482  
483 0 $Rcode = $Ftp->get( "$Code.TXT", $Tmpfile );
484 0 $Rcode = $Ftp->code();
485 0 $Message = $Ftp->message();
486 0 $Ftp->quit;
487  
488 0 ▶ if ( $Rcode =~ /^[45]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rcode =~ /^[45]/) 0 0 0
489 0 $Self->{error} = $Rcode;
490 0 $Self->{errortext} = $Message;
491 0 return $Self;
492 }
493  
494 0 local $/; # enable slurp mode
495 0 ▶ open my $F, '<', $Tmpfile or
Branch: 1 branch
BranchTrueFalseTotal
open my $F, "<", $Tmpfile or croak("error opening temp input ${Tmpfile}: $!") 0 0 0
496 croak "error opening temp input $Tmpfile: $!";
497 0 my $Data = <$F>;
498 0 close($F);
499 0 unlink($Tmpfile);
500  
501 0 $Data =~ tr/\n/ /;
502 0 $Self->{obs} = $Data;
503 }
504  
505 0 $Self->decode();
506 0 return $Self;
507 }
508  
509 #------------------------------------------------------------------------------
510 # Decodeobs takes the obs in a string format and decodes them
511 #------------------------------------------------------------------------------
512  
513 sub decodeobs {
514 8 ▶ my $Self = shift;
Subroutine decodeobs: called CC=1 SCAR=0.0
Pod documented
515 8 my $Obs = shift;
516 8 $Self->{obs} = $Obs;
517 8 $Self->decode();
518 8 return $Self;
519 }
520  
521 #------------------------------------------------------------------------------
522 # Decode does the work, and is only called internally
523 #------------------------------------------------------------------------------
524  
525 sub decode {
526 8 ▶ my $Self = shift;
Subroutine decode: called CC=82 SCAR=52.4
Pod documented
527 8 my @Cloudlevels;
528  
529 8 my @Splitter = split( /\s+/, $Self->{obs} );
530  
531 #------------------------------------------------------------------------------
532 # Break the METAR observations down and decode
533 #------------------------------------------------------------------------------
534  
535 8 my $have_icao_code = 0;
536 8 my $column = 0;
537  
538 8 foreach my $Line (@Splitter) {
539 100 $column++;
540  
541  
542 #------------------------------------------------------------------------------
543 # Report date and time
544 # These aren't always present (for example, from the http interface)
545 #------------------------------------------------------------------------------
546  
547 100 ▶ if ( $column == 1 && $Line =~ /^\d{4}\/\d{2}\/\d{2}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column == 1 and $Line =~ m[^\d{4}/\d{2}/\d{2}$]) 8 92 100
Condition66%2/3
$column == 1 and $Line =~ m[^\d{4}/\d{2}/\d{2}$]
!ll&&!rl&&r
9208
MC/DC50%1/2
$column == 1 and $Line =~ m[^\d{4}/\d{2}/\d{2}$]
$column == 1 $Line =~ m[^\d{4}/\d{2}/\d{2}$]
Truth table66%2/3
A and B
A= $column == 1B= $Line =~ m[^\d{4}/\d{2}/\d{2}$]
ABresult
0X0
100
111
548 8 $Self->{report_date} = $Line;
549 }
550  
551 100 ▶ if ( $column == 2 && $Line =~ /^\d{2}:\d{2}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column == 2 and $Line =~ /^\d{2}:\d{2}$/) 8 92 100
Condition66%2/3
$column == 2 and $Line =~ /^\d{2}:\d{2}$/
!ll&&!rl&&r
9208
MC/DC50%1/2
$column == 2 and $Line =~ /^\d{2}:\d{2}$/
$column == 2 $Line =~ /^\d{2}:\d{2}$/
Truth table66%2/3
A and B
A= $column == 2B= $Line =~ /^\d{2}:\d{2}$/
ABresult
0X0
100
111
552 8 $Self->{report_time} = $Line;
553 }
554  
555 #------------------------------------------------------------------------------
556 # ICAO station code
557 #------------------------------------------------------------------------------
558  
559 100 ▶ if ( ( $Line =~ /^([A-Z][A-Z0-9]{3})/ ) &&
Branch: 10 branches
BranchTrueFalseTotal
if ($Line =~ /^([A-Z][A-Z0-9]{3})/ and not $have_icao_code) { } 8 92 100
elsif ($Line =~ /([0-9]Z)$/) { } 8 84 92
elsif ($Line =~ /([0-9]KT)$/) { } 8 76 84
elsif ($Line =~ /([0-9]SM)$/) { } 7 69 76
elsif ($Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /x or $Line =~ /^VC([A-Z])*/ or $Line =~ /[\+\-]VC([A-Z])*/) { } 6 63 69
elsif ($Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/ or $Line =~ /^(SCT[0-9])/ or $Line =~ /^(BKN[0-9])/ or $Line =~ /^(OVC[0-9])/) { } 11 52 63
elsif ($Line =~ m[^([0-9][0-9]/[0-9][0-9])] or $Line =~ m[^(M[0-9][0-9]/)] or $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])] or $Line =~ m[^([0-9][0-9]/M[0-9][0-9])]) { } 8 44 52
elsif ($Line =~ /^(A[0-9]{4})/) { } 7 37 44
elsif ($Line =~ /^(Q[0-9]{4})/) { } 1 36 37
elsif ($Line =~ /^(RMK)/) { } 7 29 36
Condition100%3/3
$Line =~ /^([A-Z][A-Z0-9]{3})/ and not $have_icao_code
!ll&&!rl&&r
65278
MC/DC100%2/2
$Line =~ /^([A-Z][A-Z0-9]{3})/ and not $have_icao_code
$Line =~ /^([A-Z][A-Z0-9]{3})/ not $have_icao_code
Truth table100%3/3
A and not(B)
A= $Line =~ /^([A-Z][A-Z0-9]{3})/B= not $have_icao_code
ABresult
0X0
100
111
560 ( !$have_icao_code ) ) {
561 # Use the first value that looks like the ICAO code.
562 # This should either be the first item, or
563 # the third item if there is a leading date and time.
564 # (Before we checked have_icao_code, we'd get values
565 # like TSRA or FZFG later in the observation being treated
566 # as the ICAO code.)
567 # We also allow the last three characters to be digits.
568  
569 # There was a check for "AUTO" above before, for now
570 # we'll add an extra check for that value. (AUTO should
571 # show up in the fifth column.)
572 8 ▶ croak "Unexpected value AUTO for ICAO code" if $Line eq "AUTO";
Branch: 1 branch
BranchTrueFalseTotal
if $Line eq "AUTO" 0 8 8
573  
574 8 $Self->{code} = $Line;
575 8 $have_icao_code = 1;
576 }
577  
578 #------------------------------------------------------------------------------
579 # Report Time
580 #------------------------------------------------------------------------------
581  
582 elsif ( $Line =~ /([0-9]Z)$/ ) {
583 8 my $Timez = substr( $Line, 2, 4 );
584 8 $Self->{time} = $Timez;
585 8 $Self->{day} = substr( $Line, 0, 2 );
586 }
587  
588 #------------------------------------------------------------------------------
589 # Wind speed and direction
590 #------------------------------------------------------------------------------
591  
592 elsif ( $Line =~ /([0-9]KT)$/ ) {
593 8 my $Newline;
594 my $Variable;
595  
596 8 ▶ if ( $Line =~ /VRB/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Line =~ /VRB/) { } 0 8 8
597 0 $Newline = substr( $Line, 3 );
598 0 $Variable = "1";
599 }
600 else {
601 8 $Newline = $Line;
602 }
603  
604 8 my $Winddir = substr( $Newline, 0, 3 );
605 8 $Winddir =~ tr/[A-Z]/ /d;
606 8 $Winddir = $Winddir - 0;
607 8 my $Winddirtxt;
608  
609 8 ▶ if ($Variable) {
Branch: 9 branches
BranchTrueFalseTotal
if ($Variable) { } 0 8 8
elsif ($Winddir <= 22.5 or $Winddir >= 337.5) { } 2 6 8
elsif ($Winddir <= 67.5 and $Winddir >= 22.5) { } 0 6 6
elsif ($Winddir <= 112.5 and $Winddir >= 67.5) { } 0 6 6
elsif ($Winddir <= 157.5 and $Winddir >= 112.5) { } 0 6 6
elsif ($Winddir <= 202.5 and $Winddir >= 157.5) { } 0 6 6
elsif ($Winddir <= 247.5 and $Winddir >= 202.5) { } 6 0 6
elsif ($Winddir <= 292.5 and $Winddir >= 247.5) { } 0 0 0
elsif ($Winddir <= 337.5 and $Winddir >= 292.5) { } 0 0 0
610 0 $Winddirtxt = "Variable";
611 }
612 ▶ elsif ( ( $Winddir <= 22.5 ) || ( $Winddir >= 337.5 ) ) {
Condition66%2/3
$Winddir <= "22.5" or $Winddir >= "337.5"
l!l&&r!l&&!r
206
MC/DC50%1/2
$Winddir <= "22.5" or $Winddir >= "337.5"
$Winddir <= "22.5" $Winddir >= "337.5"
Truth table66%2/3
A or B
A= $Winddir <= "22.5"B= $Winddir >= "337.5"
ABresult
1X1
011
000
613 2 $Winddirtxt = "North";
614 }
615 ▶ elsif ( ( $Winddir <= 67.5 ) && ( $Winddir >= 22.5 ) ) {
Condition33%1/3
$Winddir <= "67.5" and $Winddir >= "22.5"
!ll&&!rl&&r
600
MC/DC0%0/2
$Winddir <= "67.5" and $Winddir >= "22.5"
$Winddir <= "67.5" $Winddir >= "22.5"
Truth table33%1/3
A and B
A= $Winddir <= "67.5"B= $Winddir >= "22.5"
ABresult
0X0
100
111
616 0 $Winddirtxt = "Northeast";
617 }
618 ▶ elsif ( ( $Winddir <= 112.5 ) && ( $Winddir >= 67.5 ) ) {
Condition33%1/3
$Winddir <= "112.5" and $Winddir >= "67.5"
!ll&&!rl&&r
600
MC/DC0%0/2
$Winddir <= "112.5" and $Winddir >= "67.5"
$Winddir <= "112.5" $Winddir >= "67.5"
Truth table33%1/3
A and B
A= $Winddir <= "112.5"B= $Winddir >= "67.5"
ABresult
0X0
100
111
619 0 $Winddirtxt = "East";
620 }
621 ▶ elsif ( ( $Winddir <= 157.5 ) && ( $Winddir >= 112.5 ) ) {
Condition33%1/3
$Winddir <= "157.5" and $Winddir >= "112.5"
!ll&&!rl&&r
600
MC/DC0%0/2
$Winddir <= "157.5" and $Winddir >= "112.5"
$Winddir <= "157.5" $Winddir >= "112.5"
Truth table33%1/3
A and B
A= $Winddir <= "157.5"B= $Winddir >= "112.5"
ABresult
0X0
100
111
622 0 $Winddirtxt = "Southeast";
623 }
624 ▶ elsif ( ( $Winddir <= 202.5 ) && ( $Winddir >= 157.5 ) ) {
Condition33%1/3
$Winddir <= "202.5" and $Winddir >= "157.5"
!ll&&!rl&&r
600
MC/DC0%0/2
$Winddir <= "202.5" and $Winddir >= "157.5"
$Winddir <= "202.5" $Winddir >= "157.5"
Truth table33%1/3
A and B
A= $Winddir <= "202.5"B= $Winddir >= "157.5"
ABresult
0X0
100
111
625 0 $Winddirtxt = "South";
626 }
627 ▶ elsif ( ( $Winddir <= 247.5 ) && ( $Winddir >= 202.5 ) ) {
Condition33%1/3
$Winddir <= "247.5" and $Winddir >= "202.5"
!ll&&!rl&&r
006
MC/DC0%0/2
$Winddir <= "247.5" and $Winddir >= "202.5"
$Winddir <= "247.5" $Winddir >= "202.5"
Truth table33%1/3
A and B
A= $Winddir <= "247.5"B= $Winddir >= "202.5"
ABresult
0X0
100
111
628 6 $Winddirtxt = "Southwest";
629 }
630 ▶ elsif ( ( $Winddir <= 292.5 ) && ( $Winddir >= 247.5 ) ) {
Condition0%0/3
$Winddir <= "292.5" and $Winddir >= "247.5"
!ll&&!rl&&r
000
MC/DC0%0/2
$Winddir <= "292.5" and $Winddir >= "247.5"
$Winddir <= "292.5" $Winddir >= "247.5"
Truth table0%0/3
A and B
A= $Winddir <= "292.5"B= $Winddir >= "247.5"
ABresult
0X0
100
111
631 0 $Winddirtxt = "West";
632 }
633 ▶ elsif ( ( $Winddir <= 337.5 ) && ( $Winddir >= 292.5 ) ) {
Condition0%0/3
$Winddir <= "337.5" and $Winddir >= "292.5"
!ll&&!rl&&r
000
MC/DC0%0/2
$Winddir <= "337.5" and $Winddir >= "292.5"
$Winddir <= "337.5" $Winddir >= "292.5"
Truth table0%0/3
A and B
A= $Winddir <= "337.5"B= $Winddir >= "292.5"
ABresult
0X0
100
111
634 0 $Winddirtxt = "Northwest";
635 }
636  
637 8 my $Windspeedkts = substr( $Line, 3 );
638 8 my $Windgustkts = 0;
639  
640 8 ▶ if ( $Windspeedkts =~ /G/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Windspeedkts =~ /G/) 6 2 8
641 6 my @Splitter = split( /G/, $Windspeedkts );
642  
643 6 $Windspeedkts = $Splitter[0];
644 6 $Windgustkts = $Splitter[1];
645 }
646  
647 8 $Windspeedkts =~ tr/[A-Z]//d;
648 8 $Windgustkts =~ tr/[A-Z]//d;
649  
650 8 ▶ if ( $Windspeedkts == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Windspeedkts == 0) 1 7 8
651 1 $Winddirtxt = "Calm";
652 }
653  
654 8 my $MPH = round( convert_kts_to_mph($Windspeedkts) );
655 8 my $GMPH = round( convert_kts_to_mph($Windgustkts) );
656 8 my $KMH = round( convert_kts_to_kmh($Windspeedkts) );
657 8 my $GKMH = round( convert_kts_to_kmh($Windgustkts) );
658  
659 8 $Self->{windspeedkts} = $Windspeedkts;
660 8 $Self->{windgustkts} = $Windgustkts;
661 8 $Self->{windspeedkts} = $Self->{windspeedkts} - 0;
662 8 $Self->{windspeedmph} = $MPH;
663 8 $Self->{windspeedkmh} = $KMH;
664 8 $Self->{windgustmph} = $GMPH;
665 8 $Self->{windgustkmh} = $GKMH;
666 8 $Self->{winddirtext} = $Winddirtxt;
667 8 $Self->{winddir} = $Winddir;
668 8 $Self->{winddir} = $Self->{winddir} - 0;
669 }
670  
671 #------------------------------------------------------------------------------
672 # Current Visibility
673 #------------------------------------------------------------------------------
674  
675 elsif ( $Line =~ /([0-9]SM)$/ ) {
676 7 $Line =~ tr/[A-Z]//d;
677  
678 #------------------------------------------------------------------------------
679 # Some stations were reporting fractions for this value
680 #------------------------------------------------------------------------------
681  
682 7 ▶ if ( $Line =~ /\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Line =~ m[/]) 6 1 7
683 6 my @Splitter = split( /\//, $Line );
684 6 $Line = $Splitter[0] / $Splitter[1];
685 }
686  
687 7 my $Viskm = convert_miles_to_km( $Line );
688 7 $Self->{visibility_mi} = round($Line);
689 7 $Self->{visibility_km} = round($Viskm);
690 }
691  
692 #------------------------------------------------------------------------------
693 # Current Conditions
694 #------------------------------------------------------------------------------
695  
696 ▶ elsif (
Condition66%4/6
$Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /x or $Line =~ /^VC([A-Z])*/ or $Line =~ /[\+\-]VC([A-Z])*/
l!l&&r!l&&!r
6063
$Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /x or $Line =~ /^VC([A-Z])*/
l!l&&r!l&&!r
6063
MC/DC33%1/3
$Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /x or $Line =~ /^VC([A-Z])*/ or $Line =~ /[\+\-]VC([A-Z])*/
$Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /x $Line =~ /^VC([A-Z])*/ $Line =~ /[\+\-]VC([A-Z])*/
Truth table50%2/4
A or B or C
A= $Line =~ / (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS) ([A-Z])* /xB= $Line =~ /^VC([A-Z])*/C= $Line =~ /[\+\-]VC([A-Z])*/
ABCresult
1XX1
01X1
0011
0000
697 ( $Line =~ /
698 (BR|TS|MI|PR|BC|DR|BL|SH|FZ|DZ|RA|SN|SG|IC|PE|PL|GR|GS|UP|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS)
699 ([A-Z])*
700 /x)
701 || ( $Line =~ /^VC([A-Z])*/ )
702 || ( $Line =~ /[\+\-]VC([A-Z])*/ ) )
703 {
704 my ($conditionstext, $conditions1, $conditions2, $intensity) =
705 6 translate_weather($Line, $Self->{conditionstext}, $Self->{conditions1},$Self->{conditions2});
706 6 ▶ $Self->{conditionstext} = $conditionstext if defined $conditionstext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conditionstext 6 0 6
707 6 ▶ $Self->{conditions1} = $conditions1 if defined $conditions1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conditions1 6 0 6
708 6 ▶ $Self->{conditions2} = $conditions2 if defined $conditions2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conditions2 6 0 6
709 6 ▶ $Self->{intensity} = $intensity if defined $intensity;
Branch: 1 branch
BranchTrueFalseTotal
if defined $intensity 4 2 6
710 }
711  
712 #------------------------------------------------------------------------------
713 # Cloud Cover
714 #------------------------------------------------------------------------------
715  
716 ▶ elsif (( $Line =~ /^(VV[0-9])/ )
Condition72%13/18
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/ or $Line =~ /^(SCT[0-9])/ or $Line =~ /^(BKN[0-9])/ or $Line =~ /^(OVC[0-9])/
l!l&&r!l&&!r
5652
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/ or $Line =~ /^(SCT[0-9])/ or $Line =~ /^(BKN[0-9])/
l!l&&r!l&&!r
4158
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/ or $Line =~ /^(SCT[0-9])/
l!l&&r!l&&!r
2259
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/
l!l&&r!l&&!r
0261
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/
l!l&&r!l&&!r
0063
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/
l!l&&r!l&&!r
0063
MC/DC57%4/7
$Line =~ /^(VV[0-9])/ or $Line =~ /^(SKC[0-9])/ or $Line =~ /^(CLR)/ or $Line =~ /^(FEW)/ or $Line =~ /^(SCT[0-9])/ or $Line =~ /^(BKN[0-9])/ or $Line =~ /^(OVC[0-9])/
$Line =~ /^(VV[0-9])/ $Line =~ /^(SKC[0-9])/ $Line =~ /^(CLR)/ $Line =~ /^(FEW)/ $Line =~ /^(SCT[0-9])/ $Line =~ /^(BKN[0-9])/ $Line =~ /^(OVC[0-9])/
Truth table62%5/8
A or B or C or D or E or F or G
A= $Line =~ /^(VV[0-9])/B= $Line =~ /^(SKC[0-9])/C= $Line =~ /^(CLR)/D= $Line =~ /^(FEW)/E= $Line =~ /^(SCT[0-9])/F= $Line =~ /^(BKN[0-9])/G= $Line =~ /^(OVC[0-9])/
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
717 || ( $Line =~ /^(SKC[0-9])/ )
718 || ( $Line =~ /^(CLR)/ )
719 || ( $Line =~ /^(FEW)/ )
720 || ( $Line =~ /^(SCT[0-9])/ )
721 || ( $Line =~ /^(BKN[0-9])/ )
722 || ( $Line =~ /^(OVC[0-9])/ ) )
723 {
724  
725 11 push( @Cloudlevels, $Line );
726  
727 11 ▶ if ( $Line =~ /^(CLR)/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($Line =~ /^(CLR)/) { } 0 11 11
elsif ($Line =~ /^(FEW)/) { } 2 9 11
elsif ($Line =~ /^(SCT[0-9])/) { } 2 7 9
elsif ($Line =~ /^(BKN[0-9])/) { } 1 6 7
elsif ($Line =~ /^(OVC[0-9])/) { } 6 0 6
728 0 $Self->{cloudcover} = "Clear";
729 }
730 elsif ( $Line =~ /^(FEW)/ ) {
731 2 $Self->{cloudcover} = "Fair";
732 }
733 elsif ( $Line =~ /^(SCT[0-9])/ ) {
734 2 $Self->{cloudcover} = "Partly Cloudy";
735 }
736 elsif ( $Line =~ /^(BKN[0-9])/ ) {
737 1 $Self->{cloudcover} = "Mostly Cloudy";
738 }
739 elsif ( $Line =~ /^(OVC[0-9])/ ) {
740 6 $Self->{cloudcover} = "Cloudy";
741 }
742  
743 11 ▶ if ( !$Self->{conditionstext} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Self->{"conditionstext"}) 2 9 11
744 2 $Self->{conditionstext} = $Self->{cloudcover};
745 }
746 }
747  
748 #------------------------------------------------------------------------------
749 # Get the temperature/dewpoint and calculate windchill/heat index
750 #------------------------------------------------------------------------------
751  
752 ▶ elsif (( $Line =~ /^([0-9][0-9]\/[0-9][0-9])/ )
Condition66%6/9
$Line =~ m[^([0-9][0-9]/[0-9][0-9])] or $Line =~ m[^(M[0-9][0-9]/)] or $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])] or $Line =~ m[^([0-9][0-9]/M[0-9][0-9])]
l!l&&r!l&&!r
8044
$Line =~ m[^([0-9][0-9]/[0-9][0-9])] or $Line =~ m[^(M[0-9][0-9]/)] or $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])]
l!l&&r!l&&!r
8044
$Line =~ m[^([0-9][0-9]/[0-9][0-9])] or $Line =~ m[^(M[0-9][0-9]/)]
l!l&&r!l&&!r
8044
MC/DC25%1/4
$Line =~ m[^([0-9][0-9]/[0-9][0-9])] or $Line =~ m[^(M[0-9][0-9]/)] or $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])] or $Line =~ m[^([0-9][0-9]/M[0-9][0-9])]
$Line =~ m[^([0-9][0-9]/[0-9][0-9])] $Line =~ m[^(M[0-9][0-9]/)] $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])] $Line =~ m[^([0-9][0-9]/M[0-9][0-9])]
Truth table40%2/5
A or B or C or D
A= $Line =~ m[^([0-9][0-9]/[0-9][0-9])]B= $Line =~ m[^(M[0-9][0-9]/)]C= $Line =~ m[^(M[0-9][0-9]/M[0-9][0-9])]D= $Line =~ m[^([0-9][0-9]/M[0-9][0-9])]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
753 || ( $Line =~ /^(M[0-9][0-9]\/)/ )
754 || ( $Line =~ /^(M[0-9][0-9]\/M[0-9][0-9])/ )
755 || ( $Line =~ /^([0-9][0-9]\/M[0-9][0-9])/ ) )
756 {
757 8 my @Splitter = split( /\//, $Line );
758 8 my $Temperature = $Splitter[0];
759 8 my $Dewpoint = $Splitter[1];
760  
761 8 ▶ if ( $Temperature =~ /M/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Temperature =~ /M/) 0 8 8
762 0 $Temperature =~ tr/[A-Z]//d;
763 0 $Temperature = ( $Temperature - ( $Temperature * 2 ) );
764 }
765  
766 8 ▶ if ( $Dewpoint =~ /M/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dewpoint =~ /M/) 0 8 8
767 0 $Dewpoint =~ tr/[A-Z]//d;
768 0 $Dewpoint = ( $Dewpoint - ( $Dewpoint * 2 ) );
769 }
770  
771 8 my $Tempf = convert_c_to_f( $Temperature );
772 8 my $Dewf = convert_c_to_f( $Dewpoint );
773  
774 8 my $Es =
775 6.11 * 10.0**( 7.5 * $Temperature / ( 237.7 + $Temperature ) );
776 8 my $E = 6.11 * 10.0**( 7.5 * $Dewpoint / ( 237.7 + $Dewpoint ) );
777 8 my $rh = round( ( $E / $Es ) * 100 );
778  
779 8 my $F = $Tempf;
780  
781 8 my $Heati = heat_index( $F, $rh );
782 8 my $Heatic = convert_f_to_c( $Heati );
783  
784 8 $Tempf = round($Tempf);
785 8 $Dewf = round($Dewf);
786 8 $Heati = round($Heati);
787 8 $Heatic = round($Heatic);
788  
789 8 my $Windc = windchill( $F, $Self->{windspeedmph} );
790 8 my $Windcc = convert_f_to_c( $Windc );
791 8 $Windc = round($Windc);
792 8 $Windcc = round($Windcc);
793  
794 8 $Self->{temperature_c} = $Temperature;
795 8 $Self->{temperature_f} = $Tempf;
796 8 $Self->{dewpoint_c} = $Dewpoint;
797 8 $Self->{dewpoint_f} = $Dewf;
798 8 $Self->{relative_humidity} = $rh;
799 8 $Self->{heat_index_c} = $Heatic;
800 8 $Self->{heat_index_f} = $Heati;
801 8 $Self->{windchill_c} = $Windcc;
802 8 $Self->{windchill_f} = $Windc;
803 }
804  
805 #------------------------------------------------------------------------------
806 # Calculate the atmospheric pressure in different formats.
807 # Based on report (inches of mercury)
808 #------------------------------------------------------------------------------
809  
810 elsif ( $Line =~ /^(A[0-9]{4})/ ) {
811 7 $Line =~ tr/[A-Z]//d;
812 7 my $Part1 = substr( $Line, 0, 2 );
813 7 my $Part2 = substr( $Line, 2, 4 );
814 7 $Self->{pressure_inhg} = "$Part1.$Part2";
815  
816 7 my $mb = $Self->{pressure_inhg} * 33.8639;
817 7 my $mmHg = $Self->{pressure_inhg} * 25.4;
818 7 my $lbin = ( $Self->{pressure_inhg} * 0.491154 );
819 7 my $kgcm = ( $Self->{pressure_inhg} * 0.0345316 );
820 7 $mb = round($mb);
821 7 $mmHg = round($mmHg);
822  
823 7 $Self->{pressure_mb} = $mb;
824 7 $Self->{pressure_mmhg} = $mmHg;
825 7 $Self->{pressure_lbin} = $lbin;
826 7 $Self->{pressure_kgcm} = $kgcm;
827 }
828  
829 #------------------------------------------------------------------------------
830 # Calculate the atmospheric pressure in different formats.
831 # Based on report (millibars)
832 #------------------------------------------------------------------------------
833  
834 elsif ( $Line =~ /^(Q[0-9]{4})/ ) {
835 1 $Line =~ tr/[A-Z]//d;
836 1 $Self->{pressure_mb} = $Line;
837  
838 1 my $inhg = ( $Self->{pressure_mb} * 0.02953 );
839 1 $Self->{pressure_inhg} = sprintf( "%.2f", $inhg );
840 1 my $mmHg = $Self->{pressure_inhg} * 25.4;
841 1 my $lbin = ( $Self->{pressure_inhg} * 0.491154 );
842 1 my $kgcm = ( $Self->{pressure_inhg} * 0.0345316 );
843 1 $mmHg = round($mmHg);
844  
845 1 $Self->{pressure_mmhg} = $mmHg;
846 1 $Self->{pressure_lbin} = $lbin;
847 1 $Self->{pressure_kgcm} = $kgcm;
848 }
849  
850 #------------------------------------------------------------------------------
851 # If the remarks section is starting, we are done
852 #------------------------------------------------------------------------------
853  
854 elsif ( $Line =~ /^(RMK)/ ) {
855 7 last;
856 }
857 }
858  
859 #------------------------------------------------------------------------------
860 # Read the remarks into an array for later processing
861 #------------------------------------------------------------------------------
862  
863 8 my $Remarks = 0;
864 8 my @Remarkarray;
865  
866 8 foreach my $Line (@Splitter) {
867 116 ▶ if ( $Line =~ /^(RMK)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Line =~ /^(RMK)/) 7 109 116
868 7 $Remarks = 1;
869 }
870  
871 116 ▶ if ($Remarks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Remarks) 23 93 116
872 23 push( @Remarkarray, $Line );
873 }
874 }
875  
876 #------------------------------------------------------------------------------
877 # Delete the temp file
878 #------------------------------------------------------------------------------
879  
880 8 $Self->{cloudlevel_arrayref} = \@Cloudlevels;
881 8 $Self->{station_type} = "Manual";
882  
883 #------------------------------------------------------------------------------
884 # Now we process remarks. These aren't all going to be in the report,
885 # and usually aren't. This has made it hard to develop. This section
886 # is basically incomplete, but you can get some of the data out
887 #------------------------------------------------------------------------------
888  
889 8 foreach my $Remark (@Remarkarray) {
890 23 ▶ if ($Remark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Remark) 23 0 23
891 23 my $Line = $Remark;
892  
893 23 ▶ if ( $Remark =~ /^AO[1-2]/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($Remark =~ /^AO[1-2]/) { } 1 22 23
elsif ($Remark =~ /^SLP/) { } 7 15 22
elsif ($Remark =~ /^TS/) { } 0 15 15
elsif ($Remark =~ /^5[0-9]/) { } 1 14 15
elsif ($Remark =~ /\$/) { } 0 14 14
elsif ($Remark =~ /^P[0-9]/) { } 0 14 14
elsif ($Line =~ /^(BRB|TSB|MIB|PRB|BCB|DRB|BLB|SHB|FZB|DZB|RAB|SNB|SGB|ICB|PEB|GRB|GSB|UPB|FGB|FUB|VAB|DUB|SAB|HZB|PYB|POB|SQB|FCB|SSB)/) { } 0 14 14
elsif ($Remark =~ /^T[0-9]/) { } 7 7 14
894 1 $Self->{station_type} = "Automated";
895 }
896 elsif ( $Remark =~ /^SLP/ ) {
897 7 $Remark =~ tr/[A-Z]//d;
898  
899 7 ▶ if ( !defined $Remark || $Remark eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Remark or $Remark eq "") 0 7 7
Condition33%1/3
not defined $Remark or $Remark eq ""
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $Remark or $Remark eq ""
not defined $Remark $Remark eq ""
Truth table33%1/3
not(A) or B
A= not defined $RemarkB= $Remark eq ""
ABresult
1X1
011
000
900 0 $Remark = 0;
901 }
902  
903 7 ▶ if ( ($Remark) && ( $Remark >= 800 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Remark and $Remark >= 800) { } 0 7 7
Condition33%1/3
$Remark and $Remark >= 800
!ll&&!rl&&r
070
MC/DC0%0/2
$Remark and $Remark >= 800
$Remark $Remark >= 800
Truth table33%1/3
A and B
A= $RemarkB= $Remark >= 800
ABresult
0X0
100
111
904 0 $Remark = $Remark * .1;
905 0 $Remark = $Remark + 900;
906 }
907 else {
908 7 $Remark = $Remark * .1;
909 7 $Remark = $Remark + 1000;
910 }
911  
912 7 $Self->{slp_inhg} = ( $Remark * 0.0295300 );
913 7 $Self->{slp_inhg} = substr( $Self->{slp_inhg}, 0, 5 );
914 7 $Self->{slp_mmhg} = round( $Remark * 0.750062 );
915 7 $Self->{slp_lbin} = ( $Remark * 0.0145038 );
916 7 $Self->{slp_kgcm} = ( $Remark * 0.00101972 );
917 7 $Self->{slp_mb} = round($Remark);
918 }
919  
920 #------------------------------------------------------------------------------
921 # Thunderstorm info
922 #------------------------------------------------------------------------------
923  
924 elsif ( $Remark =~ /^TS/ ) {
925 0 $Self->{storm} = $Remark;
926 }
927  
928 #------------------------------------------------------------------------------
929 # Three hour pressure tendency
930 #------------------------------------------------------------------------------
931  
932 elsif ( $Remark =~ /^5[0-9]/ ) {
933 1 $Self->{thpressure} = $Remark;
934 }
935  
936 #------------------------------------------------------------------------------
937 # Automated station needs maintenance
938 #------------------------------------------------------------------------------
939  
940 elsif ( $Remark =~ /\$/ ) {
941 0 $Self->{maintenance} = $Remark;
942 }
943  
944 #------------------------------------------------------------------------------
945 # Precipitation since last report (100ths of an inch)
946 #------------------------------------------------------------------------------
947  
948 elsif ( $Remark =~ /^P[0-9]/ ) {
949 0 $Self->{precipslr} = $Remark;
950 }
951  
952 #------------------------------------------------------------------------------
953 # Event beginning or ending
954 #------------------------------------------------------------------------------
955  
956 elsif ( $Line =~ /^(BRB|TSB|MIB|PRB|BCB|DRB|BLB|SHB|FZB|DZB|RAB|SNB|SGB|ICB|PEB|GRB|GSB|UPB|FGB|FUB|VAB|DUB|SAB|HZB|PYB|POB|SQB|FCB|SSB)/ )
957 {
958 0 $Self->{eventbe} = $Remark;
959 }
960  
961 #------------------------------------------------------------------------------
962 # Precise temperature reading
963 #------------------------------------------------------------------------------
964  
965 elsif ( $Remark =~ /^T[0-9]/ ) {
966 7 $Self->{ptemperature} = $Remark;
967 }
968 }
969 }
970  
971 8 my $Templatefile = $Self->{tfile};
972  
973 8 ▶ if ($Templatefile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Templatefile) 0 8 8
974 0 local $/; # enable slurp mode
975 0 ▶ open my $F, '<', $Templatefile or
Branch: 1 branch
BranchTrueFalseTotal
open my $F, "<", $Templatefile or croak("error opening template file ${Templatefile}: $!") 0 0 0
976 croak "error opening template file $Templatefile: $!";
977 0 my $tout = <$F>;
978 0 close($F);
979  
980 0 $tout =~ s{ %% ( .*? ) %% }
981 { exists( $Self->{$1} )
982 0 ▶ ? $Self->{$1}
Branch: 1 branch
BranchTrueFalseTotal
exists $Self->{$1} ? : 0 0 0
983 : ""
984 }gsex;
985  
986 0 $Self->{templateout} = $tout;
987 }
988  
989 8 $Self->{remark_arrayref} = \@Remarkarray;
990 8 return $Self;
991 }
992  
993 6 1;
994 __END__
↑ Summary