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

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 103 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # Weather::GHCN::Station.pm - class for Station metadata
2
3 =head1 NAME
4
5 Weather::GHCN::Station - class for Station metadata
6  
7 =head1 VERSION
8  
9 version v0.0.002
10
11 =head1 SYNOPSIS
12
13 use Weather::GHCN::Station;
14
15 my $stn_obj = Weather::GHCN::Station->new (
16 id => 'CA006105976',
17 country => 'CA',
18 state => 'ON',
19 active => '1899-2022',
20 lat => '45.3833',
21 long => '-75.7167',
22 elev => 79,
23 name => 'OTTAWA CDA',
24 gsn => '',
25 );
26
27 =head1 DESCRIPTION
28
29 The B<Weather::GHCN::Station> class is used to encapsulate the metadata for a
30 station as obtained from the NOAA Global Historical Climatology
31 Network data repository. Data is sourced from the station list and
32 the station inventory.
33
34 The module is primarily for use by Weather::GHCN::Fetch and Weather::GHCN::StationTable.
35
36 =cut
37
38 use v5.18; # minimum for Object::Pad
39 use Object::Pad 0.66 qw( :experimental(init_expr) );
40
41 package Weather::GHCN::Station;
42 class Weather::GHCN::Station;
43
44 our $VERSION = 'v0.0.002';
45
46 use Weather::GHCN::Common qw( rng_new iso_date_time );
47 use Const::Fast;
48
49 const my $EMPTY => q(); # empty string
50 const my $TAB => qq(\t); # tab character
51 const my $TRUE => 1; # perl's usual TRUE
52 const my $FALSE => not $TRUE; # a dual-var consisting of '' and 0
53
54 const my $NOAA_DATA => 'https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/all/';
55
56 # below this value is an error causing stn rejection
57 const my $ERR_THRESHOLD => 50;
58
59 # attributes
60 field $id :reader :param; # [0]
61 field $country :reader :param; # [1]
62 field $state :reader :param; # [2]
63 field $active :mutator :param; # [3]
64 field $lat :reader :param; # [4]
65 field $long :reader :param; # [5]
66 field $elev :reader :param; # [6]
67 field $name :reader :param; # [7]
68 field $gsn :reader :param; # [8]
69 field $elems_href :reader { {} }; # [9]
70 field $idx :mutator; # [10]
71 field $note_nrs :mutator { rng_new() }; # [11]
72
73 =head1 FIELD ACCESSORS
74
75 =over 4
76
77 =item id
78
79 Returns the Station id.
80
81 =item country
82
83 Returns the two-character GEC country code for the station.
84
85 =item state
86
87 Returns the two-character state code for US stations, or province code for
88 Canadian stations.
89
90 =item active
91
92 Returns the active range of the station.
93
94 =item lat
95
96 Returns the station latitude.
97
98 =item long
99
100 Returns the station longitude.
101
102 =item elev
103
104 Returns the station elevation.
105
106 =item name
107
108 Returns the station name.
109
110 =item gsn
111
112 Returns the boolean indicating whether this is a GSN station.
113
114 =item elems_href
115
116 Returns a hash reference for the measurements available from the
117 station daily data records.
118
119 =item idx
120
121 Returns an index number which is shared by stations that have the
122 same physical location; i.e. latitude and longitude. When station
123 instrumentation is changed or there are other significant changes,
124 a new station id may be assigned. This index enables you to group
125 together stations that represent the same spot but which may have
126 different active time periods.
127
128 =item note_nrs
129
130 Returns a number range string that represents notes which are errors
131 or warnings about the station.
132
133 =back
134
135 =cut
136 ######################################################################
137 # Class (:common) methods
138 ######################################################################
139
140 =head1 CLASS METHODS
141
142 =head2 new ()
143
144 Create a new Station object.
145
146
147 =head2 Headings ()
148
149 Column headings may be needed prior to creating any Station instances,
150 for example to print them before any data is loaded. This Headings
151 class method is provided to handle that situation. There is a
152 corresponding instance method.
153
154 If called in list context, a list is return; in scalar context, a
155 list reference is returned.
156
157 =cut
158
159 method Headings :common {
160 my @h = qw(StationId Country State Active Lat Long Elev Location Notes StnIdx Grid );
161 return wantarray ? @h : \@h;
162 }
163
164 ######################################################################
165 # Instance methods
166 ######################################################################
167
168 =head1 INSTANCE METHODS
169
170 =head2 headings
171
172 See Headings in Class Methods.
173
174 =cut
175
176 method headings {
177 my @h = Headings;
178 return wantarray ? @h : \@h;
179 }
180
181 =head2 add_note ($note_id, $msg=undef, $verbose=$FALSE)
182
183 This method allows you to add numbered notes to each station object.
184 It is up to the caller to assign meaning to the numbers. They are
185 stored in a range list (i.e. a Set::IntSpan::Fast object) so that
186 they can be printed compactly even if there a gaps in the number ranges.
187 For example, notes 1, 5 and 20 through 25 will print as 1,5,20-25.
188
189 The message argument is optional, but if you provide a message then
190 it will be display on STDERR if it is considered an error message.
191 The threshold is 50 (as established by the hard coded constant
192 $ERR_THRESHOLD). Anything below that is considered an error. Otherwise
193 it's considered a warning.
194
195 When the $VERBOSE argument is true, it causes warnings to also print
196 to STDERR.
197
198 =cut
199
200 method add_note ($note_id, $msg=undef, $verbose=$FALSE) {
201 $note_nrs->add_from_string($note_id);
202
203 return unless $msg;
204
205 if ($note_id < $ERR_THRESHOLD) {
206 say {*STDERR} '*E* ', $msg;
207 } else {
208 say {*STDERR} '*W* ', $msg if $verbose;
209 }
210
211 return;
212 }
213
214 =head2 row
215
216 Returns the station object as a list of field values. The fields
217 are:
218
219 $id, $country, $state, $active, $lat, $long, $elev, $name,
220 $note_text, $idx, $grid>
221
222 The second last value, $idx is a unique serial index number provided
223 by GHCH::StationTable during load_stations(). Unless set by a consumer
224 of this module, its value will be B<undef>.
225
226 The last value, $grid, is a string derived by truncating $lat and
227 $long to 1 decimal place, converting them to N/S W/E format, and
228 concatenting them. This results in a box that defines an area within
229 which there may be other nearby stations of interest.
230
231 If called in list context, a list is return; in scalar context, a
232 list reference is returned.
233
234 =cut
235
236 method row {
237
238 my $note_text = $note_nrs->cardinality ? '[' . $note_nrs->as_string . ']' : $EMPTY;
239
240 my @row = (
241 $id,
242 $country,
243 $state,
244 $active,
245 $lat,
246 $long,
247 $elev,
248 $name,
249 $note_text,
250 $idx,
251 $self->grid,
252 );
253
254 return wantarray ? @row : \@row;
255 }
256
257 =head2 coordinates
258
259 Returns the latitude and longitude coordinates of the station in
260 decimal format as a space-separated string.
261
262 =cut
263
264 method coordinates {
265 # uncoverable branch true
266 # uncoverable condition left
267 # uncoverable condition right
268 return $EMPTY unless $lat and $long;
269 return sprintf '%f %f', $lat, $long;
270 }
271
272 =head2 description
273
274 Return lines of text which describe the station in attribute: value
275 format.
276
277 =cut
278
279 method description {
280 # uncoverable branch true
281 my $is_gsn = $gsn ? 'yes' : 'no';
282 my $elems = join q( ), sort keys $elems_href->%*;
283
284 # note: those the values are lined up here, they won't be when
285 # imported into Google Earth
286 my $text = <<~"_EOT_";
287 Id: $id
288 Name: $name
289 Country: $country
290 State: $state
291 Active: $active
292 Coord: $lat $long
293 Elev: $elev
294 Is GSN? $is_gsn
295 Metrics: $elems
296 _EOT_
297
298 return $text;
299 }
300
301 =head2 error_count
302
303 Returns a count of the number of errors that were flagged for this
304 station. Errors generally make the station unsuitable for use.
305 Warnings are not included.
306
307 =cut
308
309 method error_count {
310 my $err_count = grep { $_ < $ERR_THRESHOLD } $note_nrs->as_array;
311
312 return $err_count;
313 }
314
315 =head2 grid
316
317 Returns the latitude and longitude to a single decimal place and using
318 cardinal (N/S E/W) notation. This value can be used for grouping
319 together stations that are within a 1/10th degree grid and which
320 may be assumed to experience similar weather conditions.
321
322 =cut
323
324 method grid {
325 my ($x, $y) = ($lat, $long);
326 # uncoverable branch true
327 my $xh = $x < 0 ? 'S' : 'N';
328 # uncoverable branch false
329 my $yh = $y < 0 ? 'W' : 'E';
330
331 ## no critic [ProhibitMagicNumbers]
332 # uncoverable branch true
333 $x *= $x < 0 ? -1 : 1;
334 # uncoverable branch false
335 $y *= $y < 0 ? -1 : 1;
336
337 my $grid = sprintf '%4.1f%s %4.1f%s', $x, $xh, $y, $yh;
338
339 return $grid;
340 }
341
342 =head2 selected
343
344 Returns a boolean indicating whether the station was selected for
345 data loading. Selected stations are those that meet the filtering
346 criteria (station id, station name, active range etc.) and that
347 are not flagged with errors.
348
349 =cut
350
351 method selected {
352 # uncoverable branch true
353 return $note_nrs->cardinality == 0 ? $TRUE : $FALSE;
354 }
355
356 =head2 url
357
358 Returns the URL for the station's daily data web page in the NOAA
359 GHCN data repository.
360
361 =cut
362
363 method url {
364 return $NOAA_DATA . $id . '.dly';
365 }
366
367
368 =head2 DOES
369
370 Defined by Object::Pad. Included for POD::Coverage.
371
372 =head2 META
373
374 Defined by Object::Pad. Included for POD::Coverage.
375
376 =head1 AUTHOR
377
378 Gary Puckering (jgpuckering@rogers.com)
379
380 =head1 LICENSE AND COPYRIGHT
381
382 Copyright 2022, Gary Puckering
383
384 =cut
385
386 1;