ReadGRIB/PlaceIterator.pm

filter: Statementstmt 92.1% 94 / 102 Branchbran 73.3% 22 / 30 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 86.6% 13 / 15 Podpod 100% 8 / 8 totaltotal 85.9% 141 / 164 CC 19 SCAR 11.9
CC
19
Cov
87%
numLong
6.0
numLat
6.0
_setIndex
4.0
CRAP 3.3
« ReadGRIB.pm ↑ Summary ReadGRIB/Place.pm »
1 #
2 #===============================================================================
3 #
4 # FILE: PlaceIterator.pm
5 #
6 # DESCRIPTION: A Geo::ReadGRIB::PlaceIterator object contains a collection of
7 # Geo::ReadGRIB data for a regular area and has methods to return
8 # Geo::ReadGRIB::Place in sorted sequence of locations
9 #
10 # FILES: ---
11 # BUGS: ---
12 # NOTES: ---
13 # AUTHOR: Frank Lyon Cox (Dr), <frank@pwizardry.com>
14 # COMPANY: Practial Wizardry
15 # VERSION: 1.0
16 # CREATED: 2/2/2009 12:15:57 PM Pacific Standard Time
17 # REVISION: ---
18 #===============================================================================
19  
20 package Geo::ReadGRIB::PlaceIterator;
21  
22 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 13 ▶use Geo::ReadGRIB::Place;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 13 our $VERSION = 1.0;
27  
28 #--------------------------------------------------------------------------
29 # new( )
30 #--------------------------------------------------------------------------
31 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 14 my $class = shift;
33  
34 14 my $self = {};
35 14 bless $self, $class;
36 14 $self->_init;
37 14 return $self;
38 }
39  
40  
41 #--------------------------------------------------------------------------
42 # _init( )
43 #--------------------------------------------------------------------------
44 14 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
45 14 my $self = shift;
46  
47 14 $self->{shifted_west_by} = 0;
48 14 $self->isSorted( undef );
49 14 $self->{place_index} = 0;
50 14 $self->{place_sorted} = [];
51 14 $self->{x_y} = [];
52 14 $self->{count_of_places} = 0;
53 14 return;
54 }
55  
56 #--------------------------------------------------------------------------
57 # first( )
58 #--------------------------------------------------------------------------
59 6 ▶sub first {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
60 6 my $self = shift;
61 6 ▶ $self->_indexData() if not $self->isSorted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSorted 2 4 6
62 6 $self->_setIndex( 0 );
63 6 return;
64 }
65  
66  
67 #--------------------------------------------------------------------------
68 # next( )
69 #--------------------------------------------------------------------------
70 609 ▶sub next {
Subroutine next: called CC=3 SCAR=12.0
Pod documented
71 609 my $self = shift;
72 609 ▶ $self->_indexData() if not $self->isSorted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSorted 0 609 609
73 609 ▶ if ( $self->_setIndex == $self->{count_of_places} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_setIndex == $self->{"count_of_places"}) 0 609 609
74 0 return;
75 }
76 609 return $self->_setIndex('+');
77 }
78  
79 #--------------------------------------------------------------------------
80 # current( )
81 #
82 # returns a Geo::ReadGRIB::Place object
83 #--------------------------------------------------------------------------
84 605 ▶sub current {
Subroutine current: called CC=4 SCAR=13.9
Pod documented
85 605 my $self = shift;
86  
87 605 ▶ $self->_indexData() if not $self->isSorted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSorted 2 603 605
88  
89 605 my $index = $self->_setIndex;
90  
91 605 my $p = Geo::ReadGRIB::Place->new;
92  
93 605 $p->thisTime( $self->{place_sorted}->[ $index ]->[0] );
94 605 $p->lat( $self->{place_sorted}->[ $index ]->[1] );
95 605 $p->long( $self->{place_sorted}->[ $index ]->[2] );
96  
97 605 ▶ if ( not defined $p->thisTime ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p->thisTime) 1 604 605
98 1 return;
99 }
100  
101 604 for my $type ( keys %{ $self->{data}->{$p->thisTime}->{$p->lat}->{$p->long} } ) {
102 604 $p->types( $type );
103 604 $p->data( $type, $self->{data}->{$p->thisTime}->{$p->lat}->{$p->long}->{$type} );
104 }
105  
106 604 return $p;
107 }
108  
109  
110 #--------------------------------------------------------------------------
111 # numLong( )
112 #--------------------------------------------------------------------------
113 0 ▶sub numLong {
Subroutine numLong: not called CC=2 SCAR=17.9
Pod documented
114 0 my $self = shift;
115 0 ▶ $self->_indexData() if not $self->isSorted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSorted 0 0 0
116 0 return $self->{x_y}->[0];
117 }
118  
119 #--------------------------------------------------------------------------
120 # numLat( )
121 #--------------------------------------------------------------------------
122 0 ▶sub numLat {
Subroutine numLat: not called CC=2 SCAR=17.9
Pod documented
123 0 my $self = shift;
124 0 ▶ $self->_indexData() if not $self->isSorted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSorted 0 0 0
125 0 return $self->{x_y}->[1];
126 }
127  
128 #--------------------------------------------------------------------------
129 # _setIndex( )
130 #--------------------------------------------------------------------------
131 1829 ▶sub _setIndex {
Subroutine _setIndex: called CC=4 SCAR=14.0
132 1829 my $self = shift;
133 1829 my $index = shift;
134  
135 1829 ▶ if ( defined $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 615 1214 1829
136 615 ▶ if ( $index eq '+' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index eq '+') { } 609 6 615
137 609 $self->{place_index} = $self->{place_index} + 1;
138 }
139 else {
140 6 $self->{place_index} = $index;
141 }
142 }
143  
144 1829 ▶ if ( $self->{place_index} > $self->{count_of_places} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"place_index"} > $self->{"count_of_places"}) 0 1829 1829
145 0 return;
146 }
147  
148 1829 return $self->{place_index};
149 }
150  
151 #--------------------------------------------------------------------------
152 # isSorted( )
153 #--------------------------------------------------------------------------
154 1240 ▶sub isSorted {
Subroutine isSorted: called CC=2 SCAR=6.9
Pod documented
155 1240 my $self = shift;
156 1240 my $bool = shift;
157 1240 ▶ if ( $bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bool) 4 1236 1240
158 4 $self->{is_sorted} = 1;
159 }
160 1240 return $self->{is_sorted};
161 }
162  
163 #--------------------------------------------------------------------------
164 # _indexData( )
165 #
166 # Inspect the data section and prepare a sorted list of
167 # [time, lat, long]
168 #
169 # -- we also count the number of lats and longs
170 #
171 # Object data is updated
172 #--------------------------------------------------------------------------
173 4 ▶sub _indexData {
Subroutine _indexData: called CC=4 SCAR=13.9
174 4 my $self = shift;
175  
176 # Inspect the data section and prepare a sorted list of
177 # [time, lat, long] and an array of times
178 # -- we also count the number of lats and longs
179  
180 4 my ( @tm, $these, $dataType );
181  
182 4 my ( $thisTime ) = keys %{ $self->{data} };
183  
184 4 my ( $laC, $loC );
185 4 ▶ while ( my ( $time, $laf ) = each %{ $self->{data} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"data"};}) 4 4 8
186 4 $laC = 0;
187 4 ▶ while ( my ( $la, $lof ) = each %$laf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$laf) 609 4 613
188 609 ▶ while ( my ( $lo, $dtf ) = each %$lof ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$lof) 621 609 1230
189 621 $loC++;
190 621 push @$these, [ $time, $la, $lo ];
191 621 ($dataType) = each %$dtf;
192 }
193 }
194 }
195  
196 4 $self->{x_y} = [ $loC / scalar keys %{ $self->{data}->{$thisTime} },
197 4 scalar keys %{ $self->{data}->{$thisTime} } ];
198  
199 4 $self->{place_sorted} = $self->_sortThese($these);
200 4 $self->{count_of_places} = $loC;
201 4 $self->isSorted( 1 );
202 4 $self->first;
203  
204 4 return;
205 }
206  
207 #--------------------------------------------------------------------------
208 # _sortThese( )
209 #--------------------------------------------------------------------------
210 4 ▶sub _sortThese {
Subroutine _sortThese: called CC=3 SCAR=11.2
211 4 my $self = shift;
212 4 my $coArray = shift;
213  
214 # sort by lat and then by long
215 my @psort = sort {
216 4 ▶ $a->[0] <=> $b->[0] || $b->[1] <=> $a->[1] || $a->[2] <=> $b->[2]
Condition66%4/6
$Geo::ReadGRIB::PlaceIterator::a->[0] <=> $Geo::ReadGRIB::PlaceIterator::b->[0] || $Geo::ReadGRIB::PlaceIterator::b->[1] <=> $Geo::ReadGRIB::PlaceIterator::a->[1] || $Geo::ReadGRIB::PlaceIterator::a->[2] <=> $Geo::ReadGRIB::PlaceIterator::b->[2]
l!l&&r!l&&!r
4250170
$Geo::ReadGRIB::PlaceIterator::a->[0] <=> $Geo::ReadGRIB::PlaceIterator::b->[0] or $Geo::ReadGRIB::PlaceIterator::b->[1] <=> $Geo::ReadGRIB::PlaceIterator::a->[1]
l!l&&r!l&&!r
0425017
MC/DC0%0/3
$Geo::ReadGRIB::PlaceIterator::a->[0] <=> $Geo::ReadGRIB::PlaceIterator::b->[0] || $Geo::ReadGRIB::PlaceIterator::b->[1] <=> $Geo::ReadGRIB::PlaceIterator::a->[1] || $Geo::ReadGRIB::PlaceIterator::a->[2] <=> $Geo::ReadGRIB::PlaceIterator::b->[2]
$Geo::ReadGRIB::PlaceIterator::a->[0] <=> $Geo::ReadGRIB::PlaceIterator::b->[0] $Geo::ReadGRIB::PlaceIterator::b->[1] <=> $Geo::ReadGRIB::PlaceIterator::a->[1] $Geo::ReadGRIB::PlaceIterator::a->[2] <=> $Geo::ReadGRIB::PlaceIterator::b->[2]
Truth table50%2/4
A or B || C
A= $Geo::ReadGRIB::PlaceIterator::a->[0] <=> $Geo::ReadGRIB::PlaceIterator::b->[0]B= $Geo::ReadGRIB::PlaceIterator::b->[1] <=> $Geo::ReadGRIB::PlaceIterator::a->[1]C= $Geo::ReadGRIB::PlaceIterator::a->[2] <=> $Geo::ReadGRIB::PlaceIterator::b->[2]
ABCresult
1XX1
01X1
0011
0000
217 } @$coArray;
218  
219 4 return \@psort;
220 }
221  
222 #--------------------------------------------------------------------------
223 # addData( time, la, lo, type, data )
224 #
225 # Add a unit of data to internal structure
226 #--------------------------------------------------------------------------
227 1091 ▶sub addData {
Subroutine addData: called CC=2 SCAR=6.9
Pod documented
228 1091 my $self = shift;
229 1091 my $time = shift;
230 1091 my $la = shift;
231 1091 my $lo = shift;
232 1091 my $type = shift;
233 1091 my $data = shift;
234  
235 1091 ▶ $self->{data}->{$time}->{$la}->{$lo}->{$type} = $data if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 1091 0 1091
236 1091 $self->{is_sorted} = undef;
237  
238 1091 return $self->{data};
239 }
240  
241  
242  
243 13 1;
244  
245  
246  
247 #---------------------------------------------------------------------------
248 # Module Documentation
249 #---------------------------------------------------------------------------
250  
251 =head1 NAME
252  
253 Geo::ReadGRIB::PlaceIterator - Provides methods to iterate through GRIB data
254 in geographic order and to return Geo::ReadGRIB::Place objects for each location.
255  
256 =head1 VERSION
257  
258 This documentation refers to Geo::ReadGRIB::PlaceIterator version 1.0
259  
260 =head1 SYNOPSIS
261  
262 use Geo::ReadGRIB;
263  
264 $w = new Geo::ReadGRIB "grib-file";
265 $w->getFullCatalog;
266  
267 print $w->show,"\n";
268
269 $plit = $w->extractLaLo(data_type, lat1, long1, lat2, long2, time);
270 die $w->getError if $w->getError;
271  
272 # $plit is a Geo::ReadGRIB::PlaceIterator
273  
274 for $y ( 0 .. $plit->numLat -1 ) {
275 for $x ( 0 .. $plit->numLong -1 ) {
276
277 my $place = $plit->current;
278
279 # $place is a Geo::ReadGRIB::Place object
280  
281 $time = $place->thisTime;
282 $latitude = $place->lat;
283 $longitude = $place->long;
284 $data_types = $place->types; # an array ref of type names
285 $data = $place->data(data_type);
286  
287 # process data for $x, $y
288  
289 $plit->next;
290 }
291 }
292  
293  
294 =head1 DESCRIPTION
295  
296 A PlaceIterator objects let you iterate through places.
297  
298 Objects of this class are returned by the extractLaLo() or extract() method of a
299 Geo::ReadGRIB object. It contains data for one or more data types at one or more
300 times for the area extracted. The locations are sorted first by time
301 then latitude and then by longitude. Methods are provided for sequential access
302 to this data.
303  
304 The first() method sets the iterator index to the most northwest (lat, long)
305 pair. The current() method returns a Place object for the current location.
306 The next() method advances the current location one place in rows from west to
307 east and north to south starting in the northwest corner and ending in the
308 southeast. After the last item in the southeast corner of the extracted region,
309 another call to next() returns B<undef>.
310  
311 The convenience methods numLat() and numLong() return the number of latitude and
312 longitude points respectively. This is handy to, for example, provide the x, y
313 dimensions for a GD::Image new() method.
314  
315 =head1 METHODS
316  
317 =over 4
318  
319 =item $object->new;
320  
321 =item $object->first;
322  
323 =item $object->next;
324  
325 =item $place = $object->current;
326  
327 =item $y = $object->numLat;
328  
329 =item $y = $object->numLong;
330  
331 =item $y = $object->addData;
332  
333 =item $y = $object->isSorted;
334  
335 =back
336  
337 =head1 BUGS AND LIMITATIONS
338  
339 There are no known bugs in this module. Please report problems through
340  
341 http://rt.cpan.org
342  
343 or contact Frank Cox, <frank.l.cox@gmail.com> Patches are welcome.
344  
345 =head1 AUTHOR
346  
347 Frank Cox, E<lt>frank.l.cox@gmail.comE<gt>
348  
349  
350 =head1 LICENCE AND COPYRIGHT
351  
352 2009 by Frank Cox
353  
354 This library is free software; you can redistribute it and/or modify
355 it under the same terms as Perl itself, either Perl version 5.8.4 or,
356 at your option, any later version of Perl 5 you may have available.
357  
358  
359 =cut
360  
« ReadGRIB.pm ↑ Summary ReadGRIB/Place.pm »