bufr.pm:bufralter.pl_source

Differences

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

Link to this comparison view

Next revision
Previous revision
bufr.pm:bufralter.pl_source [2010-03-31 14:44:03]
pals created
bufr.pm:bufralter.pl_source [2023-02-05 10:21:16] (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 Getopt::Long; use Getopt::Long;
 use Pod::Usage qw(pod2usage); use Pod::Usage qw(pod2usage);
 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/emos/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 55: Line 61:
            'subcategory=i',            'subcategory=i',
            'subcentre=i',            'subcentre=i',
 +           'tableformat=s',
            'tablepath=s',            'tablepath=s',
            'update_number=i',            'update_number=i',
Line 79: Line 86:
 Geo::BUFR->set_strict_checking($strict_checking); Geo::BUFR->set_strict_checking($strict_checking);
  
-# Set verbosity level for the BUFR module. Must be set also for each +# Set verbosity level 
-# BUFR object generated +Geo::BUFR->set_verbose($option{verbose}) if $option{verbose}; 
-my $verbose = $option{verbose} ? $option{verbose} : 0+ 
-Geo::BUFR->set_verbose($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 92: Line 101:
     Geo::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 
-    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 104: Line 117:
     $OUT = *STDOUT;     $OUT = *STDOUT;
 } }
 +binmode($OUT);
  
 # Change input separator to 'BUFR' # Change input separator to 'BUFR'
Line 138: Line 152:
     $/ = $oldeol;     $/ = $oldeol;
     my $bufr = Geo::BUFR->new($msg);     my $bufr = Geo::BUFR->new($msg);
-    $bufr->set_verbose($verbose); 
  
     $out .= alter($bufr);     $out .= alter($bufr);
Line 147: Line 160:
 } }
  
-print $OUT $out;+print $OUT $out if $out;
  
  
Line 159: Line 172:
     }     }
  
-    my $new_bufr;+    my $new_bufr = Geo::BUFR->new();
     my @subset_data; # Will contain data values for subset 1,2...     my @subset_data; # Will contain data values for subset 1,2...
     my @subset_desc; # Will contain the set of descriptors for subset 1,2...     my @subset_desc; # Will contain the set of descriptors for subset 1,2...
