ReadGRIB.pm

filter: Statementstmt 82.0% 365 / 445 Branchbran 68.2% 116 / 170 Conditioncond 43.7% 21 / 48 MC/DCmcdc 20.6% 6 / 29 Subroutinesub 83.6% 41 / 49 Podpod 100% 36 / 36 totaltotal 75.2% 585 / 777 CC 107 SCAR 25.9
CC
107
Cov
76%
extractLaLo
42.9
extract
25.2
calInc
20.0
CRAP 13.3
↑ Summary ReadGRIB/PlaceIterator.pm »
1 #--------------------------------------------------------------------------
2 # Geo::ReadGRIB
3 #
4 # - A Perl extension that gives read access to GRIB "GRIdded Binary"
5 # format Weather data files.
6 #
7 # - Copyright (C) 2006 by Frank Cox
8 #--------------------------------------------------------------------------
9  
10 package Geo::ReadGRIB;
11  
12 13 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 our $VERSION = 1.4;
19  
20 13 ▶use Geo::ReadGRIB::PlaceIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 13 my $LIB_DIR = "./";
23  
24 # try to find wgrib.exe
25 13 foreach my $inc (@INC) {
26 30 ▶ if ( -e "$inc/Geo/wgrib.exe" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$inc/Geo/wgrib.exe") 13 17 30
27 13 $LIB_DIR = "$inc/Geo";
28 13 last;
29 }
30 }
31  
32 13 ▶unless ( -e "$LIB_DIR/wgrib.exe" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$LIB_DIR/wgrib.exe") 0 13 13
33 0 die "CAN'T CONTINUE: Path to wgrib.exe not found";
34 }
35  
36 ## Set some signal handlers to clean up temp files in case of interruptions
37 # it does this by calling exit(0) which will run the END block
38  
39 0 ▶$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 print "ReadGRIB attempting cleanup...\n";
41 0 exit(0);
42 13 };
43  
44 13 ▶END {
Subroutine END: called CC=1 SCAR=0.0
45 13 ▶ unlink glob("wgrib.tmp.*");
Subroutine BEGIN: called CC=1 SCAR=0.0
46 }
47  
48 #--------------------------------------------------------------------------
49 # new()
50 #--------------------------------------------------------------------------
51 13 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
52  
53 13 my $class = shift;
54 13 my $gFile = shift;
55 13 ▶ unless ( defined $gFile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gFile) 0 13 13
56 0 croak "new(): Usage: Geo::ReadGRIB->new(GRIB_FILE)";
57 }
58 13 my $self = {};
59 13 bless $self, $class;
60  
61 13 $self->{fileName} = $gFile;
62 13 $self->{DEBUG} = 0;
63 13 $self->backflip( 0 );
64  
65 13 $self->openGrib();
66  
67 13 return $self;
68 }
69  
70 #--------------------------------------------------------------------------
71 # openGrib()
72 #
73 # Open grib file using wgrib.exe and extract header data
74 #
75 # Version 1.0 added a call to _getCatalog() here to get all critical
76 # header data
77 #--------------------------------------------------------------------------
78 13 ▶sub openGrib {
Subroutine openGrib: called CC=6 SCAR=18.0
Pod documented
79  
80 13 ▶ use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 13 my $self = shift;
83  
84 13 my $tmp = $self->tempfile();
85 13 my $cmd = "\"$LIB_DIR\"/wgrib.exe \"$self->{fileName}\" -d 1 -4yr -PDS10 -GDS10 -text -nh -o $tmp";
86  
87 13 my $header = qx($cmd);
88 13 unlink $tmp;
89  
90 13 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 13 13
91 0 croak "Error in $cmd: $?";
92 }
93  
94 13 my @inv = split /:/, $header;
95  
96 13 my ( $arg, $val, %head );
97  
98 13 $head{recNum} = $inv[0];
99 13 $head{offset} = $inv[1];
100 13 $head{name} = $inv[3];
101 13 $head{level} = $inv[11];
102 13 $head{fcst} = $inv[12];
103  
104 13 foreach my $invel (@inv) {
105 208 chomp $invel;
106  
107 # print "$invel \n";
108 208 ▶ if ( $invel =~ /=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invel =~ /=/) 143 65 208
109 143 ( $arg, $val ) = split /=/, $invel;
110 143 $val =~ s/^\s+//;
111 143 $head{$arg} = $val;
112  
113 # print " ($arg,$val) \n";
114 }
115 }
116  
117 13 foreach ( sort keys %head ) {
118 # print " $_: $head{$_}\n";
119 208 $self->{$_} = $head{$_};
120 }
121  
122  
123 # reduce date string to 'time' format
124 13 my ( $yr, $mo, $day, $hr ) = unpack 'A4A2A2A2', $self->{d};
125 13 $self->{TIME} = timegm( 0, 0, $hr, $day, $mo - 1, $yr - 1900 );
126  
127 13 $self->{LAST_TIME} = $self->{THIS_TIME} = $self->{TIME};
128  
129 13 $self->parseGDS( $head{GDS10} );
130  
131 # if this isn't a lat/long grid we can't go on
132 13 ▶ if ( $self->{GRID_TYPE} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"GRID_TYPE"} != 0) 0 13 13
133 0 croak "GDS byte 6 not 0: Only latitude/longitude grids are currently supported";
134 }
135  
136 13 $self->_getCatalog;
137  
138 13 return;
139 }
140  
141 #--------------------------------------------------------------------------
142 # getCatalogVerbose() DEPRECATED. Use getFullCatalog() instead
143 #
144 # This method is now redundent and just calls getFullCatalog() and sets
145 # an error.
146 #--------------------------------------------------------------------------
147 0 ▶sub getCatalogVerbose {
Subroutine getCatalogVerbose: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self = shift;
149 0 $self->error( "Method getCatalogVerbose() DEPRECATED and is now redundant "
150 . "in Geo::ReadGRIB V0.98 and above. Use getFullCatalog() instead" );
151 0 $self->getFullCatalog();
152 0 return 1;
153 }
154  
155  
156 #--------------------------------------------------------------------------
157 # getCatalog( ) DEPRECATED. It now does nothing but set an error.
158 # Do not use in new code and please remove it from old code.
159 #
160 # The catalog is now fetched in openGRIB() and this method dose not need
161 # to be called.
162 #--------------------------------------------------------------------------
163 0 ▶sub getCatalog {
Subroutine getCatalog: not called CC=1 SCAR=6.9
Pod documented
164 0 my $self = shift;
165 0 $self->error( "Method getCatalog DEPRECATED and is no longer needed "
166 . "in Geo::ReadGRIB V1.0 and above" );
167 0 return 1;
168 }
169  
170 #--------------------------------------------------------------------------
171 # _getCatalog()
172 #--------------------------------------------------------------------------
173 13 ▶sub _getCatalog {
Subroutine _getCatalog: called CC=6 SCAR=18.1
174  
175 13 my $self = shift;
176  
177 13 my $tmp = $self->tempfile();
178 13 my $cmd = "\"$LIB_DIR\"/wgrib.exe \"$self->{fileName}\" -o $tmp";
179  
180 13 my @cat = qx($cmd);
181 13 unlink $tmp;
182  
183 13 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 13 13
184 0 croak "Error in \$cmd: $?";
185 }
186  
187 13 my $timeRange = $self->TR;
188  
189 13 my ( @line, $offset );
190 13 foreach (@cat) {
191 433 @line = split /:/;
192 433 $line[8] =~ s/P1=//;
193 433 $line[9] =~ s/P2=//;
194 433 ▶ if ( $timeRange == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($timeRange == 0) { } 427 6 433
elsif ($timeRange == 10) { } 6 0 6
195 427 $offset = $line[8];
196 }
197 elsif ( $timeRange == 10 ) {
198 6 $offset = ($line[8] * 256) + $line[9];
199 }
200 else {
201 0 croak "Time Range flag $timeRange not yet supported. Please ask the Geo::ReadGRIB maintainer to add support for GRIB files like this";
202 }
203  
204 433 $offset = $offset * 3600 + $self->{TIME};
205 433 ▶ $self->{LAST_TIME} = $offset if $offset > $self->{LAST_TIME};
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $self->{"LAST_TIME"} 420 13 433
206 433 $self->{catalog}->{ $offset }->{ $line[3] } = $line[0];
207 }
208  
209 13 return;
210 }
211  
212 #--------------------------------------------------------------------------
213 # getFullCatalog()
214 #
215 # recovers the verbose catalog which has text discriptions
216 # of data items.
217 #--------------------------------------------------------------------------
218 9 ▶sub getFullCatalog{
Subroutine getFullCatalog: called CC=3 SCAR=11.0
Pod documented
219  
220 9 my $self = shift;
221  
222 9 my $tmp = $self->tempfile();
223 9 my $cmd = "\"$LIB_DIR\"/wgrib.exe -v \"$self->{fileName}\" -o $tmp";
224  
225 9 my @cat = qx($cmd);
226 9 unlink $tmp;
227  
228 9 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 9 9
229 0 croak "Error in \$cmd: $?";
230 }
231  
232 9 my @line;
233 9 foreach (@cat) {
234 309 chomp;
235 309 @line = split /:/;
236 309 $line[7] =~ s/"//g;
237 309 $self->{v_catalog}->{ $line[3] } = $line[7];
238 }
239 9 return;
240 }
241  
242 #--------------------------------------------------------------------------
243 # parseGDS()
244 #
245 # Assumes gds is dumped in "decimal" (-GDS10)
246 #--------------------------------------------------------------------------
247 13 ▶sub parseGDS {
Subroutine parseGDS: called CC=9 SCAR=23.8
Pod documented
248  
249 13 my $self = shift;
250 13 my $gds = shift;
251  
252 13 $gds =~ s/^\s+//;
253  
254 13 my @GDS = split /\s+/, $gds;
255  
256 13 $self->{GRID_TYPE} = $GDS[5];
257  
258 13 my @slice = @GDS[ 6, 7 ];
259 13 $self->Ni( $self->toDecimal( \@slice ) );
260  
261 13 @slice = @GDS[ 8, 9 ];
262 13 $self->Nj( $self->toDecimal( \@slice ) );
263  
264 13 @slice = @GDS[ 10, 11, 12 ];
265 13 $self->La1( $self->toDecimal( \@slice ) / 1000 );
266  
267 13 @slice = @GDS[ 13, 14, 15 ];
268 13 $self->Lo1( $self->toDecimal( \@slice ) / 1000 );
269  
270 13 my @rc_flags = split '', sprintf "%08b", $GDS[16];
271
272 # if INCREMENTS_FLAG is true get increments from GDS else calculate
273 13 $self->{INCREMENTS_FLAG} = $rc_flags[0];
274  
275 13 @slice = @GDS[ 17, 18, 19 ];
276 13 $self->La2( $self->toDecimal( \@slice ) / 1000 );
277  
278 13 @slice = @GDS[ 20, 21, 22 ];
279 13 $self->Lo2( $self->toDecimal( \@slice ) / 1000 );
280  
281 13 @slice = @GDS[ 23, 24 ];
282 13 ▶ if ( ($slice[0] == $slice[1] and $slice[0] == 255) and not $self->{INCREMENTS_FLAG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slice[0] == $slice[1] and $slice[0] == 255 and not $self->{'INCREMENTS_FLAG'}) { } 0 13 13
Condition33%2/6
$slice[0] == $slice[1] and $slice[0] == 255 and not $self->{"INCREMENTS_FLAG"}
!ll&&!rl&&r
1300
$slice[0] == $slice[1] and $slice[0] == 255
!ll&&!rl&&r
1300
MC/DC0%0/3
$slice[0] == $slice[1] and $slice[0] == 255 and not $self->{"INCREMENTS_FLAG"}
$slice[0] == $slice[1] $slice[0] == 255 not $self->{"INCREMENTS_FLAG"}
Truth table25%1/4
A and B and not(C)
A= $slice[0] == $slice[1]B= $slice[0] == 255C= not $self->{"INCREMENTS_FLAG"}
ABCresult
0XX0
10X0
1100
1111
283 0 $self->LoInc( $self->calInc( $self->Lo1, $self->Lo2, $self->Ni ) );
284 }
285 else {
286 13 $self->LoInc( $self->toDecimal( \@slice ) / 1000 );
287 }
288  
289 13 @slice = @GDS[ 25, 26 ];
290 13 ▶ if ( ($slice[0] == $slice[1] and $slice[0] == 255) and not $self->{INCREMENTS_FLAG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slice[0] == $slice[1] and $slice[0] == 255 and not $self->{'INCREMENTS_FLAG'}) { } 0 13 13
Condition33%2/6
$slice[0] == $slice[1] and $slice[0] == 255 and not $self->{"INCREMENTS_FLAG"}
!ll&&!rl&&r
1300
$slice[0] == $slice[1] and $slice[0] == 255
!ll&&!rl&&r
1300
MC/DC0%0/3
$slice[0] == $slice[1] and $slice[0] == 255 and not $self->{"INCREMENTS_FLAG"}
$slice[0] == $slice[1] $slice[0] == 255 not $self->{"INCREMENTS_FLAG"}
Truth table25%1/4
A and B and not(C)
A= $slice[0] == $slice[1]B= $slice[0] == 255C= not $self->{"INCREMENTS_FLAG"}
ABCresult
0XX0
10X0
1100
1111
291 0 $self->LaInc = $self->calInc( $self->La1, $self->La2, $self->Nj );
292 }
293 else {
294 13 $self->LaInc( $self->toDecimal( \@slice ) / 1000 );
295 }
296  
297 13 my @scan_mode_flags = split '', sprintf "%08b", $GDS[27];
298  
299 # if SN_SCAN_FLAG true data runs in numeric order south-north
300 # elce data runs north-south
301 13 $self->sn_scan_flag( $scan_mode_flags[1] );
302  
303 13 ▶ if ( $scan_mode_flags[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan_mode_flags[0]) 0 13 13
304 0 croak "Scan mode -i not yet supported. Please contact the Geo::ReadGRIB maintainer to add support for GRIB files that scan east to west";
305 }
306  
307 13 ▶ if ( $scan_mode_flags[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan_mode_flags[2]) 0 13 13
308 0 croak "Scan mode (J,I) not yet supported. Please contact the Geo::ReadGRIB maintainer to add support for GRIB files where adjacent points are consecutive in the j direction (north/south)";
309 }
310  
311 # Calculate effective Lo where thay are shifted west to 0 degrees.
312 # This will be used for finding data offset and for checking for
313 # out of range args where ranges may cross long 0
314 13 $self->{eLo2} = $self->Lo2;
315 13 $self->{eLo1} = $self->Lo2 - ($self->Ni -1) * $self->LoInc;
316 13 $self->{Lo_SHIFT} = 0 - $self->{eLo1};
317  
318 13 return;
319 }
320  
321 #--------------------------------------------------------------------------
322 # sn_scan_flag( [flag] ) - getter/setter
323 #--------------------------------------------------------------------------
324 1335 ▶sub sn_scan_flag {
Subroutine sn_scan_flag: called CC=2 SCAR=6.9
Pod documented
325 1335 my ( $self, $flag ) = @_;
326 1335 ▶ $self->{SN_SCAN_FLAG} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 1322 1335
327 1335 return $self->{SN_SCAN_FLAG};
328 }
329  
330 #--------------------------------------------------------------------------
331 # error( [error message] ) - getter/setter for errors
332 #--------------------------------------------------------------------------
333 42 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod documented
334 42 my ( $self, $error ) = @_;
335 42 ▶ $self->{ERROR} = $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 7 35 42
336 42 return $self->{ERROR};
337 }
338  
339 #--------------------------------------------------------------------------
340 # Lo1( [val] ) - Getter/setter for Lo1
341 #--------------------------------------------------------------------------
342 103 ▶sub Lo1 {
Subroutine Lo1: called CC=2 SCAR=6.9
Pod documented
343 103 my ( $self, $flag ) = @_;
344 103 ▶ $self->{Lo1} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 90 103
345 103 return $self->{Lo1};
346 }
347  
348 #--------------------------------------------------------------------------
349 # Lo2( [flag] ) - Getter/setter for Lo2
350 #--------------------------------------------------------------------------
351 71 ▶sub Lo2 {
Subroutine Lo2: called CC=2 SCAR=6.9
Pod documented
352 71 my ( $self, $flag ) = @_;
353 71 ▶ $self->{Lo2} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 58 71
354 71 return $self->{Lo2};
355 }
356  
357 #--------------------------------------------------------------------------
358 # LoInc( [flag] ) - Getter/setter for LoInc
359 #--------------------------------------------------------------------------
360 1315 ▶sub LoInc {
Subroutine LoInc: called CC=2 SCAR=6.9
Pod documented
361 1315 my ( $self, $flag ) = @_;
362 1315 ▶ $self->{LoInc} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 1302 1315
363 1315 return $self->{LoInc};
364 }
365  
366 #--------------------------------------------------------------------------
367 # La1( [flag] ) - Getter/setter for La1
368 #--------------------------------------------------------------------------
369 1334 ▶sub La1 {
Subroutine La1: called CC=2 SCAR=6.9
Pod documented
370 1334 my ( $self, $flag ) = @_;
371 1334 ▶ $self->{La1} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 1321 1334
372 1334 return $self->{La1};
373 }
374  
375 #--------------------------------------------------------------------------
376 # TR( [flag] ) - Getter/setter for TR
377 #--------------------------------------------------------------------------
378 13 ▶sub TR {
Subroutine TR: called CC=2 SCAR=7.1
Pod documented
379 13 my ( $self, $flag ) = @_;
380 13 ▶ $self->{TR} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 0 13 13
381 13 return $self->{TR};
382 }
383  
384 #--------------------------------------------------------------------------
385 # La2( [flag] ) - Getter/setter for La2
386 #--------------------------------------------------------------------------
387 58 ▶sub La2 {
Subroutine La2: called CC=2 SCAR=6.9
Pod documented
388 58 my ( $self, $flag ) = @_;
389 58 ▶ $self->{La2} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 45 58
390 58 return $self->{La2};
391 }
392  
393 #--------------------------------------------------------------------------
394 # LaInc( [flag] )- Getter/setter for LaInc
395 #--------------------------------------------------------------------------
396 1937 ▶sub LaInc {
Subroutine LaInc: called CC=2 SCAR=6.9
Pod documented
397 1937 my ( $self, $flag ) = @_;
398 1937 ▶ $self->{LaInc} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 1924 1937
399 1937 return $self->{LaInc};
400 }
401  
402 #--------------------------------------------------------------------------
403 # Ni( [flag] ) - Getter/setter for Ni
404 #--------------------------------------------------------------------------
405 1309 ▶sub Ni {
Subroutine Ni: called CC=2 SCAR=6.9
Pod documented
406 1309 my ( $self, $flag ) = @_;
407 1309 ▶ $self->{Ni} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 1296 1309
408 1309 return $self->{Ni};
409 }
410  
411 #--------------------------------------------------------------------------
412 # Nj( [flag] ) - Getter/setter for Nj
413 #--------------------------------------------------------------------------
414 13 ▶sub Nj {
Subroutine Nj: called CC=2 SCAR=7.1
Pod documented
415 13 my ( $self, $flag ) = @_;
416 13 ▶ $self->{Nj} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 13 0 13
417 13 return $self->{Nj};
418 }
419  
420 #--------------------------------------------------------------------------
421 # clearError()
422 # set errors undef
423 #--------------------------------------------------------------------------
424 1273 ▶sub clearError {
Subroutine clearError: called CC=1 SCAR=0.0
Pod documented
425 1273 my $self = shift;
426 1273 undef $self->{ERROR};
427 }
428  
429  
430 #--------------------------------------------------------------------------
431 # validLa( lat )
432 #
433 # check if lats are in range and return true if they arr, else false
434 #--------------------------------------------------------------------------
435 27 ▶sub validLa {
Subroutine validLa: called CC=6 SCAR=17.9
Pod documented
436  
437 27 my $self = shift;
438 27 my $lat = shift;
439  
440 27 ▶ if ( $self->sn_scan_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sn_scan_flag) { } 13 14 27
441 13 ▶ if ( $lat < $self->La1 or $lat > $self->La2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat < $self->La1 or $lat > $self->La2) 3 10 13
Condition100%3/3
$lat < $self->La1 or $lat > $self->La2
l!l&&r!l&&!r
1210
MC/DC100%2/2
$lat < $self->La1 or $lat > $self->La2
$lat < $self->La1 $lat > $self->La2
Truth table100%3/3
A or B
A= $lat < $self->La1B= $lat > $self->La2
ABresult
1X1
011
000
442 3 return 0;
443 }
444 }
445 else {
446 14 ▶ if ( $lat > $self->La1 or $lat < $self->La2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat > $self->La1 or $lat < $self->La2) 2 12 14
Condition66%2/3
$lat > $self->La1 or $lat < $self->La2
l!l&&r!l&&!r
2012
MC/DC50%1/2
$lat > $self->La1 or $lat < $self->La2
$lat > $self->La1 $lat < $self->La2
Truth table66%2/3
A or B
A= $lat > $self->La1B= $lat < $self->La2
ABresult
1X1
011
000
447 2 return 0;
448 }
449 }
450  
451  
452 22 return 1;
453 }
454  
455 #--------------------------------------------------------------------------
456 # validLo( 1ong )
457 #
458 # check that longs are in range and return true if they are, else false.
459 #--------------------------------------------------------------------------
460 22 ▶sub validLo {
Subroutine validLo: called CC=7 SCAR=20.3
Pod documented
461  
462 22 my $self = shift;
463 22 my $lo = shift;
464  
465 22 ▶ if ( $self->sn_scan_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sn_scan_flag) { } 10 12 22
466 10 $lo += $self->{Lo_SHIFT};
467 10 ▶ if ( $lo > 360 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo > 360) 0 10 10
468 0 $lo -= 360;
469 }
470
471 10 $lo /= $self->LoInc;
472 10 ▶ if ( $lo < 0 or $lo > $self->Ni ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo < 0 or $lo > $self->Ni) 0 10 10
Condition33%1/3
$lo < 0 or $lo > $self->Ni
l!l&&r!l&&!r
0010
MC/DC0%0/2
$lo < 0 or $lo > $self->Ni
$lo < 0 $lo > $self->Ni
Truth table33%1/3
A or B
A= $lo < 0B= $lo > $self->Ni
ABresult
1X1
011
000
473 0 return 0;
474 }
475 }
476 else {
477 12 ▶ if ( $lo < $self->Lo1 or $lo > $self->Lo2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo < $self->Lo1 or $lo > $self->Lo2) 2 10 12
Condition100%3/3
$lo < $self->Lo1 or $lo > $self->Lo2
l!l&&r!l&&!r
1110
MC/DC100%2/2
$lo < $self->Lo1 or $lo > $self->Lo2
$lo < $self->Lo1 $lo > $self->Lo2
Truth table100%3/3
A or B
A= $lo < $self->Lo1B= $lo > $self->Lo2
ABresult
1X1
011
000
478 2 return 0;
479 }
480 }
481  
482 20 return 1;
483 }
484  
485 #--------------------------------------------------------------------------
486 # adjustLong( long )
487 #
488 # takes a long and returns a effective long adjusted to the shifted grid
489 #--------------------------------------------------------------------------
490 0 ▶sub adjustLong {
Subroutine adjustLong: not called CC=1 SCAR=6.9
Pod documented
491  
492 0 my $self = shift;
493 0 my $l1 = shift;
494 }
495  
496 #--------------------------------------------------------------------------
497 # toDecimal()
498 #
499 # helper method for parseGDS()
500 #--------------------------------------------------------------------------
501 104 ▶sub toDecimal {
Subroutine toDecimal: called CC=4 SCAR=13.9
Pod documented
502  
503 104 my $self = shift;
504 104 my $inArray = shift;
505  
506 # if the most segnificant bit is one it's negative...
507 104 my $isNeg = 0;
508 104 ▶ if ( $$inArray[0] >= 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inArray->[0] >= 128) 6 98 104
509 6 $isNeg = 1;
510 6 $$inArray[0] -= 128;
511 }
512  
513 # print "===== " . $$inArray[0] . " -- " . 2**((@$inArray -1) *8) . "\n";
514  
515 104 my ( $result, $m );
516 104 ▶ for ( my $i = @$inArray - 1, my $j = 0 ; $i >= 0 ; $i--, $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 260 104 364
517 260 $m = 2**( $j * 8 );
518 260 $result += $$inArray[$i] * $m;
519 }
520  
521 104 ▶ $result *= -1 if $isNeg;
Branch: 1 branch
BranchTrueFalseTotal
if $isNeg 6 98 104
522 104 return sprintf "%.2d", $result;
523 }
524  
525 #--------------------------------------------------------------------------
526 # dumpit()
527 #--------------------------------------------------------------------------
528 0 ▶sub dumpit {
Subroutine dumpit: not called CC=1 SCAR=1.2
Pod documented
529  
530 0 my $self = shift;
531  
532 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
533 0 print Dumper($self);
534 0 return;
535 }
536  
537 #--------------------------------------------------------------------------
538 # calInc( cord1, cord2, points)
539 #
540 # finds degrees between grid points given the start and end and
541 # number of points. If one cord is negative South or west long/lat assumed
542 #--------------------------------------------------------------------------#
543 0 ▶sub calInc {
Subroutine calInc: not called CC=4 SCAR=30.0
Pod documented
544  
545 0 my $self = shift;
546 0 my $c1 = shift;
547 0 my $c2 = shift;
548 0 my $pts = shift;
549  
550 0 my $size;
551 0 ▶ if ( $pts == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pts == 0) { } 0 0 0
elsif ($c1 < 0 or $c2 < 0) { } 0 0 0
552 0 $size = 0;
553 0 $self->error( "calInc: \$pts = 0" );
554 }
555 ▶ elsif ( $c1 < 0 or $c2 < 0 ) {
Condition0%0/3
$c1 < 0 or $c2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$c1 < 0 or $c2 < 0
$c1 < 0 $c2 < 0
Truth table0%0/3
A or B
A= $c1 < 0B= $c2 < 0
ABresult
1X1
011
000
556  
557 # print "$size = (abs($c1) + abs($c2) +1) / $pts;\n";
558 0 $size = ( abs($c1) + abs($c2) + 1 ) / $pts;
559 }
560 else {
561  
562 # print "$size = (abs($c1 - $c2)) / $pts\n";
563 0 $size = ( abs( $c1 - $c2 ) ) / $pts;
564 }
565 0 return sprintf "%.2f", $size;
566 }
567  
568 #--------------------------------------------------------------------------
569 # lalo2offset(lat, long)
570 #
571 # converts long/lat pairs in degrees to grib table offset
572 #--------------------------------------------------------------------------
573 1273 ▶sub lalo2offset {
Subroutine lalo2offset: called CC=3 SCAR=11.0
Pod documented
574  
575 1273 my $self = shift;
576 1273 my $lat = shift;
577 1273 my $long = shift;
578  
579
580 1273 my $thislong = 0;
581  
582 1273 $self->clearError;
583 1273 my $out;
584
585 1273 ▶ if ( $self->sn_scan_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sn_scan_flag) { } 1216 57 1273
586  
587 # shift long east until Lo1 = 0 and make sure any long
588 # > 360 degrees is moved back into the range of 0 - 360
589 1216 $thislong = $long + $self->{Lo_SHIFT};
590 1216 ▶ if ( $thislong > 360 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thislong > 360) 0 1216 1216
591 0 $thislong -= 360;
592 }
593  
594 1216 $out = ( ( ( $lat - $self->La1 ) / $self->LaInc ) * $self->Ni )
595 + ( ($thislong ) / $self->LoInc );
596 }
597 else {
598 57 $out = ( ( $self->La1 - $lat ) / $self->LaInc ) * $self->Ni
599 + ( ($long - $self->Lo1 ) / $self->LoInc );
600 }
601
602 1273 return sprintf "%.0f", sprintf "%.6f", $out;
603 }
604  
605 #--------------------------------------------------------------------------
606 # $plit = $w->extractLaLo( data_types, lat1, long1, lat2, long2, time )
607 #
608 #
609 # data_types is a scalar containing a single data type as a string or
610 # an array ref of data type strings.
611 #
612 # Extracts forecast data for a range of locations from (lat1, long1) to
613 # (lat2, long2) for the given data_type and time.
614 #
615 # This will be much faster than repeated calls to extract() because only one
616 # call to wgrib and just one file open are required.
617 #
618 # Returns a Geo::ReadGRIB::PlaceIterator object.
619 #
620 # Data is no longer stored in the current object by default as of version
621 # 1.4. To turn this behavior back on use the backflip() method;
622 #
623 #
624 # require: lat1 >= lat2 and long1 <= long2 - that is, lat1 is north or lat2
625 # and long1 is west of long2 (or is the same as)
626 #
627 #--------------------------------------------------------------------------
628 6 ▶sub extractLaLo {
Subroutine extractLaLo: called CC=25 SCAR=37.6
Pod documented
629  
630 6 my $self = shift;
631 6 my $type_s = shift;
632 6 my $lat1 = shift;
633 6 my $long1 = shift;
634 6 my $lat2 = shift;
635 6 my $long2 = shift;
636 6 my $time = shift;
637  
638 6 ▶ my $flipBack = $self->backflip() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->backflip ? : 1 5 6
639 6 ▶ my $DEBUG = $self->{DEBUG} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'DEBUG'} ? : 0 6 6
640 6 my $LoInc = $self->LoInc;
641  
642 6 my @types;
643 6 ▶ if ( ref $type_s eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $type_s eq 'ARRAY') { } 0 6 6
elsif ($type_s =~ /\w+/) { } 6 0 6
644 0 push @types, @$type_s;
645 }
646 elsif ( $type_s =~ /\w+/ ) {
647 6 push @types, $type_s;
648 }
649 else {
650 0 $self->error( "ERROR extractLaLo() \$types required" );
651 0 return;
652 }
653  
654 6 my @times = sort keys %{ $self->{catalog} };
655  
656 # First see that requested values are in range...
657 6 my ($lo1, $lo2, $la1, $la2) = ($self->Lo1, $self->Lo2, $self->La1, $self->La2, );
658 6 ▶ if ( not $lat1 >= $lat2 or not $long1 <= $long2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lat1 >= $lat2 and $long1 <= $long2) 0 6 6
Condition33%1/3
$lat1 >= $lat2 and $long1 <= $long2
!ll&&!rl&&r
006
MC/DC0%0/2
$lat1 >= $lat2 and $long1 <= $long2
$lat1 >= $lat2 $long1 <= $long2
Truth table33%1/3
A and B
A= $lat1 >= $lat2B= $long1 <= $long2
ABresult
0X0
100
111
659 0 $self->error( "ERROR extractLaLo() require: lat1 >= lat2 and long1 <= long2" );
660 0 return;
661 }
662  
663 6 ▶ if ( not defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 6 6
664 0 $self->error( "ERROR extractLaLo() \$time is required " );
665 0 return;
666 }
667  
668 6 ▶ if ( $time < $self->{TIME} or $time > $self->{LAST_TIME} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}) 0 6 6
Condition33%1/3
$time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}
$time < $self->{"TIME"} $time > $self->{"LAST_TIME"}
Truth table33%1/3
A or B
A= $time < $self->{"TIME"}B= $time > $self->{"LAST_TIME"}
ABresult
1X1
011
000
669 0 $self->error( "ERROR extractLaLo() \$time \"$time\" out of range "
670 . scalar gmtime( $times[0] ) . " ($times[0]) to "
671 . scalar gmtime( $times[-1] ) . " ($times[-1])" );
672 0 return;
673 }
674  
675 6 ▶ if ( not $self->validLa($lat1) or not $self->validLa($lat2) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validLa($lat1) and $self->validLa($lat2)) 0 6 6
Condition33%1/3
$self->validLa($lat1) and $self->validLa($lat2)
!ll&&!rl&&r
006
MC/DC0%0/2
$self->validLa($lat1) and $self->validLa($lat2)
$self->validLa($lat1) $self->validLa($lat2)
Truth table33%1/3
A and B
A= $self->validLa($lat1)B= $self->validLa($lat2)
ABresult
0X0
100
111
676 0 $self->error( "extractLaLo(): LAT >$lat1 or $lat2< out of range $la1 to $la2" );
677 0 return;
678 }
679  
680 6 ▶ if ( not $self->validLo( $long1 ) or not $self->validLo( $long2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validLo($long1) and $self->validLo($long2)) 0 6 6
Condition33%1/3
$self->validLo($long1) and $self->validLo($long2)
!ll&&!rl&&r
006
MC/DC0%0/2
$self->validLo($long1) and $self->validLo($long2)
$self->validLo($long1) $self->validLo($long2)
Truth table33%1/3
A and B
A= $self->validLo($long1)B= $self->validLo($long2)
ABresult
0X0
100
111
681 0 $self->error( "extractLaLo(): LONG: >$long1 or $long2 < out of range $lo1 to $lo2" );
682 0 return;
683 }
684  
685 6 my $plit = Geo::ReadGRIB::PlaceIterator->new();
686  
687 # if time is given, use nearest time in catalog...
688 6 ▶ if ( defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 6 0 6
689 6 $self->findNearestTime( $time );
690 }
691  
692 6 my ( $dtaLength, $fileDump, $offset, $dump, $record, $lo, $la );
693 6 my $tm = $self->{THIS_TIME};
694  
695  
696 6 for my $type ( @types ) {
697 6 $record = $self->{catalog}->{$tm}->{$type};
698  
699 6 ▶ if ( not defined $record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 0 6 6
700 0 $self->error( "extractLaLo(): Not a valid type: $type" );
701 0 return 1;
702 }
703  
704 6 my $tmp = $self->tempfile();
705 6 my $cmd = "\"$LIB_DIR\"/wgrib.exe \"$self->{fileName}\" -d $record -nh -o $tmp";
706 6 my $res = qx($cmd);
707 6 ▶ my $F = IO::File->new( $tmp ) or croak "Can't open temp file";
Branch: 1 branch
BranchTrueFalseTotal
my $F = "IO::File"->new($tmp) or croak("Can't open temp file") 6 0 6
708  
709 # Make sure first offset is smallest for any scanning order
710 6 my ( $offsetFst, $offsetLst ) = sort {$a <=> $b} (
711 $self->lalo2offset($lat1, $long1), $self->lalo2offset($lat2, $long2) );
712 6 $dtaLength = ($offsetLst - $offsetFst +1) * 4;
713 6 seek $F, $offsetFst * 4, 0;
714 6 read $F, $fileDump, $dtaLength;
715  
716 6 my ($x, $y);
717 6 $dump = "";
718 6 ▶ for ( $la = $lat1, $x = 0 ; $la >= $lat2 ; $la -= $self->LaInc, $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($la >= $lat2) 651 6 657
719 651 my $num_lo = sprintf "%d", (($long2 - $long1) / $LoInc) +1;
720 651 $offset = $self->lalo2offset( $la, $long1 ) - $offsetFst;
721 651 my @luck = unpack "f*", substr $fileDump, $offset * 4, 4 * $num_lo;
722 651 ▶ for ( $lo = $long1, $y = 0 ; $lo <= $long2 ; $lo += $LoInc, $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo <= $long2) 1083 651 1734
723 1083 my $dump = shift @luck;
724 1083 ▶ $dump = defined $dump ? sprintf "%.2f", $dump : 0.00;
Branch: 1 branch
BranchTrueFalseTotal
defined $dump ? : 1082 1 1083
725 1083 ▶ $dump = 0 if $dump eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dump eq "" 0 1083 1083
726 1083 ▶ $dump = "UNDEF" if $dump > 999900000000000000000;
Branch: 1 branch
BranchTrueFalseTotal
if $dump > "9.999e+20" 0 1083 1083
727 1083 ▶ print gmtime($tm) . ": $self->{v_catalog}->{$type} $dump\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 1083 1083
728 1083 ▶ $self->{data}->{$tm}->{$la}->{$lo}->{$type} = $dump if $flipBack;
Branch: 1 branch
BranchTrueFalseTotal
if $flipBack 231 852 1083
729 # TODO look into the negative lats with CMS data
730 # $self->{matrix}->{$tm}->{$type}->[$x]->[$y] = $dump if $la >= 0 and $lo >= 0;
731 1083 $plit->addData( $tm, $la, $lo, $type, $dump );
732 }
733 }
734 6 close $F;
735 6 unlink $tmp;
736 6 undef $fileDump;
737 }
738  
739 6 return $plit;
740 }
741  
742 #--------------------------------------------------------------------------
743 # backflip( [flag] ) - Getter/setter for the backflip() state.
744 #
745 # When true, certain older behaviors return. In version 1.4 this is only the
746 # storage in the current object of data extracted by extractLaLo().
747 #
748 # default false
749 #--------------------------------------------------------------------------
750 20 ▶sub backflip {
Subroutine backflip: called CC=2 SCAR=6.9
Pod documented
751 20 my ( $self, $flag ) = @_;
752 20 ▶ $self->{backflip} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 14 6 20
753 20 return $self->{backflip};
754 }
755  
756 #---------------------------------------------------------------------------
757 # getMatrix({ time => time, type => type })
758 #
759 # return the array of the extracted region for type and time
760 # POSSIBLE FUTURE FUNCTION
761 #---------------------------------------------------------------------------
762 #sub getMatrix {
763 # my ( $self, $r ) = @_;
764 # return $self->{matrix}->{ $r->{time} }->{ $r->{type} };
765 #}
766  
767 #--------------------------------------------------------------------------
768 # $plit = extract(data_type, lat, long, [time])
769 #
770 # Extracts forecast data for given type and location. Ectracts data for all
771 # times in file unless a specific time is given in epoch seconds.
772 #
773 # Returns a Geo::ReadGRIB::PlaceIterator and extracted data is also
774 # retained in the ReadGRIB object.
775 #
776 # type will be one of the data types in the data
777 #--------------------------------------------------------------------------
778 15 ▶sub extract {
Subroutine extract: called CC=18 SCAR=32.3
Pod documented
779  
780 15 my $self = shift;
781 15 my $type = shift;
782 15 my $lat = shift;
783 15 my $long = shift;
784 15 my $time = shift;
785  
786 # First see that requested values are in range...
787 15 my ($lo1, $lo2, $la1, $la2) = ($self->Lo1, $self->Lo2, $self->La1, $self->La2, );
788 15 ▶ if ( not $self->validLa($lat) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validLa($lat)) 5 10 15
789 5 $self->error( "extract(): LAT >$lat< out of range $la1 to $la2" );
790 5 return;
791 }
792  
793 10 ▶ if ( not $self->validLo($long) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validLo($long)) 2 8 10
794 2 $self->error( "extract(): LONG: >$long< out of range $lo1 to $lo2" );
795 2 return;
796 }
797  
798 8 my $offset = $self->lalo2offset( $lat, $long );
799  
800 8 ▶ if ( $offset < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 8 8
801 0 $self->error( "extract(): offset less than zero" );
802 0 return 1;
803 }
804  
805 8 ▶ $time = 0 unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 8 8
806  
807 8 ▶ if ( $time != 0 and $time < $self->{TIME}
Branch: 1 branch
BranchTrueFalseTotal
if ($time != 0 and $time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}) 0 8 8
Condition33%2/6
$time != 0 and $time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}
l!l&&r!l&&!r
008
$time != 0 and $time < $self->{"TIME"}
!ll&&!rl&&r
080
MC/DC0%0/3
$time != 0 and $time < $self->{"TIME"} or $time > $self->{"LAST_TIME"}
$time != 0 $time < $self->{"TIME"} $time > $self->{"LAST_TIME"}
Truth table20%1/5
A and B or C
A= $time != 0B= $time < $self->{"TIME"}C= $time > $self->{"LAST_TIME"}
ABCresult
11X1
0X11
1011
0X00
1000
808 or $time > $self->{LAST_TIME} ) {
809  
810 0 my @times = sort keys %{ $self->{catalog} };
811 0 $self->error( "ERROR extract() \$time \"$time\" out of range "
812 . scalar gmtime( $times[0] ) . " ($times[0]) to "
813 . scalar gmtime( $times[-1] ) . " ($times[-1])" );
814 0 return 1;
815 }
816  
817 # If a time is given find nearest in catalog
818 8 ▶ if ( defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 8 0 8
819 8 $self->findNearestTime( $time );
820 }
821  
822 8 my ( $record, $cmd, $res, $dump );
823  
824 8 ▶ unless ( defined $self->{v_catalog}->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"v_catalog"}{$type}) 0 8 8
825 0 $self->error( "extract() Type not found: $type" );
826 0 return 1;
827 }
828  
829 # Give avaiable data for type and offset.
830 # All times returned unless $time is given.
831 #
832 # If record is alredy in $self->{data} use that
833 # else go to disk...
834  
835 8 my $plit = Geo::ReadGRIB::PlaceIterator->new();
836  
837 8 $dump = "";
838 8 foreach my $tm ( sort keys %{ $self->{catalog} } ) {
839 8 ▶ if ( $time != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time != 0) 8 0 8
840 8 $tm = $self->{THIS_TIME};
841 }
842
843 8 $record = $self->{catalog}->{$tm}->{$type};
844  
845 8 ▶ if ( not defined $record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 0 8 8
846 0 $self->error( "extract(): Not a valid type: $type" );
847 0 return 1;
848 }
849  
850 8 my $tmp = $self->tempfile();
851 8 $cmd = "\"$LIB_DIR\"/wgrib.exe \"$self->{fileName}\" -d $record -nh -o $tmp";
852 8 $res = qx($cmd);
853 print "$cmd - OFFSET: $offset " . $offset * 4 . " bytes\n"
854 8 ▶ if $self->{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 8 8
855 8 ▶ my $F = IO::File->new( "$tmp" ) or croak "Can't open temp file";
Branch: 1 branch
BranchTrueFalseTotal
my $F = "IO::File"->new("$tmp") or croak("Can't open temp file") 8 0 8
856 8 seek $F, $offset * 4, 0;
857 8 read $F, $dump, 4;
858 8 $dump = unpack "f", $dump;
859 8 $dump = sprintf "%.2f", $dump;
860 8 ▶ $dump = "UNDEF" if $dump > 999900000000000000000;
Branch: 1 branch
BranchTrueFalseTotal
if $dump > "9.999e+20" 3 5 8
861 print gmtime($tm) . ": $self->{v_catalog}->{$type} $dump\n"
862 8 ▶ if $self->{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 8 8
863 8 $self->{data}->{$tm}->{$lat}->{$long}->{$type} = $dump;
864 8 $plit->addData( $tm, $lat, $long, $type, $dump );
865 8 close $F;
866 8 unlink $tmp;
867 8 ▶ last if $time != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $time != 0 8 0 8
868 }
869 8 return $plit;
870 }
871  
872 #--------------------------------------------------------------------------
873 # findNearestTime( time )
874 #
875 # Find nearest time in catalog...
876 #--------------------------------------------------------------------------
877 14 ▶sub findNearestTime {
Subroutine findNearestTime: called CC=6 SCAR=18.2
Pod documented
878  
879 14 my $self = shift;
880 14 my $time = shift;
881  
882 14 my @times = sort keys %{ $self->{catalog} };
883  
884 14 ▶ if ( 1 == @times ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @times) { } 7 7 14
885 7 $self->{THIS_TIME} = $times[0];
886 }
887 else {
888 7 ▶ for ( my $i = 0, my $j = 1 ; $j <= @times ; $i++, $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= @times) 283 0 283
889 283 ▶ if ( $time >= $times[$i] and $time <= $times[$j] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time >= $times[$i] and $time <= $times[$j]) 7 276 283
Condition66%2/3
$time >= $times[$i] and $time <= $times[$j]
!ll&&!rl&&r
02767
MC/DC50%1/2
$time >= $times[$i] and $time <= $times[$j]
$time >= $times[$i] $time <= $times[$j]
Truth table66%2/3
A and B
A= $time >= $times[$i]B= $time <= $times[$j]
ABresult
0X0
100
111
890 7 ▶ if ( ( $time - $times[$i] ) <= ( $times[$j] - $time ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time - $times[$i] <= $times[$j] - $time) { } 0 7 7
891 0 $self->{THIS_TIME} = $times[$i];
892 }
893 else {
894 7 $self->{THIS_TIME} = $times[$j];
895 }
896 7 last;
897 }
898 }
899 }
900 }
901  
902 #--------------------------------------------------------------------------
903 # getDataHash()
904 #
905 # Returns a hash ref with all the data items in the object.
906 # This will be all the data extracted from the GRIB file for
907 # in the life of the object.
908 #
909 # The structure is
910 #
911 # $t->{time}->{lat}->{long}->{type}
912 #--------------------------------------------------------------------------
913 4 ▶sub getDataHash {
Subroutine getDataHash: called CC=1 SCAR=0.0
Pod documented
914 4 my $self = shift;
915 4 return $self->{data};
916 }
917  
918  
919 #--------------------------------------------------------------------------
920 # clearData( )
921 #--------------------------------------------------------------------------
922 0 ▶sub clearData {
Subroutine clearData: not called CC=1 SCAR=6.9
Pod documented
923 0 my $self = shift;
924 0 undef $self->{data};
925 0 return ;
926 }
927  
928 #--------------------------------------------------------------------------
929 # getError()
930 #
931 # returns error string from $self->error
932 #--------------------------------------------------------------------------
933 28 ▶sub getError {
Subroutine getError: called CC=2 SCAR=6.9
Pod documented
934 28 my $self = shift;
935 28 ▶ return defined $self->error ? $self->error : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->error ? : 7 21 28
936 }
937  
938 #--------------------------------------------------------------------------
939 # m2ft(meters)
940 #
941 # convert meters to feet
942 #--------------------------------------------------------------------------
943 0 ▶sub m2ft {
Subroutine m2ft: not called CC=1 SCAR=6.9
Pod documented
944  
945 0 my $self = shift;
946 0 my $m = shift;
947 0 return $m * 3.28;
948 }
949  
950 #--------------------------------------------------------------------------
951 # tempfile()
952 #
953 # return a temp file name
954 #--------------------------------------------------------------------------
955 49 ▶sub tempfile {
Subroutine tempfile: called CC=1 SCAR=0.0
Pod documented
956  
957 49 my $self = shift;
958  
959 13 ▶ use File::Temp qw(:mktemp);
Subroutine BEGIN: called CC=1 SCAR=0.0
960  
961 49 my ( $fh, $fn ) = mkstemp("wgrib.tmp.XXXXXXXXX");
962 49 return $fn;
963 }
964  
965 #--------------------------------------------------------------------------
966 # $p = getParam(parm_name)
967 #
968 # getParam(param_name) returns a scalar with the value of param_name
969 # getParam("show") returns a scalar listing published parameter names.
970 #--------------------------------------------------------------------------
971 2 ▶sub getParam {
Subroutine getParam: called CC=4 SCAR=15.0
Pod documented
972  
973 2 my $self = shift;
974 2 my $arg = shift;
975  
976 2 my @published = qw/TIME LAST_TIME La1 La2 LaInc Lo1 Lo2 LoInc fileName/;
977  
978 2 my $param;
979 2 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 2 0 2
980 2 ▶ if ( $arg =~ /show/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /show/i) { } 1 1 2
elsif (grep /$arg/, @published) { } 1 0 1
981 1 $param = "@published";
982 }
983 elsif ( grep /$arg/, @published ) {
984 1 $param = $self->{$arg};
985 }
986 else {
987 0 $self->error( "getParam(): $arg - Undefined or unpublished parameter" );
988 0 return 1;
989 }
990 }
991 else {
992 0 $self->error( "getParam(): Usage: getParam(param_name)" );
993 0 return 1;
994 }
995  
996 2 return $param;
997 }
998  
999 #--------------------------------------------------------------------------
1000 # show()
1001 #
1002 # Returns a scalar containing a string with some selected meta data
1003 # describing the GRIB file.
1004 #--------------------------------------------------------------------------
1005 3 ▶sub show {
Subroutine show: called CC=5 SCAR=18.6
Pod documented
1006  
1007 3 my $self = shift;
1008 3 my $arg = shift;
1009  
1010 3 my $param;
1011  
1012 3 my @published = qw/LAST_TIME La1 La2 LaInc Lo1 Lo2 LoInc TIME fileName/;
1013  
1014 3 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 0 3 3
1015 0 ▶ if ( $arg =~ /show/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /show/i) { } 0 0 0
elsif (grep /$arg/, @published) { } 0 0 0
1016 0 $param = "@published";
1017 }
1018 elsif ( grep /$arg/, @published ) {
1019 0 $param = $self->{$arg};
1020 }
1021 else {
1022 0 $self->error( "show(): $arg - Undefined or unpublished parameter" );
1023 0 return 1;
1024 }
1025 }
1026 else {
1027 3 my $types;
1028 3 foreach ( sort keys %{ $self->{v_catalog} } ) {
1029 3 $types .= sprintf "%8s: %s\n", $_, $self->{v_catalog}->{$_};
1030 }
1031  
1032 3 my @times = sort keys %{ $self->{catalog} };
1033 3 my $t = scalar gmtime( $times[0] ) . " ($times[0]) to ";
1034 3 $t .= scalar gmtime( $times[-1] ) . " ($times[-1])";
1035  
1036 3 $param = <<" PARAM";
1037
1038 Locations:
1039
1040 lat: $self->{La1} to $self->{La2}
1041 long: $self->{Lo1} to $self->{Lo2}
1042
1043 Times:
1044
1045 $t
1046
1047 Types:
1048 \n$types
1049 PARAM
1050 }
1051 3 return $param;
1052 }
1053  
1054 13 1;
1055  
1056 __DATA__
↑ Summary ReadGRIB/PlaceIterator.pm »