| 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 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 18 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 18 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 18 | ▶ | use Getopt::Long; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 18 | ▶ | use Pod::Usage qw(pod2usage); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 18 | ▶ | use Geo::BUFR; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 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 | ▶ | use constant DEFAULT_TABLE_FORMAT => 'BUFRDC'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set | ||||||||||||||
| 34 | 18 | ▶ | use constant DEFAULT_TABLE_PATH_BUFRDC => '/usr/local/lib/bufrtables'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 18 | ▶ | use constant DEFAULT_TABLE_PATH_ECCODES => '/usr/local/share/eccodes/definitions/bufr/tables'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | # Parse command line options | ||||||||||||||
| 38 | 18 | my %option = (); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 18 | ▶ | GetOptions( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ▶ | pod2usage(-verbose => 1) if $option{help}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | |||||||||||||||
| 54 | # Make sure there is an input file | ||||||||||||||
| 55 | 18 | ▶ | pod2usage(-verbose => 0) unless @ARGV == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 18 | my $infile = shift; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 18 | ▶ | my $width = $option{width} ? $option{width} : 15; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | |||||||||||||||
| 60 | # Default is croak if (recoverable) error found in encoded BUFR format | ||||||||||||||
| 61 | my $strict_checking = defined $option{strict_checking} | ||||||||||||||
| 62 | 18 | ▶ | ? $option{strict_checking} : 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 18 | Geo::BUFR->set_strict_checking($strict_checking); | |||||||||||||
| 64 | |||||||||||||||
| 65 | # Set verbosity level | ||||||||||||||
| 66 | 18 | ▶ | Geo::BUFR->set_verbose($option{verbose}) if $option{verbose}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | |||||||||||||||
| 68 | # Set BUFR table format | ||||||||||||||
| 69 | 18 | ▶ | my $tableformat = (defined $option{tableformat}) ? uc $option{tableformat} : DEFAULT_TABLE_FORMAT; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 18 | Geo::BUFR->set_tableformat($tableformat); | |||||||||||||
| 71 | |||||||||||||||
| 72 | # Set BUFR table path | ||||||||||||||
| 73 | 18 | ▶ | if ($option{tablepath}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 74 | # Command line option --tablepath overrides all | ||||||||||||||
| 75 | 18 | Geo::BUFR->set_tablepath($option{tablepath}); | |||||||||||||
| 76 | } elsif ($ENV{BUFR_TABLES}) { | ||||||||||||||
| 77 | # If no --tablepath option, use the BUFR_TABLES environment variable | ||||||||||||||
| 78 | 0 | Geo::BUFR->set_tablepath($ENV{BUFR_TABLES}); | |||||||||||||
| 79 | } else { | ||||||||||||||
| 80 | # If all else fails, use the default tablepath in BUFRDC/ECCODES | ||||||||||||||
| 81 | 0 | ▶ | if ($tableformat eq 'BUFRDC') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 82 | 0 | Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_BUFRDC); | |||||||||||||
| 83 | } elsif ($tableformat eq 'ECCODES') { | ||||||||||||||
| 84 | 0 | Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_ECCODES); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 18 | my $dumped_message = do { | |||||||||||||
| 89 | 18 | local $/; # Enable slurp mode | |||||||||||||
| 90 | 18 | ▶ | open my $fh, '<', $infile or die "Can't open $infile: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 18 | <$fh>; | |||||||||||||
| 92 | }; | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 18 | my $bufr = Geo::BUFR->new(); | |||||||||||||
| 95 | |||||||||||||||
| 96 | 18 | my $buffer = $bufr->reencode_message($dumped_message, $width); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 18 | ▶ | if ($option{outfile}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 1 | my $outfile = $option{outfile}; | |||||||||||||
| 100 | 1 | ▶ | open my $fh, '>', $outfile or die "Can't open $outfile: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 1 | binmode($fh); | |||||||||||||
| 102 | 1 | print $fh $buffer; | |||||||||||||
| 103 | } else { | ||||||||||||||
| 104 | 17 | binmode(STDOUT); | |||||||||||||
| 105 | 17 | print $buffer; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =pod | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =encoding utf8 | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head1 SYNOPSIS | ||||||||||||||
| 113 | |||||||||||||||
| 114 | bufr_reencode.pl <file containing decoded BUFR message(s)> | ||||||||||||||
| 115 | [--outfile <file to print encoded BUFR message(s) to>] | ||||||||||||||
| 116 | [--width n] | ||||||||||||||
| 117 | [--strict_checking n] | ||||||||||||||
| 118 | [--tableformat <BUFRDC|ECCODES>] | ||||||||||||||
| 119 | [--tablepath <path to BUFR tables>] | ||||||||||||||
| 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 <filename> Will print encoded BUFR messages to <filename> | ||||||||||||||
| 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 <path to BUFR tables> | ||||||||||||||
| 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_<tableformat> 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 E<lt>pal.sannes@met.noE<gt> | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||||||
| 182 | |||||||||||||||
| 183 | Copyright (C) 2010-2025 MET Norway | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =cut | ||||||||||||||