File Coverage

lib/Weather/GHCN/Measures.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 19 19 100.0


line stmt bran cond sub pod time code
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.010
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 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 [ValuesAndExpressions::ProhibitVersionStrings]
50             ## no critic [TestingAndDebugging::RequireUseWarnings]
51            
52 5     5   7283 use v5.18; # minimum for Object::Pad
  5         30  
53 5     5   1114 use Object::Pad 0.66 qw( :experimental(init_expr) );
  5         24564  
  5         28  
54            
55            
56             package Weather::GHCN::Measures;
57             class Weather::GHCN::Measures;
58            
59             our $VERSION = 'v0.0.010';
60            
61 5     5   3085 use Const::Fast;
  5         8498  
  5         30  
62            
63             =head1 METHODS
64            
65             =head2 new( [$opt_href] )
66            
67             Create a new Measures object.
68            
69             Other GHCN modules will use the measures determined by instantiating
70             this object to get those data measurements from the GHCN daily data.
71            
72             The optional argument passed to new() is expected to be a reference
73             to a hash which contains kw/value pairs such as would be gathered by
74             the consumer of GHCN modules when it calls Getopt::Long with an
75             options list that includes:
76            
77             qw( tavg precip anomalies )
78            
79             If the key 'tavg' is present and has a true value, then the TAVG
80             measure is included.
81            
82             If the key 'precip' is present and has a true value, then the PRCP,
83             SNOW and SNWD (Snow Days) measures are included.
84            
85             If the key 'anomalies' is found and has a true values, then additional
86             columns are added in order to provide a place for temperature
87             anomalies to be report.
88            
89             =cut
90            
91 1227     1227 1 4241 field @measures :reader;
  1227         3956  
92 109429     109429 1 214425 field $re :reader;
  109429         656421  
93            
94             =head1 FIELD ACCESSORS
95            
96             =head2 measures
97            
98             Returns a list of the measures applicable to the options provided
99             in the constructor.
100            
101             =head2 re
102            
103             Returns a regular expression object that can be used to validate
104             measure names by pattern matching. Any measure name matching this
105             B is valid for the options given in the object constructor.
106            
107             =head2 new( $opt_href )
108            
109             Creates a new Measures object, using the options in the argument,
110             which must be a Weather::GHCN::Options object.
111            
112             =cut
113            
114             BUILD {
115             my ($opt_href) = @_;
116            
117             $opt_href //= {};
118            
119             my %opt = ( $opt_href->%* );
120            
121             @measures = qw( TMAX TMIN Tavg );
122            
123             push @measures, qw( TAVG ) if $opt{tavg};
124             push @measures, qw( PRCP SNOW SNWD ) if $opt{precip};
125            
126             if ( $opt{anomalies} ) {
127             push @measures, qw( A_TMAX A_TMIN A_Tavg);
128             push @measures, qw( A_TAVG ) if $opt{tavg};
129             push @measures, qw( A_PRCP A_SNOW A_SNWD ) if $opt{precip};
130             }
131            
132             my $m_re = join q(|), @measures;
133             $re = qr{ \A $m_re \Z }xms;
134             }
135            
136            
137             =head2 DOES
138            
139             Defined by Object::Pad. Included for POD::Coverage.
140            
141             =head2 META
142            
143             Defined by Object::Pad. Included for POD::Coverage.
144            
145             =head1 AUTHOR
146            
147             Gary Puckering (jgpuckering@rogers.com)
148            
149             =head1 LICENSE AND COPYRIGHT
150            
151             Copyright 2022, Gary Puckering
152            
153             =cut
154            
155             1;