bufr.pm:bufrextract.pl_source

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revision Both sides next revision
bufr.pm:bufrextract.pl_source [2016-11-29 08:41:07]
pals created
bufr.pm:bufrextract.pl_source [2022-05-31 09:29:31]
external edit
Line 1: Line 1:
 <code perl> <code perl>
-#!/usr/bin/perl -w+#!/usr/bin/perl
  
-# (C) Copyright 2010-2016 MET Norway+# (C) Copyright 2010-2019 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);
Line 31: Line 32:
            \%option,            \%option,
            'ahl=s',        # Extract BUFR messages with AHL matching <ahl_regexp> only            'ahl=s',        # Extract BUFR messages with AHL matching <ahl_regexp> only
-           'only_ahl',     # Extract AHLs only 
-           'reuse_ahl=i',  # Reuse last AHL if current BUFR message has no AHL 
-           'without_ahl',  # Print the BUFR messages only, skipping AHLs 
            'help',         # Print help information and exit            'help',         # Print help information and exit
 +           'only_ahl',     # Extract AHLs only
            'outfile=s',    # Print to file instead of STDOUT            'outfile=s',    # Print to file instead of STDOUT
 +           'reuse_ahl=i',  # Reuse last AHL if current BUFR message has no AHL
            'verbose=i',    # Set verbose level to n, 0<=n<=6 (default 0)            'verbose=i',    # Set verbose level to n, 0<=n<=6 (default 0)
 +           'without_ahl',  # Print the BUFR messages only, skipping AHLs
        ) or pod2usage(-verbose => 0);        ) or pod2usage(-verbose => 0);
  
Line 71: Line 72:
     $OUT = *STDOUT;     $OUT = *STDOUT;
 } }
 +binmode($OUT);
  
 # No need to decode section 4 here # No need to decode section 4 here
Line 133: Line 135:
         last READLOOP if $current_subset_number == 0;         last READLOOP if $current_subset_number == 0;
  
- $current_message_number = $bufr->get_current_message_number(); +        $current_message_number = $bufr->get_current_message_number(); 
- $current_ahl = $bufr->get_current_ahl() || '';+        $current_ahl = $bufr->get_current_ahl() || '';
  
- # Must use \r\r\n after AHL, or else BUFR.pm will not +        if ($current_ahl && !$bufr->ahl_is_reused()) { 
- # recognize AHL. Should I change this in BUFR.pm? +            if ($option{only_ahl}) { 
- if ($current_ahl && !$bufr->ahl_is_reused()) { +                print $OUT $current_ahl, "\n"; 
-     if ($option{only_ahl}) { +            } elsif (!$option{without_ahl}) { 
- print $OUT $current_ahl, "\n"; +                # Use \r\r\n after AHL, since this is the standard 
-     } elsif (!$option{without_ahl}) { +                # sequence used in GTS bulletins 
- print $OUT $current_ahl . "\r\r\n"; +                print $OUT $current_ahl . "\r\r\n"; 
-     +            
-+        
- next READLOOP if $option{only_ahl};+        next READLOOP if $option{only_ahl};
  
- my $msg = $bufr->get_bufr_message(); +        my $msg = $bufr->get_bufr_message(); 
- print $OUT $msg;+        print $OUT $msg;
     }     }
 } }
Line 171: Line 173:
       [--ahl <ahl_regexp>]       [--ahl <ahl_regexp>]
       [--only_ahl] | [--without_ahl]       [--only_ahl] | [--without_ahl]
 +      [--outfile <filename>]
       [--reuse_ahl n]       [--reuse_ahl n]
       [--help]       [--help]
-      [--outfile <filename>] 
       [--verbose n]       [--verbose n]
  
Line 195: Line 197:
    --only_ahl         Extract AHLs only    --only_ahl         Extract AHLs only
    --without_ahl      Extract BUFR messages only    --without_ahl      Extract BUFR messages only
 +   --outfile <filename>
 +                      Will print to <filename> instead of STDOUT
    --reuse_ahl n  n=0 (default) AHL is considered belonging to a BUFR message    --reuse_ahl n  n=0 (default) AHL is considered belonging to a BUFR message
                       only if immediately preceding                       only if immediately preceding
Line 201: Line 205:
    --help             Display Usage and explain the options used. For even    --help             Display Usage and explain the options used. For even
                       more info you might prefer to consult perldoc bufrextract.pl                       more info you might prefer to consult perldoc bufrextract.pl
-   --outfile <filename> 
-                      Will print to <filename> instead of STDOUT 
    --verbose n        Set verbose level to n, 0<=n<=6 (default 0)    --verbose n        Set verbose level to n, 0<=n<=6 (default 0)
  
Line 212: Line 214:
  
 If the BUFR file(s) are known to consist solely of GTS bulletins, you If the BUFR file(s) are known to consist solely of GTS bulletins, you
-probably should use C<--reuse 1> when applying C<--ahl>, in order to +might consider setting C<--reuse 1> when applying C<--ahl>, in order 
-extract all (and not only the first) BUFR messages in multi message +to extract all (and not only the first) BUFR messages in multi message 
-bulletins (such bulletins are very rare, though). Note that the+bulletins. Such bulletins are very rare nowadayshowever, and see 
 +also the L</"CAVEAT"> for more on this option. Note that the
 corresponding AHL is still extracted (and printed) only once. corresponding AHL is still extracted (and printed) only once.
  
Line 231: Line 234:
  
   next if $bufr->get_data_category() != 1;   next if $bufr->get_data_category() != 1;
 +
 +Or to extract BUFR messages with TM315009 only:
 +
 +  next if bufr->get_descriptors_unexpanded() ne '315009';
  
 =head1 CAVEAT =head1 CAVEAT
  
-bufrextract.pl expects the character sequence CRCRLF (\r\r\n in perl) +Sometimes GTS bulletins are erroneously issued with extra characters 
-between the AHL and the start of the BUFR message. If the file of BUFR +between the GTS AHL and the start of BUFR message (besides the 
-messages has been prepared from collection of GTS bulletins where +standard character sequence CRCRLF), likely leading bufrextract.pl to 
-this control character sequence has been removed, you should replace +miss the AHL. Also, if applying C<--reuse 1>, the BUFR message of such 
-every 'BUFR' in file with '\r\r\nBUFR' before applying bufrextract.pl +GTS bulletin will then be wrongly associated with the AHL of the 
-in order to get the AHLs or filter on AHL.+previous GTS bulletin when filtering on AHL. If bulletins with this 
 +kind of error is more of a concern than multi message bulletins, you 
 +should probably refrain from making use of the C<--reuse 1> option.
  
 =head1 AUTHOR =head1 AUTHOR
Line 247: Line 256:
 =head1 COPYRIGHT =head1 COPYRIGHT
  
-Copyright (C) 2010-2016 MET Norway+Copyright (C) 2010-2019 MET Norway
  
 =cut =cut
 +
 </code> </code>
  • bufr.pm/bufrextract.pl_source.txt
  • Last modified: 2023-02-05 09:07:45
  • by pals