| 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 | use v5.18; # minimum for Object::Pad | ||
| 31 | |||
| 32 | package Weather::GHCN::Common; | ||
| 33 | |||
| 34 | our $VERSION = 'v0.0.003'; | ||
| 35 | |||
| 36 | |||
| 37 | use feature 'signatures'; | ||
| 38 | no warnings 'experimental::signatures'; | ||
| 39 | |||
| 40 | ## no critic [ProhibitSubroutinePrototypes] | ||
| 41 | |||
| 42 | use Exporter; | ||
| 43 | use parent 'Exporter'; | ||
| 44 | |||
| 45 | use Carp qw(croak); | ||
| 46 | use Const::Fast; | ||
| 47 | use Try::Tiny; | ||
| 48 | use Set::IntSpan::Fast; | ||
| 49 | |||
| 50 | const my $EMPTY => q(); | ||
| 51 | const my $TAB => qq(\t); | ||
| 52 | const my $NL => qq(\n); | ||
| 53 | |||
| 54 | const my $RANGE_RE => qr{ \d+ (?: [-] \d+ )? }xms; | ||
| 55 | const my $RANGE_LIST_RE => qr{ \A $RANGE_RE (?: [,] $RANGE_RE )* \Z }xms; | ||
| 56 | |||
| 57 | 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 | 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) { | ||
| 84 | |||
| 85 | $arg //= q(); | ||
| 86 | |||
| 87 | my $text = reverse $arg; | ||
| 88 | |||
| 89 | $text =~ s{ (\d\d\d) (?=\d) (?! \d* [.] ) }{$1,}xmsg; | ||
| 90 | |||
| 91 | 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] | ||
| 108 | my $s; | ||
| 109 | |||
| 110 | # treat undef as an empty range | ||
| 111 | my @ranges = map { $_ // q() } @args; | ||
| 112 | |||
| 113 | try { | ||
| 114 | $s = Set::IntSpan::Fast->new( @ranges ); | ||
| 115 | } catch { | ||
| 116 | croak 'Common::rng_new ' . $_; | ||
| 117 | }; | ||
| 118 | 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) { | ||
| 130 | 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) { | ||
| 144 | croak "*E* invalid range argument: $rng" | ||
| 145 | unless $rng =~ $RANGE_LIST_RE; | ||
| 146 | croak "*E* invalid domain argument: $rng" | ||
| 147 | unless $domain =~ $RANGE_LIST_RE; | ||
| 148 | |||
| 149 | my $rng_obj = rng_new($rng); | ||
| 150 | my $domain_obj = rng_new($domain); | ||
| 151 | |||
| 152 | 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) { | ||
| 165 | return $EMPTY if not defined $list_or_list_of_lists; | ||
| 166 | return $EMPTY if not $list_or_list_of_lists->@*; | ||
| 167 | |||
| 168 | my $argref = ref $list_or_list_of_lists->[0]; | ||
| 169 | |||
| 170 | my $result = $EMPTY; | ||
| 171 | |||
| 172 | if ($argref eq 'ARRAY') { | ||
| 173 | my @rows; | ||
| 174 | foreach my $row_aref ( $list_or_list_of_lists->@* ) { | ||
| 175 | push @rows, join $TAB, $row_aref->@*; | ||
| 176 | } | ||
| 177 | $result = join $NL, @rows; | ||
| 178 | } elsif ($argref eq $EMPTY) { | ||
| 179 | $result = join $NL, $list_or_list_of_lists->@*; | ||
| 180 | } else { | ||
| 181 | croak '*E* tsv() invalid argument: ' . $argref; | ||
| 182 | } | ||
| 183 | |||
| 184 | 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) { | ||
| 196 | ## no critic [ProhibitMagicNumbers] | ||
| 197 | |||
| 198 | croak 'iso_date_time requires at least a 6-element localtime array' | ||
| 199 | if @now < 6; | ||
| 200 | |||
| 201 | my @ymdhms = ( $now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0] ); | ||
| 202 | |||
| 203 | return wantarray | ||
| 204 | ? @ymdhms | ||
| 205 | : sprintf '%4d-%02d-%02d %02d:%02d:%02d', @ymdhms | ||
| 206 | ; | ||
| 207 | } | ||
| 208 | |||
| 209 | 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 |