Line 172: Line 185:
  
         if ($isub == 1) {         if ($isub == 1) {
-            $new_bufr = Geo::BUFR->clone($bufr);+            $new_bufr->copy_from($bufr,'metadata');
             @subset_data = ();             @subset_data = ();
             @subset_desc = ();             @subset_desc = ();
Line 196: Line 209:
                 for (my $i=0; $i < @$descriptors; $i++) {                 for (my $i=0; $i < @$descriptors; $i++) {
                     if ($descriptors->[$i] == $desc) {                     if ($descriptors->[$i] == $desc) {
-                        $data->[$i] = ($value eq 'missing'undef $value;+                        if ($value =~ /(.*)\+$/) { 
 +                            $data->[$i] +$1; 
 +                        } elsif ($value eq 'missing'
 +                            $data->[$i] = undef
 +                        } else { 
 +                            $data->[$i] = $value; 
 +                        }
                         next DESCRIPTOR;                         next DESCRIPTOR;
                     }                     }
Line 323: Line 342:
  
 =pod =pod
 +
 +=encoding utf8
  
 =head1 SYNOPSIS =head1 SYNOPSIS
  
   bufralter.pl <bufr file>   bufralter.pl <bufr file>
-      [--data <descriptor=value>]+      [--data <descriptor=value[+]>]
       [--bufr_edition <value>]       [--bufr_edition <value>]
       [--centre <value>]       [--centre <value>]
Line 351: Line 372:
       [--outfile <file>]       [--outfile <file>]
       [--strict_checking n]       [--strict_checking n]
 +      [--tableformat <BUFRDC|ECCODES>]
       [--tablepath <path to BUFR tables>]       [--tablepath <path to BUFR tables>]
       [--verbose n]       [--verbose n]
Line 358: Line 380:
  
 Will alter the BUFR messages in <bufr file> according to what is Will alter the BUFR messages in <bufr file> according to what is
-specified by the options provided. The modified file (note that text +specified by the options provided. The modified file (text surrounding 
-surrounding the BUFR messages is not affected) will be printed to +the BUFR messages will not be affected) will be printed to STDOUT 
-STDOUT (unless C<--outfile> is used).+(unless C<--outfile> is set).
  
 Execute without arguments for Usage, with option C<--help> for some Execute without arguments for Usage, with option C<--help> for some
 additional info. additional info.
- 
-bufralter.pl has quite a few options to choose from, but it is easy to 
-think of even more usages not yet covered - like being able to change 
-every data value for a descriptor by the same amount, increment date 
-etc. In that case: modify your own copy of bufralter.pl, or please 
-contact the author with your suggestion for extensions. 
  
 =head1 OPTIONS =head1 OPTIONS
  
- +   --data <descriptor=value[+]> Set (first) data value in section 4 for 
-   --data <descriptor=value> Set data value in section 4 for descriptor. +                    descriptor. A trailing '+' means that the value 
-                    Use 'missing' to set a missing value and repeat the +                    should be added to existing value. Use 'missing' 
-                    option if more sequence descriptors are to be set. +                    to set a missing value. Repeat the option if more 
-                    Example: --value 004005=10 --value 012101=missing +                    sequence descriptors are to be set. Example: 
-                    This will set the data value for first (and only first!) +                    --data 004004=-1+ --data 004005=50 --data 
-                    occurrence of these descriptors in every subset and +                    012101=missing This will set the data value for 
-                    every message in <bufr file> to the given value.+                    first (and only first!) occurrence of these 
 +                    descriptors in every subset and every message in 
 +                    <bufr file> to the given value (subtracting 1 from 
 +                    the existing value for 004004)
    --bufr_edition <value> Set BUFR edition to <value>. If the new edition    --bufr_edition <value> Set BUFR edition to <value>. If the new edition
                     involves some metadata not present in the old edition,                     involves some metadata not present in the old edition,
Line 406: Line 425:
    --compress 0|1   Set compression in section 3 to 0 or 1    --compress 0|1   Set compression in section 3 to 0 or 1
    --remove_sec2    Remove optional section 2 if present    --remove_sec2    Remove optional section 2 if present
-   --remove_qc      Remove all quality information,+   --remove_qc      Remove all quality control information,
                     i.e. remove all descriptors from 222000 on                     i.e. remove all descriptors from 222000 on
    --outfile <filename>    --outfile <filename>
Line 416: Line 435:
                              Nothing more in this message will be                              Nothing more in this message will be
                              decoded/encoded.                              decoded/encoded.
 +   --tableformat    Currently supported are BUFRDC and ECCODES (default is BUFRDC)
    --tablepath <path to BUFR tables>    --tablepath <path to BUFR tables>
-                    Set path to BUFR tables (overrides ENV{BUFR_TABLES}) +                    Set path to BUFR tables (overrides $ENV{BUFR_TABLES}) 
-   --verbose n      Set verbose level to n, 0<=n<=(default 0). Verbose+   --verbose n      Set verbose level to n, 0<=n<=(default 0). Verbose
                     output is sent to STDOUT, so ought to be combined with                     output is sent to STDOUT, so ought to be combined with
                     option --outfile                     option --outfile
Line 429: Line 449:
 set the environment variable BUFR_TABLES to the directory where your set the environment variable BUFR_TABLES to the directory where your
 BUFR tables are located (unless the default path provided by BUFR tables are located (unless the default path provided by
-bufralter.pl works for you).+bufralter.pl works for you). For tableformat ECCODES, se 
 +L<http://search.cpan.org/dist/Geo-BUFR/lib/Geo/BUFR.pm#BUFR-TABLE-FILES> 
 +for more info on how to set C<--tablepath> (or BUFR_TABLES).
  
 =head1 AUTHOR =head1 AUTHOR
Line 437: Line 459:
 =head1 COPYRIGHT =head1 COPYRIGHT
  
-Copyright (C) 2010 met.no+Copyright (C) 2010-2023 MET Norway
  
 =cut =cut
 </code> </code>
  • bufr.pm/bufralter.pl_source.1270046643.txt.gz
  • Last modified: 2022-05-31 09:23:11
  • (external edit)