File Coverage

bufr_reencode.pl
Criterion Covered Total %
statement 53 57 92.9
branch 14 28 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 76 94 80.8


line stmt bran cond sub pod time code
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 18     18   83363 use strict;
  18         30  
  18         684  
23 18     18   80 use warnings;
  18         24  
  18         1003  
24 18     18   110 use Carp;
  18         30  
  18         1387  
25 18     18   12999 use Getopt::Long;
  18         279756  
  18         127  
26 18     18   12611 use Pod::Usage qw(pod2usage);
  18         1209847  
  18         1530  
27 18     18   25150 use Geo::BUFR;
  18         91  
  18         1023  
28              
29             # This is actually default in BUFR.pm, but provided here to make it
30             # easier for users to change to 'ECCODES' if preferred
31 18     18   127 use constant DEFAULT_TABLE_FORMAT => 'BUFRDC';
  18         24  
  18         1533  
32              
33             # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set
34 18     18   91 use constant DEFAULT_TABLE_PATH_BUFRDC => '/usr/local/lib/bufrtables';
  18         25  
  18         908  
35 18     18   152 use constant DEFAULT_TABLE_PATH_ECCODES => '/usr/local/share/eccodes/definitions/bufr/tables';
  18         27  
  18         35268  
36              
37             # Parse command line options
38 18         2713217 my %option = ();
39              
40 18 50       156 GetOptions(
41             \%option,
42             'help',
43             'outfile=s',
44             'strict_checking=i',
45             'tableformat=s',
46             'tablepath=s',
47             'verbose=i',
48             'width=i',
49             ) or pod2usage(-verbose => 0);
50              
51             # User asked for help
52 18 50       23073 pod2usage(-verbose => 1) if $option{help};
53              
54             # Make sure there is an input file
55 18 50       76 pod2usage(-verbose => 0) unless @ARGV == 1;
56 18         49 my $infile = shift;
57              
58 18 100       85 my $width = $option{width} ? $option{width} : 15;
59              
60             # Default is croak if (recoverable) error found in encoded BUFR format
61             my $strict_checking = defined $option{strict_checking}
62 18 50       65 ? $option{strict_checking} : 2;
63 18         237 Geo::BUFR->set_strict_checking($strict_checking);
64              
65             # Set verbosity level
66 18 50       75 Geo::BUFR->set_verbose($option{verbose}) if $option{verbose};
67              
68             # Set BUFR table format
69 18 100       72 my $tableformat = (defined $option{tableformat}) ? uc $option{tableformat} : DEFAULT_TABLE_FORMAT;
70 18         127 Geo::BUFR->set_tableformat($tableformat);
71              
72             # Set BUFR table path
73 18 50       61 if ($option{tablepath}) {
    0          
74             # Command line option --tablepath overrides all
75 18         120 Geo::BUFR->set_tablepath($option{tablepath});
76             } elsif ($ENV{BUFR_TABLES}) {
77             # If no --tablepath option, use the BUFR_TABLES environment variable
78 0         0 Geo::BUFR->set_tablepath($ENV{BUFR_TABLES});
79             } else {
80             # If all else fails, use the default tablepath in BUFRDC/ECCODES
81 0 0       0 if ($tableformat eq 'BUFRDC') {
    0          
82 0         0 Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_BUFRDC);
83             } elsif ($tableformat eq 'ECCODES') {
84 0         0 Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_ECCODES);
85             }
86             }
87              
88 18         34 my $dumped_message = do {
89 18         83 local $/; # Enable slurp mode
90 18 50       1377 open my $fh, '<', $infile or die "Can't open $infile: $!";
91 18         1568 <$fh>;
92             };
93              
94 18         189 my $bufr = Geo::BUFR->new();
95              
96 18         151 my $buffer = $bufr->reencode_message($dumped_message, $width);
97              
98 18 100       104 if ($option{outfile}) {
99 1         3 my $outfile = $option{outfile};
100 1 50       247 open my $fh, '>', $outfile or die "Can't open $outfile: $!";
101 1         5 binmode($fh);
102 1         0 print $fh $buffer;
103             } else {
104 17         81 binmode(STDOUT);
105 17         0 print $buffer;
106             }
107              
108             =pod
109              
110             =encoding utf8
111              
112             =head1 SYNOPSIS
113              
114             bufr_reencode.pl
115             [--outfile ]
116             [--width n]
117             [--strict_checking n]
118             [--tableformat ]
119             [--tablepath ]
120             [--verbose n]
121             [--help]
122              
123             =head1 DESCRIPTION
124              
125             Encode BUFR messages from a file containing decoded BUFR messages
126             from bufrread.pl (possibly edited). Prints to STDOUT unless option
127             C<--outfile> is used.
128              
129             Execute without arguments for Usage, with option --help for some
130             additional info.
131              
132             =head1 OPTIONS
133              
134             Bufr_reencode.pl will create BUFR message(s) printed to STDOUT from
135             contents of input file, which should match exactly what you would get
136             by running bufrread.pl on the final BUFR message(s).
137              
138             Normal use:
139              
140             bufr_reencode.pl bufr.decoded > reencoded.bufr
141              
142             after first having done
143              
144             bufrread.pl 'BUFR file' > bufr.decoded
145             Edit file bufr.decoded as desired
146              
147             Options (may be abbreviated, e.g. C<--h> or C<-h> for C<--help>):
148              
149             --outfile Will print encoded BUFR messages to
150             instead of STDOUT
151             --width n The decoded message(s) was created by using
152             bufrread.pl with option --width n
153             --strict_checking n n=0 Disable strict checking of BUFR format
154             n=1 Issue warning if (recoverable) error in
155             BUFR format
156             n=2 (default) Croak if (recoverable) error in BUFR format.
157             Nothing more in this message will be encoded.
158             --verbose n Set verbose level to n, 0<=n<=6 (default 0).
159             Verbose output is sent to STDOUT, so ought to
160             be combined with option --outfile
161             --tableformat Currently supported are BUFRDC and ECCODES (default is BUFRDC)
162             --tablepath
163             If used, will set path to BUFR tables. If not set,
164             will fetch tables from the environment variable
165             BUFR_TABLES, or if this is not set: will use
166             DEFAULT_TABLE_PATH_ hard coded in source code.
167             --help Display Usage and explain the options used. Almost
168             the same as consulting perldoc bufr_reencode.pl
169              
170             =head1 CAVEAT
171              
172             'Optional section present' in section 1 of BUFR message will always be
173             set to 0, as reencode_message in Geo::BUFR does not provide encoding
174             of section 2. A warning will be printed to STDERR if 'Optional section
175             present' originally was 1.
176              
177             =head1 AUTHOR
178              
179             Pål Sannes Epal.sannes@met.noE
180              
181             =head1 COPYRIGHT
182              
183             Copyright (C) 2010-2025 MET Norway
184              
185             =cut