libdballe  7.6
wr_codec.h
1 #ifndef DBALLE_MSG_WR_CODEC_H
2 #define DBALLE_MSG_WR_CODEC_H
3 
4 #include <dballe/msg/codec.h>
5 #include <wreport/varinfo.h>
6 #include <stdint.h>
7 #include <map>
8 #include <string>
9 
10 namespace wreport {
11 struct Bulletin;
12 struct Subset;
13 }
14 
15 namespace dballe {
16 struct Msg;
17 
18 namespace msg {
19 struct Context;
20 
21 class WRImporter : public Importer
22 {
23 public:
24  WRImporter(const Options& opts);
25 
29  Messages from_bulletin(const wreport::Bulletin& msg) const override;
30 
43  bool foreach_decoded_bulletin(const wreport::Bulletin& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const;
44 };
45 
46 class BufrImporter : public WRImporter
47 {
48 public:
49  BufrImporter(const Options& opts=Options());
50  virtual ~BufrImporter();
51 
52  bool foreach_decoded(const BinaryMessage& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const override;
53 };
54 
55 class CrexImporter : public WRImporter
56 {
57 public:
58  CrexImporter(const Options& opts=Options());
59  virtual ~CrexImporter();
60 
61  bool foreach_decoded(const BinaryMessage& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const override;
62 };
63 
64 namespace wr {
65 class Template;
66 }
67 
68 class WRExporter : public Exporter
69 {
70 public:
71  WRExporter(const Options& opts);
72 
76  std::unique_ptr<wreport::Bulletin> to_bulletin(const Messages& msgs) const override;
77 
81  std::unique_ptr<wr::Template> infer_template(const Messages& msgs) const;
82 };
83 
84 class BufrExporter : public WRExporter
85 {
86 public:
87  BufrExporter(const Options& opts=Options());
88  virtual ~BufrExporter();
89 
90  virtual std::string to_binary(const Messages& msgs) const;
91  virtual std::unique_ptr<wreport::Bulletin> make_bulletin() const;
92 };
93 
94 class CrexExporter : public WRExporter
95 {
96 public:
97  CrexExporter(const Options& opts=Options());
98  virtual ~CrexExporter();
99 
100  virtual std::string to_binary(const Messages& msgs) const;
101  virtual std::unique_ptr<wreport::Bulletin> make_bulletin() const;
102 };
103 
104 namespace wr {
105 
106 struct TemplateRegistry;
107 
108 class Template
109 {
110 protected:
111  virtual void setupBulletin(wreport::Bulletin& bulletin);
112  virtual void to_subset(const Msg& msg, wreport::Subset& subset);
113 
114  void add(wreport::Varcode code, const msg::Context* ctx, int shortcut) const;
115  void add(wreport::Varcode code, const msg::Context* ctx, wreport::Varcode srccode) const;
116  void add(wreport::Varcode code, const msg::Context* ctx) const;
117  void add(wreport::Varcode code, int shortcut) const;
118  void add(wreport::Varcode code, wreport::Varcode srccode, const Level& level, const Trange& trange) const;
119  void add(wreport::Varcode code, const wreport::Var* var) const;
120  // Set station name, truncating it if it's too long
121  void do_station_name(wreport::Varcode dstcode) const;
122 
124  const wreport::Var* find_station_var(wreport::Varcode code) const;
125 
126  void do_ecmwf_past_wtr() const;
127  void do_station_height() const;
128  // WMO block and station numbers
129  void do_D01001() const;
130  void do_D01004() const;
131  // Date (year, month, day)
132  void do_D01011() const;
133  // Time (hour and minute), return the hour
134  int do_D01012() const;
135  // Time (hour, minute, second)
136  void do_D01013() const;
137  // Latitude and longitude, high accuracy
138  void do_D01021() const;
139  void do_D01022() const;
140  // Latitude and longitude, coarse accuracy
141  void do_D01023() const;
142 
143 public:
144  const Exporter::Options& opts;
145  const Messages& msgs;
146  const Msg* msg = 0; // Msg being read
147  const msg::Context* c_station = 0;
148  const msg::Context* c_gnd_instant = 0;
149  wreport::Subset* subset = 0; // Subset being written
150 
151  Template(const Exporter::Options& opts, const Messages& msgs)
152  : opts(opts), msgs(msgs) {}
153  virtual ~Template() {}
154 
155  virtual const char* name() const = 0;
156  virtual const char* description() const = 0;
157  virtual void to_bulletin(wreport::Bulletin& bulletin);
158 };
159 
161 {
162  typedef std::function<std::unique_ptr<Template>(const Exporter::Options& opts, const Messages& msgs)> factory_func;
163 
164  unsigned data_category = MISSING_INT;
165  std::string name;
166  std::string description;
167  factory_func factory;
168 
169  TemplateFactory(unsigned data_category, std::string name, std::string description, factory_func factory)
170  : data_category(data_category), name(name), description(description), factory(factory) {}
171 };
172 
173 struct TemplateRegistry : public std::map<std::string, TemplateFactory>
174 {
175  static const TemplateRegistry& get();
176  static const TemplateFactory& get(const std::string& name);
177 
178  void register_factory(
179  unsigned data_category,
180  const std::string& name,
181  const std::string& desc,
182  TemplateFactory::factory_func fac);
183 };
184 
185 }
186 }
187 }
188 #endif
Store an array of physical data all on the same level.
Definition: context.h:44
bool foreach_decoded(const BinaryMessage &msg, std::function< bool(std::unique_ptr< Message > &&)> dest) const override
Decode a message from its raw encoded representation, calling dest on each resulting Message...
Definition: wr_codec.h:160
Definition: codec.h:107
Definition: wr_codec.h:21
virtual std::unique_ptr< wreport::Bulletin > make_bulletin() const
Create a bulletin that works with this exporter.
Storage for related physical data.
Definition: msg.h:133
virtual std::string to_binary(const Messages &msgs) const
Encode a message.
Definition: codec.h:35
General codec options.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
Message importer.
Definition: codec.h:32
bool foreach_decoded_bulletin(const wreport::Bulletin &msg, std::function< bool(std::unique_ptr< Message > &&)> dest) const
Decode a message from its decoded bulletin, calling dest on each resulting Msg.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: wr_codec.h:173
Vertical level or layer.
Definition: types.h:515
Definition: wr_codec.h:108
Messages from_bulletin(const wreport::Bulletin &msg) const override
Import a decoded BUFR/CREX message.
virtual std::unique_ptr< wreport::Bulletin > make_bulletin() const
Create a bulletin that works with this exporter.
Binary message.
Definition: file.h:131
Definition: wr_codec.h:55
Ordered collection of messages.
Definition: message.h:64
Definition: wr_codec.h:94
Definition: conversion.h:6
Definition: wr_codec.h:46
Definition: wr_codec.h:84
bool foreach_decoded(const BinaryMessage &msg, std::function< bool(std::unique_ptr< Message > &&)> dest) const override
Decode a message from its raw encoded representation, calling dest on each resulting Message...
std::unique_ptr< wreport::Bulletin > to_bulletin(const Messages &msgs) const override
Import a decoded BUFR/CREX message.
Message exporter.
Definition: codec.h:104
Definition: wr_codec.h:68
virtual std::string to_binary(const Messages &msgs) const
Encode a message.
const wreport::Var * find_station_var(wreport::Varcode code) const
Find a variable in c_station, or nullptr if not found.
std::unique_ptr< wr::Template > infer_template(const Messages &msgs) const
Infer a template name from the message contents.