blib/lib/Geo/WeatherNOAA.pm

filter: Statementstmt 45.7% 133 / 291 Branchbran 10.1% 12 / 118 Conditioncond 22.8% 13 / 57 MC/DCmcdc 3.0% 1 / 33 Subroutinesub 85.0% 17 / 20 Podpod 53.8% 7 / 13 totaltotal 34.4% 183 / 532 CC 94 SCAR 48.3
CC
94
Cov
34%
get_city_hourly
262.3
process_city_zone
219.2
get_city_zone
138.2
CRAP 124.8
↑ Summary
1  
2 # $Id: WeatherNOAA.pm,v 4.38 2006/12/10 21:58:11 msolomon Exp $
3  
4  
5 package Geo::WeatherNOAA;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15  
16 1 @ISA = qw(Exporter);
17 # Items to export into callers namespace by default. Note: do not export
18 # names by default without a very good reason. Use EXPORT_OK instead.
19 # Do not simply export all your public functions/methods/constants.
20 1 @EXPORT = qw(
21 make_noaa_table
22  
23 print_forecast
24 print_current
25  
26 get_city_zone
27 process_city_zone
28  
29 get_city_hourly
30 process_city_hourly
31 );
32  
33 1 $VERSION = do { my @r = (q$Revision: 4.38 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
34 # my $URL_BASE = 'http://iwin.nws.noaa.gov/iwin/';
35 1 my $URL_BASE = 'http://www.weather.gov/view/prodsByState.php?';
36 # state=ia&prodtype=zone
37  
38 1 ▶use vars '$proxy_from_env';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 $proxy_from_env = 0;
40  
41 # Preloaded methods go here.
42  
43 0 ▶sub print_forecast {
Subroutine print_forecast: not called CC=3 SCAR=24.8
Pod documented
44 0 my ($city, $state, $filename, $fileopt, $UA) = @_;
45 0 my $in = get_city_zone($city,$state,$filename,$fileopt,$UA);
46  
47 0 my $out;
48  
49 0 $out = "Geo::WeatherNOAA.pm v.$Geo::WeatherNOAA::VERSION\n";
50  
51 0 my ($date,$warnings,$forecast) =
52 process_city_zone($city,$state,$filename,$fileopt);
53  
54 0 $out .= "As of $date:\n";
55 0 foreach my $warning (@$warnings) {
56 0 $out .= wrap('WARNING: ',' ',"$warning\n");
57 }
58 0 foreach my $key (keys %$forecast) {
59 0 $out .= wrap('',' ',"$key: $forecast->{$key}\n");
60 }
61 0 return $out
62 }
63  
64  
65 #########################################################################
66 #########################################################################
67 #
68 # Zone file processing
69 #
70 #########################################################################
71 #########################################################################
72 2 ▶sub process_city_zone {
Subroutine process_city_zone: called CC=20 SCAR=53.9
Pod documented
73 2 my ($city, $state, $filename, $fileopt, $UA) = @_;
74 2 my $in = get_city_zone($city,$state,$filename,$fileopt,$UA);
75  
76 # Return error if problem getting URL
77 2 ▶ if ($in =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /Error/) 0 2 2
78 0 my %error;
79 my @null;
80 0 $error{'Error'} = 'Error';
81 0 $error{'Network Error'} = $in;
82 0 return ('',\@null,\%error);
83 }
84 #print STDERR $in;exit;
85  
86 # Split coverage, date, and forecast
87 #
88 2 my ($coverage, $date, $forecast) = ($in =~ /(^.*?)\012 # Coverage
89 (\d.*?)\012 # Date
90 (.*)/sx); # Entire Forecast
91
92 # Format Coverage
93 #
94 2 $coverage =~ s/corrected//gi; # Remove stat word
95 2 $coverage =~ s/(\/|-|\.\.\.)/, /g; # Turn weird punct to commas
96 2 $coverage =~ s/,\s*$//; # Remove last comma
97 2 $coverage = ucfirst_words($coverage); # Make caps correct
98
99 # Format date (easy)
100 #
101 2 $date = format_date($date);
102  
103 # Vars for forecast
104 #
105 2 my %forecast;
106 2 tie %forecast, "Tie::IxHash";
107 2 my @warnings;
108  
109 # Iterate through forecast and assign warnings to list or pairs to hash
110 #
111 my $forecast_item; # Used as place holder for line breaks of $value
112 2 my $warnings_done = 0; # Flag for warnings (Always at top of forcast)
113  
114 2 foreach my $line (split "\012",$forecast) {
115 # Be-gone if we've got temp data (will include parse for that later)
116 0 ▶ last if $line =~ /^\.</;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.</ 0 0 0
117  
118 0 my ($key,$value);
119 0 ($key,$value) = ($line =~ /(.*?)\.\.\.(.*)/);
120  
121 0 ▶ if (! $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 0 0 0
122 # If there's no value, this must be either a warning or
123 # a continutation of value-data
124 0 $key = $line;
125 }
126 0 ▶ next if ($key =~ /EXTENDED/);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /EXTENDED/ 0 0 0
127  
128  
129 0 ▶ $warnings_done = 1 if ( ($key) and ($value) );
Branch: 1 branch
BranchTrueFalseTotal
if $key and $value 0 0 0
Condition0%0/3
$key and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $value
$key $value
Truth table0%0/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
130 #print "WARN_DONE: $warnings_done\n";
131  
132 0 ▶ if ($warnings_done) {
Branch: 2 branches
BranchTrueFalseTotal
if ($warnings_done) { } 0 0 0
elsif (not $warnings_done) { } 0 0 0
133 0 ▶ if ( ($key =~ s/^\.//) and ($value) and ($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ s/^\.// and $value and $key) { } 0 0 0
elsif ($forecast_item) { } 0 0 0
Condition0%0/6
$key =~ s/^\.// and $value and $key
!ll&&!rl&&r
000
$key =~ s/^\.// and $value
!ll&&!rl&&r
000
MC/DC0%0/3
$key =~ s/^\.// and $value and $key
$key =~ s/^\.// $value $key
Truth table0%0/4
A and B and C
A= $key =~ s/^\.//B= $valueC= $key
ABCresult
0XX0
10X0
1100
1111
134 # Add VALUE to KEY (new key)
135 0 $key =~ s/^\.//;
136 0 $key = ucfirst_words($key);
137 0 $forecast_item = $key;
138 0 $forecast{$forecast_item} .= $value;
139 }
140 elsif ( $forecast_item ) {
141 # Add KEY (with data) to OLD KEY (FORECAST_ITEM)
142 0 $forecast{$forecast_item} .= ' ' . $key;
143 0 ▶ $forecast{$forecast_item} .= ', ' . $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
144 }
145 else {
146 # print "LINE IGNORED\n";
147 }
148 }
149 elsif (! $warnings_done ) {
150 0 ▶ if ( (!$key) and ($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $key and $value) { } 0 0 0
elsif ($key and not $value) { } 0 0 0
Condition0%0/3
not $key and $value
!ll&&!rl&&r
000
MC/DC0%0/2
not $key and $value
not $key $value
Truth table0%0/3
not(A) and B
A= not $keyB= $value
ABresult
0X0
100
111
151 0 $value = ucfirst lc $value;
152 0 push @warnings, $value;
153 }
154 ▶ elsif ( ($key) and (!$value) ) {
Condition0%0/3
$key and not $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key and not $value
$key not $value
Truth table0%0/3
A and not(B)
A= $keyB= not $value
ABresult
0X0
100
111
155 0 $key = ucfirst lc $key;
156 0 push @warnings, $key;
157 }
158 }
159 else {
160 # line ignored
161 }
162 }
163  
164 2 foreach my $key ( keys %forecast ) {
165 0 $forecast{$key} =~ tr/\012//d; # Remove newlines
166 #$forecast{$key} = lc($forecast{$key}); # No all CAPS
167 0 $forecast{$key} =~ s/\s+/ /g; # Rid of multi-spaces
168 0 $forecast{$key} = sent_caps($forecast{$key}); # Proper sentance caps
169 }
170  
171 2 return ($date,\@warnings,\%forecast,$coverage);
172  
173 } # process_city_zone()
174  
175  
176 2 ▶sub get_city_zone {
Subroutine get_city_zone: called CC=15 SCAR=49.3
Pod documented
177 2 my ($city, $state, $filename, $fileopt, $UA) = @_;
178  
179 # my $URL = $URL_BASE . lc $state . '/zone.html';
180 2 my $URL = $URL_BASE . 'state=' . lc($state) . '&prodtype=zone';
181
182  
183 # City and States must be capital
184 #
185 2 $state = uc($state);
186 2 $city = uc($city);
187  
188 # Declare some working vars
189 #
190 2 my ($rawData, $coverage);
191  
192 # Get data from filehandle object
193 #
194 2 $rawData = get_data($URL,$filename,$fileopt,$UA);
195
196 # clean raw data to remove leading whitespace (I hope this works) 2006-12-10
197 #$rawData =~ s#\n\s*\.#\n.#gm;
198  
199 #my $t1 = $rawData;
200 #$rawData =~ s#\r\n#\n#sg;
201 2 $rawData =~ s#\n\s+#\n#sg;
202  
203 2 $rawData =~ s#\$\$#ENDOFSECTION#sg;
204  
205 # print STDERR $rawData;
206 2 foreach my $line ($rawData =~ /\n(\w\wZ.*?)\012/sg) {
207 0 $line =~ s/\r//g;
208 0 $line =~ s/\n//g;
209 # print STDERR "HERE: " . $line . "\n";
210 0 my $pattern = "$line\\n(.*?)\\n(?:ENDOFSECTION|NNN)";
211 #print STDERR "PATTERN=" . $pattern . "<--\n";
212 0 my $section = ($rawData =~ /$pattern/sg)[0];
213 0 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 0 0 0
214 #print "\n\n$section\n";
215 #print " SIZE OF DATA: " . length($section) . "\n";
216 # Iterate though section and get coverage
217 0 my $coverage_ended = 0;
218 0 foreach my $line (split /\012/, $section) {
219 0 $line =~ tr/\015//d; # \r
220 0 ▶ $coverage .= $line . "\n" if (! $coverage_ended);
Branch: 1 branch
BranchTrueFalseTotal
unless $coverage_ended 0 0 0
221 0 ▶ if ($line !~ /^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\w/) 0 0 0
222 0 $coverage_ended = 1;
223 }
224 }
225 0 ▶ return $section if ( ($coverage =~ /$city/i) && ($section =~ /\d{4}/));
Branch: 1 branch
BranchTrueFalseTotal
if $coverage =~ /$city/i and $section =~ /\d{4}/ 0 0 0
Condition0%0/3
$coverage =~ /$city/i and $section =~ /\d{4}/
!ll&&!rl&&r
000
MC/DC0%0/2
$coverage =~ /$city/i and $section =~ /\d{4}/
$coverage =~ /$city/i $section =~ /\d{4}/
Truth table0%0/3
A and B
A= $coverage =~ /$city/iB= $section =~ /\d{4}/
ABresult
0X0
100
111
226 }
227 }
228 2 return "$city not found";
229  
230 # Return error if there's an error
231 0 ▶ if ($rawData =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rawData =~ /Error/) 0 0 0
232 0 return $rawData;
233 }
234  
235 # Find our city's data from all raw data
236 #
237 #foreach my $section ($rawData =~ /\012${state}Z.*? # StateZone
238 0 foreach my $section ($rawData =~ /\013\w\wZ.*? # StateZone
239 \012\013(.*?) # Data sect
240 \012\013(?:\$\$|NNN)/xsg) {
241 # print STDERR "\n\nSECTION:\n$section\n";
242 # Iterate though section and get coverage
243 0 my $coverage_ended = 0;
244 0 foreach my $line (split /\012/, $section) {
245 0 $line =~ tr/\015//d; # \r
246 0 ▶ $coverage .= $line . "\n" if (! $coverage_ended);
Branch: 1 branch
BranchTrueFalseTotal
unless $coverage_ended 0 0 0
247 0 ▶ if ($line !~ /^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\w/) 0 0 0
248 0 $coverage_ended = 1;
249 }
250 }
251 0 ▶ return $section if ( ($coverage =~ /$city/i) && ($section =~ /\d{4}/));
Branch: 1 branch
BranchTrueFalseTotal
if $coverage =~ /$city/i and $section =~ /\d{4}/ 0 0 0
Condition0%0/3
$coverage =~ /$city/i and $section =~ /\d{4}/
!ll&&!rl&&r
000
MC/DC0%0/2
$coverage =~ /$city/i and $section =~ /\d{4}/
$coverage =~ /$city/i $section =~ /\d{4}/
Truth table0%0/3
A and B
A= $coverage =~ /$city/iB= $section =~ /\d{4}/
ABresult
0X0
100
111
252 }
253 0 return "$city not found";
254 }
255  
256 ##############################################################################
257 ##############################################################################
258 ##
259 ## Html for Mark's Site
260 ##
261 ##############################################################################
262 ##############################################################################
263  
264 5 ▶sub font {
Subroutine font: called CC=3 SCAR=11.0
Pod undocumented
265 5 my $in = shift;
266 5 ▶ my $size = shift || 2;
Condition100%2/2
shift() || 2
l!l
14
MC/DC100%1/1
shift() || 2
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
267 5 ▶ my $font_face = $main::font_face || 'FACE="Helvetica, Lucida, Ariel"';
Condition50%1/2
$main::font_face || 'FACE="Helvetica, Lucida, Ariel"'
l!l
05
MC/DC0%0/1
$main::font_face || 'FACE="Helvetica, Lucida, Ariel"'
$main::font_face
Truth table50%1/2
A
A= $main::font_face
Aresult
11
00
268 5 return qq|<FONT SIZE="$size" $font_face>$in</FONT>|;
269 }
270  
271 1 ▶sub make_noaa_table {
Subroutine make_noaa_table: called CC=11 SCAR=25.9
Pod documented
272 1 my ($city, $state, $filename, $fileopt, $UA, $max_items) = @_;
273  
274 1 ▶ $fileopt ||= 'get';
Condition50%1/2
$fileopt ||= "get"
l!l
10
MC/DC0%0/1
$fileopt ||= "get"
$fileopt
Truth table50%1/2
A
A= $fileopt
Aresult
11
00
275 1 ▶ $max_items && $max_items--;
Branch: 1 branch
BranchTrueFalseTotal
$max_items and --$max_items 0 1 1
276 1 ▶ $max_items ||= 4;
Condition50%1/2
$max_items ||= 4
l!l
01
MC/DC0%0/1
$max_items ||= 4
$max_items
Truth table50%1/2
A
A= $max_items
Aresult
11
00
277
278 1 ▶ my $med_bg = $main::med_bg || '#ddddff';
Condition50%1/2
$main::med_bg || '#ddddff'
l!l
01
MC/DC0%0/1
$main::med_bg || '#ddddff'
$main::med_bg
Truth table50%1/2
A
A= $main::med_bg
Aresult
11
00
279 1 ▶ my $light_bg = $main::light_bg || '#eeeeff';
Condition50%1/2
$main::light_bg || '#eeeeff'
l!l
01
MC/DC0%0/1
$main::light_bg || '#eeeeff'
$main::light_bg
Truth table50%1/2
A
A= $main::light_bg
Aresult
11
00
280 1 ▶ my $font_face = $main::font_face || 'FACE="Helvetica, Lucida, Ariel"';
Condition50%1/2
$main::font_face || 'FACE="Helvetica, Lucida, Ariel"'
l!l
01
MC/DC0%0/1
$main::font_face || 'FACE="Helvetica, Lucida, Ariel"'
$main::font_face
Truth table50%1/2
A
A= $main::font_face
Aresult
11
00
281  
282 1 my $locfilename;
283 1 $locfilename = $filename . "_hourly";
284 1 my $current = process_city_hourly( $city,$state,$locfilename,$fileopt,$UA );
285
286 1 $locfilename = $filename . "_zone";
287 1 my ($date,$warnings,$forecast,$coverage) = process_city_zone( $city,$state,$locfilename,$fileopt,$UA);
288 1 my $cols = (keys %$forecast);
289 1 ▶ $cols = $max_items if $cols > $max_items;
Branch: 1 branch
BranchTrueFalseTotal
if $cols > $max_items 0 1 1
290 1 my $out;
291 1 $out .= qq|<TABLE WIDTH="100%" CELLPADDING=1>\n|;
292 1 $out .= qq|<!-- Current weather row -->\n|;
293 1 $out .= qq|<TR VALIGN=TOP><TD BGCOLOR="$med_bg">\n|;
294 1 $out .= font('Current') . "\n</TD>\n";
295 1 $out .= qq|<TD COLSPAN="$cols">|;
296 1 $out .= font($current) . "\n</TD></TR>\n";
297  
298 # Add one to make cols real width of table
299 #
300 1 $cols++;
301  
302 # Add warnings, if needed
303 #
304 1 ▶ if (@$warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$warnings) 0 1 1
305 0 $out .= qq|<!-- Warnings -->\n|;
306 0 foreach my $warning (@$warnings) {
307 0 $out .= qq|<TR BGCOLOR="#FF8389" ALIGN="CENTER">\n|;
308 0 $out .= qq|\t<TD COLSPAN="$cols">|;
309 0 $out .= qq|<FONT $font_face COLOR="#440000">\n|;
310 0 $out .= qq|\t$warning\n</TD></TR>\n|;
311 }
312 }
313  
314 # Iterate over the first $max_items items in forecast
315 #
316 1 my $bottom; # add this after the iteration;
317 1 $out .= qq|<TR VALIGN="TOP" BGCOLOR="$med_bg">\n|;
318 1 $bottom .= qq|<TR VALIGN="TOP">\n|;
319 1 foreach my $key ( (keys %$forecast)[0..($cols - 1)] ) {
320 #print STDERR "DEBUG: $key\n";
321 0 $out .= "\t<TD>" . font($key) . "</TD>\n";
322 0 $bottom .= "\t<TD>" . font($forecast->{$key}) . "</TD>\n";
323 }
324 1 $out .= "</TR>\n" . $bottom . "</TR>\n";
325
326 # Add coverage area
327 1 $out .= qq|<TR BGCOLOR="$light_bg" ALIGN="LEFT">\n|;
328 1 $out .= qq| <TD>| . font('Area') . qq|</TD>\n|;
329 1 $out .= qq| <TD COLSPAN="$cols">| . font($coverage,1) . qq|</TD>\n|;
330 1 $out .= qq|</TR>\n|;
331  
332 # Add credits
333 #
334 1 my $wx_cred = '<A HREF="http://www.noaa.gov">NOAA</A> forecast made ' .
335 "$date by " .
336 "<A HREF=\"http://www.seva.net/~msolomon/WeatherNOAA/dist/\">" .
337 "Geo::WeatherNOAA</A> V.$Geo::WeatherNOAA::VERSION";
338 1 $out .= qq|<TR BGCOLOR="$light_bg" ALIGN="CENTER">\n|;
339 1 $out .= qq|<TD COLSPAN="$cols">| . font($wx_cred) . "</TD></TR>\n";
340 1 $out .= qq|</TABLE>\n|;
341  
342  
343  
344 }
345  
346 ##############################################################################
347 ##############################################################################
348 ##
349 ## Misc funcs
350 ##
351 ##############################################################################
352 ##############################################################################
353  
354 3 ▶sub get_url {
Subroutine get_url: called CC=6 SCAR=20.2
Pod undocumented
355 3 my ($URL, $UA) = @_;
356  
357 3 ▶ $URL or die "No URL to get!";
Branch: 1 branch
BranchTrueFalseTotal
$URL or die "No URL to get!" 3 0 3
358  
359 # Create the useragent and get the data
360 #
361 3 ▶ if (! $UA) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($UA) 3 0 3
362 3 $UA = new LWP::UserAgent;
363 3 ▶ if ( $ENV{'HTTP_PROXY'} or $ENV{'http_proxy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_PROXY"} or $ENV{"http_proxy"}) 0 3 3
Condition33%1/3
$ENV{"HTTP_PROXY"} or $ENV{"http_proxy"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$ENV{"HTTP_PROXY"} or $ENV{"http_proxy"}
$ENV{"HTTP_PROXY"} $ENV{"http_proxy"}
Truth table33%1/3
A or B
A= $ENV{"HTTP_PROXY"}B= $ENV{"http_proxy"}
ABresult
1X1
011
000
364 0 $UA->env_proxy;
365 }
366 }
367 3 $UA->agent("WeatherNOAA/$VERSION");
368
369 # Create a request
370 3 my $req = new HTTP::Request GET => $URL;
371 3 my $res = $UA->request($req);
372 3 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 3 3
373 0 return $res->content;
374 }
375 else {
376 3 return;
377 }
378 } # getURL()
379  
380 3 ▶sub get_data {
Subroutine get_data: called CC=13 SCAR=43.3
Pod undocumented
381 3 my ($URL,$filename,$fileopt,$UA) = @_;
382  
383 3 ▶ $fileopt ||= 'get';
Condition50%1/2
$fileopt ||= "get"
l!l
30
MC/DC0%0/1
$fileopt ||= "get"
$fileopt
Truth table50%1/2
A
A= $fileopt
Aresult
11
00
384  
385 3 my $data; # Data
386  
387 3 ▶ if ( ($fileopt eq 'get') || ($fileopt eq 'save') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fileopt eq "get" or $fileopt eq "save") 3 0 3
Condition66%2/3
$fileopt eq "get" or $fileopt eq "save"
l!l&&r!l&&!r
120
MC/DC0%0/2
$fileopt eq "get" or $fileopt eq "save"
$fileopt eq "get" $fileopt eq "save"
Truth table66%2/3
A or B
A= $fileopt eq "get"B= $fileopt eq "save"
ABresult
1X1
011
000
388 3 ▶ print STDERR "Retrieving $URL\n" if $main::opt_v;
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_v 0 3 3
389 3 ▶ $data = get_url($URL,$UA) ||
Condition50%1/2
get_url($URL, $UA) || (return "Error getting data from $URL")
l!l
03
MC/DC0%0/1
get_url($URL, $UA) || (return "Error getting data from $URL")
get_url($URL, $UA)
Truth table50%1/2
A
A= get_url($URL, $UA)
Aresult
11
00
390 return "Error getting data from $URL";
391 0 ▶ if ( $fileopt eq 'save' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fileopt eq "save") 0 0 0
392 0 ▶ print STDERR "Writing $URL to $filename\n" if $main::opt_v;
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_v 0 0 0
393 0 ▶ open(OUT,">$filename") or die "Cannot create $filename";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$filename" or die "Cannot create $filename" 0 0 0
394 0 print OUT $data;
395 0 close OUT;
396 0 $fileopt = 'usefile';
397 }
398 }
399 0 ▶ if ( $fileopt eq 'usefile' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fileopt eq "usefile") 0 0 0
400 0 ▶ print STDERR "Reading data from $filename\n" if $main::opt_v;
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_v 0 0 0
401 0 ▶ open(FILE,$filename) or die "Cannot read $filename";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filename or die "Cannot read $filename" 0 0 0
402 0 ▶ while (<FILE>) { $data .= $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
403 }
404 0 return $data;
405 } # get_fh
406  
407 2 ▶sub format_date {
Subroutine format_date: called CC=1 SCAR=0.0
Pod undocumented
408 2 my $in = shift;
409 2 $in =~ s/^(\d+)(\d\d)\s(AM|PM)\s(\w+)\s(\w+)\s(\w+)\s0*(\d+)/$1:$2\L$3\E ($4) \u\L$5\E\E \u\L$6 $7,/;
410 2 $in =~ tr/\015//d; # \r
411 2 return $in;
412 }
413 0 ▶sub sent_caps {
Subroutine sent_caps: not called CC=1 SCAR=6.9
Pod undocumented
414 0 my $in = shift;
415 0 $in = ucfirst(lc($in));
416 0 $in =~ s/(\.\W+)(\w)/$1\U$2/g; # Proper sentance caps
417 0 return $in;
418 }
419  
420 2 ▶sub ucfirst_words {
Subroutine ucfirst_words: called CC=1 SCAR=0.0
Pod undocumented
421 2 my ($in) = @_;
422 2 return join " ", map ucfirst(lc($_)),(split /\s+/, $in);
423 }
424  
425 #########################################################################
426 #########################################################################
427 ##
428 ## Hourly city data
429 ##
430 #########################################################################
431 #########################################################################
432  
433 1 ▶sub get_city_hourly {
Subroutine get_city_hourly: called CC=20 SCAR=55.7
Pod documented
434 1 my ($city,$state,$filename,$fileopt,$UA) = @_;
435
436 # City and state in all caps please
437 #
438 1 $city = uc $city;
439 1 $state = uc $state;
440  
441 # work var
442 1 my ($fields,$line,$date,$time);
443
444 # Get data
445 #
446 #my $URL = $URL_BASE . lc $state . '/hourly.html';
447 1 my $URL = $URL_BASE . 'state=' . lc($state) . '&prodtype=hourly';
448 #print STDERR "Getting data from $URL\n";
449 1 my $data = get_data($URL,$filename,$fileopt,$UA);
450 1 my $datalength = length($data);
451 #print STDERR "Got data ($datalength)\n";
452  
453 # Return error if there's an error
454 1 ▶ if ($data =~ /Error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /Error/) 1 0 1
455 1 my %retHash;
456 1 $retHash{ERROR} = $data;
457 1 return \%retHash;
458 }
459  
460 0 $data =~ s/\015//g; # \r
461  
462 #print STDERR "LOOKING FOR: " . $city . "\n";
463  
464 # Get line for our city from Data
465 #
466 0 foreach (split /\012/, $data) {
467 0 chomp;
468 0 s/^\s*//;
469 0 ▶ $date = $_ if /^\s*(\d+)(\d\d)\s+(AM|PM)\s+(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(\d+)(\d\d)\s+(AM|PM)\s+(\w+)/ 0 0 0
470 0 ▶ $time = "$1:$2 $3" if (($1) && ($2) && ($3));
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $2 and $3 0 0 0
Condition0%0/6
$1 and $2 and $3
!ll&&!rl&&r
000
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/3
$1 and $2 and $3
$1 $2 $3
Truth table0%0/4
A and B and C
A= $1B= $2C= $3
ABCresult
0XX0
10X0
1100
1111
471 0 ▶ $fields = $_ if /^CITY/;
Branch: 1 branch
BranchTrueFalseTotal
if /^CITY/ 0 0 0
472 0 ▶ $line = $_ if /^$city\s/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$city\s/ 0 0 0
473  
474 #print STDERR "LINE: $line\n" if $line;
475
476 # Newest data seems to be at the top of the file
477 0 ▶ last if $line;
Branch: 1 branch
BranchTrueFalseTotal
if $line 0 0 0
478 }
479 0 $date = format_date($date);
480  
481 # Set pack strings
482 #
483 0 my $fields_pack_str;
484 my $values_pack_str;
485 0 ▶ if ( ($fields =~ /TMP/) and ($fields =~ /\sDP\s/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields =~ /TMP/ and $fields =~ /\sDP\s/) { } 0 0 0
Condition0%0/3
$fields =~ /TMP/ and $fields =~ /\sDP\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$fields =~ /TMP/ and $fields =~ /\sDP\s/
$fields =~ /TMP/ $fields =~ /\sDP\s/
Truth table0%0/3
A and B
A= $fields =~ /TMP/B= $fields =~ /\sDP\s/
ABresult
0X0
100
111
486 #print STDERR "NEW FORMAT!\n";
487 0 $fields_pack_str =
488 '@0 A15 @15 A9 @25 A3 @29 A2 @33 A2 @36 A8 @47 A5 @54 A7';
489 0 $values_pack_str =
490 '@0 A15 @15 A8 @24 A4 @28 A4 @32 A3 @36 A8 @46 A7 @53 A8';
491 }
492 else {
493 #print STDERR "OLD FORMAT!\n";
494 0 $fields_pack_str =
495 '@0 A15 @15 A9 @24 A5 @29 A5 @35 A4 @39 A8 @47 A8 @55 A8';
496 0 $values_pack_str =
497 '@0 A15 @15 A9 @24 A5 @29 A5 @34 A4 @39 A8 @47 A8 @55 A8';
498 }
499  
500 # unpack gives error of the string is smaller than the unpack string
501 0 ▶ $line .= ' ' x (64 - length($line)) if length($line) < 64;
Branch: 1 branch
BranchTrueFalseTotal
if length $line < 64 0 0 0
502
503 0 ▶ return { } unless ( ($line) && ($fields) ); # Return ref to empty hash
Branch: 1 branch
BranchTrueFalseTotal
unless $line and $fields 0 0 0
Condition0%0/3
$line and $fields
!ll&&!rl&&r
000
MC/DC0%0/2
$line and $fields
$line $fields
Truth table0%0/3
A and B
A= $lineB= $fields
ABresult
0X0
100
111
504  
505 0 my @fields;
506 0 ▶ push @fields, 'DATE', 'TIME', unpack $fields_pack_str, $fields if $fields;
Branch: 1 branch
BranchTrueFalseTotal
if $fields 0 0 0
507 #'@0 A15 @15 A9 @24 A5 @29 A5 @35 A4 @39 A8 @47 A8 @55 A8', $fields if $fields;
508 0 my @values;
509 0 push @values, $date, $time, unpack $values_pack_str, $line;
510 #print STDERR "$line\n";
511 #'@0 A15 @15 A9 @24 A5 @29 A5 @34 A4 @39 A8 @47 A8 @55 A8', $line;
512  
513
514  
515 0 ▶ return { } if $values[3] eq 'NOT AVBL'; # Return ref to empty hash
Branch: 1 branch
BranchTrueFalseTotal
if $values[3] eq "NOT AVBL" 0 0 0
516  
517 0 my %retValue;
518 0 foreach my $i (0..$#fields) {
519 # Convert odd fieldnames to standard
520 0 ▶ $fields[$i] = 'DEWPT' if $fields[$i] eq 'DP';
Branch: 1 branch
BranchTrueFalseTotal
if $fields[$i] eq "DP" 0 0 0
521 0 ▶ $fields[$i] = 'TEMP' if $fields[$i] eq 'TMP';
Branch: 1 branch
BranchTrueFalseTotal
if $fields[$i] eq "TMP" 0 0 0
522  
523 # Assign value
524 0 $retValue{$fields[$i]} = $values[$i];
525 }
526  
527 0 return \%retValue;
528  
529 } # get_city_hourly()
530  
531 0 ▶sub print_current {
Subroutine print_current: not called CC=1 SCAR=6.9
Pod documented
532 0 my ($city,$state,$filename,$fileopt,$UA) = @_;
533 0 my $in = process_city_hourly($city, $state, $filename, $fileopt,$UA);
534 0 return wrap('',' ',$in)
535 }
536  
537
538 1 ▶sub process_city_hourly {
Subroutine process_city_hourly: called CC=11 SCAR=46.6
Pod documented
539 1 my ($city,$state,$filename,$fileopt,$UA) = @_;
540 1 my $in = get_city_hourly($city, $state, $filename, $fileopt,$UA);
541  
542 1 $state = uc($state);
543  
544 1 ▶ return $in->{ERROR} if $in->{ERROR};
Branch: 1 branch
BranchTrueFalseTotal
if $in->{"ERROR"} 1 0 1
545 0 ▶ $in->{CITY} or return "No data available";
Branch: 1 branch
BranchTrueFalseTotal
$in->{"CITY"} or return "No data available" 0 0 0
546 0 $in->{CITY} = ucfirst_words($in->{CITY});
547
548 0 my %sky = (
549 'SUNNY' => 'sunny skies',
550 'MOSUNNY' => 'mostly sunny skies',
551 'PTSUNNY' => 'partly sunny skies',
552 'CLEAR' => 'clear weather',
553 'DRIZZLE' => 'a drizzle',
554 'CLOUDY' => 'cloudy skies',
555 'MOCLDY' => 'mostly cloudy skies',
556 'PTCLDY' => 'partly cloudy skies',
557 'LGT RAIN' => 'light rain',
558 'FRZ DRZL' => 'freezing drizzle',
559 'FLURRIES' => 'flurries',
560 'LGT SNOW' => 'light snow',
561 'SNOW' => 'snow',
562 'N/A' => 'N/A',
563 'NOT AVBL' => '*not available*',
564 'FAIR' => 'fair weather');
565  
566 # Format the wind direction and speed
567 #
568 0 my %compass = qw/N north S south E east W west/;
569 # my $direction = join '',map $compass{$_},split(/(\w)\d/g, $in->{WIND});
570 0 my $direction;
571 {
572 0 $direction = $in->{WIND};
573 0 $direction =~ s/(.*?)G.*/$1/; # Remove gusts
574 0 $direction =~ s/\d//g; # Remove digits
575 0 ▶ if ( $direction ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction) 0 0 0
576 0 $direction = $compass{$direction};
577 }
578 }
579 0 my ($speed) = ($in->{WIND} =~ /(\d+)/);
580 0 my ($gusts) = ($in->{WIND} =~ /G(\d+)/);
581  
582 0 ▶ if ($in->{WIND} eq 'CALM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->{'WIND'} eq 'CALM') { } 0 0 0
583 0 $in->{WIND} = 'calm';
584 }
585 else {
586 0 $in->{WIND} = "$direction at ${speed} mph";
587 0 ▶ $in->{WIND} .= ", gusts up to ${gusts} mph" if $gusts;
Branch: 1 branch
BranchTrueFalseTotal
if $gusts 0 0 0
588 }
589  
590 # Format relative humidity and ibarometric pressure
591 #
592 0 my $rh_pres;
593 0 ▶ if ($in->{RH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->{"RH"}) 0 0 0
594 0 $rh_pres = " The relative humidity was $in->{RH}\%";
595 }
596 0 ▶ if ($in->{PRES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->{"PRES"}) 0 0 0
597 0 my %rise_fall = qw/R rising S steady F falling/;
598 # my $direction = join '',map $rise_fall{$_},split(/\d(\w)/g, $in->{PRES});
599 0 my $direction;
600 {
601 0 $direction = $in->{PRES};
602 0 $direction = ( $direction =~ /.*(\w)$/ )[0];
603 0 ▶ if ( $direction ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction) 0 0 0
604 0 $direction = $rise_fall{$direction};
605 }
606 }
607 0 $in->{PRES} =~ tr/RSF//d;
608 0 ▶ if ($rh_pres) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_pres) { } 0 0 0
609 0 $rh_pres .= ", and b";
610 }
611 else {
612 0 $rh_pres .= " B";
613 }
614 0 $rh_pres .= "arometric pressure was $direction from $in->{PRES} in";
615 }
616 0 ▶ $rh_pres .= '.' if $rh_pres;
Branch: 1 branch
BranchTrueFalseTotal
if $rh_pres 0 0 0
617  
618 # Format output sentence
619 #
620 0 my $out;
621 0 $out = "At $in->{TIME}, $in->{CITY}, $state conditions were ";
622 0 $out .= $sky{$in->{'SKY/WX'}} . " ";
623 0 $out .= "at $in->{TEMP}&deg;F, wind was $in->{WIND}. $rh_pres\n";
624 0 return $out;
625  
626 } # process_city_hourly()
627  
628 # Autoload methods go after =cut, and are processed by the autosplit program.
629  
630 1 1;
631 __END__
↑ Summary