User Tools

Site Tools


bufr.pm:bufr_reencode_source

This is an old revision of the document!


#!/usr/bin/perl -w

# (C) Copyright 2010, met.no
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# Encode a BUFR message from a file containing a decoded BUFR message
# from bufrread.pl (possibly edited).

# Normal use:
#     bufr_reencode bufr.decoded > bufr.reencoded
# after first having done 
#     bufrread.pl <BUFR file> > bufr.decoded
#     Edit file bufr.decoded as desired

# Author: P. Sannes met.no 2010

use strict;
use Getopt::Long;
use Carp;
use File::Slurp;
use BUFR;

# Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set
my $DEFAULT_TABLE_PATH = '/usr/local/lib/bufrtables';

# Parse command line options
my %option = ();

GetOptions(
           \%option,
           'help',
           'outfile=s',
           'strict_checking=i',
           'tablepath=s',
           'verbose=i',
           'width=i',
       ) or die "Wrong option(s), execute $0 without arguments for Usage\n";

# User asked for help
usage_verbose() if $option{help};

my $width = $option{width} ? $option{width} : 15;

my $infile = shift;
usage() unless $infile and -f $infile;

# Default is croak if (recoverable) error found in encoded BUFR format
my $strict_checking = defined $option{strict_checking}
    ? $option{strict_checking} : 2;
BUFR->set_strict_checking($strict_checking);

my $verbose = $option{verbose} ? $option{verbose} : 0;

# Set verbosity level for the BUFR module. Must be set also for each
# BUFR object generated
BUFR->set_verbose($verbose);

# Set BUFR table path
if ($option{tablepath}) {
    # Command line option --tablepath overrides all
    BUFR->set_tablepath($option{tablepath});
} elsif ($ENV{BUFR_TABLES}) {
    # If no --tablepath option, use the BUFR_TABLES environment variable
    BUFR->set_tablepath($ENV{BUFR_TABLES});
} else {
    # If all else fails, use the default bufrtables
    BUFR->set_tablepath($DEFAULT_TABLE_PATH);
}

my $dumped_message = read_file($infile);
my $bufr = BUFR->new();

# This sets object verbose level equal to class verbose level
$bufr->set_verbose($verbose);

if ($option{outfile}) {
    open(my $OUT, '>',$option{outfile})
        or die "Cannot open $option{outfile} for writing: $!";
    print $OUT $bufr->reencode_message($dumped_message, $width);
    close $OUT or die "Cannot close $option{outfile}: $!";
} else {
    print $bufr->reencode_message($dumped_message, $width);
}


sub usage {
    print <<"EOF";

Print encoded BUFR message(s) to STDOUT (or file named in option
--outfile) based on decoded BUFR message(s) from bufrread.pl

Usage: $0 <file containing decoded BUFR message(s)>
        [--width n]
        [--tablepath <path to BUFR tables>]
        [--outfile <file to print encoded BUFR message(s) to>]
        [--strict_checking n]
        [--verbose n]
        [--help]
Try '$0 --help' for more information.
EOF
    exit 0;
}

sub usage_verbose {
    print <<"EOF";

Will create BUFR message(s) printed to STDOUT from content of input
file, which should match exactly what you would get by running
bufrread.pl on the final BUFR message(s).

Normal use:
     $0 bufr.decoded > bufr.reencoded
  after first having done
     bufrread.pl <BUFR file> > bufr.decoded
     Edit file bufr.decoded as desired

Options:
      --width n
  The decoded message(s) was created by using bufrread.pl with option --width n
      --tablepath <path to BUFR tables>
  If used, will set path to BUFR tables. If not set, will fetch tables
  from the environment variable BUFR_TABLES, or if this is not set: will use
  $DEFAULT_TABLE_PATH
      --outfile <filename>
  Will print encoded BUFR messages to <filename> instead of STDOUT
      --strict_checking n n=0 Disable strict checking of BUFR format
                          n=1 Issue warning if (recoverable) error in
                              BUFR format
                          n=2 (default) Croak if (recoverable) error in BUFR format.
                              Nothing more in this message will be encoded.
      --verbose n
  Set verbose level to n, 0<=n<=3 (default 0). Verbose output is sent to STDOUT,
  so ought to be combined with option --outfile
      --help
  Will print this help message and then exit

Note: 'Optional section present' will always be set to 0, as
reencode_message in BUFR.pm currently does not provide encoding
of section 2. A warning will be printed to STDERR if 'Optional
section present' originally was 1.

EOF
    exit 0;
}
bufr.pm/bufr_reencode_source.1268212885.txt.gz · Last modified: (external edit)