libdballe  9.14
wr_codec.h
1 #ifndef DBALLE_MSG_WR_CODEC_H
2 #define DBALLE_MSG_WR_CODEC_H
3 
4 #include <dballe/core/fwd.h>
5 #include <dballe/exporter.h>
6 #include <dballe/importer.h>
7 #include <dballe/msg/msg.h>
8 #include <functional>
9 #include <map>
10 #include <stdint.h>
11 #include <string>
12 #include <wreport/varinfo.h>
13 
14 namespace wreport {
15 struct Bulletin;
16 struct Subset;
17 } // namespace wreport
18 
19 namespace dballe {
20 namespace impl {
21 namespace msg {
22 
24 {
25 public:
27 
31  std::vector<std::shared_ptr<dballe::Message>>
32  from_bulletin(const wreport::Bulletin& msg) const override;
33 
48  const wreport::Bulletin& msg,
49  std::function<bool(std::shared_ptr<dballe::Message>)> dest) const;
50 };
51 
52 class BufrImporter : public WRImporter
53 {
54 public:
57  virtual ~BufrImporter();
58 
59  Encoding encoding() const override { return Encoding::BUFR; }
60 
61  bool foreach_decoded(const BinaryMessage& msg,
62  std::function<bool(std::shared_ptr<dballe::Message>)>
63  dest) const override;
64 };
65 
66 class CrexImporter : public WRImporter
67 {
68 public:
71  virtual ~CrexImporter();
72 
73  Encoding encoding() const override { return Encoding::CREX; }
74 
75  bool foreach_decoded(const BinaryMessage& msg,
76  std::function<bool(std::shared_ptr<dballe::Message>)>
77  dest) const override;
78 };
79 
80 namespace wr {
81 class Template;
82 }
83 
85 {
86 public:
88 
92  std::unique_ptr<wreport::Bulletin>
93  to_bulletin(const std::vector<std::shared_ptr<dballe::Message>>& msgs)
94  const override;
95 
99  std::unique_ptr<wr::Template> infer_template(const Messages& msgs) const;
100 };
101 
102 class BufrExporter : public WRExporter
103 {
104 public:
106  dballe::ExporterOptions::defaults);
107  virtual ~BufrExporter();
108 
109  std::string to_binary(const Messages& msgs) const override;
110  std::unique_ptr<wreport::Bulletin> make_bulletin() const override;
111 };
112 
113 class CrexExporter : public WRExporter
114 {
115 public:
117  dballe::ExporterOptions::defaults);
118  virtual ~CrexExporter();
119 
120  std::string to_binary(const Messages& msgs) const override;
121  std::unique_ptr<wreport::Bulletin> make_bulletin() const override;
122 };
123 
124 namespace wr {
125 
126 struct TemplateRegistry;
127 
128 class Template
129 {
130 protected:
131  virtual void setupBulletin(wreport::Bulletin& bulletin);
132  virtual void to_subset(const Message& msg, wreport::Subset& subset);
133 
134  void add(wreport::Varcode code, const msg::Context* ctx,
135  const Shortcut& shortcut) const;
136  void add(wreport::Varcode code, const msg::Context* ctx,
137  wreport::Varcode srccode) const;
138  void add(wreport::Varcode code, const msg::Context* ctx) const;
139  void add(wreport::Varcode code, const Values& values) const;
140  void add(wreport::Varcode code, const Values& values,
141  const Shortcut& shortcut) const;
142  void add(wreport::Varcode code, const Shortcut& shortcut) const;
143  void add(wreport::Varcode code, wreport::Varcode srccode,
144  const Level& level, const Trange& trange) const;
145  void add(wreport::Varcode code, const wreport::Var* var) const;
146  // Set station name, truncating it if it's too long
147  void do_station_name(wreport::Varcode dstcode) const;
148 
150  const wreport::Var* find_station_var(wreport::Varcode code) const;
151 
152  void do_ecmwf_past_wtr() const;
153  void do_station_height() const;
154  // WMO block and station numbers
155  void do_D01001() const;
156  void do_D01004() const;
157  // Date (year, month, day)
158  void do_D01011() const;
159  // Time (hour and minute), return the hour
160  int do_D01012() const;
161  // Time (hour, minute, second)
162  void do_D01013() const;
163  // Latitude and longitude, high accuracy
164  void do_D01021() const;
165  void do_D01022() const;
166  // Latitude and longitude, coarse accuracy
167  void do_D01023() const;
168 
169 public:
170  const dballe::ExporterOptions& opts;
171  const Messages& msgs;
172  const Message* msg = 0; // Message being read
173  const msg::Context* c_gnd_instant = 0;
174  wreport::Subset* subset = 0; // Subset being written
175 
176  Template(const dballe::ExporterOptions& opts, const Messages& msgs)
177  : opts(opts), msgs(msgs)
178  {
179  }
180  virtual ~Template() {}
181 
182  virtual const char* name() const = 0;
183  virtual const char* description() const = 0;
184  virtual void to_bulletin(wreport::Bulletin& bulletin);
185 };
186 
188 {
189  typedef std::function<std::unique_ptr<Template>(
190  const dballe::ExporterOptions& opts, const Messages& msgs)>
191  factory_func;
192 
193  unsigned data_category = MISSING_INT;
194  std::string name;
195  std::string description;
196  factory_func factory;
197 
198  TemplateFactory(unsigned data_category, std::string name,
199  std::string description, factory_func factory)
200  : data_category(data_category), name(name), description(description),
201  factory(factory)
202  {
203  }
204 };
205 
206 struct TemplateRegistry : public std::map<std::string, TemplateFactory>
207 {
208  static const TemplateRegistry& get();
209  static const TemplateFactory& get(const std::string& name);
210 
211  void register_factory(unsigned data_category, const std::string& name,
212  const std::string& desc,
213  TemplateFactory::factory_func fac);
214 };
215 
216 } // namespace wr
217 } // namespace msg
218 } // namespace impl
219 } // namespace dballe
220 #endif
std::unique_ptr< wreport::Bulletin > make_bulletin() const override
Create a bulletin that works with this exporter.
Binary message.
Definition: file.h:134
std::unique_ptr< wreport::Bulletin > to_bulletin(const std::vector< std::shared_ptr< dballe::Message >> &msgs) const override
Import a decoded BUFR/CREX message.
Definition: wr_codec.h:23
Definition: wr_codec.h:187
Definition: wr_codec.h:128
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:688
Definition: cmdline.h:18
Options to control message export.
Definition: exporter.h:24
Definition: importer.h:146
Definition: wr_codec.h:52
Definition: wr_codec.h:66
Options to control message import.
Definition: importer.h:24
Encoding encoding() const override
Return the encoding for this importer.
Definition: wr_codec.h:73
Vertical level or layer.
Definition: types.h:624
bool foreach_decoded_bulletin(const wreport::Bulletin &msg, std::function< bool(std::shared_ptr< dballe::Message >)> dest) const
Build Message objects a decoded bulletin, calling dest on each resulting Message. ...
Store an array of physical data all on the same level.
Definition: context.h:23
std::unique_ptr< wreport::Bulletin > make_bulletin() const override
Create a bulletin that works with this exporter.
Definition: wr_codec.h:102
const wreport::Var * find_station_var(wreport::Varcode code) const
Find a variable in c_station, or nullptr if not found.
Definition: shortcuts.h:11
static const ImporterOptions defaults
Default importer options.
Definition: importer.h:52
Definition: wr_codec.h:113
Definition: wr_codec.h:206
Definition: exporter.h:110
Storage for related physical data.
Definition: msg.h:133
std::unique_ptr< wr::Template > infer_template(const Messages &msgs) const
Infer a template name from the message contents.
Encoding encoding() const override
Return the encoding for this importer.
Definition: wr_codec.h:59
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:215
Definition: wr_codec.h:84
std::vector< std::shared_ptr< dballe::Message > > from_bulletin(const wreport::Bulletin &msg) const override
Import a decoded BUFR/CREX message.