blib/lib/Weather/GHCN/Measures.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 41 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # Weather::GHCN::Measure.pm - class for creating a list of weather measures based on ghcn_fetch options -tavg and -precip
2
3 ## no critic (Documentation::RequirePodAtEnd)
4
5 =head1 NAME
6
7 Weather::GHCN::Measures - provide a list of meteorological metrics to be obtained from GHCN daily data
8  
9 =head1 VERSION
10  
11 version v0.0.003
12
13 =head1 SYNOPSIS
14
15 use Weather::GHCN::Measures;
16
17 my $opt_href => {
18 location => 'New York',
19 country => 'US',
20 };
21
22 my $mobj = Weather::GHCN::Measures->new( $opt_href );
23
24 say join ' ', $mobj->measures->@*;
25
26 if ( 'TMAX' =~ $mobj->re ) {
27 say 'TMAX is available';
28 }
29
30
31 =head1 DESCRIPTION
32
33 The B<Weather::GHCN::Measures> module provides a class that is used to encapsulate
34 the set of meteorological measures that the GHCN module consumer wants
35 to obtain from NOAA GHCN data. By default, the object returned by
36 instantiating this class will include TMAX (maximum daily temperature)
37 and TMIN (minimum daily temperature) as required metrics. It will also
38 include Tavg, which is derived by averaging TMAX and TMIN.
39
40 To include other metrics, notably TAVG (which is computed by the
41 weather station instrumentation), and precipitation metrics (PRCP, SNOW
42 and SNWD) see the new() method documentation.
43
44 The module is primarily for use by module Weather::GHCN::StationTable.
45
46 =cut
47
48 # these are needed because perlcritic fails to detect that Object::Pad handles these things
49 ## no critic [Modules::RequireFilenameMatchesPackage]
50 ## no critic [TestingAndDebugging::RequireUseWarnings]
51 ## no critic [TestingAndDebugging::RequireUseStrict]
52
53 use v5.18; # minimum for Object::Pad
54 use Object::Pad 0.66 qw( :experimental(init_expr) );
55
56
57 package Weather::GHCN::Measures;
58 class Weather::GHCN::Measures;
59
60 our $VERSION = 'v0.0.003';
61
62 use Const::Fast;
63
64 =head1 METHODS
65
66 =head2 new( [$opt_href] )
67
68 Create a new Measures object.
69
70 Other GHCN modules will use the measures determined by instantiating
71 this object to get those data measurements from the GHCN daily data.
72
73 The optional argument passed to new() is expected to be a reference
74 to a hash which contains kw/value pairs such as would be gathered by
75 the consumer of GHCN modules when it calls Getopt::Long with an
76 options list that includes:
77
78 qw( tavg precip anomalies )
79
80 If the key 'tavg' is present and has a true value, then the TAVG
81 measure is included.
82
83 If the key 'precip' is present and has a true value, then the PRCP,
84 SNOW and SNWD (Snow Days) measures are included.
85
86 If the key 'anomalies' is found and has a true values, then additional
87 columns are added in order to provide a place for temperature
88 anomalies to be report.
89
90 =cut
91
92 field @measures :reader;
93 field $re :reader;
94
95 =head1 FIELD ACCESSORS
96
97 =head2 measures
98
99 Returns a list of the measures applicable to the options provided
100 in the constructor.
101
102 =head2 re
103
104 Returns a regular expression object that can be used to validate
105 measure names by pattern matching. Any measure name matching this
106 B<re> is valid for the options given in the object constructor.
107
108 =head2 new( $opt_href )
109
110 Creates a new Measures object, using the options in the argument,
111 which must be a Weather::GHCN::Options object.
112
113 =cut
114
115 BUILD {
116 my ($opt_href) = @_;
117
118 $opt_href //= {};
119
120 my %opt = ( $opt_href->%* );
121
122 @measures = qw( TMAX TMIN Tavg );
123
124 push @measures, qw( TAVG ) if $opt{tavg};
125 push @measures, qw( PRCP SNOW SNWD ) if $opt{precip};
126
127 if ( $opt{anomalies} ) {
128 push @measures, qw( A_TMAX A_TMIN A_Tavg);
129 push @measures, qw( A_TAVG ) if $opt{tavg};
130 push @measures, qw( A_PRCP A_SNOW A_SNWD ) if $opt{precip};
131 }
132
133 my $m_re = join q(|), @measures;
134 $re = qr{ \A $m_re \Z }xms;
135 }
136
137
138 =head2 DOES
139
140 Defined by Object::Pad. Included for POD::Coverage.
141
142 =head2 META
143
144 Defined by Object::Pad. Included for POD::Coverage.
145
146 =head1 AUTHOR
147
148 Gary Puckering (jgpuckering@rogers.com)
149
150 =head1 LICENSE AND COPYRIGHT
151
152 Copyright 2022, Gary Puckering
153
154 =cut
155
156 1;