blib/lib/Geo/WeatherNWS.pm

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