bufr.pm:bufr_reencode_source

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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:
-<code> +<code perl
-#!/usr/bin/perl -w+#!/usr/bin/perl
  
-# (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: +
-#     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 strict;
 +use warnings;
 +use Carp;
 use Getopt::Long; use Getopt::Long;
-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 'ECCODES' if preferred 
 +use constant DEFAULT_TABLE_FORMAT => 'BUFRDC';
  
-my $DEFAULT_TABLE_PATH = '/usr/local/lib/emos/bufrtables';+# Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set 
 +use constant DEFAULT_TABLE_PATH_BUFRDC ='/usr/local/lib/bufrtables'; 
 +use constant DEFAULT_TABLE_PATH_ECCODES => '/usr/local/share/eccodes/definitions/bufr/tables';
  
 # Parse command line options # Parse command line options
Line 48: Line 44:
            'outfile=s',            'outfile=s',
            'strict_checking=i',            'strict_checking=i',
 +           'tableformat=s',
            'tablepath=s',            'tablepath=s',
            'verbose=i',            'verbose=i',
            'width=i',            'width=i',
-       ) or die "Wrong option(s), execute $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 $infile and -f $infile;+ 
 +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->set_strict_checking($strict_checking);+Geo::BUFR->set_strict_checking($strict_checking);
  
-my $verbose = $option{verbose} $option{verbose} : 0;+# Set verbosity level 
 +Geo::BUFR->set_verbose($option{verbose}) if $option{verbose};
  
-# 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->set_tablepath($option{tablepath});+    Geo::BUFR->set_tablepath($option{tablepath});
 } 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->set_tablepath($ENV{BUFR_TABLES});+    Geo::BUFR->set_tablepath($ENV{BUFR_TABLES});
 } else { } else {
-    # If all else fails, use the libemos bufrtables +    # If all else fails, use the default tablepath in BUFRDC/ECCODES 
-    BUFR->set_tablepath($DEFAULT_TABLE_PATH);+    if ($tableformat eq 'BUFRDC') { 
 +        Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_BUFRDC); 
 +    } elsif ($tableformat eq 'ECCODES'
 +        Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_ECCODES); 
 +    }
 } }
  
-my $dumped_message = read_file($infile)+my $dumped_message = do { 
-my $bufr = BUFR->new();+    local $/# Enable slurp mode 
 +    open my $fh, '<', $infile or die "Can't open $infile: $!"; 
 +    <$fh>
 +};
  
-# This sets object verbose level equal to class verbose level +my $bufr = Geo::BUFR->new(); 
-$bufr->set_verbose($verbose);+ 
 +my $buffer = $bufr->reencode_message($dumped_message, $width);
  
 if ($option{outfile}) { if ($option{outfile}) {
-    open(my $OUT, '>',$option{outfile}) +    my $outfile = $option{outfile}; 
-        or die "Cannot open $option{outfile} for writing: $!"; +    open my $fh, '>', $outfile or die "Can'open $outfile: $!"; 
-    print $OUT $bufr->reencode_message($dumped_message, $width); +    binmode($fh); 
-    close $OUT or die "Cannot close $option{outfile}: $!";+    print $fh $buffer;
 } else { } else {
-    print $bufr->reencode_message($dumped_message, $width);+    binmode(STDOUT); 
 +    print $buffer;
 } }
  
 +=pod
  
-sub usage { +=encoding utf8
-    print <<"EOF";+
  
-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 <file containing decoded BUFR message(s)> 
-        [--width n] +       [--outfile <file to print encoded BUFR message(s) to>] 
-        [--tablepath <path to BUFR tables>+       [--width n] 
-        [--outfile <file to print encoded BUFR message(s) to>] +       [--strict_checking n] 
-        [--strict_checking n] +       [--tableformat <BUFRDC|ECCODES>
-        [--verbose n] +       [--tablepath <path to BUFR tables>] 
-        [--help+       [--verbose n] 
-Try '$0 --help' for more information. +       [--help]
-EOF +
-    exit 0; +
-}+
  
-sub usage_verbose { +=head1 DESCRIPTION
-    print <<"EOF";+
  
-Will create BUFR message(s) printed to STDOUT from content of input +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<--outfile> is used. 
 + 
 +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 of input file, which should match exactly what you would get 
 +by running bufrread.pl on the final BUFR message(s).
  
 Normal use: Normal use:
-     $0 bufr.decoded > bufr.reencoded + 
-  after first having done +     bufr_reencode.pl bufr.decoded > reencoded.bufr 
-     bufrread.pl <BUFR file> bufr.decoded+ 
 +after first having done 
 + 
 +     bufrread.pl 'BUFR file> bufr.decoded
      Edit file bufr.decoded as desired      Edit file bufr.decoded as desired
  
-Options+Options (may be abbreviated, e.gC<--h> or C<-hfor C<--help>):
-      --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 (recoverableerror 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 +   --outfile <filename>  Will print encoded BUFR messages to <filename> 
-reencode_message in BUFR.pm currently does not provide encoding +                         instead of STDOUT 
-of section 2. A warning will be printed to STDERR if 'Optional +   --width n             The decoded message(s) was created by using 
-section present' originally was 1.+                         bufrread.pl with option --width n 
 +   --strict_checking n   n=Disable strict checking of BUFR format 
 +                         n=1 Issue warning if (recoverable) error in 
 +                             BUFR format 
 +                         n=(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<=6 (default 0). 
 +                         Verbose output is sent to STDOUT, so ought to 
 +                         be combined with option --outfile 
 +   --tableformat         Currently supported are BUFRDC and ECCODES (default is BUFRDC) 
 +   --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_<tableformat> hard coded in source code. 
 +   --help                Display Usage and explain the options used. Almost 
 +                         the same as consulting perldoc bufr_reencode.pl
  
-EOF +=head1 CAVEAT 
-    exit 0; + 
-}+'Optional section present' in section 1 of BUFR message will always be 
 +set to 0, as reencode_message in Geo::BUFR does not provide encoding 
 +of section 2. A warning will be printed to STDERR if 'Optional section 
 +present' originally was 1. 
 + 
 +=head1 AUTHOR 
 + 
 +Pål Sannes E<lt>pal.sannes@met.noE<gt> 
 + 
 +=head1 COPYRIGHT 
 + 
 +Copyright (C) 2010-2023 MET Norway 
 + 
 +=cut
 </code> </code>
  • bufr.pm/bufr_reencode_source.1267015910.txt.gz
  • Last modified: 2022-05-31 09:23:11
  • (external edit)