bufr.pm:bufr_reencode_source

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
bufr.pm:bufr_reencode_source [2010-10-01 17:17:46]
pals
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 22: Line 22:
  
 use strict; use strict;
 +use warnings;
 use Carp; use Carp;
 use Getopt::Long; use Getopt::Long;
 use Pod::Usage qw(pod2usage); use Pod::Usage qw(pod2usage);
-use File::Slurp qw(read_file write_file); 
 use Geo::BUFR; use Geo::BUFR;
 +
 +# This is actually default in BUFR.pm, but provided here to make it
 +# easier for users to change to 'ECCODES' if preferred
 +use constant DEFAULT_TABLE_FORMAT => 'BUFRDC';
  
 # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set
-use constant DEFAULT_TABLE_PATH => '/usr/local/lib/bufrtables';+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 39: Line 44:
            'outfile=s',            'outfile=s',
            'strict_checking=i',            'strict_checking=i',
 +           'tableformat=s',
            'tablepath=s',            'tablepath=s',
            'verbose=i',            'verbose=i',
Line 47: Line 53:
 pod2usage(-verbose => 1) 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;
-pod2usage(-verbose => 0) 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
Line 59: Line 66:
 # Set verbosity level # Set verbosity level
 Geo::BUFR->set_verbose($option{verbose}) if $option{verbose}; Geo::BUFR->set_verbose($option{verbose}) if $option{verbose};
 +
 +# Set BUFR table format
 +my $tableformat = (defined $option{tableformat}) ? uc $option{tableformat} : DEFAULT_TABLE_FORMAT;
 +Geo::BUFR->set_tableformat($tableformat);
  
 # Set BUFR table path # Set BUFR table path
Line 68: Line 79:
     Geo::BUFR->set_tablepath($ENV{BUFR_TABLES});     Geo::BUFR->set_tablepath($ENV{BUFR_TABLES});
 } else { } else {
-    # If all else fails, use the default bufrtables +    # If all else fails, use the default tablepath in BUFRDC/ECCODES 
-    Geo::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 { 
 +    local $/; # Enable slurp mode 
 +    open my $fh, '<', $infile or die "Can't open $infile: $!"; 
 +    <$fh>; 
 +}; 
 my $bufr = Geo::BUFR->new(); my $bufr = Geo::BUFR->new();
 +
 +my $buffer = $bufr->reencode_message($dumped_message, $width);
  
 if ($option{outfile}) { if ($option{outfile}) {
-    my $buffer $bufr->reencode_message($dumped_message, $width); +    my $outfile = $option{outfile}
-    write_file($option{outfile}, {binmode => ':raw'}, $buffer);+    open my $fh'>', $outfile or die "Can't open $outfile: $!"; 
 +    binmode($fh)
 +    print $fh $buffer;
 } else { } else {
-    print $bufr->reencode_message($dumped_message, $width);+    binmode(STDOUT); 
 +    print $buffer;
 } }
  
 =pod =pod
 +
 +=encoding utf8
  
 =head1 SYNOPSIS =head1 SYNOPSIS
Line 90: Line 117:
        [--width n]        [--width n]
        [--strict_checking n]        [--strict_checking n]
 +       [--tableformat <BUFRDC|ECCODES>]
        [--tablepath <path to BUFR tables>]        [--tablepath <path to BUFR tables>]
        [--verbose n]        [--verbose n]
Line 102: Line 130:
 Execute without arguments for Usage, with option --help for some Execute without arguments for Usage, with option --help for some
 additional info. additional info.
- 
-See L</OPTIONS> for normal use. 
  
 =head1 OPTIONS =head1 OPTIONS
  
 Bufr_reencode.pl will create BUFR message(s) printed to STDOUT from Bufr_reencode.pl will create BUFR message(s) printed to STDOUT from
-content of input file, which should match exactly what you would get+contents of input file, which should match exactly what you would get
 by running bufrread.pl on the final BUFR message(s). by running bufrread.pl on the final BUFR message(s).
  
Line 131: Line 157:
                          n=2 (default) Croak if (recoverable) error in BUFR format.                          n=2 (default) Croak if (recoverable) error in BUFR format.
                              Nothing more in this message will be encoded.                              Nothing more in this message will be encoded.
-   --verbose n           Set verbose level to n, 0<=n<=(default 0).+   --verbose n           Set verbose level to n, 0<=n<=(default 0).
                          Verbose output is sent to STDOUT, so ought to                          Verbose output is sent to STDOUT, so ought to
                          be combined with option --outfile                          be combined with option --outfile
 +   --tableformat         Currently supported are BUFRDC and ECCODES (default is BUFRDC)
    --tablepath <path to BUFR tables>    --tablepath <path to BUFR tables>
                          If used, will set path to BUFR tables. If not set,                          If used, will set path to BUFR tables. If not set,
                          will fetch tables from the environment variable                          will fetch tables from the environment variable
                          BUFR_TABLES, or if this is not set: will use                          BUFR_TABLES, or if this is not set: will use
-                         DEFAULT_TABLE_PATH hard coded in source code.+                         DEFAULT_TABLE_PATH_<tableformat> hard coded in source code.
    --help                Display Usage and explain the options used. Almost    --help                Display Usage and explain the options used. Almost
-                         the same as consulting perldoc bufrencode.pl+                         the same as consulting perldoc bufr_reencode.pl
  
 =head1 CAVEAT =head1 CAVEAT
  
 'Optional section present' in section 1 of BUFR message will always be 'Optional section present' in section 1 of BUFR message will always be
-set to 0, as reencode_message in Geo::BUFR currently does not provide +set to 0, as reencode_message in Geo::BUFR does not provide encoding 
-encoding of section 2. A warning will be printed to STDERR if +of section 2. A warning will be printed to STDERR if 'Optional section 
-'Optional section present' originally was 1.+present' originally was 1.
  
 =head1 AUTHOR =head1 AUTHOR
Line 155: Line 182:
 =head1 COPYRIGHT =head1 COPYRIGHT
  
-Copyright (C) 2010 met.no+Copyright (C) 2010-2023 MET Norway
  
 =cut =cut
 </code> </code>
  • bufr.pm/bufr_reencode_source.1285953466.txt.gz
  • Last modified: 2022-05-31 09:23:11
  • (external edit)