lib/Weather/GHCN/App/Extremes.pm

filter: Statementstmt 91.5% 151 / 165 Branchbran 68.7% 44 / 64 Conditioncond 42.4% 14 / 33 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 76.1% 233 / 306 CC 50 SCAR 29.0
CC
50
Cov
80%
run
46.6
read_data
13.5
report_extremes_per_year
8.5
CRAP 18.2
« blib/lib/Weather/GHCN/App/StationCounts.pm ↑ Summary blib/lib/Weather/GHCN/Common.pm »
1 # Weather::GHCN::Extremes.pm - analyze of extremes from Weather::GHCN::Fetch.pm output
2
3 =head1 NAME
4
5 Weather::GHCN::App::Extremes - Report temperature extremes from Weather::GHCN::Fetch output
6  
7 =head1 VERSION
8  
9 version v0.0.003
10
11 =head1 SYNOPSIS
12
13 use Weather::GHCN::App::Extremes;
14
15 Weather::GHCN::App::Extremes->run( \@ARGV );
16
17 See ghcn_extremes -help for details.
18
19 =cut
20
21 ########################################################################
22 # Pragmas
23 ########################################################################
24 1 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 package Weather::GHCN::App::Extremes;
27
28 1 our $VERSION = 'v0.0.003';
29
30 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
32
33 ########################################################################
34 # Export
35 ########################################################################
36
37 1 require Exporter;
38
39 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
40
41 1 our @EXPORT = ( 'run' );
42
43 ########################################################################
44 # perlcritic rules
45 ########################################################################
46
47 ## no critic [Subroutines::ProhibitSubroutinePrototypes]
48 ## no critic [References::ProhibitDoubleSigils];
49 ## no critic [ErrorHandling::RequireCarping]
50
51 ########################################################################
52 # Libraries
53 ########################################################################
54 1 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use Hash::Wrap {-lvalue => 1, -defined => 1, -as => '_wrap_hash'};
Subroutine BEGIN: called CC=1 SCAR=0.0
58
59 1 ▶use ControlBreak;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use List::Util qw(max min sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Set::IntSpan::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
62
63 # modules for Windows only
64 1 ▶use if $^O eq 'MSWin32', 'Win32::Clipboard';
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 ########################################################################
67 # Global delarations
68 ########################################################################
69
70 # is it ok to use Win32::Clipboard?
71 1 our $USE_WINCLIP = $^O eq 'MSWin32';
72
73 1 my $Opt;
74
75 my @ExtremeWaves;
76 1 my %Location;
77
78 ########################################################################
79 # Constants
80 ########################################################################
81
82 1 const my $EMPTY => q(); # empty string
83 1 const my $SPACE => q( ); # space character
84 1 const my $TAB => qq(\t); # tab character
85 1 const my $DASH => q(-); # dash character
86 1 const my $TRUE => 1; # perl's usual TRUE
87 1 const my $FALSE => not $TRUE; # a dual-var consisting of '' and 0
88
89 1 const my $DEFAULT_HOT_LIMIT => 30;
90 1 const my $DEFAULT_COLD_LIMIT => -20;
91 1 const my $DEFAULT_NDAYS => 5;
92
93 ########################################################################
94 # Script Mainline
95 ########################################################################
96
97 1 ▶__PACKAGE__->run( \@ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
98
99 #-----------------------------------------------------------------------
100 =head1 SUBROUTINES
101
102 =head2 run ( \@ARGV )
103
104 Invoke this subroutine, passing in a reference to @ARGV, in order to
105 perform an analysis of the heat or cold waves in the input data.
106
107 Input is from stdin, or from the files listed in @ARGV. Data should
108 contain tab-separated output if the format generated by:
109
110 ghcn_fetch -report id
111
112 The following columns are expected:
113
114 Year, Month, Day, Decade, S_Decade, S_Year, S_Qtr,
115 TMAX, TMIN, Tavg, Qflags, StationId, Location
116
117 Any other columns are ignored.
118
119 See ghnc_extremes.pl -help for details.
120
121 =cut
122
123 ▶sub run ($progname, $argv_aref) {
Subroutine run: called CC=19 SCAR=38.4
Pod documented
124
125 3 $Opt = get_options($argv_aref);
126
127 3 my @files = $argv_aref->@*;
128
129 3 ▶ my $limit = $Opt->limit //
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 0 2 2
Condition66%2/3
$Opt->limit // ($Opt->cold ? $DEFAULT_COLD_LIMIT : $DEFAULT_HOT_LIMIT)
l!l&&r!l&&!r
120
MC/DC0%0/2
$Opt->limit // ($Opt->cold ? $DEFAULT_COLD_LIMIT : $DEFAULT_HOT_LIMIT)
$Opt->limit ($Opt->cold ? $DEFAULT_COLD_LIMIT : $DEFAULT_HOT_LIMIT)
Truth table66%2/3
A // B
A= $Opt->limitB= ($Opt->cold ? $DEFAULT_COLD_LIMIT : $DEFAULT_HOT_LIMIT)
ABresult
1X1
011
000
130 ( $Opt->cold ? $DEFAULT_COLD_LIMIT
131 : $DEFAULT_HOT_LIMIT
132 );
133
134 3 ▶ my $ndays = $Opt->ndays // $DEFAULT_NDAYS;
Condition66%2/3
$Opt->ndays // $DEFAULT_NDAYS
l!l&&r!l&&!r
120
MC/DC0%0/2
$Opt->ndays // $DEFAULT_NDAYS
$Opt->ndays $DEFAULT_NDAYS
Truth table66%2/3
A // B
A= $Opt->ndaysB= $DEFAULT_NDAYS
ABresult
1X1
011
000
135
136 3 ▶ my $cmp_op = $Opt->cold ? '<=' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 1 2 3
137
138 3 my $years_set = Set::IntSpan::Fast->new;
139
140 3 my ( $output, $new_fh, $old_fh );
141 3 ▶ if ( $Opt->outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP) 0 3 3
Condition33%1/3
$Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP
!ll&&!rl&&r
300
MC/DC0%0/2
$Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP
$Opt->outclip $Weather::GHCN::App::Extremes::USE_WINCLIP
Truth table33%1/3
A and B
A= $Opt->outclipB= $Weather::GHCN::App::Extremes::USE_WINCLIP
ABresult
0X0
100
111
142 0 ▶ open $new_fh, '>', \$output
Branch: 1 branch
BranchTrueFalseTotal
open $new_fh, ">", \$output or die "Unable to open buffer for write" 0 0 0
143 or die 'Unable to open buffer for write';
144 0 $old_fh = select $new_fh; ## no critic (ProhibitOneArgSelect)
145 }
146
147 3 my @files = $argv_aref->@*;
148 3 ▶ @files = ('-') unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 3 3
149
150 3 foreach my $file (@files) {
151 3 my $fh;
152 3 ▶ if ($file eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '-') { } 0 3 3
153 0 $fh = *STDIN;
154 } else {
155 3 ▶ open ($fh, '<', $file) or die;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die 0 3 3
156 }
157
158 3 @ExtremeWaves = ();
159 3 %Location = ();
160
161 # controlling on bool is_extreme and alpha stnid, minor to major
162 3 my $cb = ControlBreak->new( '+XT', 'STNID', 'EOF' );
163
164 3 read_data( $fh, $cb, $limit );
165 }
166
167 3 my $years_href;
168 3 ▶ if ( $Opt->peryear ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->peryear) { } 1 2 3
169 1 $years_href = report_extremes_per_year($limit, $ndays, $cmp_op);
170 } else {
171 2 $years_href = report_extremes_daycounts($limit, $ndays, $cmp_op)
172 }
173
174 # generate lines for each year that was missing
175 3 ▶ if ($Opt->nogaps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->nogaps) 0 3 3
176 0 foreach my $stnid (keys $years_href->%*) {
177 0 my @years = sort keys $years_href->{$stnid}->%*;
178
179 0 my $s = Set::IntSpan::Fast->new( min(@years) .. max(@years) );
180 0 my $t = Set::IntSpan::Fast->new( @years );
181 0 my $gaps = $s->diff($t);
182 0 my $iter = $gaps->iterate_runs();
183
184 0 ▶ while ( my ( $from, $to ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 0 0 0
185 0 foreach my $yr ($from .. $to) {
186 0 say join $TAB, $stnid, $Location{$stnid}, $yr;
187 }
188 }
189 }
190 }
191
192 WRAP_UP:
193 # send output to the Windows clipboard
194 3 ▶ if ( $Opt->outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP) 0 3 3
Condition33%1/3
$Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP
!ll&&!rl&&r
300
MC/DC0%0/2
$Opt->outclip and $Weather::GHCN::App::Extremes::USE_WINCLIP
$Opt->outclip $Weather::GHCN::App::Extremes::USE_WINCLIP
Truth table33%1/3
A and B
A= $Opt->outclipB= $Weather::GHCN::App::Extremes::USE_WINCLIP
ABresult
0X0
100
111
195 0 Win32::Clipboard->new()->Set( $output );
196 0 select $old_fh;
197 }
198
199 3 return;
200 }
201
202 ########################################################################
203 # Script-specific Subroutines
204 ########################################################################
205
206 =head2 read_data ( $fh, $cb, $limit )
207
208 Read weather data from the filehandle and collect extreme waves
209 according to $limit and $Opt->cold (true for cold waves, false for
210 heat waves).
211
212 =cut
213
214 ▶sub read_data ( $fh, $cb, $limit ) {
Subroutine read_data: called CC=13 SCAR=26.0
Pod documented
215
216 3 my $extremes_begins;
217 my @extreme_days;
218 3 my $lineno;
219
220 3 ▶ while ( my $data = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = readline $fh)) 14076 0 14076
221 14076 chomp $data;
222 14076 ▶ next if $data eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $data eq $EMPTY 0 14076 14076
223 14076 ▶ last if $data =~ m{ \A Notes: }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ / \A Notes: /msux 0 14076 14076
224
225 14076 my ($year,$month,$day,$decade,$s_decade,$s_year,$s_qtr,$tmax,$tmin,$tavg,$qflags,$stnid,$loc ) = split $TAB, $data;
226
227 14076 $lineno++;
228 14076 ▶ if ($lineno == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno == 1) 3 14073 14076
229 3 ▶ die '*E* invalid input data: ' . $data
Branch: 1 branch
BranchTrueFalseTotal
unless $year eq "Year" and $tmax =~ / \A TMAX /msux and $tmin =~ / \A TMIN /msux 0 3 3
Condition33%2/6
$year eq "Year" and $tmax =~ / \A TMAX /msux and $tmin =~ / \A TMIN /msux
!ll&&!rl&&r
003
$year eq "Year" and $tmax =~ / \A TMAX /msux
!ll&&!rl&&r
003
MC/DC0%0/3
$year eq "Year" and $tmax =~ / \A TMAX /msux and $tmin =~ / \A TMIN /msux
$year eq "Year" $tmax =~ / \A TMAX /msux $tmin =~ / \A TMIN /msux
Truth table25%1/4
A and B and C
A= $year eq "Year"B= $tmax =~ / \A TMAX /msuxC= $tmin =~ / \A TMIN /msux
ABCresult
0XX0
10X0
1100
1111
230 unless $year eq 'Year' and $tmax =~ m{ \A TMAX }xms and $tmin =~ m{ \A TMIN }xms;
231 3 next;
232 }
233
234 14073 ▶ last unless $year =~ m{ \A \d{4} \Z }xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $year =~ / \A \d{4} \Z /msux 3 14070 14073
235
236 14070 my $ymd = sprintf '%04d-%02d-%02d', $year, $month, $day;
237
238 14070 ▶ my $value = $Opt->cold ? $tmin : $tmax;
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 4690 9380 14070
239
240 14070 ▶ next if not defined $value or $value eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq $EMPTY 243 13827 14070
Condition66%2/3
not defined $value or $value eq $EMPTY
l!l&&r!l&&!r
024313827
MC/DC50%1/2
not defined $value or $value eq $EMPTY
not defined $value $value eq $EMPTY
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq $EMPTY
ABresult
1X1
011
000
241
242 13827 $Location{$stnid} = $loc;
243
244 13827 ▶ my $is_extreme = $Opt->cold
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 4575 9252 13827
245 ? $value <= $limit
246 : $value >= $limit
247 ;
248
249 my $on_break = sub {
250 13827 ▶ if ( $is_extreme ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 2 branches
BranchTrueFalseTotal
if ($is_extreme) { } 1185 12642 13827
elsif ($cb->break('XT')) { } 545 12097 12642
251 1185 ▶ $extremes_begins = $ymd if $cb->break('XT');
Branch: 1 branch
BranchTrueFalseTotal
if $cb->break("XT") 544 641 1185
252 1185 push @extreme_days, [$ymd, $value, $stnid, $loc];
253 } elsif ($cb->break('XT')) {
254 545 push @ExtremeWaves, [$extremes_begins, [@extreme_days], $stnid, $loc ];
255 545 $extremes_begins = undef;
256 545 @extreme_days = ();
257 }
258 13827 ▶ if ($cb->break('STNID')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb->break("STNID")) 3 13824 13827
259 3 $extremes_begins = undef;
260 3 @extreme_days = ();
261 }
262 13827 };
263
264 13827 $cb->test_and_do($is_extreme, $stnid, eof, $on_break);
265 }
266
267 3 return;
268 }
269
270 =head2 report_extremes_daycounts ($limit, $ndays, $cmp_op)
271
272 Analyzes the input data lookin for $ndays consecutive days when
273 the temperature is beyond $limit. By default, heatwaves are examined.
274 If the option -cold is given, then cold waves are examined.
275
276 Returns a reference to a hash keyed on year, and which contains a
277 tab_separated line of text that includes the station id, location,
278 year, ymd the wave began, the number of days the wave lasted,
279 the average temperature during the wave, and the most extreme (hot
280 or cold) temperature during the wave.
281
282 =cut
283
284 ▶sub report_extremes_daycounts ($limit, $ndays, $cmp_op) {
Subroutine report_extremes_daycounts: called CC=6 SCAR=18.1
Pod documented
285 2 my %years;
286 my %location;
287
288 2 my $daycount_col_head = sprintf '%d-day waves %s %dC', $ndays, $cmp_op, $limit;
289 2 say join $TAB, 'StnId', 'Location', 'Year', 'YMD', $daycount_col_head, 'Avg C', 'Max C';
290
291 2 foreach my $xw_aref (@ExtremeWaves) {
292 338 my ($xw_begin, $xdays_aref, $stnid, $loc) = $xw_aref->@*;
293
294 338 ▶ $stnid //= $EMPTY;
Condition33%1/3
$stnid //= $EMPTY
l!l&&r!l&&!r
33800
MC/DC0%0/2
$stnid //= $EMPTY
$stnid $EMPTY
Truth table33%1/3
A //= B
A= $stnidB= $EMPTY
ABresult
1X1
011
000
295 338 ▶ $loc //= $EMPTY;
Condition33%1/3
$loc //= $EMPTY
l!l&&r!l&&!r
33800
MC/DC0%0/2
$loc //= $EMPTY
$loc $EMPTY
Truth table33%1/3
A //= B
A= $locB= $EMPTY
ABresult
1X1
011
000
296
297 338 my $count = scalar $xdays_aref->@*;
298
299 338 ▶ next if $count < $ndays;
Branch: 1 branch
BranchTrueFalseTotal
if $count < $ndays 280 58 338
300
301 58 my $year = substr $xw_begin, 0, 4; ## no critic [ProhibitMagicNumbers]
302
303 58 my @temps = map { $_->[1] } $xdays_aref->@*;
304 58 my $sum = sum(@temps);
305 58 ▶ my $extreme = $Opt->cold ? min(@temps) : max(@temps);
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 40 18 58
306 58 my $avg = sprintf '%0.1f', $sum / $count;
307 say join $TAB,
308 58 $stnid, $Location{$stnid}, $year, $xw_begin, $count, $avg, $extreme;
309 58 $years{$stnid}{$year}++;
310 }
311
312 2 return \%years;
313 }
314
315 =head2 report_extremes_per_year ($limit, $ndays, $cmp_op)
316
317 Analyzes the input data lookin for $ndays consecutive days when
318 the temperature is beyond $limit. By default, heatwaves are examined.
319 If the option -cold is given, then cold waves are examined.
320
321 Returns a reference to a hash keyed on year, and which contains a
322 tab_separated line of text that includes the station id, location,
323 year, and a count of the number of waves detected during that year.
324
325 =cut
326
327 ▶sub report_extremes_per_year ($limit, $ndays, $cmp_op) {
Subroutine report_extremes_per_year: called CC=8 SCAR=21.3
Pod documented
328 1 ▶ my $type = $Opt->cold ? 'Coldwaves' : 'Heatwaves';
Branch: 1 branch
BranchTrueFalseTotal
$Opt->cold ? : 0 1 1
329 1 my $title = sprintf '%d-day waves %s %dC', $ndays, $cmp_op, $limit;
330 1 say join $TAB, 'StnId', 'Location', 'Year', $title;
331
332 1 my %years;
333
334 1 foreach my $xw_aref (@ExtremeWaves) {
335 207 my ($xw_begin, $xdays_aref, $stnid, $loc) = $xw_aref->@*;
336 207 ▶ $stnid //= $EMPTY;
Condition33%1/3
$stnid //= $EMPTY
l!l&&r!l&&!r
20700
MC/DC0%0/2
$stnid //= $EMPTY
$stnid $EMPTY
Truth table33%1/3
A //= B
A= $stnidB= $EMPTY
ABresult
1X1
011
000
337 207 ▶ $loc //= $EMPTY;
Condition33%1/3
$loc //= $EMPTY
l!l&&r!l&&!r
20700
MC/DC0%0/2
$loc //= $EMPTY
$loc $EMPTY
Truth table33%1/3
A //= B
A= $locB= $EMPTY
ABresult
1X1
011
000
338 207 my ($year) = split $DASH, $xw_begin;
339 207 my $count = scalar $xdays_aref->@*;
340 207 ▶ next if $count < $ndays;
Branch: 1 branch
BranchTrueFalseTotal
if $count < $ndays 189 18 207
341 18 $years{$stnid}{$year}++;
342 }
343
344 1 foreach my $stnid ( sort keys %years ) {
345 2 foreach my $yr ( sort keys $years{$stnid}->%* ) {
346 12 say join $TAB, $stnid, $Location{$stnid}, $yr, $years{$stnid}{$yr};
347 }
348 }
349
350 1 return \%years;
351 }
352
353 ########################################################################
354 # Script-standard Subroutines
355 ########################################################################
356
357 =head2 get_options ( \@ARGV )
358
359 B<get_options> encapsulates everything we need to process command line
360 options, or to set options when invoking this script from a test script.
361
362 Normally it's called by passing a reference to @ARGV; from a test script
363 you'd set up a local array variable to specify the options.
364
365 By convention, you should set up a file-scoped lexical variable named
366 $Opt and set it in the mainline using the return value from this function.
367 Then all options can be accessed used $Opt->option notation.
368
369 =cut
370
371 ▶sub get_options ($argv_aref) {
Subroutine get_options: called CC=4 SCAR=14.1
Pod documented
372
373 3 my @options = (
374 'limit=i', # lower bound of extremes daily temperature
375 'ndays=i', # number of consecutive days needed to be a extremes
376 'peryear', # report number of heatwaves per year
377 'cold', # report coldwaves instead of heatwaves
378 'nogaps', # generate a line for missing years (for charting)
379 'outclip', # output data to the Windows clipboard
380 'help','usage|?', # help
381 );
382
383 3 my %opt;
384
385 # create a list of option key names by stripping the various adornments
386 3 my @keys = map { (split m{ [!+=:|] }xms)[0] } grep { !ref } @options;
387 # initialize all possible options to undef
388 3 @opt{ @keys } = ( undef ) x @keys;
389
390 3 ▶ GetOptionsFromArray($argv_aref, \%opt, @options)
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray($argv_aref, \%opt, @options) or pod2usage(2) 0 3 3
391 or pod2usage(2);
392
393 # Make %opt into an object and name it the same as what we usually
394 # call the global options object. Note that this doesn't set the
395 # global -- the script will have to do that using the return value
396 # from this function. But, what this does is allow us to call
397 # $Opt->help and other option within this function using the same
398 # syntax as what we use in the script. This is handy if you need
399 # to rename option '-foo' to '-bar' because you can do a find/replace
400 # on '$Opt->foo' and you'll get any instances of it here as well as
401 # in the script.
402
403 ## no critic [Capitalization]
404 ## no critic [ProhibitReusedNames]
405 3 my $Opt = _wrap_hash \%opt;
406
407 3 ▶ pod2usage(1) if $Opt->usage;
Branch: 1 branch
BranchTrueFalseTotal
if $Opt->usage 0 3 3
408 3 ▶ pod2usage(-verbose => 2) if $Opt->help;
Branch: 1 branch
BranchTrueFalseTotal
if $Opt->help 0 3 3
409
410 3 return $Opt;
411 }
412
413 1 1; # needed in case we import this as a module (e.g. for testing)
414
415 =head1 AUTHOR
416
417 Gary Puckering (jgpuckering@rogers.com)
418
419 =head1 LICENSE AND COPYRIGHT
420
421 Copyright 2022, Gary Puckering
422
423 =cut
« blib/lib/Weather/GHCN/App/StationCounts.pm ↑ Summary blib/lib/Weather/GHCN/Common.pm »