| 1 | # Weather::GHCN::Common.pm - common functions for GHCN scripts and modules | ||||||||||||||
| 2 | |||||||||||||||
| 3 | ## no critic (Documentation::RequirePodAtEnd) | ||||||||||||||
| 4 | |||||||||||||||
| 5 | =head1 NAME | ||||||||||||||
| 6 | |||||||||||||||
| 7 | Weather::GHCN::Common - common functions for GHCN scripts and modules | ||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||
| 10 | |||||||||||||||
| 11 | version v0.0.003 | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||
| 14 | |||||||||||||||
| 15 | use Weather::GHCN::Common qw(:all); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||
| 19 | |||||||||||||||
| 20 | The B<Weather::GHCN::Common> module provides functions that are used in more | ||||||||||||||
| 21 | than one GHCN module, or that may be useful in application scripts; | ||||||||||||||
| 22 | e.g. rng_valid() to validate number ranges that might be provided | ||||||||||||||
| 23 | to a script using command line arguments. | ||||||||||||||
| 24 | |||||||||||||||
| 25 | The module is primarily for use by modules Weather::GHCN::Fetch, Weather::GHCN::Options, | ||||||||||||||
| 26 | Weather::GHCN::Station, and Weather::GHCN::StationTable. | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =cut | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 6 | ▶ | use v5.18; # minimum for Object::Pad | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | package Weather::GHCN::Common; | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 6 | our $VERSION = 'v0.0.003'; | |||||||||||||
| 35 | |||||||||||||||
| 36 | |||||||||||||||
| 37 | 6 | ▶ | use feature 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 6 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | ## no critic [ProhibitSubroutinePrototypes] | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 6 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 6 | ▶ | use parent 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 6 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 6 | ▶ | use Const::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 6 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | 6 | ▶ | use Set::IntSpan::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | |||||||||||||||
| 50 | 6 | const my $EMPTY => q(); | |||||||||||||
| 51 | 6 | const my $TAB => qq(\t); | |||||||||||||
| 52 | 6 | const my $NL => qq(\n); | |||||||||||||
| 53 | |||||||||||||||
| 54 | 6 | const my $RANGE_RE => qr{ \d+ (?: [-] \d+ )? }xms; | |||||||||||||
| 55 | 6 | const my $RANGE_LIST_RE => qr{ \A $RANGE_RE (?: [,] $RANGE_RE )* \Z }xms; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 6 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||
| 58 | commify | ||||||||||||||
| 59 | np_trim | ||||||||||||||
| 60 | rng_new | ||||||||||||||
| 61 | rng_valid | ||||||||||||||
| 62 | rng_within | ||||||||||||||
| 63 | tsv | ||||||||||||||
| 64 | iso_date_time | ||||||||||||||
| 65 | ) ] ); | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 6 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||
| 68 | |||||||||||||||
| 69 | |||||||||||||||
| 70 | =head1 FUNCTIONS | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =head2 commify($number) | ||||||||||||||
| 73 | |||||||||||||||
| 74 | Insert commas into a number so that digits are grouped in threes; | ||||||||||||||
| 75 | e.g. 12345 becomes 12,345. | ||||||||||||||
| 76 | |||||||||||||||
| 77 | The argument can be a number or a string of digits, with or without | ||||||||||||||
| 78 | a decimal. Digits after a decimal are unaffected. | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =cut | ||||||||||||||
| 81 | |||||||||||||||
| 82 | # insert commas into a number | ||||||||||||||
| 83 | ▶ | sub commify ($arg) { | |||||||||||||
|
Subroutine
commify: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 84 | |||||||||||||||
| 85 | 14 | ▶ | $arg //= q(); | ||||||||||||
|
Condition100%2/2
$arg //= ""
MC/DC100%1/1
$arg //= ""
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||
| 86 | |||||||||||||||
| 87 | 14 | my $text = reverse $arg; | |||||||||||||
| 88 | |||||||||||||||
| 89 | 14 | $text =~ s{ (\d\d\d) (?=\d) (?! \d* [.] ) }{$1,}xmsg; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 14 | return scalar reverse $text; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =head2 rng_new(@args) | ||||||||||||||
| 95 | |||||||||||||||
| 96 | Wrapper for Set::IntSpan::Fast->new(), it provides a shorter name | ||||||||||||||
| 97 | as well as: | ||||||||||||||
| 98 | |||||||||||||||
| 99 | - allowing an undef $range to create an empty set | ||||||||||||||
| 100 | - croaking if new() fails for any reason | ||||||||||||||
| 101 | |||||||||||||||
| 102 | The arguments to rng_new can consist of a range string (e.g. '1-5,12') | ||||||||||||||
| 103 | or a perl list of numbers (e.g. 1,7,12,20..25) or a mix of both. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | ▶ | sub rng_new (@args) { ## no critic [RequireFinalReturn] | |||||||||||||
|
Subroutine
rng_new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 108 | 27 | my $s; | |||||||||||||
| 109 | |||||||||||||||
| 110 | # treat undef as an empty range | ||||||||||||||
| 111 | 27 | ▶ | my @ranges = map { $_ // q() } @args; | ||||||||||||
|
Condition100%2/2
$_ // ""
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||
| 112 | |||||||||||||||
| 113 | try { | ||||||||||||||
| 114 | 27 | ▶ | $s = Set::IntSpan::Fast->new( @ranges ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 115 | } catch { | ||||||||||||||
| 116 | 0 | ▶ | croak 'Common::rng_new ' . $_; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 117 | 27 | }; | |||||||||||||
| 118 | 27 | return $s; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head2 rng_valid($range) | ||||||||||||||
| 122 | |||||||||||||||
| 123 | Returns true if the range string is valid for Set::IntSpan::Fast. Valid | ||||||||||||||
| 124 | ranges consist of numbers, a pair of numbers delimited by dash | ||||||||||||||
| 125 | (e.g 15-75), or a mix of those delimited by commas (e.g. '5-9,12,25-30'). | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =cut | ||||||||||||||
| 128 | |||||||||||||||
| 129 | ▶ | sub rng_valid ($rng) { | |||||||||||||
|
Subroutine
rng_valid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 130 | 7 | return $rng =~ $RANGE_LIST_RE; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | |||||||||||||||
| 134 | =head2 rng_within($range, $domain) | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Returns true if the range string is lies within the domain range. For | ||||||||||||||
| 137 | example rng_within('3-5', '1-12') return true, whereas | ||||||||||||||
| 138 | rng_within('1800,1950', '1900-2100') returns false because 1800 is | ||||||||||||||
| 139 | not within the domain of 1900 to 2100. | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =cut | ||||||||||||||
| 142 | |||||||||||||||
| 143 | ▶ | sub rng_within ($rng, $domain) { | |||||||||||||
|
Subroutine
rng_within: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 144 | 11 | ▶ | croak "*E* invalid range argument: $rng" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | unless $rng =~ $RANGE_LIST_RE; | ||||||||||||||
| 146 | 10 | ▶ | croak "*E* invalid domain argument: $rng" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | unless $domain =~ $RANGE_LIST_RE; | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 9 | my $rng_obj = rng_new($rng); | |||||||||||||
| 150 | 9 | my $domain_obj = rng_new($domain); | |||||||||||||
| 151 | |||||||||||||||
| 152 | 9 | return $rng_obj->subset($domain_obj); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | |||||||||||||||
| 156 | =head2 tsv($list_or_list_of_lists) | ||||||||||||||
| 157 | |||||||||||||||
| 158 | Takes a perl list and returns an equivalent tab-separated string. | ||||||||||||||
| 159 | Alternatively, takes a list of lists and returns a newline-separated | ||||||||||||||
| 160 | string of tab-separated values. | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =cut | ||||||||||||||
| 163 | |||||||||||||||
| 164 | ▶ | sub tsv ($list_or_list_of_lists) { | |||||||||||||
|
Subroutine
tsv: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 165 | 6 | ▶ | return $EMPTY if not defined $list_or_list_of_lists; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 5 | ▶ | return $EMPTY if not $list_or_list_of_lists->@*; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | |||||||||||||||
| 168 | 4 | my $argref = ref $list_or_list_of_lists->[0]; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 4 | my $result = $EMPTY; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 4 | ▶ | if ($argref eq 'ARRAY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 173 | 1 | my @rows; | |||||||||||||
| 174 | 1 | foreach my $row_aref ( $list_or_list_of_lists->@* ) { | |||||||||||||
| 175 | 2 | push @rows, join $TAB, $row_aref->@*; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | 1 | $result = join $NL, @rows; | |||||||||||||
| 178 | } elsif ($argref eq $EMPTY) { | ||||||||||||||
| 179 | 2 | $result = join $NL, $list_or_list_of_lists->@*; | |||||||||||||
| 180 | } else { | ||||||||||||||
| 181 | 1 | croak '*E* tsv() invalid argument: ' . $argref; | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | 3 | return $result; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | |||||||||||||||
| 188 | =head2 iso_date_time(@now) | ||||||||||||||
| 189 | |||||||||||||||
| 190 | Takes the first 6 elements from a perl localtime array and formats | ||||||||||||||
| 191 | them into an ISO date string YYYY-MM-DD HH:MM:SS. | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =cut | ||||||||||||||
| 194 | |||||||||||||||
| 195 | ▶ | sub iso_date_time (@now) { | |||||||||||||
|
Subroutine
iso_date_time: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 196 | ## no critic [ProhibitMagicNumbers] | ||||||||||||||
| 197 | |||||||||||||||
| 198 | 9 | ▶ | croak 'iso_date_time requires at least a 6-element localtime array' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | if @now < 6; | ||||||||||||||
| 200 | |||||||||||||||
| 201 | 7 | my @ymdhms = ( $now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0] ); | |||||||||||||
| 202 | |||||||||||||||
| 203 | return wantarray | ||||||||||||||
| 204 | ? @ymdhms | ||||||||||||||
| 205 | 7 | ▶ | : sprintf '%4d-%02d-%02d %02d:%02d:%02d', @ymdhms | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | ; | ||||||||||||||
| 207 | } | ||||||||||||||
| 208 | |||||||||||||||
| 209 | 6 | 1; | |||||||||||||
| 210 | |||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||
| 212 | |||||||||||||||
| 213 | Gary Puckering (jgpuckering@rogers.com) | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 216 | |||||||||||||||
| 217 | Copyright 2022, Gary Puckering | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =cut | ||||||||||||||