bufrextract.pl

filter: Statementstmt 88.0% 66 / 75 Branchbran 66.0% 33 / 50 Conditioncond 55.8% 24 / 43 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 70.2% 137 / 195 CC 24 SCAR 33.4
CC
24
Cov
73%
extract
37.9
filter_on_ahl
3.0
BEGIN
1.0
CRAP 28.3
« blib/script/bufrextract.pl ↑ Summary bufrread.pl »
1 #!/usr/bin/perl
2  
3 # (C) Copyright 2010-2025 MET Norway
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19  
20 # pod included at end of file
21  
22 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use Geo::BUFR;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Parse command line options
29 7 my %option = ();
30 7 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%option, "ahl=s", "gts", "help", "only_ahl", "outfile=s", "verbose=i", "without_ahl") or pod2usage("-verbose", 0) 0 7 7
31 \%option,
32 'ahl=s', # Extract BUFR messages with AHL matching <ahl_regexp> only
33 'gts', # Include full gts message envelope if present
34 'help', # Print help information and exit
35 'only_ahl', # Extract AHLs only
36 'outfile=s', # Print to file instead of STDOUT
37 'verbose=i', # Set verbose level to n, 0<=n<=6 (default 0)
38 'without_ahl', # Print the BUFR messages only, skipping AHLs
39 ) or pod2usage(-verbose => 0);
40  
41 # User asked for help
42 7 ▶pod2usage(-verbose => 1) if $option{help};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"help"} 0 7 7
43  
44 # only_ahl and without_ahl are mutually exclusive
45 pod2usage( -message => "Options only_ahl, without_ahl and gts are mutually exclusive",
46 -exitval => 2,
47 -verbose => 0)
48 if ( ($option{only_ahl} && ($option{without_ahl} || $option{gts}))
49 || ($option{without_ahl} && ($option{only_ahl} || $option{gts}))
50 7 ▶ || ($option{gts} && ($option{only_ahl} || $option{without_ahl})) );
Branch: 1 branch
BranchTrueFalseTotal
if $option{"only_ahl"} and $option{"without_ahl"} || $option{"gts"} or $option{"without_ahl"} and $option{"only_ahl"} || $option{"gts"} or $option{"gts"} and $option{"only_ahl"} || $option{"without_ahl"} 0 7 7
Condition45%11/24
$option{"only_ahl"} and $option{"without_ahl"} || $option{"gts"} or $option{"without_ahl"} and $option{"only_ahl"} || $option{"gts"} or $option{"gts"} and $option{"only_ahl"} || $option{"without_ahl"}
l!l&&r!l&&!r
007
$option{"only_ahl"} and $option{"without_ahl"} || $option{"gts"} or $option{"without_ahl"} and $option{"only_ahl"} || $option{"gts"}
l!l&&r!l&&!r
007
$option{"only_ahl"} and $option{"without_ahl"} || $option{"gts"}
!ll&&!rl&&r
520
$option{"without_ahl"} or $option{"gts"}
l!l&&r!l&&!r
002
$option{"without_ahl"} and $option{"only_ahl"} || $option{"gts"}
!ll&&!rl&&r
610
$option{"only_ahl"} or $option{"gts"}
l!l&&r!l&&!r
001
$option{"gts"} and $option{"only_ahl"} || $option{"without_ahl"}
!ll&&!rl&&r
520
$option{"only_ahl"} or $option{"without_ahl"}
l!l&&r!l&&!r
002
MC/DC0%0/9
$option{"only_ahl"} and $option{"without_ahl"} || $option{"gts"} or $option{"without_ahl"} and $option{"only_ahl"} || $option{"gts"} or $option{"gts"} and $option{"only_ahl"} || $option{"without_ahl"}
$option{"only_ahl"} $option{"without_ahl"} $option{"gts"} $option{"without_ahl"} $option{"only_ahl"} $option{"gts"} $option{"gts"} $option{"only_ahl"} $option{"without_ahl"}
Truth table0%0/22
A and B or C or D and E or F or G and H or I
A= $option{"only_ahl"}B= $option{"without_ahl"}C= $option{"gts"}D= $option{"without_ahl"}E= $option{"only_ahl"}F= $option{"gts"}G= $option{"gts"}H= $option{"only_ahl"}I= $option{"without_ahl"}
ABCDEFGHIresult
11XXXXXXX1
101XXXXXX1
0XX11XXXX1
0XX101XXX1
10011XXXX1
100101XXX1
0XX0XX11X1
0XX0XX1011
0XX10011X1
0XX1001011
1000XX11X1
1000XX1011
10010011X1
1001001011
0XX0XX0XX0
0XX0XX1000
0XX1000XX0
0XX1001000
1000XX0XX0
1000XX1000
1001000XX0
1001001000
51  
52 # Make sure there is at least one input file
53 7 ▶pod2usage(-verbose => 0) unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 7 7
54  
55 # Set verbosity level
56 7 ▶Geo::BUFR->set_verbose($option{verbose}) if $option{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"verbose"} 0 7 7
57  
58 7 my $ahl_regexp;
59 7 ▶if ($option{ahl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"ahl"}) 3 4 7
60 3 eval { $ahl_regexp = qr/$option{ahl}/ };
61 3 ▶ die "Argument to --ahl is not a valid Perl regular expression: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
62 }
63  
64 # Where to direct output (including verbose output, but not output to STDERR)
65 7 my $OUT;
66 7 ▶if ($option{outfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'outfile'}) { } 5 2 7
67 open($OUT, '>', $option{outfile})
68 5 ▶ or die "Cannot open $option{outfile} for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $OUT, ">", $option{"outfile"} or die qq[Cannot open $option{"outfile"} for writing: $!] 0 5 5
69 } else {
70 2 $OUT = *STDOUT;
71 }
72 7 binmode($OUT);
73  
74 # No need to decode section 4 here
75 7 Geo::BUFR->set_nodata(1);
76  
77 # Loop for processing of BUFR input files
78 7 foreach my $inputfname ( @ARGV ) {
79 7 my $bufr = Geo::BUFR->new();
80 7 ▶ $bufr->set_filter_cb(\&filter_on_ahl,$ahl_regexp) if $option{ahl};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"ahl"} 3 4 7
81  
82 # Open BUFR file
83 7 $bufr->fopen($inputfname);
84  
85 # Process input file
86 7 extract($bufr);
87 7 $bufr->fclose();
88 }
89  
90  
91 # Extract BUFR messages and/or AHLs from BUFR file
92 sub extract {
93 7 ▶ my $bufr = shift; # BUFR object
Subroutine extract: called CC=22 SCAR=36.4
94  
95 7 my ($current_message_number, $current_ahl);
96 READLOOP:
97 7 ▶ while (not $bufr->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $bufr->eof) 63 0 63
98  
99 # Read next observation. If an error is encountered during
100 # decoding, skip this observation while printing the error
101 # message to STDERR, also displaying ahl of bulletin if found
102 # (but skip error message if the message should be skipped on
103 # --ahl anyway).
104 63 eval {
105 63 $bufr->next_observation();
106 };
107 63 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 63 63
108 0 ▶ $current_ahl = $bufr->get_current_ahl() || '';
Condition0%0/2
$bufr->get_current_ahl || ''
l!l
00
MC/DC0%0/1
$bufr->get_current_ahl || ''
$bufr->get_current_ahl
Truth table0%0/2
A
A= $bufr->get_current_ahl
Aresult
11
00
109 0 ▶ next READLOOP if $option{ahl} && $current_ahl !~ $ahl_regexp;
Branch: 1 branch
BranchTrueFalseTotal
if $option{"ahl"} and not $current_ahl =~ /$ahl_regexp/ 0 0 0
Condition0%0/3
$option{"ahl"} and not $current_ahl =~ /$ahl_regexp/
!ll&&!rl&&r
000
MC/DC0%0/2
$option{"ahl"} and not $current_ahl =~ /$ahl_regexp/
$option{"ahl"} not $current_ahl =~ /$ahl_regexp/
Truth table0%0/3
A and not(B)
A= $option{"ahl"}B= not $current_ahl =~ /$ahl_regexp/
ABresult
0X0
100
111
110  
111 0 warn $@;
112 # Try to extract message number and ahl of the bulletin
113 # where the error occurred
114 0 $current_message_number = $bufr->get_current_message_number();
115 0 ▶ if (defined $current_message_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_message_number) 0 0 0
116 0 my $error_msg = "In message $current_message_number";
117 0 ▶ $error_msg .= " contained in bulletin with ahl $current_ahl\n"
Branch: 1 branch
BranchTrueFalseTotal
if $current_ahl 0 0 0
118 if $current_ahl;
119 0 ▶ warn $error_msg if $error_msg;
Branch: 1 branch
BranchTrueFalseTotal
if $error_msg 0 0 0
120 }
121 0 next READLOOP;
122 }
123  
124 63 ▶ next READLOOP if $option{ahl} && $bufr->is_filtered();
Branch: 1 branch
BranchTrueFalseTotal
if $option{"ahl"} and $bufr->is_filtered 12 51 63
Condition100%3/3
$option{"ahl"} and $bufr->is_filtered
!ll&&!rl&&r
361512
MC/DC100%2/2
$option{"ahl"} and $bufr->is_filtered
$option{"ahl"} $bufr->is_filtered
Truth table100%3/3
A and B
A= $option{"ahl"}B= $bufr->is_filtered
ABresult
0X0
100
111
125 # Skip messages where stated length of BUFR message is sure to
126 # be erroneous, unless we want ahls only (or should we skip
127 # message in this case also? Hard choice...)
128 51 ▶ next READLOOP if !$option{only_ahl} && $bufr->bad_bufrlength();
Branch: 1 branch
BranchTrueFalseTotal
if not $option{"only_ahl"} and $bufr->bad_bufrlength 11 40 51
Condition100%3/3
not $option{"only_ahl"} and $bufr->bad_bufrlength
!ll&&!rl&&r
142611
MC/DC100%2/2
not $option{"only_ahl"} and $bufr->bad_bufrlength
not $option{"only_ahl"} $bufr->bad_bufrlength
Truth table100%3/3
not(A) and B
A= not $option{"only_ahl"}B= $bufr->bad_bufrlength
ABresult
0X0
100
111
129  
130 40 my $current_subset_number = $bufr->get_current_subset_number();
131 # If next_observation() did find a BUFR message, subset number
132 # should have been set to at least 1 (even in a 0 subset message)
133 40 ▶ last READLOOP if $current_subset_number == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $current_subset_number == 0 7 33 40
134  
135 33 $current_message_number = $bufr->get_current_message_number();
136 33 ▶ $current_ahl = $bufr->get_current_ahl() || '';
Condition100%2/2
$bufr->get_current_ahl || ''
l!l
285
MC/DC100%1/1
$bufr->get_current_ahl || ''
$bufr->get_current_ahl
Truth table100%2/2
A
A= $bufr->get_current_ahl
Aresult
11
00
137 33 my $gts_eom = '';
138  
139 33 ▶ if ($current_ahl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_ahl) 28 5 33
140 28 ▶ if ($option{only_ahl}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($option{'only_ahl'}) { } 10 18 28
elsif (not $option{'without_ahl'}) { } 14 4 18
141 10 print $OUT $current_ahl, "\n";
142 } elsif (!$option{without_ahl}) {
143 14 ▶ if ($option{gts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"gts"}) 7 7 14
144 7 ▶ my $current_gts_starting_line = $bufr->get_current_gts_starting_line() || '';
Condition50%1/2
$bufr->get_current_gts_starting_line || ''
l!l
70
MC/DC0%0/1
$bufr->get_current_gts_starting_line || ''
$bufr->get_current_gts_starting_line
Truth table50%1/2
A
A= $bufr->get_current_gts_starting_line
Aresult
11
00
145 7 print $OUT $current_gts_starting_line;
146 7 ▶ $gts_eom = $bufr->get_current_gts_eom() || '';
Condition100%2/2
$bufr->get_current_gts_eom || ''
l!l
52
MC/DC100%1/1
$bufr->get_current_gts_eom || ''
$bufr->get_current_gts_eom
Truth table100%2/2
A
A= $bufr->get_current_gts_eom
Aresult
11
00
147 }
148 # Use \r\r\n after AHL, since this is the standard
149 # sequence used in GTS bulletins
150 14 print $OUT $current_ahl . "\r\r\n";
151 }
152 }
153 33 ▶ next READLOOP if $option{only_ahl};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"only_ahl"} 12 21 33
154  
155 21 my $msg = $bufr->get_bufr_message();
156 21 print $OUT $msg, $gts_eom;
157 }
158 }
159  
160 # Filter routines
161  
162 sub filter_on_ahl {
163 24 ▶ my $bufr = shift;
Subroutine filter_on_ahl: called CC=3 SCAR=11.0
164 24 my $ahl_regexp = shift;
165 24 ▶ my $ahl = $bufr->get_current_ahl() || '';
Condition100%2/2
$bufr->get_current_ahl || ''
l!l
186
MC/DC100%1/1
$bufr->get_current_ahl || ''
$bufr->get_current_ahl
Truth table100%2/2
A
A= $bufr->get_current_ahl
Aresult
11
00
166 24 ▶ return $ahl =~ $ahl_regexp ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ahl =~ /$ahl_regexp/ ? : 12 12 24
167 }
168  
169  
170 =pod
171  
172 =encoding utf8
173  
174 =head1 SYNOPSIS
175  
176 bufrextract.pl <bufr file(s)>
177 [--ahl <ahl_regexp>]
178 [--only_ahl] | [--without_ahl] | [--gts]
179 [--outfile <filename>]
180 [--help]
181 [--verbose n]
182  
183 =head1 DESCRIPTION
184  
185 Extract all BUFR messages and/or corresponding AHLs from BUFR file(s),
186 possibly filtering on AHL.
187  
188 The AHL (Abbreviated Header Line) is recognized as the TTAAii CCCC
189 YYGGgg [BBB] immediately preceding the BUFR message.
190  
191 Execute without arguments for Usage, with option C<--help> for some
192 additional info. See also L<https://wiki.met.no/bufr.pm/start> for
193 examples of use.
194  
195  
196 =head1 OPTIONS
197  
198 --ahl <ahl_regexp> Extract BUFR messages and/or AHLs with AHL
199 matching <ahl_regexp> only
200 --gts Include full gts message envelope if present
201 --only_ahl Extract AHLs only
202 --without_ahl Extract BUFR messages only
203 --outfile <filename>
204 Will print to <filename> instead of STDOUT
205 --help Display Usage and explain the options used. For even
206 more info you might prefer to consult perldoc bufrextract.pl
207 --verbose n Set verbose level to n, 0<=n<=6 (default 0)
208  
209 Options may be abbreviated, e.g. C<--h> or C<-h> for C<--help>.
210  
211 For option C<--ahl> the <ahl_regexp> should be a Perl regular
212 expression. E.g. C<--ahl 'ISS... ENMI'> will decode only BUFR SHIP
213 (ISS) from CCCC=ENMI.
214  
215 Use option C<--gts> if you want the full GTS message envelope (if
216 present) to be included in output. There are 2 main variations on this
217 envelope (SOH/ETX and ZCZC notation), for details see the Manual on
218 the GTS: Attachment II-4. Format of Meteorological Messages.
219  
220 No bufrtables are needed for running bufrextract.pl, since section 4
221 in BUFR message will not be decoded (which also speeds up execution
222 quite a bit).
223  
224 =head1 HINTS
225  
226 With a little knowledge of Perl you could easily extend bufrextract.pl
227 to extract BUFR messages based on whatever information is available in
228 section 0-3, by making your own copy of bufrextract.pl and then
229 employing one of the many C<get_> subroutines in BUFR.pm. For example,
230 to extract only BUFR messages with data category 1, add the following
231 line just before calling C<is_filtered()> in code:
232  
233 next if $bufr->get_data_category() != 1;
234  
235 Or to extract BUFR messages with TM315009 only:
236  
237 next if $bufr->get_descriptors_unexpanded() ne '315009';
238  
239 =head1 CAVEAT
240  
241 Sometimes GTS bulletins are erroneously issued with extra characters
242 between the GTS AHL and the start of BUFR message (besides the
243 standard character sequence CRCRLF), likely leading bufrextract.pl to
244 miss the AHL.
245  
246 =head1 AUTHOR
247  
248 Pål Sannes E<lt>pal.sannes@met.noE<gt>
249  
250 =head1 COPYRIGHT
251  
252 Copyright (C) 2010-2025 MET Norway
253  
254 =cut
« blib/script/bufrextract.pl ↑ Summary bufrread.pl »