lib/Weather/GHCN/App/StationCounts.pm

filter: Statementstmt 89.8% 97 / 108 Branchbran 52.6% 20 / 38 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 76.0% 140 / 184 CC 27 SCAR 22.4
CC
27
Cov
77%
run
17.2
read_data
11.6
parse_active_range
7.9
CRAP 9.4
« blib/lib/Weather/GHCN/Common.pm ↑ Summary lib/Weather/GHCN/CountryCodes.pm »
1 # ghcn_station_counts.pl - Count stations in ghcn_fetch.pl station output
2
3 =head1 NAME
4
5 Weather::GHCN::App::StationCounts - Count stations in Weather::GHCN::Fetch station output
6  
7 =head1 VERSION
8  
9 version v0.0.002
10
11 =head1 SYNOPSIS
12
13 use Weather::GHCN::App::StationCounts;
14
15 Weather::GHCN::App::StationCounts->run( \@ARGV );
16
17 See ghcn_station_counts.pl -help for details.
18
19 =cut
20
21 ########################################################################
22 # Pragmas
23 ########################################################################
24 1 ▶use v5.18; # minimum for Object::Pad
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 package Weather::GHCN::App::StationCounts;
27
28 1 our $VERSION = 'v0.0.002';
29
30
31 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
33
34 ########################################################################
35 # Export
36 ########################################################################
37
38 1 require Exporter;
39
40 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
41
42 1 our @EXPORT = ( 'run' );
43
44 ########################################################################
45 # perlcritic rules
46 ########################################################################
47
48 ## no critic [ErrorHandling::RequireCarping]
49 ## no critic [References::ProhibitDoubleSigils];
50 ## no critic [Subroutines::ProhibitSubroutinePrototypes]
51
52
53 ########################################################################
54 # Libraries
55 ########################################################################
56 1 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Hash::Wrap {-lvalue => 1, -defined => 1, -as => '_wrap_hash'};
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
61
62 1 ▶use if $^O eq 'MSWin32', 'Win32::Clipboard';
Subroutine BEGIN: called CC=1 SCAR=0.0
63
64 ########################################################################
65 # Global delarations
66 ########################################################################
67
68 # is it ok to use Win32::Clipboard?
69 1 our $USE_WINCLIP = $^O eq 'MSWin32';
70
71 1 my $Opt;
72
73 ########################################################################
74 # Constants
75 ########################################################################
76
77 1 const my $EMPTY => q(); # empty string
78 1 const my $SPACE => q( ); # space character
79 1 const my $TAB => qq(\t); # tab character
80 1 const my $TRUE => 1; # perl's usual TRUE
81 1 const my $FALSE => not $TRUE; # a dual-var consisting of '' and 0
82
83 1 const my $RANGE_RE => qr{ \d{4} ( [-] \d{4} )? }xms;
84 1 const my $RANGELIST_RE => qr{ $RANGE_RE ( [,] $RANGE_RE )* }xms;
85 1 const my $FIXABLE_RE => qr{ \A [\d,-]{4} \Z }xms;
86
87 ########################################################################
88 # Script Mainline
89 ########################################################################
90 1 ▶__PACKAGE__->run( \@ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
91
92 #-----------------------------------------------------------------------
93
94 =head1 SUBROUTINES
95
96 =head2 run ($progname, $argv_aref)
97
98 Encapsulates the mainline logic so this module can be used in a test
99 script. An application script merely needs to use this module and
100 then call:
101
102 Weather::GHCN::App::StationCounts->run( \@ARGV );
103
104 See ghcn_station_counts.pl -help for details.
105
106 =cut
107
108 ▶sub run ($progname, $argv_aref) {
Subroutine run: called CC=11 SCAR=28.5
Pod documented
109
110 1 my %count;
111
112 1 $Opt = get_options($argv_aref);
113
114 1 my ( $output, $new_fh, $old_fh );
115 1 ▶ if ( $Opt->outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP) 0 1 1
Condition33%1/3
$Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP
!ll&&!rl&&r
100
MC/DC0%0/2
$Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP
$Opt->outclip $Weather::GHCN::App::StationCounts::USE_WINCLIP
Truth table33%1/3
A and B
A= $Opt->outclipB= $Weather::GHCN::App::StationCounts::USE_WINCLIP
ABresult
0X0
100
111
116 0 ▶ open $new_fh, '>', \$output
Branch: 1 branch
BranchTrueFalseTotal
open $new_fh, ">", \$output or die "Unable to open buffer for write" 0 0 0
117 or die 'Unable to open buffer for write';
118 0 $old_fh = select $new_fh; ## no critic (ProhibitOneArgSelect)
119 }
120
121 1 my @files = $argv_aref->@*;
122 1 ▶ @files = ('-') unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 1 1
123
124 1 foreach my $file (@files) {
125 1 my $fh;
126 1 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 1 1
127 0 $fh = *STDIN;
128 } else {
129 1 ▶ open ($fh, '<', $file) or die;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die 0 1 1
130 }
131
132 1 read_data( $fh, \%count );
133 }
134
135 1 say join "\t", qw(Year Decade Stn_Count);
136
137 1 foreach my $yr (sort { $a <=> $b } keys %count) {
138 154 my $stn_count = keys %{ $count{$yr} };
139 154 my $decade = (substr $yr, 0, 3) . '0s';
140 154 say sprintf "%3d\t%s\t%d", $yr, $decade, $stn_count;
141 }
142
143 WRAP_UP:
144 # send output to the Windows clipboard
145 1 ▶ if ( $Opt->outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP) 0 1 1
Condition33%1/3
$Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP
!ll&&!rl&&r
100
MC/DC0%0/2
$Opt->outclip and $Weather::GHCN::App::StationCounts::USE_WINCLIP
$Opt->outclip $Weather::GHCN::App::StationCounts::USE_WINCLIP
Truth table33%1/3
A and B
A= $Opt->outclipB= $Weather::GHCN::App::StationCounts::USE_WINCLIP
ABresult
0X0
100
111
146 0 Win32::Clipboard->new()->Set( $output );
147 0 select $old_fh;
148 }
149
150
151 1 return;
152 }
153
154 ########################################################################
155 # Script-specific Subroutines
156 ########################################################################
157
158 =head2 read_data( $fh, \%count )
159
160 From the file handle $fh, read a list of stations in the format
161 generated by Fetch.pm, and count the stations that were active in any
162 given year.
163
164 =cut
165
166 ▶sub read_data ($fh, $count_href) {
Subroutine read_data: called CC=11 SCAR=24.5
Pod documented
167 1 my $lineno = 0;
168
169 1 ▶ while ( my $data = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = readline $fh)) 14 0 14
170 14 chomp $data;
171 14 ▶ next if $data =~ m{ \A \s* \Z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ / \A \s* \Z /msux 2 12 14
172
173 12 my ($stnid, $co, $state, $active) = split m{\t}xms, $data;
174
175 12 $lineno++;
176 12 ▶ if ($lineno == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno == 1) 1 11 12
177 1 ▶ die '*E* invalid input data'
Branch: 1 branch
BranchTrueFalseTotal
unless $stnid eq "StationId" and $active eq "Active" 0 1 1
Condition33%1/3
$stnid eq "StationId" and $active eq "Active"
!ll&&!rl&&r
001
MC/DC0%0/2
$stnid eq "StationId" and $active eq "Active"
$stnid eq "StationId" $active eq "Active"
Truth table33%1/3
A and B
A= $stnid eq "StationId"B= $active eq "Active"
ABresult
0X0
100
111
178 unless $stnid eq 'StationId' and $active eq 'Active';
179 1 next;
180 }
181
182 11 ▶ last if not $active;
Branch: 1 branch
BranchTrueFalseTotal
unless $active 1 10 11
183
184 10 my @rangelist = parse_active_range($stnid, $active);
185
186 10 ▶ next unless @rangelist;
Branch: 1 branch
BranchTrueFalseTotal
unless @rangelist 0 10 10
187
188 10 foreach my $range (@rangelist) {
189 10 my ($from, $to) = split m{-}xms, $range;
190
191 10 ▶ $to //= $from;
Condition66%2/3
$to //= $from
l!l&&r!l&&!r
910
MC/DC0%0/2
$to //= $from
$to $from
Truth table66%2/3
A //= B
A= $toB= $from
ABresult
1X1
011
000
192
193 10 foreach my $yr ($from..$to) {
194 502 $count_href->{$yr}{$stnid}++;
195 }
196 }
197 }
198
199 1 return;
200 }
201
202 =head2 parse_active_range ($stnid, $active)
203
204 Sometime the active range in data retreived from the NOAA station
205 inventory is malformed. This routine tries to spot these malformed
206 ranges and fix them.
207
208 =cut
209
210 ▶sub parse_active_range ($stnid, $active) {
Subroutine parse_active_range: called CC=4 SCAR=20.7
Pod documented
211
212 10 ▶ if ( $active =~ m{ \A \d\d,\d\d\d,\d\d\d \Z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active =~ / \A \d\d,\d\d\d,\d\d\d \Z /msux) 0 10 10
213 # misplaced commas, but we can fix it
214 0 my $s = $active;
215 0 $s =~ s{ [,] }{}xmsg;
216 0 ▶ if ( $s =~ m{ (\d\d\d\d) (\d\d\d\d) }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ / (\d\d\d\d) (\d\d\d\d) /msux) 0 0 0
217 0 $active = $1 . '-' . $2;
218 }
219 }
220
221 10 ▶ if ( $active !~ m{ \A $RANGELIST_RE \Z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($active =~ / \A $RANGELIST_RE \Z /msux) 0 10 10
222 0 warn "*W* unrecognized range list at stnid $stnid: $active\n";
223 0 return;
224 }
225
226 10 my @rangelist = split ",", $active;
227
228 10 return @rangelist;
229 }
230
231
232 ########################################################################
233 # Script-standard Subroutines
234 ########################################################################
235
236 =head2 get_options ( \@argv )
237
238 get_options encapsulates everything we need to process command line
239 options, or to set options when invoking this script from a test script.
240
241 Normally it's called by passing a reference to @ARGV; from a test script
242 you'd set up a local array variable to specify the options.
243
244 By convention, you should set up a file-scoped lexical variable named
245 $Opt and set it in the mainline using the return value from this function.
246 Then all options can be accessed used $Opt->option notation.
247
248 =cut
249
250 ▶sub get_options ($argv_aref) {
Subroutine get_options: called CC=4 SCAR=14.1
Pod documented
251
252 1 my @options = (
253 'outclip', # output data to the Windows clipboard
254 'debug', # enable debug() statements on stderr
255 'help','usage|?', # help
256 );
257
258 1 my %opt;
259
260 # create a list of option key names by stripping the various adornments
261 1 my @keys = map { (split m{ [!+=:|] }xms)[0] } grep { !ref } @options;
262 # initialize all possible options to undef
263 1 @opt{ @keys } = ( undef ) x @keys;
264
265 1 ▶ GetOptionsFromArray($argv_aref, \%opt, @options)
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray($argv_aref, \%opt, @options) or pod2usage(2) 0 1 1
266 or pod2usage(2);
267
268 # Make %opt into an object and name it the same as what we usually
269 # call the global options object. Note that this doesn't set the
270 # global -- the script will have to do that using the return value
271 # from this function. But, what this does is allow us to call
272 # $Opt->help and other option within this function using the same
273 # syntax as what we use in the script. This is handy if you need
274 # to rename option '-foo' to '-bar' because you can do a find/replace
275 # on '$Opt->foo' and you'll get any instances of it here as well as
276 # in the script.
277
278 ## no critic [Capitalization]
279 ## no critic [ProhibitReusedNames]
280 1 my $Opt = _wrap_hash \%opt;
281
282 1 ▶ pod2usage(1) if $Opt->usage;
Branch: 1 branch
BranchTrueFalseTotal
if $Opt->usage 0 1 1
283 1 ▶ pod2usage(-verbose => 2) if $Opt->help;
Branch: 1 branch
BranchTrueFalseTotal
if $Opt->help 0 1 1
284
285
286 1 return $Opt;
287 }
288
289 1 1; # needed in case we import this as a module (e.g. for testing)
290
291 =head1 AUTHOR
292
293 Gary Puckering (jgpuckering@rogers.com)
294
295 =head1 LICENSE AND COPYRIGHT
296
297 Copyright 2022, Gary Puckering
298
299 =cut
300
« blib/lib/Weather/GHCN/Common.pm ↑ Summary lib/Weather/GHCN/CountryCodes.pm »