lib/Weather/GHCN/App/Fetch.pm

filter: Statementstmt 45.5% 97 / 213 Branchbran 19.2% 15 / 78 Conditioncond 19.0% 8 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 77.2% 17 / 22 Podpod 100% 6 / 6 totaltotal 37.0% 143 / 386 CC 57 SCAR 63.4
CC
57
Cov
36%
run
975.7
get_user_options_tk
132.0
get_user_options
3.1
CRAP 569.5
↑ Summary blib/lib/Weather/GHCN/App/Fetch.pm »
1 # Weather::GHCN::Fetch.pm - class for creating applications that fetch NOAA GHCN data
2
3 =head1 NAME
4
5 Weather::GHCN::App::Fetch - Fetch station and weather data from the NOAA GHCN repository
6  
7 =head1 VERSION
8  
9 version v0.0.002
10
11 =head1 SYNOPSIS
12
13 use Weather::GHCN::App::Fetch;
14
15 Weather::GHCN::App::Fetch->run( \@ARGV );
16
17 See ghcn_fetch.pl -help for details.
18
19 =cut
20
21 # Testing notes:
22 #
23 # The quickest way to spot check results from this script is to compare them
24 # to those obtained from:
25 #
26 # https://ottawa.weatherstats.ca/charts/
27 #
28 # Run the script with parameters such as -prov ON -loc "Ottawa Int" -range
29 # 2017-2018 -precip -tavg -o first with the -daily option, then again with
30 # -monthly and -yearly. You can then compare results to various charts you
31 # generate using the above link by selecting Ottawa (Kanata - Orleans),
32 # which I've verified corresponds to station CA006105976 (Ottawa Int'l).
33 #
34 # Charts to use include Temperature (TMAX, TMIN, TAVG, Avg), Snowfall
35 # (SNOW), Snow on Ground (SNWD) and Total Precipitation (PRCP). Annual and
36 # monthly charts work well, but you may need daily charts and some
37 # investigation of the NOAA source data if there are anomalies. Sometimes
38 # the NOAA data has missing data; e.g. station CA006105976 (Ottawa Int'l)
39 # is missing days 6-28 for 2018-02.
40
41 ########################################################################
42 # Pragmas
43 ########################################################################
44 1 ▶use v5.18; # minimum for Object::Pad
Subroutine BEGIN: called CC=1 SCAR=0.0
45
46 package Weather::GHCN::App::Fetch;
47
48 1 our $VERSION = 'v0.0.002';
49
50 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
52
53 ########################################################################
54 # Export
55 ########################################################################
56
57 1 require Exporter;
58
59 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
60
61 1 our @EXPORT = ( 'run' );
62
63 ########################################################################
64 # perlcritic rules
65 ########################################################################
66
67 ## no critic [ErrorHandling::RequireCarping]
68 ## no critic [InputOutput::RequireBriefOpen]
69 ## no critic [ProhibitSubroutinePrototypes]
70
71 # due to subroutine signatures, perlcritic can't seem to handle disabling
72 # the following warnings on the subs where they occur
73 ## no critic [Subroutines::ProhibitExcessComplexity]
74
75 # due to use of postfix dereferencing, we have to disable these warnings
76 ## no critic [References::ProhibitDoubleSigils]
77
78 ########################################################################
79 # Libraries and Features
80 ########################################################################
81 1 ▶use Object::Pad 0.66 qw( :experimental(init_expr) );
Subroutine BEGIN: called CC=1 SCAR=0.0
82
83 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
87
88 # cpan modules
89 1 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
90
91 # modules for Windows only
92 1 ▶use if $^O eq 'MSWin32', 'Win32::Clipboard';
Subroutine BEGIN: called CC=1 SCAR=0.0
93
94 # conditional modules
95 1 ▶use Module::Load::Conditional qw( can_load check_install requires );
Subroutine BEGIN: called CC=1 SCAR=0.0
96
97 # custom modules
98 1 ▶use Weather::GHCN::Common qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use Weather::GHCN::StationTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
100
101 ########################################################################
102 # Global delarations
103 ########################################################################
104
105 # is it ok to use Tk?
106 1 our $TK_MODULES = {
107 'Tk' => undef,
108 'Tk::Getopt' => undef,
109 };
110
111 # is it ok to use Win32::Clipboard?
112 1 our $USE_WINCLIP = $^O eq 'MSWin32';
113 1 our $USE_TK = can_load( modules => $TK_MODULES );
114
115 1 my $Opt; # options object, with property accessors for each user option
116
117 # options that relate to script execution, not GHCN processing and output
118 my $Opt_file; # file in which to save options from GUI dialog
119 1 my $Opt_gui; # launch the GUI dialog
120 1 my $Opt_help; # display POD documentation
121 1 my $Opt_readme; # launch a browser displaying the GHCN readme file
122 1 my $Opt_usage; # display a synopsis of the command line syntax
123 1 my $Opt_outclip; # send report output to the Windows clipboard instead of STDOUT
124
125 ########################################################################
126 # Constants
127 ########################################################################
128
129 1 const my $EMPTY => q(); # empty string
130 1 const my $SPACE => q( ); # space character
131 1 const my $DASH => q(-); # dash character
132 1 const my $TAB => qq(\t); # tab character
133 1 const my $NL => qq(\n); # perl universal newline (any platform)
134 1 const my $TRUE => 1; # perl's usual TRUE
135 1 const my $FALSE => not $TRUE; # a dual-var consisting of '' and 0
136
137 1 const my $CONFIG_FILE => '$HOME/.ghcn_fetch.yaml';
138
139 1 const my $STN_THRESHOLD => 100; # ask if number of selected stations exceeds this
140
141 1 const my $STN_ID_RE => qr{ [[:upper:]]{2} [[:alnum:]\_\-]{9} }xms;
142
143 ########################################################################
144 # Script Mainline
145 ########################################################################
146
147 1 ▶__PACKAGE__->run( \@ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
148
149 =head1 SUBROUTINES
150
151 =head2 run ( \@ARGV )
152
153 Invoke this subroutine, passing in a reference to @ARGV, in order to
154 fetch NOAA GHCN station data or daily weather data.
155
156 See ghnc_fetch.pl -help for details.
157
158 =cut
159
160 ▶sub run ($progname, $argv_aref) {
Subroutine run: called CC=44 SCAR=68.8
Pod documented
161
162 1 local @ARGV = $argv_aref->@*;
163
164 1 ▶ warn "*W* -gui option unavailable -- try installing Tk and Tk::GetOptions"
Branch: 1 branch
BranchTrueFalseTotal
unless $Weather::GHCN::App::Fetch::USE_TK 1 0 1
165 if not $USE_TK;
166
167 1 my $ghcn = Weather::GHCN::StationTable->new;
168
169 1 $ghcn->tstats->start('_Overall');
170
171 1 Getopt::Long::Configure ('pass_through');
172
173 # If the first command line argument is a report_type, remove and save it
174 1 my $report_type;
175 1 ▶ if (@ARGV > 0 and $ARGV[0] =~ m{ \A [^-][[:alpha:]]+ \b }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 0 and $ARGV[0] =~ / \A [^-][[:alpha:]]+ \b /msux) 0 1 1
Condition33%1/3
@ARGV > 0 and $ARGV[0] =~ / \A [^-][[:alpha:]]+ \b /msux
!ll&&!rl&&r
010
MC/DC0%0/2
@ARGV > 0 and $ARGV[0] =~ / \A [^-][[:alpha:]]+ \b /msux
@ARGV > 0 $ARGV[0] =~ / \A [^-][[:alpha:]]+ \b /msux
Truth table33%1/3
A and B
A= @ARGV > 0B= $ARGV[0] =~ / \A [^-][[:alpha:]]+ \b /msux
ABresult
0X0
100
111
176 0 my $rt_arg = shift @ARGV;
177 0 my $rt = deabbrev_report_type( $rt_arg );
178 0 ▶ $report_type = $rt // $rt_arg;
Condition0%0/3
$rt // $rt_arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$rt // $rt_arg
$rt $rt_arg
Truth table0%0/3
A // B
A= $rtB= $rt_arg
ABresult
1X1
011
000
179 }
180
181 # The -optfile option, which is preprocessed by GetOptions before
182 # we call Tk::Getopt, has an overlapping abbreviation with -outclip.
183 # To avoid GetOptions from stripping -o from @ARGV, we disambiguate
184 # -o by changing it to -outclip. This means that -optfile can
185 # only by abbreviated down to -op.
186 # Note: normally we shouldn't be changing @ARGV but in this case
187 # we do want to.
188
189 ## no critic [ProhibitCStyleForLoops]
190 ## no critic [RequireLocalizedPunctuationVars]
191 1 ▶ for (my $ii=0; $ii < @ARGV; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < @ARGV) 14 1 15
192 14 ▶ $ARGV[$ii] = '-outclip'
Branch: 1 branch
BranchTrueFalseTotal
if $ARGV[$ii] eq "-o" and $ii == $#ARGV or $ii < $#ARGV - 1 and $ARGV[$ii + 1] =~ m[ /A [-]]msux 0 14 14
Condition44%4/9
$ARGV[$ii] eq "-o" and $ii == $#ARGV or $ii < $#ARGV - 1 and $ARGV[$ii + 1] =~ m[ /A [-]]msux
l!l&&r!l&&!r
0014
$ARGV[$ii] eq "-o" and $ii == $#ARGV
!ll&&!rl&&r
1400
$ii < $#ARGV - 1 and $ARGV[$ii + 1] =~ m[ /A [-]]msux
!ll&&!rl&&r
2120
MC/DC0%0/4
$ARGV[$ii] eq "-o" and $ii == $#ARGV or $ii < $#ARGV - 1 and $ARGV[$ii + 1] =~ m[ /A [-]]msux
$ARGV[$ii] eq "-o" $ii == $#ARGV $ii < $#ARGV - 1 $ARGV[$ii + 1] =~ m[ /A [-]]msux
Truth table14%1/7
A and B or C and D
A= $ARGV[$ii] eq "-o"B= $ii == $#ARGVC= $ii < $#ARGV - 1D= $ARGV[$ii + 1] =~ m[ /A [-]]msux
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
193 if $ARGV[$ii] eq '-o' and
194 ($ii == $#ARGV) # a lone -o at the end
195 or # a -o followed by an option
196 ($ii < $#ARGV - 1 and
197 $ARGV[$ii+1] =~ m{ /A [-]}xms);
198 }
199
200 # record the number of command line arguments before they are removed by GetOptions
201 1 my $argv_count = @ARGV;
202
203 1 my %script_args = (
204 'help' => \$Opt_help,
205 'usage|?' => \$Opt_usage,
206 'optfile:s' => \$Opt_file,
207 'readme' => \$Opt_readme,
208 );
209
210 1 ▶ $script_args{'gui'} = \$Opt_gui
Branch: 1 branch
BranchTrueFalseTotal
if $Weather::GHCN::App::Fetch::USE_TK 0 1 1
211 if $USE_TK;
212
213 1 ▶ $script_args{'outclip|o'} = \$Opt_outclip
Branch: 1 branch
BranchTrueFalseTotal
if $Weather::GHCN::App::Fetch::USE_WINCLIP 0 1 1
214 if $USE_WINCLIP;
215
216 1 GetOptions( %script_args );
217
218 1 ▶ if ( $Opt_help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_help) 0 1 1
219 0 pod2usage(-verbose => 2);
220 0 exit;
221 }
222
223 1 ▶ if ( $Opt_usage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_usage) 0 1 1
224 0 pod2usage(1);
225 0 exit;
226 }
227
228 # launch the default browser with the NOAA Daily readme.txt file content
229 1 ▶ if ( $Opt_readme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_readme) 0 1 1
230 0 system 'start https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt';
231 0 exit;
232 }
233
234 # default to -gui if no command line arguments were provided and
235 # we aren't taking input from a pipe or file
236 1 ▶ $Opt_gui = 1 if $USE_TK and $argv_count == 0 and -t *STDIN;
Branch: 1 branch
BranchTrueFalseTotal
if $Weather::GHCN::App::Fetch::USE_TK and $argv_count == 0 and -t *STDIN 0 1 1
Condition33%2/6
$Weather::GHCN::App::Fetch::USE_TK and $argv_count == 0 and -t *STDIN
!ll&&!rl&&r
100
$Weather::GHCN::App::Fetch::USE_TK and $argv_count == 0
!ll&&!rl&&r
100
MC/DC0%0/3
$Weather::GHCN::App::Fetch::USE_TK and $argv_count == 0 and -t *STDIN
$Weather::GHCN::App::Fetch::USE_TK $argv_count == 0 -t *STDIN
Truth table25%1/4
A and B and C
A= $Weather::GHCN::App::Fetch::USE_TKB= $argv_count == 0C= -t *STDIN
ABCresult
0XX0
10X0
1100
1111
237
238 1 my $user_opt_href = get_user_options($Opt_file);
239
240 1 ▶ $user_opt_href->{report} = $report_type
Branch: 1 branch
BranchTrueFalseTotal
if $report_type 0 1 1
241 if $report_type;
242
243 1 ▶ my $config_file = $user_opt_href->{config} // $CONFIG_FILE;
Condition33%1/3
$user_opt_href->{"config"} // $CONFIG_FILE
l!l&&r!l&&!r
100
MC/DC0%0/2
$user_opt_href->{"config"} // $CONFIG_FILE
$user_opt_href->{"config"} $CONFIG_FILE
Truth table33%1/3
A // B
A= $user_opt_href->{"config"}B= $CONFIG_FILE
ABresult
1X1
011
000
244
245 1 ▶ die '*E* unrecognized options: ' . join $SPACE, @ARGV
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 1 1
246 if @ARGV;
247
248 1 my %stnid_filter;
249
250 my @errors;
251 1 ($Opt, @errors) = $ghcn->set_options(
252 user_options => $user_opt_href,
253 config_file => $config_file,
254 stnid_filter => \%stnid_filter,
255 );
256
257 0 ▶ die join qq(\n), @errors, qq(\n)
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
258 if @errors;
259
260 0 my ( $output, $new_fh, $old_fh );
261 0 ▶ if ( $Opt_outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP) 0 0 0
Condition0%0/3
$Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP
!ll&&!rl&&r
000
MC/DC0%0/2
$Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP
$Opt_outclip $Weather::GHCN::App::Fetch::USE_WINCLIP
Truth table0%0/3
A and B
A= $Opt_outclipB= $Weather::GHCN::App::Fetch::USE_WINCLIP
ABresult
0X0
100
111
262 0 ▶ open $new_fh, '>', \$output
Branch: 1 branch
BranchTrueFalseTotal
open $new_fh, ">", \$output or die "Unable to open buffer for write" 0 0 0
263 or die 'Unable to open buffer for write';
264 0 $old_fh = select $new_fh; ## no critic (ProhibitOneArgSelect)
265 }
266
267 # get a list of station id's from stdin if it's a pipe or file
268 # (but not if stdin is pointing to the terminal)
269 0 ▶ if ( -p *STDIN || -f *STDIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-p *STDIN or -f *STDIN) 0 0 0
Condition0%0/3
-p *STDIN or -f *STDIN
l!l&&r!l&&!r
000
MC/DC0%0/2
-p *STDIN or -f *STDIN
-p *STDIN -f *STDIN
Truth table0%0/3
A or B
A= -p *STDINB= -f *STDIN
ABresult
1X1
011
000
270 0 my $ii;
271 0 ▶ while (<STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 0 0 0
272 0 chomp;
273 0 my @id_list = $_ =~ m{ $STN_ID_RE }xmsg;
274 0 foreach my $id ( @id_list ) {
275 0 $stnid_filter{$id}++;
276 0 $ii++;
277 }
278 }
279 0 ▶ die "*E* no station id's found in the input"
Branch: 1 branch
BranchTrueFalseTotal
unless $ii 0 0 0
280 unless $ii;
281 }
282
283 0 $ghcn->load_stations;
284
285 0 say {*STDERR} '*I* ', $ghcn->stn_count, ' stations found';
286 0 say {*STDERR} '*I* ', $ghcn->stn_selected_count, ' stations match location and GSN options';
287 0 say {*STDERR} '*I* ', $ghcn->stn_filtered_count, ' stations matched range and measurement options';
288
289 0 ▶ if ($ghcn->stn_filtered_count > $STN_THRESHOLD ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ghcn->stn_filtered_count > $STN_THRESHOLD) 0 0 0
290 0 print {*STDERR} ">>>> There are a lot of stations to process. Continue (y/n)?\n>>>> ";
291 0 my $reply = <>;
292 0 chomp $reply;
293 0 ▶ exit if $reply =~ m{ \A ( n | no ) }xmsi;
Branch: 1 branch
BranchTrueFalseTotal
if $reply =~ / \A ( n | no ) /imsux 0 0 0
294 }
295
296 0 ▶ if ($Opt->report) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->report) 0 0 0
297 0 say $ghcn->get_header;
298
299 # this prints detailed station data if $Opt->report eq 'id'
300 $ghcn->load_data(
301 # set a callback routine for printing progress messages
302 0 ▶ progress_sub => sub { say {*STDERR} @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 # set a callback routine for printing rows when -report id
304 0 ▶ row_sub => sub { say join "\t", @{ $_[0] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 );
306
307 0 ▶ if ($Opt->report eq 'id' and $Opt->nogaps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->report eq "id" and $Opt->nogaps) 0 0 0
Condition0%0/3
$Opt->report eq "id" and $Opt->nogaps
!ll&&!rl&&r
000
MC/DC0%0/2
$Opt->report eq "id" and $Opt->nogaps
$Opt->report eq "id" $Opt->nogaps
Truth table0%0/3
A and B
A= $Opt->report eq "id"B= $Opt->nogaps
ABresult
0X0
100
111
308 0 say $ghcn->get_missing_rows;
309 }
310
311 # these only do something when $Opt->report ne 'id'
312 0 $ghcn->summarize_data;
313 0 say $ghcn->get_summary_data;
314 0 say $EMPTY;
315
316 0 ▶ goto WRAP_UP if $Opt->dataonly;
Branch: 1 branch
BranchTrueFalseTotal
if $Opt->dataonly 0 0 0
317
318 0 say $EMPTY;
319 0 say $ghcn->get_footer;
320
321 0 say $EMPTY;
322 0 say $ghcn->get_flag_statistics;
323 }
324
325 0 ▶ if ( $Opt->defined('kml') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->defined("kml")) 0 0 0
326 0 ▶ if ($Opt->kml eq $EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->kml eq $EMPTY) { } 0 0 0
327 0 say $ghcn->export_kml;
328 0 goto WRAP_UP;
329 } else {
330 0 $ghcn->export_kml;
331 }
332 }
333
334 0 say $EMPTY;
335 0 say $ghcn->get_stations( kept => 1 );
336
337 0 my @rejected = $ghcn->get_stations( list => 1, kept => 0, no_header => 1 );
338 0 ▶ if (@rejected) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rejected) 0 0 0
339 0 say $EMPTY;
340 0 say 'Stations that failed to meet range or quality criteria:';
341 0 say tsv(\@rejected);
342 }
343
344 0 ▶ if ( $ghcn->has_missing_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ghcn->has_missing_data) 0 0 0
345 0 warn '*W* some data was missing for the stations and date range processed' . $NL;
346 0 say $EMPTY;
347 0 say $ghcn->get_missing_data_ranges;
348 }
349
350 0 $ghcn->tstats->stop('_Overall') ;
351 0 $ghcn->tstats->finish;
352
353 0 say $EMPTY;
354 0 say $ghcn->get_options;
355
356 0 say $EMPTY;
357 0 say 'Script:';
358 0 say $TAB, $PROGRAM_NAME;
359 0 say "\tWeather::GHCN::StationTable version " . $Weather::GHCN::StationTable::VERSION;
360 0 say $TAB, 'Config file: ' . $ghcn->config_file;
361
362 0 ▶ if ( $Opt->performance ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt->performance) 0 0 0
363 0 say $EMPTY;
364 0 ▶ say sprintf 'Timing statistics (ms)%s:', $Opt->performance ? ' and memory [bytes]' : $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
$Opt->performance ? : 0 0 0
365 0 say $ghcn->get_timing_stats;
366
367 0 say $EMPTY;
368 0 say $ghcn->get_hash_stats;
369 }
370
371 WRAP_UP:
372 # send output to the Windows clipboard
373 0 ▶ if ( $Opt_outclip and $USE_WINCLIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP) 0 0 0
Condition0%0/3
$Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP
!ll&&!rl&&r
000
MC/DC0%0/2
$Opt_outclip and $Weather::GHCN::App::Fetch::USE_WINCLIP
$Opt_outclip $Weather::GHCN::App::Fetch::USE_WINCLIP
Truth table0%0/3
A and B
A= $Opt_outclipB= $Weather::GHCN::App::Fetch::USE_WINCLIP
ABresult
0X0
100
111
374 0 Win32::Clipboard->new()->Set( $output );
375 0 select $old_fh;
376 }
377
378 0 return;
379 }
380
381 ########################################################################
382 # Subroutines
383 ########################################################################
384
385 =head2 get_user_options ( $optfile=undef )
386
387 Fetch.pm uses B<get_user_options()> to either get user options
388 via B<Tk::GetOptions> -- if it is installed -- or via B<Getopt::Long>.
389
390 =cut
391
392 1 ▶sub get_user_options ( $optfile=undef ) {
Subroutine get_user_options: called CC=3 SCAR=11.2
Pod documented
393
394 1 ▶ my $user_opt_href = $USE_TK
Branch: 1 branch
BranchTrueFalseTotal
$Weather::GHCN::App::Fetch::USE_TK ? : 0 1 1
395 ? get_user_options_tk($optfile)
396 : get_user_options_no_tk($optfile)
397 ;
398
399 1 return $user_opt_href;
400 }
401
402 =head2 get_user_options_no_tk ( $optfile=undef )
403
404 This function obtains user options from @ARGV by calling B<Getopt::Long>
405 B<GetOptions> using a list of option definitions obtained by calling
406 B<Weather::GHCN::Options->get_getopt_list()>. The options (and their values)
407 are extracted from @ARGV and put in a hash, a reference to which is
408 then returned.
409
410 The $optfile argument is only used by B<get_user_options_no_tk>.
411
412 =cut
413
414 1 ▶sub get_user_options_no_tk ( $optfile=undef ) {
Subroutine get_user_options_no_tk: called CC=2 SCAR=6.9
Pod documented
415
416 1 my @options = ( Weather::GHCN::Options->get_getopt_list() );
417
418 1 my %opt;
419 1 GetOptions( \%opt, @options);
420
421 1 return \%opt;
422 }
423
424 =head2 get_user_options_tk ( $optfile=undef )
425
426 This function returns a reference to a hash of user options obtained
427 by calling B<Tk::Getopt>. This may launch a GUI dialog to collect
428 the options.
429
430 The optional $optfile argument specifies a filename which
431 B<Tk::GetOptions> can use to store or load options.
432
433 =cut
434
435 0 ▶sub get_user_options_tk ( $optfile=undef ) {
Subroutine get_user_options_tk: not called CC=11 SCAR=48.8
Pod documented
436
437 0 ▶ if (not $USE_TK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Weather::GHCN::App::Fetch::USE_TK) 0 0 0
438 0 warn "*E* Tk or Tk::Getopt not installed";
439 0 return;
440 }
441
442 0 my %opt;
443
444 ## no critic [ProhibitNoisyQuotes]
445 0 my @opttable = ( Weather::GHCN::Options->get_tk_options_table() );
446
447 0 my $optobj = Tk::Getopt->new(
448 -opttable => \@opttable,
449 -options => \%opt,
450 -filename => $optfile);
451
452 0 $optobj->set_defaults; # set default values
453
454 0 ▶ $optobj->load_options # configuration file
Branch: 1 branch
BranchTrueFalseTotal
if defined $optfile and -e $optfile 0 0 0
Condition0%0/3
defined $optfile and -e $optfile
!ll&&!rl&&r
000
MC/DC0%0/2
defined $optfile and -e $optfile
defined $optfile -e $optfile
Truth table0%0/3
A and B
A= defined $optfileB= -e $optfile
ABresult
0X0
100
111
455 if defined $optfile and -e $optfile;
456
457 0 $optobj->get_options; # command line
458
459 # Because Tk::Getopt doesn't support option abbreviations for choice
460 # lists -- there's no need for the gui since choices are provided in a
461 # drop list -- we run into a problem when using Tk:Getopt without the
462 # gui. In that case, an abbrevation for a -report option will be caught
463 # during process_options call (just below), before our call to
464 # TableStation::set_options can expand the abbrevation by calling
465 # Options::validate. So, for the sake of this
466 # script we do an abbrevation substition right here, and then call our own
467 # validate_report_type sub which gets the valid report types from the
468 # options table. We do our own error report too, because Tk::Getopt
469 # reports choices as a list of ARRAY(<address>) rather than as values.
470
471 0 ▶ if ( $optobj->{options}->{report} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optobj->{"options"}{"report"}) 0 0 0
472 0 my %report_abbrev = abbrev( qw(id daily monthly yearly) );
473 0 my $rt = deabbrev_report_type( $optobj->{options}->{report} );
474 0 ▶ $optobj->{options}->{report} = $rt
Branch: 1 branch
BranchTrueFalseTotal
if $rt 0 0 0
475 if $rt;
476 }
477 die '*E* invalid report option: ' . $optobj->{options}->{report} . "\n"
478 0 ▶ unless valid_report_type( $optobj->{options}->{report}, \@opttable );
Branch: 1 branch
BranchTrueFalseTotal
unless valid_report_type($optobj->{"options"}{"report"}, \@opttable) 0 0 0
479
480 0 $optobj->process_options; # process callbacks, check restrictions ...
481
482 0 ▶ if ($Opt_gui) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt_gui) 0 0 0
483 0 my $top = MainWindow->new;
484 0 $top->geometry('500x300+300+200');
485 0 $top->title('GHCN Daily Parser');
486
487 0 my $retval = $optobj->option_dialog(
488 $top,
489 -toplevel => 'Frame',
490 -buttons => [qw/ok cancel save/], # not using cancel apply undo save defaults
491 -statusbar => 1,
492 -wait => 1,
493 -pack => [-fill => 'both', -expand => 1],
494 );
495
496 0 ▶ die "*I* action cancelled\n" if $retval and $retval eq 'cancel';
Branch: 1 branch
BranchTrueFalseTotal
if $retval and $retval eq "cancel" 0 0 0
Condition0%0/3
$retval and $retval eq "cancel"
!ll&&!rl&&r
000
MC/DC0%0/2
$retval and $retval eq "cancel"
$retval $retval eq "cancel"
Truth table0%0/3
A and B
A= $retvalB= $retval eq "cancel"
ABresult
0X0
100
111
497 }
498
499 0 return \%opt;
500 }
501
502 =head2 valid_report_type ($rt, \@opttable)
503
504 This function is used to valid the report type. Valid values are
505 defined in the built-in Tk options table, which can be obtained by
506 calling:
507
508 my @opttable = ( Weather::GHCN::Options->get_tk_options_table() );
509
510 =cut
511
512 ▶sub valid_report_type ($rt, $opttable_aref) {
Subroutine valid_report_type: not called CC=1 SCAR=6.9
Pod documented
513 0 my $choices_href = Weather::GHCN::Options->get_option_choices;
514 0 return $choices_href->{'report'}->{$rt};
515 }
516
517 =head2 deabbrev_report_type ($rt)
518
519 The report types supported by the -report option can be abbrevated,
520 so long as the abbrevation is unambiquous. For example, 'daily' can
521 be abbreviated to 'dail', 'dai', 'da', or even 'd'.
522
523 This function takes a (possibly abbreviated) report type and returns
524 an unabbreviated report type.
525
526 =cut
527
528 ▶sub deabbrev_report_type ($rt) {
Subroutine deabbrev_report_type: not called CC=1 SCAR=6.9
Pod documented
529 0 my %r_abbrev = abbrev( qw(id daily monthly yearly) );
530 0 my $deabbreved = $r_abbrev{ $rt };
531 0 return $deabbreved;
532 }
533
534 =head1 AUTHOR
535
536 Gary Puckering (jgpuckering@rogers.com)
537
538 =head1 LICENSE AND COPYRIGHT
539
540 Copyright 2022, Gary Puckering
541
542 =cut
543
544 1 1;
↑ Summary blib/lib/Weather/GHCN/App/Fetch.pm »