| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # (C) Copyright 2010-2025 MET Norway | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation; either version 2 of the License, or | ||
| 8 | # (at your option) any later version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | # General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with this program; if not, write to the Free Software | ||
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
| 18 | # 02110-1301, USA. | ||
| 19 | |||
| 20 | # pod included at end of file | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use Getopt::Long; | ||
| 25 | use Pod::Usage qw(pod2usage); | ||
| 26 | use Geo::BUFR; | ||
| 27 | |||
| 28 | # Parse command line options | ||
| 29 | my %option = (); | ||
| 30 | GetOptions( | ||
| 31 | \%option, | ||
| 32 | 'ahl=s', # Extract BUFR messages with AHL matching <ahl_regexp> only | ||
| 33 | 'gts', # Include full gts message envelope if present | ||
| 34 | 'help', # Print help information and exit | ||
| 35 | 'only_ahl', # Extract AHLs only | ||
| 36 | 'outfile=s', # Print to file instead of STDOUT | ||
| 37 | 'verbose=i', # Set verbose level to n, 0<=n<=6 (default 0) | ||
| 38 | 'without_ahl', # Print the BUFR messages only, skipping AHLs | ||
| 39 | ) or pod2usage(-verbose => 0); | ||
| 40 | |||
| 41 | # User asked for help | ||
| 42 | pod2usage(-verbose => 1) if $option{help}; | ||
| 43 | |||
| 44 | # only_ahl and without_ahl are mutually exclusive | ||
| 45 | pod2usage( -message => "Options only_ahl, without_ahl and gts are mutually exclusive", | ||
| 46 | -exitval => 2, | ||
| 47 | -verbose => 0) | ||
| 48 | if ( ($option{only_ahl} && ($option{without_ahl} || $option{gts})) | ||
| 49 | || ($option{without_ahl} && ($option{only_ahl} || $option{gts})) | ||
| 50 | || ($option{gts} && ($option{only_ahl} || $option{without_ahl})) ); | ||
| 51 | |||
| 52 | # Make sure there is at least one input file | ||
| 53 | pod2usage(-verbose => 0) unless @ARGV; | ||
| 54 | |||
| 55 | # Set verbosity level | ||
| 56 | Geo::BUFR->set_verbose($option{verbose}) if $option{verbose}; | ||
| 57 | |||
| 58 | my $ahl_regexp; | ||
| 59 | if ($option{ahl}) { | ||
| 60 | eval { $ahl_regexp = qr/$option{ahl}/ }; | ||
| 61 | die "Argument to --ahl is not a valid Perl regular expression: $@" if $@; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Where to direct output (including verbose output, but not output to STDERR) | ||
| 65 | my $OUT; | ||
| 66 | if ($option{outfile}) { | ||
| 67 | open($OUT, '>', $option{outfile}) | ||
| 68 | or die "Cannot open $option{outfile} for writing: $!"; | ||
| 69 | } else { | ||
| 70 | $OUT = *STDOUT; | ||
| 71 | } | ||
| 72 | binmode($OUT); | ||
| 73 | |||
| 74 | # No need to decode section 4 here | ||
| 75 | Geo::BUFR->set_nodata(1); | ||
| 76 | |||
| 77 | # Loop for processing of BUFR input files | ||
| 78 | foreach my $inputfname ( @ARGV ) { | ||
| 79 | my $bufr = Geo::BUFR->new(); | ||
| 80 | $bufr->set_filter_cb(\&filter_on_ahl,$ahl_regexp) if $option{ahl}; | ||
| 81 | |||
| 82 | # Open BUFR file | ||
| 83 | $bufr->fopen($inputfname); | ||
| 84 | |||
| 85 | # Process input file | ||
| 86 | extract($bufr); | ||
| 87 | $bufr->fclose(); | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | # Extract BUFR messages and/or AHLs from BUFR file | ||
| 92 | sub extract { | ||
| 93 | my $bufr = shift; # BUFR object | ||
| 94 | |||
| 95 | my ($current_message_number, $current_ahl); | ||
| 96 | READLOOP: | ||
| 97 | while (not $bufr->eof()) { | ||
| 98 | |||
| 99 | # Read next observation. If an error is encountered during | ||
| 100 | # decoding, skip this observation while printing the error | ||
| 101 | # message to STDERR, also displaying ahl of bulletin if found | ||
| 102 | # (but skip error message if the message should be skipped on | ||
| 103 | # --ahl anyway). | ||
| 104 | eval { | ||
| 105 | $bufr->next_observation(); | ||
| 106 | }; | ||
| 107 | if ($@) { | ||
| 108 | $current_ahl = $bufr->get_current_ahl() || ''; | ||
| 109 | next READLOOP if $option{ahl} && $current_ahl !~ $ahl_regexp; | ||
| 110 | |||
| 111 | warn $@; | ||
| 112 | # Try to extract message number and ahl of the bulletin | ||
| 113 | # where the error occurred | ||
| 114 | $current_message_number = $bufr->get_current_message_number(); | ||
| 115 | if (defined $current_message_number) { | ||
| 116 | my $error_msg = "In message $current_message_number"; | ||
| 117 | $error_msg .= " contained in bulletin with ahl $current_ahl\n" | ||
| 118 | if $current_ahl; | ||
| 119 | warn $error_msg if $error_msg; | ||
| 120 | } | ||
| 121 | next READLOOP; | ||
| 122 | } | ||
| 123 | |||
| 124 | next READLOOP if $option{ahl} && $bufr->is_filtered(); | ||
| 125 | # Skip messages where stated length of BUFR message is sure to | ||
| 126 | # be erroneous, unless we want ahls only (or should we skip | ||
| 127 | # message in this case also? Hard choice...) | ||
| 128 | next READLOOP if !$option{only_ahl} && $bufr->bad_bufrlength(); | ||
| 129 | |||
| 130 | my $current_subset_number = $bufr->get_current_subset_number(); | ||
| 131 | # If next_observation() did find a BUFR message, subset number | ||
| 132 | # should have been set to at least 1 (even in a 0 subset message) | ||
| 133 | last READLOOP if $current_subset_number == 0; | ||
| 134 | |||
| 135 | $current_message_number = $bufr->get_current_message_number(); | ||
| 136 | $current_ahl = $bufr->get_current_ahl() || ''; | ||
| 137 | my $gts_eom = ''; | ||
| 138 | |||
| 139 | if ($current_ahl) { | ||
| 140 | if ($option{only_ahl}) { | ||
| 141 | print $OUT $current_ahl, "\n"; | ||
| 142 | } elsif (!$option{without_ahl}) { | ||
| 143 | if ($option{gts}) { | ||
| 144 | my $current_gts_starting_line = $bufr->get_current_gts_starting_line() || ''; | ||
| 145 | print $OUT $current_gts_starting_line; | ||
| 146 | $gts_eom = $bufr->get_current_gts_eom() || ''; | ||
| 147 | } | ||
| 148 | # Use \r\r\n after AHL, since this is the standard | ||
| 149 | # sequence used in GTS bulletins | ||
| 150 | print $OUT $current_ahl . "\r\r\n"; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | next READLOOP if $option{only_ahl}; | ||
| 154 | |||
| 155 | my $msg = $bufr->get_bufr_message(); | ||
| 156 | print $OUT $msg, $gts_eom; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # Filter routines | ||
| 161 | |||
| 162 | sub filter_on_ahl { | ||
| 163 | my $bufr = shift; | ||
| 164 | my $ahl_regexp = shift; | ||
| 165 | my $ahl = $bufr->get_current_ahl() || ''; | ||
| 166 | return $ahl =~ $ahl_regexp ? 0 : 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | =pod | ||
| 171 | |||
| 172 | =encoding utf8 | ||
| 173 | |||
| 174 | =head1 SYNOPSIS | ||
| 175 | |||
| 176 | bufrextract.pl <bufr file(s)> | ||
| 177 | [--ahl <ahl_regexp>] | ||
| 178 | [--only_ahl] | [--without_ahl] | [--gts] | ||
| 179 | [--outfile <filename>] | ||
| 180 | [--help] | ||
| 181 | [--verbose n] | ||
| 182 | |||
| 183 | =head1 DESCRIPTION | ||
| 184 | |||
| 185 | Extract all BUFR messages and/or corresponding AHLs from BUFR file(s), | ||
| 186 | possibly filtering on AHL. | ||
| 187 | |||
| 188 | The AHL (Abbreviated Header Line) is recognized as the TTAAii CCCC | ||
| 189 | YYGGgg [BBB] immediately preceding the BUFR message. | ||
| 190 | |||
| 191 | Execute without arguments for Usage, with option C<--help> for some | ||
| 192 | additional info. See also L<https://wiki.met.no/bufr.pm/start> for | ||
| 193 | examples of use. | ||
| 194 | |||
| 195 | |||
| 196 | =head1 OPTIONS | ||
| 197 | |||
| 198 | --ahl <ahl_regexp> Extract BUFR messages and/or AHLs with AHL | ||
| 199 | matching <ahl_regexp> only | ||
| 200 | --gts Include full gts message envelope if present | ||
| 201 | --only_ahl Extract AHLs only | ||
| 202 | --without_ahl Extract BUFR messages only | ||
| 203 | --outfile <filename> | ||
| 204 | Will print to <filename> instead of STDOUT | ||
| 205 | --help Display Usage and explain the options used. For even | ||
| 206 | more info you might prefer to consult perldoc bufrextract.pl | ||
| 207 | --verbose n Set verbose level to n, 0<=n<=6 (default 0) | ||
| 208 | |||
| 209 | Options may be abbreviated, e.g. C<--h> or C<-h> for C<--help>. | ||
| 210 | |||
| 211 | For option C<--ahl> the <ahl_regexp> should be a Perl regular | ||
| 212 | expression. E.g. C<--ahl 'ISS... ENMI'> will decode only BUFR SHIP | ||
| 213 | (ISS) from CCCC=ENMI. | ||
| 214 | |||
| 215 | Use option C<--gts> if you want the full GTS message envelope (if | ||
| 216 | present) to be included in output. There are 2 main variations on this | ||
| 217 | envelope (SOH/ETX and ZCZC notation), for details see the Manual on | ||
| 218 | the GTS: Attachment II-4. Format of Meteorological Messages. | ||
| 219 | |||
| 220 | No bufrtables are needed for running bufrextract.pl, since section 4 | ||
| 221 | in BUFR message will not be decoded (which also speeds up execution | ||
| 222 | quite a bit). | ||
| 223 | |||
| 224 | =head1 HINTS | ||
| 225 | |||
| 226 | With a little knowledge of Perl you could easily extend bufrextract.pl | ||
| 227 | to extract BUFR messages based on whatever information is available in | ||
| 228 | section 0-3, by making your own copy of bufrextract.pl and then | ||
| 229 | employing one of the many C<get_> subroutines in BUFR.pm. For example, | ||
| 230 | to extract only BUFR messages with data category 1, add the following | ||
| 231 | line just before calling C<is_filtered()> in code: | ||
| 232 | |||
| 233 | next if $bufr->get_data_category() != 1; | ||
| 234 | |||
| 235 | Or to extract BUFR messages with TM315009 only: | ||
| 236 | |||
| 237 | next if $bufr->get_descriptors_unexpanded() ne '315009'; | ||
| 238 | |||
| 239 | =head1 CAVEAT | ||
| 240 | |||
| 241 | Sometimes GTS bulletins are erroneously issued with extra characters | ||
| 242 | between the GTS AHL and the start of BUFR message (besides the | ||
| 243 | standard character sequence CRCRLF), likely leading bufrextract.pl to | ||
| 244 | miss the AHL. | ||
| 245 | |||
| 246 | =head1 AUTHOR | ||
| 247 | |||
| 248 | Pål Sannes E<lt>pal.sannes@met.noE<gt> | ||
| 249 | |||
| 250 | =head1 COPYRIGHT | ||
| 251 | |||
| 252 | Copyright (C) 2010-2025 MET Norway | ||
| 253 | |||
| 254 | =cut |