bufr.pm:bufrencode_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:bufrencode_source [2014-05-28 14:27:06]
pals
bufr.pm:bufrencode_source [2023-02-05 10:18:55] (current)
pals
Line 1: Line 1:
 <code perl> <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 Getopt::Long; use Getopt::Long;
 use Pod::Usage qw(pod2usage); use Pod::Usage qw(pod2usage);
-use File::Slurp qw(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 40: Line 45:
            'outfile=s',            'outfile=s',
            'strict_checking=i',            'strict_checking=i',
 +           'tableformat=s',
            'tablepath=s',            'tablepath=s',
            'verbose=i',            'verbose=i',
Line 60: 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 69: 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); 
 +    }
 } }
  
Line 87: Line 101:
  
 # Print the encoded BUFR message # Print the encoded BUFR message
 +my $buffer = $bufr->encode_message($data_refs, $desc_refs);
 if ($option{outfile}) { if ($option{outfile}) {
-    my $buffer $bufr->encode_message($data_refs, $desc_refs); +    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->encode_message($data_refs, $desc_refs);+    binmode(STDOUT); 
 +    print $buffer;
 } }
- 
  
 # See OPTIONS section in pod for format of metadata file # See OPTIONS section in pod for format of metadata file
Line 105: Line 122:
         chomp;         chomp;
         next if /^\s*$/;         next if /^\s*$/;
 +        s/^\s+//;
         my ($key, $value) = split /\s+/, $_, 2;         my ($key, $value) = split /\s+/, $_, 2;
         $metadata{$key} = $value;         $metadata{$key} = $value;
Line 180: Line 198:
  
 =pod =pod
 +
 +=encoding utf8
  
 =head1 SYNOPSIS =head1 SYNOPSIS
Line 186: Line 206:
       [--outfile <file to print encoded BUFR message to>]       [--outfile <file to print encoded BUFR message to>]
       [--strict_checking n]       [--strict_checking n]
 +      [--tableformat <BUFRDC|ECCODES>]
       [--tablepath <path to BUFR tables>]       [--tablepath <path to BUFR tables>]
       [--verbose n]       [--verbose n]
Line 197: Line 218:
  
 Execute without arguments for Usage, with option --help for some Execute without arguments for Usage, with option --help for some
-additional info. See also L</https://wiki.met.no/bufr.pm/start> for+additional info. See also L<https://wiki.met.no/bufr.pm/start> for
 examples of use. examples of use.
  
 =head1 OPTIONS =head1 OPTIONS
  
-   --outfile <filename>  Will print the encoded BUFR message to <filename> +   --help               Display Usage and explain the options. Almost 
-                         instead of STDOUT +                        the same as consulting perldoc bufrencode.pl 
- +   --outfile <filename> Will print the encoded BUFR message to <filename> 
-   --strict_checking n   n=0 Disable strict checking of BUFR format +                        instead of STDOUT 
-                         n=1 Issue warning if (recoverable) error in +   --strict_checking n  n=0 Disable strict checking of BUFR format 
-                             BUFR format +                        n=1 Issue warning if (recoverable) error in 
-                         n=2 (default) Croak if (recoverable) error in BUFR format. +                            BUFR format 
-                             Nothing more in this message will be encoded. +                        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<=6 (default 0)+   --tableformat        Currently supported are BUFRDC and ECCODES (default is BUFRDC)
-                         Verbose output is sent to STDOUT, so ought to +
-                         be combined with option --outfile +
    --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 
-                         will fetch tables from the environment variable +                        set, will fetch tables from the environment 
-                         BUFR_TABLES, or if this is not set: will use +                        variable BUFR_TABLES, or if this is not set: 
-                         DEFAULT_TABLE_PATH hard coded in source code. +                        will use DEFAULT_TABLE_PATH_<tableformat> 
- +                        hard coded in source code. 
-   --help                Display Usage and explain the optionsAlmost +   --verbose n          Set verbose level to n, 0<=n<=6 (default 0)
-                         the same as consulting perldoc bufrencode.pl+                        Verbose output is sent to STDOUT, so ought to 
 +                        be combined with option --outfile
  
 =head2 Required options =head2 Required options
Line 301: Line 320:
 =head1 COPYRIGHT =head1 COPYRIGHT
  
-Copyright (C) 2010 met.no+Copyright (C) 2010-2023 MET Norway
  
 =cut =cut
 </code> </code>
  • bufr.pm/bufrencode_source.1401287226.txt.gz
  • Last modified: 2022-05-31 09:23:11
  • (external edit)