libdballe  7.29
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 #include <functional>
10 
11 namespace wreport {
12 struct Bulletin;
13 struct Subset;
14 }
15 
16 namespace dballe {
17 struct Msg;
18 
19 namespace msg {
20 struct Context;
21 
22 class WRImporter : public Importer
23 {
24 public:
25  WRImporter(const Options& opts);
26 
30  Messages from_bulletin(const wreport::Bulletin& msg) const override;
31 
44  bool foreach_decoded_bulletin(const wreport::Bulletin& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const;
45 };
46 
47 class BufrImporter : public WRImporter
48 {
49 public:
50  BufrImporter(const Options& opts=Options());
51  virtual ~BufrImporter();
52 
53  bool foreach_decoded(const BinaryMessage& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const override;
54 };
55 
56 class CrexImporter : public WRImporter
57 {
58 public:
59  CrexImporter(const Options& opts=Options());
60  virtual ~CrexImporter();
61 
62  bool foreach_decoded(const BinaryMessage& msg, std::function<bool(std::unique_ptr<Message>&&)> dest) const override;
63 };
64 
65 namespace wr {
66 class Template;
67 }
68 
69 class WRExporter : public Exporter
70 {
71 public:
72  WRExporter(const Options& opts);
73 
77  std::unique_ptr<wreport::Bulletin> to_bulletin(const Messages& msgs) const override;
78 
82  std::unique_ptr<wr::Template> infer_template(const Messages& msgs) const;
83 };
84 
85 class BufrExporter : public WRExporter
86 {
87 public:
88  BufrExporter(const Options& opts=Options());
89  virtual ~BufrExporter();
90 
91  virtual std::string to_binary(const Messages& msgs) const;
92  virtual std::unique_ptr<wreport::Bulletin> make_bulletin() const;
93 };
94 
95 class CrexExporter : public WRExporter
96 {
97 public:
98  CrexExporter(const Options& opts=Options());
99  virtual ~CrexExporter();
100 
101  virtual std::string to_binary(const Messages& msgs) const;
102  virtual std::unique_ptr<wreport::Bulletin> make_bulletin() const;
103 };
104 
105 namespace wr {
106 
107 struct TemplateRegistry;
108 
109 class Template
110 {
111 protected:
112  virtual void setupBulletin(wreport::Bulletin& bulletin);
113  virtual void to_subset(const Msg& msg, wreport::Subset& subset);
114 
115  void add(wreport::Varcode code, const msg::Context* ctx, int shortcut) const;
116  void add(wreport::Varcode code, const msg::Context* ctx, wreport::Varcode srccode) const;
117  void add(wreport::Varcode code, const msg::Context* ctx) const;
118  void add(wreport::Varcode code, int shortcut) const;
119  void add(wreport::Varcode code, wreport::Varcode srccode, const Level& level, const Trange& trange) const;
120  void add(wreport::Varcode code, const wreport::Var* var) const;
121  // Set station name, truncating it if it's too long
122  void do_station_name(wreport::Varcode dstcode) const;
123 
126 
127  void do_ecmwf_past_wtr() const;
128  void do_station_height() const;
129  // WMO block and station numbers
130  void do_D01001() const;
131  void do_D01004() const;
132  // Date (year, month, day)
133  void do_D01011() const;
134  // Time (hour and minute), return the hour
135  int do_D01012() const;
136  // Time (hour, minute, second)
137  void do_D01013() const;
138  // Latitude and longitude, high accuracy
139  void do_D01021() const;
140  void do_D01022() const;
141  // Latitude and longitude, coarse accuracy
142  void do_D01023() const;
143 
144 public:
145  const Exporter::Options& opts;
146  const Messages& msgs;
147  const Msg* msg = 0; // Msg being read
148  const msg::Context* c_station = 0;
149  const msg::Context* c_gnd_instant = 0;
150  wreport::Subset* subset = 0; // Subset being written
151 
152  Template(const Exporter::Options& opts, const Messages& msgs)
153  : opts(opts), msgs(msgs) {}
154  virtual ~Template() {}
155 
156  virtual const char* name() const = 0;
157  virtual const char* description() const = 0;
158  virtual void to_bulletin(wreport::Bulletin& bulletin);
159 };
160 
162 {
163  typedef std::function<std::unique_ptr<Template>(const Exporter::Options& opts, const Messages& msgs)> factory_func;
164 
165  unsigned data_category = MISSING_INT;
166  std::string name;
167  std::string description;
168  factory_func factory;
169 
170  TemplateFactory(unsigned data_category, std::string name, std::string description, factory_func factory)
171  : data_category(data_category), name(name), description(description), factory(factory) {}
172 };
173 
174 struct TemplateRegistry : public std::map<std::string, TemplateFactory>
175 {
176  static const TemplateRegistry& get();
177  static const TemplateFactory& get(const std::string& name);
178 
179  void register_factory(
180  unsigned data_category,
181  const std::string& name,
182  const std::string& desc,
183  TemplateFactory::factory_func fac);
184 };
185 
186 }
187 }
188 }
189 #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:161
Definition: codec.h:108
Definition: wr_codec.h:22
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:70
virtual std::string to_binary(const Messages &msgs) const
Encode a message.
Definition: codec.h:36
General codec options.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:587
Message importer.
Definition: codec.h:33
bool foreach_decoded_bulletin(const wreport::Bulletin &msg, std::function< bool(std::unique_ptr< Message > &&)> dest) const
Build Message objects a decoded bulletin, calling dest on each resulting Message. ...
Definition: wr_codec.h:174
Vertical level or layer.
Definition: types.h:532
Definition: wr_codec.h:109
uint16_t Varcode
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:132
Definition: wr_codec.h:56
Ordered collection of messages.
Definition: message.h:67
Definition: wr_codec.h:95
Definition: wr_codec.h:47
Definition: wr_codec.h:85
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:105
Definition: wr_codec.h:69
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.