User Tools

Site Tools


bufr.pm:bufrresolve_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.

# Usage 1: $0 <descriptor(s)> [options]

# Resolve the given descriptor(s) fully into table B descriptors, with
# name, unit, scale, reference value and width written on each line.
# With option --noexpand no expansion of D descriptors and replication
# is done. With option --partial table D descriptors are expanded only
# once and replication ignored. Option --simple is like --partial, but
# all descriptors are listed on one line without any info.

# The tables used can be chosen by the user with options --bufrtable
# and --tablepath. Default is DEFAULT_TABLE in directory
# DEFAULT_TABLE_PATH or in directory $ENV{BUFR_TABLES} if this is set.

# Usage 2: $0 --code <code_table> [options]

# Print the content of code or flag table <code_table>

# Usage 3: $0 --flag <value> --code <flag_table> [options]

# Display the bits set for flag value <value> in flag table <flag_table>.

# It is supposed the code and flag tables are contained in a file with
# same name as corresponding B and D tables except for having prefix C
# instead of B or D.

# Author: P.Sannes met.no August 2008

use strict;
use Getopt::Long;

# metno module
use BUFR;

use constant DEFAULT_TABLE_PATH => '/usr/local/lib/emos/bufrtables';
use constant DEFAULT_TABLE => 'B0000000000098013001';

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

GetOptions(
           \%option,
           'tablepath=s',# Set BUFR table path
           'code=s',     # Print the content of code table
           'flag=i',     # Resolve the flag value given
           'help',       # Print help information and exit
           'noexpand',   # Don't expand D descriptors
           'partial',    # Expand D descriptors only once, ignoring
                         # replication
           'simple',     # Like 'partial', but displaying the resulting
                         # descriptors on one line
           'bufrtable=s',# Set BUFR tables
           'verbose',    # Display path and tables used
       ) or die "Wrong option(s), execute $0 without arguments for Usage\n";

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

# No arguments if --code or --flag, else there should be at least one argument
if (defined $option{code} or defined $option{flag}) {
    usage() if @ARGV;
} else {
    usage() if not @ARGV;
}

# If --flag is set, user must also provide code table
usage() if defined $option{flag} and !defined $option{code};

# Set verbosity level for the BUFR module
my $verbose = $option{verbose} ? 1 : 0;
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 libemos bufrtables
    BUFR->set_tablepath(DEFAULT_TABLE_PATH);
}
print 'BUFR tablepath: ', BUFR->get_tablepath(), "\n" if $verbose;

# BUFR table file to use
my $table = $option{bufrtable} || DEFAULT_TABLE;

my $bufr = BUFR->new();

if (defined $option{code}) {
    # Resolve flag value or dump code table
    my $code_table = $option{code};
    if (defined $option{flag}) {
        print $bufr->resolve_flagvalue($option{flag}, $code_table, $table);
    } else {
        print $bufr->dump_codetable($code_table, $table);
    }
} else {
    # Resolve descriptor(s)
    $bufr->load_BDtables($table);
    if ($option{simple}) {
        print $bufr->resolve_descriptor('simply', @ARGV);
    } elsif ($option{partial}) {
        print $bufr->resolve_descriptor('partially', @ARGV);
    } elsif ($option{noexpand}) {
        print $bufr->resolve_descriptor('noexpand', @ARGV);
    } else {
        print $bufr->resolve_descriptor('fully', @ARGV);
    }
}


sub usage {
    print <<"EOF";

Usage 1: $0 <descriptor(s)> [options]

  where options (may be abbreviated, e.g. --h or -h for --help) are

        [--partial]                    Expand D descriptors only once, ignoring
                                       replication
        [--simple]                     Like --partial, but displaying the resulting
                                       descriptors on one line
        [--noexpand]                   Don't expand D descriptors at all
        [--bufrtable <name of BUFR B or D table]  Set BUFR tables
        [--tablepath <path to BUFR tables>]  Set BUFR table path
        [--verbose]                    Display path and tables used
        [--help]                       Print this help info and exit

  --simple, --noexpand and --partial are mutually exclusive (full expansion is default).
  Without --simple will display each B descriptor with name, unit, scale,
  reference value and data width (in bits).

Usage 2: $0 --code <code_table> [options]

  Print the content of code or flag table <code_table> to screen.

Usage 3: $0 --flag <value> --code <flag_table> [options]

  Display the bits set for flag value <value> for flag table <flag_table>.

For Usage 2 and 3 the only useful options are --bufrtable, --tablepath and --verbose.

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