decoder.h
#ifndef WREPORT_BUFR_DECODER_H
#define WREPORT_BUFR_DECODER_H
#include <wreport/var.h>
#include <wreport/bulletin.h>
#include <wreport/bulletin/interpreter.h>
#include <wreport/bufr/input.h>
namespace wreport {
namespace bufr {
struct DispatchToSubsets;
struct Decoder
{
Input in;
/* Output decoded variables */
BufrBulletin& out;
size_t expected_subsets;
bool conf_add_undef_attrs = false;
unsigned optional_section_length = 0;
FILE* verbose_output = nullptr;
Decoder(const std::string& buf, const char* fname, size_t offset, BufrBulletin& out);
void read_options(const BufrCodecOptions& opts);
void decode_sec1ed3();
void decode_sec1ed4();
/* Decode the message header only */
void decode_header();
/* Decode message data section after the header has been decoded */
void decode_data();
};
struct DecoderTarget
{
Input& in;
DecoderTarget(Input& in) : in(in) {}
virtual ~DecoderTarget() {}
virtual const Subset& reference_subset() const = 0;
virtual Varinfo lookup_info(unsigned pos) const = 0;
virtual Var decode_uniform_b_value(Varinfo info) = 0;
virtual const Var& decode_and_add_to_all(Varinfo info) = 0;
virtual const Var& decode_and_add_bitmap(const Tables& tables, Varcode code, unsigned bitmap_size) = 0;
virtual void decode_and_set_attribute(Varinfo info, unsigned pos) = 0;
virtual void decode_and_add_b_value(Varinfo info) = 0;
virtual void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField& field) = 0;
virtual void decode_and_add_raw_character_data(Varinfo info) = 0;
virtual int decode_c03_refval_override(unsigned bits) = 0;
virtual void print_last_variable_added(FILE* out) = 0;
virtual void print_last_attribute_added(FILE* out, Varcode code, unsigned pos) = 0;
};
struct UncompressedDecoderTarget : public DecoderTarget
{
Subset& out;
UncompressedDecoderTarget(Input& in, Subset& out);
const Subset& reference_subset() const override;
Varinfo lookup_info(unsigned pos) const override;
Var decode_uniform_b_value(Varinfo info) override;
const Var& decode_and_add_to_all(Varinfo info) override;
const Var& decode_and_add_bitmap(const Tables& tables, Varcode code, unsigned bitmap_size) override;
void decode_and_set_attribute(Varinfo info, unsigned pos) override;
void decode_and_add_b_value(Varinfo info) override;
void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField& field) override;
void decode_and_add_raw_character_data(Varinfo info) override;
int decode_c03_refval_override(unsigned bits) override;
void print_last_variable_added(FILE* out) override;
void print_last_attribute_added(FILE* out, Varcode code, unsigned pos) override;
};
struct CompressedDecoderTarget : public DecoderTarget
{
Bulletin& out;
unsigned subset_count;
CompressedDecoderTarget(Input& in, Bulletin& out);
const Subset& reference_subset() const override;
Varinfo lookup_info(unsigned pos) const override;
Var decode_uniform_b_value(Varinfo info) override;
const Var& decode_and_add_to_all(Varinfo info) override;
const Var& decode_and_add_bitmap(const Tables& tables, Varcode code, unsigned bitmap_size) override;
void decode_and_set_attribute(Varinfo info, unsigned pos) override;
void decode_and_add_b_value(Varinfo info) override;
void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField& field) override;
void decode_and_add_raw_character_data(Varinfo info) override;
int decode_c03_refval_override(unsigned bits) override;
void print_last_variable_added(FILE* out) override;
void print_last_attribute_added(FILE* out, Varcode code, unsigned pos) override;
protected:
void decode_b_value(Varinfo info, std::function<void(unsigned, Var&&)> dest);
};
struct DataSectionDecoder : public bulletin::Interpreter
{
DecoderTarget& target;
DataSectionDecoder(Bulletin& bulletin, DecoderTarget& target);
unsigned define_delayed_replication_factor(Varinfo info) override;
unsigned define_associated_field_significance(Varinfo info) override;
unsigned define_bitmap_delayed_replication_factor(Varinfo info) override;
void define_c03_refval_override(Varcode code) override;
void define_bitmap(unsigned bitmap_size) override;
void define_attribute(Varinfo info, unsigned pos) override;
void define_substituted_value(unsigned pos) override;
void define_variable(Varinfo info) override;
void define_variable_with_associated_field(Varinfo info) override;
void define_raw_character_data(Varcode code) override;
};
struct VerboseDataSectionDecoder : public DataSectionDecoder
{
protected:
void print_lead(Varcode code);
void print_lead_continued();
public:
FILE* out;
unsigned indent = 0;
unsigned indent_step = 2;
VerboseDataSectionDecoder(Bulletin& bulletin, DecoderTarget& target, FILE* out);
void b_variable(Varcode code) override;
void c_modifier(Varcode code, Opcodes& next) override;
void r_replication(Varcode code, Varcode delayed_code, const Opcodes& ops) override;
void run_d_expansion(Varcode code) override;
unsigned define_delayed_replication_factor(Varinfo info) override;
unsigned define_associated_field_significance(Varinfo info) override;
unsigned define_bitmap_delayed_replication_factor(Varinfo info) override;
void define_c03_refval_override(Varcode code) override;
void define_bitmap(unsigned bitmap_size) override;
void define_attribute(Varinfo info, unsigned pos) override;
void define_substituted_value(unsigned pos) override;
void define_variable(Varinfo info) override;
void define_variable_with_associated_field(Varinfo info) override;
void define_raw_character_data(Varcode code) override;
};
}
}
#endif
wreport::BufrBulletin
BUFR bulletin implementation.
Definition bulletin.h:229
wreport::BufrCodecOptions
Options used to configure BUFR decoding.
Definition bulletin.h:200
wreport::Bulletin
Storage for the decoded data of a BUFR or CREX message.
Definition bulletin.h:30
wreport::Subset
Represent a BUFR/CREX data subset as a list of decoded variables.
Definition subset.h:13
wreport::Var
A physical variable.
Definition var.h:25
wreport::bufr::Input
Binary buffer with bit-level read operations.
Definition input.h:46
wreport::bulletin
The bulletin namespace contains bulletin implementation details, internals and utility functions.
Definition input.h:13
wreport
String functions.
Definition benchmark.h:13
wreport::Varinfo
const _Varinfo * Varinfo
Varinfo reference.
Definition fwd.h:11
wreport::Varcode
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
wreport::Opcodes
Sequence of opcodes, as a slice of a Varcode vector.
Definition opcodes.h:20
wreport::Tables
Collection of BUFR/CREX tables used to work on a bulletin.
Definition tables.h:15
wreport::bufr::CompressedDecoderTarget::decode_c03_refval_override
int decode_c03_refval_override(unsigned bits) override
Decode the given number of bits a signed integer, to use as a new value for B table reference value.
wreport::bufr::CompressedDecoderTarget::decode_and_set_attribute
void decode_and_set_attribute(Varinfo info, unsigned pos) override
Decode an attribute with the given description, and add it to data at position pos.
wreport::bufr::CompressedDecoderTarget::decode_and_add_raw_character_data
void decode_and_add_raw_character_data(Varinfo info) override
Decode raw character data described by code and add it to the target subset(s).
wreport::bufr::CompressedDecoderTarget::out
Bulletin & out
Output bulletin.
Definition decoder.h:142
wreport::bufr::CompressedDecoderTarget::decode_and_add_b_value_with_associated_field
void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField &field) override
Decode a B-table value with associated field, and add its value(s) to the target subset(s).
wreport::bufr::CompressedDecoderTarget::decode_and_add_b_value
void decode_and_add_b_value(Varinfo info) override
Decode a B-table value and add its value(s) to the target subset(s).
wreport::bufr::CompressedDecoderTarget::decode_and_add_to_all
const Var & decode_and_add_to_all(Varinfo info) override
Decode and add the same value to all datasets, return a reference to one of the variables added.
wreport::bufr::CompressedDecoderTarget::print_last_attribute_added
void print_last_attribute_added(FILE *out, Varcode code, unsigned pos) override
Print the value(s) of the last attributes(s) with the given code added to out.
wreport::bufr::CompressedDecoderTarget::reference_subset
const Subset & reference_subset() const override
Return the reference to a subset that is receiving the data currently decoded.
wreport::bufr::CompressedDecoderTarget::lookup_info
Varinfo lookup_info(unsigned pos) const override
Return information about a value previously stored at the given position.
wreport::bufr::CompressedDecoderTarget::subset_count
unsigned subset_count
Number of subsets in data section.
Definition decoder.h:145
wreport::bufr::CompressedDecoderTarget::print_last_variable_added
void print_last_variable_added(FILE *out) override
Print the value(s) of the last variable(s) added to out.
wreport::bufr::CompressedDecoderTarget::decode_uniform_b_value
Var decode_uniform_b_value(Varinfo info) override
Decode a value that must always be the same across all datasets.
wreport::bufr::DataSectionDecoder::define_substituted_value
void define_substituted_value(unsigned pos) override
Request processing of a substituted value corresponding to position pos in the list or previous varia...
wreport::bufr::DataSectionDecoder::define_c03_refval_override
void define_c03_refval_override(Varcode code) override
Request processing of a new value for the reference value of the given B code.
wreport::bufr::DataSectionDecoder::define_variable
void define_variable(Varinfo info) override
Request processing, according to info, of a data variable.
wreport::bufr::DataSectionDecoder::define_attribute
void define_attribute(Varinfo info, unsigned pos) override
Request processing of an attribute encoded with info, related to the variable as position pos in the ...
wreport::bufr::DataSectionDecoder::define_raw_character_data
void define_raw_character_data(Varcode code) override
Request processing of C05yyy raw character data.
wreport::bufr::DataSectionDecoder::define_associated_field_significance
unsigned define_associated_field_significance(Varinfo info) override
Request processing of an associated field significance variable (B31021).
wreport::bufr::DataSectionDecoder::define_delayed_replication_factor
unsigned define_delayed_replication_factor(Varinfo info) override
Request processing, according to info, of a data variabile that is significant for controlling the en...
wreport::bufr::DataSectionDecoder::define_bitmap
void define_bitmap(unsigned bitmap_size) override
Request processing of a data present bitmap.
wreport::bufr::DataSectionDecoder::define_bitmap_delayed_replication_factor
unsigned define_bitmap_delayed_replication_factor(Varinfo info) override
Request processing of a delayed replication factor variable used to encode the size of a bitmap.
wreport::bufr::DataSectionDecoder::define_variable_with_associated_field
void define_variable_with_associated_field(Varinfo info) override
Request processing, according to info, of a data variable.
wreport::bufr::DecoderTarget
Definition decoder.h:43
wreport::bufr::DecoderTarget::decode_c03_refval_override
virtual int decode_c03_refval_override(unsigned bits)=0
Decode the given number of bits a signed integer, to use as a new value for B table reference value.
wreport::bufr::DecoderTarget::decode_and_add_b_value
virtual void decode_and_add_b_value(Varinfo info)=0
Decode a B-table value and add its value(s) to the target subset(s).
wreport::bufr::DecoderTarget::decode_and_add_raw_character_data
virtual void decode_and_add_raw_character_data(Varinfo info)=0
Decode raw character data described by code and add it to the target subset(s).
wreport::bufr::DecoderTarget::in
Input & in
Input buffer.
Definition decoder.h:45
wreport::bufr::DecoderTarget::decode_and_add_b_value_with_associated_field
virtual void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField &field)=0
Decode a B-table value with associated field, and add its value(s) to the target subset(s).
wreport::bufr::DecoderTarget::lookup_info
virtual Varinfo lookup_info(unsigned pos) const =0
Return information about a value previously stored at the given position.
wreport::bufr::DecoderTarget::decode_and_add_to_all
virtual const Var & decode_and_add_to_all(Varinfo info)=0
Decode and add the same value to all datasets, return a reference to one of the variables added.
wreport::bufr::DecoderTarget::print_last_attribute_added
virtual void print_last_attribute_added(FILE *out, Varcode code, unsigned pos)=0
Print the value(s) of the last attributes(s) with the given code added to out.
wreport::bufr::DecoderTarget::decode_uniform_b_value
virtual Var decode_uniform_b_value(Varinfo info)=0
Decode a value that must always be the same across all datasets.
wreport::bufr::DecoderTarget::print_last_variable_added
virtual void print_last_variable_added(FILE *out)=0
Print the value(s) of the last variable(s) added to out.
wreport::bufr::DecoderTarget::reference_subset
virtual const Subset & reference_subset() const =0
Return the reference to a subset that is receiving the data currently decoded.
wreport::bufr::DecoderTarget::decode_and_set_attribute
virtual void decode_and_set_attribute(Varinfo info, unsigned pos)=0
Decode an attribute with the given description, and add it to data at position pos.
wreport::bufr::Decoder::verbose_output
FILE * verbose_output
If set, be verbose and print a trace of decoding to the given file.
Definition decoder.h:26
wreport::bufr::Decoder::in
Input in
Input data.
Definition decoder.h:16
wreport::bufr::Decoder::conf_add_undef_attrs
bool conf_add_undef_attrs
True if undefined attributes are added to the output, else false.
Definition decoder.h:22
wreport::bufr::Decoder::expected_subsets
size_t expected_subsets
Number of expected subsets (read in decode_header, used in decode_data).
Definition decoder.h:20
wreport::bufr::Decoder::optional_section_length
unsigned optional_section_length
Optional section length decoded from the message.
Definition decoder.h:24
wreport::bufr::DispatchToSubsets
Definition input.h:20
wreport::bufr::UncompressedDecoderTarget::decode_c03_refval_override
int decode_c03_refval_override(unsigned bits) override
Decode the given number of bits a signed integer, to use as a new value for B table reference value.
wreport::bufr::UncompressedDecoderTarget::reference_subset
const Subset & reference_subset() const override
Return the reference to a subset that is receiving the data currently decoded.
wreport::bufr::UncompressedDecoderTarget::out
Subset & out
Subset where decoded variables go.
Definition decoder.h:120
wreport::bufr::UncompressedDecoderTarget::decode_and_add_b_value
void decode_and_add_b_value(Varinfo info) override
Decode a B-table value and add its value(s) to the target subset(s).
wreport::bufr::UncompressedDecoderTarget::lookup_info
Varinfo lookup_info(unsigned pos) const override
Return information about a value previously stored at the given position.
wreport::bufr::UncompressedDecoderTarget::decode_and_add_b_value_with_associated_field
void decode_and_add_b_value_with_associated_field(Varinfo info, const bulletin::AssociatedField &field) override
Decode a B-table value with associated field, and add its value(s) to the target subset(s).
wreport::bufr::UncompressedDecoderTarget::print_last_attribute_added
void print_last_attribute_added(FILE *out, Varcode code, unsigned pos) override
Print the value(s) of the last attributes(s) with the given code added to out.
wreport::bufr::UncompressedDecoderTarget::decode_and_set_attribute
void decode_and_set_attribute(Varinfo info, unsigned pos) override
Decode an attribute with the given description, and add it to data at position pos.
wreport::bufr::UncompressedDecoderTarget::decode_uniform_b_value
Var decode_uniform_b_value(Varinfo info) override
Decode a value that must always be the same across all datasets.
wreport::bufr::UncompressedDecoderTarget::decode_and_add_to_all
const Var & decode_and_add_to_all(Varinfo info) override
Decode and add the same value to all datasets, return a reference to one of the variables added.
wreport::bufr::UncompressedDecoderTarget::decode_and_add_raw_character_data
void decode_and_add_raw_character_data(Varinfo info) override
Decode raw character data described by code and add it to the target subset(s).
wreport::bufr::UncompressedDecoderTarget::print_last_variable_added
void print_last_variable_added(FILE *out) override
Print the value(s) of the last variable(s) added to out.
wreport::bufr::VerboseDataSectionDecoder::define_variable_with_associated_field
void define_variable_with_associated_field(Varinfo info) override
Request processing, according to info, of a data variable.
wreport::bufr::VerboseDataSectionDecoder::indent
unsigned indent
Current indent level.
Definition decoder.h:206
wreport::bufr::VerboseDataSectionDecoder::define_associated_field_significance
unsigned define_associated_field_significance(Varinfo info) override
Request processing of an associated field significance variable (B31021).
wreport::bufr::VerboseDataSectionDecoder::run_d_expansion
void run_d_expansion(Varcode code) override
Executes the expansion of code, which has been put on top of the opcode stack.
wreport::bufr::VerboseDataSectionDecoder::c_modifier
void c_modifier(Varcode code, Opcodes &next) override
Notify of a C modifier.
wreport::bufr::VerboseDataSectionDecoder::define_substituted_value
void define_substituted_value(unsigned pos) override
Request processing of a substituted value corresponding to position pos in the list or previous varia...
wreport::bufr::VerboseDataSectionDecoder::define_c03_refval_override
void define_c03_refval_override(Varcode code) override
Request processing of a new value for the reference value of the given B code.
wreport::bufr::VerboseDataSectionDecoder::define_bitmap
void define_bitmap(unsigned bitmap_size) override
Request processing of a data present bitmap.
wreport::bufr::VerboseDataSectionDecoder::define_bitmap_delayed_replication_factor
unsigned define_bitmap_delayed_replication_factor(Varinfo info) override
Request processing of a delayed replication factor variable used to encode the size of a bitmap.
wreport::bufr::VerboseDataSectionDecoder::r_replication
void r_replication(Varcode code, Varcode delayed_code, const Opcodes &ops) override
Handle a replicated section.
wreport::bufr::VerboseDataSectionDecoder::define_delayed_replication_factor
unsigned define_delayed_replication_factor(Varinfo info) override
Request processing, according to info, of a data variabile that is significant for controlling the en...
wreport::bufr::VerboseDataSectionDecoder::indent_step
unsigned indent_step
How many spaces in an indentation level.
Definition decoder.h:209
wreport::bufr::VerboseDataSectionDecoder::print_lead
void print_lead(Varcode code)
Print line lead (indentation and formatted code).
wreport::bufr::VerboseDataSectionDecoder::b_variable
void b_variable(Varcode code) override
Notify of a B variable entry.
wreport::bufr::VerboseDataSectionDecoder::define_attribute
void define_attribute(Varinfo info, unsigned pos) override
Request processing of an attribute encoded with info, related to the variable as position pos in the ...
wreport::bufr::VerboseDataSectionDecoder::define_raw_character_data
void define_raw_character_data(Varcode code) override
Request processing of C05yyy raw character data.
wreport::bufr::VerboseDataSectionDecoder::define_variable
void define_variable(Varinfo info) override
Request processing, according to info, of a data variable.
wreport::bulletin::AssociatedField
Definition associated_fields.h:13
wreport::bulletin::Interpreter
Interpreter for data descriptor sections.
Definition interpreter.h:26