blib/lib/Weather/GHCN/App/StationCounts.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 182 CC 15 SCAR 29.2
CC
15
Cov
0%
read_data
42.0
run
30.0
parse_active_range
20.0
CRAP 18.6
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.003
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 use v5.18; # minimum for Object::Pad
25
26 package Weather::GHCN::App::StationCounts;
27
28 our $VERSION = 'v0.0.003';
29
30
31 use feature 'signatures';
32 no warnings 'experimental::signatures';
33
34 ########################################################################
35 # Export
36 ########################################################################
37
38 require Exporter;
39
40 use base 'Exporter';
41
42 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 use Getopt::Long qw( GetOptionsFromArray );
57 use Pod::Usage;
58 use Const::Fast;
59 use Hash::Wrap {-lvalue => 1, -defined => 1, -as => '_wrap_hash'};
60 use Const::Fast;
61
62 use if $^O eq 'MSWin32', 'Win32::Clipboard';
63
64 ########################################################################
65 # Global delarations
66 ########################################################################
67
68 # is it ok to use Win32::Clipboard?
69 our $USE_WINCLIP = $^O eq 'MSWin32';
70
71 my $Opt;
72
73 ########################################################################
74 # Constants
75 ########################################################################
76
77 const my $EMPTY => q(); # empty string
78 const my $SPACE => q( ); # space character
79 const my $TAB => qq(\t); # tab character
80 const my $TRUE => 1; # perl's usual TRUE
81 const my $FALSE => not $TRUE; # a dual-var consisting of '' and 0
82
83 const my $RANGE_RE => qr{ \d{4} ( [-] \d{4} )? }xms;
84 const my $RANGELIST_RE => qr{ $RANGE_RE ( [,] $RANGE_RE )* }xms;
85 const my $FIXABLE_RE => qr{ \A [\d,-]{4} \Z }xms;
86
87 ########################################################################
88 # Script Mainline
89 ########################################################################
90 __PACKAGE__->run( \@ARGV ) unless caller;
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) {
109
110 my %count;
111
112 $Opt = get_options($argv_aref);
113
114 my ( $output, $new_fh, $old_fh );
115 if ( $Opt->outclip and $USE_WINCLIP ) {
116 open $new_fh, '>', \$output
117 or die 'Unable to open buffer for write';
118 $old_fh = select $new_fh; ## no critic (ProhibitOneArgSelect)
119 }
120
121 my @files = $argv_aref->@*;
122 @files = ('-') unless @files;
123
124 foreach my $file (@files) {
125 my $fh;
126 if ($file eq '-') {
127 $fh = *STDIN;
128 } else {
129 open ($fh, '<', $file) or die;
130 }
131
132 read_data( $fh, \%count );
133 }
134
135 say join "\t", qw(Year Decade Stn_Count);
136
137 foreach my $yr (sort { $a <=> $b } keys %count) {
138 my $stn_count = keys %{ $count{$yr} };
139 my $decade = (substr $yr, 0, 3) . '0s';
140 say sprintf "%3d\t%s\t%d", $yr, $decade, $stn_count;
141 }
142
143 WRAP_UP:
144 # send output to the Windows clipboard
145 if ( $Opt->outclip and $USE_WINCLIP ) {
146 Win32::Clipboard->new()->Set( $output );
147 select $old_fh;
148 }
149
150
151 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) {
167 my $lineno = 0;
168
169 while ( my $data = <$fh> ) {
170 chomp $data;
171 next if $data =~ m{ \A \s* \Z }xms;
172
173 my ($stnid, $co, $state, $active) = split m{\t}xms, $data;
174
175 $lineno++;
176 if ($lineno == 1) {
177 die '*E* invalid input data'
178 unless $stnid eq 'StationId' and $active eq 'Active';
179 next;
180 }
181
182 last if not $active;
183
184 my @rangelist = parse_active_range($stnid, $active);
185
186 next unless @rangelist;
187
188 foreach my $range (@rangelist) {
189 my ($from, $to) = split m{-}xms, $range;
190
191 $to //= $from;
192
193 foreach my $yr ($from..$to) {
194 $count_href->{$yr}{$stnid}++;
195 }
196 }
197 }
198
199 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) {
211
212 if ( $active =~ m{ \A \d\d,\d\d\d,\d\d\d \Z }xms ) {
213 # misplaced commas, but we can fix it
214 my $s = $active;
215 $s =~ s{ [,] }{}xmsg;
216 if ( $s =~ m{ (\d\d\d\d) (\d\d\d\d) }xms ) {
217 $active = $1 . '-' . $2;
218 }
219 }
220
221 if ( $active !~ m{ \A $RANGELIST_RE \Z }xms ) {
222 warn "*W* unrecognized range list at stnid $stnid: $active\n";
223 return;
224 }
225
226 my @rangelist = split ",", $active;
227
228 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) {
251
252 my @options = (
253 'outclip', # output data to the Windows clipboard
254 'debug', # enable debug() statements on stderr
255 'help','usage|?', # help
256 );
257
258 my %opt;
259
260 # create a list of option key names by stripping the various adornments
261 my @keys = map { (split m{ [!+=:|] }xms)[0] } grep { !ref } @options;
262 # initialize all possible options to undef
263 @opt{ @keys } = ( undef ) x @keys;
264
265 GetOptionsFromArray($argv_aref, \%opt, @options)
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 my $Opt = _wrap_hash \%opt;
281
282 pod2usage(1) if $Opt->usage;
283 pod2usage(-verbose => 2) if $Opt->help;
284
285
286 return $Opt;
287 }
288
289 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