| 1 | #!/usr/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 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use Pod::Usage qw(pod2usage); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use Geo::BUFR; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # This is actually default in BUFR.pm, but provided here to make it | ||||||||||||||||||||||||||||||||
| 29 | # easier for users to change to 'ECCODES' if preferred | ||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | use constant DEFAULT_TABLE_FORMAT => 'BUFRDC'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | # Will be used if neither --tablepath nor $ENV{BUFR_TABLES} is set | ||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use constant DEFAULT_TABLE_PATH_BUFRDC => '/usr/local/lib/bufrtables'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use constant DEFAULT_TABLE_PATH_ECCODES => '/usr/local/share/eccodes/definitions/bufr/tables'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # Parse command line options | ||||||||||||||||||||||||||||||||
| 37 | 3 | our %option = (); | |||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | GetOptions( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | \%option, | ||||||||||||||||||||||||||||||||
| 40 | 'bufr_edition=i', | ||||||||||||||||||||||||||||||||
| 41 | 'category=i', | ||||||||||||||||||||||||||||||||
| 42 | 'centre=i', | ||||||||||||||||||||||||||||||||
| 43 | 'compress=i', | ||||||||||||||||||||||||||||||||
| 44 | 'data=s%', | ||||||||||||||||||||||||||||||||
| 45 | 'day=i', | ||||||||||||||||||||||||||||||||
| 46 | 'help', | ||||||||||||||||||||||||||||||||
| 47 | 'hour=i', | ||||||||||||||||||||||||||||||||
| 48 | 'int_subcategory=i', | ||||||||||||||||||||||||||||||||
| 49 | 'loc_subcategory=i', | ||||||||||||||||||||||||||||||||
| 50 | 'local_table_version=i', | ||||||||||||||||||||||||||||||||
| 51 | 'master_table_version=i', | ||||||||||||||||||||||||||||||||
| 52 | 'minute=i', | ||||||||||||||||||||||||||||||||
| 53 | 'month=i', | ||||||||||||||||||||||||||||||||
| 54 | 'observed=i', | ||||||||||||||||||||||||||||||||
| 55 | 'outfile=s', | ||||||||||||||||||||||||||||||||
| 56 | 'remove_qc', | ||||||||||||||||||||||||||||||||
| 57 | 'remove_sec2', | ||||||||||||||||||||||||||||||||
| 58 | 'second=i', | ||||||||||||||||||||||||||||||||
| 59 | 'strict_checking=i', | ||||||||||||||||||||||||||||||||
| 60 | 'subcategory=i', | ||||||||||||||||||||||||||||||||
| 61 | 'subcentre=i', | ||||||||||||||||||||||||||||||||
| 62 | 'tableformat=s', | ||||||||||||||||||||||||||||||||
| 63 | 'tablepath=s', | ||||||||||||||||||||||||||||||||
| 64 | 'update_number=i', | ||||||||||||||||||||||||||||||||
| 65 | 'verbose=i', | ||||||||||||||||||||||||||||||||
| 66 | 'year=i', | ||||||||||||||||||||||||||||||||
| 67 | 'year_of_century=i', | ||||||||||||||||||||||||||||||||
| 68 | ) or pod2usage(-verbose => 0); | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | # User asked for help | ||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | pod2usage(-verbose => 1) if $option{help}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | # Make sure there is an input file | ||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | pod2usage(-verbose => 0) unless @ARGV == 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 3 | my $infile = $ARGV[0]; | |||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | open(my $IN, '<',$infile) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | or die "Cannot open $infile: $!"; | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | # Default is to ignore 'recoverable' errors found in decoded or | ||||||||||||||||||||||||||||||||
| 81 | # encoded BUFR format. This can be changed by setting strict_checking, | ||||||||||||||||||||||||||||||||
| 82 | # which will then apply both to decoding and encoding. | ||||||||||||||||||||||||||||||||
| 83 | my $strict_checking = defined $option{strict_checking} | ||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | ? $option{strict_checking} : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 3 | Geo::BUFR->set_strict_checking($strict_checking); | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | # Set verbosity level | ||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | Geo::BUFR->set_verbose($option{verbose}) if $option{verbose}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | # Set BUFR table format | ||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | my $tableformat = (defined $option{tableformat}) ? uc $option{tableformat} : DEFAULT_TABLE_FORMAT; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 3 | Geo::BUFR->set_tableformat($tableformat); | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | # Set BUFR table path | ||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | if ($option{tablepath}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 96 | # Command line option --tablepath overrides all | ||||||||||||||||||||||||||||||||
| 97 | 3 | Geo::BUFR->set_tablepath($option{tablepath}); | |||||||||||||||||||||||||||||||
| 98 | } elsif ($ENV{BUFR_TABLES}) { | ||||||||||||||||||||||||||||||||
| 99 | # If no --tablepath option, use the BUFR_TABLES environment variable | ||||||||||||||||||||||||||||||||
| 100 | 0 | Geo::BUFR->set_tablepath($ENV{BUFR_TABLES}); | |||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||
| 102 | # If all else fails, use the default tablepath in BUFRDC/ECCODES | ||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($tableformat eq 'BUFRDC') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 104 | 0 | Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_BUFRDC); | |||||||||||||||||||||||||||||||
| 105 | } elsif ($tableformat eq 'ECCODES') { | ||||||||||||||||||||||||||||||||
| 106 | 0 | Geo::BUFR->set_tablepath(DEFAULT_TABLE_PATH_ECCODES); | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # Where to print the altered BUFR message(s) | ||||||||||||||||||||||||||||||||
| 111 | 3 | my $OUT; | |||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | if ($option{outfile}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | open($OUT, '>', $option{outfile}) | ||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | or die "Cannot open $option{outfile} for writing: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||
| 116 | 0 | $OUT = *STDOUT; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | 3 | binmode($OUT); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # Change input separator to 'BUFR' | ||||||||||||||||||||||||||||||||
| 121 | 3 | my $oldeol = $/; | |||||||||||||||||||||||||||||||
| 122 | 3 | $/ = 'BUFR'; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # Read in everything before first 'BUFR' | ||||||||||||||||||||||||||||||||
| 125 | 3 | my $out = <$IN>; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | while (my $msg = <$IN>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | # Leave input unaltered if 'BUFR' is not start of a BUFR message | ||||||||||||||||||||||||||||||||
| 129 | 3 | ▶ | if (length($msg) < 4) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 0 | $out .= $msg; | |||||||||||||||||||||||||||||||
| 131 | 0 | next; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | 3 | my $len = unpack 'N', "\0$msg"; | |||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | if ($len < 8 || $len > length($msg) + 4) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$len < 8 or $len > length($msg) + 4
MC/DC0%0/2
$len < 8 or $len > length($msg) + 4
$len < 8
$len > length($msg) + 4
Truth table33%1/3
A or B
A= $len < 8B= $len > length($msg) + 4
| |||||||||||||||||||||||||||||||||
| 135 | 0 | $out .= $msg; | |||||||||||||||||||||||||||||||
| 136 | 0 | next; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 3 | ▶ | if (substr($msg,$len-8,4) != '7777') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 0 | $out .= $msg; | |||||||||||||||||||||||||||||||
| 140 | 0 | next; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | # 'BUFR' is quite probably start of a valid BUFR message, so | ||||||||||||||||||||||||||||||||
| 144 | # transfer 'BUFR' from $out to $msg, transfer text following BUFR | ||||||||||||||||||||||||||||||||
| 145 | # message from $msg to $out, and try to alter $msg. Input | ||||||||||||||||||||||||||||||||
| 146 | # separator must be reverted before calling Geo::BUFR routines | ||||||||||||||||||||||||||||||||
| 147 | 3 | chomp $out; | |||||||||||||||||||||||||||||||
| 148 | 3 | my $rest = substr($msg,$len-4); | |||||||||||||||||||||||||||||||
| 149 | 3 | $msg = 'BUFR' . substr($msg,0,$len-4); | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 3 | $/ = $oldeol; | |||||||||||||||||||||||||||||||
| 152 | 3 | my $bufr = Geo::BUFR->new($msg); | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 3 | $out .= alter($bufr); | |||||||||||||||||||||||||||||||
| 155 | 3 | $out .= $rest; | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 3 | $bufr->fclose(); | |||||||||||||||||||||||||||||||
| 158 | 3 | $/ = 'BUFR'; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 3 | ▶ | print $OUT $out if $out; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | # Extract data from BUFR file, possibly alter the data, and write the | ||||||||||||||||||||||||||||||||
| 165 | # new messages to STDOUT. | ||||||||||||||||||||||||||||||||
| 166 | sub alter { | ||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | my $bufr = shift; # BUFR object | ||||||||||||||||||||||||||||||
|
Subroutine
alter: called CC=15 SCAR=27.8
| |||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | if ($option{remove_qc}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 0 | Geo::BUFR->set_noqc(); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 3 | my $new_bufr = Geo::BUFR->new(); | |||||||||||||||||||||||||||||||
| 174 | 3 | my @subset_data; # Will contain data values for subset 1,2... | |||||||||||||||||||||||||||||||
| 175 | my @subset_desc; # Will contain the set of descriptors for subset 1,2... | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | READLOOP: | ||||||||||||||||||||||||||||||||
| 178 | 3 | ▶ | while (not $bufr->eof()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | # Read (and decode) next observation | ||||||||||||||||||||||||||||||||
| 181 | 5 | my ($data, $descriptors) = $bufr->next_observation(); | |||||||||||||||||||||||||||||||
| 182 | 5 | my $isub = $bufr->get_current_subset_number(); | |||||||||||||||||||||||||||||||
| 183 | 5 | my $nsubsets = $bufr->get_number_of_subsets(); | |||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 5 | ▶ | if ($isub == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | 3 | $new_bufr->copy_from($bufr,'metadata'); | |||||||||||||||||||||||||||||||
| 187 | 3 | @subset_data = (); | |||||||||||||||||||||||||||||||
| 188 | 3 | @subset_desc = (); | |||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 3 | set_section1_data($bufr, $new_bufr); | |||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 3 | ▶ | if (defined $option{observed}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 1 | $new_bufr->set_observed_data($option{observed}); | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | 3 | ▶ | if (defined $option{compress}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 1 | $new_bufr->set_compressed_data($option{compress}); | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | if ($option{remove_sec2}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 0 | $new_bufr->set_optional_section(0); | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | 3 | ▶ | if ($option{remove_qc}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 0 | remove_qc_from_unexpanded($new_bufr); | |||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 5 | ▶ | if (defined $option{data}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 5 | ▶ | DESCRIPTOR: while (my ($desc, $value) = each %{$option{data}}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 208 | 10 | ▶ | for (my $i=0; $i < @$descriptors; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 115 | ▶ | if ($descriptors->[$i] == $desc) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 10 | ▶ | if ($value =~ /(.*)\+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 211 | 0 | $data->[$i] += $1; | |||||||||||||||||||||||||||||||
| 212 | } elsif ($value eq 'missing') { | ||||||||||||||||||||||||||||||||
| 213 | 5 | $data->[$i] = undef; | |||||||||||||||||||||||||||||||
| 214 | } else { | ||||||||||||||||||||||||||||||||
| 215 | 5 | $data->[$i] = $value; | |||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | 10 | next DESCRIPTOR; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | 5 | $subset_data[$isub] = $data; | |||||||||||||||||||||||||||||||
| 223 | 5 | $subset_desc[$isub] = $descriptors; | |||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 5 | ▶ | if ($isub == $nsubsets) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 3 | return $new_bufr->encode_message(\@subset_data, \@subset_desc); | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | sub set_section1_data { | ||||||||||||||||||||||||||||||||
| 232 | 3 | ▶ | my ($bufr, $new_bufr) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
set_section1_data: called CC=21 SCAR=34.1
| |||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 3 | ▶ | if (defined $option{centre}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 235 | 3 | $new_bufr->set_centre($option{centre}); | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | if (defined $option{subcentre}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 3 | $new_bufr->set_subcentre($option{subcentre}); | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | 3 | ▶ | if (defined $option{update_number}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | 3 | ▶ | if ($option{update_number} >= 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | 1 | $new_bufr->set_update_sequence_number($option{update_number}); | |||||||||||||||||||||||||||||||
| 243 | } else { | ||||||||||||||||||||||||||||||||
| 244 | 2 | my $old_number = $bufr->get_update_sequence_number(); | |||||||||||||||||||||||||||||||
| 245 | 2 | my $update_number = $option{update_number}; | |||||||||||||||||||||||||||||||
| 246 | 2 | ▶ | if ($option{update_number} == -1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 247 | 2 | $new_bufr->set_update_sequence_number($old_number + 1); | |||||||||||||||||||||||||||||||
| 248 | } elsif ($option{update_number} == -2) { | ||||||||||||||||||||||||||||||||
| 249 | 0 | $new_bufr->set_update_sequence_number($old_number - 1); | |||||||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||||||
| 251 | 0 | pod2usage(-verbose => 1); | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||
| 255 | 3 | ▶ | if (defined $option{category}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 256 | 3 | $new_bufr->set_data_category($option{category}); | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | 3 | ▶ | if (defined $option{subcategory}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 259 | 3 | $new_bufr->set_data_subcategory($option{subcategory}); | |||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | 3 | ▶ | if (defined $option{int_subcategory}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | 0 | $new_bufr->set_int_data_subcategory($option{int_subcategory}); | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | 3 | ▶ | if (defined $option{loc_subcategory}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | 0 | $new_bufr->set_loc_data_subcategory($option{loc_subcategory}); | |||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||
| 267 | 3 | ▶ | if (defined $option{master_table_version}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 268 | 2 | $new_bufr->set_master_table_version($option{master_table_version}); | |||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||
| 270 | 3 | ▶ | if (defined $option{local_table_version}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 2 | $new_bufr->set_local_table_version($option{local_table_version}); | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | 3 | ▶ | if (defined $option{year}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | 2 | $new_bufr->set_year($option{year}); | |||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | 3 | ▶ | if (defined $option{year_of_century}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 0 | $new_bufr->set_year_of_century($option{year_of_century}); | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | 3 | ▶ | if (defined $option{month}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 280 | 3 | $new_bufr->set_month($option{month}); | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | 3 | ▶ | if (defined $option{day}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 283 | 3 | $new_bufr->set_day($option{day}); | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | if (defined $option{hour}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 286 | 3 | $new_bufr->set_hour($option{hour}); | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | 3 | ▶ | if (defined $option{minute}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | 3 | $new_bufr->set_minute($option{minute}); | |||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | 3 | ▶ | if (defined $option{second}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 292 | 0 | $new_bufr->set_second($option{second}); | |||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | # Should be processed last of the change metadata options, | ||||||||||||||||||||||||||||||||
| 295 | # because setting of BUFR edition may depend on other | ||||||||||||||||||||||||||||||||
| 296 | # metadata which user has opted to set | ||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | if (defined $option{bufr_edition}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 298 | 3 | set_bufr_edition($option{bufr_edition}, $bufr, $new_bufr); | |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | 3 | return; | |||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | sub remove_qc_from_unexpanded { | ||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | my $bufr = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
remove_qc_from_unexpanded: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 305 | 0 | my $desc = $bufr->get_descriptors_unexpanded(); | |||||||||||||||||||||||||||||||
| 306 | 0 | $desc =~ s/ 222000.*//; | |||||||||||||||||||||||||||||||
| 307 | 0 | $bufr->set_descriptors_unexpanded($desc); | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | # If user hasn't provided the new metadata required for the new bufr | ||||||||||||||||||||||||||||||||
| 311 | # edition, we make some educated guesses of these new metadata. | ||||||||||||||||||||||||||||||||
| 312 | sub set_bufr_edition { | ||||||||||||||||||||||||||||||||
| 313 | 3 | ▶ | my ($new_bufr_edition, $bufr, $new_bufr) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
set_bufr_edition: called CC=10 SCAR=25.7
| |||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 3 | my $old_bufr_edition = $bufr->get_bufr_edition(); | |||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 3 | ▶ | if ($old_bufr_edition == 4 and $new_bufr_edition < 4) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$old_bufr_edition == 4 and $new_bufr_edition < 4
MC/DC50%1/2
$old_bufr_edition == 4 and $new_bufr_edition < 4
$old_bufr_edition == 4
$new_bufr_edition < 4
Truth table66%2/3
A and B
A= $old_bufr_edition == 4B= $new_bufr_edition < 4
| |||||||||||||||||||||||||||||||||
| 318 | 2 | ▶ | if (!defined $new_bufr->get_data_subcategory()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 319 | 0 | $new_bufr->set_data_subcategory($bufr->get_loc_data_subcategory()); | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | # get_year_of_century() fetches from YEAR if YEAR_OF_CENTURY isn't set | ||||||||||||||||||||||||||||||||
| 322 | 2 | $new_bufr->set_year_of_century($new_bufr->get_year_of_century()); | |||||||||||||||||||||||||||||||
| 323 | ▶ | } elsif ($old_bufr_edition < 4 and $new_bufr_edition == 4) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$old_bufr_edition < 4 and $new_bufr_edition == 4
MC/DC0%0/2
$old_bufr_edition < 4 and $new_bufr_edition == 4
$old_bufr_edition < 4
$new_bufr_edition == 4
Truth table33%1/3
A and B
A= $old_bufr_edition < 4B= $new_bufr_edition == 4
| |||||||||||||||||||||||||||||||||
| 324 | 1 | ▶ | if (!defined $new_bufr->get_loc_data_subcategory()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | 1 | $new_bufr->set_loc_data_subcategory($bufr->get_data_subcategory()); | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | 1 | ▶ | if (!defined $new_bufr->get_int_data_subcategory()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 328 | 1 | $new_bufr->set_int_data_subcategory(255); # Undefined value | |||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | 1 | ▶ | if (!defined $new_bufr->get_year()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | # Should work most of the time | ||||||||||||||||||||||||||||||||
| 332 | 0 | $new_bufr->set_year($bufr->get_year_of_century() + 2000); | |||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | 1 | ▶ | if (!defined $new_bufr->get_second()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 335 | 1 | $new_bufr->set_second(0); | |||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 3 | $new_bufr->set_bufr_edition($new_bufr_edition); | |||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | =pod | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | bufralter.pl <bufr file> | ||||||||||||||||||||||||||||||||
| 349 | [--data <descriptor=value[+]>] | ||||||||||||||||||||||||||||||||
| 350 | [--bufr_edition <value>] | ||||||||||||||||||||||||||||||||
| 351 | [--centre <value>] | ||||||||||||||||||||||||||||||||
| 352 | [--subcentre <value>] | ||||||||||||||||||||||||||||||||
| 353 | [--update_number <value>] | ||||||||||||||||||||||||||||||||
| 354 | [--category <value>] | ||||||||||||||||||||||||||||||||
| 355 | [--subcategory <value>] | ||||||||||||||||||||||||||||||||
| 356 | [--int_subcategory <value>] | ||||||||||||||||||||||||||||||||
| 357 | [--loc_subcategory <value>] | ||||||||||||||||||||||||||||||||
| 358 | [--master_table_version <value>] | ||||||||||||||||||||||||||||||||
| 359 | [--local_table_version <value>] | ||||||||||||||||||||||||||||||||
| 360 | [--year <value>] | ||||||||||||||||||||||||||||||||
| 361 | [--year_of_century <value>] | ||||||||||||||||||||||||||||||||
| 362 | [--month <value>] | ||||||||||||||||||||||||||||||||
| 363 | [--day <value>] | ||||||||||||||||||||||||||||||||
| 364 | [--hour <value>] | ||||||||||||||||||||||||||||||||
| 365 | [--minute <value>] | ||||||||||||||||||||||||||||||||
| 366 | [--second <value>] | ||||||||||||||||||||||||||||||||
| 367 | [--observed 0|1] | ||||||||||||||||||||||||||||||||
| 368 | [--compress 0|1] | ||||||||||||||||||||||||||||||||
| 369 | [--remove_sec2] | ||||||||||||||||||||||||||||||||
| 370 | [--remove_qc] | ||||||||||||||||||||||||||||||||
| 371 | [--outfile <file>] | ||||||||||||||||||||||||||||||||
| 372 | [--strict_checking n] | ||||||||||||||||||||||||||||||||
| 373 | [--tableformat <BUFRDC|ECCODES>] | ||||||||||||||||||||||||||||||||
| 374 | [--tablepath <path to BUFR tables>] | ||||||||||||||||||||||||||||||||
| 375 | [--verbose n] | ||||||||||||||||||||||||||||||||
| 376 | [--help] | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | Will alter the BUFR messages in <bufr file> according to what is | ||||||||||||||||||||||||||||||||
| 381 | specified by the options provided. The modified file (text surrounding | ||||||||||||||||||||||||||||||||
| 382 | the BUFR messages will not be affected) will be printed to STDOUT | ||||||||||||||||||||||||||||||||
| 383 | (unless C<--outfile> is set). | ||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | Execute without arguments for Usage, with option C<--help> for some | ||||||||||||||||||||||||||||||||
| 386 | additional info. | ||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | --data <descriptor=value[+]> Set (first) data value in section 4 for | ||||||||||||||||||||||||||||||||
| 391 | descriptor. A trailing '+' means that the value | ||||||||||||||||||||||||||||||||
| 392 | should be added to existing value. Use 'missing' | ||||||||||||||||||||||||||||||||
| 393 | to set a missing value. Repeat the option if more | ||||||||||||||||||||||||||||||||
| 394 | sequence descriptors are to be set. Example: | ||||||||||||||||||||||||||||||||
| 395 | --data 004004=-1+ --data 004005=50 --data | ||||||||||||||||||||||||||||||||
| 396 | 012101=missing This will set the data value for | ||||||||||||||||||||||||||||||||
| 397 | first (and only first!) occurrence of these 3 | ||||||||||||||||||||||||||||||||
| 398 | descriptors in every subset and every message in | ||||||||||||||||||||||||||||||||
| 399 | <bufr file> to the given value (subtracting 1 from | ||||||||||||||||||||||||||||||||
| 400 | the existing value for 004004) | ||||||||||||||||||||||||||||||||
| 401 | --bufr_edition <value> Set BUFR edition to <value>. If the new edition | ||||||||||||||||||||||||||||||||
| 402 | involves some metadata not present in the old edition, | ||||||||||||||||||||||||||||||||
| 403 | some educated guesses for these new metadata are made, | ||||||||||||||||||||||||||||||||
| 404 | but you should also consider setting these new metadata | ||||||||||||||||||||||||||||||||
| 405 | explicitely | ||||||||||||||||||||||||||||||||
| 406 | --centre <value> Set originating centre to <value> | ||||||||||||||||||||||||||||||||
| 407 | --subcentre <value> | ||||||||||||||||||||||||||||||||
| 408 | Set originating subcentre to <value> | ||||||||||||||||||||||||||||||||
| 409 | --update_number <value> | ||||||||||||||||||||||||||||||||
| 410 | Set update sequence number to <value>. Use the special | ||||||||||||||||||||||||||||||||
| 411 | value -1 to increment existing update sequence number, | ||||||||||||||||||||||||||||||||
| 412 | -2 to decrement it | ||||||||||||||||||||||||||||||||
| 413 | --category <value> Set data category to <value> | ||||||||||||||||||||||||||||||||
| 414 | --subcategory <value> Set data sub-category to <value> | ||||||||||||||||||||||||||||||||
| 415 | --int_subcategory <value> Set international data sub-category to <value> | ||||||||||||||||||||||||||||||||
| 416 | --loc_subcategory <value> Set local data sub-category to <value> | ||||||||||||||||||||||||||||||||
| 417 | --master_table_version <value> | ||||||||||||||||||||||||||||||||
| 418 | Set master table version number to <value> | ||||||||||||||||||||||||||||||||
| 419 | --local_table_version <value> | ||||||||||||||||||||||||||||||||
| 420 | Set local table version number to <value> | ||||||||||||||||||||||||||||||||
| 421 | --<time_var> <value> Set <time_var> (= year | year_of_century | month | | ||||||||||||||||||||||||||||||||
| 422 | day | hour | minute | second) in section 1 to <value> | ||||||||||||||||||||||||||||||||
| 423 | --observed 0|1 Set observed data in section 3 to 0 or 1 | ||||||||||||||||||||||||||||||||
| 424 | --compress 0|1 Set compression in section 3 to 0 or 1 | ||||||||||||||||||||||||||||||||
| 425 | --remove_sec2 Remove optional section 2 if present | ||||||||||||||||||||||||||||||||
| 426 | --remove_qc Remove all quality control information, | ||||||||||||||||||||||||||||||||
| 427 | i.e. remove all descriptors from 222000 on | ||||||||||||||||||||||||||||||||
| 428 | --outfile <filename> | ||||||||||||||||||||||||||||||||
| 429 | Will print to <filename> instead of STDOUT | ||||||||||||||||||||||||||||||||
| 430 | --strict_checking n n=0 (default) Disable strict checking of BUFR format | ||||||||||||||||||||||||||||||||
| 431 | n=1 Issue warning if (recoverable) error in | ||||||||||||||||||||||||||||||||
| 432 | BUFR format | ||||||||||||||||||||||||||||||||
| 433 | n=2 Croak if (recoverable) error in BUFR format. | ||||||||||||||||||||||||||||||||
| 434 | Nothing more in this message will be | ||||||||||||||||||||||||||||||||
| 435 | decoded/encoded. | ||||||||||||||||||||||||||||||||
| 436 | --tableformat Currently supported are BUFRDC and ECCODES (default is BUFRDC) | ||||||||||||||||||||||||||||||||
| 437 | --tablepath <path to BUFR tables> | ||||||||||||||||||||||||||||||||
| 438 | Set path to BUFR tables (overrides $ENV{BUFR_TABLES}) | ||||||||||||||||||||||||||||||||
| 439 | --verbose n Set verbose level to n, 0<=n<=6 (default 0). Verbose | ||||||||||||||||||||||||||||||||
| 440 | output is sent to STDOUT, so ought to be combined with | ||||||||||||||||||||||||||||||||
| 441 | option --outfile | ||||||||||||||||||||||||||||||||
| 442 | --help Display Usage and explain the options used. Almost | ||||||||||||||||||||||||||||||||
| 443 | the same as consulting perldoc bufralter.pl | ||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||
| 445 | Options may be abbreviated, e.g. C<--he> or C<-he> for C<--help>. | ||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||
| 447 | To avoid having to use the C<--tablepath> option, you are adviced to | ||||||||||||||||||||||||||||||||
| 448 | set the environment variable BUFR_TABLES to the directory where your | ||||||||||||||||||||||||||||||||
| 449 | BUFR tables are located (unless the default path provided by | ||||||||||||||||||||||||||||||||
| 450 | bufralter.pl works for you). For tableformat ECCODES, se | ||||||||||||||||||||||||||||||||
| 451 | L<http://search.cpan.org/dist/Geo-BUFR/lib/Geo/BUFR.pm#BUFR-TABLE-FILES> | ||||||||||||||||||||||||||||||||
| 452 | for more info on how to set C<--tablepath> (or BUFR_TABLES). | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | Pål Sannes E<lt>pal.sannes@met.noE<gt> | ||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | Copyright (C) 2010-2025 MET Norway | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | =cut | ||||||||||||||||||||||||||||||||