Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
bufr.pm:bufr_reencode_source [2010-02-24 12:51:50] pals created |
bufr.pm:bufr_reencode_source [2023-02-05 10:20:14] (current) pals |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | < |
- | # | + | # |
- | # (C) Copyright 2010, met.no | + | # (C) Copyright 2010-2023 MET Norway |
# | # | ||
# This program is free software; you can redistribute it and/or modify | # This program is free software; you can redistribute it and/or modify | ||
Line 19: | Line 19: | ||
# 02110-1301, USA. | # 02110-1301, USA. | ||
- | # Encode a BUFR message from a file containing a decoded BUFR message | + | # pod included at end of file |
- | # from bufrread.pl (possibly edited). | + | |
- | + | ||
- | # Normal use: | + | |
- | # | + | |
- | # after first having done | + | |
- | # | + | |
- | # Edit file bufr.decoded as desired | + | |
- | + | ||
- | # Author: P. Sannes met.no 2010 | + | |
use strict; | use strict; | ||
+ | use warnings; | ||
+ | use Carp; | ||
use Getopt:: | use Getopt:: | ||
- | use Carp; | + | use Pod::Usage qw(pod2usage); |
- | use File::Slurp; | + | use Geo::BUFR; |
- | # metno module | + | # This is actually default in BUFR.pm, but provided here to make it |
- | use BUFR; | + | # easier for users to change to ' |
+ | use constant DEFAULT_TABLE_FORMAT => ' | ||
- | my $DEFAULT_TABLE_PATH | + | # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set |
+ | use constant DEFAULT_TABLE_PATH_BUFRDC | ||
+ | use constant DEFAULT_TABLE_PATH_ECCODES => '/ | ||
# Parse command line options | # Parse command line options | ||
Line 48: | Line 44: | ||
' | ' | ||
' | ' | ||
+ | ' | ||
' | ' | ||
' | ' | ||
' | ' | ||
- | ) or die "Wrong option(s), execute $0 without arguments for Usage\n" | + | ) or pod2usage(-verbose => 0); |
# User asked for help | # User asked for help | ||
- | usage_verbose() if $option{help}; | + | pod2usage(-verbose => 1) if $option{help}; |
- | + | ||
- | my $width = $option{width} ? $option{width} : 15; | + | |
+ | # Make sure there is an input file | ||
+ | pod2usage(-verbose => 0) unless @ARGV == 1; | ||
my $infile = shift; | my $infile = shift; | ||
- | usage() unless | + | |
+ | my $width = $option{width} ? $option{width} : 15; | ||
# Default is croak if (recoverable) error found in encoded BUFR format | # Default is croak if (recoverable) error found in encoded BUFR format | ||
my $strict_checking = defined $option{strict_checking} | my $strict_checking = defined $option{strict_checking} | ||
? $option{strict_checking} : 2; | ? $option{strict_checking} : 2; | ||
- | BUFR-> | + | Geo::BUFR-> |
- | my $verbose = $option{verbose} | + | # Set verbosity level |
+ | Geo:: | ||
- | # Set verbosity level for the BUFR module. Must be set also for each | + | # Set BUFR table format |
- | # BUFR object generated | + | my $tableformat = (defined $option{tableformat}) ? uc $option{tableformat} : DEFAULT_TABLE_FORMAT; |
- | BUFR->set_verbose($verbose); | + | Geo::BUFR->set_tableformat($tableformat); |
# Set BUFR table path | # Set BUFR table path | ||
if ($option{tablepath}) { | if ($option{tablepath}) { | ||
# Command line option --tablepath overrides all | # Command line option --tablepath overrides all | ||
- | BUFR-> | + | |
} elsif ($ENV{BUFR_TABLES}) { | } elsif ($ENV{BUFR_TABLES}) { | ||
# If no --tablepath option, use the BUFR_TABLES environment variable | # If no --tablepath option, use the BUFR_TABLES environment variable | ||
- | BUFR-> | + | |
} else { | } else { | ||
- | # If all else fails, use the libemos bufrtables | + | # If all else fails, use the default tablepath in BUFRDC/ |
- | BUFR-> | + | |
+ | Geo::BUFR-> | ||
+ | } elsif ($tableformat eq ' | ||
+ | Geo:: | ||
+ | } | ||
} | } | ||
- | my $dumped_message = read_file($infile); | + | my $dumped_message = do { |
- | my $bufr = BUFR->new(); | + | local $/; # Enable slurp mode |
+ | | ||
+ | <$fh>; | ||
+ | }; | ||
- | # This sets object verbose level equal to class verbose level | + | my $bufr = Geo:: |
- | $bufr->set_verbose($verbose); | + | |
+ | my $buffer = $bufr->reencode_message($dumped_message, | ||
if ($option{outfile}) { | if ($option{outfile}) { | ||
- | open(my $OUT, '>', | + | |
- | | + | |
- | | + | |
- | | + | |
} else { | } else { | ||
- | print $bufr-> | + | |
+ | | ||
} | } | ||
+ | =pod | ||
- | sub usage { | + | =encoding utf8 |
- | print <<" | + | |
- | Print encoded BUFR message(s) to STDOUT (or file named in option | + | =head1 SYNOPSIS |
- | --outfile) based on decoded BUFR message(s) from bufrread.pl | + | |
- | Usage: $0 <file containing decoded BUFR message(s)> | + | bufr_reencode.pl |
- | | + | |
- | [--tablepath <path to BUFR tables> | + | |
- | | + | |
- | [--strict_checking | + | |
- | [--verbose | + | [--tablepath <path to BUFR tables>] |
- | [--help] | + | [--verbose n] |
- | Try ' | + | [--help] |
- | EOF | + | |
- | exit 0; | + | |
- | } | + | |
- | sub usage_verbose { | + | =head1 DESCRIPTION |
- | print <<" | + | |
- | Will create BUFR message(s) printed to STDOUT from content | + | Encode BUFR messages from a file containing decoded BUFR messages |
- | file, which should match exactly what you would get by running | + | from bufrread.pl (possibly edited). Prints to STDOUT unless option |
- | bufrread.pl on the final BUFR message(s). | + | C< |
+ | |||
+ | Execute without arguments for Usage, with option --help for some | ||
+ | additional info. | ||
+ | |||
+ | =head1 OPTIONS | ||
+ | |||
+ | Bufr_reencode.pl will create BUFR message(s) printed to STDOUT from | ||
+ | contents | ||
+ | by running bufrread.pl on the final BUFR message(s). | ||
Normal use: | Normal use: | ||
- | | + | |
- | after first having done | + | |
- | | + | |
+ | after first having done | ||
+ | |||
+ | | ||
Edit file bufr.decoded as desired | Edit file bufr.decoded as desired | ||
- | Options: | + | Options (may be abbreviated, |
- | --width n | + | |
- | The decoded message(s) was created by using bufrread.pl with option | + | |
- | --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, | + | |
- | $DEFAULT_TABLE_PATH | + | |
- | | + | |
- | Will print encoded BUFR messages to <filename> | + | |
- | | + | |
- | 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< | + | |
- | so ought to be combined with option --outfile | + | |
- | --help | + | |
- | Will print this help message and then exit | + | |
- | Note: ' | + | |
- | reencode_message | + | |
- | of section | + | |
- | section present' | + | |
+ | | ||
+ | n=1 Issue warning if (recoverable) error in | ||
+ | BUFR format | ||
+ | n=2 (default) Croak if (recoverable) error in BUFR format. | ||
+ | | ||
+ | | ||
+ | | ||
+ | be combined with option --outfile | ||
+ | | ||
+ | | ||
+ | If used, will set path to BUFR tables. If not set, | ||
+ | will fetch tables from the environment variable | ||
+ | | ||
+ | DEFAULT_TABLE_PATH_< | ||
+ | | ||
+ | the same as consulting perldoc bufr_reencode.pl | ||
- | EOF | + | =head1 CAVEAT |
- | | + | |
- | } | + | ' |
+ | set to 0, as reencode_message in Geo::BUFR does not provide encoding | ||
+ | of section 2. A warning will be printed to STDERR if ' | ||
+ | present' | ||
+ | |||
+ | =head1 AUTHOR | ||
+ | |||
+ | Pål Sannes E< | ||
+ | |||
+ | =head1 COPYRIGHT | ||
+ | |||
+ | Copyright (C) 2010-2023 MET Norway | ||
+ | |||
+ | =cut | ||
</ | </ |