Reading and decoding bulletins
void read_bufr(const Options& opts, const char* fname, BulletinHandler& handler, bool header_only=false)
{
FILE* in = fopen(fname, "rb");
if (in == NULL)
try {
string raw_data;
long offset;
{
if (header_only)
bulletin->decode_header(raw_data, fname, offset);
else
bulletin->decode(raw_data, fname, offset);
handler.handle(*bulletin);
}
fclose(in);
} catch (...) {
fclose(in);
throw;
}
}
void read_crex(const Options& opts, const char* fname, BulletinHandler& handler, bool header_only=false)
{
FILE* in = fopen(fname, "rt");
if (in == NULL)
try {
string raw_data;
long offset;
{
bulletin->decode(raw_data, fname, offset);
handler.handle(*bulletin);
}
fclose(in);
} catch (...) {
fclose(in);
throw;
}
}
Creating bulletins
#include <cstring>
void do_makebuoy()
{
auto_ptr<BufrBulletin> bulletin(BufrBulletin::create());
bulletin->edition = 4;
bulletin->type = 1;
bulletin->subtype = 21;
bulletin->localsubtype = 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->centre = 98;
bulletin->subcentre = 0;
bulletin->master_table = 14;
bulletin->local_table = 0;
bulletin->compression = 0;
bulletin->optional_section_length = 4;
bulletin->optional_section = new char[4];
memcpy(bulletin->optional_section, "test", 4);
bulletin->datadesc.push_back(
WR_VAR(3, 8, 3));
bulletin->load_tables();
Subset& s = bulletin->obtain_subset(0);
s.store_variable_i(
WR_VAR(0, 1, 5), 65602);
s.store_variable_d(
WR_VAR(0, 1, 12), 12.0);
s.store_variable_d(
WR_VAR(0, 1, 13), 0.2);
s.store_variable_i(
WR_VAR(0, 2, 1), 0);
s.store_variable_i(
WR_VAR(0, 4, 1), 2011);
s.store_variable_i(
WR_VAR(0, 4, 2), 10);
s.store_variable_i(
WR_VAR(0, 4, 3), 3);
s.store_variable_i(
WR_VAR(0, 4, 4), 17);
s.store_variable_i(
WR_VAR(0, 4, 5), 0);
s.store_variable_d(
WR_VAR(0, 5, 2), 59.03);
s.store_variable_d(
WR_VAR(0, 6, 2), -2.99);
s.store_variable_d(
WR_VAR(0, 10, 4), 99520.0);
s.store_variable_d(
WR_VAR(0, 10, 51), 99520.0);
s.store_variable_d(
WR_VAR(0, 10, 61), 310);
s.store_variable_i(
WR_VAR(0, 10, 63), 7);
s.store_variable_undef(
WR_VAR(0, 11, 11));
s.store_variable_undef(
WR_VAR(0, 11, 12));
s.store_variable_d(
WR_VAR(0, 12, 4), 278.5);
s.store_variable_undef(
WR_VAR(0, 12, 6));
s.store_variable_undef(
WR_VAR(0, 13, 3));
s.store_variable_undef(
WR_VAR(0, 20, 1));
s.store_variable_undef(
WR_VAR(0, 20, 3));
s.store_variable_undef(
WR_VAR(0, 20, 4));
s.store_variable_undef(
WR_VAR(0, 20, 5));
s.store_variable_undef(
WR_VAR(0, 20, 10));
s.store_variable_undef(
WR_VAR(0, 8, 2));
s.store_variable_undef(
WR_VAR(0, 20, 11));
s.store_variable_undef(
WR_VAR(0, 20, 13));
s.store_variable_undef(
WR_VAR(0, 20, 12));
s.store_variable_undef(
WR_VAR(0, 20, 12));
s.store_variable_undef(
WR_VAR(0, 20, 12));
s.store_variable_d(
WR_VAR(0, 22, 42), 280.8);
string encoded;
bulletin->encode(encoded);
if (fwrite(encoded.data(), encoded.size(), 1, stdout) != 1)
perror("cannot write BUFR to standard output");
}
Printing bulletin contents
struct PrintContents : public BulletinHandler
{
FILE* out;
PrintContents(FILE* out=stderr) : out(out) {}
{
}
};
struct PrintStructure : public BulletinHandler
{
FILE* out;
PrintStructure(FILE* out=stderr) : out(out) {}
{
}
};
struct PrintDDS : public BulletinHandler
{
FILE* out;
PrintDDS(FILE* out=stderr) : out(out) {}
{
}
};
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 BulletinHandler
{
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, 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);
}
}
};