Reading and decoding bulletins
#include <wreport/bulletin.h>
void read_bufr_raw(const Options& opts, const char* fname, RawHandler& handler)
{
FILE* in = fopen(fname, "rb");
if (in == NULL)
error_system::throwf("opening file %s", fname);
try {
std::string raw_data;
off_t offset;
while (BufrBulletin::read(in, raw_data, fname, &offset))
handler.handle_raw_bufr(raw_data, fname, offset);
fclose(in);
} catch (...) {
fclose(in);
throw;
}
}
void read_crex_raw(const Options& opts, const char* fname, RawHandler& handler)
{
FILE* in = fopen(fname, "rt");
if (in == NULL)
error_system::throwf("opening file %s", fname);
try {
unique_ptr<Bulletin> bulletin(CrexBulletin::create());
string raw_data;
off_t offset;
while (CrexBulletin::read(in, raw_data, fname, &offset))
handler.handle_raw_crex(raw_data, fname, offset);
fclose(in);
} catch (...) {
fclose(in);
throw;
}
}
String functions.
Definition: benchmark.h:13
Configuration variables to control configurable aspects of wreport's behaviour.
Creating bulletins
#include <wreport/bulletin.h>
#include <cstring>
void do_makebuoy()
{
unique_ptr<BufrBulletin> bulletin(BufrBulletin::create());
bulletin->edition_number = 4;
bulletin->data_category = 1;
bulletin->data_subcategory = 21;
bulletin->data_subcategory_local = 255;
bulletin->rep_year = 2011;
bulletin->rep_month = 10;
bulletin->rep_day = 3;
bulletin->rep_hour = 17;
bulletin->rep_minute = 0;
bulletin->rep_second = 0;
bulletin->originating_centre = 98;
bulletin->originating_subcentre = 0;
bulletin->master_table_version_number = 14;
bulletin->master_table_version_number_local = 0;
bulletin->compression = false;
bulletin->optional_section = "test";
bulletin->datadesc.push_back(
WR_VAR(3, 8, 3));
bulletin->load_tables();
Subset& s = bulletin->obtain_subset(0);
string encoded = bulletin->encode();
if (fwrite(encoded.data(), encoded.size(), 1, stdout) != 1)
perror("cannot write BUFR to standard output");
}
Represent a BUFR/CREX data subset as a list of decoded variables.
Definition: subset.h:13
void store_variable_i(Varcode code, int val)
Store a new variable in the message, providing its value as an int.
void store_variable_undef(Varcode code)
Store a new, undefined variable in the message.
void store_variable_d(Varcode code, double val)
Store a new variable in the message, providing its value as a double.
#define WR_VAR(f, x, y)
Create a WMO variable code from its F, X and Y components.
Definition: varinfo.h:67
Printing bulletin contents
#include <wreport/bulletin.h>
#include <wreport/bulletin/dds-scanfeatures.h>
#include <cstring>
struct PrintContents : public BulletinFullHandler
{
FILE* out;
PrintContents(FILE* out=stderr) : out(out) {}
{
}
};
struct PrintTrace : public BulletinFullHandler
{
FILE* out;
PrintTrace(FILE* out=stderr) : out(out) {}
void handle_raw_bufr(const std::string& raw_data, const char* fname, long offset) override
{
try {
handle(*bulletin);
} catch (std::exception& e) {
fprintf(stderr, "%s:%ld:%s\n", fname, offset, e.what());
}
}
};
struct PrintStructure : public BulletinFullHandler
{
FILE* out;
PrintStructure(FILE* out=stderr) : out(out) {}
{
}
};
struct PrintDDS : public BulletinHeadHandler
{
FILE* out;
PrintDDS(FILE* out=stderr) : out(out) {}
{
}
};
struct PrintTables : public BulletinHeadHandler
{
FILE* out;
bool header_printed;
PrintTables(FILE* out=stderr) : out(out), header_printed(false) {}
{
{
if (!header_printed)
{
fprintf(out,
"%-*s\tOffset\tCentre\tSubc.\tMaster\tLocal\n", (
int)b.
fname.size(),
"Filename");
header_printed = true;
}
fprintf(out, "%s\t%zd\t%d\t%d\t%d\t%d\n",
m->originating_centre, m->originating_subcentre,
m->master_table_version_number, m->master_table_version_number_local);
}
{
if (!header_printed)
{
fprintf(out, "Filename\tOffset\tMaster\tEdition\tTable\n");
header_printed = true;
}
fprintf(out, "%s\t%zd\t%d\t%d\t%d\n",
m->master_table_number, m->edition_number, m->master_table_version_number);
}
else
{
fprintf(out, "%s\t%zd\tunknown message type\n",
}
}
};
struct PrintFeatures : public BulletinHeadHandler
{
FILE* out;
PrintFeatures(FILE* out=stderr) : out(out) {}
{
scan.run();
bool first = true;
for (const auto& f: scan.features)
if (first)
{
fprintf(out, "%s", f.c_str());
first = false;
} else
fprintf(out, ",%s", f.c_str());
fprintf(out, "\n");
}
};
BUFR bulletin implementation.
Definition: bulletin.h:229
static std::unique_ptr< BufrBulletin > decode_verbose(const std::string &raw, FILE *out, const char *fname="(memory)", size_t offset=0)
Parse an encoded BUFR message, printing decoding information.
Storage for the decoded data of a BUFR or CREX message.
Definition: bulletin.h:30
void print_structured(FILE *out) const
Dump the contents of this bulletin, in a more structured way.
std::vector< Varcode > datadesc
Parsed data descriptor section.
Definition: bulletin.h:119
off_t offset
File offset of the start of the message.
Definition: bulletin.h:48
Tables tables
Varcode and opcode tables used for encoding or decoding.
Definition: bulletin.h:116
std::string fname
Input file name (optional).
Definition: bulletin.h:39
void print_datadesc(FILE *out, unsigned indent=0) const
Pretty-print the data descriptor section.
void print(FILE *out) const
Dump the contents of this bulletin.
CREX bulletin implementation.
Definition: bulletin.h:397
Interpreter that scans what features are used by a bulletin.
Definition: dds-scanfeatures.h:14
Printing library configuration
#include <cstdlib>
void do_info()
{
printf("Tables search paths (tried in order):\n");
printf("Extra tables directory: %s (env var WREPORT_EXTRA_TABLES)\n", getenv("WREPORT_EXTRA_TABLES"));
printf("System tables directory: %s (env var WREPORT_TABLES)\n", getenv("WREPORT_TABLES"));
printf("Compiled-in default tables directory: %s\n", TABLE_DIR);
}
Iterating bulletin contents
struct PrintVars : public BulletinFullHandler
{
FILE* out;
const std::vector<wreport::Varcode>& codes;
PrintVars(const std::vector<wreport::Varcode>& codes, FILE* out=stdout)
: out(out), codes(codes) {}
{
for (size_t i = 0; i < subset.size(); ++i)
if (subset[i].code() == code)
return &subset[i];
return NULL;
}
{
for (
size_t sset = 0; sset < b.
subsets.size(); ++sset)
{
fprintf(out,
"%s:%zd:", b.
fname.c_str(), sset + 1);
for (size_t i = 0; i < codes.size(); ++i)
{
const Var* var = find_varcode(b.
subsets[sset], codes[i]);
if (var)
{
string formatted = var->
format();
fprintf(out, "\t%s", formatted.c_str());
}
}
putc('\n', out);
}
}
};
std::vector< Subset > subsets
Decoded variables.
Definition: bulletin.h:122
A physical variable.
Definition: var.h:25
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: fwd.h:12